Source for file Config.php

Documentation is available at Config.php

  1. <?php
  2. // vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 fdm=marker encoding=utf8 :
  3. /**
  4.  * Pxxo - build self-supported and interoperable Web graphical components
  5.  * 
  6.  * Copyright (c) 2008, Nicolas Thouvenin
  7.  *
  8.  * All rights reserved.
  9.  *
  10.  * Redistribution and use in source and binary forms, with or without
  11.  * modification, are permitted provided that the following conditions are met:
  12.  *
  13.  *     * Redistributions of source code must retain the above copyright
  14.  *       notice, this list of conditions and the following disclaimer.
  15.  *     * Redistributions in binary form must reproduce the above copyright
  16.  *       notice, this list of conditions and the following disclaimer in the
  17.  *       documentation and/or other materials provided with the distribution.
  18.  *     * Neither the name of the author nor the names of its contributors may be
  19.  *       used to endorse or promote products derived from this software without
  20.  *       specific prior written permission.
  21.  *
  22.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
  23.  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  24.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25.  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
  26.  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  27.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  28.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  29.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32.  
  33.  * @package    Pxxo
  34.  * @copyright  Copyright (c) 2008 Nicolas Thouvenin
  35.  * @license    http://opensource.org/licenses/bsd-license.php
  36.  * @version    $Id$
  37.  */
  38.  
  39. if (!defined('PXXO_ZEND_PATH')) {
  40.     define('PXXO_ZEND_PATH'realpath(rtrim(dirname(__FILE__),DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'ZendFramework'));
  41.     $include_path ini_get('include_path');
  42.     if ((PXXO_ZEND_PATH !== false&& (strpos($include_pathPXXO_ZEND_PATH=== false))
  43.         ini_set('include_path'$include_path.PATH_SEPARATOR.PXXO_ZEND_PATH);
  44. }
  45.  
  46. require_once 'Zend/Registry.php';
  47. require_once 'Zend/Locale.php';
  48.  
  49. /**
  50.  * Classe de configuration
  51.  *
  52.  * Permet de stocker des parametres de maniere unique.
  53.  * tout en les rendant disponnible partout
  54.  *
  55.  * @package    Pxxo
  56.  * @copyright  Copyright (c) 2008 Nicolas Thouvenin
  57.  * @license    http://opensource.org/licenses/bsd-license.php
  58.  */
  59. class Pxxo_Config 
  60. {
  61.     /**
  62.      * Chemin physique de la racine de l'application
  63.      * @var        string 
  64.      */
  65.     public $path = '';
  66.     /**
  67.      * Nom physique (UNIX) de l'application
  68.      * @var        string 
  69.      */
  70.     public $name;
  71.     /**
  72.      * Courriel de l'application
  73.      * @var        string 
  74.      */
  75.     public $mail;
  76.     /**
  77.      * Version de l'application
  78.      * @var        string 
  79.      */
  80.     public $version;
  81.     /**
  82.      * Charset de l'application
  83.      * @var        string 
  84.      */
  85.     public $charset = 'UTF-8';
  86.     /**
  87.      * Langue par defaut de l'application
  88.      * @var        mixed 
  89.      */
  90.     public $language;
  91.     /**
  92.      * l'URL du serveur de l'application sans le chemin et l'éventuel script terminal
  93.      * exemple: http://localhost
  94.      * @var        string 
  95.      */
  96.     public $url_host;
  97.     /**
  98.      * Chemin dans l'URL vers la racine de l'application
  99.      * exemple: /monapplication
  100.      * @var        string 
  101.      */
  102.     public $url_root;
  103.     /**
  104.      * URL complete vers la racine de l'application (concaténation de url_host et url_root)
  105.      * exemple: http://localhost/monapplication
  106.      * @var        string 
  107.      */
  108.     public $url_base;
  109.     /**
  110.      * Nom du script pointant vers le point d'entré de l'application
  111.      * exemple : article3.php
  112.      * @var        string 
  113.      */
  114.     public $url_script;
  115.     /**
  116.      * Paramètre GET du point d'entré de l'application
  117.      * exemple : action=monaction&param=bidon
  118.      * @var        string 
  119.      */
  120.     public $url_query;
  121.     /**
  122.      * Filtre appliqué sur url_query permettant de ne conserver que les paramètres du point d'entrée de l'application
  123.      * exemple : si les paramètres de l'url sont action=home&article=1&monparam=bidon
  124.      *           et que url_query_filter vaut /^(action|article)/ (c'est une expression régulière)
  125.      *           les paramètres conservés dans url_query seront action=home&article=1
  126.      * Par défaut on ne laisse passer aucun paramètres dans l'url.
  127.      * @var        string 
  128.      */
  129.     public $url_query_filter;
  130.     /**
  131.      * URL complète du point d'entré de l'application (concaténation de url_base et url_script)
  132.      * exemple : http://localhost/monapplication/article3.php?action=monaction&param=bidon
  133.      * @var        string 
  134.      */
  135.     public $url;
  136.     /**
  137.      * Repertoire temporaire
  138.      * @var        string 
  139.      */
  140.     public $temp_path = '';
  141.     /**
  142.      * Nom de la première classe Pxxo mère de toute les autres
  143.      * @var        string 
  144.      */
  145.     public $class_core;
  146.     /**
  147.      * Tableau contenant les arguments Ã  donner au constructeur de la classe "class_core"
  148.      * @var        array 
  149.      */
  150.     public $class_args;
  151.     /**
  152.      * Préfix communn Ã  toutes les classes Pxxo
  153.      * @var        mixed 
  154.      */
  155.     public $class_prefix  = '';
  156.     /**
  157.      * Liste des chemins (genre include_path) pour trouver les classes Pxxo
  158.      * @var        string 
  159.      */
  160.     public $class_path = '';
  161.     /**
  162.      * Active ou non le cache
  163.      * @var        integer 
  164.      */
  165.     public $cache_flag = null;
  166.     /**
  167.      * Chemin Physique vers le réperoire de cache
  168.      * @var        string 
  169.      */
  170.     public $cache_path = '';
  171.     /**
  172.      * Niveau cf P_C_*
  173.      * @var        integer 
  174.      */
  175.     public $cache_level = null;
  176.     /**
  177.      * Niveau de debug 0 désactivé,  1 : code HTML non "compressé et report de toutes les erreurs PHP, 2 : active la trace pxxo,  3 : active le profiler
  178.      * @var        integer 
  179.      */
  180.     public $debug_level = 0;
  181.     /**
  182.      * Le mode Session est-il activé
  183.      * @var        string 
  184.      */
  185.     public $session_flag = false;
  186.     /**
  187.      * Les sessions sont-elle activé automatiquement
  188.      * @var        string 
  189.      */
  190.     public $session_auto = false;
  191.     /**
  192.      * Utilisation ou non du controller
  193.      * @var        mixed 
  194.      */
  195.     public $controller_used = false;
  196.     /**
  197.      * Nom du theme par defaut pour les objects Pxxo_Pago_Action
  198.      * @var        string 
  199.      */
  200.     public $theme_name = '';
  201.     /**
  202.      * Liste des chemins d'accès aux thèmes
  203.      * @var        string 
  204.      */
  205.     public $theme_path = '';
  206.     /**
  207.      * chemin vers un répertoire physique ou seront généré les resources
  208.      * @var        mixed 
  209.      */
  210.     public $resources_path = '';
  211.     /**
  212.      * url correspondant à au répertoire physique
  213.      * @var        mixed 
  214.      */
  215.     public $resources_url = '';
  216.     /**
  217.      * @var        string active la modification de Pxxo_Config via des requetes GET ou POST
  218.      */
  219.     public $request_flag = false;
  220.     /**
  221.      * @var        array liste des variables potentiellement modifiable  via des requetes GET ou POST
  222.      */
  223.     public $request_auth = array('language');
  224.     /**
  225.      * @var        int     Spécifie où les messages d'erreur doivent aller. (cf. http://fr.php.net/error_log)
  226.      */
  227.     public $error_log_type = null;
  228.     /**
  229.      * @var        string La destination dépend du type (cf. http://fr.php.net/error_log)
  230.      */
  231.     public $error_log_destination = null;
  232.     /**
  233.      * @var        string Les en-têtes supplémentaires (cf. http://fr.php.net/error_log)
  234.      */
  235.     public $error_log_extras = null;
  236.     /**
  237.      * @var        Zend_Locale 
  238.      */
  239.     public $locale;
  240.  
  241.     /**
  242.      * Vérification de la cohérence des valeurs de la configuration
  243.      *
  244.      */
  245.     public function check()
  246.     {
  247.         // Controle des niveaux
  248.         settype($this->debug_level'integer');
  249.         if ($this->debug_level < 0)
  250.             $this->debug_level = 0;
  251.         if ($this->debug_level > 4)
  252.             $this->debug_level = 4;
  253.  
  254.         // Réglage du mode Debug
  255.         if ($this->debug_level > 0error_reporting(E_ALL);
  256.  
  257.         if ($this->request_flag === true{
  258.             foreach($this->request_auth as $p)
  259.                 if (isset($_REQUEST[$p])) $this->$p $_REQUEST[$p];
  260.         }
  261.  
  262.         // Détéction des chemins par défaut
  263.         if (empty($this->path&& isset($_SERVER['SCRIPT_FILENAME']))
  264.             $this->path = dirname($_SERVER['SCRIPT_FILENAME']);
  265.         if (empty($this->path&& isset($_SERVER['PHP_SELF']))
  266.             $this->path = dirname($_SERVER['PHP_SELF']);
  267.         if (empty($this->path))
  268.             $this->path = '.';
  269.  
  270.         if (empty($this->temp_path)) {
  271.             if (isset($_ENV['TMP'])) $this->temp_path = $_ENV['TMP'];
  272.             elseif (isset($_ENV['TMPDIR'])) $this->temp_path = $_ENV['TMPDIR'];
  273.             elseif (isset($_ENV['TEMP'])) $this->temp_path = $_ENV['TEMP'];
  274.             elseif (is_dir('/tmp')) $this->temp_path = '/tmp';
  275.             else $this->temp_path = '.';
  276.         }
  277.         if (empty($this->resources_path)) {
  278.             $this->resources_path = $this->temp_path;
  279.         }
  280.         if (empty($this->cache_path)) {
  281.             $this->cache_path = $this->temp_path;
  282.         }
  283.         if (!empty($this->class_path)) {
  284.             $include_path ini_get('include_path');
  285.             if (
  286.                 (strpos($include_path$this->class_path.PATH_SEPARATOR=== false&&
  287.                 (strpos($include_pathPATH_SEPARATOR.$this->class_path=== false)
  288.             
  289.             ini_set('include_path',$this->class_path.PATH_SEPARATOR.$include_path);
  290.         }
  291.  
  292.  
  293.         // Les chemins déduits sont-ils des répertoires ? Et pour certain peut-on écrire dedans ?
  294.         if (!is_dir($this->path))
  295.             trigger_error(sprintf('invalid Pxxo_Config parameter : `path` (%s) is not a directory',
  296.                 $this->path),
  297.             E_USER_NOTICE);
  298.         if (!is_dir($this->temp_path))
  299.             trigger_error(sprintf('invalid Pxxo_Config parameter : `temp_path` (%s) is not a directory',
  300.                 $this->temp_path),
  301.             E_USER_NOTICE);
  302.         if (!is_writable($this->temp_path))
  303.             trigger_error(sprintf('invalid Pxxo_Config parameter : `temp_path`(%s) is not writable by the web server',
  304.                 $this->temp_path),
  305.             E_USER_ERROR);
  306.         if (!is_dir($this->cache_path))
  307.             trigger_error(sprintf('invalid Pxxo_Config parameter : `cache_path` (%s) is not a directory',
  308.                 $this->cache_path),
  309.             E_USER_ERROR);
  310.         if (!is_writable($this->cache_path))
  311.             trigger_error(sprintf('invalid Pxxo_Config parameter : `cache_path` (%s) is not writable by the web server',
  312.                 $this->cache_path),
  313.             E_USER_ERROR);
  314.         if (!is_dir($this->resources_path))
  315.             trigger_error(sprintf('invalid Pxxo_Config parameter : `resources_path` (%s) is not a directory',
  316.                 $this->resources_path),
  317.             E_USER_ERROR);
  318.         if (!is_writable($this->resources_path))
  319.             trigger_error(sprintf('invalid Pxxo_Config parameter : `resources_path` (%s) is not writable by the web server',
  320.                 $this->resources_path),
  321.             E_USER_ERROR);
  322.  
  323.         // Detection et decomposition des elements de l'URI
  324.         if (!isset($this->url_root))
  325.             $this->url_root = dirname($_SERVER['PHP_SELF']);
  326.         if (strpos($this->url_root'/'=== false)
  327.             $this->url_root = '/'.$this->url_root;
  328.         $this->url_root = preg_replace(',(\/$),'''$this->url_root);
  329.  
  330.         if (!isset($this->url_host&& isset($_SERVER['HTTP_HOST']))
  331.             $this->url_host = 'http'.(!isset($_SERVER['HTTPS'])||strtolower($_SERVER['HTTPS'])!= 'on'?'':'s').'://'.(isset($_SERVER["HTTP_X_FORWARDED_HOST"]$_SERVER['HTTP_X_FORWARDED_HOST'$_SERVER['HTTP_HOST']);
  332.         $this->url_host = preg_replace('/\/$/'''$this->url_host);
  333.  
  334.         if (!isset($this->url_base))
  335.             $this->url_base = $this->url_host.$this->url_root;
  336.         $this->url_base = preg_replace('/\/$/'''$this->url_base);
  337.  
  338.         if (!isset($this->url_script))
  339.             $this->url_script = basename($_SERVER['PHP_SELF']);
  340.         $this->url_script = preg_replace('/\//'''$this->url_script);
  341.  
  342.         if (!isset($this->url_query))
  343.             $this->url_query = isset($_SERVER['QUERY_STRING']$_SERVER['QUERY_STRING''';
  344.  
  345.         // Filtrage des paramètres de l'url
  346.         if (isset($this->url_query_filter))
  347.         {
  348.             $url_query_tmp array();
  349.             parse_str($this->url_query$url_query_tmp);
  350.             foreach(array_keys($url_query_tmpas $key)
  351.                 if (!preg_match($this->url_query_filter,$key))
  352.                     unset($url_query_tmp[$key]);
  353.             require_once 'PHP/Compat/Function/http_build_query.php'// for php4 compatibility
  354.             // rebuild the modified url
  355.             $this->url_query = http_build_query($url_query_tmp);
  356.             $this->url_query = preg_replace('/&/''&amp;'$this->url_query);
  357.         }   
  358.         else
  359.             $this->url_query = ''// when no url_query_filter is defined, do not forward any GET parameters
  360.  
  361.         if (!isset($this->url))
  362.             $this->url = $this->url_base .'/'$this->url_script . ($this->url_query != '' '?'.$this->url_query : '');
  363.  
  364.         if ($this->resources_url === '')
  365.             $this->resources_url = $this->url_root;
  366.  
  367.         // Session
  368.         if ($this->session_flag === true{
  369.             if (!empty($this->session_path)) {
  370.                 if (!file_exists($this->session_path))
  371.                     mkdir($this->session_path);
  372.                 if (is_dir($this->session_path))
  373.                     session_save_path($this->session_path);
  374.             }
  375.             if ($this->session_auto === false{
  376.                 session_start();
  377.             }
  378.             //session_cache_limiter('must-revalidate'); // trop dur d'imposer cela Ã  tout le monde
  379.         }
  380.  
  381.         // Locale
  382.         $this->locale  = new Zend_Locale(Zend_Locale::BROWSER);
  383.  
  384.         if (is_null($this->languageor empty($this->language)) {
  385.             $this->language = $this->locale->getLanguage();
  386.         }
  387.     }
  388.     /**
  389.      * Enregistre la classe courante comme classe de référence
  390.      */
  391.     public function register()
  392.     {
  393.         $this->check();
  394.         $o new ReflectionClass('Pxxo_Config');
  395.         $vars $o->getProperties();
  396.         foreach($vars as $var{
  397.             Zend_Registry::set($var->getName()$this->{$var->getName()});
  398.         }
  399.     }
  400.     /**
  401.      * Renvoit un paramètre de la configuration de référence
  402.      *
  403.      * @static
  404.      * @param    string 
  405.      * @return   mixed 
  406.      */
  407.     public static function get($name)
  408.     {
  409.         if (Zend_Registry::isRegistered($name)) {
  410.             return Zend_Registry::get($name);
  411.         }
  412.     }
  413.     /**
  414.      * Renvoit un paramètre (non standard) de la configuration de référence
  415.      *
  416.      * @param    string 
  417.      * @return   mixed 
  418.      */
  419.     public function __get($name)
  420.     {
  421.         return Zend_Registry::get($name);
  422.     }
  423.  
  424.     /**
  425.      * Fixe un paramètre de la configuration de référence
  426.      *
  427.      * @param    string 
  428.      * @return   mixed 
  429.      */
  430.     public static function set($name&$value)
  431.     {
  432.         return Zend_Registry::set($name$value);
  433.     }
  434.     /**
  435.      * Fixe un paramètre (non standard) de la configuration de référence
  436.      *
  437.      * @param    string 
  438.      * @return   mixed 
  439.      */
  440.     public function __set($name$value)
  441.     {
  442.         return Zend_Registry::set($name$value);
  443.     }
  444. }

Documentation generated on Thu, 13 Mar 2008 22:03:13 +0100 by phpDocumentor 1.4.1