[6109] | 1 | <?php |
---|
| 2 | /* ----------------------------------------------------------------------------- |
---|
| 3 | Theme : Gally/default |
---|
| 4 | Author : Grum |
---|
| 5 | email : grum@piwigo.org |
---|
| 6 | website : http://photos.grum.fr |
---|
| 7 | |
---|
| 8 | << May the Little SpaceFrog be with you ! >> |
---|
| 9 | ------------------------------------------------------------------------------ |
---|
| 10 | |
---|
| 11 | This is the class for read&write the conf files |
---|
| 12 | |
---|
| 13 | --------------------------------------------------------------------------- */ |
---|
| 14 | |
---|
| 15 | |
---|
| 16 | class Conf |
---|
| 17 | { |
---|
| 18 | protected $fileName=""; |
---|
| 19 | protected $values=Array(); |
---|
| 20 | |
---|
| 21 | public function __construct($fileName="") |
---|
| 22 | { |
---|
| 23 | $this->setFileName($fileName); |
---|
| 24 | } |
---|
| 25 | |
---|
| 26 | public function __destruct() |
---|
| 27 | { |
---|
| 28 | unset($this->values); |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | public function read($reset=true) |
---|
| 32 | { |
---|
| 33 | if(file_exists($this->fileName) and (filesize($this->fileName)>0)) |
---|
| 34 | { |
---|
| 35 | $fHandle=fopen($this->fileName, "r"); |
---|
| 36 | if($fHandle) |
---|
| 37 | { |
---|
| 38 | $fileContent=fread($fHandle, filesize($this->fileName)); |
---|
| 39 | fclose($fHandle); |
---|
| 40 | |
---|
| 41 | //replace all the '#' char by a ';' char |
---|
| 42 | $fileContent=preg_replace('/^(\s*#)/im', ';', $fileContent); |
---|
| 43 | |
---|
| 44 | $tmpFile=tempnam(dirname(__FILE__), "INI"); |
---|
| 45 | $fHandle=fopen($tmpFile, "w"); |
---|
| 46 | fwrite($fHandle, $fileContent); |
---|
| 47 | fclose($fHandle); |
---|
| 48 | |
---|
| 49 | if($reset) |
---|
| 50 | { |
---|
| 51 | unset($this->values); |
---|
| 52 | $this->values=Array(); |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | $values=parse_ini_file($tmpFile); |
---|
| 56 | foreach($values as $key => $val) |
---|
| 57 | { |
---|
| 58 | $this->values[$key] = html_entity_decode($val); |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | unlink($tmpFile); |
---|
| 62 | |
---|
| 63 | return(true); |
---|
| 64 | } |
---|
| 65 | } |
---|
| 66 | return(false); |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | public function write() |
---|
| 70 | { |
---|
| 71 | if(!file_exists(dirname($this->fileName))) |
---|
| 72 | { |
---|
| 73 | mkdir(dirname($this->fileName),0777,true); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | $fHandle=fopen($this->fileName, "w"); |
---|
| 77 | if($fHandle) |
---|
| 78 | { |
---|
| 79 | foreach($this->values as $key => $val) |
---|
| 80 | { |
---|
| 81 | if(is_string($val)) |
---|
| 82 | { |
---|
| 83 | fwrite($fHandle, $key.'="'.htmlentities(str_replace(Array("\r\n", "\n", "\r"),"",$val))."\"\n"); |
---|
| 84 | } |
---|
| 85 | elseif(is_bool($val)) |
---|
| 86 | { |
---|
| 87 | fwrite($fHandle, $key.'='.($val?'true':'false')."\n"); |
---|
| 88 | } |
---|
| 89 | else |
---|
| 90 | { |
---|
| 91 | fwrite($fHandle, $key.'='.$val."\n"); |
---|
| 92 | } |
---|
| 93 | } |
---|
| 94 | fclose($fHandle); |
---|
| 95 | return(true); |
---|
| 96 | } |
---|
| 97 | return(false); |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | public function setFileName($fileName) |
---|
| 101 | { |
---|
| 102 | if($fileName!="") |
---|
| 103 | { |
---|
| 104 | $this->fileName=$fileName; |
---|
| 105 | } |
---|
| 106 | return($this->fileName); |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | public function getFileName($fileName) |
---|
| 110 | { |
---|
| 111 | return($this->fileName); |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | public function getConf() |
---|
| 115 | { |
---|
| 116 | return($this->values); |
---|
| 117 | } |
---|
| 118 | |
---|
[8528] | 119 | public function confExists($key) |
---|
| 120 | { |
---|
| 121 | return(array_key_exists($key, $this->values)); |
---|
| 122 | } |
---|
| 123 | |
---|
[6109] | 124 | public function getConfValue($key) |
---|
| 125 | { |
---|
| 126 | if(array_key_exists($key, $this->values)) |
---|
| 127 | { |
---|
| 128 | return($this->values[$key]); |
---|
| 129 | } |
---|
| 130 | return(""); |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | public function setConf($values, $reset=true) |
---|
| 134 | { |
---|
| 135 | if(is_array($values)) |
---|
| 136 | { |
---|
| 137 | if($reset) |
---|
| 138 | { |
---|
| 139 | unset($this->values); |
---|
| 140 | $this->values=Array(); |
---|
| 141 | } |
---|
| 142 | foreach($values as $key => $val) |
---|
| 143 | { |
---|
| 144 | $this->values[$key]=$val; |
---|
| 145 | } |
---|
| 146 | } |
---|
| 147 | return($this->values); |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | |
---|
[8528] | 151 | /** |
---|
| 152 | * function used to initialize config file |
---|
| 153 | * |
---|
| 154 | */ |
---|
| 155 | static public function init($theme) |
---|
| 156 | { |
---|
| 157 | $forcedToDefault=array( |
---|
| 158 | "imageCenterMode" => '', |
---|
| 159 | "imageCenterOffset" => '', |
---|
| 160 | "imageCenterTopMin" => '', |
---|
| 161 | "imageCenterTopBorder" => '', |
---|
| 162 | "interfaceAnimated" => '', |
---|
| 163 | "commentAnimated" => '', |
---|
| 164 | "animateDelay" => '', |
---|
| 165 | "marginContainer" => '', |
---|
| 166 | "paddingContainer" => '', |
---|
| 167 | "tabsAnimated" => '', |
---|
| 168 | "tabsHidden" => '', |
---|
| 169 | "tabsVisible" => '', |
---|
| 170 | "tabsPosition" => '', |
---|
| 171 | "commentRows" => '', |
---|
| 172 | "menuAnimated" => '', |
---|
| 173 | "menuWidth" => '', |
---|
| 174 | "menuMaxWidth" => '', |
---|
| 175 | "menuMSIEMaxWidth" => '', |
---|
| 176 | "imageSimulateHighRes" => '', |
---|
| 177 | "imageScrollMinDeadArea" => '' |
---|
| 178 | //"expandMenu" => '', |
---|
| 179 | ); |
---|
| 180 | $gallyDefault = new Conf(); |
---|
| 181 | $gallyDefault->setFileName(PHPWG_ROOT_PATH."themes/gally-default/conf/default.conf"); |
---|
| 182 | $gallyDefault->read(); |
---|
| 183 | |
---|
| 184 | $configDefault = new Conf(); |
---|
| 185 | $configDefault->setFileName(PHPWG_ROOT_PATH."themes/$theme/conf/default.conf"); |
---|
| 186 | $configDefault->read(); |
---|
| 187 | |
---|
| 188 | $configLocal = new Conf(); |
---|
| 189 | $configLocal->setFileName(PHPWG_ROOT_PATH."local/themes/$theme/conf/local.conf"); |
---|
| 190 | $configLocal->read(); |
---|
| 191 | |
---|
| 192 | foreach($forcedToDefault as $key => $param) |
---|
| 193 | { |
---|
| 194 | if($gallyDefault->confExists($key)) $forcedToDefault[$key]=$gallyDefault->getConfValue($key); |
---|
| 195 | if($configDefault->confExists($key)) $forcedToDefault[$key]=$configDefault->getConfValue($key); |
---|
| 196 | } |
---|
| 197 | $configLocal->setConf($forcedToDefault, false); |
---|
| 198 | $configLocal->write(); |
---|
| 199 | } |
---|
| 200 | |
---|
[6109] | 201 | } |
---|
| 202 | |
---|
| 203 | |
---|
| 204 | ?> |
---|