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, 2004 Tamlyn Rhodes 6 * @copyright (c)2003, 2004 Tamlyn Rhodes
7 * @version $Id: iosql.class.php,v 1.5 2006/01/22 03:25:36 tamlyn Exp $ 7 * @version $Id: iosql.class.php,v 1.5 2006/01/22 03:25:36 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 a MySQL database. 14 * Class used to read and write data to and from a MySQL database.
15 * @package singapore 15 * @package singapore
16 * @author Tamlyn Rhodes <tam at zenology dot co dot uk> 16 * @author Tamlyn Rhodes <tam at zenology dot co dot uk>
17 * @copyright (c)2004 Tamlyn Rhodes 17 * @copyright (c)2004 Tamlyn Rhodes
18 */ 18 */
19 class sgIOsql extends sgIO 19 class sgIOsql extends sgIO
20 { 20 {
21 21
22 /** 22 /**
23 * Overridden in subclasses 23 * Overridden in subclasses
24 */ 24 */
25 function query($query) { } 25 function query($query) { }
26 function escape_string($query) { } 26 function escape_string($query) { }
27 function fetch_array($res) { } 27 function fetch_array($res) { }
28 function num_rows($res) { } 28 function num_rows($res) { }
29 function error() 29 function error()
30 { 30 {
31 return "unknown error"; 31 return "unknown error";
32 } 32 }
33 33
34 /** 34 /**
35 * Fetches gallery info for the specified gallery and immediate children. 35 * Fetches gallery info for the specified gallery and immediate children.
36 * @param string gallery id 36 * @param string gallery id
37 * @param string language code spec for this request (optional) 37 * @param string language code spec for this request (optional)
38 * @param int number of levels of child galleries to fetch (optional) 38 * @param int number of levels of child galleries to fetch (optional)
39 */ 39 */
40 function &getGallery($galleryId, &$parent, $getChildGalleries = 1, $language = null) 40 function &getGallery($galleryId, &$parent, $getChildGalleries = 1, $language = null)
41 { 41 {
42 $gal =& new sgGallery($galleryId, $parent); 42 $gal =& new sgGallery($galleryId, $parent);
43 43
44 if($language == null) $language = $this->config->default_language; 44 if($language == null) $language = $this->config->default_language;
45 45
46 //try to open language specific gallery info 46 //try to open language specific gallery info
47 $res = $this->query("SELECT * FROM ".$this->config->sql_prefix."galleries ". 47 $res = $this->query("SELECT * FROM ".$this->config->sql_prefix."galleries ".
48 "WHERE id='".$this->escape_string($galleryId)."' ". 48 "WHERE id='".$this->escape_string($galleryId)."' ".
49 "AND lang='".$this->escape_string($language)."'"); 49 "AND lang='".$this->escape_string($language)."'");
50 50
51 //if fail then try to open generic gallery info 51 //if fail then try to open generic gallery info
52 if(!$res || !$this->num_rows($res)) 52 if(!$res || !$this->num_rows($res))
53 $res = $this->query("SELECT * FROM ".$this->config->sql_prefix."galleries ". 53 $res = $this->query("SELECT * FROM ".$this->config->sql_prefix."galleries ".
54 "WHERE id='".$this->escape_string($galleryId)."' and lang=''"); 54 "WHERE id='".$this->escape_string($galleryId)."' and lang=''");
55 //if that succeeds then get galleries from db 55 //if that succeeds then get galleries from db
56 if($res && $this->num_rows($res)) { 56 if($res && $this->num_rows($res)) {
57 $galinfo = $this->fetch_array($res); 57 $galinfo = $this->fetch_array($res);
58 $gal->filename = $galinfo['filename']; 58 $gal->filename = $galinfo['filename'];
59 $gal->owner = $galinfo['owner']; 59 $gal->owner = $galinfo['owner'];
60 $gal->groups = $galinfo['groups']; 60 $gal->groups = $galinfo['groups'];
61 $gal->permissions = $galinfo['permissions']; 61 $gal->permissions = $galinfo['permissions'];
62 $gal->categories = $galinfo['categories']; 62 $gal->categories = $galinfo['categories'];
63 $gal->name = $galinfo['name']; 63 $gal->name = $galinfo['name'];
64 $gal->artist = $galinfo['artist']; 64 $gal->artist = $galinfo['artist'];
65 $gal->email = $galinfo['email']; 65 $gal->email = $galinfo['email'];
66 $gal->copyright = $galinfo['copyright']; 66 $gal->copyright = $galinfo['copyright'];
67 $gal->desc = $galinfo['description']; 67 $gal->desc = $galinfo['description'];
68 $gal->summary = $galinfo['summary']; 68 $gal->summary = $galinfo['summary'];
69 $gal->date = $galinfo['date']; 69 $gal->date = $galinfo['date'];
70 $gal->hits = $galinfo['hits']; 70 $gal->hits = $galinfo['hits'];
71 $gal->lasthit = $galinfo['lasthit']; 71 $gal->lasthit = $galinfo['lasthit'];
72 72
73 //try to open language specific image info 73 //try to open language specific image info
74 $res = $this->query("SELECT * FROM ".$this->config->sql_prefix."images ". 74 $res = $this->query("SELECT * FROM ".$this->config->sql_prefix."images ".
75 "WHERE galleryid='".$this->escape_string($galleryId)."' ". 75 "WHERE galleryid='".$this->escape_string($galleryId)."' ".
76 "AND lang='".$this->escape_string($language)."'"); 76 "AND lang='".$this->escape_string($language)."'");
77 77
78 //if fail then try to open generic image info 78 //if fail then try to open generic image info
79 if(!$res || !$this->num_rows($res)) 79 if(!$res || !$this->num_rows($res))
80 $res = $this->query("SELECT * FROM ".$this->config->sql_prefix."images ". 80 $res = $this->query("SELECT * FROM ".$this->config->sql_prefix."images ".
81 "WHERE galleryid='".$this->escape_string($galleryId)."' and lang=''"); 81 "WHERE galleryid='".$this->escape_string($galleryId)."' and lang=''");
82 for($i=0;$i<$this->num_rows($res);$i++) { 82 for($i=0;$i<$this->num_rows($res);$i++) {
83 $imginfo = $this->fetch_array($res); 83 $imginfo = $this->fetch_array($res);
84 $gal->images[$i] =& new sgImage($imginfo['filename'], $gal); 84 $gal->images[$i] =& new sgImage($imginfo['filename'], $gal);
85 $gal->images[$i]->thumbnail = $imginfo['thumbnail']; 85 $gal->images[$i]->thumbnail = $imginfo['thumbnail'];
86 $gal->images[$i]->owner = $imginfo['owner']; 86 $gal->images[$i]->owner = $imginfo['owner'];
87 $gal->images[$i]->groups = $imginfo['groups']; 87 $gal->images[$i]->groups = $imginfo['groups'];
88 $gal->images[$i]->permissions = $imginfo['permissions']; 88 $gal->images[$i]->permissions = $imginfo['permissions'];
89 $gal->images[$i]->categories = $imginfo['categories']; 89 $gal->images[$i]->categories = $imginfo['categories'];
90 $gal->images[$i]->name = $imginfo['name']; 90 $gal->images[$i]->name = $imginfo['name'];
91 $gal->images[$i]->artist = $imginfo['artist']; 91 $gal->images[$i]->artist = $imginfo['artist'];
92 $gal->images[$i]->email = $imginfo['email']; 92 $gal->images[$i]->email = $imginfo['email'];
93 $gal->images[$i]->copyright = $imginfo['copyright']; 93 $gal->images[$i]->copyright = $imginfo['copyright'];
94 $gal->images[$i]->desc = $imginfo['description']; 94 $gal->images[$i]->desc = $imginfo['description'];
95 $gal->images[$i]->location = $imginfo['location']; 95 $gal->images[$i]->location = $imginfo['location'];
96 $gal->images[$i]->date = $imginfo['date']; 96 $gal->images[$i]->date = $imginfo['date'];
97 $gal->images[$i]->camera = $imginfo['camera']; 97 $gal->images[$i]->camera = $imginfo['camera'];
98 $gal->images[$i]->lens = $imginfo['lens']; 98 $gal->images[$i]->lens = $imginfo['lens'];
99 $gal->images[$i]->film = $imginfo['film']; 99 $gal->images[$i]->film = $imginfo['film'];
100 $gal->images[$i]->darkroom = $imginfo['darkroom']; 100 $gal->images[$i]->darkroom = $imginfo['darkroom'];
101 $gal->images[$i]->digital = $imginfo['digital']; 101 $gal->images[$i]->digital = $imginfo['digital'];
102 $gal->images[$i]->width = $imginfo['width']; 102 $gal->images[$i]->width = $imginfo['width'];
103 $gal->images[$i]->height = $imginfo['height']; 103 $gal->images[$i]->height = $imginfo['height'];
104 $gal->images[$i]->type = $imginfo['type']; 104 $gal->images[$i]->type = $imginfo['type'];
105 $gal->images[$i]->hits = $imginfo['hits']; 105 $gal->images[$i]->hits = $imginfo['hits'];
106 $gal->images[$i]->lasthit = $imginfo['lasthit']; 106 $gal->images[$i]->lasthit = $imginfo['lasthit'];
107 } 107 }
108 108
109 } else 109 } else
110 //no record found so use iifn method implemented in parent class 110 //no record found so use iifn method implemented in parent class
111 return parent::getGallery($galleryId, $parent, $getChildGalleries, $language); 111 return parent::getGallery($galleryId, $parent, $getChildGalleries, $language);
112 112
113 //discover child galleries 113 //discover child galleries
114 $dir = Singapore::getListing($this->config->base_path.$this->config->pathto_galleries.$galleryId."/"); 114 $dir = Singapore::getListing($this->config->base_path.$this->config->pathto_galleries.$galleryId."/");
115 if($getChildGalleries) 115 if($getChildGalleries)
116 //but only fetch their info if required too 116 //but only fetch their info if required too
117 foreach($dir->dirs as $gallery) 117 foreach($dir->dirs as $gallery)
118 $gal->galleries[] =& $this->getGallery($galleryId."/".$gallery, $gal, $getChildGalleries-1, $language); 118 $gal->galleries[] =& $this->getGallery($galleryId."/".$gallery, $gal, $getChildGalleries-1, $language);
119 else 119 else
120 //otherwise just copy their names in so they can be counted 120 //otherwise just copy their names in so they can be counted
121 $gal->galleries = $dir->dirs; 121 $gal->galleries = $dir->dirs;
122 122
123 return $gal; 123 return $gal;
124 } 124 }
125 125
126 /** 126 /**
127 * Stores gallery information. 127 * Stores gallery information.
128 * @param sgGallery instance of gallery object to be stored 128 * @param sgGallery instance of gallery object to be stored
129 */ 129 */
130 function putGallery($gal, $language = "") { 130 function putGallery($gal, $language = "") {
131 //insert gallery info 131 //insert gallery info
132 $success = (bool) $this->query("REPLACE INTO ".$this->config->sql_prefix."galleries ". 132 $success = (bool) $this->query("REPLACE INTO ".$this->config->sql_prefix."galleries ".
133 "(id,lang,filename,owner,groups,permissions,categories,name,artist,". 133 "(id,lang,filename,owner,groups,permissions,categories,name,artist,".
134 "email,copyright,description,summary,date,hits,lasthit) VALUES ('". 134 "email,copyright,description,summary,date,hits,lasthit) VALUES ('".
135 $this->escape_string($gal->id)."','".$language."','". 135 $this->escape_string($gal->id)."','".$language."','".
136 $this->escape_string($gal->filename)."','". 136 $this->escape_string($gal->filename)."','".
137 $gal->owner."','".$gal->groups."',".$gal->permissions.",'". 137 $gal->owner."','".$gal->groups."',".$gal->permissions.",'".
138 $this->escape_string($gal->categories)."','". 138 $this->escape_string($gal->categories)."','".
139 $this->escape_string($gal->name)."','". 139 $this->escape_string($gal->name)."','".
140 $this->escape_string($gal->artist)."','". 140 $this->escape_string($gal->artist)."','".
141 $this->escape_string($gal->email)."','". 141 $this->escape_string($gal->email)."','".
142 $this->escape_string($gal->copyright)."','". 142 $this->escape_string($gal->copyright)."','".
143 $this->escape_string($gal->desc)."','". 143 $this->escape_string($gal->desc)."','".
144 $this->escape_string($gal->summary)."','". 144 $this->escape_string($gal->summary)."','".
145 $this->escape_string($gal->date)."',". 145 $this->escape_string($gal->date)."',".
146 $gal->hits.",".$gal->lasthit.")"); 146 $gal->hits.",".$gal->lasthit.")");
147 //delete all image info 147 //delete all image info
148 $success &= (bool) $this->query("DELETE FROM ".$this->config->sql_prefix."images ". 148 $success &= (bool) $this->query("DELETE FROM ".$this->config->sql_prefix."images ".
149 "WHERE galleryid='".$this->escape_string($gal->id)."' AND lang='".$language."'"); 149 "WHERE galleryid='".$this->escape_string($gal->id)."' AND lang='".$language."'");
150 for($i=0;$i<count($gal->images);$i++) { 150 for($i=0;$i<count($gal->images);$i++) {
151 $success &= (bool) $this->query("INSERT INTO ".$this->config->sql_prefix."images ". 151 $success &= (bool) $this->query("INSERT INTO ".$this->config->sql_prefix."images ".
152 "(galleryid,lang,filename,owner,groups,permissions,categories,name,artist,". 152 "(galleryid,lang,filename,owner,groups,permissions,categories,name,artist,".
153 "email,copyright,description,location,date,camera,lens,film,darkroom,digital,". 153 "email,copyright,description,location,date,camera,lens,film,darkroom,digital,".
154 "width,height,type,hits,lasthit) VALUES ('". 154 "width,height,type,hits,lasthit) VALUES ('".
155 $this->escape_string($gal->id)."','".$language."','". 155 $this->escape_string($gal->id)."','".$language."','".
156 $this->escape_string($gal->images[$i]->id)."','". 156 $this->escape_string($gal->images[$i]->id)."','".
157 $gal->images[$i]->owner."','".$gal->images[$i]->groups."',". 157 $gal->images[$i]->owner."','".$gal->images[$i]->groups."',".
158 $gal->images[$i]->permissions.",'". 158 $gal->images[$i]->permissions.",'".
159 $this->escape_string($gal->images[$i]->categories)."','". 159 $this->escape_string($gal->images[$i]->categories)."','".
160 $this->escape_string($gal->images[$i]->name)."','". 160 $this->escape_string($gal->images[$i]->name)."','".
161 $this->escape_string($gal->images[$i]->artist)."','". 161 $this->escape_string($gal->images[$i]->artist)."','".
162 $this->escape_string($gal->images[$i]->email)."','". 162 $this->escape_string($gal->images[$i]->email)."','".
163 $this->escape_string($gal->images[$i]->copyright)."','". 163 $this->escape_string($gal->images[$i]->copyright)."','".
164 $this->escape_string($gal->images[$i]->desc)."','". 164 $this->escape_string($gal->images[$i]->desc)."','".
165 $this->escape_string($gal->images[$i]->location)."','". 165 $this->escape_string($gal->images[$i]->location)."','".
166 $this->escape_string($gal->images[$i]->date)."','". 166 $this->escape_string($gal->images[$i]->date)."','".
167 $this->escape_string($gal->images[$i]->camera)."','". 167 $this->escape_string($gal->images[$i]->camera)."','".
168 $this->escape_string($gal->images[$i]->lens)."','". 168 $this->escape_string($gal->images[$i]->lens)."','".
169 $this->escape_string($gal->images[$i]->film)."','". 169 $this->escape_string($gal->images[$i]->film)."','".
170 $this->escape_string($gal->images[$i]->darkroom)."','". 170 $this->escape_string($gal->images[$i]->darkroom)."','".
171 $this->escape_string($gal->images[$i]->digital)."',". 171 $this->escape_string($gal->images[$i]->digital)."',".
172 $gal->images[$i]->width.",".$gal->images[$i]->height.",". 172 $gal->images[$i]->width.",".$gal->images[$i]->height.",".
173 $gal->images[$i]->type.",".$gal->images[$i]->hits.",". 173 $gal->images[$i]->type.",".$gal->images[$i]->hits.",".
174 $gal->images[$i]->lasthit.")"); 174 $gal->images[$i]->lasthit.")");
175 } 175 }
176 return $success; 176 return $success;
177 } 177 }
178 178
179 /** 179 /**
180 * Hits are loaded by getGallery so this method does nothing 180 * Hits are loaded by getGallery so this method does nothing
181 * @param sgGallery gallery object to load hits into 181 * @param sgGallery gallery object to load hits into
182 */ 182 */
183 function getHits(&$gal) { 183 function getHits(&$gal) {
184 return true; 184 return true;
185 } 185 }
186 186
187 /** 187 /**
188 * Stores gallery hits. 188 * Stores gallery hits.
189 * @param sgGallery gallery object to store 189 * @param sgGallery gallery object to store
190 */ 190 */
191 function putHits($gal) { 191 function putHits($gal) {
192 //if gallery data doesn't exist in database, add it 192 //if gallery data doesn't exist in database, add it
193 $res = $this->query("SELECT id FROM ".$this->config->sql_prefix."galleries ". 193 $res = $this->query("SELECT id FROM ".$this->config->sql_prefix."galleries ".
194 "WHERE id='".$this->escape_string($gal->id)."'"); 194 "WHERE id='".$this->escape_string($gal->id)."'");
195 if(!$res || !$this->num_rows($res)) 195 if(!$res || !$this->num_rows($res))
196 $this->putGallery($gal); 196 $this->putGallery($gal);
197 197
198 $success = (bool) $this->query("UPDATE ".$this->config->sql_prefix."galleries ". 198 $success = (bool) $this->query("UPDATE ".$this->config->sql_prefix."galleries ".
199 "SET hits=".$gal->hits.", lasthit=".$gal->lasthit." ". 199 "SET hits=".$gal->hits.", lasthit=".$gal->lasthit." ".
200 "WHERE id='".$this->escape_string($gal->id)."'"); 200 "WHERE id='".$this->escape_string($gal->id)."'");
201 foreach($gal->images as $img) 201 foreach($gal->images as $img)
202 $success &= (bool) $this->query("UPDATE ".$this->config->sql_prefix."images ". 202 $success &= (bool) $this->query("UPDATE ".$this->config->sql_prefix."images ".
203 "SET hits=".$img->hits.", lasthit=".$img->lasthit." ". 203 "SET hits=".$img->hits.", lasthit=".$img->lasthit." ".
204 "WHERE galleryid='".$this->escape_string($gal->id)."' ". 204 "WHERE galleryid='".$this->escape_string($gal->id)."' ".
205 "AND filename='".$this->escape_string($img->id)."'"); 205 "AND filename='".$this->escape_string($img->id)."'");
206 206
207 return $success; 207 return $success;
208 } 208 }
209 209
210 /** 210 /**
211 * Fetches all registered users. 211 * Fetches all registered users.
212 */ 212 */
213 function getUsers() { 213 function getUsers() {
214 $res = $this->query("SELECT * FROM ".$this->config->sql_prefix."users"); 214 $res = $this->query("SELECT * FROM ".$this->config->sql_prefix."users");
215 215
216 for($i=0;$i<$this->num_rows($res);$i++) { 216 for($i=0;$i<$this->num_rows($res);$i++) {
217 $usrinfo = $this->fetch_array($res); 217 $usrinfo = $this->fetch_array($res);
218 $users[$i] = new sgUser($usrinfo['username'],$usrinfo['userpass']); 218 $users[$i] = new sgUser($usrinfo['username'],$usrinfo['userpass']);
219 $users[$i]->permissions = $usrinfo['permissions']; 219 $users[$i]->permissions = $usrinfo['permissions'];
220 $users[$i]->groups = $usrinfo['groups']; 220 $users[$i]->groups = $usrinfo['groups'];
221 $users[$i]->email = $usrinfo['email']; 221 $users[$i]->email = $usrinfo['email'];
222 $users[$i]->fullname = $usrinfo['fullname']; 222 $users[$i]->fullname = $usrinfo['fullname'];
223 $users[$i]->description = $usrinfo['description']; 223 $users[$i]->description = $usrinfo['description'];
224 $users[$i]->stats = $usrinfo['stats']; 224 $users[$i]->stats = $usrinfo['stats'];
225 } 225 }
226 226
227 return $users; 227 return $users;
228 } 228 }
229 229
230 /** 230 /**
231 * Stores all registered users. 231 * Stores all registered users.
232 * @param array an array of sgUser objects representing the users to store 232 * @param array an array of sgUser objects representing the users to store
233 */ 233 */
234 function putUsers($users) { 234 function putUsers($users) {
235 //empty table 235 //empty table
236 $success = (bool) $this->query("DELETE FROM ".$this->config->sql_prefix."users"); 236 $success = (bool) $this->query("DELETE FROM ".$this->config->sql_prefix."users");
237 for($i=0;$i<count($users);$i++) 237 for($i=0;$i<count($users);$i++)
238 $success &= (bool) $this->query("INSERT INTO ".$this->config->sql_prefix."users ". 238 $success &= (bool) $this->query("INSERT INTO ".$this->config->sql_prefix."users ".
239 "(username,userpass,permissions,groups,email,fullname,description,stats) VALUES ('". 239 "(username,userpass,permissions,groups,email,fullname,description,stats) VALUES ('".
240 $this->escape_string($users[$i]->username)."','". 240 $this->escape_string($users[$i]->username)."','".
241 $users[$i]->userpass."',".$users[$i]->permissions.",'". 241 $users[$i]->userpass."',".$users[$i]->permissions.",'".
242 $this->escape_string($users[$i]->groups)."','". 242 $this->escape_string($users[$i]->groups)."','".
243 $this->escape_string($users[$i]->email)."','". 243 $this->escape_string($users[$i]->email)."','".
244 $this->escape_string($users[$i]->fullname)."','". 244 $this->escape_string($users[$i]->fullname)."','".
245 $this->escape_string($users[$i]->description)."','". 245 $this->escape_string($users[$i]->description)."','".
246 $this->escape_string($users[$i]->stats)."')"); 246 $this->escape_string($users[$i]->stats)."')");
247 247
248 return $success; 248 return $success;
249 } 249 }
250 } 250 }
251   251  
252 ?> 252 ?>