PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/htmlarea/contrib/php-xinha.php

http://github.com/s9y/Serendipity
PHP | 219 lines | 171 code | 16 blank | 32 comment | 12 complexity | c31f8657b82bc48833b52648412cb410 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, Apache-2.0
  1. <?php
  2. die('disabled in s9y');
  3. /** Write the appropriate xinha_config directives to pass data to a PHP (Plugin) backend file.
  4. *
  5. * ImageManager Example:
  6. * The following would be placed in step 3 of your configuration (see the NewbieGuide
  7. * (http://xinha.python-hosting.com/wiki/NewbieGuide)
  8. *
  9. * <script language="javascript">
  10. * with (xinha_config.ImageManager)
  11. * {
  12. * <?php
  13. * xinha_pass_to_php_backend
  14. * (
  15. * array
  16. * (
  17. * 'images_dir' => '/home/your/directory',
  18. * 'images_url' => '/directory'
  19. * )
  20. * )
  21. * ?>
  22. * }
  23. * </script>
  24. *
  25. */
  26. // temporary
  27. unset($_REQUEST['backend_config']);
  28. unset($_REQUEST['backend_data']);
  29. function xinha_read_passed_data() {
  30. return array("msg"=>"dynamic configuration disabled for security reason");
  31. }
  32. function xinha_passed_data_querystring() {
  33. return '';
  34. }
  35. function xinha_pass_to_php_backend($Data, $KeyLocation = 'Xinha:BackendKey', $ReturnPHP = FALSE)
  36. {
  37. $bk = array();
  38. $bk['data'] = serialize($Data);
  39. @session_start();
  40. if(!isset($_SESSION[$KeyLocation]))
  41. {
  42. $_SESSION[$KeyLocation] = uniqid('Key_');
  43. }
  44. $bk['session_name'] = session_name();
  45. $bk['key_location'] = $KeyLocation;
  46. $bk['hash'] =
  47. function_exists('sha1') ?
  48. sha1($_SESSION[$KeyLocation] . $bk['data'])
  49. : md5($_SESSION[$KeyLocation] . $bk['data']);
  50. // The data will be passed via a postback to the
  51. // backend, we want to make sure these are going to come
  52. // out from the PHP as an array like $bk above, so
  53. // we need to adjust the keys.
  54. $backend_data = array();
  55. foreach($bk as $k => $v)
  56. {
  57. $backend_data["backend_data[$k]"] = $v;
  58. }
  59. // The session_start() above may have been after data was sent, so cookies
  60. // wouldn't have worked.
  61. $backend_data[session_name()] = session_id();
  62. if($ReturnPHP)
  63. {
  64. return array('backend_data' => $backend_data);
  65. }
  66. else
  67. {
  68. echo 'backend_data = ' . xinha_to_js($backend_data) . "; \n";
  69. }
  70. }
  71. /** Convert PHP data structure to Javascript */
  72. function xinha_to_js($var, $tabs = 0)
  73. {
  74. if(is_numeric($var))
  75. {
  76. return $var;
  77. }
  78. if(is_string($var))
  79. {
  80. return "'" . xinha_js_encode($var) . "'";
  81. }
  82. if(is_array($var))
  83. {
  84. $useObject = false;
  85. foreach(array_keys($var) as $k) {
  86. if(!is_numeric($k)) $useObject = true;
  87. }
  88. $js = array();
  89. foreach($var as $k => $v)
  90. {
  91. $i = "";
  92. if($useObject) {
  93. if(preg_match('#^[a-zA-Z]+[a-zA-Z0-9]*$#', $k)) {
  94. $i .= "$k: ";
  95. } else {
  96. $i .= "'$k': ";
  97. }
  98. }
  99. $i .= xinha_to_js($v, $tabs + 1);
  100. $js[] = $i;
  101. }
  102. if($useObject) {
  103. $ret = "{\n" . xinha_tabify(implode(",\n", $js), $tabs) . "\n}";
  104. } else {
  105. $ret = "[\n" . xinha_tabify(implode(",\n", $js), $tabs) . "\n]";
  106. }
  107. return $ret;
  108. }
  109. return 'null';
  110. }
  111. /** Like htmlspecialchars() except for javascript strings. */
  112. function xinha_js_encode($string)
  113. {
  114. static $strings = "\\,\",',%,&,<,>,{,},@,\n,\r";
  115. if(!is_array($strings))
  116. {
  117. $tr = array();
  118. foreach(explode(',', $strings) as $chr)
  119. {
  120. $tr[$chr] = sprintf('\x%02X', ord($chr));
  121. }
  122. $strings = $tr;
  123. }
  124. return strtr($string, $strings);
  125. }
  126. /** Used by plugins to get the config passed via
  127. * xinha_pass_to_backend()
  128. * returns either the structure given, or NULL
  129. * if none was passed or a security error was encountered.
  130. */
  131. function oldxinha_read_passed_data()
  132. {
  133. if(isset($_REQUEST['backend_data']) && is_array($_REQUEST['backend_data']))
  134. {
  135. $bk = $_REQUEST['backend_data'];
  136. session_name($bk['session_name']);
  137. @session_start();
  138. if(!isset($_SESSION[$bk['key_location']])) return NULL;
  139. if($bk['hash'] ===
  140. function_exists('sha1') ?
  141. sha1($_SESSION[$bk['key_location']] . $bk['data'])
  142. : md5($_SESSION[$bk['key_location']] . $bk['data']))
  143. {
  144. return unserialize(ini_get('magic_quotes_gpc') ? stripslashes($bk['data']) : $bk['data']);
  145. }
  146. }
  147. return NULL;
  148. }
  149. /** Used by plugins to get a query string that can be sent to the backend
  150. * (or another part of the backend) to send the same data.
  151. */
  152. function oldxinha_passed_data_querystring()
  153. {
  154. $qs = array();
  155. if(isset($_REQUEST['backend_data']) && is_array($_REQUEST['backend_data']))
  156. {
  157. foreach($_REQUEST['backend_data'] as $k => $v)
  158. {
  159. $v = ini_get('magic_quotes_gpc') ? stripslashes($v) : $v;
  160. $qs[] = "backend_data[" . rawurlencode($k) . "]=" . rawurlencode($v);
  161. }
  162. }
  163. $qs[] = session_name() . '=' . session_id();
  164. return implode('&', $qs);
  165. }
  166. /** Just space-tab indent some text */
  167. function xinha_tabify($text, $tabs)
  168. {
  169. if($text)
  170. {
  171. return str_repeat(" ", $tabs) . preg_replace('/\n(.)/', "\n" . str_repeat(" ", $tabs) . "\$1", $text);
  172. }
  173. }
  174. /** Return upload_max_filesize value from php.ini in kilobytes (function adapted from php.net)**/
  175. function upload_max_filesize_kb()
  176. {
  177. $val = ini_get('upload_max_filesize');
  178. $val = trim($val);
  179. $last = strtolower($val{strlen($val)-1});
  180. switch($last)
  181. {
  182. // The 'G' modifier is available since PHP 5.1.0
  183. case 'g':
  184. $val *= 1024;
  185. case 'm':
  186. $val *= 1024;
  187. }
  188. return $val;
  189. }
  190. ?>