Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / examples / direct / php / router.php
1 <?php
2 require('config.php');
3
4 class BogusAction {
5         public $action;
6         public $method;
7         public $data;
8         public $tid;
9 }
10
11 $isForm = false;
12 $isUpload = false;
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
17         $isForm = true;
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);
24 }else{
25         die('Invalid request.');
26 }
27
28 function doRpc($cdata){
29     global $API;
30         try {
31                 if(!isset($API[$cdata->action])){
32                         throw new Exception('Call to undefined action: ' . $cdata->action);
33                 }
34
35                 $action = $cdata->action;
36                 $a = $API[$action];
37
38                 doAroundCalls($a['before'], $cdata);
39
40                 $method = $cdata->method;
41                 $mdef = $a['methods'][$method];
42                 if(!$mdef){
43                         throw new Exception("Call to undefined method: $method on action $action");
44                 }
45                 doAroundCalls($mdef['before'], $cdata);
46
47                 $r = array(
48                         'type'=>'rpc',
49                         'tid'=>$cdata->tid,
50                         'action'=>$action,
51                         'method'=>$method
52                 );
53
54                 require_once("classes/$action.php");
55                 $o = new $action();
56         if (isset($mdef['len'])) {
57                     $params = isset($cdata->data) && is_array($cdata->data) ? $cdata->data : array();
58                 } else {
59                     $params = array($cdata->data);
60                 }
61
62                 $r['result'] = call_user_func_array(array($o, $method), $params);
63
64                 doAroundCalls($mdef['after'], $cdata, $r);
65                 doAroundCalls($a['after'], $cdata, $r);
66         }
67         catch(Exception $e){
68                 $r['type'] = 'exception';
69                 $r['message'] = $e->getMessage();
70                 $r['where'] = $e->getTraceAsString();
71         }
72         return $r;
73 }
74
75
76 function doAroundCalls(&$fns, &$cdata, &$returnData=null){
77         if(!$fns){
78                 return;
79         }
80         if(is_array($fns)){
81                 foreach($fns as $f){
82                         $f($cdata, $returnData);
83                 }
84         }else{
85                 $fns($cdata, $returnData);
86         }
87 }
88
89 $response = null;
90 if(is_array($data)){
91         $response = array();
92         foreach($data as $d){
93                 $response[] = doRpc($d);
94         }
95 }else{
96         $response = doRpc($data);
97 }
98 if($isForm && $isUpload){
99         echo '<html><body><textarea>';
100         echo json_encode($response);
101         echo '</textarea></body></html>';
102 }else{
103         echo json_encode($response);
104 }