PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/payment_utility.php

http://github.com/calvinfroedge/PHP-Payments
PHP | 253 lines | 170 code | 25 blank | 58 comment | 32 complexity | 714cc9f83c3ae0419c89e133369dcbec MD5 | raw file
  1. <?php
  2. class Payment_Utility
  3. {
  4. public function __construct(){}
  5. /*
  6. * Checks classes which are attempted to autoload, ensures they are not ignored (ie to prevent conflict with frameworks)
  7. */
  8. public static $autoload_ignore = array();
  9. /**
  10. * Autoloader. Allows us to call classes without a require or include statement - lookups are referred here
  11. */
  12. public function class_autoload($class)
  13. {
  14. //If a class name is not going to match, don't bother looking for it - we'll just end up with an exception
  15. if(strpos($class, 'Payment') === false && strpos($class, 'Driver') === false && strpos($class, 'Method') === false) return;
  16. //Ignore classes that should be ignored
  17. foreach(static::$autoload_ignore as $ignore)
  18. {
  19. if(strpos($class, $ignore) !== false) return;
  20. }
  21. $class = strtolower($class);
  22. $base_dir = __DIR__.'/';
  23. if(file_exists($base_dir.$class.'.php'))
  24. {
  25. include_once($base_dir.$class.'.php');
  26. return;
  27. }
  28. else if(file_exists($base_dir.'payment_drivers/'.$class.'.php'))
  29. {
  30. include_once($base_dir.'payment_drivers/'.$class.'.php');
  31. return;
  32. }
  33. else if(file_exists($base_dir.'payment_methods/'.$class.'.php'))
  34. {
  35. include_once($base_dir.'payment_methods/'.$class.'.php');
  36. return;
  37. }
  38. else
  39. {
  40. throw new Exception("Could not find class");
  41. }
  42. }
  43. /**
  44. * Load a resource. Alternative to include / require / etc. Passing a key will return a specific entry in a config / lang array.
  45. */
  46. public static function load($type, $file, $key = null)
  47. {
  48. $base_dir = dirname(__DIR__);
  49. switch($type)
  50. {
  51. case $type == 'config':
  52. $ob = false;
  53. $path = $base_dir.'/config/'.$file.'.php';
  54. break;
  55. case $type == 'file':
  56. $ob = true;
  57. $path = $base_dir.'/'.$file.'.php';
  58. break;
  59. case $type == 'lang':
  60. $ob = false;
  61. $path = $base_dir.'/language/'.$file.'_lang.php';
  62. break;
  63. default:
  64. die("$type is not a valid filetype to load for Payments");
  65. }
  66. if(!is_file($path)) die("$path does not exist.");
  67. if($ob)
  68. {
  69. ob_start();
  70. include_once($path);
  71. return ob_get_clean();
  72. }
  73. else
  74. {
  75. $f = include $path;
  76. return (isset($f[$key])) ? $f[$key] : $f;
  77. }
  78. }
  79. /**
  80. * Loads all files in a particular directory
  81. *
  82. * @param string A dir to load from
  83. */
  84. public static function load_all_files($dir)
  85. {
  86. $base_dir = dirname(__DIR__);
  87. foreach(scandir($base_dir.'/'.$dir) as $k=>$v){
  88. //Ignore swap files, directory files, etc.
  89. if($v[0] !== '.' && (substr($v, -3, 3) == 'php') )
  90. {
  91. $file = str_replace('.php', '', $v);
  92. self::load('file', $dir.'/'.$file);
  93. }
  94. }
  95. }
  96. /**
  97. * Arrayize an object
  98. *
  99. * @param object the object to convert to an array
  100. * @return array a converted array
  101. */
  102. public static function arrayize_object($input)
  103. {
  104. if(!is_object($input))
  105. {
  106. return $input;
  107. }
  108. else
  109. {
  110. $final = array();
  111. $vars = get_object_vars($input);
  112. foreach($vars as $k=>$v)
  113. {
  114. if(is_object($v))
  115. {
  116. $final[$k] = self::arrayize_object($v);
  117. }
  118. else
  119. {
  120. $final[$k] = $v;
  121. }
  122. }
  123. }
  124. return $final;
  125. }
  126. /**
  127. * Sort an array by an array. Modified example from StackOverflow: http://stackoverflow.com/questions/348410/sort-an-array-based-on-another-array
  128. *
  129. * @param array An array to sort
  130. * @param array An array to sort by
  131. * @return array A sorted array
  132. */
  133. public static function sort_array_by_array($array, $order) {
  134. $ordered = array();
  135. foreach($order as $key) {
  136. if(array_key_exists($key,$array)) {
  137. $ordered[$key] = $array[$key];
  138. unset($array[$key]);
  139. }
  140. }
  141. return $ordered;
  142. }
  143. /**
  144. * Parses an XML response and creates an object using SimpleXML
  145. *
  146. * @param string raw xml string
  147. * @return object response SimpleXMLElement object
  148. */
  149. public static function parse_xml($xml_str)
  150. {
  151. $xml_str = trim($xml_str);
  152. $xml_str = preg_replace('/xmlns="(.+?)"/', '', $xml_str);
  153. if($xml_str[0] != '<')
  154. {
  155. $xml_str = explode('<', $xml_str);
  156. if(count($xml_str) > 1)
  157. {
  158. unset($xml_str[0]);
  159. $xml_str = '<'.implode('<', $xml_str);
  160. }
  161. else
  162. {
  163. $xml_str = $xml_str[0];
  164. }
  165. }
  166. try {
  167. $xml = @new SimpleXMLElement($xml_str);
  168. }
  169. catch(Exception $e) {
  170. return Payment_Response::instance()->local_response(
  171. 'failure',
  172. 'invalid_xml',
  173. $xml_str
  174. );
  175. }
  176. return $xml;
  177. }
  178. /**
  179. * Sanitizes XML params so they will not cause parsing errors on remote end
  180. *
  181. * @param array Reference to XML params
  182. */
  183. public static function sanitize_xml_params(&$params)
  184. {
  185. if(!function_exists('array_walk_sanitize_callback'))
  186. {
  187. function array_walk_sanitize_callback(&$v, $k)
  188. {
  189. if(strpos($v, '&') !== false) $v = str_replace('&', '&#x26;', $v);
  190. if(strpos($v, '<') !== false) $v = str_replace('<', '&#x3c;', $v);
  191. if(strpos($v, '>') !== false) $v = str_replace('>', '&#x3e;', $v);
  192. }
  193. }
  194. array_walk_recursive($params, 'array_walk_sanitize_callback');
  195. }
  196. /**
  197. * Connection is Secure
  198. *
  199. * Checks whether current connection is secure and will redirect
  200. * to secure version of page if 'force_secure_connection' is TRUE
  201. *
  202. * To Force HTTPS for your entire website, use a .htaccess like the following:
  203. *
  204. * RewriteEngine On
  205. * RewriteCond %{SERVER_PORT} 80
  206. * RewriteRule ^(.*)$ https://domain.com/$1 [R,L]
  207. *
  208. * @link http://davidwalsh.name/force-secure-ssl-htaccess
  209. * @return bool
  210. */
  211. public static function connection_is_secure($config)
  212. {
  213. // Check whether secure connection is required
  214. if($config['force_secure_connection'] === FALSE)
  215. {
  216. error_log('WARNING!! Using Payment Gateway without Secure Connection!', 0);
  217. return false;
  218. }
  219. // Redirect if NOT secure and forcing a secure connection.
  220. if(($_SERVER['SERVER_PORT'] === '443' && isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') === FALSE)
  221. {
  222. $loc = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  223. header($loc);
  224. exit;
  225. }
  226. return true;
  227. }
  228. }