Guest User

Untitled

a guest
Sep 21st, 2018
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. db_utils.inc
  5. Collection of database-related function used
  6. by DRI applications.
  7.  
  8. Written: 03/30/2012, Nicholas Kreidberg
  9. Revised: 03/30/2012, Nicholas Kreidberg
  10.  
  11. */
  12.  
  13. function fnDB_Open($sPKG, $sPerm, $sScope)
  14. {
  15. /*
  16. Function used to open and return database
  17. handles. All parameters are required in
  18. calls to function and if the function_exists
  19. encounters any problems it returns NULL
  20. so it is up the caller to check for return
  21. values and handle things accordingly.
  22.  
  23. Call: $hDB = fnDB_Open("PIP", "RO", "Internal");
  24. */
  25.  
  26. if(empty($sPKG) || empty($sPerm) || empty($sScope))
  27. return NULL;
  28.  
  29. $aConstants = get_defined_constants();
  30.  
  31. // Convert scope parameter to upper case
  32. $sScope = strtoupper($sScope);
  33.  
  34. if($sScope === "INT" && empty($aConstants['sDB_SCOPE_INT'])) {
  35. // Include internal credentials
  36. require_once("dbauth_int.inc");
  37. }
  38. else if($sScope === "PUB" && empty($aConstants['sDB_SCOPE_PUB'])) {
  39. // Include public credentials
  40. require_once("dbauth_pub.inc");
  41. }
  42. else {
  43. // Invalid value passed for this parameter
  44. if(empty($aConstants['sDB_SCOPE_INT']) && empty($aConstants['sDB_SCOPE_PUB']))
  45. return NULL;
  46. }
  47.  
  48. $sDB_Prefix = sprintf("sDB_%s_%s", $sPKG, $sPerm);
  49.  
  50. $sHost = constant($sDB_Prefix."_HOST"."_$sScope");
  51. $sPort = constant($sDB_Prefix."_PORT"."_$sScope");
  52. $sUser = constant($sDB_Prefix."_USER"."_$sScope");
  53. $sPW = constant($sDB_Prefix."_PW"."_$sScope");
  54. $sPW = base64_decode($sPW);
  55. $sName = constant($sDB_Prefix."_NAME"."_$sScope");
  56.  
  57. ($hDB = pg_connect("host=$sHost port=$sPort user=$sUser password=$sPW dbname=$sName")) or
  58. die("Failed to connect to the database $sPKG!");
  59.  
  60. return $hDB;
  61. }
  62. ?>
Add Comment
Please, Sign In to add comment