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 | |
---|
119 | public function getConfValue($key) |
---|
120 | { |
---|
121 | if(array_key_exists($key, $this->values)) |
---|
122 | { |
---|
123 | return($this->values[$key]); |
---|
124 | } |
---|
125 | return(""); |
---|
126 | } |
---|
127 | |
---|
128 | public function setConf($values, $reset=true) |
---|
129 | { |
---|
130 | if(is_array($values)) |
---|
131 | { |
---|
132 | if($reset) |
---|
133 | { |
---|
134 | unset($this->values); |
---|
135 | $this->values=Array(); |
---|
136 | } |
---|
137 | foreach($values as $key => $val) |
---|
138 | { |
---|
139 | $this->values[$key]=$val; |
---|
140 | } |
---|
141 | } |
---|
142 | return($this->values); |
---|
143 | } |
---|
144 | |
---|
145 | |
---|
146 | } |
---|
147 | |
---|
148 | |
---|
149 | ?> |
---|