PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/functions/general.php

https://github.com/Jon86/oscommerce
PHP | 520 lines | 282 code | 86 blank | 152 comment | 93 complexity | ea3b9b4f485464f7c9bdfb9028daf2b4 MD5 | raw file
  1. <?php
  2. /*
  3. osCommerce Online Merchant $osCommerce-SIG$
  4. Copyright (c) 2009 osCommerce (http://www.oscommerce.com)
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License v2 (1991)
  7. as published by the Free Software Foundation.
  8. */
  9. /**
  10. * Redirect to a URL address
  11. *
  12. * @param string $url The URL address to redirect to
  13. * @access public
  14. */
  15. function osc_redirect($url) {
  16. global $osC_Services;
  17. if ( ( strpos($url, "\n") !== false ) || ( strpos($url, "\r") !== false ) ) {
  18. $url = osc_href_link(FILENAME_DEFAULT, null, 'NONSSL', false);
  19. }
  20. if ( strpos($url, '&amp;') !== false ) {
  21. $url = str_replace('&amp;', '&', $url);
  22. }
  23. header('Location: ' . $url);
  24. if ( isset($osC_Services) && is_a($osC_Services, 'osC_Services') ) {
  25. $osC_Services->stopServices();
  26. }
  27. exit;
  28. }
  29. /**
  30. * Parse and output a user submited value
  31. *
  32. * @param string $string The string to parse and output
  33. * @param array $translate An array containing the characters to parse
  34. * @access public
  35. */
  36. function osc_output_string($string, $translate = null) {
  37. if (empty($translate)) {
  38. $translate = array('"' => '&quot;');
  39. }
  40. return strtr(trim($string), $translate);
  41. }
  42. /**
  43. * Strictly parse and output a user submited value
  44. *
  45. * @param string $string The string to strictly parse and output
  46. * @access public
  47. */
  48. function osc_output_string_protected($string) {
  49. return htmlspecialchars(trim($string));
  50. }
  51. /**
  52. * Sanitize a user submited value
  53. *
  54. * @param string $string The string to sanitize
  55. * @access public
  56. */
  57. function osc_sanitize_string($string) {
  58. $patterns = array('/ +/', '/[<>]/');
  59. $replace = array(' ', '_');
  60. return preg_replace($patterns, $replace, trim($string));
  61. }
  62. /**
  63. * Get all parameters in the GET scope
  64. *
  65. * @param array $exclude A list of parameters to exclude
  66. * @access public
  67. */
  68. function osc_get_all_get_params($exclude = null) {
  69. global $osC_Session;
  70. $params = '';
  71. $array = array($osC_Session->getName(),
  72. 'error',
  73. 'x',
  74. 'y');
  75. if (is_array($exclude)) {
  76. foreach ($exclude as $key) {
  77. if (!in_array($key, $array)) {
  78. $array[] = $key;
  79. }
  80. }
  81. }
  82. if (isset($_GET) && !empty($_GET)) {
  83. foreach ($_GET as $key => $value) {
  84. if (!in_array($key, $array)) {
  85. $params .= $key . (!empty($value) ? '=' . $value : '') . '&';
  86. }
  87. }
  88. $params = substr($params, 0, -1);
  89. }
  90. return $params;
  91. }
  92. /**
  93. * Return a language definition
  94. *
  95. * @param string $key The language definition to return
  96. * @param int $precision The precision to use for the rounding
  97. * @return string The language definition
  98. * @access public
  99. */
  100. function __($key) {
  101. global $osC_Language;
  102. return $osC_Language->get($key);
  103. }
  104. /**
  105. * Round a number with the wanted precision
  106. *
  107. * @param float $number The number to round
  108. * @param int $precision The precision to use for the rounding
  109. * @access public
  110. */
  111. function osc_round($number, $precision) {
  112. if ( (strpos($number, '.') !== false) && (strlen(substr($number, strpos($number, '.')+1)) > $precision) ) {
  113. $number = substr($number, 0, strpos($number, '.') + 1 + $precision + 1);
  114. if (substr($number, -1) >= 5) {
  115. if ($precision > 1) {
  116. $number = substr($number, 0, -1) + ('0.' . str_repeat(0, $precision-1) . '1');
  117. } elseif ($precision == 1) {
  118. $number = substr($number, 0, -1) + 0.1;
  119. } else {
  120. $number = substr($number, 0, -1) + 1;
  121. }
  122. } else {
  123. $number = substr($number, 0, -1);
  124. }
  125. }
  126. return $number;
  127. }
  128. /**
  129. * Create a sort heading with appropriate sort link
  130. *
  131. * @param string $key The key used for sorting
  132. * @param string $heading The heading to use the link on
  133. * @access public
  134. */
  135. function osc_create_sort_heading($key, $heading) {
  136. global $osC_Language;
  137. $current = false;
  138. $direction = false;
  139. if (!isset($_GET['sort'])) {
  140. $current = 'name';
  141. } elseif (($_GET['sort'] == $key) || ($_GET['sort'] == $key . '|d')) {
  142. $current = $key;
  143. }
  144. if ($key == $current) {
  145. if (isset($_GET['sort'])) {
  146. $direction = ($_GET['sort'] == $key) ? '+' : '-';
  147. } else {
  148. $direction = '+';
  149. }
  150. }
  151. return osc_link_object(osc_href_link(basename($_SERVER['SCRIPT_FILENAME']), osc_get_all_get_params(array('page', 'sort')) . '&sort=' . $key . ($direction == '+' ? '|d' : '')), $heading . (($key == $current) ? $direction : ''), 'title="' . (isset($_GET['sort']) && ($_GET['sort'] == $key) ? sprintf($osC_Language->get('listing_sort_ascendingly'), $heading) : sprintf($osC_Language->get('listing_sort_descendingly'), $heading)) . '" class="productListing-heading"');
  152. }
  153. /**
  154. * Generate a product ID string value containing its product attributes combinations
  155. *
  156. * @param string $id The product ID
  157. * @param array $params An array of product attributes
  158. * @access public
  159. */
  160. function osc_get_product_id_string($id, $params) {
  161. $string = (int)$id;
  162. if (is_array($params) && !empty($params)) {
  163. $attributes_check = true;
  164. $attributes_ids = array();
  165. foreach ($params as $option => $value) {
  166. if (is_numeric($option) && is_numeric($value)) {
  167. $attributes_ids[] = (int)$option . ':' . (int)$value;
  168. } else {
  169. $attributes_check = false;
  170. break;
  171. }
  172. }
  173. if ($attributes_check === true) {
  174. $string .= '#' . implode(';', $attributes_ids);
  175. }
  176. }
  177. return $string;
  178. }
  179. /**
  180. * Generate a numeric product ID without product attribute combinations
  181. *
  182. * @param string $id The product ID
  183. * @access public
  184. */
  185. function osc_get_product_id($id) {
  186. if (is_numeric($id)) {
  187. return $id;
  188. }
  189. $product = explode('#', $id, 2);
  190. return (int)$product[0];
  191. }
  192. /**
  193. * Send an email
  194. *
  195. * @param string $to_name The name of the recipient
  196. * @param string $to_email_address The email address of the recipient
  197. * @param string $subject The subject of the email
  198. * @param string $body The body text of the email
  199. * @param string $from_name The name of the sender
  200. * @param string $from_email_address The email address of the sender
  201. * @access public
  202. */
  203. function osc_email($to_name, $to_email_address, $subject, $body, $from_name, $from_email_address) {
  204. if (SEND_EMAILS == '-1') {
  205. return false;
  206. }
  207. $osC_Mail = new osC_Mail($to_name, $to_email_address, $from_name, $from_email_address, $subject);
  208. $osC_Mail->setBodyPlain($body);
  209. $osC_Mail->send();
  210. }
  211. /**
  212. * Create a random string
  213. *
  214. * @param int $length The length of the random string to create
  215. * @param string $type The type of random string to create (mixed, chars, digits)
  216. * @access public
  217. */
  218. function osc_create_random_string($length, $type = 'mixed') {
  219. if (!in_array($type, array('mixed', 'chars', 'digits'))) {
  220. return false;
  221. }
  222. $chars_pattern = 'abcdefghijklmnopqrstuvwxyz';
  223. $mixed_pattern = '1234567890' . $chars_pattern;
  224. $rand_value = '';
  225. while (strlen($rand_value) < $length) {
  226. if ($type == 'digits') {
  227. $rand_value .= osc_rand(0,9);
  228. } elseif ($type == 'chars') {
  229. $rand_value .= substr($chars_pattern, osc_rand(0, 25), 1);
  230. } else {
  231. $rand_value .= substr($mixed_pattern, osc_rand(0, 35), 1);
  232. }
  233. }
  234. return $rand_value;
  235. }
  236. /**
  237. * Alias function for empty()
  238. *
  239. * @param mixed $value The object to check if it is empty or not
  240. * @access public
  241. */
  242. function osc_empty($value) {
  243. return empty($value);
  244. }
  245. /**
  246. * Generate a random number
  247. *
  248. * @param int $min The minimum number to return
  249. * @param int $max The maxmimum number to return
  250. * @access public
  251. */
  252. function osc_rand($min = null, $max = null) {
  253. static $seeded;
  254. if (!isset($seeded)) {
  255. if (version_compare(PHP_VERSION, '4.2', '<')) {
  256. mt_srand((double)microtime()*1000000);
  257. }
  258. $seeded = true;
  259. }
  260. if (is_numeric($min) && is_numeric($max)) {
  261. if ($min >= $max) {
  262. return $min;
  263. } else {
  264. return mt_rand($min, $max);
  265. }
  266. } else {
  267. return mt_rand();
  268. }
  269. }
  270. /**
  271. * Set a cookie
  272. *
  273. * @param string $name The name of the cookie
  274. * @param string $value The value of the cookie
  275. * @param int $expire Unix timestamp of when the cookie should expire
  276. * @param string $path The path on the server for which the cookie will be available on
  277. * @param string $domain The The domain that the cookie is available on
  278. * @param boolean $secure Indicates whether the cookie should only be sent over a secure HTTPS connection
  279. * @param boolean $httpOnly Indicates whether the cookie should only accessible over the HTTP protocol
  280. * @access public
  281. */
  282. function osc_setcookie($name, $value = null, $expires = 0, $path = null, $domain = null, $secure = false, $httpOnly = false) {
  283. global $request_type;
  284. if (empty($path)) {
  285. $path = ($request_type == 'NONSSL') ? HTTP_COOKIE_PATH : HTTPS_COOKIE_PATH;
  286. }
  287. if (empty($domain)) {
  288. $domain = ($request_type == 'NONSSL') ? HTTP_COOKIE_DOMAIN : HTTPS_COOKIE_DOMAIN;
  289. }
  290. header('Set-Cookie: ' . $name . '=' . urlencode($value) . '; expires=' . date('D, d-M-Y H:i:s T', $expires) . '; path=' . $path . '; domain=' . $domain . (($secure === true) ? ' secure;' : '') . (($httpOnly === true) ? ' httponly;' : ''));
  291. }
  292. /**
  293. * Get the IP address of the client
  294. *
  295. * @access public
  296. */
  297. function osc_get_ip_address() {
  298. if (isset($_SERVER)) {
  299. if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  300. $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  301. } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
  302. $ip = $_SERVER['HTTP_CLIENT_IP'];
  303. } else {
  304. $ip = $_SERVER['REMOTE_ADDR'];
  305. }
  306. } else {
  307. if (getenv('HTTP_X_FORWARDED_FOR')) {
  308. $ip = getenv('HTTP_X_FORWARDED_FOR');
  309. } elseif (getenv('HTTP_CLIENT_IP')) {
  310. $ip = getenv('HTTP_CLIENT_IP');
  311. } else {
  312. $ip = getenv('REMOTE_ADDR');
  313. }
  314. }
  315. return $ip;
  316. }
  317. /**
  318. * Encrypt a string
  319. *
  320. * @param string $plain The string to encrypt
  321. * @access public
  322. */
  323. function osc_encrypt_string($plain) {
  324. $password = '';
  325. for ($i=0; $i<10; $i++) {
  326. $password .= osc_rand();
  327. }
  328. $salt = substr(md5($password), 0, 2);
  329. $password = md5($salt . $plain) . ':' . $salt;
  330. return $password;
  331. }
  332. /**
  333. * Validates the format of an email address
  334. *
  335. * @param string $email_address The email address to validate
  336. * @access public
  337. */
  338. function osc_validate_email_address($email_address) {
  339. $valid_address = true;
  340. $mail_pat = '/^(.+)@(.+)$/i';
  341. $valid_chars = "[^] \(\)<>@,;:\.\\\"\[]";
  342. $atom = "$valid_chars+";
  343. $quoted_user='(\"[^\"]*\")';
  344. $word = "($atom|$quoted_user)";
  345. $user_pat = "/^$word(\.$word)*$/i";
  346. $ip_domain_pat='/^\[([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\]$/i';
  347. $domain_pat = "/^$atom(\.$atom)*$/i";
  348. if (preg_match($mail_pat, $email_address, $components)) {
  349. $user = $components[1];
  350. $domain = $components[2];
  351. // validate user
  352. if (preg_match($user_pat, $user)) {
  353. // validate domain
  354. if (preg_match($ip_domain_pat, $domain, $ip_components)) {
  355. // this is an IP address
  356. for ($i=1;$i<=4;$i++) {
  357. if ($ip_components[$i] > 255) {
  358. $valid_address = false;
  359. break;
  360. }
  361. }
  362. } else {
  363. // Domain is a name, not an IP
  364. if (preg_match($domain_pat, $domain)) {
  365. // domain name seems valid, but now make sure that it ends in a valid TLD or ccTLD and that there's a hostname preceding the domain or country.
  366. $domain_components = explode(".", $domain);
  367. // Make sure there's a host name preceding the domain.
  368. if (sizeof($domain_components) < 2) {
  369. $valid_address = false;
  370. } else {
  371. $top_level_domain = strtolower($domain_components[sizeof($domain_components)-1]);
  372. // Allow all 2-letter TLDs (ccTLDs)
  373. if (preg_match('/^[a-z][a-z]$/i', $top_level_domain) != 1) {
  374. $tld_pattern = '';
  375. // Get authorized TLDs from text file
  376. $tlds = file(DIR_FS_CATALOG . 'includes/tld.txt');
  377. while (list(,$line) = each($tlds)) {
  378. // Get rid of comments
  379. $words = explode('#', $line);
  380. $tld = trim($words[0]);
  381. // TLDs should be 3 letters or more
  382. if (preg_match('/^[a-z]{3,}$/i', $tld) == 1) {
  383. $tld_pattern .= '^' . $tld . '$|';
  384. }
  385. }
  386. // Remove last '|'
  387. $tld_pattern = substr($tld_pattern, 0, -1);
  388. if (preg_match("/$tld_pattern/i", $top_level_domain) == 0) {
  389. $valid_address = false;
  390. }
  391. }
  392. }
  393. } else {
  394. $valid_address = false;
  395. }
  396. }
  397. } else {
  398. $valid_address = false;
  399. }
  400. } else {
  401. $valid_address = false;
  402. }
  403. if ($valid_address && ENTRY_EMAIL_ADDRESS_CHECK == '1') {
  404. if (!checkdnsrr($domain, "MX") && !checkdnsrr($domain, "A")) {
  405. $valid_address = false;
  406. }
  407. }
  408. return $valid_address;
  409. }
  410. /**
  411. * Sets the defined locale
  412. *
  413. * @param string $category The category of the locale to set
  414. * @param mixed $locale The locale, or an array of locales to try and set
  415. * @access public
  416. */
  417. function osc_setlocale($category, $locale) {
  418. if (version_compare(PHP_VERSION, '4.3', '<')) {
  419. if (is_array($locale)) {
  420. foreach ($locale as $l) {
  421. if (($result = setlocale($category, $l)) !== false) {
  422. return $result;
  423. }
  424. }
  425. return false;
  426. } else {
  427. return setlocale($category, $locale);
  428. }
  429. } else {
  430. return setlocale($category, $locale);
  431. }
  432. }
  433. ?>