Development

/plugins/sfWebRPCPlugin/lib/BaseWebRPCActions.class.php

You must first sign up to be able to contribute.

root/plugins/sfWebRPCPlugin/lib/BaseWebRPCActions.class.php

Revision 18635, 7.8 kB (checked in by jerome.etienne, 4 years ago)

- JSON: bug fix. removed a trailling ;

Line 
1 <?php
2
3 /**
4  * Initializes a Rest admin Base Action
5  *
6  * @package    sfWebRPCPlugin
7  * @subpackage Actions
8  * @author     Jerome Etienne
9  * @version    SVN: $Id: actions.class.php 9301 2008-05-27 01:08:46Z dwhittle $
10  */
11 abstract class BaseWebRPCActions extends sfActions
12 {
13     /**
14      * This is the main handle for the xmlrpc calls
15     */
16     public function executeRPC2($request)
17     {
18         // get all the defined RPC
19         $rpc_functions    = $this->getRPCFunctions();
20         
21         // http POST method is required for modifying the database
22         $this->forward404Unless($request->isMethod('post'), "HTTP POST is required");
23
24         // log to debug
25         ezDbg::err("enter xmlrpc");
26
27         // get xmlrpc request string posted as a raw
28         $xmlrpc_reqstr    = file_get_contents("php://input");
29
30         // parse the xmlrpc_reqstr
31         $method_name    = null;
32         $xmlrpc_params    = xmlrpc_decode_request($xmlrpc_reqstr, &$method_name);
33         ezDbg::err("enter method_name=$method_name xmlrpc param=".print_r($xmlrpc_params, true));
34
35         if( !isset($rpc_functions[$method_name]) ){
36             $xmlrpc_resp    = array("faultCode" => 1, "faultString" => "unknown method name (".$method_name.")");
37         }else{
38             $rpc_function    = $rpc_functions[$method_name];
39             $nparam        = $rpc_function['nparam'];
40             if( count($xmlrpc_params) < $nparam ){
41                 $xmlrpc_resp    = array("faultCode" => 1, "faultString" => $method_name." require ".$nparam." parameters.");
42             }else{
43                 try{
44                     ezDbg::err('trying to call ('.$rpc_function['function'].')', $xmlrpc_params);
45                     $xmlrpc_resp    = call_user_func_array($rpc_function['function'], $xmlrpc_params);
46                     //$xmlrpc_resp    = sfWebRPCPluginDemo::superAddFct(2,3);
47                 }catch(Exception $e){
48                     $xmlrpc_resp    = array("faultCode" => 1, "faultString" => "".$e->getMessage());
49                 }
50             }
51         }
52
53         // encode the xmlrpc_resp
54         $xmlrpc_respstr    = xmlrpc_encode($xmlrpc_resp);
55         {    // KLUDGE: xmlrpc_encode is unable to add the methodResponse required
56             $arr    = split("\n", $xmlrpc_respstr);
57             $arr[0]    .="\n<methodResponse>";
58             $arr[count($arr)-1]    = "</methodResponse>";
59             $xmlrpc_respstr    = implode("\n", $arr);
60         }
61         ezDbg::err("enter xmlrpc resp=".print_r($xmlrpc_respstr, true));
62         
63         // disable the web_debug bar
64         sfConfig::set('sf_web_debug', false);
65         // return the $value in xml
66         $this->getResponse()->setHttpHeader('Content-Type', 'text/xml');
67         return $this->renderText($xmlrpc_respstr);
68     }
69     
70     
71     /**
72      * This is the main handle for the JSON/JSONP calls
73     */
74     public function executeJSON(sfWebRequest $request)
75     {
76         // get all the defined RPC
77         $rpc_functions    = $this->getRPCFunctions();
78         // try to get jsonp callback - if not present assume it is plain json
79         $jsonp_cb    = $request->getParameter('callback');
80
81         // get the method
82         $method_name    = $request->getParameter('method');
83         $this->forward404Unless($method_name, "method url parameter MUST be specified");
84         // gather all the method args
85         $method_args    = array();
86         for($i = 0; $i < 99; $i++){
87             $key    = 'arg'.$i;
88             if( !$request->hasParameter($key) )    break;
89             // parse the method arg
90             // ALGO: try to json_decode the value and if it fails, treat it as a string;
91             // NOTE: json_decode return null on error, there is a trick to test explicitly the json "null" using the struct
92             $val_json    = json_decode($request->getParameter($key), true);
93             if( $request->getParameter($key) == "null" )    $method_args[]    = null;
94             else if( $val_json == null )            $method_args[]    = '"'.$request->getParameter($key).'"';
95             else                        $method_args[]    = $val_json;
96         }
97
98         if( !isset($rpc_functions[$method_name]) ){
99             $error_str    = "unknown method (".$method_name.")";
100             $this->forward404($error_str);
101         }else{
102             $rpc_function    = $rpc_functions[$method_name];
103             $nparam        = isset($rpc_function['nparam'])     ? $rpc_function['nparam']     : 99;
104             $must_post    = isset($rpc_function['must_post'])    ? $rpc_function['must_post']    : false;
105             // http POST method is required for modifying the database
106             if( $must_post && !$request->isMethod('post') )
107                 $this->forward404("HTTP POST is required for method=".$method_name);
108             if( count($method_args) < $nparam ){
109                 $this->forward404("Error ".$method_name." requires ".$nparam." parameters (got ".count($method_args).")");
110             }else{
111                 try{
112                     ezDbg::err('trying to call ('.$rpc_function['function'].')', $method_args);
113                     $resp_data    = call_user_func_array($rpc_function['function'], $method_args);
114                 }catch(Exception $e){
115                     $error_str    = "Error ".$method_name." due to ".$e->getMessage();
116                     $this->forward404($error_str);
117                 }
118             }
119         }
120         
121         // convert $resp_data into $resp_json
122         $resp_json    = json_encode($resp_data);
123
124         // build the response
125         if( $jsonp_cb ){
126             // build the whole javascript to return
127             $js_str        = $jsonp_cb.'("'.$resp_json.'");';
128             // return the $iframe_url as text/plain
129             $this->getResponse()->setHttpHeader('Content-Type', 'text/javascript');
130             return $this->renderText($js_str);
131         }else{
132             // return the $iframe_url as text/plain
133             $this->getResponse()->setHttpHeader('Content-Type', 'application/json');
134             return $this->renderText($resp_json);
135         }
136     }
137     /**
138      * This is the main handle for the XDOMRPC calls
139      *
140      * - this is ultra private ugly stuff from when i wasnt aware of jsonp :)
141      *   - dont ask im not proud
142     */
143     public function executeXDOMRPC(sfWebRequest $request)
144     {
145         // get all the defined RPC
146         $rpc_functions    = $this->getRPCFunctions();
147         // get obj_id
148         $xdomrpc_obj_id    = $request->getParameter('obj_id');
149         $this->forward404Unless($xdomrpc_obj_id);
150
151         // get the method
152         $method_name    = $request->getParameter('method_name');
153         // gather all the method args
154         $method_args    = array();
155         for($i = 0; $i < 99; $i++){
156             $key    = 'arg'.$i;
157             if( !$request->hasParameter($key) )    break;
158             // parse the method arg
159             // ALGO: try to json_decode the value and if it fails, treat it as a string;
160             // NOTE: json_decode return null on error, there is a trick to test explicitly the json "null" using the struct
161             $val_json    = json_decode($request->getParameter($key), true);
162             if( $request->getParameter($key) == "null" )    $method_args[]    = null;
163             else if( $val_json == null )            $method_args[]    = '"'.$request->getParameter($key).'"';
164             else                        $method_args[]    = $val_json;
165         }
166
167         // test if $method_name DOES exists
168         if( !isset($rpc_functions[$method_name]) ){
169             $error_str    = "unknown method (".$method_name.")";
170             $data_resp    = array(
171                 'fault'        => $error_str,
172                 'returned_val'    => null
173             );
174         }else{
175             $rpc_function    = $rpc_functions[$method_name];
176             $nparam        = isset($rpc_function['nparam'])     ? $rpc_function['nparam']     : 99;
177             $must_post    = isset($rpc_function['must_post'])    ? $rpc_function['must_post']    : false;
178             // http POST method is required for modifying the database
179             if( $must_post && !$request->isMethod('post') )
180                 $this->forward404("HTTP POST is required for method=".$method_name);
181             if( count($method_args) < $nparam ){
182                 $error_str    = "Error ".$method_name." requires ".$nparam." parameters (got ".count($method_args).")";
183                 $data_resp    = array(
184                     'fault'        => $error_str,
185                     'returned_val'    => null
186                 );               
187             }else{
188                 try{
189                     ezDbg::err('trying to call ('.$rpc_function['function'].')', $method_args);
190                     $returned_val    = call_user_func_array($rpc_function['function'], $method_args);
191                     // wrap the returned_val
192                     $data_resp    = array(
193                         'fault'        => NULL,
194                         'returned_val'    => json_encode($returned_val)
195                     );
196                 }catch(Exception $e){
197                     $error_str    = "Error ".$method_name." due to ".$e->getMessage();
198                     $data_resp    = array(
199                         'fault'        => $error_str,
200                         'returned_val'    => null
201                     );
202                 }
203             }
204         }
205         
206
207         // build the javascript to reply
208         // TODO should be js_callback parameter ?
209         $data_js    = "neoip_xdomrpc_script_reply_var_".$xdomrpc_obj_id."=".json_encode($data_resp).";";
210
211         // disable the web_debug bar
212         sfConfig::set('sf_web_debug', false);
213         // return the $data_js
214         $this->getResponse()->setHttpHeader('Content-Type', 'text/javascript');
215         return $this->renderText($data_js);
216     }
217 }
218
Note: See TracBrowser for help on using the browser.