]> dev.renevier.net Git - syp.git/blob - inc/db/mysql.php
rss feed
[syp.git] / inc / db / mysql.php
1 <?php
2 /* Copyright (c) 2009 Arnaud Renevier, Inc, published under the modified BSD
3    license. */
4
5 require_once ("./inc/db/anydb.php");
6
7 class mysqlConnection implements anydbConnection {
8     var $link = null;
9     var $dbprefix = null;
10
11     public function connect ($host, $user, $pwd, $dbname, $dbprefix) {
12         if (!function_exists ("mysql_connect")) {
13             throw new Exception (anydbConnection::err_driver_unavailable);
14         }
15         if ($this->link) { // connection has already been opened
16             return;
17         }
18         $this->link = @mysql_connect ($host,$user,$pwd,true);
19         if (!$this->link) {
20             throw new Exception (anydbConnection::err_connection);
21         }
22         if (!mysql_select_db ($dbname, $this->link)) {
23             throw new Exception (anydbConnection::err_unknown_database);
24         }
25         $this->dbprefix = $dbprefix;
26     }
27
28     public function users_table_exists () {
29         return $this->_tblexists ("users");
30     }
31     public function create_users_table () {
32         $query = sprintf ("CREATE TABLE " .
33                            "%susers (
34                             name VARCHAR(255) NOT NULL, pwd CHAR(32),
35                             PRIMARY KEY (name));", $this->dbprefix);
36         $this->_execute_query ($query);
37     }
38
39     public function items_table_exists () {
40         return $this->_tblexists ("items");
41     }
42     public function create_items_table () {
43         $query = sprintf ("CREATE TABLE " .
44                             "%sitems (
45                                 id MEDIUMINT NOT NULL AUTO_INCREMENT,
46                                 location POINT,
47                                 title VARCHAR(127),
48                                 description TEXT,
49                                 imgpath VARCHAR(255),
50                                 date DATETIME,
51                                 user VARCHAR(255),
52                                 PRIMARY KEY (id)
53                             );", $this->dbprefix);
54         $this->_execute_query ($query);
55     }
56
57     public function setpwd ($user_name, $pwd) {
58         $usrname_escaped = mysql_real_escape_string ($user_name);
59         $query = sprintf ("SELECT COUNT(*) FROM %susers WHERE name LIKE '%s';",
60                         $this->dbprefix, $usrname_escaped);
61         $res = mysql_fetch_array ($this->_execute_query ($query), MYSQL_NUM);
62         if ($res [0] == 1) {
63             $query = sprintf ("UPDATE %susers SET pwd='%s' WHERE name like '%s';", 
64                                $this->dbprefix, md5 ($pwd), $usrname_escaped);
65         } else {
66             $query = sprintf ("INSERT INTO %susers VALUES ('%s', '%s');", 
67                                $this->dbprefix, $usrname_escaped, md5 ($pwd));
68         }
69         $this->_execute_query ($query);
70     }
71
72     public function checkpwdmd5 ($user_name, $pwd_md5) {
73         $query = sprintf ("SELECT COUNT(*) FROM %susers WHERE name LIKE '%s'
74                            AND pwd LIKE '%s';",
75                            $this->dbprefix, 
76                            mysql_real_escape_string ($user_name),
77                            mysql_real_escape_string ($pwd_md5));
78         $res = mysql_fetch_array ($this->_execute_query ($query), MYSQL_NUM);
79         if ($res [0] >= 1) {
80             return true;
81         } else {
82             return false;
83         }
84     }
85
86     public function save_feature ($feature) {
87         try {
88             $id = $feature->id;
89         } catch (Exception $e) {}
90         if (isset ($id)) {
91             $query = sprintf ("UPDATE %sitems SET
92                                     imgpath='%s', 
93                                     title='%s', 
94                                     description='%s', 
95                                     location=GeomFromText('POINT(%s %s)')
96                             WHERE id = '%s';",
97                             $this->dbprefix,
98                             mysql_real_escape_string ($feature->imgpath),
99                             mysql_real_escape_string ($feature->title),
100                             mysql_real_escape_string ($feature->description),
101                             $feature->lon,
102                             $feature->lat,
103                             $id);
104                 $this->_execute_query ($query);
105                 return $feature;
106         } else {
107               $query = sprintf ("INSERT INTO %sitems
108                               (imgpath, title, description, location, date, user)
109                                 VALUES ('%s', '%s', '%s', 
110                                GeomFromText('POINT(%s %s)'), NOW(), 'admin')", 
111                               $this->dbprefix,
112                               mysql_real_escape_string ($feature->imgpath),
113                               mysql_real_escape_string ($feature->title),
114                               mysql_real_escape_string ($feature->description),
115                               $feature->lon,
116                               $feature->lat
117                     );
118
119                 $this->_execute_query ($query);
120                 $id = mysql_insert_id ();
121                 return new feature ($id, $feature->lon, $feature->lat,
122                                     $feature->imgpath, $feature->title,
123                                     $feature->description, $feature->date);
124         }
125     }
126
127     public function delete_feature ($feature) {
128         $query = sprintf ("DELETE from %sitems WHERE id = '%s'",
129                         $this->dbprefix,
130                         mysql_real_escape_string ($feature->id));
131         $this->_execute_query ($query);
132         return true;
133     }
134
135     public function getfeature ($id) {
136         $query = sprintf ("SELECT id, imgpath, title, description, AsText(location)
137                            AS location, UNIX_TIMESTAMP(date) AS date FROM %sitems
138                            WHERE id = '%s';", 
139                         $this->dbprefix, mysql_real_escape_string ($id));
140         $row = mysql_fetch_assoc ($this->_execute_query ($query));
141         if ($row === false) {
142             return null;
143         }
144         return $this->_feature_frow_row ($row);
145     }
146
147     public function listfeatures () {
148         $query = sprintf ("SELECT id, imgpath, title, description, AsText(location)
149                             AS location, UNIX_TIMESTAMP(date) AS date FROM %sitems;",
150                           $this->dbprefix);
151
152         $features = array ();
153         $res = $this->_execute_query ($query);
154         while ($row = mysql_fetch_assoc ($res)) {
155             $feature = $this->_feature_frow_row ($row);
156             if (isset ($feature)) {
157                 $features[] = $feature;
158             }
159         }
160         return $features;
161     }
162
163     public function mostrecentfeatures ($num_features) {
164         $query = sprintf ("SELECT id, imgpath, title, description,
165                            AsText(location) AS location, UNIX_TIMESTAMP(date)
166                            AS date FROM %sitems ORDER BY date DESC",
167                            $this->dbprefix);
168         if ($num_features) {
169             $query .= sprintf (" LIMIT %d", $num_features);
170         }
171         $features = array ();
172         $res = $this->_execute_query ($query);
173         while ($row = mysql_fetch_assoc ($res)) {
174             $feature = $this->_feature_frow_row ($row);
175             if (isset ($feature)) {
176                 $features[] = $feature;
177             }
178         }
179         return $features;
180     }
181
182     public function imgpath_exists ($imgpath) {
183         $query = sprintf ("SELECT COUNT(*) FROM %sitems WHERE imgpath LIKE '%s';",
184                            $this->dbprefix, mysql_real_escape_string ($imgpath));
185         $res = mysql_fetch_array  ($this->_execute_query ($query), MYSQL_NUM);
186         return ($res [0] >= 1) ? true : false;
187     }
188
189     public function mbr () {
190         $maxlon = -180;
191         $minlon = 180;
192         $maxlat = -88;
193         $minlat = 88;
194
195         if (!$this->link) {
196             return array ($minlon, $minlat, $maxlon, $maxlat);
197         }
198         try {
199             $features = $this->listfeatures ();
200         } catch (Exception $e) {
201             return array ($minlon, $minlat, $maxlon, $maxlat);
202         }
203
204         if (count ($features) == 0) {
205             return array ($minlon, $minlat, $maxlon, $maxlat);
206         } else if (count ($features) == 1) { 
207             // in case there's only one feature, we show an area of at least 
208             // 4 x 4 degrees
209             $feature = $features [0];
210
211             $minlon = max ($feature->lon - 2, -180);
212             $maxlon = min ($feature->lon + 2, 180);
213             $minlat = max ($feature->lat - 2, -90);
214             $maxlat = min ($feature->lat + 2, 90);
215
216             return array ($minlon, $minlat, $maxlon, $maxlat);
217         } else {
218             foreach ($features as $feature) {
219                 $minlon = min ($feature->lon, $minlon);
220                 $minlat = min ($feature->lat, $minlat);
221                 $maxlon = max ($feature->lon, $maxlon);
222                 $maxlat = max ($feature->lat, $maxlat);
223             }
224
225             return array ($minlon, $minlat, $maxlon, $maxlat);
226         }
227     }
228
229     public function getdbname () {
230         return "Mysql";
231     }
232
233     private function _tblexists ($tblname) {
234         $query = sprintf ("SHOW TABLES LIKE '%s%s';",
235                             $this->dbprefix, $tblname);
236         return mysql_num_rows ($this->_execute_query ($query)) == 1;
237     }
238
239     private function _feature_frow_row ($row) {
240         // XXX: should I remove invalid features from database ?
241         if (!preg_match ('/^POINT\(([0-9\-\.]+)\s+([0-9\-\.]+)\)$/',
242                         $row ["location"], $matches)) {
243             return null;
244         }
245         $lon = $matches [1];
246         $lat = $matches [2];
247         try {
248             $feature = new feature ($row ["id"], $lon, $lat, $row ["imgpath"],
249                                     $row ["title"], $row ["description"],
250                                     $row ["date"]);
251         } catch (Exception $e) {
252             return null;
253         }
254         return $feature;
255     }
256
257     private function _execute_query ($query) {
258         if (!function_exists ("mysql_query")) {
259             throw new Exception (anydbConnection::err_driver_unavailable);
260         }
261         if (!$this->link) {
262             throw new Exception (anydbConnection::err_query);
263         }
264         $res = mysql_query ($query, $this->link);
265         if ($res == false) {
266             throw new Exception (anydbConnection::err_query);
267         }
268         return $res;
269     }
270 }
271
272 $connection = new mysqlConnection();
273 ?>