[ Index ]
 

Code source de Dotclear 2.0-beta6

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/inc/clearbricks/url.handler/ -> class.url.handler.php (source)

   1  <?php
   2  # ***** BEGIN LICENSE BLOCK *****
   3  # This file is part of Clearbricks.
   4  # Copyright (c) 2006 Olivier Meunier and contributors.
   5  # All rights reserved.
   6  #
   7  # Clearbricks is free software; you can redistribute it and/or modify
   8  # it under the terms of the GNU General Public License as published by
   9  # the Free Software Foundation; either version 2 of the License, or
  10  # (at your option) any later version.
  11  # 
  12  # Clearbricks is distributed in the hope that it will be useful,
  13  # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15  # GNU General Public License for more details.
  16  # 
  17  # You should have received a copy of the GNU General Public License
  18  # along with Clearbricks; if not, write to the Free Software
  19  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  20  #
  21  # ***** END LICENSE BLOCK *****
  22  
  23  class urlHandler
  24  {
  25      protected $types = array();
  26      protected $default_handler;
  27      public $mode;
  28      public $type = 'default';
  29      
  30  	public function __construct($mode='path_info')
  31      {
  32          $this->mode = $mode;
  33      }
  34      
  35  	public function register($type,$url,$representation,$handler)
  36      {
  37          $this->types[$type] = array(
  38              'url' => $url,
  39              'representation' => $representation,
  40              'handler' => $handler
  41          );
  42      }
  43      
  44  	public function registerDefault($handler)
  45      {
  46          $this->default_handler = $handler;
  47      }
  48      
  49  	public function unregister($type)
  50      {
  51          if (isset($this->types[$type])) {
  52              unset($this->types[$type]);
  53          }
  54      }
  55      
  56  	public function getBase($type)
  57      {
  58          if (isset($this->types[$type])) {
  59              return $this->types[$type]['url'];
  60          }
  61          return null;
  62      }
  63      
  64  	public function getDocument()
  65      {
  66          $type = $args = '';
  67          
  68          if ($this->mode == 'path_info')
  69          {
  70              $part = substr($_SERVER['PATH_INFO'],1);
  71          }
  72          else
  73          {
  74              $part = '';
  75              
  76              $qs = $this->parseQueryString();
  77              
  78              # Recreates some _GET and _REQUEST pairs
  79              if (!empty($qs))
  80              {
  81                  foreach ($_GET as $k => $v) {
  82                      if (isset($_REQUEST[$k])) {
  83                          unset($_REQUEST[$k]);
  84                      }
  85                  }
  86                  $_GET = $qs;
  87                  $_REQUEST = array_merge($qs,$_REQUEST);
  88                  
  89                  list($k,$v) = each($qs);
  90                  if ($v === null) {
  91                      $part = $k;
  92                      unset($_GET[$k]);
  93                      unset($_REQUEST[$k]);
  94                  }
  95              }
  96          }
  97          
  98          $_SERVER['URL_REQUEST_PART'] = $part;
  99          
 100          $this->getArgs($part,$type,$args);
 101          
 102          if (!$type)
 103          {
 104              $this->type = 'default';
 105              $this->callDefaultHandler($args);
 106          }
 107          else
 108          {
 109              $this->type = $type;
 110              $this->callHandler($type,$args);
 111          }
 112      }
 113      
 114  	protected function getArgs($part,&$type,&$args)
 115      {
 116          if ($part == '') {
 117              $type = null;
 118              $args = null;
 119              return;
 120          }
 121          
 122          $this->sortTypes();
 123          
 124          foreach ($this->types as $k => $v)
 125          {
 126              $repr = $v['representation'];
 127              if ($repr == $part) {
 128                  $type = $k;
 129                  $args = null;
 130                  return;
 131              }
 132              elseif (preg_match('#'.$repr.'#',$part,$m))
 133              {
 134                  $type = $k;
 135                  $args = isset($m[1]) ? $m[1] : null;
 136                  return;
 137              }
 138          }
 139          
 140          # No type, pass args to default
 141          $args = $part;
 142      }
 143      
 144  	protected function callHandler($type,$args)
 145      {
 146          if (!isset($this->types[$type])) {
 147              throw new Exception('Unknown URL type');
 148          }
 149          
 150          $handler = $this->types[$type]['handler'];
 151          if (!is_callable($handler)) {
 152              throw new Exception('Unable to call function');
 153          }
 154          
 155          call_user_func($handler,$args);
 156      }
 157      
 158  	protected function callDefaultHandler($args)
 159      {
 160          if (!is_callable($this->default_handler)) {
 161              throw new Exception('Unable to call function');
 162          }
 163          
 164          call_user_func($this->default_handler,$args);
 165      }
 166      
 167  	protected function parseQueryString()
 168      {
 169          if (!empty($_SERVER['QUERY_STRING']))
 170          {
 171              $q = explode('&',$_SERVER['QUERY_STRING']);
 172              $T = array();
 173              foreach ($q as $v)
 174              {
 175                  $t = explode('=',$v,2);
 176                  
 177                  $t[0] = rawurldecode($t[0]);
 178                  if (!isset($t[1])) {
 179                      $T[$t[0]] = null;
 180                  } else {
 181                      $T[$t[0]] = $t[1];
 182                  }
 183              }
 184              
 185              return $T;
 186          }
 187          return array();
 188      }
 189      
 190  	protected function sortTypes()
 191      {
 192          foreach ($this->types as $k => $v) {
 193              $r[$k] = $v['url'];
 194          }
 195          array_multisort($r,SORT_DESC,$this->types);
 196      }
 197      
 198  	protected function parseQuery($str,&$arr = array())
 199      {
 200          if (empty($str)) {
 201              return;
 202          }
 203          foreach (explode('&',$str) as $v) {
 204              //echo $v;
 205          }
 206      }
 207  }
 208  ?>


Généré le : Fri Feb 23 22:16:06 2007 par Balluche grâce à PHPXref 0.7