1 <?php 2 /* Copyright (c) 2012, Geert Bergman (geert@scrivo.nl) 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 3. Neither the name of "Scrivo" nor the names of its contributors may be 14 * used to endorse or promote products derived from this software without 15 * specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 * POSSIBILITY OF SUCH DAMAGE. 28 * 29 * $Id: Config.php 866 2013-08-25 16:22:35Z geert $ 30 */ 31 32 /** 33 * Implementation of the \Scrivo\Config class. 34 */ 35 36 namespace Scrivo; 37 38 /** 39 * Class to hold Scrivo configuration data. 40 * 41 * Scrivo configuration data is stored in instances of this class. Scrivo 42 * configuration data comes from two different sources: 43 * 44 * * Default configuration data: entries that always exist and which have 45 * values that can be overwritten by entries in the Scrivo configuration 46 * file. 47 * * Optional configuration data: all entries in the Scrivo configuration 48 * file that are no part of the default set will also become members of the 49 * configuration class. 50 * 51 * There are also id-label pairs: certain id's can be labbelled in the database, 52 * this configuration data can be accessed through the class \Scrivo\IdLabel. 53 * 54 * When instantiating this class it will search up the directory tree for a 55 * Scrivo config file named ".htscrivo". Alternatively you can specify a file 56 * when instantiating a configuration data object. 57 * 58 * Note that only string and numeric values can be specified. These will be 59 * converted to a \Scrivo\String, int or float value. It is not possible 60 * to specify boolean values due to limitations of the php function 61 * parse_ini_file. So also note that specifing values like true, false, yes, 62 * no, on and off without quotes might not give you the expected results. 63 * When you need boolean values it is preferred to use the numeric values 0 64 * or 1. 65 * 66 * @property-read \Scrivo\String $ADMIN_IP_ADDRESSES 67 * @property-read \Scrivo\String $CACHE_DIR 68 * @property-read int $CACHE_DIR_GC 69 * @property-read \Scrivo\String $CACHE_TYPE 70 * @property-read \Scrivo\String $DB_API 71 * @property-read int $HTML_TIDY 72 * @property-read \Scrivo\String $HTTP_PROTOCOL 73 * @property-read int $JS_DEBUG 74 * @property-read \Scrivo\String $KEY_PREFIX 75 * @property-read int $ROOT_PAGE_ID 76 * @property-read int $ROOT_FOLDER_ID 77 * @property-read \Scrivo\String $SESSION_PREFIX 78 * @property-read \Scrivo\String $UI_LANG 79 * @property-read \Scrivo\String $WEBSERVICE_SPELL 80 * @property-read \Scrivo\String $WEBSERVICE_TIDY 81 */ 82 class Config { 83 84 /** 85 * Array to hold all data from the config file. 86 * @var array 87 */ 88 protected $ini; 89 90 /** 91 * Convert a string read from an ini file to its most likely type. 92 * 93 * @param string $val The value read from the ini file. 94 * 95 * @return int|float|\Scrivo\String The given value converted to its 96 * most likely type. 97 */ 98 private function convertStr(&$val) { 99 if (is_numeric($val)) { 100 if ((string)$val === (string)(int)$val) { 101 $val = intval($val); 102 } else { 103 $val = floatval($val); 104 } 105 } else { 106 $val = new \Scrivo\String($val); 107 } 108 } 109 110 /** 111 * Try to find a Scrivo config file higher up the directory tree. 112 * 113 * @param string $cfgFile The name of the configuration file to locate. 114 * 115 * @return string A Scrivo configuration file name. 116 */ 117 private function findConfigFile($cfgFile) { 118 $parts = \Scrivo\String::create($_SERVER["SCRIPT_FILENAME"])->split( 119 new \Scrivo\String(DIRECTORY_SEPARATOR)); 120 for ($i = count($parts)-2; $i >=0; $i--) { 121 array_pop($parts); 122 $file = implode("/", $parts)."/$cfgFile"; 123 if (file_exists($file)) { 124 return($file); 125 } 126 } 127 throw new \Scrivo\SystemException("Could not locate config file"); 128 } 129 130 /** 131 * Create a configuration data object. As input the constructor will 132 * look for an Scrivo config file at expected locations or at an 133 * alternative location if provided. 134 * 135 * @param \Scrivo\String If you don't want/can't use the standard Scrivo 136 * configuration file you can provide the location of an alternative file. 137 */ 138 public function __construct(\Scrivo\String $path=null) { 139 \Scrivo\ArgumentCheck::assertArgs(func_get_args(), array(null), 0); 140 141 // Read the ini file. 142 if (!$path) { 143 $path = $this->findConfigFile(".htscrivo"); 144 } 145 try { 146 $this->ini = parse_ini_file((string)$path); 147 } catch (\Exception $e) { 148 throw new \Scrivo\SystemException( 149 "Invalid config file: ".$e->getMessage()); 150 } 151 152 // The basic set of properties and their default values. 153 $def = array( 154 "ADMIN_IP_ADDRESSES" => "127.0.0.1", 155 "CACHE_DIR" => "", 156 "CACHE_DIR_GC" => 0, 157 "CACHE_TYPE" => function_exists("apc_fetch") ? "APC" : "", 158 "DB_API" => "mysql", 159 "HTML_TIDY" => 0, 160 "HTTP_PROTOCOL" => "http://", 161 "JS_DEBUG" => 0, 162 "KEY_PREFIX" => strtoupper(preg_replace("/[^a-zA_Z0-9]+/", "_", 163 isset($this->ini["WWW_ROOT"])?$this->ini["WWW_ROOT"]:""))."_", 164 "ROOT_PAGE_ID" => "1", 165 "ROOT_FOLDER_ID" => "2", 166 "SESSION_PREFIX" => 167 (isset($this->ini["DB_NAME"])?$this->ini["DB_NAME"]:"")."_". 168 (isset($this->ini["INSTANCE_ID"])?$this->ini["INSTANCE_ID"]:""). 169 "_", 170 "UI_LANG" => "en_US", 171 "WEBSERVICE_SPELL" => "http://www.scrivo.nl/spell/spell.php", 172 "WEBSERVICE_TIDY" => "http://www.scrivo.nl/tidy/tidy.php", 173 ); 174 175 // Set missing entries to the default values. 176 foreach ($def as $d=>$v) { 177 if (!isset($this->ini[$d])) { 178 $this->ini[$d] = $v; 179 } 180 } 181 182 // Rename legacy key names 183 if (!isset($this->ini["DB_HOST"]) && isset($this->ini["DB_SERVER"])) { 184 $this->ini["DB_HOST"] = $this->ini["DB_SERVER"]; 185 } 186 if (!isset($this->ini["DB_USER"]) && isset($this->ini["DB_USERCODE"])) { 187 $this->ini["DB_USER"] = $this->ini["DB_USERCODE"]; 188 } 189 190 // Convert types 191 array_walk($this->ini, array($this, 'convertStr')); 192 } 193 194 /** 195 * Test if a particular property was set. 196 * 197 * @param string $name The name of the configuration property to test. 198 * 199 * @return boolean True if the property was set, false if not. 200 */ 201 public function __isset($name) { 202 return isset($this->ini[$name]); 203 } 204 205 /** 206 * Accessor method for configuration data members. 207 * 208 * @param string $name The configuration property name to retrieve for 209 * which to retrieve it's value. 210 * 211 * @return \Scrivo\String|float|int The value of the property $name. 212 */ 213 public function __get($name) { 214 if (!$this->__isset($name)) { 215 throw new \Scrivo\SystemException( 216 "Invalid configuration property '$name'"); 217 } 218 return $this->ini[$name]; 219 } 220 221 } 222 223 ?> 224
Documentation generated by phpDocumentor 2.0.0a12 and ScrivoDocumentor on August 29, 2013