/** * Handles incoming requests to the kapost service */ public function index() { //If the request is not a post request 404 if (!$this->request->isPOST()) { return ErrorPage::response_for(404); } //If the request is not the kapost user agent 404 if (self::config()->check_user_agent == true && $this->request->getHeader('User-Agent') != 'Kapost XMLRPC::Client') { return ErrorPage::response_for(404); } $methods = array_fill_keys($this->exposed_methods, array('function' => array($this, 'handleRPCMethod'))); //Disable Content Negotiator and send the text/xml header (which kapost expects) ContentNegotiator::config()->enabled = false; $this->response->addHeader('Content-Type', 'text/xml'); $server = new xmlrpc_server($methods, false); $server->compress_response = true; if (Director::isDev()) { $server->setDebug(3); //Base 64 encoded debug information is included in the response $server->exception_handling = 2; //Exception's sent to the client } //Force the internal encoding of the XMLRPC library to utf-8 $GLOBALS['xmlrpc_internalencoding'] = self::config()->database_charset; return $server->service($this->request->getBody(), true); }
/** * Handles incoming requests to the kapost service */ public function index() { //If the request is not a post request 404 if (!$this->request->isPOST()) { return ErrorPage::response_for(404); } //If the request is not the kapost user agent 404 if (self::config()->check_user_agent == true && $this->request->getHeader('User-Agent') != 'Kapost XMLRPC::Client') { return ErrorPage::response_for(404); } $methods = array_fill_keys($this->exposed_methods, array('function' => array($this, 'handleRPCMethod'))); //Disable Content Negotiator and send the text/xml header (which kapost expects) ContentNegotiator::config()->enabled = false; $this->response->addHeader('Content-Type', 'text/xml'); $server = new xmlrpc_server($methods, false); $server->compress_response = true; if (Director::isDev()) { $server->setDebug(3); //Base 64 encoded debug information is included in the response } //Tell XML-RPC to re-throw the exception rather than trap it so we can allow the SilverStripe's normal error handling along side sending the xmlrpc response $server->exception_handling = 2; //Force the internal encoding of the XMLRPC library to utf-8 $GLOBALS['xmlrpc_internalencoding'] = self::config()->database_charset; try { return $server->service($this->request->getBody(), true); } catch (Exception $e) { //Call on SS_Log to log the error SS_Log::log($e, SS_Log::ERR); //Allow exceptions to handle the response $results = $this->extend('onException', $e); if ($results && is_array($results)) { $results = array_filter($results, function ($v) { return !is_null($v) && $v instanceof xmlrpcresp; }); if (count($results) > 0) { $this->generateErrorResponse($server, array_shift($results)); } } //If we're in dev mode relay the actual message to the client if (Director::isDev()) { $response = new xmlrpcresp(0, $e->getCode() + 100, _t('KapostService.ERROR_MESSAGE', '_{message} in {file} line {line_number}', array('message' => $e->getMessage(), 'file' => $e->getFile(), 'line_number' => $e->getLine()))); } else { $response = new xmlrpcresp(0, 17, _t('KapostService.SERVER_ERROR', '_Internal server error')); } return $this->generateErrorResponse($server, $response); } }