Example #1
0
 /**
  * return a sfDate from a string formated for "system"
  *
  * - the format may be configured in sfConfig app_sfDateTimePlugin_format_system
  * - if not present, it default to '%Y-%m-%d'
  *
  * @return	sfDate
  */
 private static function from_internal($date_str, $format)
 {
     // log to debug
     //ezDbg::err("myformat=".$format." and date_str=".$date_str);
     // parse $date_str according to $format
     $arr = self::strptime($date_str, $format);
     ezDbg::assert($arr !== false);
     // convert it to a UNIX timestamp
     $ts = mktime($arr['tm_hour'], $arr['tm_min'], $arr['tm_sec'], $arr['tm_mon'] + 1, $arr['tm_mday'], $arr['tm_year'] + 1900);
     // return a sfDate
     return new sfDate($ts);
 }
 /**
  * This is the main handle for the XDOMRPC calls
  *
  * - this is ultra private ugly stuff from when i wasnt aware of jsonp :)
  *   - dont ask im not proud
  */
 public function executeXDOMRPC(sfWebRequest $request)
 {
     // get all the defined RPC
     $rpc_functions = $this->getRPCFunctions();
     // get obj_id
     $xdomrpc_obj_id = $request->getParameter('obj_id');
     $this->forward404Unless($xdomrpc_obj_id);
     // get the method
     $method_name = $request->getParameter('method_name');
     // gather all the method args
     $method_args = array();
     for ($i = 0; $i < 99; $i++) {
         $key = 'arg' . $i;
         if (!$request->hasParameter($key)) {
             break;
         }
         // parse the method arg
         // ALGO: try to json_decode the value and if it fails, treat it as a string;
         // NOTE: json_decode return null on error, there is a trick to test explicitly the json "null" using the struct
         $val_json = json_decode($request->getParameter($key), true);
         if ($request->getParameter($key) == "null") {
             $method_args[] = null;
         } else {
             if ($val_json == null) {
                 $method_args[] = '"' . $request->getParameter($key) . '"';
             } else {
                 $method_args[] = $val_json;
             }
         }
     }
     // test if $method_name DOES exists
     if (!isset($rpc_functions[$method_name])) {
         $error_str = "unknown method (" . $method_name . ")";
         $data_resp = array('fault' => $error_str, 'returned_val' => null);
     } else {
         $rpc_function = $rpc_functions[$method_name];
         $nparam = isset($rpc_function['nparam']) ? $rpc_function['nparam'] : 99;
         $must_post = isset($rpc_function['must_post']) ? $rpc_function['must_post'] : false;
         // http POST method is required for modifying the database
         if ($must_post && !$request->isMethod('post')) {
             $this->forward404("HTTP POST is required for method=" . $method_name);
         }
         if (count($method_args) < $nparam) {
             $error_str = "Error " . $method_name . " requires " . $nparam . " parameters (got " . count($method_args) . ")";
             $data_resp = array('fault' => $error_str, 'returned_val' => null);
         } else {
             try {
                 ezDbg::err('trying to call (' . $rpc_function['function'] . ')', $method_args);
                 $returned_val = call_user_func_array($rpc_function['function'], $method_args);
                 // wrap the returned_val
                 $data_resp = array('fault' => NULL, 'returned_val' => json_encode($returned_val));
             } catch (Exception $e) {
                 $error_str = "Error " . $method_name . " due to " . $e->getMessage();
                 $data_resp = array('fault' => $error_str, 'returned_val' => null);
             }
         }
     }
     // build the javascript to reply
     // TODO should be js_callback parameter ?
     $data_js = "neoip_xdomrpc_script_reply_var_" . $xdomrpc_obj_id . "=" . json_encode($data_resp) . ";";
     // disable the web_debug bar
     sfConfig::set('sf_web_debug', false);
     // return the $data_js
     $this->getResponse()->setHttpHeader('Content-Type', 'text/javascript');
     return $this->renderText($data_js);
 }