emulate PHP 5 for backwards compatibility


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2. /**
  3.  * api: php
  4.  * title: upgrade.php
  5.  * description: backwards compatibility for older PHP interpreters
  6.  * version: 14
  7.  * license: Public Domain
  8.  * url: http://freshmeat.net/p/upgradephp
  9.  * type: functions
  10.  * category: library
  11.  * priority: auto
  12.  * sort: -200
  13.  * provides: upgrade-php
  14.  *
  15.  *
  16.  * You get PHP version independence by including() this script. It provides
  17.  * downwards compatibility to older PHP installations by emulating missing
  18.  * functions or constants using IDENTICAL NAMES. So this doesn't slow down
  19.  * script execution on setups where the native functions already exist. It
  20.  * is meant as quick drop-in solution, and to free you from rewriting or
  21.  * cumbersome workarounds - instead of using the real & more powerful funcs.
  22.  *
  23.  * It cannot mirror PHP5s extended OO-semantics and functionality into PHP4
  24.  * however. A few features are added here that weren't part of PHP yet. And
  25.  * some other function collections are separated out into the ext/ directory.
  26.  *
  27.  * And further this is PUBLIC DOMAIN (no copyright, no license, no warranty)
  28.  * so threrefore compatible to ALL open source licenses. You could rip this
  29.  * paragraph out to republish this instead only under more restrictive terms
  30.  * or your favorite license (GNU LGPL/GPL, BSDL, MPL/CDDL, Artist, PHPL, ...)
  31.  *
  32.  * Get update notes via "http://freshmeat.net/projects/upgradephp" or
  33.  * google for it. Any contribution is appreciated. <milky*users·sf·net>
  34.  *
  35.  */
  36.  
  37.  
  38.  
  39. /**
  40.  * ------------------------------ CVS ---
  41.  * @group CVS
  42.  * @since CVS
  43.  *
  44.  * planned, but as of yet unimplemented functions
  45.  * - some of these might appear in 6.0 or ParrotPHP
  46.  *
  47.  * @emulated
  48.  * sys_get_temp_dir
  49.  *
  50.  */
  51.  
  52.  
  53.  
  54. /**
  55.  * returns path of the system directory for temporary files
  56.  *
  57.  */
  58. if (!function_exists("sys_get_temp_dir")) {
  59. function sys_get_temp_dir() {
  60. # check possible alternatives
  61. ($temp = ini_get("temp_dir"))
  62. or
  63. ($temp = $_SERVER["TEMP"])
  64. or
  65. ($temp = $_SERVER["TMP"])
  66. or
  67. ($temp = "/tmp");
  68. # fin
  69. return($temp);
  70. }
  71. }
  72.  
  73.  
  74.  
  75.  
  76.  
  77. /**
  78.  * ------------------------------ 5.2 ---
  79.  * @group 5_2
  80.  * @since 5.2
  81.  *
  82.  * Additions of PHP 5.2.0
  83.  * - some listed here, might have appeared earlier or in release candidates
  84.  *
  85.  * @emulated
  86.  * json_encode
  87.  * json_decode
  88.  * error_get_last
  89.  * preg_last_error
  90.  * lchown
  91.  * lchgrp
  92.  * E_RECOVERABLE_ERROR
  93.  * M_SQRTPI
  94.  * M_LNPI
  95.  * M_EULER
  96.  * M_SQRT3
  97.  *
  98.  * @missing
  99.  * sys_getloadavg
  100.  * inet_ntop
  101.  * inet_pton
  102.  * array_fill_keys
  103.  * array_intersect_key
  104.  * array_intersect_ukey
  105.  * array_diff_key
  106.  * array_diff_ukey
  107.  * array_product
  108.  * pdo_drivers
  109.  * ftp_ssl_connect
  110.  * XmlReader
  111.  * XmlWriter
  112.  * PDO*
  113.  *
  114.  * @unimplementable
  115.  * stream_*
  116.  *
  117.  */
  118.  
  119.  
  120.  
  121. /**
  122.  * @since unknown
  123.  */
  124. if (!defined("E_RECOVERABLE_ERROR")) { define("E_RECOVERABLE_ERROR", 4096); }
  125.  
  126.  
  127.  
  128. /**
  129.  * Converts PHP variable or array into "JSON" (a JavaScript value expression
  130.  * or "object notation").
  131.  *
  132.  * @compat
  133.  * output seems identical to PECL versions
  134.  * @bugs
  135.  * doesn't take care with unicode too much
  136.  *
  137.  */
  138. if (!function_exists("json_encode")) {
  139. function json_encode($var, /*emu_args*/$obj=FALSE) {
  140.  
  141. #-- prepare JSON string
  142. $json = "";
  143.  
  144. #-- add array entries
  145. if (is_array($var) || ($obj=is_object($var))) {
  146.  
  147. #-- check if array is associative
  148. if (!$obj) foreach ((array)$var as $i=>$v) {
  149. if (!is_int($i)) {
  150. $obj = 1;
  151. break;
  152. }
  153. }
  154.  
  155. #-- concat invidual entries
  156. foreach ((array)$var as $i=>$v) {
  157. $json .= ($json ? "," : "") // comma separators
  158. . ($obj ? ("\"$i\":") : "") // assoc prefix
  159. . (json_encode($v)); // value
  160. }
  161.  
  162. #-- enclose into braces or brackets
  163. $json = $obj ? "{".$json."}" : "[".$json."]";
  164. }
  165.  
  166. #-- strings need some care
  167. elseif (is_string($var)) {
  168. if (!utf8_decode($var)) {
  169. $var = utf8_encode($var);
  170. }
  171. $var = str_replace(array("\"", "\\", "/", "\b", "\f", "\n", "\r", "\t"), array("\\\"", "\\\\", "\\/", "\\b", "\\f", "\\n", "\\r", "\\t"), $var);
  172. $json = '"' . $var . '"';
  173. }
  174.  
  175. #-- basic types
  176. elseif (is_bool($var)) {
  177. $json = $var ? "true" : "false";
  178. }
  179. elseif ($var === NULL) {
  180. $json = "null";
  181. }
  182. elseif (is_int($var) || is_float($var)) {
  183. $json = "$var";
  184. }
  185.  
  186. #-- something went wrong
  187. else {
  188. trigger_error("json_encode: don't know what a '" .gettype($var). "' is.", E_USER_ERROR);
  189. }
  190.  
  191. #-- done
  192. return($json);
  193. }
  194. }
  195.  
  196.  
  197.  
  198. /**
  199.  * Parses JSON (JavaScript value expression) into PHP variable
  200.  * (array or object).
  201.  *
  202.  * @compat
  203.  * behaves similiar to PECL version
  204.  * but is less quiet on errors
  205.  * might ignore some misformed representations
  206.  * @bugs
  207.  * doesn't decode unicode \uXXXX string escapes
  208.  *
  209.  */
  210. if (!function_exists("json_decode")) {
  211. function json_decode($json, $assoc=FALSE, /*emu_args*/$n=0,$state=0,$waitfor=0) {
  212.  
  213. #-- result var
  214. $val = NULL;
  215. static $lang_eq = array("true" => TRUE, "false" => FALSE, "null" => NULL);
  216. static $str_eq = array("n"=>"\012", "r"=>"\015", "\\"=>"\\", '"'=>'"', "f"=>"\f", "b"=>"\b", "t"=>"\t", "/"=>"/");
  217.  
  218. #-- flat char-wise parsing
  219. for (/*n*/; $n<strlen($json); /*n*/) {
  220. $c = $json[$n];
  221.  
  222. #-= in-string
  223. if ($state==='"') {
  224. if ($c == '\\') {
  225. $c = $json[++$n];
  226. if (isset($str_eq[$c])) {
  227. $val .= $str_eq[$c];
  228. }
  229. elseif ($c == "u") {
  230. $val .= "\\u";
  231. }
  232. else {
  233. $val .= "\\" . $c;
  234. }
  235. }
  236. elseif ($c == '"') {
  237. $state = 0;
  238. }
  239. else {
  240. $val .= $c;
  241. }
  242. }
  243.  
  244. #-> end of sub-call (array/object)
  245. elseif ($waitfor && (strpos($waitfor, $c) !== false)) {
  246. return array($val, $n); // return current value and state
  247. }
  248.  
  249. #-= in-array
  250. elseif ($state===']') {
  251. list($v, $n) = json_decode($json, 0, $n, 0, ",]");
  252. $val[] = $v;
  253. if ($json[$n] == "]") { return array($val, $n); }
  254. }
  255.  
  256. #-= in-object
  257. elseif ($state==='}') {
  258. list($i, $n) = json_decode($json, 0, $n, 0, ":"); // this allowed non-string indicies
  259. list($v, $n) = json_decode($json, 0, $n+1, 0, ",}");
  260. $val[$i] = $v;
  261. if ($json[$n] == "}") { return array($val, $n); }
  262. }
  263.  
  264. #-- looking for next item (0)
  265. else {
  266.  
  267. #-> whitesapce
  268. if (preg_match("/\s/", $c)) {
  269. // skip
  270. }
  271.  
  272. #-> string begin
  273. elseif ($c == '"') {
  274. $state = '"';
  275. }
  276.  
  277. #-> object
  278. elseif ($c == "{") {
  279. list($val, $n) = json_decode($json, $assoc, $n+1, '}', "}");
  280. if ($val && $n && !$assoc) {
  281. $obj = new stdClass();
  282. foreach ($val as $i=>$v) {
  283. $obj->{$i} = $v;
  284. }
  285. $val = $obj;
  286. unset($obj);
  287. }
  288. }
  289. #-> array
  290. elseif ($c == "[") {
  291. list($val, $n) = json_decode($json, $assoc, $n+1, ']', "]");
  292. }
  293.  
  294. #-> comment
  295. elseif (($c == "/") && ($json[$n+1]=="*")) {
  296. // just find end, skip over
  297. ($n = strpos($json, "*/", $n+1)) or ($n = strlen($json));
  298. }
  299.  
  300. #-> numbers
  301. elseif (preg_match("#^(-?\d+(?:\.\d+)?)(?:[eE](-?\d+))?#", substr($json, $n), $uu)) {
  302. $val = $uu[1];
  303. $n += strlen($uu[0]) - 1;
  304. $val = strpos($val, ".") ? (float)$val : (int)$val;
  305. if (isset($uu[2])) {
  306. $val *= pow(10, (int)$uu[2]);
  307. }
  308. }
  309.  
  310. #-> boolean or null
  311. elseif (preg_match("#^(true|false|null)\b#", substr($json, $n), $uu)) {
  312. $val = $lang_eq[$uu[1]];
  313. $n += strlen($uu[1]) - 1;
  314. }
  315.  
  316. #-- parsing error
  317. else {
  318. // PHPs native json_decode() breaks here usually and QUIETLY
  319. trigger_error("json_decode: error parsing '$c' at position $n", E_USER_WARNING);
  320. return $waitfor ? array(NULL, 1<<30) : NULL;
  321. }
  322.  
  323. }//state
  324.  
  325. #-- next char
  326. if ($n === NULL) { return NULL; }
  327. $n++;
  328. }//for
  329.  
  330. #-- final result
  331. return ($val);
  332. }
  333. }
  334.  
  335.  
  336.  
  337.  
  338. /**
  339.  * @stub
  340.  * @cannot-reimplement
  341.  *
  342.  */
  343. if (!function_exists("preg_last_error")) {
  344. if (!defined("PREG_NO_ERROR")) { define("PREG_NO_ERROR", 0); }
  345. if (!defined("PREG_INTERNAL_ERROR")) { define("PREG_INTERNAL_ERROR", 1); }
  346. if (!defined("PREG_BACKTRACK_LIMIT_ERROR")) { define("PREG_BACKTRACK_LIMIT_ERROR", 2); }
  347. if (!defined("PREG_RECURSION_LIMIT_ERROR")) { define("PREG_RECURSION_LIMIT_ERROR", 3); }
  348. if (!defined("PREG_BAD_UTF8_ERROR")) { define("PREG_BAD_UTF8_ERROR", 4); }
  349. function preg_last_error() {
  350. return PREG_NO_ERROR;
  351. }
  352. }
  353.  
  354.  
  355.  
  356.  
  357. /**
  358.  * @stub
  359.  *
  360.  * Returns associative array with last error message.
  361.  *
  362.  */
  363. if (!function_exists("error_get_last")) {
  364. function error_get_last() {
  365. return array(
  366. "type" => 0,
  367. "message" => $GLOBALS[php_errormsg],
  368. "file" => "unknonw",
  369. "line" => 0,
  370. );
  371. }
  372. }
  373.  
  374.  
  375.  
  376.  
  377. /**
  378.  * @flag quirky, exec
  379.  *
  380.  * Change owner/group of a symlink filename.
  381.  *
  382.  */
  383. if (!function_exists("lchown")) {
  384. function lchown($fn, $user) {
  385. if (PHP_OS != "Linux") {
  386. return false;
  387. }
  388. $user = escapeshellcmd($user);
  389. $fn = escapeshellcmd($fn);
  390. exec("chown -h '$user' '$fn'", $uu, $state);
  391. return($state);
  392. }
  393. }
  394. if (!function_exists("lchgrp")) {
  395. function lchgrp($fn, $group) {
  396. return lchown($fn, ":$group");
  397. }
  398. }
  399.  
  400.  
  401.  
  402.  
  403.  
  404.  
  405.  
  406.  
  407.  
  408.  
  409.  
  410.  
  411.  
  412.  
  413. /**
  414.  * ------------------------------ 5.1 ---
  415.  * @group 5_1
  416.  * @since 5.1
  417.  *
  418.  * Additions in PHP 5.1
  419.  * - most functions here appeared in -rc1 already
  420.  * - and were backported to 4.4 series?
  421.  *
  422.  * @emulated
  423.  * property_exists
  424.  * time_sleep_until
  425.  * fputcsv
  426.  * strptime
  427.  * ENT_COMPAT
  428.  * ENT_QUOTES
  429.  * ENT_NOQUOTES
  430.  * htmlspecialchars_decode
  431.  * PHP_INT_SIZE
  432.  * PHP_INT_MAX
  433.  * M_SQRTPI
  434.  * M_LNPI
  435.  * M_EULER
  436.  * M_SQRT3
  437.  *
  438.  * @missing
  439.  * strptime
  440.  *
  441.  * @unimplementable
  442.  * ...
  443.  *
  444.  */
  445.  
  446.  
  447.  
  448. /**
  449.  * Constants for future 64-bit integer support.
  450.  *
  451.  */
  452. if (!defined("PHP_INT_SIZE")) { define("PHP_INT_SIZE", 4); }
  453. if (!defined("PHP_INT_MAX")) { define("PHP_INT_MAX", 2147483647); }
  454.  
  455.  
  456.  
  457. /**
  458.  * @flag bugfix
  459.  * @see #33895
  460.  *
  461.  * Missing constants in 5.1, originally appeared in 4.0.
  462.  */
  463. if (!defined("M_SQRTPI")) { define("M_SQRTPI", 1.7724538509055); }
  464. if (!defined("M_LNPI")) { define("M_LNPI", 1.1447298858494); }
  465. if (!defined("M_EULER")) { define("M_EULER", 0.57721566490153); }
  466. if (!defined("M_SQRT3")) { define("M_SQRT3", 1.7320508075689); }
  467.  
  468.  
  469.  
  470.  
  471. #-- removes entities &lt; &gt; &amp; and &quot; eventually from HTML string
  472. if (!function_exists("htmlspecialchars_decode")) {
  473. if (!defined("ENT_COMPAT")) { define("ENT_COMPAT", 2); }
  474. if (!defined("ENT_QUOTES")) { define("ENT_QUOTES", 3); }
  475. if (!defined("ENT_NOQUOTES")) { define("ENT_NOQUOTES", 0); }
  476. function htmlspecialchars_decode($string, $quotes=2) {
  477. $d = $quotes & ENT_COMPAT;
  478. $s = $quotes & ENT_QUOTES;
  479. return str_replace(
  480. array("&lt;", "&gt;", ($s ? "&quot;" : "&.-;"), ($d ? "&#039;" : "&.-;"), "&amp;"),
  481. array("<", ">", "'", "\"", "&"),
  482. $string
  483. );
  484. }
  485. }
  486.  
  487.  
  488. /**
  489.  * @flag quirky, needs5
  490.  *
  491.  * Checks for existence of object property, should return TRUE even for NULL values.
  492.  *
  493.  * @compat
  494.  * uses (array) conversion to test, but that might not always work
  495.  * @bugs
  496.  * probably can't handle static classes well
  497.  */
  498. # doesn't return true for NULL values, can't handle public/private
  499. if (!function_exists("property_exists")) {
  500. function property_exists($obj, $prop) {
  501. if (!is_object($obj) && class_exists($obj)) {
  502. $obj = new $obj();
  503. }
  504. if (is_object($obj)) {
  505. return isset($obj->{$prop}) || in_array($prop, array_keys((array)$obj));
  506. }
  507. }
  508. }
  509.  
  510.  
  511. #-- halt execution, until given timestamp
  512. // I wonder who always comes up with such useless function ideas
  513. if (!function_exists("time_sleep_until")) {
  514. function time_sleep_until($t) {
  515. $delay = $t - time();
  516. if ($delay < 0) {
  517. trigger_error("time_sleep_until: timestamp in the past", E_USER_WARNING);
  518. return false;
  519. }
  520. else {
  521. sleep((int)$delay);
  522. usleep(($delay - floor($delay)) * 1000000);
  523. return true;
  524. }
  525. }
  526. }
  527.  
  528.  
  529.  
  530. /**
  531.  * @untested
  532.  *
  533.  * Writes an array as CSV text line into opened filehandle.
  534.  *
  535.  */
  536. if (!function_exists("fputcsv")) {
  537. function fputcsv($fp, $fields, $delim=",", $encl='"') {
  538. $line = "";
  539. foreach ((array)$fields as $str) {
  540. $line .= ($line ? $delim : "")
  541. . $encl
  542. . str_replace(array('\\', $encl), array('\\\\'. '\\'.$encl), $str)
  543. . $encl;
  544. }
  545. fwrite($fp, $line."\n");
  546. }
  547. }
  548.  
  549.  
  550.  
  551. /**
  552.  * @stub
  553.  * @untested
  554.  * @flag basic
  555.  *
  556.  * @compat
  557.  * only implements a few basic regular expression lookups
  558.  * no idea how to handle all of it
  559.  */
  560. if (!function_exists("strptime")) {
  561. function strptime($str, $format) {
  562. static $expand = array(
  563. "%D" => "%m/%d/%y",
  564. "%T" => "%H:%M:%S",
  565. );
  566. static $map_r = array(
  567. "%S"=>"tm_sec",
  568. "%M"=>"tm_min",
  569. "%H"=>"tm_hour",
  570. "%d"=>"tm_mday",
  571. "%m"=>"tm_mon",
  572. "%Y"=>"tm_year",
  573. "%y"=>"tm_year",
  574. "%W"=>"tm_wday",
  575. "%D"=>"tm_yday",
  576. "%u"=>"unparsed",
  577. );
  578. static $names = array(
  579. "Jan" => 1, "Feb" => 2, "Mar" => 3, "Apr" => 4, "May" => 5, "Jun" => 6,
  580. "Jul" => 7, "Aug" => 8, "Sep" => 9, "Oct" => 10, "Nov" => 11, "Dec" => 12,
  581. "Sun" => 0, "Mon" => 1, "Tue" => 2, "Wed" => 3, "Thu" => 4, "Fri" => 5, "Sat" => 6,
  582. );
  583.  
  584. #-- transform $format into extraction regex
  585. $format = str_replace(array_keys($expand), array_values($expand), $format);
  586. $preg = preg_replace("/(%\w)/", "(\w+)", preg_quote($format));
  587.  
  588. #-- record the positions of all STRFCMD-placeholders
  589. preg_match_all("/(%\w)/", $format, $positions);
  590. $positions = $positions[1];
  591.  
  592. #-- get individual values
  593. if (preg_match("#$preg#", "$str", $extracted)) {
  594.  
  595. #-- get values
  596. foreach ($positions as $pos=>$strfc) {
  597. $v = $extracted[$pos + 1];
  598.  
  599. #-- add
  600. if ($n = $map_r[$strfc]) {
  601. $vals[$n] = ($v > 0) ? (int)$v : $v;
  602. }
  603. else {
  604. $vals["unparsed"] .= $v . " ";
  605. }
  606. }
  607.  
  608. #-- fixup some entries
  609. $vals["tm_wday"] = $names[ substr($vals["tm_wday"], 0, 3) ];
  610. if ($vals["tm_year"] >= 1900) {
  611. $tm_year -= 1900;
  612. }
  613. elseif ($vals["tm_year"] > 0) {
  614. $vals["tm_year"] += 100;
  615. }
  616. if ($vals["tm_mon"]) {
  617. $vals["tm_mon"] -= 1;
  618. }
  619. else {
  620. $vals["tm_mon"] = $names[ substr($vals["tm_mon"], 0, 3) ] - 1;
  621. }
  622.  
  623. #-- calculate wday
  624. // ... (mktime)
  625. }
  626. return isset($vals) ? $vals : false;
  627. }
  628. }
  629.  
  630.  
  631.  
  632.  
  633.  
  634.  
  635.  
  636.  
  637.  
  638.  
  639. /**
  640.  * --------------------------- FUTURE ---
  641.  * @group FUTURE
  642.  * @since unknown
  643.  *
  644.  * Following functions aren't implemented in current PHP versions allthough
  645.  * they are logical and required counterparts to existing features or exist
  646.  * in the according external library.
  647.  *
  648.  * @emulated
  649.  * gzdecode
  650.  * ob_get_headers
  651.  * xmlentities
  652.  *
  653.  * @missing
  654.  * ...
  655.  *
  656.  * @unimplementable
  657.  * ...
  658.  *
  659.  */
  660.  
  661.  
  662.  
  663.  
  664.  
  665. #-- inflates a string enriched with gzip headers
  666. # (this is the logical counterpart to gzencode(), but don't tell anyone!)
  667. if (!function_exists("gzdecode")) {
  668. function gzdecode($data, $maxlen=NULL) {
  669.  
  670. #-- decode header
  671. $len = strlen($data);
  672. if ($len < 20) {
  673. return;
  674. }
  675. $head = substr($data, 0, 10);
  676. $head = unpack("n1id/C1cm/C1flg/V1mtime/C1xfl/C1os", $head);
  677. list($ID, $CM, $FLG, $MTIME, $XFL, $OS) = array_values($head);
  678. $FTEXT = 1<<0;
  679. $FHCRC = 1<<1;
  680. $FEXTRA = 1<<2;
  681. $FNAME = 1<<3;
  682. $FCOMMENT = 1<<4;
  683. $head = unpack("V1crc/V1isize", substr($data, $len-8, 8));
  684. list($CRC32, $ISIZE) = array_values($head);
  685.  
  686. #-- check gzip stream identifier
  687. if ($ID != 0x1f8b) {
  688. trigger_error("gzdecode: not in gzip format", E_USER_WARNING);
  689. return;
  690. }
  691. #-- check for deflate algorithm
  692. if ($CM != 8) {
  693. trigger_error("gzdecode: cannot decode anything but deflated streams", E_USER_WARNING);
  694. return;
  695. }
  696.  
  697. #-- start of data, skip bonus fields
  698. $s = 10;
  699. if ($FLG & $FEXTRA) {
  700. $s += $XFL;
  701. }
  702. if ($FLG & $FNAME) {
  703. $s = strpos($data, "\000", $s) + 1;
  704. }
  705. if ($FLG & $FCOMMENT) {
  706. $s = strpos($data, "\000", $s) + 1;
  707. }
  708. if ($FLG & $FHCRC) {
  709. $s += 2; // cannot check
  710. }
  711.  
  712. #-- get data, uncompress
  713. $data = substr($data, $s, $len-$s);
  714. if ($maxlen) {
  715. $data = gzinflate($data, $maxlen);
  716. return($data); // no checks(?!)
  717. }
  718. else {
  719. $data = gzinflate($data);
  720. }
  721.  
  722. #-- check+fin
  723. $chk = crc32($data);
  724. if ($CRC32 != $chk) {
  725. trigger_error("gzdecode: checksum failed (real$chk != comp$CRC32)", E_USER_WARNING);
  726. }
  727. elseif ($ISIZE != strlen($data)) {
  728. trigger_error("gzdecode: stream size mismatch", E_USER_WARNING);
  729. }
  730. else {
  731. return($data);
  732. }
  733. }
  734. }
  735.  
  736.  
  737. #-- get all already made headers(),
  738. # CANNOT be emulated, because output buffering functions
  739. # already swallow up any sent http header
  740. if (!function_exists("ob_get_headers")) {
  741. function ob_get_headers() {
  742. return (array)NULL;
  743. }
  744. }
  745.  
  746.  
  747. #-- encodes required named XML entities, like htmlentities(),
  748. # but does not re-encode numeric &#xxxx; character references
  749. # - could screw up scripts which then implement this themselves
  750. # - doesn't fix bogus or invalid numeric entities
  751. if (!function_exists("xmlentities")) {
  752. function xmlentities($str) {
  753. return strtr($str, array(
  754. "&#"=>"&#", "&"=>"&amp;", "'"=>"&apos;",
  755. "<"=>"&lt;", ">"=>"&gt;", "\""=>"&quot;",
  756. ));
  757. }
  758. }
  759.  
  760.  
  761.  
  762.  
  763.  
  764. /**
  765.  * ------------------------------ 5.0 ---
  766.  * @group 5_0
  767.  * @since 5.0
  768.  *
  769.  * PHP 5.0 introduces the Zend Engine 2 with new object-orientation features
  770.  * which cannot be reimplemented/defined for PHP4. The additional procedures
  771.  * and functions however can.
  772.  *
  773.  * @emulated
  774.  * stripos
  775.  * strripos
  776.  * str_ireplace
  777.  * get_headers
  778.  * headers_list
  779.  * fprintf
  780.  * vfprintf
  781.  * str_split
  782.  * http_build_query
  783.  * convert_uuencode
  784.  * convert_uudecode
  785.  * scandir
  786.  * idate
  787.  * time_nanosleep
  788.  * strpbrk
  789.  * get_declared_interfaces
  790.  * array_combine
  791.  * array_walk_recursive
  792.  * substr_compare
  793.  * spl_classes
  794.  * class_parents
  795.  * session_commit
  796.  * dns_check_record
  797.  * dns_get_mx
  798.  * setrawcookie
  799.  * file_put_contents
  800.  * COUNT_NORMAL
  801.  * COUNT_RECURSIVE
  802.  * count_recursive
  803.  * FILE_USE_INCLUDE_PATH
  804.  * FILE_IGNORE_NEW_LINES
  805.  * FILE_SKIP_EMPTY_LINES
  806.  * FILE_APPEND
  807.  * FILE_NO_DEFAULT_CONTEXT
  808.  * E_STRICT
  809.  *
  810.  * @missing
  811.  * proc_nice
  812.  * dns_get_record
  813.  * date_sunrise - undoc.
  814.  * date_sunset - undoc.
  815.  * PHP_CONFIG_FILE_SCAN_DIR
  816.  *
  817.  * @unimplementable
  818.  * set_exception_handler
  819.  * restore_exception_handler
  820.  * debug_print_backtrace
  821.  * class_implements
  822.  * proc_terminate
  823.  * proc_get_status
  824.  *
  825.  */
  826.  
  827.  
  828.  
  829. #-- constant: end of line
  830. if (!defined("PHP_EOL")) { define("PHP_EOL", ( (DIRECTORY_SEPARATOR == "\\") ? "\015\012" : (strncmp(PHP_OS, "D", 1) ? "\012" : "\015") ) ); } # "D" for Darwin
  831.  
  832.  
  833. #-- case-insensitive string search function,
  834. # - finds position of first occourence of a string c-i
  835. # - parameters identical to strpos()
  836. if (!function_exists("stripos")) {
  837. function stripos($haystack, $needle, $offset=NULL) {
  838.  
  839. #-- simply lowercase args
  840. $haystack = strtolower($haystack);
  841. $needle = strtolower($needle);
  842.  
  843. #-- search
  844. $pos = strpos($haystack, $needle, $offset);
  845. return($pos);
  846. }
  847. }
  848.  
  849.  
  850. #-- case-insensitive string search function
  851. # - but this one starts from the end of string (right to left)
  852. # - offset can be negative or positive
  853. if (!function_exists("strripos")) {
  854. function strripos($haystack, $needle, $offset=NULL) {
  855.  
  856. #-- lowercase incoming strings
  857. $haystack = strtolower($haystack);
  858. $needle = strtolower($needle);
  859.  
  860. #-- [-]$offset tells to ignore a few string bytes,
  861. # we simply cut a bit from the right
  862. if (isset($offset) && ($offset < 0)) {
  863. $haystack = substr($haystack, 0, strlen($haystack) - 1);
  864. }
  865.  
  866. #-- let PHP do it
  867. $pos = strrpos($haystack, $needle);
  868.  
  869. #-- [+]$offset => ignore left haystack bytes
  870. if (isset($offset) && ($offset > 0) && ($pos > $offset)) {
  871. $pos = false;
  872. }
  873.  
  874. #-- result
  875. return($pos);
  876. }
  877. }
  878.  
  879.  
  880. #-- case-insensitive version of str_replace
  881. if (!function_exists("str_ireplace")) {
  882. function str_ireplace($search, $replace, $subject, $count=NULL) {
  883.  
  884. #-- call ourselves recursively, if parameters are arrays/lists
  885. if (is_array($search)) {
  886. $replace = array_values($replace);
  887. foreach (array_values($search) as $i=>$srch) {
  888. $subject = str_ireplace($srch, $replace[$i], $subject);
  889. }
  890. }
  891.  
  892. #-- sluice replacement strings through the Perl-regex module
  893. # (faster than doing it by hand)
  894. else {
  895. $replace = addcslashes($replace, "$\\");
  896. $search = "{" . preg_quote($search) . "}i";
  897. $subject = preg_replace($search, $replace, $subject);
  898. }
  899.  
  900. #-- result
  901. return($subject);
  902. }
  903. }
  904.  
  905.  
  906. #-- performs a http HEAD request
  907. if (!function_exists("get_headers")) {
  908. function get_headers($url, $parse=0) {
  909.  
  910. #-- extract URL parts ($host, $port, $path, ...)
  911. $c = parse_url($url);
  912. extract($c);
  913. if (!isset($port)) {
  914. $port = 80;
  915. }
  916.  
  917. #-- try to open TCP connection
  918. $f = fsockopen($host, $port, $errno, $errstr, $timeout=15);
  919. if (!$f) {
  920. return;
  921. }
  922.  
  923. #-- send request header
  924. fwrite($f, "HEAD $path HTTP/1.0\015\012"
  925. . "Host: $host\015\012"
  926. . "Connection: close\015\012"
  927. . "Accept: */*, xml/*\015\012"
  928. . "User-Agent: ".trim(ini_get("user_agent"))."\015\012"
  929. . "\015\012");
  930.  
  931. #-- read incoming lines
  932. $ls = array();
  933. while ( !feof($f) && ($line = trim(fgets($f, 1<<16))) ) {
  934.  
  935. #-- read header names to make result an hash (names in array index)
  936. if ($parse) {
  937. if ($l = strpos($line, ":")) {
  938. $name = substr($line, 0, $l);
  939. $value = trim(substr($line, $l + 1));
  940. #-- merge headers
  941. if (isset($ls[$name])) {
  942. $ls[$name] .= ", $value";
  943. }
  944. else {
  945. $ls[$name] = $value;
  946. }
  947. }
  948. #-- HTTP response status header as result[0]
  949. else {
  950. $ls[] = $line;
  951. }
  952. }
  953.  
  954. #-- unparsed header list (numeric indices)
  955. else {
  956. $ls[] = $line;
  957. }
  958. }
  959.  
  960. #-- close TCP connection and give result
  961. fclose($f);
  962. return($ls);
  963. }
  964. }
  965.  
  966.  
  967. #-- list of already/potentially sent HTTP responsee headers(),
  968. # CANNOT be implemented (except for Apache module maybe)
  969. if (!function_exists("headers_list")) {
  970. function headers_list() {
  971. trigger_error("headers_list(): not supported by this PHP version", E_USER_WARNING);
  972. return (array)NULL;
  973. }
  974. }
  975.  
  976.  
  977. #-- write formatted string to stream/file,
  978. # arbitrary numer of arguments
  979. if (!function_exists("fprintf")) {
  980. function fprintf(/*...*/) {
  981. $args = func_get_args();
  982. $stream = array_shift($args);
  983. return fwrite($stream, call_user_func_array("sprintf", $args));
  984. }
  985. }
  986.  
  987.  
  988. #-- write formatted string to stream, args array
  989. if (!function_exists("vfprintf")) {
  990. function vfprintf($stream, $format, $args=NULL) {
  991. return fwrite($stream, vsprintf($format, $args));
  992. }
  993. }
  994.  
  995.  
  996. #-- splits a string in evenly sized chunks
  997. # and returns this as array
  998. if (!function_exists("str_split")) {
  999. function str_split($str, $chunk=1) {
  1000. $r = array();
  1001.  
  1002. #-- return back as one chunk completely, if size chosen too low
  1003. if ($chunk < 1) {
  1004. $r[] = $str;
  1005. }
  1006.  
  1007. #-- add substrings to result array until subject strings end reached
  1008. else {
  1009. $len = strlen($str);
  1010. for ($n=0; $n<$len; $n+=$chunk) {
  1011. $r[] = substr($str, $n, $chunk);
  1012. }
  1013. }
  1014. return($r);
  1015. }
  1016. }
  1017.  
  1018.  
  1019. #-- constructs a QUERY_STRING (application/x-www-form-urlencoded format, non-raw)
  1020. # from a nested array/hash with name=>value pairs
  1021. # - only first two args are part of the original API - rest used for recursion
  1022. if (!function_exists("http_build_query")) {
  1023. function http_build_query($data, $int_prefix="", $subarray_pfix="", $level=0) {
  1024.  
  1025. #-- empty starting string
  1026. $s = "";
  1027. ($SEP = ini_get("arg_separator.output")) or ($SEP = "&");
  1028.  
  1029. #-- traverse hash/array/list entries
  1030. foreach ($data as $index=>$value) {
  1031.  
  1032. #-- add sub_prefix for subarrays (happens for recursed innovocation)
  1033. if ($subarray_pfix) {
  1034. if ($level) {
  1035. $index = "[" . $index . "]";
  1036. }
  1037. $index = $subarray_pfix . $index;
  1038. }
  1039. #-- add user-specified prefix for integer-indices
  1040. elseif (is_int($index) && strlen($int_prefix)) {
  1041. $index = $int_prefix . $index;
  1042. }
  1043.  
  1044. #-- recurse for sub-arrays
  1045. if (is_array($value)) {
  1046. $s .= http_build_query($value, "", $index, $level + 1);
  1047. }
  1048. else { // or just literal URL parameter
  1049. $s .= $SEP . $index . "=" . urlencode($value);
  1050. }
  1051. }
  1052.  
  1053. #-- remove redundant "&" from first round (-not checked above to simplifiy loop)
  1054. if (!$subarray_pfix) {
  1055. $s = substr($s, strlen($SEP));
  1056. }
  1057.  
  1058. #-- return result / to previous array level and iteration
  1059. return($s);
  1060. }
  1061. }
  1062.  
  1063.  
  1064. #-- transform into 3to4 uuencode
  1065. # - this is the bare encoding, not the uu file format
  1066. if (!function_exists("convert_uuencode")) {
  1067. function convert_uuencode($data) {
  1068.  
  1069. #-- init vars
  1070. $out = "";
  1071. $line = "";
  1072. $len = strlen($data);
  1073. # $data .= "\252\252\252"; // PHP and uuencode(1) use some special garbage??, looks like "\000"* and "`\n`" simply appended
  1074.  
  1075. #-- canvass source string
  1076. for ($n=0; $n<$len; ) {
  1077.  
  1078. #-- make 24-bit integer from first three bytes
  1079. $x = (ord($data[$n++]) << 16)
  1080. + (ord($data[$n++]) << 8)
  1081. + (ord($data[$n++]) << 0);
  1082.  
  1083. #-- disperse that into 4 ascii characters
  1084. $line .= chr( 32 + (($x >> 18) & 0x3f) )
  1085. . chr( 32 + (($x >> 12) & 0x3f) )
  1086. . chr( 32 + (($x >> 6) & 0x3f) )
  1087. . chr( 32 + (($x >> 0) & 0x3f) );
  1088.  
  1089. #-- cut lines, inject count prefix before each
  1090. if (($n % 45) == 0) {
  1091. $out .= chr(32 + 45) . "$line\n";
  1092. $line = "";
  1093. }
  1094. }
  1095.  
  1096. #-- throw last line, +length prefix
  1097. if ($trail = ($len % 45)) {
  1098. $out .= chr(32 + $trail) . "$line\n";
  1099. }
  1100.  
  1101. // uuencode(5) doesn't tell so, but spaces are replaced with the ` char in most implementations
  1102. $out = strtr("$out \n", " ", "`");
  1103. return($out);
  1104. }
  1105. }
  1106.  
  1107.  
  1108. #-- decodes uuencoded() data again
  1109. if (!function_exists("convert_uudecode")) {
  1110. function convert_uudecode($data) {
  1111.  
  1112. #-- prepare
  1113. $out = "";
  1114. $data = strtr($data, "`", " ");
  1115.  
  1116. #-- go through lines
  1117. foreach(explode("\n", ltrim($data)) as $line) {
  1118. if (!strlen($line)) {
  1119. break; // end reached
  1120. }
  1121.  
  1122. #-- current line length prefix
  1123. unset($num);
  1124. $num = ord($line{0}) - 32;
  1125. if (($num <= 0) || ($num > 62)) { // 62 is the maximum line length
  1126. break; // according to uuencode(5), so we stop here too
  1127. }
  1128. $line = substr($line, 1);
  1129.  
  1130. #-- prepare to decode 4-char chunks
  1131. $add = "";
  1132. for ($n=0; strlen($add)<$num; ) {
  1133.  
  1134. #-- merge 24 bit integer from the 4 ascii characters (6 bit each)
  1135. $x = ((ord($line[$n++]) - 32) << 18)
  1136. + ((ord($line[$n++]) - 32) << 12) // were saner with "& 0x3f"
  1137. + ((ord($line[$n++]) - 32) << 6)
  1138. + ((ord($line[$n++]) - 32) << 0);
  1139.  
  1140. #-- reconstruct the 3 original data chars
  1141. $add .= chr( ($x >> 16) & 0xff )
  1142. . chr( ($x >> 8) & 0xff )
  1143. . chr( ($x >> 0) & 0xff );
  1144. }
  1145.  
  1146. #-- cut any trailing garbage (last two decoded chars may be wrong)
  1147. $out .= substr($add, 0, $num);
  1148. $line = "";
  1149. }
  1150.  
  1151. return($out);
  1152. }
  1153. }
  1154.  
  1155.  
  1156. #-- return array of filenames in a given directory
  1157. # (only works for local files)
  1158. if (!function_exists("scandir")) {
  1159. function scandir($dirname, $desc=0) {
  1160.  
  1161. #-- check for file:// protocol, others aren't handled
  1162. if (strpos($dirname, "file://") === 0) {
  1163. $dirname = substr($dirname, 7);
  1164. if (strpos($dirname, "localh") === 0) {
  1165. $dirname = substr($dirname, strpos($dirname, "/"));
  1166. }
  1167. }
  1168.  
  1169. #-- directory reading handle
  1170. if ($dh = opendir($dirname)) {
  1171. $ls = array();
  1172. while ($fn = readdir($dh)) {
  1173. $ls[] = $fn; // add to array
  1174. }
  1175. closedir($dh);
  1176.  
  1177. #-- sort filenames
  1178. if ($desc) {
  1179. rsort($ls);
  1180. }
  1181. else {
  1182. sort($ls);
  1183. }
  1184. return $ls;
  1185. }
  1186.  
  1187. #-- failure
  1188. return false;
  1189. }
  1190. }
  1191.  
  1192.  
  1193. #-- like date(), but returns an integer for given one-letter format parameter
  1194. if (!function_exists("idate")) {
  1195. function idate($formatchar, $timestamp=NULL) {
  1196.  
  1197. #-- reject non-simple type parameters
  1198. if (strlen($formatchar) != 1) {
  1199. return false;
  1200. }
  1201.  
  1202. #-- get current time, if not given
  1203. if (!isset($timestamp)) {
  1204. $timestamp = time();
  1205. }
  1206.  
  1207. #-- get and turn into integer
  1208. $str = date($formatchar, $timestamp);
  1209. return (int)$str;
  1210. }
  1211. }
  1212.  
  1213.  
  1214.  
  1215. #-- combined sleep() and usleep()
  1216. if (!function_exists("time_nanosleep")) {
  1217. function time_nanosleep($sec, $nano) {
  1218. sleep($sec);
  1219. usleep($nano);
  1220. }
  1221. }
  1222.  
  1223.  
  1224.  
  1225. #-- search first occourence of any of the given chars, returns rest of haystack
  1226. # (char_list must be a string for compatibility with the real PHP func)
  1227. if (!function_exists("strpbrk")) {
  1228. function strpbrk($haystack, $char_list) {
  1229.  
  1230. #-- prepare
  1231. $len = strlen($char_list);
  1232. $min = strlen($haystack);
  1233.  
  1234. #-- check with every symbol from $char_list
  1235. for ($n = 0; $n < $len; $n++) {
  1236. $l = strpos($haystack, $char_list{$n});
  1237.  
  1238. #-- get left-most occourence
  1239. if (($l !== false) && ($l < $min)) {
  1240. $min = $l;
  1241. }
  1242. }
  1243.  
  1244. #-- result
  1245. if ($min) {
  1246. return(substr($haystack, $min));
  1247. }
  1248. else {
  1249. return(false);
  1250. }
  1251. }
  1252. }
  1253.  
  1254.  
  1255.  
  1256. #-- logo image activation URL query strings (gaga feature)
  1257. if (!function_exists("php_real_logo_guid")) {
  1258. function php_real_logo_guid() { return php_logo_guid(); }
  1259. function php_egg_logo_guid() { return zend_logo_guid(); }
  1260. }
  1261.  
  1262.  
  1263. #-- no need to implement this
  1264. # (there aren't interfaces in PHP4 anyhow)
  1265. if (!function_exists("get_declared_interfaces")) {
  1266. function get_declared_interfaces() {
  1267. trigger_error("get_declared_interfaces(): Current script won't run reliably with PHP4.", E_USER_WARNING);
  1268. return( (array)NULL );
  1269. }
  1270. }
  1271.  
  1272.  
  1273. #-- creates an array from lists of $keys and $values
  1274. # (both should have same number of entries)
  1275. if (!function_exists("array_combine")) {
  1276. function array_combine($keys, $values) {
  1277.  
  1278. #-- convert input arrays into lists
  1279. $keys = array_values($keys);
  1280. $values = array_values($values);
  1281. $r = array();
  1282.  
  1283. #-- one from each
  1284. foreach ($values as $i=>$val) {
  1285. if ($key = $keys[$i]) {
  1286. $r[$key] = $val;
  1287. }
  1288. else {
  1289. $r[] = $val; // useless, PHP would have long aborted here
  1290. }
  1291. }
  1292. return($r);
  1293. }
  1294. }
  1295.  
  1296.  
  1297. #-- apply userfunction to each array element (descending recursively)
  1298. # use it like: array_walk_recursive($_POST, "stripslashes");
  1299. # - $callback can be static function name or object/method, class/method
  1300. if (!function_exists("array_walk_recursive")) {
  1301. function array_walk_recursive(&$input, $callback, $userdata=NULL) {
  1302. #-- each entry
  1303. foreach ($input as $key=>$value) {
  1304.  
  1305. #-- recurse for sub-arrays
  1306. if (is_array($value)) {
  1307. array_walk_recursive($input[$key], $callback, $userdata);
  1308. }
  1309.  
  1310. #-- $callback handles scalars
  1311. else {
  1312. call_user_func_array($callback, array(&$input[$key], $key, $userdata) );
  1313. }
  1314. }
  1315.  
  1316. // no return value
  1317. }
  1318. }
  1319.  
  1320.  
  1321. #-- complicated wrapper around substr() and and strncmp()
  1322. if (!function_exists("substr_compare")) {
  1323. function substr_compare($haystack, $needle, $offset=0, $len=0, $ci=0) {
  1324.  
  1325. #-- check params
  1326. if ($len <= 0) { // not well documented
  1327. $len = strlen($needle);
  1328. if (!$len) { return(0); }
  1329. }
  1330. #-- length exception
  1331. if ($len + $offset >= strlen($haystack)) {
  1332. trigger_error("substr_compare: given length exceeds main_str", E_USER_WARNING);
  1333. return(false);
  1334. }
  1335.  
  1336. #-- cut
  1337. if ($offset) {
  1338. $haystack = substr($haystack, $offset, $len);
  1339. }
  1340. #-- case-insensitivity
  1341. if ($ci) {
  1342. $haystack = strtolower($haystack);
  1343. $needle = strtolower($needle);
  1344. }
  1345.  
  1346. #-- do
  1347. return(strncmp($haystack, $needle, $len));
  1348. }
  1349. }
  1350.  
  1351.  
  1352. #-- stub, returns empty list as usual;
  1353. # you must load "ext/spl.php" beforehand to get this
  1354. if (!function_exists("spl_classes")) {
  1355. function spl_classes() {
  1356. trigger_error("spl_classes(): not built into this PHP version");
  1357. return (array)NULL;
  1358. }
  1359. }
  1360.  
  1361.  
  1362.  
  1363. #-- gets you list of class names the given objects class was derived from, slow
  1364. if (!function_exists("class_parents")) {
  1365. function class_parents($obj) {
  1366.  
  1367. #-- first get full list
  1368. $r = array();
  1369.  
  1370. #-- filter out
  1371. foreach ($all as $potential_parent) {
  1372. if (is_subclass_of($obj, $potential_parent)) {
  1373. $r[$potential_parent] = $potential_parent;
  1374. }
  1375. }
  1376. return($r);
  1377. }
  1378. }
  1379.  
  1380.  
  1381. #-- an alias
  1382. if (!function_exists("session_commit") && function_exists("session_write_close")) {
  1383. function session_commit() {
  1384. // simple
  1385. }
  1386. }
  1387.  
  1388.  
  1389. #-- aliases
  1390. if (!function_exists("dns_check_record")) {
  1391. function dns_check_record($host, $type=NULL) {
  1392. // synonym to
  1393. return checkdnsrr($host, $type);
  1394. }
  1395. }
  1396. if (!function_exists("dns_get_mx")) {
  1397. function dns_get_mx($host, $mx) {
  1398. $args = func_get_args();
  1399. // simple alias - except the optional, but referenced third parameter
  1400. if ($args[2]) {
  1401. $w = & $args[2];
  1402. }
  1403. else {
  1404. $w = false;
  1405. }
  1406. return getmxrr($host, $mx, $w);
  1407. }
  1408. }
  1409.  
  1410.  
  1411. #-- setrawcookie(),
  1412. # can this be emulated 100% exactly?
  1413. if (!function_exists("setrawcookie")) {
  1414. // we output everything directly as HTTP header(), PHP doesn't seem
  1415. // to manage an internal cookie list anyhow
  1416. function setrawcookie($name, $value=NULL, $expire=NULL, $path=NULL, $domain=NULL, $secure=0) {
  1417. if (isset($value) && strpbrk($value, ",; \r\t\n\f\014\013")) {
  1418. trigger_error("setrawcookie: value may not contain any of ',;
  1419. ' and some other control chars; thrown away", E_USER_WARNING);
  1420. }
  1421. else {
  1422. $h = "Set-Cookie: $name=$value"
  1423. . ($expire ? "; expires=" . gmstrftime("%a, %d-%b-%y %H:%M:%S %Z", $expire) : "")
  1424. . ($path ? "; path=$path": "")
  1425. . ($domain ? "; domain=$domain" : "")
  1426. . ($secure ? "; secure" : "");
  1427. header($h);
  1428. }
  1429. }
  1430. }
  1431.  
  1432.  
  1433. #-- write-at-once file access (counterpart to file_get_contents)
  1434. if (!function_exists("file_put_contents")) {
  1435. function file_put_contents($filename, $data, $flags=0, $resource=NULL) {
  1436.  
  1437. #-- prepare
  1438. $mode = ($flags & FILE_APPEND ? "a" : "w" ) ."b";
  1439. $incl = $flags & FILE_USE_INCLUDE_PATH;
  1440. $length = strlen($data);
  1441.  
  1442. #-- open for writing
  1443. $f = fopen($filename, $mode, $incl);
  1444. if ($f) {
  1445. $written = fwrite($f, $data);
  1446. fclose($f);
  1447.  
  1448. #-- only report success, if completely saved
  1449. return($length == $written);
  1450. }
  1451. }
  1452. }
  1453.  
  1454.  
  1455. #-- file-related constants
  1456. if (!defined("FILE_USE_INCLUDE_PATH")) { define("FILE_USE_INCLUDE_PATH", 1); }
  1457. if (!defined("FILE_IGNORE_NEW_LINES")) { define("FILE_IGNORE_NEW_LINES", 2); }
  1458. if (!defined("FILE_SKIP_EMPTY_LINES")) { define("FILE_SKIP_EMPTY_LINES", 4); }
  1459. if (!defined("FILE_APPEND")) { define("FILE_APPEND", 8); }
  1460. if (!defined("FILE_NO_DEFAULT_CONTEXT")) { define("FILE_NO_DEFAULT_CONTEXT", 16); }
  1461.  
  1462.  
  1463. #-- more new constants for 5.0
  1464. if (!defined("E_STRICT")) { define("E_STRICT", 2048); } // _STRICT is a special case of _NOTICE (_DEBUG)
  1465. # PHP_CONFIG_FILE_SCAN_DIR
  1466.  
  1467.  
  1468. #-- array count_recursive()
  1469. if (!defined("COUNT_NORMAL")) { define("COUNT_NORMAL", 0); } // count($array, 0);
  1470. if (!defined("COUNT_RECURSIVE")) { define("COUNT_RECURSIVE", 1); } // not supported
  1471.  
  1472.  
  1473. #-- we introduce a new function, because we cannot emulate the
  1474. # newly introduced second parameter to count()
  1475. if (!function_exists("count_recursive")) {
  1476. function count_recursive($array, $mode=1) {
  1477. if (!$mode) {
  1478. return(count($array));
  1479. }
  1480. else {
  1481. $c = count($array);
  1482. foreach ($array as $sub) {
  1483. if (is_array($sub)) {
  1484. $c += count_recursive($sub);
  1485. }
  1486. }
  1487. return($c);
  1488. }
  1489. }
  1490. }
  1491.  
  1492.  
  1493.  
  1494.  
  1495.  
  1496. /**
  1497.  * ------------------------------ 4.4 ---
  1498.  * @group 4_4
  1499.  * @since 4.4
  1500.  *
  1501.  * PHP 4.4 is a bugfix and backporting version created after PHP 5. It went
  1502.  * mostly unchanged, but changes a few language semantics (e.g. references).
  1503.  *
  1504.  * @emulated
  1505.  * PHP_INT_SIZE
  1506.  * PHP_INT_MAX
  1507.  * SORT_LOCALE_STRING
  1508.  *
  1509.  */
  1510.  
  1511. if (!defined("PHP_INT_SIZE")) { define("PHP_INT_SIZE", 4); }
  1512. if (!defined("PHP_INT_MAX")) { define("PHP_INT_MAX", 2147483647); }
  1513. if (!defined("SORT_LOCALE_STRING")) { define("SORT_LOCALE_STRING", 5); }
  1514.  
  1515.  
  1516.  
  1517.  
  1518.  
  1519.  
  1520. /**
  1521.  * ------------------------------ 4.3 ---
  1522.  * @group 4_3
  1523.  * @since 4.3
  1524.  *
  1525.  * Additions in 4.3 version of PHP interpreter.
  1526.  *
  1527.  * @emulated
  1528.  * file_get_contents
  1529.  * array_key_exists
  1530.  * array_intersect_assoc
  1531.  * array_diff_assoc
  1532.  * html_entity_decode
  1533.  * str_word_count
  1534.  * str_shuffle
  1535.  * get_include_path
  1536.  * set_include_path
  1537.  * restore_include_path
  1538.  * fnmatch
  1539.  * FNM_PATHNAME
  1540.  * FNM_NOESCAPE
  1541.  * FNM_PERIOD
  1542.  * FNM_LEADING_DIR
  1543.  * FNM_CASEFOLD
  1544.  * FNM_EXTMATCH
  1545.  * glob
  1546.  * GLOB_MARK
  1547.  * GLOB_NOSORT
  1548.  * GLOB_NOCHECK
  1549.  * GLOB_NOESCAPE
  1550.  * GLOB_BRACE
  1551.  * GLOB_ONLYDIR
  1552.  * GLOB_NOCASE
  1553.  * GLOB_DOTS
  1554.  * __FUNCTION__
  1555.  * PATH_SEPARATOR
  1556.  * PHP_SHLIB_SUFFIX
  1557.  * PHP_SAPI
  1558.  * PHP_PREFIX
  1559.  *
  1560.  * @missing
  1561.  * sha1_file
  1562.  * sha1 - too much code, and has been reimplemented elsewhere
  1563.  *
  1564.  * @unimplementable
  1565.  * money_format
  1566.  *
  1567.  */
  1568.  
  1569.  
  1570. #-- simplified file read-at-once function
  1571. if (!function_exists("file_get_contents")) {
  1572. function file_get_contents($filename, $use_include_path=1) {
  1573.  
  1574. #-- open file, let fopen() report error
  1575. $f = fopen($filename, "rb", $use_include_path);
  1576. if (!$f) { return; }
  1577.  
  1578. #-- read max 2MB
  1579. $content = fread($f, 1<<21);
  1580. fclose($f);
  1581. return($content);
  1582. }
  1583. }
  1584.  
  1585.  
  1586.  
  1587. #-- shell-like filename matching (* and ? globbing characters)
  1588. if (!function_exists("fnmatch")) {
  1589.  
  1590. #-- associated constants
  1591. if (!defined("FNM_PATHNAME")) { define("FNM_PATHNAME", 1<<0); } // no wildcard ever matches a "/"
  1592. if (!defined("FNM_NOESCAPE")) { define("FNM_NOESCAPE", 1<<1); } // backslash can't escape meta chars
  1593. if (!defined("FNM_PERIOD")) { define("FNM_PERIOD", 1<<2); } // leading dot must be given explicit
  1594. if (!defined("FNM_LEADING_DIR")) { define("FNM_LEADING_DIR", 1<<3); } // not in PHP
  1595. if (!defined("FNM_CASEFOLD")) { define("FNM_CASEFOLD", 0x50); } // match case-insensitive
  1596. if (!defined("FNM_EXTMATCH")) { define("FNM_EXTMATCH", 1<<5); } // not in PHP
  1597.  
  1598. #-- implementation
  1599. function fnmatch($pattern, $str, $flags=0x0000) {
  1600.  
  1601. #-- 'hidden' files
  1602. if ($flags & FNM_PERIOD) {
  1603. if (($str[0] == ".") && ($pattern[0] != ".")) {
  1604. return(false); // abort early
  1605. }
  1606. }
  1607.  
  1608. #-- case-insensitivity
  1609. $rxci = "";
  1610. if ($flags & FNM_CASEFOLD) {
  1611. $rxci = "i";
  1612. }
  1613. #-- handline of pathname separators (/)
  1614. $wild = ".";
  1615. if ($flags & FNM_PATHNAME) {
  1616. $wild = "[^/".DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR."]";
  1617. }
  1618.  
  1619. #-- check for cached regular expressions
  1620. static $cmp = array();
  1621. if (isset($cmp["$pattern+$flags"])) {
  1622. $rx = $cmp["$pattern+$flags"];
  1623. }
  1624.  
  1625. #-- convert filename globs into regex
  1626. else {
  1627. $rx = preg_quote($pattern);
  1628. $rx = strtr($rx, array(
  1629. "\\*"=>"$wild*?", "\\?"=>"$wild", "\\["=>"[", "\\]"=>"]",
  1630. ));
  1631. $rx = "{^" . $rx . "$}" . $rxci;
  1632.  
  1633. #-- cache
  1634. if (count($cmp) >= 50) {
  1635. $cmp = array(); // free
  1636. }
  1637. $cmp["$pattern+$flags"] = $rx;
  1638. }
  1639.  
  1640. #-- compare
  1641. return(preg_match($rx, $str));
  1642. }
  1643. }
  1644.  
  1645.  
  1646. #-- file search and name matching (with shell patterns)
  1647. if (!function_exists("glob")) {
  1648.  
  1649. #-- introduced constants
  1650. if (!defined("GLOB_MARK")) { define("GLOB_MARK", 1<<0); }
  1651. if (!defined("GLOB_NOSORT")) { define("GLOB_NOSORT", 1<<1); }
  1652. if (!defined("GLOB_NOCHECK")) { define("GLOB_NOCHECK", 1<<2); }
  1653. if (!defined("GLOB_NOESCAPE")) { define("GLOB_NOESCAPE", 1<<3); }
  1654. if (!defined("GLOB_BRACE")) { define("GLOB_BRACE", 1<<4); }
  1655. if (!defined("GLOB_ONLYDIR")) { define("GLOB_ONLYDIR", 1<<5); }
  1656. if (!defined("GLOB_NOCASE")) { define("GLOB_NOCASE", 1<<6); }
  1657. if (!defined("GLOB_DOTS")) { define("GLOB_DOTS", 1<<7); }
  1658. // unlikely to work under Win(?), without replacing the explode() with
  1659. // a preg_split() incorporating the native DIRECTORY_SEPARATOR as well
  1660.  
  1661. #-- implementation
  1662. function glob($pattern, $flags=0x0000) {
  1663. $ls = array();
  1664. $rxci = ($flags & GLOB_NOCASE) ? "i" : "";
  1665. #echo "\n=> glob($pattern)...\n";
  1666.  
  1667. #-- transform glob pattern into regular expression
  1668. # (similar to fnmatch() but still different enough to require a second func)
  1669. if ($pattern) {
  1670.  
  1671. #-- look at each directory/fn spec part separately
  1672. $parts2 = explode("/", $pattern);
  1673. $pat = preg_quote($pattern);
  1674. $pat = strtr($pat, array("\\*"=>".*?", "\\?"=>".?"));
  1675. if ($flags ^ GLOB_NOESCAPE) {
  1676. // uh, oh, ouuch - the above is unclean enough...
  1677. }
  1678. if ($flags ^ GLOB_BRACE) {
  1679. $pat = preg_replace("/\{(.+?)\}/e", 'strtr("[$1]", ",", "")', $pat);
  1680. }
  1681. $parts = explode("/", $pat);
  1682. #echo "parts == ".implode(" // ", $parts) . "\n";
  1683. $lasti = count($parts) - 1;
  1684. $dn = "";
  1685. foreach ($parts as $i=>$p) {
  1686.  
  1687. #-- basedir included (yet no pattern matching necessary)
  1688. if (!strpos($p, "*?") && (strpos($p, ".?")===false)) {
  1689. $dn .= $parts2[$i] . ($i!=$lasti ? "/" : "");
  1690. #echo "skip:$i, cause no pattern matching char found -> only a basedir spec\n";
  1691. continue;
  1692. }
  1693.  
  1694. #-- start reading dir + match filenames against current pattern
  1695. if ($dh = opendir($dn ?$dn:'.')) {
  1696. $with_dot = ($p[1]==".") || ($flags & GLOB_DOTS);
  1697. #echo "part:$i:$p\n";
  1698. #echo "reading dir \"$dn\"\n";
  1699. while ($fn = readdir($dh)) {
  1700. if (preg_match("\007^$p$\007$rxci", $fn)) {
  1701.  
  1702. #-- skip over 'hidden' files
  1703. if (($fn[0] == ".") && !$with_dot) {
  1704. continue;
  1705. }
  1706.  
  1707. #-- add filename only if last glob/pattern part
  1708. if ($i==$lasti) {
  1709. if (is_dir("$dn$fn")) {
  1710. if ($flags & GLOB_ONLYDIR) {
  1711. continue;
  1712. }
  1713. if ($flags & GLOB_MARK) {
  1714. $fn .= "/";
  1715. }
  1716. }
  1717. #echo "adding '$fn' for dn=$dn to list\n";
  1718. $ls[] = "$dn$fn";
  1719. }
  1720.  
  1721. #-- initiate a subsearch, merge result list in
  1722. elseif (is_dir("$dn$fn")) {
  1723. // add reamaining search patterns to current basedir
  1724. $remaind = implode("/", array_slice($parts2, $i+1));
  1725. $ls = array_merge($ls, glob("$dn$fn/$remaind", $flags));
  1726. }
  1727. }
  1728. }
  1729. closedir($dh);
  1730.  
  1731. #-- prevent scanning a 2nd part/dir in same glob() instance:
  1732. break;
  1733. }
  1734.  
  1735. #-- given dirname doesn't exist
  1736. else {
  1737. return($ls);
  1738. }
  1739.  
  1740. }// foreach $parts
  1741. }
  1742.  
  1743. #-- return result list
  1744. if (!$ls && ($flags & GLOB_NOCHECK)) {
  1745. $ls[] = $pattern;
  1746. }
  1747. if ($flags ^ GLOB_NOSORT) {
  1748. sort($ls);
  1749. }
  1750. #print_r($ls);
  1751. #echo "<=\n";
  1752. return($ls);
  1753. }
  1754. } //@FIX: fully comment, remove debugging code (- as soon as it works ;)
  1755.  
  1756.  
  1757. #-- redundant alias for isset()
  1758. if (!function_exists("array_key_exists")) {
  1759. function array_key_exists($key, $search) {
  1760. return isset($search[$key]);
  1761. }
  1762. }
  1763.  
  1764.  
  1765. #-- who could need that?
  1766. if (!function_exists("array_intersect_assoc")) {
  1767. function array_intersect_assoc( /*array, array, array...*/ ) {
  1768.  
  1769. #-- parameters, prepare
  1770. $in = func_get_args();
  1771. $cmax = count($in);
  1772. $whatsleftover = array();
  1773.  
  1774. #-- walk through each array pair
  1775. # (take first as checklist)
  1776. foreach ($in[0] as $i => $v) {
  1777. for ($c = 1; $c < $cmax; $c++) {
  1778. #-- remove entry, as soon as it isn't present
  1779. # in one of the other arrays
  1780. if (!isset($in[$c][$i]) || (@$in[$c][$i] !== $v)) {
  1781. continue 2;
  1782. }
  1783. }
  1784. #-- it was found in all other arrays
  1785. $whatsleftover[$i] = $v;
  1786. }
  1787. return $whatsleftover;
  1788. }
  1789. }
  1790.  
  1791.  
  1792. #-- the opposite of the above
  1793. if (!function_exists("array_diff_assoc")) {
  1794. function array_diff_assoc( /*array, array, array...*/ ) {
  1795.  
  1796. #-- params
  1797. $in = func_get_args();
  1798. $diff = array();
  1799.  
  1800. #-- compare each array with primary/first
  1801. foreach ($in[0] as $i=>$v) {
  1802. for ($c=1; $c<count($in); $c++) {
  1803. #-- skip as soon as it matches with entry in another array
  1804. if (isset($in[$c][$i]) && ($in[$c][$i] == $v)) {
  1805. continue 2;
  1806. }
  1807. }
  1808. #-- else
  1809. $diff[$i] = $v;
  1810. }
  1811. return $diff;
  1812. }
  1813. }
  1814.  
  1815.  
  1816. #-- opposite of htmlentities
  1817. if (!function_exists("html_entity_decode")) {
  1818. function html_entity_decode($string, $quote_style=ENT_COMPAT, $charset="ISO-8859-1") {
  1819. //@FIX: we fall short on anything other than Latin-1
  1820. $y = array_flip(get_html_translation_table(HTML_ENTITIES, $quote_style));
  1821. return strtr($string, $y);
  1822. }
  1823. }
  1824.  
  1825.  
  1826. #-- extracts single words from a string
  1827. if (!function_exists("str_word_count")) {
  1828. function str_word_count($string, $result=0) {
  1829.  
  1830. #-- let someone else do the work
  1831. preg_match_all('/([\w](?:[-\'\w]?[\w]+)*)/', $string, $uu);
  1832.  
  1833. #-- return full word list
  1834. if ($result == 1) {
  1835. return($uu[1]);
  1836. }
  1837.  
  1838. #-- array() of $pos=>$word entries
  1839. elseif ($result >= 2) {
  1840. $r = array();
  1841. $l = 0;
  1842. foreach ($uu[1] as $word) {
  1843. $l = strpos($string, $word, $l);
  1844. $r[$l] = $word;
  1845. $l += strlen($word); // speed up next search
  1846. }
  1847. return($r);
  1848. }
  1849.  
  1850. #-- only count
  1851. else {
  1852. return(count($uu[1]));
  1853. }
  1854. }
  1855. }
  1856.  
  1857.  
  1858. #-- creates a permutation of the given strings characters
  1859. # (let's hope the random number generator was alread initialized)
  1860. if (!function_exists("str_shuffle")) {
  1861. function str_shuffle($str) {
  1862. $r = "";
  1863.  
  1864. #-- cut string down with every iteration
  1865. while (strlen($str)) {
  1866. $n = strlen($str) - 1;
  1867. if ($n) {
  1868. $n = rand(0, $n); // glibcs` rand is ok since 2.1 at least
  1869. }
  1870.  
  1871. #-- cut out elected char, add to result string
  1872. $r .= $str{$n};
  1873. $str = substr($str, 0, $n) . substr($str, $n + 1);
  1874. }
  1875. return($r);
  1876. }
  1877. }
  1878.  
  1879.  
  1880. #-- simple shorthands
  1881. if (!function_exists("get_include_path")) {
  1882. function get_include_path() {
  1883. return(get_cfg_var("include_path"));
  1884. }
  1885. function set_include_path($new) {
  1886. return ini_set("include_path", $new);
  1887. }
  1888. function restore_include_path() {
  1889. ini_restore("include_path");
  1890. }
  1891. }
  1892.  
  1893.  
  1894. #-- constants for 4.3
  1895. if (!defined("PATH_SEPARATOR")) { define("PATH_SEPARATOR", ((DIRECTORY_SEPARATOR=='\\') ? ';' :':')); }
  1896. if (!defined("PHP_SHLIB_SUFFIX")) { define("PHP_SHLIB_SUFFIX", ((DIRECTORY_SEPARATOR=='\\') ? 'dll' :'so')); }
  1897. if (!defined("PHP_SAPI")) { define("PHP_SAPI", php_sapi_name()); }
  1898. if (!defined("__FUNCTION__")) { define("__FUNCTION__", NULL); } // empty string would signalize main()
  1899.  
  1900.  
  1901. #-- not identical to what PHP reports (it seems to `which` for itself)
  1902. if (!defined("PHP_PREFIX") && isset($_ENV["_"])) { define("PHP_PREFIX", substr($_ENV["_"], 0, strpos($_ENV["_"], "bin/"))); }
  1903.  
  1904.  
  1905.  
  1906.  
  1907.  
  1908.  
  1909. #------------------------------------------------------------------ 4.2 ---
  1910. # almost complete!?
  1911.  
  1912.  
  1913. #-- shy away from this one - it was broken in all real PHP4.2 versions, and
  1914. # this function emulation script won't change that
  1915. if (!function_exists("str_rot13")) {
  1916. function str_rot13($str) {
  1917. static $from = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  1918. static $to = "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm";
  1919. return strtr($str, $from, $to);
  1920. }
  1921. }
  1922.  
  1923.  
  1924. #-- well, if you need it
  1925. if (!function_exists("array_change_key_case")) {
  1926.  
  1927. #-- introduced constants
  1928. if (!defined("CASE_LOWER")) { define("CASE_LOWER", 0); }
  1929. if (!defined("CASE_UPPER")) { define("CASE_UPPER", 1); }
  1930.  
  1931. #-- implementation
  1932. function array_change_key_case($array, $case=CASE_LOWER) {
  1933.  
  1934. #-- loop through
  1935. foreach ($array as $i=>$v) {
  1936. #-- do anything for strings only
  1937. if (is_string($i)) {
  1938. unset($array[$i]);
  1939. $i = ($case==CASE_LOWER) ? strtolower($i) : strtoupper($i);
  1940. $array[$i] = $v;
  1941. }
  1942. // non-recursive
  1943. }
  1944. return($array);
  1945. }
  1946. }
  1947.  
  1948.  
  1949. #-- create fixed-length array made up of $value data
  1950. if (!function_exists("array_fill")) {
  1951. function array_fill($start_index, $num, $value) {
  1952.  
  1953. #-- params
  1954. $r = array();
  1955. $i = $start_index;
  1956. $end = $num + $start_index;
  1957.  
  1958. #-- append
  1959. for (; $i < $end; $i++)
  1960. {
  1961. $r[$i] = $value;
  1962. }
  1963. return($r);
  1964. }
  1965. }
  1966.  
  1967.  
  1968. #-- split an array into evenly sized parts
  1969. if (!function_exists("array_chunk")) {
  1970. function array_chunk($input, $size, $preserve_keys=false) {
  1971.  
  1972. #-- array for chunked output
  1973. $r = array();
  1974. $n = -1; // chunk index
  1975.  
  1976. #-- enum input array blocks
  1977. foreach ($input as $i=>$v) {
  1978.  
  1979. #-- new chunk
  1980. if (($n < 0) || (count($r[$n]) == $size)) {
  1981. $n++;
  1982. $r[$n] = array();
  1983. }
  1984.  
  1985. #-- add input value into current [$n] chunk
  1986. if ($preserve_keys) {
  1987. $r[$n][$i] = $v;
  1988. }
  1989. else {
  1990. $r[$n][] = $v;
  1991. }
  1992. }
  1993. return($r);
  1994. }
  1995. }
  1996.  
  1997.  
  1998. #-- convenience wrapper
  1999. if (!function_exists("md5_file")) {
  2000. function md5_file($filename, $raw_output=false) {
  2001.  
  2002. #-- read file, apply hash function
  2003. $data = file_get_contents($filename, "rb");
  2004. $r = md5($data);
  2005. $data = NULL;
  2006.  
  2007. #-- transform? and return
  2008. if ($raw_output) {
  2009. $r = pack("H*", $r);
  2010. }
  2011. return $r;
  2012. }
  2013. }
  2014.  
  2015.  
  2016. #-- object type checking
  2017. if (!function_exists("is_a")) {
  2018. function is_a($obj, $classname) {
  2019.  
  2020. #-- lowercase everything for comparison
  2021. $classnaqme = strtolower($classname);
  2022. $obj_class = strtolower(get_class($obj));
  2023.  
  2024. #-- two possible checks
  2025. return ($obj_class == $classname) or is_subclass_of($obj, $classname);
  2026. }
  2027. }
  2028.  
  2029.  
  2030. #-- floating point modulo
  2031. if (!function_exists("fmod")) {
  2032. function fmod($x, $y) {
  2033. $r = $x / $y;
  2034. $r -= (int)$r;
  2035. $r *= $y;
  2036. return($r);
  2037. }
  2038. }
  2039.  
  2040.  
  2041. #-- makes float variable from string
  2042. if (!function_exists("floatval")) {
  2043. function floatval($str) {
  2044. $str = ltrim($str);
  2045. return (float)$str;
  2046. }
  2047. }
  2048.  
  2049.  
  2050. #-- floats
  2051. if (!function_exists("is_infinite")) {
  2052.  
  2053. #-- constants as-is
  2054. if (!defined("NAN")) { define("NAN", "NAN"); }
  2055. if (!defined("INF")) { define("INF", "INF"); } // there is also "-INF"
  2056.  
  2057. #-- simple checks
  2058. function is_infinite($f) {
  2059. $s = (string)$f;
  2060. return( ($s=="INF") || ($s=="-INF") );
  2061. }
  2062. function is_nan($f) {
  2063. $s = (string)$f;
  2064. return( $s=="NAN" );
  2065. }
  2066. function is_finite($f) {
  2067. $s = (string)$f;
  2068. return( !strpos($s, "N") );
  2069. }
  2070. }
  2071.  
  2072.  
  2073. #-- throws value-instantiation PHP-code for given variable
  2074. # (a bit different from the standard, was intentional for its orig use)
  2075. if (!function_exists("var_export")) {
  2076. function var_export($var, $return=false, $indent="", $output="") {
  2077.  
  2078. #-- output as in-class variable definitions
  2079. if (is_object($var)) {
  2080. $output = "class " . get_class($var) . " {\n";
  2081. foreach (((array)$var) as $id=>$var) {
  2082. $output .= " var \$$id = " . var_export($var, true) . ";\n";
  2083. }
  2084. $output .= "}";
  2085. }
  2086.  
  2087. #-- array constructor
  2088. elseif (is_array($var)) {
  2089. foreach ($var as $id=>$next) {
  2090. if ($output) $output .= ",\n";
  2091. else $output = "array(\n";
  2092. $output .= $indent . ' '
  2093. . (is_numeric($id) ? $id : '"'.addslashes($id).'"')
  2094. . ' => ' . var_export($next, true, "$indent ");
  2095. }
  2096. if (empty($output)) $output = "array(";
  2097. $output .= "\n{$indent})";
  2098. #if ($indent == "") $output .= ";";
  2099. }
  2100.  
  2101. #-- literals
  2102. elseif (is_numeric($var)) {
  2103. $output = "$var";
  2104. }
  2105. elseif (is_bool($var)) {
  2106. $output = $var ? "true" : "false";
  2107. }
  2108. else {
  2109. $output = "'" . preg_replace("/([\\\\\'])/", '\\\\$1', $var) . "'";
  2110. }
  2111.  
  2112. #-- done
  2113. if ($return) {
  2114. return($output);
  2115. }
  2116. else {
  2117. print($output);
  2118. }
  2119. }
  2120. }
  2121.  
  2122.  
  2123. #-- strcmp() variant that respects locale setting,
  2124. # existed since PHP 4.0.5, but under Win32 first since 4.3.2
  2125. if (!function_exists("strcoll")) {
  2126. function strcoll($str1, $str2) {
  2127. return strcmp($str1, $str2);
  2128. }
  2129. }
  2130.  
  2131.  
  2132.  
  2133.  
  2134.  
  2135. #------------------------------------------------------------------ 4.1 ---
  2136. # nl_langinfo - unimpl?
  2137. # getmygid
  2138. # version_compare
  2139. #
  2140. # See also "ext/math41.php" for some more (rarely used mathematical funcs).
  2141.  
  2142.  
  2143.  
  2144.  
  2145. #-- aliases (an earlier fallen attempt to unify PHP function names)
  2146. if (!function_exists("diskfreespace")) {
  2147. function diskfreespace() {
  2148. return disk_free_sapce();
  2149. }
  2150. function disktotalspace() {
  2151. return disk_total_sapce();
  2152. }
  2153. }
  2154.  
  2155.  
  2156. #-- variable count of arguments (in array list) printf variant
  2157. if (!function_exists("vprintf")) {
  2158. function vprintf($format, $args=NULL) {
  2159. call_user_func_array("fprintf", get_func_args());
  2160. }
  2161. }
  2162.  
  2163.  
  2164. #-- same as above, but doesn't output directly and returns formatted string
  2165. if (!function_exists("vsprintf")) {
  2166. function vsprintf($format, $args=NULL) {
  2167. $args = array_merge(array($format), array_values((array)$args));
  2168. return call_user_func_array("sprintf", $args);
  2169. }
  2170. }
  2171.  
  2172.  
  2173. #-- can be used to simulate a register_globals=on environment
  2174. if (!function_exists("import_request_variables")) {
  2175. function import_request_variables($types="GPC", $pfix="") {
  2176.  
  2177. #-- associate abbreviations to global var names
  2178. $alias = array(
  2179. "G" => "_GET",
  2180. "P" => "_POST",
  2181. "C" => "_COOKIE",
  2182. "S" => "_SERVER", // non-standard
  2183. "E" => "_ENV", // non-standard
  2184. );
  2185. #-- alias long names (PHP < 4.0.6)
  2186. if (!isset($_REQUEST)) {
  2187. $_GET = & $HTTP_GET_VARS;
  2188. $_POST = & $HTTP_POST_VARS;
  2189. $_COOKIE = & $HTTP_COOKIE_VARS;
  2190. }
  2191.  
  2192. #-- copy
  2193. for ($i=0; $i<strlen($types); $i++) {
  2194. if ($FROM = $alias[strtoupper($c)]) {
  2195. foreach ($$FROM as $key=>$val) {
  2196. if (!isset($GLOBALS[$pfix.$key])) {
  2197. $GLOBALS[$pfix . $key] = $val;
  2198. }
  2199. }
  2200. }
  2201. }
  2202. // done
  2203. }
  2204. }
  2205.  
  2206.  
  2207. // a few mathematical functions follow
  2208. // (wether we should really emulate them is a different question)
  2209.  
  2210. #-- me has no idea what this function means
  2211. if (!function_exists("hypot")) {
  2212. function hypot($num1, $num2) {
  2213. return sqrt($num1*$num1 + $num2*$num2); // as per PHP manual ;)
  2214. }
  2215. }
  2216.  
  2217. #-- more accurate logarithm func, but we cannot simulate it
  2218. # (too much work, too slow in PHP)
  2219. if (!function_exists("log1p")) {
  2220. function log1p($x) {
  2221. return( log(1+$x) );
  2222. }
  2223. #-- same story for:
  2224. function expm1($x) {
  2225. return( exp($x)-1 );
  2226. }
  2227. }
  2228.  
  2229. #-- as per PHP manual
  2230. if (!function_exists("sinh")) {
  2231. function sinh($f) {
  2232. return( (exp($f) - exp(-$f)) / 2 );
  2233. }
  2234. function cosh($f) {
  2235. return( (exp($f) + exp(-$f)) / 2 );
  2236. }
  2237. function tanh($f) {
  2238. return( sinh($f) / cosh($f) ); // ok, that one makes sense again :)
  2239. }
  2240. }
  2241.  
  2242. #-- these look a bit more complicated
  2243. if (!function_exists("asinh")) {
  2244. function asinh($x) {
  2245. return( log($x + sqrt($x*$x+1)) );
  2246. }
  2247. function acosh($x) {
  2248. return( log($x + sqrt($x*$x-1)) );
  2249. }
  2250. function atanh($x) {
  2251. return( log1p( 2*$x / (1-$x) ) / 2 );
  2252. }
  2253. }
  2254.  
  2255.  
  2256. #-- HMAC from RFC2104, but see also PHP_Compat and Crypt_HMAC
  2257. if (!function_exists("mhash")) {
  2258.  
  2259. #-- constants
  2260. if (!defined("MHASH_CRC32")) { define("MHASH_CRC32", 0); }
  2261. if (!defined("MHASH_MD5")) { define("MHASH_MD5", 1); } // RFC1321
  2262. if (!defined("MHASH_SHA1")) { define("MHASH_SHA1", 2); } // RFC3174
  2263. if (!defined("MHASH_TIGER")) { define("MHASH_TIGER", 7); }
  2264. if (!defined("MHASH_MD4")) { define("MHASH_MD4", 16); } // RFC1320
  2265. if (!defined("MHASH_SHA256")) { define("MHASH_SHA256", 17); }
  2266. if (!defined("MHASH_ADLER32")) { define("MHASH_ADLER32", 18); }
  2267.  
  2268. #-- implementation
  2269. function mhash($hashtype, $text, $key) {
  2270.  
  2271. #-- hash function
  2272. if (!($func = mhash_get_hash_name($hashtype)) || !function_exists($func)) {
  2273. return trigger_error("mhash: cannot use hash algorithm #$hashtype/$func", E_USER_ERROR);
  2274. }
  2275. if (!$key) {
  2276. trigger_error("mhash: called without key", E_USER_WARNING);
  2277. }
  2278.  
  2279. #-- params
  2280. $bsize = mhash_get_block_size($hashtype); // fixed size, 64
  2281.  
  2282. #-- pad key
  2283. if (strlen($key) > $bsize) { // hash key, when it's too long
  2284. $key = $func($key);
  2285. $key = pack("H*", $key); // binarify
  2286. }
  2287. $key = str_pad($key, $bsize, "\0"); // fill up with NULs (1)
  2288.  
  2289. #-- prepare inner and outer padding stream
  2290. $ipad = str_pad("", $bsize, "6"); // %36
  2291. $opad = str_pad("", $bsize, "\\"); // %5C
  2292.  
  2293. #-- call hash func // php can XOR strings for us
  2294. $dgst = pack("H*", $func( ($key ^ $ipad) . $text )); // (2,3,4)
  2295. $dgst = pack("H*", $func( ($key ^ $opad) . $dgst )); // (5,6,7)
  2296. return($dgst);
  2297. }
  2298.  
  2299. #-- return which hash functions are implemented
  2300. function mhash_count() {
  2301. return(MHASH_SHA1);
  2302. }
  2303.  
  2304. #-- map numeric identifier to hash function name
  2305. function mhash_get_hash_name($i) {
  2306. static $hash_funcs = array(
  2307. MHASH_CRC32 => "crc32", // would need dechex()ing in main func?
  2308. MHASH_MD5 => "md5",
  2309. MHASH_SHA1 => "sha1",
  2310. );
  2311. return(strtoupper($hash_funcs[$i]));
  2312. }
  2313.  
  2314. #-- static value
  2315. function mhash_get_block_size($i) {
  2316. return(64);
  2317. }
  2318. }
  2319.  
  2320.  
  2321.  
  2322. #-- other stuff
  2323. /*
  2324.   removed funcs??
  2325.   [18] => leak
  2326. */
  2327.  
  2328.  
  2329.  
  2330. #-- pre-4.1 -- end
  2331. // no need to implement anything below that, because such old versions
  2332. // will be incompatbile anyhow (- none of the newer superglobals known),
  2333. // but see also "ext/old"
  2334.  
  2335.  
  2336. ?>

URL: http://www.google.com/codesearch?hl=de&q=+json_encode+show:LhmoV3E-2fk:X3Z72SIYcBg:BIe4as29lCU&sa=N&cd=20&ct=rc&cs_p=http://fresh.t-systems-sfr.com/unix/src/privat2/upgradephp-14.tgz&cs_f=upgradephp-14/upgrade.php

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.