2 require('config.php');
\r
13 if(isset($HTTP_RAW_POST_DATA)){
\r
14 header('Content-Type: text/javascript');
\r
15 $data = json_decode($HTTP_RAW_POST_DATA);
\r
16 }else if(isset($_POST['extAction'])){ // form post
\r
18 $isUpload = $_POST['extUpload'] == 'true';
\r
19 $data = new BogusAction();
\r
20 $data->action = $_POST['extAction'];
\r
21 $data->method = $_POST['extMethod'];
\r
22 $data->tid = isset($_POST['extTID']) ? $_POST['extTID'] : null; // not set for upload
\r
23 $data->data = array($_POST, $_FILES);
\r
25 die('Invalid request.');
\r
28 function doRpc($cdata){
\r
31 if(!isset($API[$cdata->action])){
\r
32 throw new Exception('Call to undefined action: ' . $cdata->action);
\r
35 $action = $cdata->action;
\r
38 doAroundCalls($a['before'], $cdata);
\r
40 $method = $cdata->method;
\r
41 $mdef = $a['methods'][$method];
\r
43 throw new Exception("Call to undefined method: $method on action $action");
\r
45 doAroundCalls($mdef['before'], $cdata);
\r
54 require_once("classes/$action.php");
\r
57 $params = isset($cdata->data) && is_array($cdata->data) ? $cdata->data : array();
\r
59 $r['result'] = call_user_func_array(array($o, $method), $params);
\r
61 doAroundCalls($mdef['after'], $cdata, $r);
\r
62 doAroundCalls($a['after'], $cdata, $r);
\r
64 catch(Exception $e){
\r
65 $r['type'] = 'exception';
\r
66 $r['message'] = $e->getMessage();
\r
67 $r['where'] = $e->getTraceAsString();
\r
73 function doAroundCalls(&$fns, &$cdata, &$returnData=null){
\r
78 foreach($fns as $f){
\r
79 $f($cdata, $returnData);
\r
82 $fns($cdata, $returnData);
\r
87 if(is_array($data)){
\r
88 $response = array();
\r
89 foreach($data as $d){
\r
90 $response[] = doRpc($d);
\r
93 $response = doRpc($data);
\r
95 if($isForm && $isUpload){
\r
96 echo '<html><body><textarea>';
\r
97 echo json_encode($response);
\r
98 echo '</textarea></body></html>';
\r
100 echo json_encode($response);
\r