compat.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. /**********************************************************\
  3. | |
  4. | The implementation of PHPRPC Protocol 3.0 |
  5. | |
  6. | compat.php |
  7. | |
  8. | Release 3.0.1 |
  9. | Copyright by Team-PHPRPC |
  10. | |
  11. | WebSite: http://www.phprpc.org/ |
  12. | http://www.phprpc.net/ |
  13. | http://www.phprpc.com/ |
  14. | http://sourceforge.net/projects/php-rpc/ |
  15. | |
  16. | Authors: Ma Bingyao <andot@ujn.edu.cn> |
  17. | |
  18. | This file may be distributed and/or modified under the |
  19. | terms of the GNU General Public License (GPL) version |
  20. | 2.0 as published by the Free Software Foundation and |
  21. | appearing in the included file LICENSE. |
  22. | |
  23. \**********************************************************/
  24. /* Provides missing functionality for older versions of PHP.
  25. *
  26. * Copyright: Ma Bingyao <andot@ujn.edu.cn>
  27. * Version: 1.5
  28. * LastModified: Apr 12, 2010
  29. * This library is free. You can redistribute it and/or modify it under GPL.
  30. */
  31. require_once("phprpc_date.php");
  32. if (!function_exists('file_get_contents')) {
  33. function file_get_contents($filename, $incpath = false, $resource_context = null) {
  34. if (false === $fh = fopen($filename, 'rb', $incpath)) {
  35. user_error('file_get_contents() failed to open stream: No such file or directory',
  36. E_USER_WARNING);
  37. return false;
  38. }
  39. clearstatcache();
  40. if ($fsize = @filesize($filename)) {
  41. $data = fread($fh, $fsize);
  42. }
  43. else {
  44. $data = '';
  45. while (!feof($fh)) {
  46. $data .= fread($fh, 8192);
  47. }
  48. }
  49. fclose($fh);
  50. return $data;
  51. }
  52. }
  53. if (!function_exists('ob_get_clean')) {
  54. function ob_get_clean() {
  55. $contents = ob_get_contents();
  56. if ($contents !== false) ob_end_clean();
  57. return $contents;
  58. }
  59. }
  60. /**
  61. 3 more bugs found and fixed:
  62. 1. failed to work when the gz contained a filename - FIXED
  63. 2. failed to work on 64-bit architecture (checksum) - FIXED
  64. 3. failed to work when the gz contained a comment - cannot verify.
  65. Returns some errors (not all!) and filename.
  66. */
  67. function gzdecode($data, &$filename = '', &$error = '', $maxlength = null) {
  68. $len = strlen($data);
  69. if ($len < 18 || strcmp(substr($data, 0, 2), "\x1f\x8b")) {
  70. $error = "Not in GZIP format.";
  71. return null; // Not GZIP format (See RFC 1952)
  72. }
  73. $method = ord(substr($data, 2, 1)); // Compression method
  74. $flags = ord(substr($data, 3, 1)); // Flags
  75. if ($flags & 31 != $flags) {
  76. $error = "Reserved bits not allowed.";
  77. return null;
  78. }
  79. // NOTE: $mtime may be negative (PHP integer limitations)
  80. $mtime = unpack("V", substr($data, 4, 4));
  81. $mtime = $mtime[1];
  82. $xfl = substr($data, 8, 1);
  83. $os = substr($data, 8, 1);
  84. $headerlen = 10;
  85. $extralen = 0;
  86. $extra = "";
  87. if ($flags & 4) {
  88. // 2-byte length prefixed EXTRA data in header
  89. if ($len - $headerlen - 2 < 8) {
  90. return false; // invalid
  91. }
  92. $extralen = unpack("v", substr($data, 8, 2));
  93. $extralen = $extralen[1];
  94. if ($len - $headerlen - 2 - $extralen < 8) {
  95. return false; // invalid
  96. }
  97. $extra = substr($data, 10, $extralen);
  98. $headerlen += 2 + $extralen;
  99. }
  100. $filenamelen = 0;
  101. $filename = "";
  102. if ($flags & 8) {
  103. // C-style string
  104. if ($len - $headerlen - 1 < 8) {
  105. return false; // invalid
  106. }
  107. $filenamelen = strpos(substr($data, $headerlen), chr(0));
  108. if ($filenamelen === false || $len - $headerlen - $filenamelen - 1 < 8) {
  109. return false; // invalid
  110. }
  111. $filename = substr($data, $headerlen, $filenamelen);
  112. $headerlen += $filenamelen + 1;
  113. }
  114. $commentlen = 0;
  115. $comment = "";
  116. if ($flags & 16) {
  117. // C-style string COMMENT data in header
  118. if ($len - $headerlen - 1 < 8) {
  119. return false; // invalid
  120. }
  121. $commentlen = strpos(substr($data, $headerlen), chr(0));
  122. if ($commentlen === false || $len - $headerlen - $commentlen - 1 < 8) {
  123. return false; // Invalid header format
  124. }
  125. $comment = substr($data, $headerlen, $commentlen);
  126. $headerlen += $commentlen + 1;
  127. }
  128. $headercrc = "";
  129. if ($flags & 2) {
  130. // 2-bytes (lowest order) of CRC32 on header present
  131. if ($len - $headerlen - 2 < 8) {
  132. return false; // invalid
  133. }
  134. $calccrc = crc32(substr($data, 0, $headerlen)) & 0xffff;
  135. $headercrc = unpack("v", substr($data, $headerlen, 2));
  136. $headercrc = $headercrc[1];
  137. if ($headercrc != $calccrc) {
  138. $error = "Header checksum failed.";
  139. return false; // Bad header CRC
  140. }
  141. $headerlen += 2;
  142. }
  143. // GZIP FOOTER
  144. $datacrc = unpack("V", substr($data, -8, 4));
  145. $datacrc = sprintf('%u', $datacrc[1] & 0xFFFFFFFF);
  146. $isize = unpack("V", substr($data, -4));
  147. $isize = $isize[1];
  148. // decompression:
  149. $bodylen = $len - $headerlen - 8;
  150. if ($bodylen < 1) {
  151. // IMPLEMENTATION BUG!
  152. return null;
  153. }
  154. $body = substr($data, $headerlen, $bodylen);
  155. $data = "";
  156. if ($bodylen > 0) {
  157. switch ($method) {
  158. case 8:
  159. // Currently the only supported compression method:
  160. $data = gzinflate($body, $maxlength);
  161. break;
  162. default:
  163. $error = "Unknown compression method.";
  164. return false;
  165. }
  166. } // zero-byte body content is allowed
  167. // Verifiy CRC32
  168. $crc = sprintf("%u", crc32($data));
  169. $crcOK = $crc == $datacrc;
  170. $lenOK = $isize == strlen($data);
  171. if (!$lenOK || !$crcOK) {
  172. $error = ( $lenOK ? '' : 'Length check FAILED. ') . ( $crcOK ? '' : 'Checksum FAILED.');
  173. return false;
  174. }
  175. return $data;
  176. }
  177. if (version_compare(phpversion(), "5", "<")) {
  178. function serialize_fix($v) {
  179. return str_replace('O:11:"phprpc_date":7:{', 'O:11:"PHPRPC_Date":7:{', serialize($v));
  180. }
  181. }
  182. else {
  183. function serialize_fix($v) {
  184. return serialize($v);
  185. }
  186. }
  187. function declare_empty_class($classname) {
  188. static $callback = null;
  189. $classname = preg_replace('/[^a-zA-Z0-9\_]/', '', $classname);
  190. if ($callback===null) {
  191. $callback = $classname;
  192. return;
  193. }
  194. if ($callback) {
  195. call_user_func($callback, $classname);
  196. }
  197. if (!class_exists($classname)) {
  198. if (version_compare(phpversion(), "5", "<")) {
  199. eval('class ' . $classname . ' { }');
  200. }
  201. else {
  202. eval('
  203. class ' . $classname . ' {
  204. private function __get($name) {
  205. $vars = (array)$this;
  206. $protected_name = "\0*\0$name";
  207. $private_name = "\0'.$classname.'\0$name";
  208. if (array_key_exists($name, $vars)) {
  209. return $this->$name;
  210. }
  211. else if (array_key_exists($protected_name, $vars)) {
  212. return $vars[$protected_name];
  213. }
  214. else if (array_key_exists($private_name, $vars)) {
  215. return $vars[$private_name];
  216. }
  217. else {
  218. $keys = array_keys($vars);
  219. $keys = array_values(preg_grep("/^\\\\x00.*?\\\\x00".$name."$/", $keys));
  220. if (isset($keys[0])) {
  221. return $vars[$keys[0]];
  222. }
  223. else {
  224. return NULL;
  225. }
  226. }
  227. }
  228. }');
  229. }
  230. }
  231. }
  232. declare_empty_class(ini_get('unserialize_callback_func'));
  233. ini_set('unserialize_callback_func', 'declare_empty_class');
  234. ?>