Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.18 KB | None | 0 0
  1. /*
  2.    javascript
  3.    futile attempts to create a generic PHP-JS communications library
  4.    input:  mixed
  5.    output: JSON (as simple as a string)
  6.            "val"    is always the result node
  7.            "error"  is sometimes supplied if there IS an error
  8. */
  9.  
  10. function jdx () {
  11.     // javascript data exchange protocol
  12.     // default is POST, but GET works too
  13.     var i; // variable i is locally reused (warn)
  14.     var v = []; // blank val array
  15.     for( var i = 2; i < arguments.length; i++ ) {
  16.         // variable i is locally reused
  17.         v[i-2] = arguments[i]; // transfer params to values
  18.     }
  19.     jQuery.ajax({
  20.         async: false, // !important;
  21.         cache: false, // !important;
  22.         global: false,
  23.         url: ".ajax.php",
  24.         type: "POST", // just cos it has no length limit
  25.         dataType: "json",
  26.         data: ({
  27.             req : arguments[0],
  28.             key : arguments[1],
  29.             val : v.join('!!JOIN!!') // meh
  30.         }),
  31.         success: function(data) {
  32.             i = data.val;
  33.         }
  34.     });
  35.     return i;
  36. }
  37.  
  38.  
  39.  
  40.  
  41. <?php
  42.     /*
  43.        PHP
  44.        futile attempts to create a generic PHP-JS communications library
  45.        input:  REST
  46.        output: JSON (as simple as a string)
  47.     */
  48.     header('Cache-Control: no-cache, must-revalidate');
  49.     header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // IE bug: caches JSON
  50.     header('Content-type: application/json'); // you return JSON
  51.    
  52.     require_once ('.conf.php');
  53.     require_once ('.userfuncs.php'); // most things the client can call
  54.    
  55.     $f2 = array();
  56.    
  57.     define (MIN_KEY_LENGTH, 8);
  58.    
  59.     if ($_GET) {
  60.         $args = $_GET; // heh, just giving it a name
  61.     } else {
  62.         $args = $_POST; // POST-compatible?
  63.     }
  64.  
  65.     $usr = $args['usr']; // optional user name
  66.     $key = $args['key']; // optional user key
  67.     $req = $args['req']; // what the client is requesting
  68.     $val = $args['val']; // anything the client wants to specify
  69.    
  70.     if ($key && strlen ($key) >= MIN_KEY_LENGTH) {
  71.         // if there is a key, then there is a user
  72.         // and the user wants to be authenticated
  73.         // and that this library only works for the active user anyhow
  74.         // unless there is a specified user
  75.         // in which case, use that user
  76.         if (!$usr) {
  77.             // use the current user.
  78.             require_once ('.auth3.php');
  79.             getAuth();
  80.             $usr = $user['username'];
  81.         }
  82.         require_once ('.auth_keys.php');
  83.        
  84.         if ($key == getuserkey ($usr)) {
  85.             $f2 = $func_private; // open up private functions
  86.         }
  87.     }
  88.    
  89.     // $ret = $$req ($val);
  90.     $allowedfunctions = array_merge ($func_public, $f2);
  91.     if (in_array ($req, $allowedfunctions)) {
  92.         try {
  93.             $ret = @call_user_func_array ($req, explode('!!JOIN!!', $val));
  94.             echo '{"val": "' . $ret . '"}';
  95.         } catch (Exception $e) { // just in case
  96.             echo ('{"val": "",
  97.                    "error": ' . $e->getMessage() . '}');
  98.         }
  99.     } else {
  100.         echo ('{"error": "command not found"}');
  101.     }
  102.    
  103.     function getPage($w) {
  104.         return $w;
  105.     }    
  106. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement