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