annotate forum/Sources/Subs-Compat.php @ 76:e3e11437ecea website

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