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