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