linea21-core
[ class tree: linea21-core ] [ index: linea21-core ] [ all elements ]

Source for file class.config_file.php

Documentation is available at class.config_file.php

  1. <?php
  2. /**
  3.  * @package linea21.core
  4.  * @subpackage config_files
  5.  * @author linea21 <info@linea21.com>
  6.  * @version $id SVN
  7.  * @access public
  8.  * @license http://opensource.org/licenses/gpl-3.0.html
  9.  *  Configuration Management
  10.  */
  11.  
  12. include_once('system/afiles/class.afiles.php');
  13. include_once('../lib/lib_common.php');
  14.  
  15. class config_file {
  16.  
  17.   var $file_release = "define_release"// fichier define_release
  18.   var $file_db = "define_db";  // fichier define_db
  19.   var $section_theme = "theme";  // section theme dans le fichier de configuration define_release
  20.   var $config_path = '../config/';
  21.   var $ini_path = '../config/';
  22.   var $backup_config_path = '../tmp/backup/config/';
  23.   var $authorized_release_sections//sections authorisées dans le fichier release
  24.   var $authorized_db_sections//sections authorisées dans le fichier db
  25.   var $authorized_theme_sections//sections authorisées dans le fichier release
  26.   var $config_extension = ".ini";
  27.   var $php_extension = ".php";
  28.   var $db_params;
  29.   var $release_params;
  30.   var $a_file;
  31.   var $theme_settings='THEME_SETTINGS';
  32.   var $theme_public='THEME_PUBLIC';
  33.   var $theme_list_public='THEME_LIST_PUBLIC';
  34.   var $theme_admin='THEME_ADMIN';
  35.   var $theme_list_admin='THEME_LIST_ADMIN';
  36.  
  37.  
  38.   /**
  39.    * PHP 4 compatibility
  40.    * as __construct()
  41.    * @return 
  42.    */
  43.  
  44.   function config_file()
  45.   {
  46.     $this->a_file=new afiles;
  47.     $this->authorized_release_sections=$GLOBALS['authorized_release_sections'];
  48.     $this->authorized_db_sections=$GLOBALS['authorized_db_sections'];
  49.     $this->authorized_theme_sections=$GLOBALS['authorized_theme_sections'];
  50.   }
  51.   /**
  52.    * config_file::__construct()
  53.    * constructeur de classe
  54.    * @access public
  55.    * @return void 
  56.    */
  57.   function __construct()
  58.   {
  59.     $this->a_file=new afiles;
  60.     $this->authorized_release_sections=$GLOBALS['authorized_release_sections'];
  61.     $this->authorized_db_sections=$GLOBALS['authorized_db_sections'];
  62.     $this->authorized_theme_sections=$GLOBALS['authorized_theme_sections'];
  63.   }
  64.   /**
  65.    * config_file::loadParams()
  66.    * charge les paramètres dans les tableaux $release_params et $db_params
  67.    * @access public
  68.    * @return void 
  69.    */
  70.   function loadParams()
  71.   {
  72.     $this->loadReleaseParams();
  73.     $this->loadDbParams();
  74.   }
  75.   /**
  76.    * config_file::setParams()
  77.    * modifie le tableau de cache interne avec le paramètre $content
  78.    * @access public
  79.    * @param string $file_name 
  80.    * @param array $content 
  81.    * @return void 
  82.    */
  83.   function setParams($file_name,$content)
  84.   {
  85.     if($file_name == $this->file_release)$this->setReleaseParams($content);
  86.     else if($file_name == $this->section_theme$this->setThemeParams($content);
  87.     else $this->setDbParams($content);
  88.   }
  89.  
  90.   /**
  91.    * config_file::loadReleaseParams()
  92.    * charge les paramètres dans le tableau $release_params
  93.    * @access public
  94.    * @return void 
  95.    */
  96.   function loadReleaseParams({
  97.     //charge les paramètres release dans un tableau $release_params
  98.     $this->release_params=$this->parseConfig($this->file_release);
  99.   }
  100.   /**
  101.    * config_file::loadDbParams()
  102.    * charge les paramètres dans le tableau $db_params
  103.    * @access public
  104.    * @return void 
  105.    */
  106.   function loadDbParams()
  107.   {
  108.     //charge les paramètres dans un tableau $db_params
  109.     $this->db_params=$this->parseConfig($this->file_db);
  110.   }
  111.  
  112.   /**
  113.    * config_file::setReleaseParams()
  114.    * modifie le tableau $release_params avec les entrées du tableau $content
  115.    * @access public
  116.    * @return void 
  117.    * @param array $content 
  118.    */
  119.   function setReleaseParams($content{
  120.  
  121.     //charge les paramètres release dans un tableau $release_params
  122.     $this->setPostParams($this->file_release,$content);
  123.   }
  124.   /**
  125.    * config_file::setThemeParams()
  126.    * modifie le tableau $release_params avec les entrées du tableau $content
  127.    * @access public
  128.    * @return void 
  129.    * @param array $content 
  130.    */
  131.   function setThemeParams($content{
  132.  
  133.     //charge les paramètres release dans un tableau $release_params
  134.     $this->setPostParams($this->section_theme,$content);
  135.   }
  136.   /**
  137.    * config_file::setDbParams()
  138.    * modifie le tableau $db_params avec les entrées du tableau $content
  139.    * @access public
  140.    * @return void 
  141.    * @param array $content 
  142.    */
  143.   function setDbParams($content)
  144.   {
  145.     //charge les paramètres dans un tableau $db_params
  146.     $this->setPostParams($this->file_db,$content);
  147.   }
  148.  
  149.   /**
  150.    * config_file::writeReleaseParams()
  151.    * écrit dans le fichier ini les paramètres du tableau $release_params
  152.    * @access public
  153.    * @return boolean 
  154.    */
  155.   function writeReleaseParams({
  156.     //écrit les paramètres $release_params vers les fichiers release  ini et php
  157.     if($this->backupParams($this->file_release)){
  158.       //on écrit les params après avoir fait le backup
  159.       if($this->a_file->mkini($this->release_params$this->ini_path.$this->file_release.$this->config_extension))
  160.       return true;
  161.     }
  162.     return false;
  163.   }
  164.   /**
  165.    * config_file::writedBParams()
  166.    * écrit dans le fichier ini les paramètres du tableau $db_params
  167.    * @access public
  168.    * @return boolean 
  169.    */
  170.   function writeDbParams({
  171.     //écrit les paramètres $db_params vers les fichier db ini et php
  172.     if($this->backupParams($this->file_db)){
  173.       //on écrit les params après avoir fait le backup
  174.       if($this->a_file->mkini($this->db_params$this->ini_path.$this->file_db.$this->config_extension))
  175.       return true;
  176.     }
  177.     return false;
  178.     //TODO : alert les paramètres db n'ont pas pu être écrit.
  179.   }
  180.  
  181.   /**
  182.    * config_file::backupParams()
  183.    * sauvegarde le fichier $file_name dans le répertoire de backup
  184.    * @access public
  185.    * @param string $file_name 
  186.    * @return boolean 
  187.    */
  188.   function backupParams($file_name)
  189.   {
  190.     SureCreateDir($this->backup_config_path.$this->config_extension0755);
  191.     if ($this->a_file->cp($this->ini_path.$file_name.$this->config_extension,$this->backup_config_path)) return true;
  192.     else return false;
  193.   }
  194.  
  195.   /**
  196.    * config_file::writePhpParams()
  197.    * écrit le fichier php $file_name dans le répertoire de config
  198.    * @access public
  199.    *  méthode dépréciée
  200.    * @param string $file_name 
  201.    * @return boolean 
  202.    */
  203.   function writePhpParams($file_name)
  204.   {
  205.     $content=$this->generateHeader($file_name);
  206.     $content.=$this->generateBody($file_name);
  207.     $content.=$this->generateFooter($file_name);
  208.     //on écrit le contenu dans le fichier php
  209.     if($this->a_file->mkfile($content,$this->config_path.$file_name.$this->php_extension)) return true;
  210.     else return false;
  211.   }
  212.   /**
  213.    * config_file::parseConfig()
  214.    * retourne un tableau contenant les paramètres du fichier ini $file_name
  215.    * @access public
  216.    * @param string $file_name 
  217.    * @return array 
  218.    */
  219.   function parseConfig($file_name)
  220.   {
  221.     //parse_ini_file
  222.     return $this->a_file->parse_ini($this->ini_path.$file_name.$this->config_extension);
  223.  
  224.   }
  225.   /**
  226.    * config_file::setPostParams()
  227.    * insère les données différentes contenues dans $post_params dans le tableau correspondant au paramètre release ou db
  228.    * @access public
  229.    * @param string $file_name 
  230.    * @param array $post_params 
  231.    * @return void 
  232.    */
  233.   function setPostParams($file_name,$post_params)
  234.   {
  235.     //parse du tableau des paramètres
  236.     // ajoute les paramètres de post
  237.     if($file_name == $this->file_release || $file_name == $this->section_theme)
  238.     {
  239.       foreach ($post_params as $key => $value)
  240.       {
  241.         foreach ($this->release_params as $section => $existing_params)
  242.         {
  243.           foreach ($existing_params as $existing_param => $existing_value)
  244.           {
  245.             // la clé existe dans le tableau de cache release_params, on insère la valeur dans ce tableau
  246.             if($existing_param == $key$this->release_params[$section][$key]=$this->strip_ini_char($value);
  247.           }
  248.         }
  249.       }
  250.     }
  251.     if($file_name == $this->file_db)
  252.     {
  253.       foreach ($post_params as $key => $value)
  254.       {
  255.         foreach ($this->db_params as $section => $existing_params)
  256.         {
  257.           foreach ($existing_params as $existing_param => $existing_value)
  258.           {
  259.             // la clé existe dans le tableau de cache db_params, on insère la valeur dans ce tableau
  260.             if($existing_param == $key$this->db_params[$section][$key]=$this->strip_ini_char($value);
  261.           }
  262.         }
  263.       }
  264.     }
  265.   }
  266.   /**
  267.    * config_file::generateBody()
  268.    * génère le contenu d'un fichier php de configuration à parti du $file_name
  269.    * @access private
  270.    * @param string $file_name 
  271.    * @return string 
  272.    */
  273.   function generateBody($file_name)
  274.   {
  275.  
  276.     $out '';
  277.  
  278.     if($file_name == $this->file_release$content=$this->release_params;
  279.     else $content=$this->db_params;
  280.  
  281.     if (!is_array($content))
  282.     return false;
  283.  
  284.     foreach ($content as $key => $ini)
  285.     {
  286.       if (is_array($ini))
  287.       {
  288.         $out .= "\n\n//    ".$key." /////// \n\n";
  289.  
  290.         foreach ($ini as $var => $value)
  291.         {
  292.           $out .= "define('".$var."',\t\t".$this->a_file->quote_ini($value).");\n";
  293.         }
  294.       }
  295.       else
  296.       {
  297.         $out .= "define('".$key."',\t\t".$this->a_file->quote_ini($ini).");\n";
  298.       }
  299.     }
  300.  
  301.     return $out;
  302.   }
  303.   /**
  304.    * config_file::generateHeader()
  305.    * génère l'en tête d'un fichier php de configuration à parti du $file_name
  306.    * @access private
  307.    * @param string $file_name 
  308.    * @return string 
  309.    */
  310.   function generateHeader($file_name)
  311.   {
  312.     $ret.="<?php \n";
  313.     $ret.="// Linea 21 - PHP ".$file_name."\n";
  314.     $ret.="// last_modify    : ".date("d-m-Y")."\n";
  315.     $ret.="// GPL 3\n";
  316.     $ret.="// http://opensource.org/licenses/gpl-3.0.html\n\n";
  317.     $ret.="// BE CAREFULL THIS FILE IS SYSTEM FILE : use interface or ".$file_name.$this->config_extension." FOR CONFIGURATION";
  318.     return $ret;
  319.   }
  320.   /**
  321.    * config_file::generateFooter()
  322.    * génère le pied d'un fichier php de configuration à parti du $file_name
  323.    * @access private
  324.    * @param string $file_name 
  325.    * @return string 
  326.    */
  327.   function generateFooter($file_name)
  328.   {
  329.     return "?> \n";
  330.   }
  331.  
  332.   /**
  333.    * config_file::getReleaseParams()
  334.    * génère le corps html d'un fichier de configuration pour affichage
  335.    * @access public
  336.    * @param string $updatable : champ texte modifiable
  337.    * @param string $hidden : présence d'input type hidden
  338.    * @return string 
  339.    */
  340.   function getReleaseParams($updatable false$hidden false{
  341.  
  342.     return $this->getAllParams($this->file_release,$updatable$hidden);
  343.   }
  344.   /**
  345.    * config_file::getDbParams()
  346.    * génère le corps html d'un fichier de configuration pour affichage
  347.    * @access public
  348.    * @param string $updatable : champ texte modifiable
  349.    * @param string $hidden : présence d'input type hidden
  350.    * @return string 
  351.    */
  352.   function getDbParams($updatable false$hidden false{
  353.  
  354.     return $this->getAllParams($this->file_db,$updatable$hidden);
  355.   }
  356.   /**
  357.    * config_file::getAllParams()
  358.    * génère le corps html d'un fichier de configuration pour affichage
  359.    * @access private
  360.    * @param string $updatable : champ texte modifiable
  361.    * @param string $hidden : présence d'input type hidden
  362.    * @param string $file_name 
  363.    * @return string 
  364.    */
  365.   function getAllParams($file_name$updatable false$hidden false)
  366.   {
  367.     $out '';
  368.     $authorized_sections=array();
  369.  
  370.     include('../languages/' LANGUAGE '/lang_system.'CHARSET .'.php');
  371.  
  372.     if($file_name == $this->file_release{
  373.       // le tableau de cache à utiliser est celui de realease
  374.       $content=$this->release_params;
  375.       //initialisation des sections à afficher/mdofier
  376.       $authorized_sections=$this->authorized_release_sections;
  377.     }
  378.     else if($file_name == $this->section_theme)
  379.     {
  380.       // le tableau de cache à utiliser est celui de realease
  381.       $content=$this->release_params;
  382.       $authorized_sections=$this->authorized_theme_sections;
  383.     }
  384.     else
  385.     {
  386.       // le tableau de cache à utiliser est celui de db
  387.       $content=$this->db_params;
  388.       //initialisation des sections à afficher/mdofier
  389.       $authorized_sections=$this->authorized_db_sections;
  390.     }
  391.  
  392.     if (!is_array($content))
  393.     return false;
  394.  
  395.     foreach ($content as $key => $ini)
  396.     {
  397.  
  398.       if (is_array($ini))
  399.       {
  400.         if((in_array($key,$authorized_sections)))
  401.         {
  402.  
  403.           if(isset($GLOBALS['lang']['system'][$key])) $label='<abbr title="'.$GLOBALS['lang']['system'][$key].'">'.$key.'</abbr>';
  404.           else $label=$key;
  405.  
  406.           $out .= "\n<br style='clear:both'>\n";
  407.           $out .= "\n<h2>".$label."</h2>\n";
  408.           if(!$updatable$out .="<dl>\n";
  409.           //génération d'un bloc
  410.           foreach ($ini as $var => $value)
  411.           {
  412.             $label='';
  413.             if(isset($GLOBALS['lang']['system'][$var])) $label='<abbr title="'.$GLOBALS['lang']['system'][$var].'">'.$var.'</abbr>';
  414.             else $label=$var;
  415.             if(!$updatable)    {
  416.               $out .="<dt>".$label;
  417.               if($hidden$out .= "<input name=\"".$var."\" id=\"".$var."\" value=\"".$value."\" type=\"hidden\"  />\n";
  418.               $out .="</dt><dd>".$value."</dd>\n";
  419.             }
  420.             else $out .= "<p><label for=\"".$var."\">".$label."</label><input name=\"".$var."\" id=\"".$var."\" value=\"".$value."\" type=\"text\" class=\"textfield\" /></p>\n";
  421.           }
  422.           if(!$updatable$out .="</dl>";
  423.         }
  424.       }
  425.       else
  426.       {
  427.         if(!$updatable && $hidden$out .= "<input name=\"".$key."\" id=\"".$key."\" value=\"".$ini."\" type=\"hidden\"  />\n";
  428.         if(!$updatable)    $out .="<dt>".$key."</dt><dd>".$ini."</dd>\n";
  429.         else $out .= "<p><label for=\"".$key."\">".$key."</label><input name=\"".$key."\" id=\"".$key."\" value=\"".$ini."\" type=\"text\" class=\"textfield\" /></p>\n";
  430.       }
  431.     }
  432.     return $out;
  433.  
  434.   }
  435.   /**
  436.    * config_file::getThemeParams()
  437.    * génère le corps html d'un fichier de configuration pour affichage
  438.    * @access public
  439.    * @param string $updatable : champ texte modifiable
  440.    * @param string $hidden : présence d'input type hidden
  441.    * @return string 
  442.    */
  443.   function getThemeParams($updatable false$hidden false{
  444.  
  445.     $out '';
  446.     $authorized_sections=array();
  447.  
  448.     include('../languages/' LANGUAGE '/lang_system.'CHARSET .'.php');
  449.  
  450.     $content=$this->release_params;
  451.     $authorized_sections=$this->authorized_theme_sections;
  452.  
  453.     $theme_public=$content[$this->theme_settings][$this->theme_public];
  454.     $theme_list_public=$content[$this->theme_settings][$this->theme_list_public];
  455.     $theme_admin=$content[$this->theme_settings][$this->theme_admin];
  456.     $theme_list_admin=$content[$this->theme_settings][$this->theme_list_admin];
  457.  
  458.     if(isset($GLOBALS['lang']['system'][$this->theme_public])) $label='<abbr title="'.$GLOBALS['lang']['system'][$this->theme_public].'">'.$this->theme_public.'</abbr>';
  459.     else $label=$this->theme_public;
  460.  
  461.     $out .= "\n<h2 style=\"clear:both;\">".$label."</h2>\n";
  462.     $out .=$this->getOneThemeParams($updatable$hiddentrue,$theme_public,$this->theme_public,$this->theme_public"public");
  463.  
  464.     //on checke si c'est un tableau séparé par des |
  465.     //on split et on boucle sur les éléments du tableau
  466.     $theme_list_public_array=explode("|",$theme_list_public);
  467.  
  468.     foreach ($theme_list_public_array as $key => $value)
  469.     {
  470.       if($value != $theme_public)
  471.       $out .=$this->getOneThemeParams($updatable$hiddenfalse,$value,$this->theme_list_public,$this->theme_public"public");
  472.     }
  473.  
  474.     if(isset($GLOBALS['lang']['system'][$this->theme_admin])) $label='<abbr title="'.$GLOBALS['lang']['system'][$this->theme_admin].'">'.$this->theme_admin.'</abbr>';
  475.     else $label=$this->theme_admin;
  476.  
  477.     $out .= "\n<h2 style=\"clear:both;\">".$label."</h2>\n";
  478.  
  479.     $out .=$this->getOneThemeParams($updatable$hiddentrue,$theme_admin,$this->theme_admin,$this->theme_admin"admin");
  480.  
  481.     //on checke si c'est un tableau séparé par des |
  482.     //on split et on boucle sur les éléments du tableau
  483.     $theme_list_admin_array=explode("|",$theme_list_admin);
  484.  
  485.     foreach ($theme_list_admin_array as $key => $value)
  486.     {
  487.       if($value != $theme_admin)
  488.       $out .=$this->getOneThemeParams($updatable$hiddenfalse,$value,$this->theme_list_admin,$this->theme_admin"admin");
  489.     }
  490.  
  491.     $out .= "\n<br style=\"clear:both;\" />\n";
  492.     return $out;
  493.   }
  494.  
  495.  
  496.   /**
  497.    * config_file::getOneThemeParams()
  498.    * génère le corps html d'un fichier de configuration pour affichage
  499.    * @access private
  500.    * @param string $updatable 
  501.    * @param string $hidden 
  502.    * @param string $selected 
  503.    * @param string $value 
  504.    * @param string $field_name 
  505.    * @param string $radio_name 
  506.    * @return string 
  507.    */
  508.   function getOneThemeParams($updatable false$hidden false$selected=false,$value,$field_name,$radio_name$type{
  509.  
  510.     include('../languages/' LANGUAGE '/lang_system.'CHARSET .'.php');
  511.  
  512.     $selected_pattern='';
  513.     $checked_pattern='';
  514.     $hidden_pattern='';
  515.     $updatable_pattern='';
  516.  
  517.     if ($selected)
  518.     {
  519.       $selected_pattern='_selected';
  520.       $checked_pattern='checked="checked"';
  521.     }
  522.     if(!$updatable)$hidden_pattern='style="display:none;"';
  523.     else $updatable_pattern="onclick=\"javascript:document.getElementById('".$value."').checked='checked';\"";
  524.     $out '';
  525.     $out .="<div class=\"config_theme" $selected_pattern"\">\n";
  526.     $out .="<img src=\"../templates/" .$type"/" .$value"/preview.png\" alt=\"" .$value"\" id=\"img_".$value."\" ".$updatable_pattern."/>\n";
  527.  
  528.     $out .= '<input type="radio" name="' .$radio_name'" id="' .$value'" value="' .$value'" ' .$checked_pattern' ' .$hidden_pattern' />';
  529.  
  530.     $out .=    '<span class="radio">' $value '</span>';
  531.     $out .="</div>";
  532.     return $out;
  533.   }
  534.  
  535.   /**
  536.    * config_file::strip_ini_char
  537.    * suppression des caractères interdits dans un fichier ini
  538.    * @access private
  539.    * @param string $content 
  540.    * @return string 
  541.    */
  542.   function strip_ini_char($content)
  543.   {
  544.     $ret='';
  545.     // on remplace les caractères "[]; par '(),
  546.     $ret=strtr($content"\"[];""'(),");
  547.     if (get_magic_quotes_gpc()) $ret=stripslashes($ret);
  548.     return $ret;
  549.   }
  550.  
  551.   /**
  552.    * config_file::initializeIniParams()
  553.    * charge les paramètres des fichiers ini et les place en constante globale par define
  554.    * @access public
  555.    * @param void 
  556.    * @return void 
  557.    */
  558.   function initializeIniParams()
  559.   {
  560.     //charge les paramètres et les place en constante globale par define
  561.     $this->loadReleaseParams();
  562.     $this->loadDbParams();
  563.  
  564.     foreach ($this->release_params as $section => $existing_params)
  565.     {
  566.       foreach ($existing_params as $existing_param => $existing_value)
  567.       {
  568.         if(!defined($existing_param))
  569.         {
  570.           if(is_numeric($existing_value))
  571.           {
  572.             $existing_value=$this->convert_type($existing_value);
  573.           }
  574.           define($existing_param,$existing_value);
  575.         }
  576.       }
  577.     }
  578.     foreach ($this->db_params as $section => $existing_params)
  579.     {
  580.       foreach ($existing_params as $existing_param => $existing_value)
  581.       {
  582.         if(!defined($existing_param))
  583.         {
  584.           if(is_numeric($existing_value))
  585.           {
  586.             $existing_value=$this->convert_type($existing_value);
  587.           }
  588.           define($existing_param,$existing_value);
  589.         }
  590.       }
  591.     }
  592.   }
  593.  
  594.   /**
  595.    * config_file::convert_type()
  596.    * conversion d'une variable en entier ou en float selon son contenu
  597.    * @param unknown_type $var 
  598.    * @return int or float
  599.    */
  600.   function convert_type$var )
  601.   {
  602.     ifis_numeric$var ) )
  603.     {
  604.       if(float)$var != (int)$var )
  605.       {
  606.         return (float)$var;
  607.       }
  608.       else
  609.       {
  610.         return (int)$var;
  611.       }
  612.     }
  613.     if$var == "true" )    return true;
  614.     if$var == "false" )    return false;
  615.     return $var;
  616.   }
  617.   /**
  618.    * config_file::getPhpInfo()
  619.    * Retourne un string contenant les informations php en html
  620.    * @access public
  621.    * @return string 
  622.    */
  623.   function getPhpInfo({
  624.     ob_start();
  625.     phpinfo();
  626.     $info ob_get_contents();
  627.     ob_end_clean();
  628.     return preg_replace('%^.*<body>(.*)</body>.*$%ms''$1'$info);
  629.   }
  630. }
  631.  
  632. ?>

Documentation generated on Fri, 16 Oct 2009 09:29:10 +0200 by phpDocumentor 1.4.1