[ Index ]
 

Code source de Plume CMS 1.2.2

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/manager/inc/ -> class.dispatcher.php (source)

   1  <?php
   2  /* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
   3  /*
   4  # ***** BEGIN LICENSE BLOCK *****
   5  # This file is part of Plume CMS, a website management application.
   6  # Copyright (C) 2001-2005 Loic d'Anterroches and contributors.
   7  #
   8  # Plume CMS is free software; you can redistribute it and/or modify
   9  # it under the terms of the GNU General Public License as published by
  10  # the Free Software Foundation; either version 2 of the License, or
  11  # (at your option) any later version.
  12  #
  13  # Plume CMS is distributed in the hope that it will be useful,
  14  # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16  # GNU General Public License for more details.
  17  #
  18  # You should have received a copy of the GNU General Public License
  19  # along with this program; if not, write to the Free Software
  20  # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  21  #
  22  # ***** END LICENSE BLOCK ***** */
  23  
  24  /**
  25   * Dispatcher of the requests to the handlers.
  26   */
  27  class Dispatcher
  28  {
  29  
  30      /**
  31       * The unique method to call.
  32       *
  33       * @param string Query string ('')
  34       */
  35      function Launch($query='')
  36      {
  37          $GLOBALS['_PX_render']['error'] = new CError();
  38  
  39          $query = preg_replace('#^(/)+#', '/', '/'.$query);
  40  
  41          Dispatcher::loadBuiltinControllers();
  42          Dispatcher::loadControllers();
  43          Dispatcher::match($query);
  44      }
  45  
  46  
  47      /**
  48       * Match a query against the actions controllers.
  49       *
  50       * @param string Query string
  51       */
  52      function match($query)
  53      {
  54          // Order the controllers by priority
  55          foreach ($GLOBALS['_PX_control'] as $key => $control) {
  56              $priority[$key] = $control['priority'];
  57          }
  58          array_multisort($priority, SORT_ASC, $GLOBALS['_PX_control']);
  59  
  60          $res = 200;
  61          foreach ($GLOBALS['_PX_control'] as $key => $control) {
  62              if (preg_match($control['regex'], $query)) {
  63                  if ($res == 404 and $control['priority'] < 8) {
  64                      continue;
  65                  }
  66                  config::setVar('action', $control['plugin']);
  67                  $res = call_user_func(array($control['plugin'], 'action'), 
  68                                        $query);
  69                  if ($res != 301 and $res != 404) {
  70                      showDebugInfo();
  71                      return;
  72                  }
  73                  if ($res == 301 and !empty($GLOBALS['_PX_redirect'])) {
  74                      header('HTTP/1.1 301 Moved Permanently');
  75                      header('Location: '.$GLOBALS['_PX_redirect']);
  76                      header('Connection: close');
  77                      return;
  78                  }
  79              }
  80          }
  81      }
  82  
  83  
  84      /**
  85       * Load the builtin controllers.
  86       *
  87       * The builtin controllers are for: news, article, category, 
  88       * page not found, search and rss.
  89       */
  90      function loadBuiltinControllers()
  91      {
  92          Dispatcher::registerController('RSS', '#^/feed/(.*)$#i', 4);
  93          Dispatcher::registerController('Sitemap', '#^/sitemap#i', 4);
  94          Dispatcher::registerController('Error404', '#^/error404$#', 4);
  95          Dispatcher::registerController('Search', '#^/search/(.*)$#i', 4);
  96          Dispatcher::registerController('Comment', '#^/comments/(\d+)/*$#i', 4);
  97          Dispatcher::registerController('Category',
  98                                         '#^(.*/)(index)([0-9]*)$|^.*/$|^$#i',
  99                                         6);
 100          Dispatcher::registerController('Article',
 101                                         '#^(.*/)([a-z]|[a-z][a-z0-9\_\-]*[a-z])(\d*)$#i',
 102                                         7);
 103          Dispatcher::registerController('News', '#^(.*/)(\d+)\-([^\.]*)$#i', 7);
 104          Dispatcher::registerController('Error404', '#.*#', 9);
 105      }
 106  
 107      /**
 108       * Load the controllers.
 109       */
 110      function loadControllers()
 111      {
 112          $base = config::f('manager_path').'/tools/';
 113          $d = dir($base);
 114          while (($entry = $d->read()) !== false) {
 115              if ($entry != '.' && $entry != '..' 
 116                  && is_dir($base.$entry) 
 117                  && file_exists($base.$entry.'/register.php')) {
 118                  include_once($base.$entry.'/register.php');
 119              }
 120          }
 121          @$d->close();
 122      }
 123  
 124  
 125      /**
 126       * Register an action controller.
 127       *
 128       * - The plugin must provide a "standalone" action method
 129       * pluginname::action($querystring)
 130       * - The priority is to order the controller matches. 
 131       * 5: Default, if the controller provides some content
 132       * 1: If the controller provides a control before, without providing
 133       * content, note that in this case the return code must be a redirection.
 134       * 8: If the controller is providing a catch all case to replace the
 135       * default 404 error page.
 136       *
 137       * @param string Plugin name providing the action controller
 138       * @param string Regex to match on the query string
 139       * @param int Priority (5)
 140       * @return void
 141       */
 142      function registerController($plugin, $regex, $priority=5)
 143      {
 144          if (!isset($GLOBALS['_PX_control'])) {
 145              $GLOBALS['_PX_control'] = array();
 146          }
 147          $GLOBALS['_PX_control'][] = array('plugin' => $plugin,
 148                                            'regex' => $regex,
 149                                            'priority' => $priority);
 150      }
 151  
 152  }
 153  
 154  ?>


Généré le : Mon Nov 26 11:57:01 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics