Rev 229 Rev 236
1 <?php 1 <?php
2   2  
3 /** 3 /**
4 * IO class. 4 * IO class.
5 * @license http://opensource.org/licenses/gpl-license.php GNU General Public License 5 * @license http://opensource.org/licenses/gpl-license.php GNU General Public License
6 * @copyright (c)2003-2005 Tamlyn Rhodes 6 * @copyright (c)2003-2005 Tamlyn Rhodes
7 * @version $Id: io_csv.class.php,v 1.34 2006/06/25 00:13:56 tamlyn Exp $ 7 * @version $Id: io_csv.class.php,v 1.34 2006/06/25 00:13:56 tamlyn Exp $
8 */ 8 */
9   9  
10 //include the base IO class 10 //include the base IO class
11 require_once dirname(__FILE__)."/io.class.php"; 11 require_once dirname(__FILE__)."/io.class.php";
12 12
13 /** 13 /**
14 * Class used to read and write data to and from CSV files. 14 * Class used to read and write data to and from CSV files.
15 * @see sgIO_iifn 15 * @see sgIO_iifn
16 * @package singapore 16 * @package singapore
17 * @author Tamlyn Rhodes <tam at zenology dot co dot uk> 17 * @author Tamlyn Rhodes <tam at zenology dot co dot uk>
18 * @copyright (c)2003, 2004 Tamlyn Rhodes 18 * @copyright (c)2003, 2004 Tamlyn Rhodes
19 */ 19 */
20 class sgIO_csv extends sgIO 20 class sgIO_csv extends sgIO
21 { 21 {
22 //constructor provided by parent class 22 //constructor provided by parent class
23 23
24 /** 24 /**
25 * Name of IO backend. 25 * Name of IO backend.
26 */ 26 */
27 function getName() 27 function getName()
28 { 28 {
29 return "CSV"; 29 return "CSV";
30 } 30 }
31   31  
32 /** 32 /**
33 * Version of IO backend. 33 * Version of IO backend.
34 */ 34 */
35 function getVersion() 35 function getVersion()
36 { 36 {
37 return "$Revision: 1.34 $"; 37 return "$Revision: 1.34 $";
38 } 38 }
39   39  
40 /** 40 /**
41 * Author of IO backend. 41 * Author of IO backend.
42 */ 42 */
43 function getAuthor() 43 function getAuthor()
44 { 44 {
45 return "Tamlyn Rhodes"; 45 return "Tamlyn Rhodes";
46 } 46 }
47   47  
48 /** 48 /**
49 * Brief description of IO backend and it's requirements. 49 * Brief description of IO backend and it's requirements.
50 */ 50 */
51 function getDescription() 51 function getDescription()
52 { 52 {
53 return "Uses comma separated value files. Does not require a database."; 53 return "Uses comma separated value files. Does not require a database.";
54 } 54 }
55   55  
56 /** 56 /**
57 * Fetches gallery info for the specified gallery and immediate children. 57 * Fetches gallery info for the specified gallery and immediate children.
58 * @param string gallery id 58 * @param string gallery id
59 * @param string language code spec for this request (optional) 59 * @param string language code spec for this request (optional)
60 * @param int number of levels of child galleries to fetch (optional) 60 * @param int number of levels of child galleries to fetch (optional)
61 * @return sgGallery the gallery object created 61 * @return sgGallery the gallery object created
62 */ 62 */
63 function &getGallery($galleryId, &$parent, $getChildGalleries = 1, $language = null) 63 function &getGallery($galleryId, &$parent, $getChildGalleries = 1, $language = null)
64 { 64 {
65 $gal =& new sgGallery($galleryId, $parent); 65 $gal =& new sgGallery($galleryId, $parent);
66   66  
67 if($language == null) { 67 if($language == null) {
68 $translator =& Translator::getInstance(); 68 $translator =& Translator::getInstance();
69 $language = $translator->language; 69 $language = $translator->language;
70 } 70 }
71 71
72 //try to open language specific metadata 72 //try to open language specific metadata
73 $fp = @fopen($this->config->base_path.$this->config->pathto_galleries.$galleryId."/metadata.$language.csv","r"); 73 $fp = @fopen($this->config->base_path.$this->config->pathto_galleries.$galleryId."/metadata.$language.csv","r");
74 74
75 //if fail then try to open generic metadata 75 //if fail then try to open generic metadata
76 if(!$fp) 76 if(!$fp)
77 $fp = @fopen($this->config->base_path.$this->config->pathto_galleries.$galleryId."/metadata.csv","r"); 77 $fp = @fopen($this->config->base_path.$this->config->pathto_galleries.$galleryId."/metadata.csv","r");
78 if($fp) { 78 if($fp) {
79   79  
80 while($temp[] = fgetcsv($fp,2048)); 80 while($temp[] = fgetcsv($fp,2048));
81 fclose($fp); 81 fclose($fp);
82 82
83 list( 83 list(
84 $gal->filename, 84 $gal->filename,
85 $gal->thumbnail, 85 $gal->thumbnail,
86 $gal->owner, 86 $gal->owner,
87 $gal->groups, 87 $gal->groups,
88 $gal->permissions, 88 $gal->permissions,
89 $gal->categories, 89 $gal->categories,
90 $gal->name, 90 $gal->name,
91 $gal->artist, 91 $gal->artist,
92 $gal->email, 92 $gal->email,
93 $gal->copyright, 93 $gal->copyright,
94 $gal->desc, 94 $gal->desc,
95 $gal->summary, 95 $gal->summary,
96 $gal->date 96 $gal->date
97 ) = $temp[1]; 97 ) = $temp[1];
98 98
99 99
100 //only fetch individual images if child galleries are required 100 //only fetch individual images if child galleries are required
101 if($getChildGalleries) { 101 if($getChildGalleries) {
102 for($i=0;$i<count($temp)-3;$i++) { 102 for($i=0;$i<count($temp)-3;$i++) {
103 $gal->images[$i] =& new sgImage($temp[$i+2][0], $gal, $this->config); 103 $gal->images[$i] =& new sgImage($temp[$i+2][0], $gal, $this->config);
104 list(, 104 list(,
105 $gal->images[$i]->thumbnail, 105 $gal->images[$i]->thumbnail,
106 $gal->images[$i]->owner, 106 $gal->images[$i]->owner,
107 $gal->images[$i]->groups, 107 $gal->images[$i]->groups,
108 $gal->images[$i]->permissions, 108 $gal->images[$i]->permissions,
109 $gal->images[$i]->categories, 109 $gal->images[$i]->categories,
110 $gal->images[$i]->name, 110 $gal->images[$i]->name,
111 $gal->images[$i]->artist, 111 $gal->images[$i]->artist,
112 $gal->images[$i]->email, 112 $gal->images[$i]->email,
113 $gal->images[$i]->copyright, 113 $gal->images[$i]->copyright,
114 $gal->images[$i]->desc, 114 $gal->images[$i]->desc,
115 $gal->images[$i]->location, 115 $gal->images[$i]->location,
116 $gal->images[$i]->date, 116 $gal->images[$i]->date,
117 $gal->images[$i]->camera, 117 $gal->images[$i]->camera,
118 $gal->images[$i]->lens, 118 $gal->images[$i]->lens,
119 $gal->images[$i]->film, 119 $gal->images[$i]->film,
120 $gal->images[$i]->darkroom, 120 $gal->images[$i]->darkroom,
121 $gal->images[$i]->digital 121 $gal->images[$i]->digital
122 ) = $temp[$i+2]; 122 ) = $temp[$i+2];
123 123
124 //get image size and type 124 //get image size and type
125 list( 125 list(
126 $gal->images[$i]->width, 126 $gal->images[$i]->width,
127 $gal->images[$i]->height, 127 $gal->images[$i]->height,
128 $gal->images[$i]->type 128 $gal->images[$i]->type
129 ) = @GetImageSize($gal->images[$i]->realPath()); 129 ) = @GetImageSize($gal->images[$i]->realPath());
130 } 130 }
131 //otherwise just fill in empty images 131 //otherwise just fill in empty images
132 } else if(count($temp) > 3) { 132 } else if(count($temp) > 3) {
133 for($i=0;$i<count($temp)-3;$i++) 133 for($i=0;$i<count($temp)-3;$i++)
134 $gal->images[$i] =& new sgImage($temp[$i+2][0], $gal); 134 $gal->images[$i] =& new sgImage($temp[$i+2][0], $gal);
135 } 135 }
136 136
137 } else 137 } else
138 //no metadata found so use iifn method implemented in superclass 138 //no metadata found so use iifn method implemented in superclass
139 return parent::getGallery($galleryId, $parent, $getChildGalleries, $language); 139 return parent::getGallery($galleryId, $parent, $getChildGalleries, $language);
140 140
141 //discover child galleries 141 //discover child galleries
142 $dir = Singapore::getListing($this->config->base_path.$this->config->pathto_galleries.$galleryId."/"); 142 $dir = Singapore::getListing($this->config->base_path.$this->config->pathto_galleries.$galleryId."/");
143 if($getChildGalleries) 143 if($getChildGalleries)
144 //but only fetch their info if required too 144 //but only fetch their info if required too
145 foreach($dir->dirs as $gallery) 145 foreach($dir->dirs as $gallery)
146 $gal->galleries[] = $this->getGallery($galleryId."/".$gallery, $gal, $getChildGalleries-1, $language); 146 $gal->galleries[] = $this->getGallery($galleryId."/".$gallery, $gal, $getChildGalleries-1, $language);
147 else 147 else
148 //otherwise just copy their names in so they can be counted 148 //otherwise just copy their names in so they can be counted
149 $gal->galleries = $dir->dirs; 149 $gal->galleries = $dir->dirs;
150 150
151 return $gal; 151 return $gal;
152 } 152 }
153 153
154 /** 154 /**
155 * Stores gallery information. 155 * Stores gallery information.
156 * @param sgGallery instance of gallery object to be stored 156 * @param sgGallery instance of gallery object to be stored
157 */ 157 */
158 function putGallery($gal) { 158 function putGallery($gal) {
159 $dataFile = $this->config->base_path.$this->config->pathto_galleries.$gal->id."/metadata.csv"; 159 $dataFile = $this->config->base_path.$this->config->pathto_galleries.$gal->id."/metadata.csv";
160 @chmod($dataFile, octdec($this->config->file_mode)); 160 @chmod($dataFile, octdec($this->config->file_mode));
161 $fp = @fopen($dataFile,"w"); 161 $fp = @fopen($dataFile,"w");
162 if(!$fp) 162 if(!$fp)
163 return false; 163 return false;
164 164
165 $success = (bool) fwrite($fp,"filename,thumbnail,owner,group(s),permissions,catergories,image name,artist name,artist email,copyright,image description,image location,date taken,camera info,lens info,film info,darkroom manipulation,digital manipulation"); 165 $success = (bool) fwrite($fp,"filename,thumbnail,owner,group(s),permissions,catergories,image name,artist name,artist email,copyright,image description,image location,date taken,camera info,lens info,film info,darkroom manipulation,digital manipulation");
166 $success &= (bool) fwrite($fp,"\n\"". 166 $success &= (bool) fwrite($fp,"\n\"".
167 $gal->filename.'",,'. 167 $gal->filename.'",,'.
168 $gal->owner.','. 168 $gal->owner.','.
169 $gal->groups.','. 169 $gal->groups.','.
170 $gal->permissions.','. 170 $gal->permissions.','.
171 $gal->categories.',"'. 171 $gal->categories.',"'.
172 str_replace('"','""',$gal->name).'","'. 172 str_replace('"','""',$gal->name).'","'.
173 str_replace('"','""',$gal->artist).'","'. 173 str_replace('"','""',$gal->artist).'","'.
174 str_replace('"','""',$gal->email).'","'. 174 str_replace('"','""',$gal->email).'","'.
175 str_replace('"','""',$gal->copyright).'","'. 175 str_replace('"','""',$gal->copyright).'","'.
176 str_replace('"','""',$gal->desc).'","'. 176 str_replace('"','""',$gal->desc).'","'.
177 str_replace('"','""',$gal->summary).'","'. 177 str_replace('"','""',$gal->summary).'","'.
178 str_replace('"','""',$gal->date).'"' 178 str_replace('"','""',$gal->date).'"'
179 ); 179 );
180 180
181 for($i=0;$i<count($gal->images);$i++) 181 for($i=0;$i<count($gal->images);$i++)
182 $success &= (bool) fwrite($fp,"\n\"". 182 $success &= (bool) fwrite($fp,"\n\"".
183 $gal->images[$i]->id.'",,'. 183 $gal->images[$i]->id.'",,'.
184 //$gal->images[$i]->thumbnail.','. 184 //$gal->images[$i]->thumbnail.','.
185 $gal->images[$i]->owner.','. 185 $gal->images[$i]->owner.','.
186 $gal->images[$i]->groups.','. 186 $gal->images[$i]->groups.','.
187 $gal->images[$i]->permissions.','. 187 $gal->images[$i]->permissions.','.
188 $gal->images[$i]->categories.',"'. 188 $gal->images[$i]->categories.',"'.
189 str_replace('"','""',$gal->images[$i]->name).'","'. 189 str_replace('"','""',$gal->images[$i]->name).'","'.
190 str_replace('"','""',$gal->images[$i]->artist).'","'. 190 str_replace('"','""',$gal->images[$i]->artist).'","'.
191 str_replace('"','""',$gal->images[$i]->email).'","'. 191 str_replace('"','""',$gal->images[$i]->email).'","'.
192 str_replace('"','""',$gal->images[$i]->copyright).'","'. 192 str_replace('"','""',$gal->images[$i]->copyright).'","'.
193 str_replace('"','""',$gal->images[$i]->desc).'","'. 193 str_replace('"','""',$gal->images[$i]->desc).'","'.
194 str_replace('"','""',$gal->images[$i]->location).'","'. 194 str_replace('"','""',$gal->images[$i]->location).'","'.
195 str_replace('"','""',$gal->images[$i]->date).'","'. 195 str_replace('"','""',$gal->images[$i]->date).'","'.
196 str_replace('"','""',$gal->images[$i]->camera).'","'. 196 str_replace('"','""',$gal->images[$i]->camera).'","'.
197 str_replace('"','""',$gal->images[$i]->lens).'","'. 197 str_replace('"','""',$gal->images[$i]->lens).'","'.
198 str_replace('"','""',$gal->images[$i]->film).'","'. 198 str_replace('"','""',$gal->images[$i]->film).'","'.
199 str_replace('"','""',$gal->images[$i]->darkroom).'","'. 199 str_replace('"','""',$gal->images[$i]->darkroom).'","'.
200 str_replace('"','""',$gal->images[$i]->digital).'"' 200 str_replace('"','""',$gal->images[$i]->digital).'"'
201 ); 201 );
202 $success &= (bool) fclose($fp); 202 $success &= (bool) fclose($fp);
203 203
204 return $success; 204 return $success;
205 } 205 }
206 206
207 /** 207 /**
208 * Fetches hit data from file. 208 * Fetches hit data from file.
209 * @param sgGallery gallery object to load hits into 209 * @param sgGallery gallery object to load hits into
210 */ 210 */
211 function getHits(&$gal) { 211 function getHits(&$gal) {
212 212
213 $fp = @fopen($this->config->base_path.$this->config->pathto_galleries.$gal->id."/hits.csv","r"); 213 $fp = @fopen($this->config->base_path.$this->config->pathto_galleries.$gal->id."/hits.csv","r");
214 214
215 if($fp) { 215 if($fp) {
216 flock($fp, LOCK_SH); 216 flock($fp, LOCK_SH);
217 while($temp[] = fgetcsv($fp,255)); 217 while($temp[] = fgetcsv($fp,255));
218 flock($fp, LOCK_UN); 218 flock($fp, LOCK_UN);
219 fclose($fp); 219 fclose($fp);
220 } else $temp = array(); 220 } else $temp = array();
221 221
222 if(isset($temp[0])) 222 if(isset($temp[0]))
223 list( 223 list(
224 , 224 ,
225 $gal->hits, 225 $gal->hits,
226 $gal->lasthit 226 $gal->lasthit
227 ) = $temp[0]; 227 ) = $temp[0];
228 228
229 for($i=0;$i<count($temp)-2;$i++) { 229 for($i=0;$i<count($temp)-2;$i++) {
230 if(isset($gal->images[$i]) && $temp[$i+1][0] == $gal->images[$i]->id) 230 if(isset($gal->images[$i]) && $temp[$i+1][0] == $gal->images[$i]->id)
231 list( 231 list(
232 , 232 ,
233 $gal->images[$i]->hits, 233 $gal->images[$i]->hits,
234 $gal->images[$i]->lasthit 234 $gal->images[$i]->lasthit
235 ) = $temp[$i+1]; 235 ) = $temp[$i+1];
236 else 236 else
237 foreach($gal->images as $key => $img) 237 foreach($gal->images as $key => $img)
238 if($temp[$i+1][0] == $img->id) 238 if($temp[$i+1][0] == $img->id)
239 list( 239 list(
240 , 240 ,
241 $gal->images[$key]->hits, 241 $gal->images[$key]->hits,
242 $gal->images[$key]->lasthit 242 $gal->images[$key]->lasthit
243 ) = $temp[$i+1]; 243 ) = $temp[$i+1];
244 244
245 } 245 }
246 246
247 return true; 247 return true;
248 } 248 }
249 249
250 /** 250 /**
251 * Stores gallery hits. 251 * Stores gallery hits.
252 * @param sgGallery gallery object to store 252 * @param sgGallery gallery object to store
253 */ 253 */
254 function putHits($gal) { 254 function putHits($gal) {
255 $logfile = $this->config->base_path.$this->config->pathto_galleries.$gal->id."/hits.csv"; 255 $logfile = $this->config->base_path.$this->config->pathto_galleries.$gal->id."/hits.csv";
256 if(!file_exists($logfile) && !@touch($logfile)) return false; 256 if(!file_exists($logfile) && !@touch($logfile)) return false;
257 @chmod($logfile, octdec($this->config->file_mode)); 257 @chmod($logfile, octdec($this->config->file_mode));
258 $fp = @fopen($logfile,"r+"); 258 $fp = @fopen($logfile,"r+");
259 if(!$fp) return false; 259 if(!$fp) return false;
260 260
261 flock($fp, LOCK_EX); 261 flock($fp, LOCK_EX);
262 ftruncate($fp, 0); 262 ftruncate($fp, 0);
263 fwrite($fp, '"'. 263 fwrite($fp, '"'.
264 $gal->id.'",'. 264 $gal->id.'",'.
265 $gal->hits.','. 265 $gal->hits.','.
266 $gal->lasthit 266 $gal->lasthit
267 ); 267 );
268 268
269 foreach($gal->images as $img) 269 foreach($gal->images as $img)
270 fwrite($fp, "\n\"". 270 fwrite($fp, "\n\"".
271 $img->id.'",'. 271 $img->id.'",'.
272 $img->hits.','. 272 $img->hits.','.
273 $img->lasthit 273 $img->lasthit
274 ); 274 );
275 flock($fp, LOCK_UN); 275 flock($fp, LOCK_UN);
276 fclose($fp); 276 fclose($fp);
277 return true; 277 return true;
278 } 278 }
279 279
280 /** 280 /**
281 * Fetches all registered users. 281 * Fetches all registered users.
282 */ 282 */
283 function getUsers() { 283 function getUsers() {
284 $fp = fopen($this->config->base_path.$this->config->pathto_data_dir."users.csv.php","r"); 284 $fp = fopen($this->config->base_path.$this->config->pathto_data_dir."users.csv.php","r");
285 285
286 //strip off description line 286 //strip off description line
287 fgetcsv($fp,1024); 287 fgetcsv($fp,1024);
288 288
289 for($i=0;$entry = fgetcsv($fp,1000,",");$i++) { 289 for($i=0;$entry = fgetcsv($fp,1000,",");$i++) {
290 $users[$i] = new sgUser(null,null); 290 $users[$i] = new sgUser(null,null);
291 list( 291 list(
292 $users[$i]->username, 292 $users[$i]->username,
293 $users[$i]->userpass, 293 $users[$i]->userpass,
294 $users[$i]->permissions, 294 $users[$i]->permissions,
295 $users[$i]->groups, 295 $users[$i]->groups,
296 $users[$i]->email, 296 $users[$i]->email,
297 $users[$i]->fullname, 297 $users[$i]->fullname,
298 $users[$i]->description, 298 $users[$i]->description,
299 $users[$i]->stats 299 $users[$i]->stats
300 ) = $entry; 300 ) = $entry;
301 } 301 }
302 302
303 fclose($fp); 303 fclose($fp);
304 return $users; 304 return $users;
305 } 305 }
306 306
307 /** 307 /**
308 * Stores all registered users. 308 * Stores all registered users.
309 * @param array an array of sgUser objects representing the users to store 309 * @param array an array of sgUser objects representing the users to store
310 */ 310 */
311 function putUsers($users) { 311 function putUsers($users) {
312 $fp = fopen($this->config->base_path.$this->config->pathto_data_dir."users.csv.php","w"); 312 $fp = fopen($this->config->base_path.$this->config->pathto_data_dir."users.csv.php","w");
313 if(!$fp) return false; 313 if(!$fp) return false;
314 314
315 $success = (bool) fwrite($fp,"<?php die(\"The contents of this file are hidden\"); ?>username,md5(pass),permissions,group(s),email,name,description,stats\n"); 315 $success = (bool) fwrite($fp,"<?php die(\"The contents of this file are hidden\"); ?>username,md5(pass),permissions,group(s),email,name,description,stats\n");
316 for($i=0;$i<count($users);$i++) 316 for($i=0;$i<count($users);$i++)
317 $success &= (bool) fwrite($fp,$users[$i]->username.",".$users[$i]->userpass.",".$users[$i]->permissions.",\"".$users[$i]->groups."\",\"".$users[$i]->email."\",\"".$users[$i]->fullname."\",\"".$users[$i]->description."\",\"".$users[$i]->stats."\"\n"); 317 $success &= (bool) fwrite($fp,$users[$i]->username.",".$users[$i]->userpass.",".$users[$i]->permissions.",\"".$users[$i]->groups."\",\"".$users[$i]->email."\",\"".$users[$i]->fullname."\",\"".$users[$i]->description."\",\"".$users[$i]->stats."\"\n");
318 318
319 fclose($fp); 319 fclose($fp);
320 return $success; 320 return $success;
321 } 321 }
322 322
323 } 323 }
324   324  
325 ?> 325 ?>