[ Index ]

PHP Cross Reference of Akelos Framework

title

Body

[close]

/ -> AkRequest.php (source)

   1  <?php
   2  /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
   3  
   4  // +----------------------------------------------------------------------+
   5  // | Akelos Framework - http://www.akelos.org                             |
   6  // +----------------------------------------------------------------------+
   7  // | Copyright (c) 2002-2006, Akelos Media, S.L.  & Bermi Ferrer Martinez |
   8  // | Released under the GNU Lesser General Public License, see LICENSE.txt|
   9  // +----------------------------------------------------------------------+
  10  
  11  /**
  12   * @package ActionController
  13   * @subpackage Request
  14   * @author Bermi Ferrer <bermi a.t akelos c.om>
  15   * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
  16   * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
  17   */
  18  
  19  if(!defined('AK_DEFAULT_CONTROLLER')){
  20      define('AK_DEFAULT_CONTROLLER', 'page');
  21  }
  22  if(!defined('AK_DEFAULT_ACTION')){
  23      define('AK_DEFAULT_ACTION', 'index');
  24  }
  25  
  26  defined('AK_HIGH_LOAD_MODE') ? null : define('AK_HIGH_LOAD_MODE', false);
  27  defined('AK_AUTOMATIC_DB_CONNECTION') ? null : define('AK_AUTOMATIC_DB_CONNECTION', !AK_HIGH_LOAD_MODE);
  28  defined('AK_AUTOMATIC_SESSION_START') ? null : define('AK_AUTOMATIC_SESSION_START', !AK_HIGH_LOAD_MODE);
  29  
  30  // IIS does not provide a valid REQUEST_URI so we need to guess it from the script name + query string
  31  $_SERVER['REQUEST_URI'] = (isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $_SERVER['SCRIPT_NAME'].(( isset($_SERVER['QUERY_STRING']) ? '?' . $_SERVER['QUERY_STRING'] : '')));
  32  
  33  require_once (AK_LIB_DIR.DS.'AkRequestMimeType.php');
  34  /**
  35  * Class that handles incoming request.
  36  * 
  37  * The Request Object handles user request (CLI, GET, POST, session or
  38  * cookie requests), transforms it and sets it up for the
  39  * ApplicationController class, who takes control of the data
  40  * flow.
  41  * 
  42  * @author Bermi Ferrer <bermi@akelos.com>
  43  * @copyright Copyright (c) 2002-2005, Akelos Media, S.L. http://www.akelos.org
  44  * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
  45  */
  46  class AkRequest extends AkObject
  47  {
  48  
  49      /**
  50      * Array containing the request parameters.
  51      * 
  52      * This property stores the parameters parsed from the
  53      * parseRequest() method. This array is used by addParams()
  54      * method.
  55      * 
  56      * @access private
  57      * @var array $_request
  58      */
  59      var $_request = array();
  60  
  61      var $_init_check = false;
  62      var $__internationalization_support_enabled = false;
  63  
  64      var $action = AK_DEFAULT_ACTION;
  65      var $controller = AK_DEFAULT_CONTROLLER;
  66      var $view;
  67  
  68      /**
  69      * Holds information about current environment. Initially a reference to $_SERVER
  70      *
  71      * @var array
  72      */
  73      var $env = array();
  74      
  75      var $mime_types = array( 
  76                  'text/html'                => 'html', 
  77                  'application/xhtml+xml'    => 'html', 
  78                  'application/xml'          => 'xml', 
  79                  'text/xml'                 => 'xml', 
  80                  'text/javascript'          => 'js', 
  81                  'application/javascript'   => 'js', 
  82                  'application/x-javascript' => 'js', 
  83                  'application/json'         => 'json',
  84                  'text/x-json'              => 'json', 
  85                  'application/rss+xml'      => 'rss', 
  86                  'application/atom+xml'     => 'atom', 
  87                  '*/*'                      => 'html', 
  88                  //'application/x-www-form-urlencoded' => 'www-form', 
  89                  //'application/x-www-form-urlencoded' => 'www-form',
  90                  'default'                  => 'html', 
  91              );
  92              
  93      var $_format;
  94      /**
  95      * String parse method.
  96      * 
  97      * This method gets a petition as parameter, using the "Ruby
  98      * on Rails" request format (see prettyURL in RoR documentation). The format is:
  99      * file.php?ak=/controller/action/id&paramN=valueN
 100      * 
 101      * This method requires for a previous execution of the _mergeRequest() method, 
 102      * in order to merge all the request all i one array.
 103      *
 104      * This method expands dynamically the class Request, adding a public property for
 105      * every parameter sent in the request.
 106      *
 107      * 
 108      * @access public
 109      * @return array
 110      */
 111      function _parseAkRequestString($ak_request_string, $pattern = '/')
 112      {
 113          $result = array();
 114          $ak_request = trim($ak_request_string,$pattern);
 115          if(strstr($ak_request,$pattern)){
 116              $result = explode($pattern,$ak_request);
 117          }
 118          return $result;
 119      }
 120  
 121  
 122      function __construct ()
 123      {
 124          $this->init();
 125          $this->getFormat();
 126          
 127      }
 128  
 129      /**
 130      * Initialization method.
 131      * 
 132      * Initialization method. Use this via the class constructor.
 133      * 
 134      * @access public
 135      * @uses parseRequest
 136      * @return void 
 137      */
 138      function init()
 139      {
 140          if(!$this->_init_check){
 141              $this->env =& $_SERVER;
 142              $this->_fixGpcMagic();
 143              $this->_urlDecode();
 144  
 145              $this->_mergeRequest();
 146  
 147              if(is_array($this->_request)){
 148                  foreach ($this->_request as $k=>$v){
 149                      $this->_addParam($k, $v);
 150                  }
 151              }
 152  
 153              $this->_init_check = true;
 154          }
 155  
 156          if(defined('AK_LOG_EVENTS') && AK_LOG_EVENTS){
 157              $this->Logger =& Ak::getLogger();
 158              $this->Logger->message($this->Logger->formatText('Request','green').' from '.$this->getRemoteIp(), $this->getParams());
 159          }
 160      }
 161  
 162      function get($var_name)
 163      {
 164          return $this->_request[$var_name];
 165      }
 166  
 167      function getParams()
 168      {
 169          return array_merge(array('controller'=>$this->controller,'action'=>$this->action),$this->_request);
 170      }
 171  
 172      function getAction()
 173      {
 174          return $this->action;
 175      }
 176  
 177      function getController()
 178      {
 179          return $this->controller;
 180      }
 181  
 182      function reset()
 183      {
 184          $this->_request = array();
 185          $this->_init_check = false;
 186      }
 187  
 188      function set($variable, $value)
 189      {
 190          $this->_addParam($variable, $value);
 191      }
 192  
 193  
 194      function checkForRoutedRequests(&$Router)
 195      {
 196          $ak_request = isset($this->_request['ak']) ? '/'.trim($this->_request['ak'],'/').'/' : '/';
 197  
 198          if($found = $Router->toParams($ak_request)){
 199              if(!isset($found['controller'])){
 200                  trigger_error(Ak::t('No controller was specified.'), E_USER_WARNING);
 201              }
 202              if(!isset($found['action'])){
 203                  trigger_error(Ak::t('No action was specified.'), E_USER_WARNING);
 204              }
 205  
 206              if(isset($found['controller'])){
 207                  if($this->_addParam('controller',$found['controller'])){
 208                      $this->controller = $this->_request['controller'] = $found['controller'];
 209                  }
 210              }
 211              if(isset($found['action'])){
 212                  if($this->_addParam('action',$found['action'])){
 213                      $this->action = $this->_request['action'] = $found['action'];
 214                  }
 215              }
 216              if(isset($found['module'])){
 217                  if($this->_addParam('module',$found['module'])){
 218                      $this->module = $this->_request['module'] = $found['module'];
 219                  }
 220              }
 221  
 222              foreach ($found as $k=>$v){
 223                  if($this->_addParam($k,$v)){
 224                      $this->_request[$k] = $v;
 225                  }
 226              }
 227          }
 228      }
 229  
 230  
 231      function isValidControllerName($controller_name)
 232      {
 233          return $this->_validateTechName($controller_name);
 234      }
 235  
 236      function isValidActionName($action_name)
 237      {
 238          return $this->_validateTechName($action_name);
 239      }
 240  
 241      function isValidModuleName($module_name)
 242      {
 243          return preg_match('/^[A-Za-z]{1,}[A-Za-z0-9_\/]*$/', $module_name);
 244      }
 245  
 246  
 247  
 248      /**
 249      * Returns both GET and POST parameters in a single array.
 250      */
 251      function getParameters()
 252      {
 253          if(empty($this->parameters)){
 254              $this->parameters = $this->getParams();
 255          }
 256          return $this->parameters;
 257      }
 258  
 259      function setPathParameters($parameters)
 260      {
 261          $this->_path_parameters = $parameters;
 262      }
 263  
 264      function getPathParameters()
 265      {
 266          return empty($this->_path_parameters) ? array() : $this->_path_parameters;
 267      }
 268  
 269      function getUrlParams()
 270      {
 271          return $_GET;
 272      }
 273  
 274      /**
 275      * Must be implemented in the concrete request
 276      */
 277      function getQueryParameters ()
 278      {
 279      }
 280      function getRequestParameters ()
 281      {
 282      }
 283  
 284      /**
 285       * Returns the path minus the web server relative installation directory. This method returns null unless the web server is apache.
 286       */
 287      function getRelativeUrlRoot()
 288      {
 289          return str_replace('/index.php','', @$this->env['PHP_SELF']);
 290      }
 291  
 292      /**
 293       * Returns the locale identifier of current URL
 294       */
 295      function getLocaleFromUrl()
 296      {
 297          $locale = Ak::get_url_locale();
 298          if(strstr(AK_CURRENT_URL,AK_SITE_URL.'/'.$locale)){
 299              return $locale;
 300          }
 301          return '';
 302      }
 303  
 304      /**
 305      * Returns the HTTP request method as a lowercase symbol ('get, for example)
 306      */
 307      function getMethod()
 308      {
 309          return strtolower(isset($this->env['REQUEST_METHOD'])?$this->env['REQUEST_METHOD']:'get');
 310      }
 311  
 312      /**
 313      * Is this a GET request?  Equivalent to $Request->getMethod() == 'get'
 314      */
 315      function isGet()
 316      {
 317          return $this->getMethod() == 'get';
 318      }
 319  
 320      /**
 321      * Is this a POST request?  Equivalent to $Request->getMethod() == 'post'
 322      */
 323      function isPost()
 324      {
 325          return $this->getMethod() == 'post';
 326      }
 327  
 328      /**
 329      * Is this a PUT request?  Equivalent to $Request->getMethod() == 'put'
 330      */
 331      function isPut()
 332      {
 333          return isset($this->env['REQUEST_METHOD']) ? $this->getMethod() == 'put' : false;
 334      }
 335  
 336      /**
 337      * Is this a DELETE request?  Equivalent to $Request->getMethod() == 'delete'
 338      */
 339      function isDelete()
 340      {
 341          return $this->getMethod() == 'delete';
 342      }
 343  
 344      /**
 345      * Is this a HEAD request?  Equivalent to $Request->getMethod() == 'head'
 346      */
 347      function isHead()
 348      {
 349          return $this->getMethod() == 'head';
 350      }
 351  
 352  
 353  
 354      /**
 355      * Determine originating IP address.  REMOTE_ADDR is the standard
 356      * but will fail if( the user is behind a proxy.  HTTP_CLIENT_IP and/or
 357      * HTTP_X_FORWARDED_FOR are set by proxies so check for these before
 358      * falling back to REMOTE_ADDR.  HTTP_X_FORWARDED_FOR may be a comma-
 359      * delimited list in the case of multiple chained proxies; the first is
 360      * the originating IP.
 361      */
 362      function getRemoteIp()
 363      {
 364          if(!empty($this->env['HTTP_CLIENT_IP'])){
 365              return $this->env['HTTP_CLIENT_IP'];
 366          }
 367          if(!empty($this->env['HTTP_X_FORWARDED_FOR'])){
 368              foreach ((strstr($this->env['HTTP_X_FORWARDED_FOR'],',') ? split(',',$this->env['HTTP_X_FORWARDED_FOR']) : array($this->env['HTTP_X_FORWARDED_FOR'])) as $remote_ip){
 369                  if($remote_ip == 'unknown' ||
 370                  preg_match('/^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/', $remote_ip) ||
 371                  preg_match('/^([0-9a-fA-F]{4}|0)(\:([0-9a-fA-F]{4}|0)){7}$/', $remote_ip)
 372                  ){
 373                      return $remote_ip;
 374                  }
 375              }
 376          }
 377          return empty($this->env['REMOTE_ADDR']) ? '' : $this->env['REMOTE_ADDR'];
 378  
 379      }
 380  
 381      /**
 382      * Returns the domain part of a host, such as akelos.com in 'www.akelos.com'. You can specify
 383      * a different <tt>tld_length</tt>, such as 2 to catch akelos.co.uk in 'www.akelos.co.uk'.
 384      */
 385      function getDomain($tld_length = 1)
 386      {
 387          return preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/',$this->getHost()) ?
 388          null :
 389          join('.',array_slice(explode('.',$this->getHost()),(1 + $tld_length)*-1));
 390      }
 391  
 392      /**
 393      * Returns all the subdomains as an array, so ['dev', 'www'] would be returned for 'dev.www.akelos.com'.
 394      * You can specify a different <tt>tld_length</tt>, such as 2 to catch ['www'] instead of ['www', 'akelos']
 395      * in 'www.akelos.co.uk'.
 396      */
 397      function getSubdomains($tld_length = 1)
 398      {
 399          return preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/',$this->getHost()) ||
 400          !strstr($this->getHost(),'.') ? array() : (array)array_slice(explode('.',$this->getHost()),0,(1 + $tld_length)*-1);
 401      }
 402  
 403  
 404      /**
 405      * Returns the request URI correctly
 406      */
 407      function getRequestUri()
 408      {
 409          return $this->getProtocol().$this->getHostWithPort();
 410      }
 411  
 412      /**
 413      * Return 'https://' if( this is an SSL request and 'http://' otherwise.
 414      */
 415      function getProtocol()
 416      {
 417          return $this->isSsl() ? 'https://' : 'http://';
 418      }
 419  
 420      /**
 421      * Is this an SSL request?
 422      */
 423      function isSsl()
 424      {
 425          return isset($this->env['HTTPS']) && ($this->env['HTTPS'] === true || $this->env['HTTPS'] == 'on');
 426      }
 427  
 428      /**
 429      * Returns the interpreted path to requested resource
 430      */
 431      function getPath()
 432      {
 433          return strstr($this->env['REQUEST_URI'],'?') ? substr($this->env['REQUEST_URI'],0,strpos($this->env['REQUEST_URI'],'?')) : $this->env['REQUEST_URI'];
 434      }
 435  
 436      /**
 437      * Returns the port number of this request as an integer.
 438      */
 439      function getPort()
 440      {
 441          $this->port_as_int = AK_WEB_REQUEST ? AK_SERVER_PORT : 80;
 442          return $this->port_as_int;
 443      }
 444  
 445      /**
 446      * Returns the standard port number for this request's protocol
 447      */
 448      function getStandardPort()
 449      {
 450          return $this->isSsl() ? 443 : 80;
 451      }
 452  
 453      /**
 454      * Returns a port suffix like ':8080' if( the port number of this request
 455      * is not the default HTTP port 80 or HTTPS port 443.
 456      */
 457      function getPortString()
 458      {
 459          $port = $this->getPort();
 460          return $port == $this->getStandardPort() ? '' : ($port ? ':'.$this->getPort() : '');
 461      }
 462  
 463      /**
 464      * Returns a host:port string for this request, such as example.com or
 465      * example.com:8080.
 466      */
 467      function getHostWithPort()
 468      {
 469          return $this->getHost() . $this->getPortString();
 470      }
 471  
 472  
 473      function getHost()
 474      {
 475          if(!empty($this->_host)){
 476              return $this->_host;
 477          }
 478          return AK_WEB_REQUEST ? $this->env['SERVER_NAME'] : 'localhost';
 479      }
 480  
 481      function &getSession()
 482      {
 483          return $_SESSION;
 484      }
 485  
 486      function resetSession()
 487      {
 488          $_SESSION = array();
 489      }
 490  
 491      function &getCookies()
 492      {
 493          return $_COOKIE;
 494      }
 495  
 496  
 497      function &getEnv()
 498      {
 499          return $this->env;
 500      }
 501  
 502  
 503      function getServerSoftware()
 504      {
 505          if(!empty($this->env['SERVER_SOFTWARE'])){
 506              if(preg_match('/^([a-zA-Z]+)/', $this->env['SERVER_SOFTWARE'],$match)){
 507                  return strtolower($match[0]);
 508              }
 509          }
 510          return '';
 511      }
 512  
 513  
 514      /**
 515      * Returns true if the request's 'X-Requested-With' header contains
 516      * 'XMLHttpRequest'. (The Prototype Javascript library sends this header with
 517      * every Ajax request.)
 518      */
 519      function isXmlHttpRequest()
 520      {
 521          return !empty($this->env['HTTP_X_REQUESTED_WITH']) && strstr(strtolower($this->env['HTTP_X_REQUESTED_WITH']),'xmlhttprequest');
 522      }
 523      function xhr()
 524      {
 525          return $this->isXmlHttpRequest();
 526      }
 527  
 528      function isAjax()
 529      {
 530          return $this->isXmlHttpRequest();
 531      }
 532  
 533  
 534      /**
 535       * Receive the raw post data.
 536       * This is useful for services such as REST, XMLRPC and SOAP
 537       * which communicate over HTTP POST but don't use the traditional parameter format.
 538       */
 539      function getRawPost()
 540      {
 541          return empty($_ENV['RAW_POST_DATA']) ? '' : $_ENV['RAW_POST_DATA'];
 542      }
 543  
 544  
 545      function _validateTechName($name)
 546      {
 547          return preg_match('/^[A-Za-z]{1,}[A-Za-z0-9_]*$/',$name);
 548      }
 549  
 550  
 551  
 552      // {{{ _mergeRequest()
 553  
 554      /**
 555      * Populates $this->_request attribute with incoming request in the following precedence:
 556      *
 557      * $_SESSION['request'] <- This will override options provided by previous methods
 558      * $_COOKIE
 559      * $_POST
 560      * $_GET 
 561      * Command line params
 562      * 
 563      * @access public
 564      * @return void Void returned. Modifies the private property "
 565      */
 566      function _mergeRequest()
 567      {
 568          $this->_request = array();
 569  
 570          $session_params = isset($_SESSION['request']) ? $_SESSION['request'] : null;
 571          $command_line_params = !empty($_REQUEST)  ? $_REQUEST : null;
 572  
 573          $requests = array($command_line_params, $_GET, array_merge_recursive($_POST, $this->getPutParams(), $this->_getNormalizedFilesArray()), $_COOKIE, $session_params);
 574  
 575          foreach ($requests as $request){
 576              $this->_request = (!is_null($request) && is_array($request)) ?
 577              array_merge($this->_request,$request) : $this->_request;
 578          }
 579      }
 580  
 581      // }}}
 582  
 583      function _getNormalizedFilesArray($params = null, $first_call = true)
 584      {
 585          $params = $first_call ? $_FILES : $params;
 586          $result = array();
 587  
 588          $params = array_diff($params,array(''));
 589          if(!empty($params) && is_array($params)){
 590              foreach ($params as $name=>$details){
 591  
 592                  if(is_array($details) && !empty($details['name']) &&  !empty($details['tmp_name']) &&  !empty($details['size'])){
 593                      if(is_array($details['tmp_name'])){
 594                          foreach ($details['tmp_name'] as $item=>$item_details){
 595                              if(is_array($item_details)){
 596                                  foreach (array_keys($item_details) as $k){
 597                                      if(UPLOAD_ERR_NO_FILE != $details['error'][$item][$k]){
 598                                          $result[$name][$item][$k] = array(
 599                                          'name'=>$details['name'][$item][$k],
 600                                          'tmp_name'=>$details['tmp_name'][$item][$k],
 601                                          'size'=>$details['size'][$item][$k],
 602                                          'type'=>$details['type'][$item][$k],
 603                                          'error'=>$details['error'][$item][$k],
 604                                          );
 605                                      }
 606                                  }
 607                              }else{
 608                                  if(UPLOAD_ERR_NO_FILE != $details['error'][$item]){
 609                                      $result[$name][$item] = array(
 610                                      'name'=>$details['name'][$item],
 611                                      'tmp_name'=>$details['tmp_name'][$item],
 612                                      'size'=>$details['size'][$item],
 613                                      'type'=>$details['type'][$item],
 614                                      'error'=>$details['error'][$item],
 615                                      );
 616                                  }
 617                              }
 618                          }
 619                      }elseif ($first_call){
 620                          $result[$name] = $details;
 621                      }else{
 622                          $result[$name][] = $details;
 623                      }
 624                  }elseif(is_array($details)){
 625                      $_nested = $this->_getNormalizedFilesArray($details, false);
 626  
 627                      if(!empty($_nested)){
 628                          $result = array_merge(array($name=>$_nested), $result);
 629                      }
 630                  }
 631              }
 632          }
 633  
 634          return $result;
 635      }
 636  
 637      // {{{ _addParams()
 638  
 639      /**
 640      * Builds (i.e., "expands") the Request class for accessing
 641      * the request parameters as public properties.
 642      * For example, when the requests is "ak=/controller/action/id&parameter=value", 
 643      * once parsed, you can access the parameters of the request just like
 644      * an object, e.g.:
 645      *
 646      *   $value_to_get = $request->parameter
 647      * 
 648      * @access private
 649      * @return void 
 650      */
 651      function _addParam($variable, $value)
 652      {
 653          if($variable[0] != '_'){
 654              if( ( $variable == 'action' && !$this->isValidActionName($value)) ||
 655              ( $variable == 'controller' && !$this->isValidControllerName($value)) ||
 656              ( $variable == 'module' && !$this->isValidModuleName($value))
 657              ){
 658                  return false;
 659              }
 660              $this->$variable = $value;
 661              return true;
 662          }
 663          return false;
 664      }
 665  
 666      // }}}
 667  
 668  
 669      /**
 670      * Correct double-escaping problems caused by "magic quotes" in some PHP
 671      * installations.
 672      */
 673      function _fixGpcMagic()
 674      {
 675          if(!defined('AK_GPC_MAGIC_FIXED')){
 676              if (get_magic_quotes_gpc()) {
 677                  array_walk($_GET, array('AkRequest', '_fixGpc'));
 678                  array_walk($_POST, array('AkRequest', '_fixGpc'));
 679                  array_walk($_COOKIE, array('AkRequest', '_fixGpc'));
 680              }
 681              define('AK_GPC_MAGIC_FIXED',true);
 682          }
 683      }
 684  
 685      function _fixGpc(&$item)
 686      {
 687          if (is_array($item)) {
 688              array_walk($item, array('AkRequest', '_fixGpc'));
 689          }else {
 690              $item = stripslashes($item);
 691          }
 692      }
 693  
 694  
 695      function _urlDecode()
 696      {
 697          if(!defined('AK_URL_DECODED')){
 698              array_walk($_GET, array('AkRequest', '_performUrlDecode'));
 699              define('AK_URL_DECODED',true);
 700          }
 701      }
 702  
 703      function _performUrlDecode(&$item)
 704      {
 705          if (is_array($item)) {
 706              array_walk($item, array('AkRequest', '_performUrlDecode'));
 707          }else {
 708              $item = urldecode($item);
 709          }
 710      }
 711      function getAccepts()
 712      {
 713          $accept_header = isset($this->env['HTTP_ACCEPT'])?$this->env['HTTP_ACCEPT']:'';
 714          $accepts = array();
 715          foreach (explode(',',$accept_header) as $index=>$acceptable){ 
 716                   $mime_struct = $this->_parseMimeType($acceptable); 
 717                   if (empty($mime_struct['q'])) $mime_struct['q'] = '1.0'; 
 718                    
 719                   //we need the original index inside this structure  
 720                   //because usort happily rearranges the array on equality 
 721                   //therefore we first compare the 'q' and then 'i' 
 722                   $mime_struct['i'] = $index; 
 723                   $accepts[] = $mime_struct; 
 724               } 
 725               usort($accepts,array(&$this,'_sortAcceptHeader')); 
 726                
 727               //we throw away the old index 
 728               foreach ($accepts as $array){ 
 729                   unset($array['i']); 
 730               } 
 731               return $accepts; 
 732      }
 733      function setFormat($format)
 734      {
 735          $this->_format = $format;
 736      }
 737      
 738      function getFormat()
 739      {
 740  
 741          
 742          if (isset($this->_format)) {
 743              return $this->_format;
 744          } else if (isset($this->_request['format'])) {
 745              $this->_format = $this->_request['format'];
 746          } else {
 747              list($format, $requestPath) = AkRequestMimeType::getFormat(@$this->_request['ak']);
 748              
 749              $this->_format = $format;
 750              $this->_request['format'] = $format;
 751              if ($requestPath!=null) {
 752                  $this->_request['ak'] = $requestPath;
 753              }
 754          }
 755          return $this->_format;
 756      }
 757      
 758  
 759      // {{{ recognize()
 760  
 761      /**
 762      * Recognizes a Request and returns the responsible controller instance
 763      * 
 764      * @return AkActionController
 765      */
 766      function &recognize($Map = null)
 767      {
 768          AK_ENVIRONMENT != 'setup' ? $this->_connectToDatabase() : null;
 769          $this->_startSession();
 770          $this->_enableInternationalizationSupport();
 771          $this->_mapRoutes($Map);
 772  
 773          $params = $this->getParams();
 774  
 775          $module_path = $module_class_peffix = '';
 776          if(!empty($params['module'])){
 777              $module_path = trim(str_replace(array('/','\\'), DS, Ak::sanitize_include($params['module'], 'high')), DS).DS;
 778              $module_shared_model = AK_CONTROLLERS_DIR.DS.trim($module_path,DS).'_controller.php';
 779              $module_class_peffix = str_replace(' ','_',AkInflector::titleize(str_replace(DS,' ', trim($module_path, DS)))).'_';
 780          }
 781  
 782          $controller_file_name = AkInflector::underscore($params['controller']).'_controller.php';
 783          $controller_class_name = $module_class_peffix.AkInflector::camelize($params['controller']).'Controller';
 784          $controller_path = AK_CONTROLLERS_DIR.DS.$module_path.$controller_file_name;
 785          include_once(AK_APP_DIR.DS.'application_controller.php');
 786  
 787          if(!empty($module_path) && file_exists($module_shared_model)){
 788              include_once($module_shared_model);
 789          }
 790  
 791          if(!is_file($controller_path) || !include_once($controller_path)){
 792              defined('AK_LOG_EVENTS') && AK_LOG_EVENTS && $this->Logger->error('Controller '.$controller_path.' not found.');
 793              if(AK_ENVIRONMENT == 'development'){
 794                  trigger_error(Ak::t('Could not find the file /app/controllers/<i>%controller_file_name</i> for '.
 795                  'the controller %controller_class_name',
 796                  array('%controller_file_name'=> $controller_file_name,
 797                  '%controller_class_name' => $controller_class_name)), E_USER_ERROR);
 798              }elseif(@include(AK_PUBLIC_DIR.DS.'404.php')){
 799                  exit;
 800              }else{
 801                  header("HTTP/1.1 404 Not Found");
 802                  die('404 Not found');
 803              }
 804          }
 805          if(!class_exists($controller_class_name)){
 806              defined('AK_LOG_EVENTS') && AK_LOG_EVENTS && $this->Logger->error('Controller '.$controller_path.' does not implement '.$controller_class_name.' class.');
 807              if(AK_ENVIRONMENT == 'development'){
 808                  trigger_error(Ak::t('Controller <i>%controller_name</i> does not exist',
 809                  array('%controller_name' => $controller_class_name)), E_USER_ERROR);
 810              }elseif(@include(AK_PUBLIC_DIR.DS.'405.php')){
 811                  exit;
 812              }else{
 813                  header("HTTP/1.1 405 Method Not Allowed");
 814                  die('405 Method Not Allowed');
 815              }
 816          }
 817          $Controller =& new $controller_class_name(array('controller'=>true));
 818          $Controller->_module_path = $module_path;
 819          isset($_SESSION) ? $Controller->session =& $_SESSION : null;
 820          return $Controller;
 821  
 822      }
 823  
 824      // }}}
 825  
 826      function _enableInternationalizationSupport()
 827      {
 828          if(AK_AVAILABLE_LOCALES != 'en'){
 829              require_once (AK_LIB_DIR.DS.'AkLocaleManager.php');
 830  
 831              $LocaleManager = new AkLocaleManager();
 832              $LocaleManager->init();
 833              $LocaleManager->initApplicationInternationalization($this);
 834              $this->__internationalization_support_enabled = true;
 835          }
 836      }
 837  
 838      function _mapRoutes($Map = null)
 839      {
 840          require_once (AK_LIB_DIR.DS.'AkRouter.php');
 841  
 842          if(AK_ENVIRONMENT != 'setup' && is_file(AK_ROUTES_MAPPING_FILE)){
 843              if(empty($Map)){
 844                  $Map =& AkRouter();
 845              }
 846              include(AK_ROUTES_MAPPING_FILE);
 847              // Set this routes for being used via Ak::toUrl
 848              Ak::toUrl($Map,true);
 849              $this->checkForRoutedRequests($Map);
 850          }
 851      }
 852  
 853  
 854      function _connectToDatabase()
 855      {
 856          if(AK_AUTOMATIC_DB_CONNECTION){
 857              Ak::db(AK_DEFAULT_DATABASE_PROFILE);
 858          }
 859      }
 860  
 861      function _startSession()
 862      {
 863          if(AK_AUTOMATIC_SESSION_START){
 864              if(!isset($_SESSION)){
 865                  require_once (AK_LIB_DIR.DS.'AkSession.php');
 866                  $SessionHandler = &AkSession::initHandler();
 867                  @session_start();
 868              }
 869          }
 870      }
 871  
 872      function getPutParams()
 873      {
 874          if(!isset($this->put) && $this->isPut() && $data = $this->getPutRequestData()){
 875              $this->put = array();
 876              parse_str(urldecode($data), $this->put);
 877          }
 878          return isset($this->put) ? $this->put : array();
 879      }
 880  
 881      function getPutRequestData()
 882      {
 883          if(!empty($_SERVER['CONTENT_LENGTH'])){
 884              $putdata = fopen('php://input', 'r');
 885              $result = fread($putdata, $_SERVER['CONTENT_LENGTH']);
 886              fclose($putdata);
 887              return $result;
 888          }else{
 889              return false;
 890          }
 891      }
 892  }
 893  
 894  function &AkRequest()
 895  {
 896      $null = null;
 897      $AkRequest =& Ak::singleton('AkRequest', $null);
 898      return $AkRequest;
 899  }
 900  
 901  ?>


Generated: Mon Oct 27 12:43:49 2008 Cross-referenced by PHPXref 0.6