utils.inc.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. define('CONFIGFILE', '../config.txt');
  3. class utils
  4. {
  5. private static $m_aConfig = null;
  6. public static function ReadParam($sName, $defaultValue = "")
  7. {
  8. return isset($_REQUEST[$sName]) ? $_REQUEST[$sName] : $defaultValue;
  9. }
  10. public static function ReadPostedParam($sName, $defaultValue = "")
  11. {
  12. return isset($_POST[$sName]) ? $_POST[$sName] : $defaultValue;
  13. }
  14. public static function GetNewTransactionId()
  15. {
  16. // TO DO implement the real mechanism here
  17. return sprintf("%08x", rand(0,2000000000));
  18. }
  19. public static function IsTransactionValid($sId)
  20. {
  21. // TO DO implement the real mechanism here
  22. return true;
  23. }
  24. public static function ReadFromFile($sFileName)
  25. {
  26. if (!file_exists($sFileName)) return false;
  27. return file_get_contents($sFileName);
  28. }
  29. public static function ReadConfig()
  30. {
  31. self::$m_aConfig = array();
  32. $sConfigContents = self::ReadFromFile(CONFIGFILE);
  33. if (!$sConfigContents) throw new Exception("Could not load file ".CONFIGFILE);
  34. foreach (explode("\n", $sConfigContents) as $sLine)
  35. {
  36. $sLine = trim($sLine);
  37. if (($iPos = strpos($sLine, '#')) !== false)
  38. {
  39. // strip out the end of the line right after the #
  40. $sLine = substr($sLine, 0, $iPos);
  41. }
  42. $aMatches = array();
  43. if (preg_match("@(\\S+.*)=\s*(\S+.*)@", $sLine, $aMatches))
  44. {
  45. $sParamName = trim($aMatches[1]);
  46. $sParamValue = trim($aMatches[2]);
  47. self::$m_aConfig[$sParamName] = $sParamValue;
  48. }
  49. }
  50. }
  51. public static function GetConfig($sParamName, $defaultValue = "")
  52. {
  53. if (is_null(self::$m_aConfig))
  54. {
  55. self::ReadConfig();
  56. }
  57. if (array_key_exists($sParamName, self::$m_aConfig))
  58. {
  59. return self::$m_aConfig[$sParamName];
  60. }
  61. else
  62. {
  63. return $defaultValue;
  64. }
  65. }
  66. }
  67. ?>