Exemplo n.º 1
0
 /**
  * If this is a xajax request, call the requested PHP function, build the response and send it back to the browser
  *
  * This is the main server side engine for xajax.
  * It handles all the incoming requests, including the firing of events and handling of the response.
  * If your RequestURI is the same as your web page, then this function should be called before ANY
  * headers or HTML is output from your script.
  * 
  * This function may exit after the request is processed, if the 'core.exit_after' option is set to true.
  *
  * @return void
  * 
  * @see <Xajax\Xajax->canProcessRequest>
  */
 public function processRequest()
 {
     if (isset($_SERVER['HTTP_CHALLENGE_RESPONSE'])) {
         $this->challengeResponse = $_SERVER['HTTP_CHALLENGE_RESPONSE'];
     }
     // Check to see if headers have already been sent out, in which case we can't do our job
     if (headers_sent($filename, $linenumber)) {
         echo $this->trans('errors.output.already-sent', array('location' => $filename . ':' . $linenumber)), "\n", $this->trans('errors.output.advice');
         exit;
     }
     //
     if (!$this->canProcessRequest()) {
         return;
     }
     // Use xajax error handler if necessary
     if ($this->getOption('core.error.handle')) {
         $this->setErrorMessage('');
         set_error_handler(array($this, "errorHandler"));
     }
     $bEndRequest = false;
     $mResult = true;
     // handle beforeProcessing event
     if (isset($this->aProcessingEvents[self::PROCESSING_EVENT_BEFORE])) {
         $xResponse = $this->aProcessingEvents[self::PROCESSING_EVENT_BEFORE]->call(array(&$bEndRequest));
         if ($xResponse instanceof Response\Response) {
             $this->xResponseManager->append($xResponse);
         }
     }
     if (!$bEndRequest) {
         $mResult = $this->xPluginManager->processRequest();
     }
     // Clean the processing buffer
     if ($this->getOption('core.process.clean_buffer')) {
         $er = error_reporting(0);
         while (ob_get_level() > 0) {
             ob_end_clean();
         }
         error_reporting($er);
     }
     if ($mResult === true) {
         // handle afterProcessing event
         if (isset($this->aProcessingEvents[self::PROCESSING_EVENT_AFTER])) {
             $bEndRequest = false;
             $xResponse = $this->aProcessingEvents[self::PROCESSING_EVENT_AFTER]->call(array($bEndRequest));
             if ($xResponse instanceof Response\Response) {
                 $this->xResponseManager->append($xResponse);
             }
         }
     } else {
         if (is_string($mResult)) {
             // $mResult contains an error message
             // the request was missing the cooresponding handler function
             // or an error occurred while attempting to execute the
             // handler.  replace the response, if one has been started
             // and send a debug message.
             $this->xResponseManager->clear();
             $this->xResponseManager->append(new Response\Response());
             // handle invalidRequest event
             if (isset($this->aProcessingEvents[self::PROCESSING_EVENT_INVALID])) {
                 $xResponse = $this->aProcessingEvents[self::PROCESSING_EVENT_INVALID]->call($mResult);
                 if ($xResponse instanceof Response\Response) {
                     $this->xResponseManager->append($xResponse);
                 }
             }
             $this->xResponseManager->debug($mResult);
         }
     }
     if ($this->sErrorMessage && $this->getOption('core.error.handle') && $this->hasOption('core.error.log_file')) {
         if (($fH = @fopen($this->getOption('core.error.log_file'), "a")) != null) {
             fwrite($fH, $this->trans('errors.debug.ts-message', array('timestamp' => strftime("%b %e %Y %I:%M:%S %p"), 'message' => $this->sErrorMessage)));
             fclose($fH);
         } else {
             $this->xResponseManager->debug($this->trans('errors.debug.write-log', array('file' => $this->getOption('core.error.log_file'))));
         }
         $this->xResponseManager->debug($this->trans('errors.debug.message', array('message' => $this->sErrorMessage)));
     }
     $this->xResponseManager->send();
     if ($this->getOption('core.error.handle')) {
         restore_error_handler();
     }
     if ($this->getOption('core.process.exit_after')) {
         exit;
     }
 }