/** * 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'); }
/** * Note the start time of YAWF */ public static function start() { if (self::$start) { throw new Exception('YAWF has already started'); } self::$start = microtime(TRUE); // Keep note of our start time self::hook('default', 'self::unknown'); // Set the default hook method }
/** * Start an HTML form by returning an opening form tag * * @param String $id the form's id (required) * @param String $action the form's action (optional) * @param Array $attrs the form tag's attributes (optional) * @return String the opening HTML form tag */ public static function form_open($id, $action = NULL, $attrs = array()) { if (is_null($action) && ($app = YAWF::prop(Symbol::APP))) { $action = $app->get_path(); } $attrs['id'] = $id; $attrs['action'] = AppView::uri($action, array_key($attrs, 'prefix')); $attrs['method'] = array_key($attrs, 'method', 'post'); // by default return '<form ' . self::attrs($attrs) . ">\n"; }
/** * 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 }
/** * Return a request session object * * @return Request_session a request session object holding session values */ protected function session_object() { if ($session = YAWF::prop(Symbol::SESSION)) { return $session; } else { return YAWF::prop(Symbol::SESSION, new Request_session()); } }
/** * Create a new controller flash object * * @param Array $array an optional array of flash messages to display now */ public function __construct($array = NULL) { $this->session = YAWF::prop(Symbol::SESSION); $var = self::SESSION_VAR; if (!is_object($this->session->{$var})) { $this->session->{$var} = new Object(); } $this->flash_now = is_null($array) ? $this->session->{$var} : new Object($array); $this->session->{$var} = $this->flash_next = new Object(); }
<?php // Copyright (c) 2010 Guanoo, Inc. // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public License // as published by the Free Software Foundation; either version 3 // of the License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more details. // Run YAWF! chdir('../..'); ini_set('include_path', 'app:yawf:.'); require_once 'lib/utils.php'; YAWF::respond_to_web_request(); // End of index.php
/** * 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}"); } }
/** * Catch all undefined methods calls by calling YAWF::unknown * to throw an exception. * * @param String $name the name of the unknown method call * @param Array $args the arguments passed to the unknown method */ public function __call($name, $args) { YAWF::unknown($name, $args); }
/** * Copy the Ruby on Rails "t()" translate function to translate some text * * @param String $lookup the lookup string (see "configs/translate.yaml" file) * @param Array $replacements an optional array of text replacements to make * @return String the translated text after any replacements have been made */ function t($lookup, $replacements = array()) { static $is_loaded = FALSE; // for speed if (!$is_loaded) { load_tool('Translate'); } $is_loaded = TRUE; $app = YAWF::prop(Symbol::APP); return $app ? Translate::into($app->get_lang(), $lookup, $replacements) : NULL; }