/**
  * Intercepts the result of Controller::handleRequest() to log the values into the database
  * @param {SS_HTTPRequest} $request The SS_HTTPRequest object that is responsible for distributing request parsing.
  * @return {SS_HTTPResponse} The response that this controller produces, including HTTP headers such as redirection info
  */
 public function handleRequest(SS_HTTPRequest $request, DataModel $model)
 {
     $response = parent::handleRequest($request, $model);
     //Log Request
     try {
         $xml = simplexml_load_string($this->request->getBody());
         if ($xml) {
             //Strip sensitive info from request
             if (strpos($xml->methodName, 'metaWeblog.') === 0 || strpos($xml->methodName, 'kapost.') === 0) {
                 $xml->params->param[2]->value->string = '[' . _t('LoggedKapostService.PASSWORD_FILTERED', '_PASSWORD FILTERED') . ']';
                 //For metaWeblog.newMediaObject requests clear the bits for the file before writing
                 if ($xml->methodName == 'metaWeblog.newMediaObject') {
                     $xml->params->param[3]->value->struct->member[2]->value->base64 = '[' . _t('LoggedKapostService.BITS_FILTERED', '_BASE64 BITS FILTERED') . ']';
                 }
             }
             //Write a log entry
             $logEntry = new KapostBridgeLog();
             $logEntry->Method = $xml->methodName->__toString();
             $logEntry->Request = $xml->asXML();
             $logEntry->Response = $response instanceof SS_HTTPResponse ? $this->cleanDebugInfo($response->getBody()) : (is_string($response) ? $this->cleanDebugInfo($response) : null);
             $logEntry->write();
         }
     } catch (Exception $e) {
     }
     return $response;
 }
 /**
  * Cleans up the expired tokens after writing
  */
 protected function onAfterWrite()
 {
     parent::onAfterWrite();
     //Clean up the expired tokens
     $expiredTokens = KapostPreviewToken::get()->filter('Created:LessThan', date('Y-m-d H:i:s', strtotime('-' . KapostService::config()->preview_token_expiry . ' minutes')));
     if ($expiredTokens->count() > 0) {
         foreach ($expiredTokens as $token) {
             $token->delete();
         }
     }
 }
 /**
  * Intercepts the result of Controller::handleRequest() to log the values into the database
  * @param {SS_HTTPRequest} $request The SS_HTTPRequest object that is responsible for distributing request parsing.
  * @return {SS_HTTPResponse} The response that this controller produces, including HTTP headers such as redirection info
  */
 public function handleRequest(SS_HTTPRequest $request, DataModel $model)
 {
     $response = parent::handleRequest($request, $model);
     //Log Request
     try {
         $xml = @simplexml_load_string($this->request->getBody());
         if ($xml) {
             //Strip sensitive info from request
             if (strpos($xml->methodName, 'metaWeblog.') === 0 || strpos($xml->methodName, 'kapost.') === 0) {
                 $xml->params->param[2]->value->string = '[' . _t('LoggedKapostService.PASSWORD_FILTERED', '_PASSWORD FILTERED') . ']';
                 //For metaWeblog.newMediaObject requests clear the bits for the file before writing
                 if ($xml->methodName == 'metaWeblog.newMediaObject') {
                     $xml->params->param[3]->value->struct->member[2]->value->base64 = '[' . _t('LoggedKapostService.BITS_FILTERED', '_BASE64 BITS FILTERED') . ']';
                 }
             }
             //Write a log entry
             $methodName = $xml->methodName->__toString();
             $requestXML = $xml->asXML();
         } else {
             $methodName = _t('LoggedKapostService.UNKNOWN_METHOD', '_Unknown Method');
             $requestXML = _t('LoggedKapostService.REQUEST_PARSE_ERROR', '_Request Parsing Error');
         }
         //If the ignore not found configuration option is set to true and the response is a 404 do not log
         if ($this->config()->ignore_not_found && $response instanceof SS_HTTPResponse && $response->getStatusCode() == 404) {
             return $response;
         }
         //Write a log entry
         $logEntry = new KapostBridgeLog();
         $logEntry->Method = $methodName;
         $logEntry->Request = $requestXML;
         $logEntry->Response = $response instanceof SS_HTTPResponse ? $this->cleanDebugInfo($response->getBody()) : (is_string($response) ? $this->cleanDebugInfo($response) : null);
         $logEntry->UserAgent = $request->getHeader('User-Agent');
         $logEntry->write();
     } catch (Exception $e) {
     }
     return $response;
 }
 /**
  * Gets the referenced object of this kapost bridge log entry
  */
 public function getReferenceObject()
 {
     if ($this->reference_object === false) {
         $kapostRefID = false;
         if ($this->Method == 'metaWeblog.getPost' || $this->Method == 'metaWeblog.editPost') {
             $xml = simplexml_load_string($this->Request);
             if ($xml) {
                 $kapostRefID = $xml->params->param[0]->value->string->__toString();
             }
         } else {
             if ($this->Method == 'metaWeblog.newPost' || $this->Method == 'kapost.getPreview' || $this->Method == 'metaWeblog.newMediaObject') {
                 $xml = simplexml_load_string($this->Response);
                 if ($xml && !isset($xml->fault)) {
                     if ($this->Method == 'metaWeblog.newPost') {
                         $kapostRefID = $xml->params->param[0]->value->string->__toString();
                     } else {
                         if ($this->Method == 'kapost.getPreview') {
                             $kapostRefID = $xml->params->param[0]->value->struct->member[1]->value->string->__toString();
                         } else {
                             if ($this->Method == 'metaWeblog.newMediaObject') {
                                 //Attempt to lookup the referenced file
                                 $fileURL = $xml->params->param[0]->value->struct->member[1]->value->string->__toString();
                                 if (!empty($fileURL)) {
                                     $file = KapostService::find_file_by_url($fileURL);
                                     if (!empty($file) && $file !== false && $file->ID > 0) {
                                         $this->reference_object = $file;
                                         return $this->reference_object;
                                     } else {
                                         $this->reference_object = null;
                                         return;
                                     }
                                 } else {
                                     $this->reference_object = null;
                                     return;
                                 }
                             }
                         }
                     }
                 }
             } else {
                 $this->reference_object = null;
                 return;
             }
         }
         //If we have a
         if (!empty($kapostRefID)) {
             Versioned::reset();
             //Reset versioned to remove filters
             //Try looking for a page
             $page = SiteTree::get()->filter('KapostRefID', Convert::raw2sql($kapostRefID))->first();
             if (!empty($page) && $page !== false && $page->exists()) {
                 $this->reference_object = $page;
             } else {
                 //Allow extensions to add their own logic
                 $extensionResult = array_filter($this->extend('updateObjectLookup', $kapostRefID), function ($item) {
                     return is_object($item);
                 });
                 $extensionResult = array_shift($extensionResult);
                 if (!empty($extensionResult) && $extensionResult !== false && $extensionResult->exists()) {
                     $this->reference_object = $kapostObj;
                 } else {
                     //Look for a kapost object that is not a preview
                     $kapostObj = KapostObject::get()->filter('KapostRefID', Convert::raw2sql($kapostRefID))->first();
                     if (!empty($kapostObj) && $kapostObj !== false && $kapostObj->exists() && $kapostObj->IsKapostPreview == false) {
                         $this->reference_object = $kapostObj;
                     } else {
                         //No result so set to null
                         $this->reference_object = null;
                     }
                 }
             }
         } else {
             //No Kapost Reference ID so set to null
             $this->reference_object = null;
         }
     }
     return $this->reference_object;
 }
 /**
  * Cleans up expired Kapost previews after twice the token expiry
  */
 private function cleanUpExpiredPreviews()
 {
     $expiredPreviews = KapostObject::get()->filter('IsKapostPreview', true)->filter('LastEdited:LessThan', date('Y-m-d H:i:s', strtotime('-' . KapostService::config()->preview_data_expiry . ' minutes')));
     if ($expiredPreviews->count() > 0) {
         foreach ($expiredPreviews as $kapostObj) {
             $kapostObj->delete();
         }
     }
 }