PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/data/xinha/contrib/php-xinha.php

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