| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 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 |
|
|---|
| 19 |
$rpc_functions = $this->getRPCFunctions(); |
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
$this->forward404Unless($request->isMethod('post'), "HTTP POST is required"); |
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
ezDbg::err("enter xmlrpc"); |
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
$xmlrpc_reqstr = file_get_contents("php://input"); |
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 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 |
|
|---|
| 47 |
}catch(Exception $e){ |
|---|
| 48 |
$xmlrpc_resp = array("faultCode" => 1, "faultString" => "".$e->getMessage()); |
|---|
| 49 |
} |
|---|
| 50 |
} |
|---|
| 51 |
} |
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
$xmlrpc_respstr = xmlrpc_encode($xmlrpc_resp); |
|---|
| 55 |
{ |
|---|
| 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 |
|
|---|
| 64 |
sfConfig::set('sf_web_debug', false); |
|---|
| 65 |
|
|---|
| 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 |
|
|---|
| 77 |
$rpc_functions = $this->getRPCFunctions(); |
|---|
| 78 |
|
|---|
| 79 |
$jsonp_cb = $request->getParameter('callback'); |
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
$method_name = $request->getParameter('method'); |
|---|
| 83 |
$this->forward404Unless($method_name, "method url parameter MUST be specified"); |
|---|
| 84 |
|
|---|
| 85 |
$method_args = array(); |
|---|
| 86 |
for($i = 0; $i < 99; $i++){ |
|---|
| 87 |
$key = 'arg'.$i; |
|---|
| 88 |
if( !$request->hasParameter($key) ) break; |
|---|
| 89 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 122 |
$resp_json = json_encode($resp_data); |
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
if( $jsonp_cb ){ |
|---|
| 126 |
|
|---|
| 127 |
$js_str = $jsonp_cb.'("'.$resp_json.'");'; |
|---|
| 128 |
|
|---|
| 129 |
$this->getResponse()->setHttpHeader('Content-Type', 'text/javascript'); |
|---|
| 130 |
return $this->renderText($js_str); |
|---|
| 131 |
}else{ |
|---|
| 132 |
|
|---|
| 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 |
|
|---|
| 146 |
$rpc_functions = $this->getRPCFunctions(); |
|---|
| 147 |
|
|---|
| 148 |
$xdomrpc_obj_id = $request->getParameter('obj_id'); |
|---|
| 149 |
$this->forward404Unless($xdomrpc_obj_id); |
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 |
$method_name = $request->getParameter('method_name'); |
|---|
| 153 |
|
|---|
| 154 |
$method_args = array(); |
|---|
| 155 |
for($i = 0; $i < 99; $i++){ |
|---|
| 156 |
$key = 'arg'.$i; |
|---|
| 157 |
if( !$request->hasParameter($key) ) break; |
|---|
| 158 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 208 |
// TODO should be js_callback parameter ? |
|---|
| 209 |
$data_js = "neoip_xdomrpc_script_reply_var_".$xdomrpc_obj_id."=".json_encode($data_resp).";"; |
|---|
| 210 |
|
|---|
| 211 |
|
|---|
| 212 |
sfConfig::set('sf_web_debug', false); |
|---|
| 213 |
|
|---|
| 214 |
$this->getResponse()->setHttpHeader('Content-Type', 'text/javascript'); |
|---|
| 215 |
return $this->renderText($data_js); |
|---|
| 216 |
} |
|---|
| 217 |
} |
|---|
| 218 |
|
|---|