2 /** Write the appropriate xinha_config directives to pass data to a PHP (Plugin) backend file.
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)
8 * <script language="javascript">
9 * with (xinha_config.ImageManager)
12 * xinha_pass_to_php_backend
16 * 'images_dir' => '/home/your/directory',
17 * 'images_url' => '/directory'
26 function xinha_pass_to_php_backend($Data, $KeyLocation = 'Xinha:BackendKey', $ReturnPHP = FALSE)
30 $bk['data'] = serialize($Data);
33 if(!isset($_SESSION[$KeyLocation]))
35 $_SESSION[$KeyLocation] = uniqid('Key_');
38 $bk['session_name'] = session_name();
39 $bk['key_location'] = $KeyLocation;
41 function_exists('sha1') ?
42 sha1($_SESSION[$KeyLocation] . $bk['data'])
43 : md5($_SESSION[$KeyLocation] . $bk['data']);
46 // The data will be passed via a postback to the
47 // backend, we want to make sure these are going to come
48 // out from the PHP as an array like $bk above, so
49 // we need to adjust the keys.
50 $backend_data = array();
51 foreach($bk as $k => $v)
53 $backend_data["backend_data[$k]"] = $v;
56 // The session_start() above may have been after data was sent, so cookies
57 // wouldn't have worked.
58 $backend_data[session_name()] = session_id();
62 return array('backend_data' => $backend_data);
66 echo 'backend_data = ' . xinha_to_js($backend_data) . "; \n";
70 /** Convert PHP data structure to Javascript */
72 function xinha_to_js($var, $tabs = 0)
81 return "'" . xinha_js_encode($var) . "'";
87 foreach(array_keys($var) as $k) {
88 if(!is_numeric($k)) $useObject = true;
91 foreach($var as $k => $v)
95 if(preg_match('#^[a-zA-Z]+[a-zA-Z0-9]*$#', $k)) {
101 $i .= xinha_to_js($v, $tabs +
1);
105 $ret = "{\n" . xinha_tabify(implode(",\n", $js), $tabs) . "\n}";
107 $ret = "[\n" . xinha_tabify(implode(",\n", $js), $tabs) . "\n]";
115 /** Like htmlspecialchars() except for javascript strings. */
117 function xinha_js_encode($string)
119 static $strings = "\\,\",',%,&,<,>,{,},@,\n,\r";
121 if(!is_array($strings))
124 foreach(explode(',', $strings) as $chr)
126 $tr[$chr] = sprintf('\x%02X', ord($chr));
131 return strtr($string, $strings);
135 /** Used by plugins to get the config passed via
136 * xinha_pass_to_backend()
137 * returns either the structure given, or NULL
138 * if none was passed or a security error was encountered.
141 function xinha_read_passed_data()
143 if(isset($_REQUEST['backend_data']) && is_array($_REQUEST['backend_data']))
145 $bk = $_REQUEST['backend_data'];
146 session_name($bk['session_name']);
148 if(!isset($_SESSION[$bk['key_location']])) return NULL;
151 function_exists('sha1') ?
152 sha1($_SESSION[$bk['key_location']] . $bk['data'])
153 : md5($_SESSION[$bk['key_location']] . $bk['data']))
155 return unserialize(ini_get('magic_quotes_gpc') ?
stripslashes($bk['data']) : $bk['data']);
162 /** Used by plugins to get a query string that can be sent to the backend
163 * (or another part of the backend) to send the same data.
166 function xinha_passed_data_querystring()
169 if(isset($_REQUEST['backend_data']) && is_array($_REQUEST['backend_data']))
171 foreach($_REQUEST['backend_data'] as $k => $v)
173 $v = ini_get('magic_quotes_gpc') ?
stripslashes($v) : $v;
174 $qs[] = "backend_data[" . rawurlencode($k) . "]=" . rawurlencode($v);
178 $qs[] = session_name() . '=' . session_id();
179 return implode('&', $qs);
183 /** Just space-tab indent some text */
184 function xinha_tabify($text, $tabs)
188 return str_repeat(" ", $tabs) . preg_replace('/\n(.)/', "\n" . str_repeat(" ", $tabs) . "\$1", $text);
192 /** Return upload_max_filesize value from php.ini in kilobytes (function adapted from php.net)**/
193 function upload_max_filesize_kb()
195 $val = ini_get('upload_max_filesize');
197 $last = strtolower($val{strlen($val)-1});
200 // The 'G' modifier is available since PHP 5.1.0