public function preDispatch() { parent::preDispatch(); $this->_helper->layout->disableLayout(); $this->getHelper('viewRenderer')->setNoRender(); $this->_jsonServer->setClass($this->_getHandleClassName()); $this->_jsonServer->handle(); }
public function handle($request = false) { if ($request->isParseError()) { $this->fault('Parse error', -32700); } return parent::handle($request); }
/** * handler for JSON api requests * * @return JSON */ public function handle() { try { // init server and request first $server = new Zend_Json_Server(); $server->setClass('Setup_Frontend_Json', 'Setup'); $server->setClass('Tinebase_Frontend_Json', 'Tinebase'); $server->setAutoHandleExceptions(false); $server->setAutoEmitResponse(false); $request = new Zend_Json_Server_Request_Http(); Setup_Core::initFramework(); $method = $request->getMethod(); $jsonKey = isset($_SERVER['HTTP_X_TINE20_JSONKEY']) ? $_SERVER['HTTP_X_TINE20_JSONKEY'] : ''; Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' is JSON request. method: ' . $method); $anonymnousMethods = array('Setup.getAllRegistryData', 'Setup.login', 'Tinebase.getAvailableTranslations', 'Tinebase.getTranslations', 'Tinebase.setLocale'); if (!Setup_Core::configFileExists()) { $anonymnousMethods = array_merge($anonymnousMethods, array('Setup.envCheck')); } // check json key for all methods but some exceptoins if (!in_array($method, $anonymnousMethods) && Setup_Core::configFileExists() && (empty($jsonKey) || $jsonKey != Setup_Core::get('jsonKey') || !Setup_Core::isRegistered(Setup_Core::USER))) { if (!Setup_Core::isRegistered(Setup_Core::USER)) { Setup_Core::getLogger()->INFO(__METHOD__ . '::' . __LINE__ . ' Attempt to request a privileged Json-API method without authorisation from "' . $_SERVER['REMOTE_ADDR'] . '". (session timeout?)'); throw new Tinebase_Exception_AccessDenied('Not Authorised', 401); } else { Setup_Core::getLogger()->WARN(__METHOD__ . '::' . __LINE__ . ' Fatal: got wrong json key! (' . $jsonKey . ') Possible CSRF attempt!' . ' affected account: ' . print_r(Setup_Core::getUser(), true) . ' request: ' . print_r($_REQUEST, true)); throw new Tinebase_Exception_AccessDenied('Not Authorised', 401); } } $response = $server->handle($request); } catch (Exception $exception) { $response = $this->_handleException($server, $request, $exception); } echo $response; }
/** * Start a JSON-RPC server, passing the handler class. * @param string $service_class */ public function service($service_class) { $request = new Zend_Json_Server_Request(); $request->setOptions(array("method" => $this->_request->getParam("method"), "id" => $this->_request->getParam("id"), "params" => $this->_request->getParam("params"))); $server = new Zend_Json_Server(); $server->setClass($service_class); $server->handle($request); exit; }
public function serviceAction() { $class = 'Admin_Model_RPC'; $server = new Zend_Json_Server(); $server->setClass($class); $server->setAutoEmitResponse(true); $server->handle(); exit; }
/** * JSON SERVER */ public function jsonAction() { $server = new Zend_Json_Server(); $server->setClass('My_Application_Model_Data'); if ('GET' == $_SERVER['REQUEST_METHOD']) { $server->setTarget($this->_url)->setEnvelope(Zend_Json_Server_Smd::ENV_JSONRPC_2); $smd = $server->getServiceMap(); header('Content-Type: application/json'); echo $smd; return; } echo $server->handle(); }
public function jsonAction() { $server = new Zend_Json_Server(); $server->setClass('My_Server_Ventas'); if ('GET' == $_SERVER['REQUEST_METHOD']) { $server->setEnvelope(Zend_Json_Server_Smd::ENV_JSONRPC_2); $smd = $server->getServiceMap(); //header('Content-Type: application/json'); echo $smd; return; } $server->handle(); }
public function jsonAction() { $this->_helper->viewRenderer->setNoRender(); $server = new Zend_Json_Server(); $server->setClass('Application_Model_Data', 'cf'); if ('GET' == $_SERVER['REQUEST_METHOD']) { $server->setTarget('http://localhost/xmlrpc-test/public/server/json')->setEnvelope(Zend_Json_Server_Smd::ENV_JSONRPC_2); $smd = $server->getServiceMap(); header('Content-Type: application/json'); echo $smd; return; } echo $server->handle(); }
/** * JSON-RPC controller * * @return void */ public function indexAction() { $this->_helper->layout->disableLayout(); $this->_helper->viewRenderer->setNoRender(); $server = new Zend_Json_Server(); $server->setClass('WorklogService'); if ('GET' == $_SERVER['REQUEST_METHOD']) { $server->setTarget('/rpc/index')->setEnvelope(Zend_Json_Server_Smd::ENV_JSONRPC_2)->setDojoCompatible(true); $smd = $server->getServiceMap(); header('Content-Type: application/json'); echo $smd; return; } // Handle the request: $server->handle(); }
/** * JSON CLIENT */ public function jsonAction() { $server = new Zend_Json_Server(); $server->setClass('My_Application_Model_Data'); if ('GET' == $_SERVER['REQUEST_METHOD']) { $server->setTarget($this->_url)->setEnvelope(Zend_Json_Server_Smd::ENV_JSONRPC_2); $smd = $server->getServiceMap(); if (isset($_GET['method'])) { //On recupere les methodes depuis le service map $arrSmd = Zend_Json::decode($smd); //Construction du tableau contenant les liens des methodes $links = '<h2>' . 'Liste des methodes JSON' . '</h2>' . '<table>' . '<tr>'; foreach ($arrSmd["services"] as $serv => $value) { $links .= '<td width="100px" align="center">' . '<a href="' . $this->_url . '?method=' . $serv . '">' . $serv . '</a>' . '</td>'; } $links .= '</tr>' . '</table>'; //Envoi du tableau a la vue $this->view->links = $links; //On recupere la methode appelee $method = $_GET['method']; $nbParam = 0; //Nombre de parametre de la methode //On cree un formulaire contenant les parametres $formtxt = '<form name="' . $method . '" method="post">' . '<table>'; foreach ($smd->getService($method)->getParams() as $param) { $formtxt .= '<tr>' . '<td>' . $param['name'] . ' (' . $param['type'] . ') :</td>' . '<td><input type=text name="' . $param['name'] . '"></td>' . '<tr>'; $nbParam++; } $formtxt .= '<tr>' . '<td></td>' . '<td><input type=submit value=Envoyer></td>' . '</tr>' . '</table>' . '</form>'; //On envoi le formulaire a la vue json client $this->view->form = $formtxt; //On envoi le nom de la methode traitee a la vue $this->view->method = $method; //On envoi le nombre de parametres du formulaire a la vue $this->view->nbParam = $nbParam; } else { $this->_helper->viewRenderer->setNoRender(true); header('Content-Type: application/json'); echo $smd; return; } } else { $this->_helper->viewRenderer->setNoRender(); echo $server->handle(); } }
public function indexAction() { // Instantiate server, etc. $server = new Zend_Json_Server(); Zend_Registry::set("fileserver_helper", $this->_fileHelper); $server->setClass('Admin_Model_FileServer'); if ('GET' == $_SERVER['REQUEST_METHOD']) { // Indicate the URL endpoint, and the JSON-RPC version used: $server->setTarget('/file-server/')->setEnvelope(Zend_Json_Server_Smd::ENV_JSONRPC_2); // Grab the SMD $smd = $server->getServiceMap(); // Return the SMD to the client header('Content-Type: application/json'); echo $smd; die; } $server->handle(); die; }
$autoloader = Zend_Loader_Autoloader::getInstance(); /** * ================================================================== * Prepare environment and execute JSON calls * ================================================================== */ require APPLICATION_PATH . '/includes/classes/remote.class.php'; header('Access-Control-Allow-Origin: *'); $server = new Zend_Json_Server(); $server->setClass('Kimai_Remote_Api'); if ('GET' == $_SERVER['REQUEST_METHOD']) { // Indicate the URL endpoint, and the JSON-RPC version used: $server->setTarget('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'])->setEnvelope(Zend_Json_Server_Smd::ENV_JSONRPC_2); // Grab the SMD $smd = $server->getServiceMap(); // Return the SMD to the client header('Content-Type: application/json'); echo $smd; return; } /** * http request will * - parse php://input * - json_decode it * - auto setOptions * therefore request should be a string e.g. {jsonrpc : '2.0', method: '<actionString>', params : [param1, param2], id : '<anyId>' } */ $request = new Zend_Json_Server_Request_Http(); $server->setRequest($request); $server->handle();
function testHandleReturnObject() { $server = new Zend_Json_Server(); $server->addFunction('Zend_Json_Server_TestFunc8'); ob_start(); $server->handle(array('method' => 'Zend_Json_Server_TestFunc8')); $result = ob_get_clean(); $this->assertEquals('{"__className": "stdClass", "foo" : "bar", "baz" : true, "bat" : 123, "qux" : false, "status" : "success"}', $result, "Bas Response"); }
function testHandleException() { $server = new Zend_Json_Server(); $server->addFunction('Zend_Json_Server_TestFunc10'); ob_start(); $server->handle(array('method' => 'Zend_Json_Server_TestFunc10')); ob_end_clean(); ob_start(); $server->fault(new Exception('An error occurred.', 404)); $result = ob_get_clean(); $this->assertEquals('{"msg" : "An error occurred.", "code" : 404}', $result, "Bad Response"); }
/** * JSONRPC server */ public function jsonrpcAction() { $server = new Zend_Json_Server(); $server->setClass( 'User_Service_User', 'user' ); echo $server->handle(); }
/** * JSON-RPC entrance. * * @return Void */ public function apiAction() { Zend_Registry::set('CMS', true); /** * Prepare the server. Zend_Json_Server cannot work with batched requests natively, * so that's taken care of customly here. Therefore, autoEmitResponse is set to false * so the server doesn't print the response directly. */ $server = new Zend_Json_Server(); $server->setClass('Garp_Content_Manager_Proxy'); $server->setAutoEmitResponse(false); if ($this->getRequest()->isPost()) { $post = $this->_getJsonRpcRequest(); $batch = false; $responses = array(); $requests = Zend_Json::decode($post, Zend_Json::TYPE_ARRAY); /** * Check if this was a batch request. In that case the array is a plain array of * arrays. If not, there will be a 'jsonrpc' key in the root of the array. */ $batch = !array_key_exists('jsonrpc', $requests); if (!$batch) { $requests = array($requests); } foreach ($requests as $i => $request) { $request = $this->_reformJsonRpcRequest($request); $requestJson = Zend_Json::encode($request); $requestObj = new Zend_Json_Server_Request(); $requestObj->loadJson($requestJson); $server->setRequest($requestObj); /** * Note; response gets returned by reference, resulting in a $responses array * containing all the same items. * That's why clone is used here. */ $response = clone $server->handle(); $responses[] = $response; } $response = $batch ? '[' . implode(',', $responses) . ']' : $responses[0]; } else { $response = $server->getServiceMap(); } $this->_helper->layout->setLayout('json'); // filter out escaped slashes, because they're annoying and not necessary. $response = str_replace('\\/', '/', $response); $this->view->response = $response; }
function example($cycle) { $server = new Zend_Json_Server(); $server->setClass('WOPR_Test_Example'); return $cycle->send($server->handle(), Cycle_Response::JSON); }
/** * Override of run method to provide JSON server interface * * @return void */ public function run() { if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') { // run Zend_Json_Server instead of the MVC stack $request = new Zend_Controller_Request_Http(); $info = explode('/', trim($request->getPathInfo(), '/')); $module = $info[0]; $service = $info[1]; // attach the service to the JSON-RPC server $service = $this->_loadService($module, $service); $server = new Zend_Json_Server(); $server->setClass($service); if ('GET' == $_SERVER['REQUEST_METHOD']) { // return the service map $server->setEnvelope(Zend_Json_Server_Smd::ENV_JSONRPC_2); $smd = $server->getServiceMap(); header('Content-Type: application/json'); echo $smd; return; } $server->handle(); } else { parent::run(); } }
/** * Verarbeitet einen Aufruf des Json Servers * @param Dragon_Json_Server_Request_Http $request * @return mixed|null|Zend_Json_Server_Response * @throws InvalidArgumentException */ public function handle($request = null) { $requestmethod = null; if (isset($request)) { if (!$request instanceof Dragon_Json_Server_Request_Http) { throw new Dragon_Application_Exception_System('incorrect requestclass', array('requestclass' => get_class($request))); } $requestmethod = 'POST'; } else { $request = new Dragon_Json_Server_Request_Http(); } if (!isset($requestmethod)) { $requestmethod = $_SERVER['REQUEST_METHOD']; } $configCache = new Dragon_Application_Config('dragon/json/cache'); if (!isset($configCache->filepath) || !Zend_Server_Cache::get($configCache->filepath, $this)) { $configRoute = new Dragon_Application_Config('dragon/json/route'); $packageregistry = Zend_Registry::get('Dragon_Package_Registry'); foreach ($packageregistry->getClassnames('Service') as $servicename) { if (isset($configRoute->{$servicename})) { $routename = $configRoute->{$servicename}; } else { $routename = str_replace('_', '.', $servicename); } $this->setClass($servicename, $routename); } if (isset($configCache->filepath)) { Zend_Server_Cache::save($configCache->filepath, $this); } } switch ($requestmethod) { case 'POST': $this->setResponse(new Dragon_Json_Server_Response_Http()); $autoemitresponse = $this->autoEmitResponse(); if ($autoemitresponse) { $this->setAutoEmitResponse(false); } $response = parent::handle($request); $result = $response->getResult(); if (isset($result['result'])) { $subresult =& $result['result']; } else { $subresult =& $result; } if (is_array($subresult)) { foreach ($request->getMap() as $key => $value) { if (isset($subresult[$key])) { $subresult[$value] = $subresult[$key]; } } $response->setResult($result); } if ($autoemitresponse) { $this->setAutoEmitResponse(true); echo $response; return; } return $response; break; case 'GET': header('Content-Type: application/json'); $servicemap = $this->getServiceMap(); Zend_Registry::get('Dragon_Plugin_Registry')->invoke('Dragon_Json_Plugin_Servicemap_Interface', array($servicemap)); echo $servicemap; break; } }