/**
  * Register a controller to respond to an alias.
  *
  * @param $alias
  *   alias under the controller is registered.
  *
  * @param $klass
  *   the controller Class
  *
  * @param $file
  *   the file where the controller is defined. It will be required before a
  *   controller will be created.
  */
 public function register($alias, $klass, $file = null)
 {
     if (!is_null($file) && (!is_file($file) || !is_readable($file))) {
         No2_Logger::err(get_class($this) . "::register: can't read controller file: {$file}");
         $file = null;
     }
     $this->mapping[$alias] = ['controller' => $klass, 'file' => $file];
 }
Esempio n. 2
0
 /**
  * save the current user id in the session.
  *
  * @param $user
  *   a User object. The client is considered logged in as
  *   <code>$user</code> when this function return. if NULL, the current
  *   user is "logged out", meaning the session user id is unset.
  */
 public static function current_is($user)
 {
     if (is_null($user)) {
         static::$current_user = NULL;
         if (session_active()) {
             unset($_SESSION['uid']);
         }
     } else {
         if ($user instanceof static) {
             static::$current_user = $user;
             if (session_active()) {
                 $_SESSION['uid'] = $user->id;
             }
         } else {
             No2_Logger::err(get_called_class() . 'current_is: ' . 'Wrong user type: ' . get_class($user));
         }
     }
 }
 /**
  * Save the current Object. If the object was loaded by the database, an
  * UPDATE is performed and an INSERT otherwise. This method will call
  * is_valid() and ensure that true is returned before attempting to save
  * it.
  *
  * @param $do_validate
  *   Control if the object's state should be validated (calling and
  *   checking the return value of is_valid()). If $do_validate is true,
  *   is_valid() is called and false is returned by save() if validation
  *   failed. if $do_validate is false, then save() doesn't try to validate
  *   the object.
  *
  * @return
  *   false on error, true otherwise.
  */
 public function save($do_validate = true)
 {
     $is_new_record = $this->is_new_record();
     if ($do_validate && !$this->is_valid()) {
         return false;
     }
     // filter to only SET dirty properties.
     $properties = [];
     foreach ($this->__dirty_db_data as $prop => $dirty) {
         if ($dirty) {
             $properties[$prop] = $this->massage_for_storage($prop, $this->__db_data[$prop]);
         }
     }
     if (!$is_new_record && empty($properties)) {
         // We're doing an UPDATE with no new values. Don't commit anything
         // to the db and return true to notice the caller that somehow,
         // save was a success.
         $success = true;
         $rows = [];
     } else {
         $query = new No2_SQLQuery(get_class($this));
         $query->query_on($this->__db_profile);
         if ($is_new_record) {
             $rows = $query->insert1($properties);
         } else {
             $rows = $query->set($properties)->id($this->id)->update();
         }
         $success = !is_null($rows);
     }
     if (!$success) {
         /*
          * this is strange because we passed through is_valid(). There is
          * some inconsistancies between programmed validation and
          * database validation.
          */
         No2_Logger::err(get_class($this) . '::save: ' . 'database failed to save me:' . print_r($this, true));
     } else {
         // the database query was successful.
         // we're now a saved record, congrats.
         $this->__is_new_record = false;
         // load the row properties from the database response. This is
         // needed for database generated fields.
         foreach ($rows as $name => $value) {
             $this->{$name} = $value;
         }
         // reset the dirty properties, because the db now match them.
         $this->__dirty_db_data = [];
     }
     return $success;
 }
Esempio n. 4
0
        <?php 
        die;
    }
}
$view = $controller->view();
if (No2_HTTP::is_error($view->status()) && !$controller->can_render_errors()) {
    /*
     * The controller declined error handling, so we load the default error
     * controller to generate the response.
     */
    require_once APPDIR . '/controllers/error.class.php';
    $controller = new ErrorController($view->status());
    unset($view);
    goto invoke_it;
}
/* from this point, $controller and $view are set and valid. */
/*
 * Here we know the status code, log the request and render the requested ressource.
 */
No2_Logger::info("{$_SERVER['REMOTE_ADDR']} - {$_SERVER['REQUEST_METHOD']} - {$_SERVER['REQUEST_URI']} - {$view->status()}");
/* kindly ask the view to render the response */
try {
    /*
     * Don't try to buffer the view's output using something like ob_start(),
     * it will OOM PHP if the response is moderately big.
     */
    $view->render();
    die;
} catch (Exception $e) {
    No2_Logger::err('view rendering exception: ' . $e->getMessage());
}