+ // $format is currently only relevant for an ApiAction anyway
+ if ($format === null) {
+ $format = $this->format;
+ }
+
+ common_debug("User error '{$code}' on '{$this->action}': {$msg}", __FILE__);
+
+ if (!array_key_exists($code, ClientErrorAction::$status)) {
+ $code = 400;
+ }
+
+ $status_string = ClientErrorAction::$status[$code];
+
+ switch ($format) {
+ case 'xml':
+ header("HTTP/1.1 {$code} {$status_string}");
+ $this->initDocument('xml');
+ $this->elementStart('hash');
+ $this->element('error', null, $msg);
+ $this->element('request', null, $_SERVER['REQUEST_URI']);
+ $this->elementEnd('hash');
+ $this->endDocument('xml');
+ break;
+ case 'json':
+ if (!isset($this->callback)) {
+ header("HTTP/1.1 {$code} {$status_string}");
+ }
+ $this->initDocument('json');
+ $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
+ $this->text(json_encode($error_array));
+ $this->endDocument('json');
+ break;
+ case 'text':
+ header("HTTP/1.1 {$code} {$status_string}");
+ header('Content-Type: text/plain; charset=utf-8');
+ echo $msg;
+ break;
+ default:
+ throw new ClientException($msg, $code);
+ }
+ exit((int)$code);
+ }
+
+ /**
+ * If not logged in, take appropriate action (redir or exception)
+ *
+ * @param boolean $redir Redirect to login if not logged in
+ *
+ * @return boolean true if logged in (never returns if not)
+ */
+ public function checkLogin($redir=true)
+ {
+ if (common_logged_in()) {
+ return true;
+ }
+
+ if ($redir==true) {
+ common_set_returnto($_SERVER['REQUEST_URI']);
+ common_redirect(common_local_url('login'));
+ }
+
+ // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
+ $this->clientError(_('Not logged in.'), 403);