code/control/KapostService.php
public function index() {
        //If the request is not a post request 404
        if(!$this->request->isPOST()) {
            if(class_exists('ErrorPage')) {
                $response=ErrorPage::response_for(404);
                if(!empty($response)) {
                    return parent::httpError(404, $response);
                }
            }
            
            return parent::httpError(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') {
            if(class_exists('ErrorPage')) {
                $response=ErrorPage::response_for(404);
                if(!empty($response)) {
                    return parent::httpError(404, $response);
                }
            }
            
            return parent::httpError(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 PhpXmlRpc\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
        PhpXmlRpc\PhpXmlRpc::$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 PhpXmlRpc\Response);});
            
                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 PhpXmlRpc\Response(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 PhpXmlRpc\Response(0, 17, _t('KapostService.SERVER_ERROR', '_Internal server error'));
            }
            
            return $this->generateErrorResponse($server, $response);
        }
    }
public function index() {
        //If the request is not a post request 404
        if(!$this->request->isPOST()) {
            if(class_exists('ErrorPage')) {
                $response=ErrorPage::response_for(404);
                if(!empty($response)) {
                    return parent::httpError(404, $response);
                }
            }
            
            return parent::httpError(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') {
            if(class_exists('ErrorPage')) {
                $response=ErrorPage::response_for(404);
                if(!empty($response)) {
                    return parent::httpError(404, $response);
                }
            }
            
            return parent::httpError(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 PhpXmlRpc\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
        PhpXmlRpc\PhpXmlRpc::$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 PhpXmlRpc\Response);});
            
                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 PhpXmlRpc\Response(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 PhpXmlRpc\Response(0, 17, _t('KapostService.SERVER_ERROR', '_Internal server error'));
            }
            
            return $this->generateErrorResponse($server, $response);
        }
    }
public function index ( ) { //If the request is not a post request 404 if ( ! $ this -> request -> isPOST ( ) ) { if ( class_exists ( 'ErrorPage' ) ) { $ response = ErrorPage :: response_for ( 404 ) ; if ( ! empty ( $ response ) ) { return parent :: httpError ( 404 , $ response ) ; } } return parent :: httpError ( 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' ) { if ( class_exists ( 'ErrorPage' ) ) { $ response = ErrorPage :: response_for ( 404 ) ; if ( ! empty ( $ response ) ) { return parent :: httpError ( 404 , $ response ) ; } } return parent :: httpError ( 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 PhpXmlRpc \ 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 PhpXmlRpc \ PhpXmlRpc :: $ 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 PhpXmlRpc \ Response ) ; } ) ; 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 PhpXmlRpc \ Response ( 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 PhpXmlRpc \ Response ( 0 , 17 , _t ( 'KapostService.SERVER_ERROR' , '_Internal server error' ) ) ; } return $ this -> generateErrorResponse ( $ server , $ response ) ; } }
Handles incoming requests to the kapost service
Handles incoming requests to the kapost service