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