compat.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. if (!function_exists('gzdecode')) {
  68. function gzdecode($data, &$filename = '', &$error = '', $maxlength = null) {
  69. $len = strlen($data);
  70. if ($len < 18 || strcmp(substr($data, 0, 2), "\x1f\x8b")) {
  71. $error = "Not in GZIP format.";
  72. return null; // Not GZIP format (See RFC 1952)
  73. }
  74. $method = ord(substr($data, 2, 1)); // Compression method
  75. $flags = ord(substr($data, 3, 1)); // Flags
  76. if ($flags & 31 != $flags) {
  77. $error = "Reserved bits not allowed.";
  78. return null;
  79. }
  80. // NOTE: $mtime may be negative (PHP integer limitations)
  81. $mtime = unpack("V", substr($data, 4, 4));
  82. $mtime = $mtime[1];
  83. $xfl = substr($data, 8, 1);
  84. $os = substr($data, 8, 1);
  85. $headerlen = 10;
  86. $extralen = 0;
  87. $extra = "";
  88. if ($flags & 4) {
  89. // 2-byte length prefixed EXTRA data in header
  90. if ($len - $headerlen - 2 < 8) {
  91. return false; // invalid
  92. }
  93. $extralen = unpack("v", substr($data, 8, 2));
  94. $extralen = $extralen[1];
  95. if ($len - $headerlen - 2 - $extralen < 8) {
  96. return false; // invalid
  97. }
  98. $extra = substr($data, 10, $extralen);
  99. $headerlen += 2 + $extralen;
  100. }
  101. $filenamelen = 0;
  102. $filename = "";
  103. if ($flags & 8) {
  104. // C-style string
  105. if ($len - $headerlen - 1 < 8) {
  106. return false; // invalid
  107. }
  108. $filenamelen = strpos(substr($data, $headerlen), chr(0));
  109. if ($filenamelen === false || $len - $headerlen - $filenamelen - 1 < 8) {
  110. return false; // invalid
  111. }
  112. $filename = substr($data, $headerlen, $filenamelen);
  113. $headerlen += $filenamelen + 1;
  114. }
  115. $commentlen = 0;
  116. $comment = "";
  117. if ($flags & 16) {
  118. // C-style string COMMENT data in header
  119. if ($len - $headerlen - 1 < 8) {
  120. return false; // invalid
  121. }
  122. $commentlen = strpos(substr($data, $headerlen), chr(0));
  123. if ($commentlen === false || $len - $headerlen - $commentlen - 1 < 8) {
  124. return false; // Invalid header format
  125. }
  126. $comment = substr($data, $headerlen, $commentlen);
  127. $headerlen += $commentlen + 1;
  128. }
  129. $headercrc = "";
  130. if ($flags & 2) {
  131. // 2-bytes (lowest order) of CRC32 on header present
  132. if ($len - $headerlen - 2 < 8) {
  133. return false; // invalid
  134. }
  135. $calccrc = crc32(substr($data, 0, $headerlen)) & 0xffff;
  136. $headercrc = unpack("v", substr($data, $headerlen, 2));
  137. $headercrc = $headercrc[1];
  138. if ($headercrc != $calccrc) {
  139. $error = "Header checksum failed.";
  140. return false; // Bad header CRC
  141. }
  142. $headerlen += 2;
  143. }
  144. // GZIP FOOTER
  145. $datacrc = unpack("V", substr($data, -8, 4));
  146. $datacrc = sprintf('%u', $datacrc[1] & 0xFFFFFFFF);
  147. $isize = unpack("V", substr($data, -4));
  148. $isize = $isize[1];
  149. // decompression:
  150. $bodylen = $len - $headerlen - 8;
  151. if ($bodylen < 1) {
  152. // IMPLEMENTATION BUG!
  153. return null;
  154. }
  155. $body = substr($data, $headerlen, $bodylen);
  156. $data = "";
  157. if ($bodylen > 0) {
  158. switch ($method) {
  159. case 8:
  160. // Currently the only supported compression method:
  161. $data = gzinflate($body, $maxlength);
  162. break;
  163. default:
  164. $error = "Unknown compression method.";
  165. return false;
  166. }
  167. } // zero-byte body content is allowed
  168. // Verifiy CRC32
  169. $crc = sprintf("%u", crc32($data));
  170. $crcOK = $crc == $datacrc;
  171. $lenOK = $isize == strlen($data);
  172. if (!$lenOK || !$crcOK) {
  173. $error = ( $lenOK ? '' : 'Length check FAILED. ') . ( $crcOK ? '' : 'Checksum FAILED.');
  174. return false;
  175. }
  176. return $data;
  177. }
  178. }
  179. if (version_compare(phpversion(), "5", "<")) {
  180. function serialize_fix($v) {
  181. return str_replace('O:11:"phprpc_date":7:{', 'O:11:"PHPRPC_Date":7:{', serialize($v));
  182. }
  183. }
  184. else {
  185. function serialize_fix($v) {
  186. return serialize($v);
  187. }
  188. }
  189. function declare_empty_class($classname) {
  190. static $callback = null;
  191. $classname = preg_replace('/[^a-zA-Z0-9\_]/', '', $classname);
  192. if ($callback===null) {
  193. $callback = $classname;
  194. return;
  195. }
  196. if ($callback) {
  197. call_user_func($callback, $classname);
  198. }
  199. if (!class_exists($classname)) {
  200. if (version_compare(phpversion(), "5", "<")) {
  201. eval('class ' . $classname . ' { }');
  202. }
  203. else {
  204. eval('
  205. class ' . $classname . ' {
  206. private function __get($name) {
  207. $vars = (array)$this;
  208. $protected_name = "\0*\0$name";
  209. $private_name = "\0'.$classname.'\0$name";
  210. if (array_key_exists($name, $vars)) {
  211. return $this->$name;
  212. }
  213. else if (array_key_exists($protected_name, $vars)) {
  214. return $vars[$protected_name];
  215. }
  216. else if (array_key_exists($private_name, $vars)) {
  217. return $vars[$private_name];
  218. }
  219. else {
  220. $keys = array_keys($vars);
  221. $keys = array_values(preg_grep("/^\\\\x00.*?\\\\x00".$name."$/", $keys));
  222. if (isset($keys[0])) {
  223. return $vars[$keys[0]];
  224. }
  225. else {
  226. return NULL;
  227. }
  228. }
  229. }
  230. }');
  231. }
  232. }
  233. }
  234. declare_empty_class(ini_get('unserialize_callback_func'));
  235. ini_set('unserialize_callback_func', 'declare_empty_class');
  236. ?>