Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- javascript
- futile attempts to create a generic PHP-JS communications library
- input: mixed
- output: JSON (as simple as a string)
- "val" is always the result node
- "error" is sometimes supplied if there IS an error
- */
- function jdx () {
- // javascript data exchange protocol
- // default is POST, but GET works too
- var i; // variable i is locally reused (warn)
- var v = []; // blank val array
- for( var i = 2; i < arguments.length; i++ ) {
- // variable i is locally reused
- v[i-2] = arguments[i]; // transfer params to values
- }
- jQuery.ajax({
- async: false, // !important;
- cache: false, // !important;
- global: false,
- url: ".ajax.php",
- type: "POST", // just cos it has no length limit
- dataType: "json",
- data: ({
- req : arguments[0],
- key : arguments[1],
- val : v.join('!!JOIN!!') // meh
- }),
- success: function(data) {
- i = data.val;
- }
- });
- return i;
- }
- <?php
- /*
- PHP
- futile attempts to create a generic PHP-JS communications library
- input: REST
- output: JSON (as simple as a string)
- */
- header('Cache-Control: no-cache, must-revalidate');
- header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // IE bug: caches JSON
- header('Content-type: application/json'); // you return JSON
- require_once ('.conf.php');
- require_once ('.userfuncs.php'); // most things the client can call
- $f2 = array();
- define (MIN_KEY_LENGTH, 8);
- if ($_GET) {
- $args = $_GET; // heh, just giving it a name
- } else {
- $args = $_POST; // POST-compatible?
- }
- $usr = $args['usr']; // optional user name
- $key = $args['key']; // optional user key
- $req = $args['req']; // what the client is requesting
- $val = $args['val']; // anything the client wants to specify
- if ($key && strlen ($key) >= MIN_KEY_LENGTH) {
- // if there is a key, then there is a user
- // and the user wants to be authenticated
- // and that this library only works for the active user anyhow
- // unless there is a specified user
- // in which case, use that user
- if (!$usr) {
- // use the current user.
- require_once ('.auth3.php');
- getAuth();
- $usr = $user['username'];
- }
- require_once ('.auth_keys.php');
- if ($key == getuserkey ($usr)) {
- $f2 = $func_private; // open up private functions
- }
- }
- // $ret = $$req ($val);
- $allowedfunctions = array_merge ($func_public, $f2);
- if (in_array ($req, $allowedfunctions)) {
- try {
- $ret = @call_user_func_array ($req, explode('!!JOIN!!', $val));
- echo '{"val": "' . $ret . '"}';
- } catch (Exception $e) { // just in case
- echo ('{"val": "",
- "error": ' . $e->getMessage() . '}');
- }
- } else {
- echo ('{"error": "command not found"}');
- }
- function getPage($w) {
- return $w;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement