PageRenderTime 44ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/core/ajax_lib/Php.php

https://code.google.com/p/mwenhanced/
PHP | 359 lines | 217 code | 34 blank | 108 comment | 38 complexity | ad41dd3634a0893213dfce812979f4ce MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0, AGPL-1.0, GPL-2.0, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /**
  3. * JsHttpRequest: PHP backend for JavaScript DHTML loader.
  4. * (C) 2005 Dmitry Koterov, http://forum.dklab.ru/users/DmitryKoterov/
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. * See http://www.gnu.org/copyleft/lesser.html
  11. *
  12. * Do not remove this comment if you want to use the script!
  13. * ?? ????€??? ?????? ???????????, ???? ?? ?????? ???????????? ??????!
  14. *
  15. * This backend library also supports POST requests additionally to GET.
  16. *
  17. * @author Dmitry Koterov
  18. * @version 4.14
  19. */
  20. class JsHttpRequest
  21. {
  22. var $SCRIPT_ENCODING = "windows-1251";
  23. var $SCRIPT_DECODE_MODE = '';
  24. var $LOADER = null;
  25. var $ID = null;
  26. // Internal; uniq value.
  27. var $_uniqHash;
  28. // Internal: response content-type depending on loader type.
  29. var $_contentTypes = array(
  30. "script" => "text/javascript",
  31. "xml" => "text/plain",
  32. "form" => "text/html",
  33. "" => "text/plain", // for unknown loader
  34. );
  35. /**
  36. * Constructor.
  37. *
  38. * Create new JsHttpRequest backend object and attach it
  39. * to script output buffer. As a result - script will always return
  40. * correct JavaScript code, even in case of fatal errors.
  41. */
  42. function JsHttpRequest($enc)
  43. {
  44. // QUERY_STRING is in form: PHPSESSID=<sid>&a=aaa&b=bbb&<id>
  45. // where <id> is request ID, <sid> - session ID (if present),
  46. // PHPSESSID - session parameter name (by default = "PHPSESSID").
  47. // Parse QUERY_STRING wrapper format.
  48. if (preg_match('/^(.*)(?:&|^)JsHttpRequest=(\d+)-([^&]+)((?:&|$).*)$/s', $_SERVER['QUERY_STRING'], $m)) {
  49. $this->ID = $m[2];
  50. $this->LOADER = strtolower($m[3]);
  51. $_SERVER['QUERY_STRING'] = $m[1] . $m[4];
  52. unset($_GET['JsHttpRequest']);
  53. unset($_REQUEST['JsHttpRequest']);
  54. } else {
  55. $this->ID = 0;
  56. $this->LOADER = 'unknown';
  57. }
  58. // Start OB handling early.
  59. $this->_uniqHash = md5(microtime() . getmypid());
  60. ini_set('error_prepend_string', ini_get('error_prepend_string') . $this->_uniqHash);
  61. ini_set('error_append_string', ini_get('error_append_string') . $this->_uniqHash);
  62. ob_start(array(&$this, "_obHandler"));
  63. // Set up encoding.
  64. $this->setEncoding($enc);
  65. // Check if headers are already sent (see Content-Type library usage).
  66. // If true - generate debug message and exit.
  67. $file = $line = null;
  68. if (headers_sent($file, $line)) {
  69. trigger_error(
  70. "HTTP headers are already sent" . ($line !== null? " in $file on line $line" : "") . ". "
  71. . "Possibly you have extra spaces (or newlines) before first line of the script or any library. "
  72. . "Please note that Subsys_JsHttpRequest uses its own Content-Type header and fails if "
  73. . "this header cannot be set. See header() function documentation for details",
  74. E_USER_ERROR
  75. );
  76. exit();
  77. }
  78. }
  79. /**
  80. * string getJsCode()
  81. *
  82. * Return JavaScript part of library.
  83. */
  84. function getJsCode()
  85. {
  86. return file_get_contents(dirname(__FILE__).'/JsHttpRequest.js');
  87. }
  88. /**
  89. * void setEncoding(string $encoding)
  90. *
  91. * Set active script encoding & correct QUERY_STRING according to it.
  92. * Examples:
  93. * "windows-1251" - set plain encoding (non-windows characters,
  94. * e.g. hieroglyphs, are totally ignored)
  95. * "windows-1251 entities" - set windows encoding, BUT additionally replace:
  96. * "&" -> "&amp;"
  97. * hieroglyph -> &#XXXX; entity
  98. */
  99. function setEncoding($enc)
  100. {
  101. // Parse encoding.
  102. preg_match('/^(\S*)(?:\s+(\S*))$/', $enc, $p);
  103. $this->SCRIPT_ENCODING = strtolower(!empty($p[1])? $p[1] : $enc);
  104. $this->SCRIPT_DECODE_MODE = !empty($p[2])? $p[2] : '';
  105. // Manually parse QUERY_STRING because of damned Unicode's %uXXXX.
  106. $this->_correctSuperglobals();
  107. }
  108. /**
  109. * string quoteInput(string $input)
  110. *
  111. * Quote string according to input decoding mode.
  112. * If entities is used (see setEncoding()), no '&' character is quoted,
  113. * only '"', '>' and '<' (we presume than '&' is already quoted by
  114. * input reader function).
  115. *
  116. * Use this function INSTEAD of htmlspecialchars() for $_GET data
  117. * in your scripts.
  118. */
  119. function quoteInput($s)
  120. {
  121. if ($this->SCRIPT_DECODE_MODE == 'entities')
  122. return str_replace(array('"', '<', '>'), array('&quot;', '&lt;', '&gt;'), $s);
  123. else
  124. return htmlspecialchars($s);
  125. }
  126. /**
  127. * Convert PHP scalar, array or hash to JS scalar/array/hash.
  128. */
  129. function php2js($a)
  130. {
  131. if (is_null($a)) return 'null';
  132. if ($a === false) return 'false';
  133. if ($a === true) return 'true';
  134. if (is_scalar($a)) {
  135. $a = addslashes($a);
  136. $a = str_replace("\n", '\n', $a);
  137. $a = str_replace("\r", '\r', $a);
  138. $a = preg_replace('{(</)(script)}i', "$1'+'$2", $a); // for FORM loader
  139. return "'$a'";
  140. }
  141. $isList = true;
  142. for ($i=0, reset($a); $i<count($a); $i++, next($a))
  143. if (key($a) !== $i) { $isList = false; break; }
  144. $result = array();
  145. if ($isList) {
  146. foreach ($a as $v) $result[] = JsHttpRequest::php2js($v);
  147. return '[ ' . join(', ', $result) . ' ]';
  148. } else {
  149. foreach ($a as $k=>$v) $result[] = JsHttpRequest::php2js($k) . ': ' . JsHttpRequest::php2js($v);
  150. return '{ ' . join(', ', $result) . ' }';
  151. }
  152. }
  153. /**
  154. * Internal methods.
  155. */
  156. /**
  157. * Parse & decode QUERY_STRING.
  158. */
  159. function _correctSuperglobals()
  160. {
  161. // In case of FORM loader we may go to nirvana, everything is already parsed by PHP.
  162. if ($this->LOADER == 'form') return;
  163. // ATTENTION!!!
  164. // HTTP_RAW_POST_DATA is only accessible when Content-Type of POST request
  165. // is NOT default "application/x-www-form-urlencoded"!!!
  166. // Library frontend sets "application/octet-stream" for that purpose,
  167. // see JavaScript code.
  168. $source = array(
  169. '_GET' => !empty($_SERVER['QUERY_STRING'])? $_SERVER['QUERY_STRING'] : null,
  170. '_POST'=> !empty($GLOBALS['HTTP_RAW_POST_DATA'])? $GLOBALS['HTTP_RAW_POST_DATA'] : null
  171. );
  172. foreach ($source as $dst=>$src) {
  173. // First correct all 2-byte entities.
  174. $s = preg_replace('/%(?!5B)(?!5D)([0-9a-f]{2})/si', '%u00\\1', $src);
  175. // Now we can use standard parse_str() with no worry!
  176. $data = null;
  177. parse_str($s, $data);
  178. $GLOBALS[$dst] = $this->_ucs2EntitiesDecode($data);
  179. }
  180. $GLOBALS['HTTP_GET_VARS'] = $_GET; // deprecated vars
  181. $GLOBALS['HTTP_POST_VARS'] = $_POST;
  182. $_REQUEST =
  183. (isset($_COOKIE)? $_COOKIE : array()) +
  184. (isset($_POST)? $_POST : array()) +
  185. (isset($_GET)? $_GET : array());
  186. if (ini_get('register_globals')) {
  187. // TODO?
  188. }
  189. }
  190. /**
  191. * Called in case of error too!
  192. */
  193. function _obHandler($text)
  194. {
  195. // Check for error.
  196. if (preg_match('{'.$this->_uniqHash.'(.*?)'.$this->_uniqHash.'}sx', $text)) {
  197. $text = str_replace($this->_uniqHash, '', $text);
  198. }
  199. // Content-type header.
  200. // In XMLHttpRRequest mode we must return text/plain - damned stupid Opera 8.0. :(
  201. $ctype = !empty($this->_contentTypes[$this->LOADER])? $this->_contentTypes[$this->LOADER] : $this->_contentTypes[''];
  202. header("Content-type: $ctype; charset={$this->SCRIPT_ENCODING}");
  203. // Make resulting hash.
  204. if (!isset($this->RESULT)) $this->RESULT = isset($GLOBALS['_RESULT'])? $GLOBALS['_RESULT'] : null;
  205. $result = $this->php2js($this->RESULT);
  206. $jsWindow = $ctype == "text/html"? 'parent.' : '';
  207. $text =
  208. "// BEGIN JsHttpRequest\n" .
  209. "{$jsWindow}JsHttpRequest.dataReady(\n" .
  210. " " . $this->php2js($this->ID) . ", // this ID is passed from JavaScript frontend\n" .
  211. " " . $this->php2js(trim($text)) . ",\n" .
  212. " " . $result . "\n" .
  213. ")\n" .
  214. "// END JsHttpRequest\n" .
  215. "";
  216. if ($jsWindow) {
  217. $text = '<script type="text/javascript" language="JavaScript"><!--' . "\n$text" . '//--></script>';
  218. }
  219. // $f = fopen("/debug", "w"); fwrite($f, $text); fclose($f);
  220. return $text;
  221. }
  222. /**
  223. * Decode all %uXXXX entities in string or array (recurrent).
  224. * String must not contain %XX entities - they are ignored!
  225. */
  226. function _ucs2EntitiesDecode($data)
  227. {
  228. if (is_array($data)) {
  229. $d = array();
  230. foreach ($data as $k=>$v) {
  231. $d[$this->_ucs2EntitiesDecode($k)] = $this->_ucs2EntitiesDecode($v);
  232. }
  233. return $d;
  234. } else {
  235. if (strpos($data, '%u') !== false) { // improve speed
  236. $data = preg_replace_callback('/%u([0-9A-F]{1,4})/si', array(&$this, '_ucs2EntitiesDecodeCallback'), $data);
  237. }
  238. return $data;
  239. }
  240. }
  241. /**
  242. * Decode one %uXXXX entity (RE callback).
  243. */
  244. function _ucs2EntitiesDecodeCallback($p)
  245. {
  246. $hex = $p[1];
  247. $dec = hexdec($hex);
  248. if ($dec === "38" && $this->SCRIPT_DECODE_MODE == 'entities') {
  249. // Process "&" separately in "entities" decode mode.
  250. $c = "&amp;";
  251. } else {
  252. if (is_callable('iconv')) {
  253. $c = @iconv('UCS-2BE', $this->SCRIPT_ENCODING, pack('n', $dec));
  254. } else {
  255. $c = $this->_decUcs2Decode($dec, $this->SCRIPT_ENCODING);
  256. }
  257. if (!strlen($c)) {
  258. if ($this->SCRIPT_DECODE_MODE == 'entities') {
  259. $c = '&#'.$dec.';';
  260. } else {
  261. $c = '?';
  262. }
  263. }
  264. }
  265. return $c;
  266. }
  267. /**
  268. * If there is no ICONV, try to decode 1-byte characters manually
  269. * (for most popular charsets only).
  270. */
  271. /**
  272. * Convert from UCS-2BE decimal to $toEnc.
  273. */
  274. function _decUcs2Decode($code, $toEnc)
  275. {
  276. if ($code < 128) return chr($code);
  277. if (isset($this->_encTables[$toEnc])) {
  278. $p = array_search($code, $this->_encTables[$toEnc]);
  279. if ($p !== false) return chr(128 + $p);
  280. }
  281. return "";
  282. }
  283. /**
  284. * UCS-2BE -> 1-byte encodings (from #128).
  285. */
  286. var $_encTables = array(
  287. 'windows-1251' => array(
  288. 0x0402, 0x0403, 0x201A, 0x0453, 0x201E, 0x2026, 0x2020, 0x2021,
  289. 0x20AC, 0x2030, 0x0409, 0x2039, 0x040A, 0x040C, 0x040B, 0x040F,
  290. 0x0452, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
  291. 0x0098, 0x2122, 0x0459, 0x203A, 0x045A, 0x045C, 0x045B, 0x045F,
  292. 0x00A0, 0x040E, 0x045E, 0x0408, 0x00A4, 0x0490, 0x00A6, 0x00A7,
  293. 0x0401, 0x00A9, 0x0404, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x0407,
  294. 0x00B0, 0x00B1, 0x0406, 0x0456, 0x0491, 0x00B5, 0x00B6, 0x00B7,
  295. 0x0451, 0x2116, 0x0454, 0x00BB, 0x0458, 0x0405, 0x0455, 0x0457,
  296. 0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417,
  297. 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E, 0x041F,
  298. 0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427,
  299. 0x0428, 0x0429, 0x042A, 0x042B, 0x042C, 0x042D, 0x042E, 0x042F,
  300. 0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437,
  301. 0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E, 0x043F,
  302. 0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447,
  303. 0x0448, 0x0449, 0x044A, 0x044B, 0x044C, 0x044D, 0x044E, 0x044F,
  304. ),
  305. 'koi8-r' => array(
  306. 0x2500, 0x2502, 0x250C, 0x2510, 0x2514, 0x2518, 0x251C, 0x2524,
  307. 0x252C, 0x2534, 0x253C, 0x2580, 0x2584, 0x2588, 0x258C, 0x2590,
  308. 0x2591, 0x2592, 0x2593, 0x2320, 0x25A0, 0x2219, 0x221A, 0x2248,
  309. 0x2264, 0x2265, 0x00A0, 0x2321, 0x00B0, 0x00B2, 0x00B7, 0x00F7,
  310. 0x2550, 0x2551, 0x2552, 0x0451, 0x2553, 0x2554, 0x2555, 0x2556,
  311. 0x2557, 0x2558, 0x2559, 0x255A, 0x255B, 0x255C, 0x255d, 0x255E,
  312. 0x255F, 0x2560, 0x2561, 0x0401, 0x2562, 0x2563, 0x2564, 0x2565,
  313. 0x2566, 0x2567, 0x2568, 0x2569, 0x256A, 0x256B, 0x256C, 0x00A9,
  314. 0x044E, 0x0430, 0x0431, 0x0446, 0x0434, 0x0435, 0x0444, 0x0433,
  315. 0x0445, 0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043d, 0x043E,
  316. 0x043F, 0x044F, 0x0440, 0x0441, 0x0442, 0x0443, 0x0436, 0x0432,
  317. 0x044C, 0x044B, 0x0437, 0x0448, 0x044d, 0x0449, 0x0447, 0x044A,
  318. 0x042E, 0x0410, 0x0411, 0x0426, 0x0414, 0x0415, 0x0424, 0x0413,
  319. 0x0425, 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041d, 0x041E,
  320. 0x041F, 0x042F, 0x0420, 0x0421, 0x0422, 0x0423, 0x0416, 0x0412,
  321. 0x042C, 0x042B, 0x0417, 0x0428, 0x042d, 0x0429, 0x0427, 0x042A
  322. ),
  323. );
  324. }
  325. ?>