tpayUtil

Class Util.

Defined (1)

The class is defined in the following location(s).

/includes/lib/src/_class_tpay/util.php  
  1. class Util 
  2. const REMOTE_ADDR = 'REMOTE_ADDR'; 
  3.  
  4. /** 
  5. * Parse template file 
  6. * @param string $templateFileName filename 
  7. * @return string 
  8. */ 
  9. public static function parseTemplate($templateFileName, $data = array()) 
  10. $templateDirectory = dirname(__FILE__) . '/../'; 
  11. $buffer = false; 
  12.  
  13. if (ob_get_length() > 0) { 
  14. $buffer = ob_get_contents(); 
  15. ob_clean(); 
  16. ob_start(); 
  17.  
  18. if (!file_exists($templateDirectory . $templateFileName . '.phtml')) { 
  19. return ''; 
  20. include_once $templateDirectory . $templateFileName . '.phtml'; 
  21. $parsedHTML = ob_get_contents(); 
  22. ob_clean(); 
  23.  
  24. if ($buffer !== false) { 
  25. ob_start(); 
  26. echo $buffer; 
  27.  
  28. return $parsedHTML; 
  29.  
  30. /** 
  31. * Checking installed PHP version 
  32. * @throws TException 
  33. */ 
  34. public static function checkVersionPHP() 
  35. if (version_compare(phpversion(), '5.3.0', '<')) { 
  36. throw new TException(sprintf('Your PHP version is too old, please upgrade to a newer version. 
  37. Your version is %s, library requires %s', phpversion(), '5.3.0')); 
  38.  
  39. /** 
  40. * Require PHP file 
  41. * @param string $name file name without php extension 
  42. * @throws TException 
  43. */ 
  44. public static function loadClass($name) 
  45. $classDirectory = dirname(__FILE__) . '/../_class_tpay/'; 
  46. $filePath = $classDirectory . $name . '.php'; 
  47.  
  48. if (!file_exists($classDirectory)) { 
  49.  
  50. throw new TException('directory not found (' . $classDirectory . ')'); 
  51. if (!file_exists($filePath)) { 
  52. throw new TException('no such a file (' . $filePath . ')'); 
  53. require_once($filePath); 
  54.  
  55. /** 
  56. * Save text to log file with details 
  57. * @param string $title action name 
  58. * @param string $text text to save 
  59. */ 
  60. public static function log($title, $text) 
  61. $text = (string)$text; 
  62. $logFilePath = dirname(__FILE__) . '/../log'; 
  63.  
  64. $ip = (isset($_SERVER[static::REMOTE_ADDR])) ? $_SERVER[static::REMOTE_ADDR] : ''; 
  65.  
  66. $logText = "\n==========================="; 
  67. $logText .= "\n" . $title; 
  68. $logText .= "\n==========================="; 
  69. $logText .= "\n" . date('Y-m-d H:i:s'); 
  70. $logText .= "\nip: " . $ip; 
  71. $logText .= "\n"; 
  72. $logText .= $text; 
  73. $logText .= "\n\n"; 
  74.  
  75. if (file_exists($logFilePath) && is_writable($logFilePath)) { 
  76. file_put_contents($logFilePath, $logText, FILE_APPEND); 
  77.  
  78. /** 
  79. * Save one line to log file 
  80. * @param string $text text to save 
  81. */ 
  82. public static function logLine($text) 
  83. $text = (string)$text; 
  84. $logFilePath = dirname(__FILE__) . '/../log'; 
  85. if (file_exists($logFilePath) && is_writable($logFilePath)) { 
  86. file_put_contents($logFilePath, "\n" . $text, FILE_APPEND); 
  87.  
  88. /** 
  89. * Get value from $_POST array. 
  90. * If not exists return false 
  91. * @param string $name 
  92. * @param string $type variable type 
  93. * @return mixed 
  94. * @throws TException 
  95. */ 
  96. public static function post($name, $type) 
  97. if (!isset($_POST[$name])) { 
  98. return false; 
  99. $val = $_POST[$name]; 
  100. if ($type === 'int') { 
  101. $val = (int)$val; 
  102. } elseif ($type === 'float') { 
  103. $val = (float)$val; 
  104. } elseif ($type === 'string') { 
  105. $val = (string)$val; 
  106. } else { 
  107. throw new TException('Undefined $_POST variable type'); 
  108.  
  109. return $val; 
  110.  
  111. /** 
  112. * Get substring by pattern 
  113. * @param string $pattern pattern 
  114. * @param string $string content 
  115. * @return string 
  116. */ 
  117. public static function findSubstring($pattern, $string) 
  118. preg_match_all($pattern, $string, $matches); 
  119. if (isset($matches[1]) && isset($matches[1][0])) { 
  120. return $matches[1][0]; 
  121. return '';