Subs-Compat.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. <?php
  2. /**
  3. * This file provides compatibility functions and code for older versions of
  4. * PHP, such as the sha1() function, missing extensions, or 64-bit vs 32-bit
  5. * systems. It is only included for those older versions or when the respective
  6. * extension or function cannot be found.
  7. *
  8. * Simple Machines Forum (SMF)
  9. *
  10. * @package SMF
  11. * @author Simple Machines http://www.simplemachines.org
  12. * @copyright 2011 Simple Machines
  13. * @license http://www.simplemachines.org/about/smf/license.php BSD
  14. *
  15. * @version 2.1 Alpha 1
  16. */
  17. if (!defined('SMF'))
  18. die('Hacking attempt...');
  19. if (!function_exists('stripos'))
  20. {
  21. function stripos($haystack, $needle, $offset = 0)
  22. {
  23. return strpos(strtolower($haystack), strtolower($needle), $offset);
  24. }
  25. }
  26. if (!function_exists('md5_file'))
  27. {
  28. function md5_file($filename)
  29. {
  30. // This isn't the most efficient way in the world, but then we don't have MD5_CTX do we?
  31. return md5(file_get_contents($filename));
  32. }
  33. }
  34. if (!function_exists('str_split'))
  35. {
  36. /**
  37. * Split a string into an array.
  38. * @param $str the string to split
  39. * @param $str_length
  40. */
  41. function str_split($str, $str_length = 1)
  42. {
  43. if ($str_length < 1)
  44. return false;
  45. // This could be shorter but isn't because short solutions can fail!
  46. $str_array = array();
  47. $count = 0;
  48. while (1 == 1)
  49. {
  50. if ($count >= strlen($str))
  51. break;
  52. $str_array[] = substr($str, $count, $str_length);
  53. $count += $str_length;
  54. }
  55. return $str_array;
  56. }
  57. }
  58. if (!function_exists('file_get_contents'))
  59. {
  60. function file_get_contents($filename, $include_path = false)
  61. {
  62. if ($filename === 'about:mozilla' && $include_path === true)
  63. return 'Mozilla Firefox!';
  64. $fp = fopen($filename, 'rb', $include_path);
  65. if ($fp == false)
  66. return false;
  67. if (is_file($filename))
  68. $data = fread($fp, filesize($filename));
  69. else
  70. {
  71. $data = '';
  72. while (!feof($fp))
  73. $data .= fread($fp, 8192);
  74. }
  75. fclose($fp);
  76. return $data;
  77. }
  78. }
  79. /**
  80. * Define the old SMF sha1 function.
  81. * @param $str the string
  82. */
  83. function sha1_smf($str)
  84. {
  85. // If we have mhash loaded in, use it instead!
  86. if (function_exists('mhash') && defined('MHASH_SHA1'))
  87. return bin2hex(mhash(MHASH_SHA1, $str));
  88. $nblk = (strlen($str) + 8 >> 6) + 1;
  89. $blks = array_pad(array(), $nblk * 16, 0);
  90. for ($i = 0; $i < strlen($str); $i++)
  91. $blks[$i >> 2] |= ord($str{$i}) << (24 - ($i % 4) * 8);
  92. $blks[$i >> 2] |= 0x80 << (24 - ($i % 4) * 8);
  93. return sha1_core($blks, strlen($str) * 8);
  94. }
  95. /**
  96. * This is the core SHA-1 calculation routine, used by sha1().
  97. */
  98. function sha1_core($x, $len)
  99. {
  100. @$x[$len >> 5] |= 0x80 << (24 - $len % 32);
  101. $x[(($len + 64 >> 9) << 4) + 15] = $len;
  102. $w = array();
  103. $a = 1732584193;
  104. $b = -271733879;
  105. $c = -1732584194;
  106. $d = 271733878;
  107. $e = -1009589776;
  108. for ($i = 0, $n = count($x); $i < $n; $i += 16)
  109. {
  110. $olda = $a;
  111. $oldb = $b;
  112. $oldc = $c;
  113. $oldd = $d;
  114. $olde = $e;
  115. for ($j = 0; $j < 80; $j++)
  116. {
  117. if ($j < 16)
  118. $w[$j] = isset($x[$i + $j]) ? $x[$i + $j] : 0;
  119. else
  120. $w[$j] = sha1_rol($w[$j - 3] ^ $w[$j - 8] ^ $w[$j - 14] ^ $w[$j - 16], 1);
  121. $t = sha1_rol($a, 5) + sha1_ft($j, $b, $c, $d) + $e + $w[$j] + sha1_kt($j);
  122. $e = $d;
  123. $d = $c;
  124. $c = sha1_rol($b, 30);
  125. $b = $a;
  126. $a = $t;
  127. }
  128. $a += $olda;
  129. $b += $oldb;
  130. $c += $oldc;
  131. $d += $oldd;
  132. $e += $olde;
  133. }
  134. return sprintf('%08x%08x%08x%08x%08x', $a, $b, $c, $d, $e);
  135. }
  136. function sha1_ft($t, $b, $c, $d)
  137. {
  138. if ($t < 20)
  139. return ($b & $c) | ((~$b) & $d);
  140. if ($t < 40)
  141. return $b ^ $c ^ $d;
  142. if ($t < 60)
  143. return ($b & $c) | ($b & $d) | ($c & $d);
  144. return $b ^ $c ^ $d;
  145. }
  146. function sha1_kt($t)
  147. {
  148. return $t < 20 ? 1518500249 : ($t < 40 ? 1859775393 : ($t < 60 ? -1894007588 : -899497514));
  149. }
  150. function sha1_rol($num, $cnt)
  151. {
  152. // Unfortunately, PHP uses unsigned 32-bit longs only. So we have to kludge it a bit.
  153. if ($num & 0x80000000)
  154. $a = ($num >> 1 & 0x7fffffff) >> (31 - $cnt);
  155. else
  156. $a = $num >> (32 - $cnt);
  157. return ($num << $cnt) | $a;
  158. }
  159. /**
  160. * Still on old PHP - bad boy! (the built in one would be faster.)
  161. */
  162. if (!function_exists('sha1'))
  163. {
  164. function sha1($str)
  165. {
  166. return sha1_smf($str);
  167. }
  168. }
  169. if (!function_exists('array_combine'))
  170. {
  171. function array_combine($keys, $values)
  172. {
  173. $ret = array();
  174. if (($array_error = !is_array($keys) || !is_array($values)) || empty($values) || ($count=count($keys)) != count($values))
  175. {
  176. trigger_error('array_combine(): Both parameters should be non-empty arrays with an equal number of elements', E_USER_WARNING);
  177. if ($array_error)
  178. return;
  179. return false;
  180. }
  181. // Ensure that both arrays aren't associative arrays.
  182. $keys = array_values($keys);
  183. $values = array_values($values);
  184. for ($i=0; $i < $count; $i++)
  185. $ret[$keys[$i]] = $values[$i];
  186. return $ret;
  187. }
  188. }
  189. if (!function_exists('array_diff_key'))
  190. {
  191. function array_diff_key()
  192. {
  193. $arrays = func_get_args();
  194. $result = array_shift($arrays);
  195. foreach ($arrays as $array)
  196. {
  197. foreach ($result as $key => $v)
  198. {
  199. if (array_key_exists($key, $array))
  200. {
  201. unset($result[$key]);
  202. }
  203. }
  204. }
  205. return $result;
  206. }
  207. }
  208. if (!function_exists('mysql_real_escape_string'))
  209. {
  210. function mysql_real_escape_string($string, $connection = null)
  211. {
  212. return mysql_escape_string($string);
  213. }
  214. }
  215. if (!function_exists('smf_crc32'))
  216. {
  217. /**
  218. * Compatibility function.
  219. * crc32 doesn't work as expected on 64-bit functions - make our own.
  220. * http://www.php.net/crc32#79567
  221. * @param $number
  222. */
  223. function smf_crc32($number)
  224. {
  225. $crc = crc32($number);
  226. if ($crc & 0x80000000)
  227. {
  228. $crc ^= 0xffffffff;
  229. $crc += 1;
  230. $crc = -$crc;
  231. }
  232. return $crc;
  233. }
  234. }
  235. // PHP < 4.3.2 doesn't have this function
  236. if (!function_exists('session_regenerate_id'))
  237. {
  238. function session_regenerate_id()
  239. {
  240. // Too late to change the session now.
  241. if (headers_sent())
  242. return false;
  243. session_id(strtolower(md5(uniqid(mt_rand(), true))));
  244. return true;
  245. }
  246. }
  247. // @author [Unknown] [email protected]
  248. // @link http://www.simplemachines.org/community/index.php?msg=2420295
  249. if (!function_exists('str_ireplace'))
  250. {
  251. function str_ireplace($search, $replace, $subject, $count = -1)
  252. {
  253. global $context;
  254. // @todo Using preg should give us better Unicode support for case folding.
  255. // But technically, this doesn't do the same thing that str_ireplace() does in PHP 5.
  256. // Might be better to always omit the u parameter.
  257. $endu = '~i' . ($context['utf8'] ? 'u' : '');
  258. if (is_array($search))
  259. foreach ($search as $k => $pat)
  260. $search[$k] = '~' . preg_quote($pat, '~') . $endu;
  261. else
  262. $search = '~' . preg_quote($search, '~') . $endu;
  263. return preg_replace($search, $replace, $subject, $count > 0 ? $count : -1);
  264. }
  265. }
  266. /**
  267. * Load a < PHP 5 class file
  268. *
  269. * @param string $filename
  270. */
  271. function loadOldClassFile($filename)
  272. {
  273. global $sourcedir;
  274. static $files_included = array();
  275. // Check if it was included before.
  276. if (in_array($filename, $files_included))
  277. return;
  278. // Make sure we don't include it again.
  279. $files_included[] = $filename;
  280. // Do some replacements to make it PHP 4 compatible.
  281. eval('?' . '>' . preg_replace(array(
  282. '~class\s+([\w-_]+)([^}]+)function\s+__construct\s*\(~',
  283. '~([\s\t]+)public\s+\$~',
  284. '~([\s\t]+)private\s+\$~',
  285. '~([\s\t]+)protected\s+\$~',
  286. '~([\s\t]+)public\s+function\s+~',
  287. '~([\s\t]+)private\s+function\s+~',
  288. '~([\s\t]+)protected\s+function\s+~',
  289. ), array(
  290. 'class $1$2function $1(',
  291. '$1var $',
  292. '$1var $',
  293. '$1var $',
  294. '$1function ',
  295. '$1function ',
  296. '$1function ',
  297. ), rtrim(file_get_contents($sourcedir . '/' . $filename))));
  298. }
  299. /**
  300. * PHP 4 didn't have bcpowmod.
  301. */
  302. if (!function_exists('bcpowmod') && function_exists('bcpow'))
  303. {
  304. function bcpowmod($num1, $num2, $num3)
  305. {
  306. return bcmod(bcpow($num1, $num2), $num3);
  307. }
  308. }
  309. /**
  310. * Random seed generator
  311. */
  312. if (version_compare(PHP_VERSION, '4.2.0', '<'))
  313. {
  314. $seed = ($modSettings['rand_seed'] + ((double) microtime() * 1000003)) & 0x7fffffff;
  315. mt_srand($seed);
  316. }
  317. ?>