[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

/gp-includes/ -> route.php (source)

   1  <?php
   2  /**
   3   * Base controller class
   4   */
   5  class GP_Route {
   6      
   7      var $api = false;
   8      
   9      var $errors = array();
  10      var $notices = array();
  11      var $request_running = false;
  12      var $template_path = null;
  13      
  14      var $fake_request = false;
  15      var $exited = false;
  16      var $exit_message;
  17      var $redirected = false;
  18      var $redirected_to = null;
  19      var $rendered_template = false;
  20      var $loaded_template = null;
  21      var $template_output = null;
  22      var $headers = array();
  23          
  24  	function __construct() {
  25          
  26      }
  27      
  28  	function die_with_error( $message, $status = 500 ) {
  29          $this->status_header( $status );
  30          $this->exit_( $message );
  31      }
  32      
  33  	function before_request() {
  34          do_action( 'before_request', $this->class_name, $this->last_method_called );
  35      }
  36  
  37  	function after_request() {
  38          // we can't unregister a shutdown function
  39          // this check prevents this method from being run twice
  40          if ( !$this->request_running ) return;
  41          // set errors and notices
  42          if ( !headers_sent() ) {
  43              $this->set_notices_and_errors();
  44          }
  45          do_action( 'after_request', $this->class_name, $this->last_method_called );
  46      }
  47  
  48      /**
  49       * Validates a thing and add its errors to the route's errors.
  50       * 
  51       * @param object $thing a GP_Thing instance to validate
  52       * @return bool whether the thing is valid
  53       */
  54  	function validate( $thing ) {
  55          $verdict = $thing->validate();
  56          $this->errors = array_merge( $this->errors, $thing->errors );
  57          return $verdict;
  58      }
  59      
  60      /**
  61       * Same as validate(), but redirects to $url if the thing isn't valid.
  62       * 
  63       * Note: this method calls $this->exit_() after the redirect and the code after it won't
  64       * be executed.
  65       * 
  66       * @param object $thing a GP_Thing instance to validate
  67       * @param string $url where to redirect if the thing doesn't validate
  68       * @return bool whether the thing is valid
  69       */
  70  	function invalid_and_redirect( $thing, $url = null ) {
  71          $valid = $this->validate( $thing );
  72          if ( !$valid ) {
  73              $this->redirect( $url );
  74              return true;
  75          }
  76          return false;
  77      }
  78      
  79  	function can( $action, $object_type = null, $object_id = null ) {
  80          return GP::$user->current()->can( $action, $object_type, $object_id );
  81      }
  82      
  83      /**
  84       * If the current user isn't allowed to do an action, redirect and exit the current request
  85       * 
  86       * @param string $action
  87       * @param`string $object_type
  88       * @param string $object_id
  89       * @param string $url    The URL to redirect. Default value: referrer or index page, if referrer is missing
  90       */
  91  	function cannot_and_redirect( $action, $object_type = null, $object_id = null, $url = null ) {
  92          $can = $this->can( $action, $object_type, $object_id );
  93          if ( !$can ) {
  94              $this->redirect_with_error( __('You are not allowed to do that!'), $url );
  95              return true;
  96          }
  97          return false;
  98      }
  99  
 100  	function can_or_forbidden( $action, $object_type = null, $object_id = null, $message = 'You are not allowed to do that!' ) {
 101          $can = $this->can( $action, $object_type, $object_id );
 102          if ( !$can ) {
 103              $this->die_with_error( $message, 403 );
 104          }
 105          return false;
 106      }
 107  
 108  	function logged_in_or_forbidden() {
 109          if ( !GP::$user->logged_in() ) {
 110              $this->die_with_error( 'Forbidden', 403 );
 111          }
 112      }
 113      
 114  	function redirect_with_error( $message, $url = null ) {
 115          $this->errors[] = $message;
 116          $this->redirect( $url );
 117      }
 118      
 119  	function redirect( $url = null ) {
 120          if ( $this->fake_request ) {
 121              $this->redirected = true;
 122              $this->redirected_to = $url;
 123              return;
 124          }
 125  
 126          $this->set_notices_and_errors();
 127          // TODO: do not redirect to projects, but to /
 128          // currently it goes to /projects, because / redirects too and the notice is gone
 129          if ( is_null( $url ) )  $url = isset( $_SERVER['HTTP_REFERER'] )? $_SERVER['HTTP_REFERER'] : gp_url( '/projects' );
 130          gp_redirect( $url );
 131          $this->tmpl( 'redirect', compact( 'url' ) );
 132      }
 133      
 134  	function headers_for_download( $filename ) {
 135          $this->header('Content-Description: File Transfer');
 136          $this->header('Pragma: public');
 137          $this->header('Expires: 0');
 138          $this->header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
 139          $this->header("Content-Disposition: attachment; filename=$filename");
 140          $this->header("Content-Type: application/octet-stream", true);
 141          $this->header('Connection: close');
 142      }
 143  
 144  	function set_notices_and_errors() {
 145          if ( $this->fake_request ) return;
 146          
 147          foreach( $this->notices as $notice ) {
 148              gp_notice_set( $notice );
 149          }
 150          $this->notices = array();
 151          
 152          foreach( $this->errors as $error ) {
 153              gp_notice_set( $error, 'error' );
 154          }
 155          $this->errors = array();
 156      }
 157      
 158      /**
 159       * Loads a template.
 160       * 
 161       * @param string $template template name to load
 162       * @param array $args Associative array with arguements, which will be exported in the template PHP file
 163       * @param bool|string $honor_api If this is true or 'api' and the route is processing an API request
 164       *         the template name will be suffixed with .api. The actual file loaded will be template.api.php
 165       */
 166  	function tmpl( $template, $args = array(), $honor_api = true ) {
 167          if ( $this->fake_request ) {
 168              $this->rendered_template = true;
 169              $this->loaded_template = $template;
 170          }
 171          $this->set_notices_and_errors();
 172          if ( $this->api && $honor_api !== false && 'no-api' !== $honor_api ) {
 173              $template = $template.'.api';
 174              $this->header('Content-Type: application/json');
 175          } else {
 176              $this->header('Content-Type: text/html; charset=utf-8');
 177          }
 178          if ( $this->fake_request ) {
 179              $this->template_output = gp_tmpl_get_output( $template, $args, $this->template_path );
 180              return true;
 181          }
 182          
 183          return gp_tmpl_load( $template, $args, $this->template_path );
 184      }
 185      
 186  	function tmpl_404( $args ) {
 187          $this->tmpl( '404', $args + array('title' => __('Not Found'), 'http_status' => 404 ) );
 188      }
 189      
 190  	function exit_( $message = 0 ) {
 191          if ( $this->fake_request ) {
 192              $this->exited = true;
 193              $this->exit_message = $message;
 194          }
 195          exit( $message );
 196      }
 197      
 198  	function header( $string ) {
 199          if ( $this->fake_request ) {
 200              list( $header, $value ) = explode( ':', $string, 2 );
 201              $this->headers[$header] = $value;
 202          } else {
 203              header( $string );
 204          }        
 205      }
 206      
 207  	function status_header( $status ) {
 208          if ( $this->fake_request ) {
 209              $this->http_status = $status;
 210              return;
 211          }
 212          return status_header( $status );
 213      }
 214  }


Generated: Thu May 24 03:59:35 2012 Hosted by follow the white rabbit.