/** * Log the time it took to run the command, given the options and arguments */ public function __destruct() { $name = $this->name; $opts = json_encode((array) $this->opts); $args = json_encode((array) $this->args); YAWF::finish("\"{$name}\" command completed with opts {$opts} and args {$args}"); }
/** * Call the controller parent's before method, before making a new service * then checking that the service has authorization and setting its params. */ protected function before() { parent::before(); $this->service = $this->app->new_service(); if (!$this->service->auth()) { YAWF::finish(); } $this->set_params_from_parts('api_version', NULL, 'id'); }
/** * Require the web user to login using basic auth. * * Either pass the required username and password, * or pass an object in place of the username, and * have the object provide a "basic_auth()" method * with username and password parameters, returning * TRUE if the username and password are authorized. * * Note that this method will call YAWF::finish() * by default, to cause the web request to finish. * * @param String/Object $username the username or object with "basic_auth()" * @param String $password the required password (when username is a string) * @param Boolean $do_return whether to always return (default is FALSE) * @return Boolean whether the login succeeded */ public static function login($username, $password = '', $do_return = FALSE) { if (is_object($username)) { if ($username->basic_auth(self::username(), self::password())) { return TRUE; } } elseif (is_string($username)) { if (self::username() == $username && self::password() == $password) { return TRUE; } } // Wrong username and/or password self::challenge(); if ($do_return) { return FALSE; } YAWF::finish(); // this exits }
/** * Redirect to another URI, and possibly exit * * @param String $uri the URI to redirect at * @param Array $options an optional array of options (e.g. "exit") */ public function redirect($uri, $options = array()) { // Set flash messages to be shown on the next view page foreach (array('notice', 'warning', 'alert') as $level) { if ($message = array_key($options, $level)) { $this->controller->flash($level, $message); } } // Remember, type for interface testing is "test" if ($this->content_type !== DEFAULT_CONTENT_TYPE) { $uri .= '.' . $this->content_type; } // Set a location header and optional status $view_uri = AppView::uri($uri); $header = "Location: {$view_uri}"; if ($status = array_key($options, 'status')) { header($header, TRUE, $status); // set user-defined HTTP status code } else { header($header); } // Remain silent, and optionally exit or finish (preferred for logging) $this->is_silent = TRUE; if (array_key($options, 'exit')) { exit; } // careful! it stops our logging! if (array_key($options, 'finish')) { YAWF::finish("Redirected to {$view_uri}"); } }