router.php 3.06 KB
<?php
/**
  *  router.php
  *  http://www.ibm.com/developerworks/opensource/library/wa-aj-streamline/
  *  @version $Id: router.php 687 2011-12-24 14:36:27Z elena $
  */

   require('config.php');

   class ExtAction {
	  public $action;
	  public $method;
	  public $data;
	  public $tid;
      }

   function doRpc($cdata){
	  global $API;

	  try {
	      if(!isset($API[$cdata->action])) {
		  throw new Exception('Call to undefined action: ' . $cdata->action);
	      }
	      $action = $cdata->action;
	      $a = $API[$action];

	      doAroundCalls($a['before'], $cdata);

	      $method = $cdata->method;
	      $mdef = $a['methods'][$method];
	      if(!$mdef){
		  throw new Exception("Call to undefined method: $method on action $action");
	      }
	      doAroundCalls($mdef['before'], $cdata);

	      $r = array(
		  'type'=>'rpc',
		  'tid'=>$cdata->tid,
		  'action'=>$action,
		  'method'=>$method
	      );

	      $o = new $action();

	      $params = isset($cdata->data) && is_array($cdata->data) ?
						  $cdata->data : array();

	      $r['result'] = call_user_func_array(array($o, $method), $params);

	      doAroundCalls($mdef['after'], $cdata, $r);
	      doAroundCalls($a['after'], $cdata, $r);
	  } catch(Exception $e) {
	      $r['type'] = 'exception';
	      $r['message'] = $e->getMessage();
	      $r['where'] = $e->getTraceAsString();
	  }

	  return $r;
      }

   function doAroundCalls(&$fns, &$cdata, &$returnData=null) {
	  if(!$fns) {
	      return;
	  }
	  if(is_array($fns)) {
	      foreach($fns as $f) {
		  $f($cdata, $returnData);
	      }
	  } else {
	      $fns($cdata, $returnData);
	  }
      }


/**
*  Main
*/

//    define('log',fopen('log','w'));
    $isForm = false;
    $isUpload = false;


/*
*  artificial truc to get $HTTP_RAW_POST_DATA from POST AJAX
*/
     if ($_SERVER['REQUEST_METHOD'] === 'POST' && !isset($_POST['extAction'])) {
// Read the input from stdin
	  $HTTP_RAW_POST_DATA = trim(file_get_contents('php://input'));
      }

    if(!isset($_GET['sessionID'])) die('NO SESSION ID SPECIFIED');

    $usrMgr = new UserMgr();
    $usrMgr->setPath();

    if (isset($HTTP_RAW_POST_DATA)) {
	header('Content-Type: text/javascript');
	$data = json_decode($HTTP_RAW_POST_DATA);
    } else if(isset($_POST['extAction'])) {
	$isForm = true;
	$isUpload = $_POST['extUpload'] == 'true';
	$data = new ExtAction();
	$data->action = $_POST['extAction'];
	$data->method = $_POST['extMethod'];
	$data->tid = isset($_POST['extTID']) ? $_POST['extTID'] : null;
	$data->data = array($_POST, $_FILES);

    }
   else {
 	  die('INVALID REQUEST');
      }

  // $RRR = print_r($data->data, true);
 //  $RRR = print_r($data, true);
//  fwrite(log,$RRR.PHP_EOL);


      $response = null;
      if(is_array($data)) {
	  $response = array();
	  foreach($data as $d) {
	      $response[] = doRpc($d);
	  }
      } else {

	  $response = doRpc($data);
      }

      if($isForm && $isUpload) {
	  echo '<html><body><textarea>';
	  echo json_encode($response);
	  echo '</textarea></body></html>';
      } else {
        echo json_encode($response);
      }

//      fclose(log);
?>