| [ Index ] |
|
Code source de LifeType 1.2.4 |
1 <?php 2 3 lt_include( PLOG_CLASS_PATH."class/config/configabstractstorage.class.php" ); 4 5 if (!defined("DEFAULT_CONFIG_FILE")) { 6 define( "DEFAULT_CONFIG_FILE", PLOG_CLASS_PATH."config/config.properties.php" ); 7 } 8 9 /** 10 * \ingroup Config 11 * 12 * Implements a file-based configuration backend. The data in the file has to be arranged 13 * in a php array called $config, in the same way the file config/config.properties.php is arranged. 14 * 15 * The backend will use the array keys as its own keys and the values as its own values, and will take 16 * care of serializing and unserializing data as needed (so that we can for example save Objects and 17 * Arrays to the config file) 18 */ 19 class ConfigFileStorage extends ConfigAbstractStorage 20 { 21 22 var $_configFile; 23 var $_props; 24 25 /** 26 * Opens the configuration file. By default it is config/config.properties.php 27 * if no parameter is specified. If there is a parameter specified, that 28 * is the file the constructor will try to open. 29 * If no file name is specified, it defaults to config/config.properties.php. 30 * 31 * @param configFile The name of the file we would like to use. 32 */ 33 function ConfigFileStorage( $params = null ) 34 { 35 $this->ConfigAbstractStorage( $params ); 36 37 if( !isset($params["file"])) 38 $configFile = DEFAULT_CONFIG_FILE; 39 else 40 $configFile = $params["file"]; 41 42 $this->_configFile = $configFile; 43 44 $this->reload(); 45 } 46 47 48 /** 49 * Reloads the contents from the configuration file. 50 * 51 * @return Returns true if successul or false otherwise 52 */ 53 function reload() 54 { 55 lt_include( PLOG_CLASS_PATH."class/file/file.class.php" ); 56 lt_include( PLOG_CLASS_PATH."class/config/properties.class.php" ); 57 if( File::isReadable( $this->_configFile )) { 58 // Note: It is correct to not use lt_include() here 59 include( $this->_configFile ); 60 $this->_props = new Properties( $config ); 61 $result = true; 62 } 63 else { 64 $this->_props = new Properties(); 65 $result = false; 66 } 67 68 return( $result ); 69 } 70 71 /** 72 * Returns the name of the configuration file being used. 73 * 74 * @return The name of the configuration file being used. 75 */ 76 function getConfigFileName() 77 { 78 return $this->_configFile; 79 } 80 81 /** 82 * Create a new config file from scratch. 83 * 84 * $return true if config file could be created, false otherwise 85 */ 86 function createConfigFile( $configFileName = null ) 87 { 88 // please keep this synced to the release/config.properties.php.dist file. 89 $defaultConfigFile = '<?php 90 # 91 # database settings 92 # 93 $config[\'db_host\'] = \'\'; 94 $config[\'db_username\'] = \'\'; 95 $config[\'db_password\'] = \'\'; 96 $config[\'db_database\'] = \'\'; 97 $config[\'db_character_set\'] = \'default\'; 98 $config[\'db_persistent\'] = true; 99 # 100 # the database prefix will be appended to the name of each database tables in case you want 101 # to have more than one version of plog running at the same time, such as the stable and 102 # unstable one for testing. Each one could use a different prefix and therefore they could 103 # coexist in the same unique database. If you change this after the initial configuration done 104 # with the installation wizard, please make sure that you also rename the tables. 105 # 106 $config[\'db_prefix\'] = \'\'; 107 ?>'; 108 109 lt_include( PLOG_CLASS_PATH."class/file/file.class.php" ); 110 $file = new File( $configFileName ); 111 $writable = $file->open( 'w' ); 112 if ($writable) { 113 $file->write( $defaultConfigFile ); 114 $file->close(); 115 return true; 116 } else { 117 return false; 118 } 119 } 120 121 /** 122 * Private function that given a piece of PHP data, will return an string representing 123 * it, literally. Examples: 124 * 125 * data is a boolean type. Result --> the string 'true' 126 * data is string type. Result --> string "value_of_the_string" 127 * data is an array. Result --> string containing "Array( "..", "...", "..") " 128 * 129 * Objects are saved serialized and since there is no way to detect if it's an object 130 * or not, it will be up to the user of the class to de-serialize it. 131 * 132 * <b>:TODO:</b> This function does not handle very well sparse arrays, but it does 133 * handles arrays within arrays. 134 * 135 * @private 136 * @param data The data we'd like to get the string representation 137 * @return An string representing the data, so that eval'ing it would yield 138 * the the same result as the $data parameter. 139 */ 140 function _getDataString( $data ) 141 { 142 if( $this->_getType( $data ) == TYPE_INTEGER ) { 143 $dataString = $data; 144 } 145 elseif( $this->_getType( $data ) == TYPE_BOOLEAN ) { 146 if( $data ) 147 $dataString = "true"; 148 else 149 $dataString = "false"; 150 } 151 elseif( $this->_getType( $data ) == TYPE_STRING ) { 152 $dataString = "'".$data."'"; 153 } 154 elseif( $this->_getType( $data ) == TYPE_ARRAY ) { 155 // arrays can be recursive, so... 156 $dataString = "Array ("; 157 foreach( $data as $key => $item ) { 158 159 if( $key != "" ) { 160 if( !is_numeric($key)) { 161 $dataString .= "'".$key."' => "; 162 } 163 } 164 165 $dataString .= $this->_getDataString( $item ).","; 166 } 167 if( $dataString[strlen($dataString)-1] == "," ) 168 $dataString[strlen($dataString)-1] = ")"; 169 else 170 $dataString .= ")"; 171 } 172 elseif( $this->_getType( $data ) == TYPE_OBJECT ) { 173 $dataString = serialize( $data ); 174 } 175 176 return $dataString; 177 } 178 179 /** 180 * Saves a setting to the configuration file. If the setting already exists, the current 181 * value is overwritten. Otherwise, it will be appended in the end of the file. 182 * <b>NOTE:</b> This method is highly unoptimized because every time that we call saveValue, 183 * we are writing the whole file to disk... Bad ;) But it works, so we'll leave it as it 184 * is for the time being... 185 * 186 * @param name Name of the setting. 187 * @param value Value of the setting. 188 * @return True if success or false otherwise. 189 */ 190 function saveValue( $name, $value ) 191 { 192 // open the config file 193 lt_include( PLOG_CLASS_PATH."class/file/file.class.php" ); 194 $f = new File( $this->_configFile ); 195 196 // there was a problem opening the file 197 if( !$f->open("r+")) 198 return false; 199 200 // now we have to process each of the lines 201 $contents = $f->readFile(); 202 203 // escape the value 204 $value = str_replace( "'", "\'", $value ); 205 206 $i = 0; 207 $result = Array(); 208 209 $valueString = $this->_getDataString( $value ); 210 211 // depending if it's a string or not, we need a different regexp and a 212 // expression that will replace the original 213 if( $this->_getType( $value ) == TYPE_STRING ) { 214 $regexp = "/ *\\\$config\[[\"']".$name."[\"']\] *= *[\"'](.*)[\"']; */"; 215 $replaceWith = "\$config['".$name."'] = ".$valueString.";"; 216 } 217 else { 218 $regexp = "/ *\\\$config\[[\"']".$name."[\"']\] *= *(.*); */"; 219 $replaceWith = "\$config['".$name."'] = ".$valueString.";"; 220 } 221 222 while( $i < count($contents)) { 223 $line = $contents[$i]; 224 225 $newline = preg_replace($regexp, $replaceWith, $line ); 226 $i++; 227 228 229 // for some reason, looks like we're getting some garbage in the end 230 // of the file... couldn't find any better way to do it 231 if( $newline != "?>" ) { 232 $newline .= "\n"; 233 array_push($result, $newline ); 234 } 235 else { 236 array_push( $result, "?>"); 237 break; 238 } 239 } 240 241 // the only thing we have to do know is save the contents of $result 242 // to the output file 243 //$result = str_replace( "'", "\\'", $result ); 244 $f->writeLines( $result ); 245 246 return true; 247 } 248 249 function getValue( $key, $defaultValue = null ) 250 { 251 $value = $this->_props->getValue( $key ); 252 if( $value == "" || $value == null ) 253 if(isset($defaultValue)) 254 $value = $defaultValue; 255 256 return $value; 257 } 258 259 /** 260 * @see ConfigAbstractStorage::setValue() 261 */ 262 function setValue( $key, $value ) 263 { 264 return $this->_props->setValue( $key, $value ); 265 } 266 267 /** 268 * @see ConfigAbstractStorage::getKeys() 269 */ 270 function getKeys() 271 { 272 return $this->_props->getKeys(); 273 } 274 275 /** 276 * @see ConfigAbstractStorage::getValues() 277 */ 278 function getValues() 279 { 280 return $this->_props->getValues(); 281 } 282 283 /** 284 * @see ConfigAbstractStorage::getAsArray() 285 */ 286 function getAsArray() 287 { 288 return $this->_props->getAsArray(); 289 } 290 291 /** 292 * @see ConfigAbstractStorage::save() 293 */ 294 function save() 295 { 296 foreach( $this->_props->getAsArray() as $key => $value ) { 297 $this->saveValue( $key, $value ); 298 } 299 300 return true; 301 } 302 303 /** 304 * shortcut for one of the most sought after config keys: temp_folder 305 * 306 * not really needed, but it makes my life easier since I never remember 307 * whether it is tmp_folder, temp_folder, temp_dir, or whatever :) 308 * 309 * @return The name of the folder used for temporary storage 310 */ 311 function getTempFolder() 312 { 313 return $this->getValue( "temp_folder" ); 314 } 315 316 } 317 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Mon Nov 26 21:04:15 2007 | par Balluche grâce à PHPXref 0.7 |
|