Upgrade to ExtJS 3.3.1 - Released 11/30/2010
[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
57                 $params = isset($cdata->data) && is_array($cdata->data) ? $cdata->data : array();
58
59                 $r['result'] = call_user_func_array(array($o, $method), $params);
60
61                 doAroundCalls($mdef['after'], $cdata, $r);
62                 doAroundCalls($a['after'], $cdata, $r);
63         }
64         catch(Exception $e){
65                 $r['type'] = 'exception';
66                 $r['message'] = $e->getMessage();
67                 $r['where'] = $e->getTraceAsString();
68         }
69         return $r;
70 }
71
72
73 function doAroundCalls(&$fns, &$cdata, &$returnData=null){
74         if(!$fns){
75                 return;
76         }
77         if(is_array($fns)){
78                 foreach($fns as $f){
79                         $f($cdata, $returnData);
80                 }
81         }else{
82                 $fns($cdata, $returnData);
83         }
84 }
85
86 $response = null;
87 if(is_array($data)){
88         $response = array();
89         foreach($data as $d){
90                 $response[] = doRpc($d);
91         }
92 }else{
93         $response = doRpc($data);
94 }
95 if($isForm && $isUpload){
96         echo '<html><body><textarea>';
97         echo json_encode($response);
98         echo '</textarea></body></html>';
99 }else{
100         echo json_encode($response);
101 }