/**
  * Sends a JSON-encoded response.
  * 
  * @param	array		$data
  */
 protected function sendJsonResponse(array $data)
 {
     $json = JSON::encode($data);
     // send JSON response
     header('Content-type: application/json');
     echo $json;
     exit;
 }
	/**
	 * @see	wcf\action\AJAXInvokeAction::sendResponse()
	 */
	protected function sendResponse() {
		if (!isset($_POST['isFallback'])) {
			parent::sendResponse();
		}
		
		// IE9 is mad if iframe response is application/json
		header('Content-type: text/plain');
		echo JSON::encode($this->response);
		exit;
	}
Exemple #3
0
	/**
	 * @see	wcf\action\Action::execute()
	 */
	public function execute() {
		AbstractSecureAction::execute();
		
		// execute clipboard action
		$this->executeAction();
		
		// get editor items
		$returnValues = $this->getEditorItems();
		// send JSON response
		header('Content-type: application/json');
		echo JSON::encode($returnValues);
		exit;
	}
 /**
  * @see	\wcf\action\IAction::execute()
  */
 public final function execute()
 {
     parent::execute();
     $methodName = 'step' . StringUtil::firstCharToUpperCase($this->step);
     if (!method_exists($this, $methodName)) {
         throw new AJAXException("Class '" . get_class($this) . "' does not implement the required method '" . $methodName . "'");
     }
     // execute step
     $this->{$methodName}();
     $this->executed();
     // send JSON-encoded response
     header('Content-type: application/json');
     echo JSON::encode($this->data);
     exit;
 }
 /**
  * Sends a JSON-encoded response.
  * 
  * @param	integer		$progress
  * @param	array		$parameters
  * @param	string		$proceedURL
  */
 protected function sendResponse($progress = 0, array $parameters = null, $proceedURL = '')
 {
     if ($parameters === null) {
         $parameters = $this->parameters;
     }
     // build return values
     $returnValues = array('className' => $this->className, 'loopCount' => $this->loopCount + 1, 'parameters' => $parameters, 'proceedURL' => $proceedURL, 'progress' => $progress);
     // include template on startup
     if ($this->loopCount == -1) {
         $returnValues['template'] = WCF::getTPL()->fetch('worker');
     }
     // send JSON-encoded response
     header('Content-type: application/json');
     echo JSON::encode($returnValues);
     exit;
 }
 /**
  * @see	wcf\action\IAction::execute()
  */
 public function execute()
 {
     parent::execute();
     // validate class name
     if (!class_exists($this->className)) {
         throw new SystemException("unknown class '" . $this->className . "'");
     }
     if (!ClassUtil::isInstanceOf($this->className, 'wcf\\data\\IDatabaseObjectAction')) {
         throw new SystemException("'" . $this->className . "' should implement wcf\\system\\IDatabaseObjectAction");
     }
     // create object action instance
     $this->objectAction = new $this->className($this->objectIDs, $this->actionName, $this->parameters);
     // validate action
     try {
         $this->objectAction->validateAction();
     } catch (UserInputException $e) {
         $this->throwException($e);
     } catch (ValidateActionException $e) {
         $this->throwException($e);
     }
     // execute action
     try {
         $this->response = $this->objectAction->executeAction();
     } catch (\Exception $e) {
         $this->throwException($e);
     }
     $this->executed();
     // send JSON-encoded response
     header('Content-type: application/json');
     echo JSON::encode($this->response);
     exit;
 }
	/**
	 * Sends JSON-Encoded response.
	 */
	protected function sendResponse() {
		header('Content-type: application/json');
		echo JSON::encode($this->response);
		exit;
	}
 /**
  * Writes an error to log file.
  */
 protected function logError()
 {
     if (!empty($this->exceptionID)) {
         return;
     }
     $logFile = WCF_DIR . 'log/' . gmdate('Y-m-d', TIME_NOW) . '.txt';
     // try to create file
     @touch($logFile);
     // validate if file exists and is accessible for us
     if (!file_exists($logFile) || !is_writable($logFile)) {
         /*
         	We cannot recover if we reached this point, the server admin
         	is urged to fix his pretty much broken configuration.
         	
         	GLaDOS: Look at you, sailing through the air majestically, like an eagle... piloting a blimp.
         */
         return;
     }
     $e = $this->getPrevious() ?: $this;
     // don't forget to update ExceptionLogViewPage, when changing the log file format
     $message = gmdate('r', TIME_NOW) . "\n" . 'Message: ' . $e->getMessage() . "\n" . 'File: ' . $e->getFile() . ' (' . $e->getLine() . ")\n" . 'PHP version: ' . phpversion() . "\n" . 'WCF version: ' . WCF_VERSION . "\n" . 'Request URI: ' . (isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '') . "\n" . 'Referrer: ' . (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '') . "\n" . 'User-Agent: ' . (isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '') . "\n" . 'Information: ' . JSON::encode($this->information) . "\n" . "Stacktrace: \n  " . implode("\n  ", explode("\n", $this->__getTraceAsString())) . "\n";
     // calculate Exception-ID
     $this->exceptionID = StringUtil::getHash($message);
     $message = "<<<<<<<<" . $this->exceptionID . "<<<<\n" . $message . "<<<<\n\n";
     // append
     @file_put_contents($logFile, $message, FILE_APPEND);
 }
 /**
  * @see	\wcf\action\IAction::execute()
  */
 public function execute()
 {
     AbstractAction::execute();
     $returnValues = null;
     switch ($this->actionName) {
         case 'count':
             $returnValues = array('count' => $this->count());
             break;
         case 'getQuotes':
             $returnValues = array('template' => $this->getQuotes());
             break;
         case 'markForRemoval':
             $this->markForRemoval();
             break;
         case 'remove':
             $returnValues = array('count' => $this->remove());
             break;
         case 'removeMarkedQuotes':
             $returnValues = array('count' => $this->removeMarkedQuotes());
             break;
         default:
             throw new SystemException("Unknown action '" . $this->actionName . "'");
             break;
     }
     if (is_array($returnValues) && $this->_getFullQuoteObjectIDs) {
         $returnValues['fullQuoteObjectIDs'] = $this->getFullQuoteObjectIDs();
     }
     $this->executed();
     // force session update
     WCF::getSession()->update();
     WCF::getSession()->disableUpdate();
     if ($returnValues !== null) {
         // send JSON-encoded response
         header('Content-type: application/json');
         echo JSON::encode($returnValues);
     }
     exit;
 }
 /**
  * Displays an error message.
  * 
  * @param	string	$name
  * @param	array	$parameters
  */
 public function error($name, array $parameters = array())
 {
     Log::error('package.' . $name . ':' . JSON::encode($parameters));
     if ($parameters) {
         throw new ArgvException(CLIWCF::getLanguage()->getDynamicVariable('wcf.acp.package.error.' . $name, $parameters), $this->getUsage());
     } else {
         throw new ArgvException(CLIWCF::getLanguage()->get('wcf.acp.package.error.' . $name), $this->argv->getUsageMessage());
     }
 }
Exemple #11
0
 /**
  * @see	\wcf\action\IAction::execute()
  */
 public function execute()
 {
     AbstractAction::execute();
     $returnValues = array('actionName' => $this->actionName, 'pollID' => $this->pollID);
     switch ($this->actionName) {
         case 'getResult':
             $this->getResult($returnValues);
             break;
         case 'getVote':
             $this->getVote($returnValues);
             break;
         case 'vote':
             $this->vote($returnValues);
             break;
     }
     $this->executed();
     // send JSON-encoded response
     header('Content-type: application/json');
     echo JSON::encode($returnValues);
     exit;
 }
 /**
  * @see	wcf\action\IAction::execute()
  */
 public function execute()
 {
     parent::execute();
     // get object ids
     $objectIDs = $this->getObjectIDs();
     // create object action instance
     $this->objectAction = new $this->parameters['className']($objectIDs, $this->parameters['actionName']);
     // validate action
     try {
         $this->objectAction->validateAction();
     } catch (ValidateActionException $e) {
         throw new AJAXException("validation failed: " . $e->getMessage());
     }
     // execute action
     try {
         $this->response = $this->objectAction->executeAction();
     } catch (\Exception $e) {
         throw new AJAXException('unknown exception caught: ' . $e->getMessage());
     }
     $this->executed();
     // send JSON-encoded response
     header('Content-type: application/json');
     echo JSON::encode($this->response);
     exit;
 }
Exemple #13
0
 /**
  * Initializes command handling.
  */
 protected function initCommands()
 {
     // add command name completer
     self::getReader()->addCompleter(new CLICommandNameCompleter());
     while (true) {
         // roll back open transactions of the previous command, as they are dangerous in a long living script
         if (WCF::getDB()->rollBackTransaction()) {
             Log::warn('Previous command had an open transaction.');
         }
         self::getReader()->setHistoryEnabled(true);
         $line = self::getReader()->readLine('>');
         if ($line === null) {
             exit;
         }
         $line = StringUtil::trim($line);
         try {
             $command = CLICommandHandler::getCommand($line);
             $command->execute(CLICommandHandler::getParameters($line));
         } catch (IllegalLinkException $e) {
             Log::error('notFound:' . JSON::encode(array('command' => $line)));
             self::getReader()->println(WCF::getLanguage()->getDynamicVariable('wcf.cli.error.command.notFound', array('command' => $line)));
             if (self::getArgvParser()->exitOnFail) {
                 exit(1);
             }
             continue;
         } catch (PermissionDeniedException $e) {
             Log::error('permissionDenied');
             self::getReader()->println(WCF::getLanguage()->getDynamicVariable('wcf.global.error.permissionDenied'));
             if (self::getArgvParser()->exitOnFail) {
                 exit(1);
             }
             continue;
         } catch (ArgvException $e) {
             // show error message and usage
             if ($e->getMessage()) {
                 echo $e->getMessage() . PHP_EOL;
             }
             echo $e->getUsageMessage();
             if (self::getArgvParser()->exitOnFail) {
                 exit(1);
             }
             continue;
         } catch (\Exception $e) {
             Log::error($e);
             if (self::getArgvParser()->exitOnFail) {
                 exit(1);
             }
             continue;
         }
     }
 }
Exemple #14
0
	/**
	 * Throws a JSON-encoded error message
	 * 
	 * @param	string		$message
	 * @param	boolean		$isDoomsday
	 * @param	string		$stacktrace
	 * @param	array<mixed>	$returnValues
	 */
	public function __construct($message, $errorType = self::INTERNAL_ERROR, $stacktrace = null, $returnValues = array()) {
		if ($stacktrace === null) $stacktrace = $this->getTraceAsString();
		
		if (WCF::debugModeIsEnabled()) {
			$responseData = array(
				'message' => $message,
				'stacktrace' => nl2br($stacktrace)
			);
		}
		else {
			$responseData = array(
				'message' => $this->_getMessage()
			);
		}
		
		$responseData['code'] = $errorType;
		$responseData['returnValues'] = $returnValues;
		
		$statusHeader = '';
		switch ($errorType) {
			case self::MISSING_PARAMETERS:
				$statusHeader = 'HTTP/1.0 400 Bad Request';
				$responseData['message'] = WCF::getLanguage()->get('wcf.ajax.error.badRequest');
				
				$this->logError();
			break;
			
			case self::SESSION_EXPIRED:
				$statusHeader = 'HTTP/1.0 401 Unauthorized';
			break;
			
			case self::INSUFFICIENT_PERMISSIONS:
				$statusHeader = 'HTTP/1.0 403 Forbidden';
			break;
			
			case self::BAD_PARAMETERS:
				$statusHeader = 'HTTP/1.0 412 Precondition Failed';
			break;
			
			default:
			case self::INTERNAL_ERROR:
				//header('HTTP/1.0 418 I\'m a Teapot');
				header('HTTP/1.0 503 Service Unavailable');
				
				$responseData['code'] = self::INTERNAL_ERROR;
				if (!WCF::debugModeIsEnabled()) {
					$responseData['message'] = WCF::getLanguage()->get('wcf.ajax.error.internalError');
				}
				
				$this->logError();
			break;
		}
		
		header($statusHeader);
		header('Content-type: application/json');
		echo JSON::encode($responseData);
		exit;
	}
 /**
  * Throws a JSON-encoded error message
  * 
  * @param	string		$message
  * @param	boolean		$isDoomsday
  * @param	string		$stacktrace
  * @param	array		$returnValues
  * @param	string		$exceptionID
  * @param	array<mixed>	$returnValues
  */
 public function __construct($message, $errorType = self::INTERNAL_ERROR, $stacktrace = null, $returnValues = array(), $exceptionID = '')
 {
     if ($stacktrace === null) {
         $stacktrace = $this->getTraceAsString();
     }
     $responseData = array('code' => $errorType, 'message' => $message, 'returnValues' => $returnValues);
     // include a stacktrace if:
     // - debug mode is enabled
     // - within ACP and a SystemException was thrown
     if (WCF::debugModeIsEnabled(false) || WCF::debugModeIsEnabled() && self::INTERNAL_ERROR) {
         $responseData['stacktrace'] = nl2br($stacktrace);
     }
     $statusHeader = '';
     switch ($errorType) {
         case self::MISSING_PARAMETERS:
             $statusHeader = 'HTTP/1.0 400 Bad Request';
             $responseData['exceptionID'] = $exceptionID;
             $responseData['message'] = WCF::getLanguage()->get('wcf.ajax.error.badRequest');
             break;
         case self::SESSION_EXPIRED:
             $statusHeader = 'HTTP/1.0 409 Conflict';
             break;
         case self::INSUFFICIENT_PERMISSIONS:
             $statusHeader = 'HTTP/1.0 403 Forbidden';
             break;
         case self::BAD_PARAMETERS:
             $statusHeader = 'HTTP/1.0 431 Bad Parameters';
             $responseData['exceptionID'] = $exceptionID;
             break;
         default:
         case self::INTERNAL_ERROR:
             //header('HTTP/1.0 418 I\'m a Teapot');
             header('HTTP/1.0 503 Service Unavailable');
             $responseData['code'] = self::INTERNAL_ERROR;
             $responseData['exceptionID'] = $exceptionID;
             if (!WCF::debugModeIsEnabled()) {
                 $responseData['message'] = WCF::getLanguage()->get('wcf.ajax.error.internalError');
             }
             break;
     }
     header($statusHeader);
     header('Content-type: application/json');
     echo JSON::encode($responseData);
     exit;
 }