[ Index ] |
|
Code source de Plume CMS 1.2.2 |
1 <?php 2 # ***** BEGIN LICENSE BLOCK ***** 3 # Version: MPL 1.1/GPL 2.0/LGPL 2.1 4 # 5 # The contents of this file are subject to the Mozilla Public License Version 6 # 1.1 (the "License"); you may not use this file except in compliance with 7 # the License. You may obtain a copy of the License at 8 # http://www.mozilla.org/MPL/ 9 # 10 # Software distributed under the License is distributed on an "AS IS" basis, 11 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 12 # for the specific language governing rights and limitations under the 13 # License. 14 # 15 # The Original Code is DotClear Weblog. 16 # 17 # The Initial Developer of the Original Code is 18 # Olivier Meunier. 19 # Portions created by the Initial Developer are Copyright (C) 2003 20 # the Initial Developer. All Rights Reserved. 21 # 22 # Contributor(s): 23 # loïc d'Anterroches: Modification to manage a different config file format. 24 # 25 # Alternatively, the contents of this file may be used under the terms of 26 # either the GNU General Public License Version 2 or later (the "GPL"), or 27 # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), 28 # in which case the provisions of the GPL or the LGPL are applicable instead 29 # of those above. If you wish to allow use of your version of this file only 30 # under the terms of either the GPL or the LGPL, and not to allow others to 31 # use your version of this file under the terms of the MPL, indicate your 32 # decision by deleting the provisions above and replace them with the notice 33 # and other provisions required by the GPL or the LGPL. If you do not delete 34 # the provisions above, a recipient may use your version of this file under 35 # the terms of any one of the MPL, the GPL or the LGPL. 36 # 37 # ***** END LICENSE BLOCK ***** 38 39 # Classe de gestion d'un fichier de configuration 40 41 class configfile 42 { 43 var $file; 44 var $content; 45 var $prefix = '_PX_config'; 46 47 function configfile($file) 48 { 49 clearstatcache(); 50 if (file_exists($file)) { 51 $this->file = $file; 52 $f = fopen($file, 'r'); 53 $this->content = fread($f, filesize($file)); 54 fclose($f); 55 } else { 56 $this->file = false; 57 } 58 } 59 60 /** édition d'une variable */ 61 function editVar($name,$value) 62 { 63 if ($this->file !== false) 64 { 65 if (is_array($name)) { 66 $temp = ''; 67 reset($name); 68 foreach ($name as $k => $v) { 69 $temp .= '[\''.$v.'\']'; 70 } 71 $name = $temp; 72 } else { 73 $name = "['$name']"; 74 } 75 $match = '/(\$'.preg_quote($this->prefix.$name,'/').')[\s]*=[\s]*(.+);/mU'; 76 77 if (preg_match($match,$this->content)) 78 { 79 $value = $this->exp_var($value); 80 $replace = '$1 = '.$value.';'; 81 82 $this->content = preg_replace($match,$replace,$this->content); 83 } 84 } 85 } 86 87 /** récupération d'une variable sans évaluation (string en sortie)*/ 88 function getVar($name) 89 { 90 if ($this->file !== false) 91 { 92 if (is_array($name)) { 93 $temp = ''; 94 reset($name); 95 foreach ($name as $k => $v) { 96 $temp .= '[\''.$v.'\']'; 97 } 98 $name = $temp; 99 } else { 100 $name = "['$name']"; 101 } 102 103 $match = '/(\$'.preg_quote($this->prefix.$name,'/').')[\s]*=[\s]*(.+);/mU'; 104 105 if (preg_match($match,$this->content,$res)) 106 { 107 return trim($res[2]); 108 } 109 } 110 return false; 111 } 112 113 /** ajouter une variable */ 114 function addVar($name, $value, $comment='') 115 { 116 if (false === $this->getVar($name)) 117 { 118 if (is_array($name)) { 119 $temp = ''; 120 reset($name); 121 foreach ($name as $k => $v) { 122 $temp .= '[\''.$v.'\']'; 123 } 124 $name = $temp; 125 } else { 126 $name = "['$name']"; 127 } 128 //the ? > at the end of the file is used as a marker 129 $string = ''; 130 if (!empty($comment)) $string = '/* '.$comment.' */'."\n"; 131 $value = $this->exp_var($value); 132 $string .= '$'.$this->prefix.$name.' = '.$value.';'."\n\n".'?>'; 133 $this->content = str_replace('?>', $string, $this->content); 134 return true; 135 } 136 return false; 137 } 138 139 140 /** sauvegarde du fichier */ 141 function saveFile() 142 { 143 if (($fp = @fopen($this->file,'w')) !== false) { 144 if (fwrite($fp,$this->content,strlen($this->content)) !== false) { 145 $res = true; 146 } else { 147 $res = false; 148 } 149 fclose($fp); 150 return $res; 151 } else { 152 return false; 153 } 154 } 155 156 157 /** exportation d'une variable */ 158 function exp_var($var) 159 { 160 if (gettype($var) == 'array') 161 { 162 $arry_res = array(); 163 foreach ($var as $k => $v) { 164 $arry_res[] = $k.' => '.$this->exp_var($v); 165 } 166 return 'array('.implode(',',$arry_res).')'; 167 } 168 elseif (gettype($var) == 'string') 169 { 170 return "'".addslashes($var)."'"; 171 } 172 elseif (gettype($var) == 'boolean') 173 { 174 return ($var) ? 'true' : 'false'; 175 } 176 else 177 { 178 return $var; 179 } 180 } 181 } 182 183 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Mon Nov 26 11:57:01 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |