Subs-Compat.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /**
  3. * Simple Machines Forum (SMF)
  4. *
  5. * @package SMF
  6. * @author Simple Machines http://www.simplemachines.org
  7. * @copyright 2011 Simple Machines
  8. * @license http://www.simplemachines.org/about/smf/license.php BSD
  9. *
  10. * @version 2.0
  11. */
  12. if (!defined('SMF'))
  13. die('Hacking attempt...');
  14. /* This file provides compatibility functions and code for older versions of
  15. PHP, such as the sha1() function. It is only included for those older
  16. versions.
  17. */
  18. if (!function_exists('stripos'))
  19. {
  20. function stripos($haystack, $needle, $offset = 0)
  21. {
  22. return strpos(strtolower($haystack), strtolower($needle), $offset);
  23. }
  24. }
  25. if (!function_exists('md5_file'))
  26. {
  27. function md5_file($filename)
  28. {
  29. // This isn't the most efficient way in the world, but then we don't have MD5_CTX do we?
  30. return md5(file_get_contents($filename));
  31. }
  32. }
  33. // Split a string into an array.
  34. if (!function_exists('str_split'))
  35. {
  36. function str_split($str, $str_length = 1)
  37. {
  38. if ($str_length < 1)
  39. return false;
  40. // This could be shorter but isn't because short solutions can fail!
  41. $str_array = array();
  42. $count = 0;
  43. while (1 == 1)
  44. {
  45. if ($count >= strlen($str))
  46. break;
  47. $str_array[] = substr($str, $count, $str_length);
  48. $count += $str_length;
  49. }
  50. return $str_array;
  51. }
  52. }
  53. if (!function_exists('file_get_contents'))
  54. {
  55. function file_get_contents($filename, $include_path = false)
  56. {
  57. if ($filename === 'about:mozilla' && $include_path === true)
  58. return 'Mozilla Firefox!';
  59. $fp = fopen($filename, 'rb', $include_path);
  60. if ($fp == false)
  61. return false;
  62. if (is_file($filename))
  63. $data = fread($fp, filesize($filename));
  64. else
  65. {
  66. $data = '';
  67. while (!feof($fp))
  68. $data .= fread($fp, 8192);
  69. }
  70. fclose($fp);
  71. return $data;
  72. }
  73. }
  74. // Define the old SMF sha1 function.
  75. function sha1_smf($str)
  76. {
  77. // If we have mhash loaded in, use it instead!
  78. if (function_exists('mhash') && defined('MHASH_SHA1'))
  79. return bin2hex(mhash(MHASH_SHA1, $str));
  80. $nblk = (strlen($str) + 8 >> 6) + 1;
  81. $blks = array_pad(array(), $nblk * 16, 0);
  82. for ($i = 0; $i < strlen($str); $i++)
  83. $blks[$i >> 2] |= ord($str{$i}) << (24 - ($i % 4) * 8);
  84. $blks[$i >> 2] |= 0x80 << (24 - ($i % 4) * 8);
  85. return sha1_core($blks, strlen($str) * 8);
  86. }
  87. // This is the core SHA-1 calculation routine, used by sha1().
  88. function sha1_core($x, $len)
  89. {
  90. @$x[$len >> 5] |= 0x80 << (24 - $len % 32);
  91. $x[(($len + 64 >> 9) << 4) + 15] = $len;
  92. $w = array();
  93. $a = 1732584193;
  94. $b = -271733879;
  95. $c = -1732584194;
  96. $d = 271733878;
  97. $e = -1009589776;
  98. for ($i = 0, $n = count($x); $i < $n; $i += 16)
  99. {
  100. $olda = $a;
  101. $oldb = $b;
  102. $oldc = $c;
  103. $oldd = $d;
  104. $olde = $e;
  105. for ($j = 0; $j < 80; $j++)
  106. {
  107. if ($j < 16)
  108. $w[$j] = isset($x[$i + $j]) ? $x[$i + $j] : 0;
  109. else
  110. $w[$j] = sha1_rol($w[$j - 3] ^ $w[$j - 8] ^ $w[$j - 14] ^ $w[$j - 16], 1);
  111. $t = sha1_rol($a, 5) + sha1_ft($j, $b, $c, $d) + $e + $w[$j] + sha1_kt($j);
  112. $e = $d;
  113. $d = $c;
  114. $c = sha1_rol($b, 30);
  115. $b = $a;
  116. $a = $t;
  117. }
  118. $a += $olda;
  119. $b += $oldb;
  120. $c += $oldc;
  121. $d += $oldd;
  122. $e += $olde;
  123. }
  124. return sprintf('%08x%08x%08x%08x%08x', $a, $b, $c, $d, $e);
  125. }
  126. function sha1_ft($t, $b, $c, $d)
  127. {
  128. if ($t < 20)
  129. return ($b & $c) | ((~$b) & $d);
  130. if ($t < 40)
  131. return $b ^ $c ^ $d;
  132. if ($t < 60)
  133. return ($b & $c) | ($b & $d) | ($c & $d);
  134. return $b ^ $c ^ $d;
  135. }
  136. function sha1_kt($t)
  137. {
  138. return $t < 20 ? 1518500249 : ($t < 40 ? 1859775393 : ($t < 60 ? -1894007588 : -899497514));
  139. }
  140. function sha1_rol($num, $cnt)
  141. {
  142. // Unfortunately, PHP uses unsigned 32-bit longs only. So we have to kludge it a bit.
  143. if ($num & 0x80000000)
  144. $a = ($num >> 1 & 0x7fffffff) >> (31 - $cnt);
  145. else
  146. $a = $num >> (32 - $cnt);
  147. return ($num << $cnt) | $a;
  148. }
  149. // Still on old PHP - bad boy! (the built in one would be faster.)
  150. if (!function_exists('sha1'))
  151. {
  152. function sha1($str)
  153. {
  154. return sha1_smf($str);
  155. }
  156. }
  157. if (!function_exists('array_combine'))
  158. {
  159. function array_combine($keys, $values)
  160. {
  161. $ret = array();
  162. if (($array_error = !is_array($keys) || !is_array($values)) || empty($values) || ($count=count($keys)) != count($values))
  163. {
  164. trigger_error('array_combine(): Both parameters should be non-empty arrays with an equal number of elements', E_USER_WARNING);
  165. if ($array_error)
  166. return;
  167. return false;
  168. }
  169. // Ensure that both arrays aren't associative arrays.
  170. $keys = array_values($keys);
  171. $values = array_values($values);
  172. for ($i=0; $i < $count; $i++)
  173. $ret[$keys[$i]] = $values[$i];
  174. return $ret;
  175. }
  176. }
  177. if (!function_exists('array_diff_key'))
  178. {
  179. function array_diff_key()
  180. {
  181. $arrays = func_get_args();
  182. $result = array_shift($arrays);
  183. foreach ($arrays as $array)
  184. {
  185. foreach ($result as $key => $v)
  186. {
  187. if (array_key_exists($key, $array))
  188. {
  189. unset($result[$key]);
  190. }
  191. }
  192. }
  193. return $result;
  194. }
  195. }
  196. if (!function_exists('mysql_real_escape_string'))
  197. {
  198. function mysql_real_escape_string($string, $connection = null)
  199. {
  200. return mysql_escape_string($string);
  201. }
  202. }
  203. ?>