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