]> dev.renevier.net Git - syp.git/blob - inc/db/mysql.php
initial commit
[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 ("./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                                 imgurl VARCHAR(255),
48                                 title VARCHAR(127),
49                                 description TEXT,
50                                 location POINT,
51                                 date DATETIME,
52                                 PRIMARY KEY (imgurl)
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         $query = sprintf ("SELECT imgurl FROM %sitems WHERE imgurl = '%s'", 
88                            $this->dbprefix, 
89                            mysql_real_escape_string ($feature->imgurl)); 
90         $this->_execute_query ($query);
91         if (mysql_affected_rows ($this->link) == 0) {
92             if ($feature->imgurl_exists ()) {
93               $query = sprintf ("INSERT INTO %sitems
94                                    (imgurl, title, description, location, date)
95                                    VALUES ('%s', '%s', '%s', 
96                                    GeomFromText('POINT(%s %s)'), NOW())", 
97                               $this->dbprefix,
98                               mysql_real_escape_string ($feature->imgurl),
99                               mysql_real_escape_string ($feature->title),
100                               mysql_real_escape_string ($feature->description),
101                               $feature->lon,
102                               $feature->lat
103                     );
104                 $this->_execute_query ($query);
105                 return true;
106             } else {
107                 return false;
108             }
109         } else {
110             $query = sprintf ("UPDATE %sitems SET title='%s', description='%s',
111                                location=GeomFromText('POINT(%s %s)')
112                                where imgurl = '%s'",
113                              $this->dbprefix,
114                              mysql_real_escape_string ($feature->title),
115                              mysql_real_escape_string ($feature->description),
116                              $feature->lon,
117                              $feature->lat,
118                              mysql_real_escape_string ($feature->imgurl)
119                 );
120             $this->_execute_query ($query);
121             return true;
122         }
123     }
124
125     public function delete_feature ($feature) {
126         $query = sprintf ("DELETE from %sitems WHERE imgurl like '%s'",
127                         $this->dbprefix,
128                         mysql_real_escape_string ($feature->imgurl));
129         $this->_execute_query ($query);
130         return true;
131     }
132
133     public function getfeature ($imgurl) {
134         $query = sprintf ("SELECT imgurl, title, description, AsText(location)
135                            AS location FROM %sitems WHERE imgurl like '%s';", 
136                         $this->dbprefix, mysql_real_escape_string ($imgurl));
137         $row = mysql_fetch_assoc ($this->_execute_query ($query));
138         if (!isset ($row)) {
139             return null;
140         }
141         return $this->_feature_frow_row ($row);
142     }
143
144     public function listfeatures () {
145         $query = sprintf ("SELECT imgurl, title, description, AsText(location)
146                             AS location FROM %sitems;",
147                           $this->dbprefix);
148
149         $features = array ();
150         $res = $this->_execute_query ($query);
151         while ($row = mysql_fetch_assoc ($res)) {
152             $feature = $this->_feature_frow_row ($row);
153             if (isset ($feature)) {
154                 $features[] = $feature;
155             }
156         }
157         return $features;
158     }
159
160     public function imgurl_exists ($imgurl) {
161         $query = sprintf ("SELECT COUNT(*) FROM %sitems WHERE imgurl LIKE '%s';",
162                            $this->dbprefix, mysql_real_escape_string ($imgurl));
163         $res = mysql_fetch_array  ($this->_execute_query ($query), MYSQL_NUM);
164         return ($res [0] >= 1) ? true : false;
165     }
166
167     public function mbr () {
168         $maxlon = -180;
169         $minlon = 180;
170         $maxlat = -88;
171         $minlat = 88;
172
173         if (!$this->link) {
174             return array ($minlon, $minlat, $maxlon, $maxlat);
175         }
176         try {
177             $features = $this->listfeatures ();
178         } catch (Exception $e) {
179             return array ($minlon, $minlat, $maxlon, $maxlat);
180         }
181
182         if (count ($features) == 0) {
183             return array ($minlon, $minlat, $maxlon, $maxlat);
184         } else if (count ($features) == 1) { 
185             // in case there's only one feature, we show an area of at least 
186             // 4 x 4 degrees
187             $feature = $features [0];
188
189             $minlon = max ($feature->lon - 2, -180);
190             $maxlon = min ($feature->lon + 2, 180);
191             $minlat = max ($feature->lat - 2, -90);
192             $maxlat = min ($feature->lat + 2, 90);
193
194             return array ($minlon, $minlat, $maxlon, $maxlat);
195         } else {
196             foreach ($features as $feature) {
197                 $minlon = min ($feature->lon, $minlon);
198                 $minlat = min ($feature->lat, $minlat);
199                 $maxlon = max ($feature->lon, $maxlon);
200                 $maxlat = max ($feature->lat, $maxlat);
201             }
202
203             return array ($minlon, $minlat, $maxlon, $maxlat);
204         }
205     }
206
207     public function getdbname () {
208         return "Mysql";
209     }
210
211     private function _tblexists ($tblname) {
212         $query = sprintf ("SHOW TABLES LIKE '%s%s';",
213                             $this->dbprefix, $tblname);
214         return mysql_num_rows ($this->_execute_query ($query)) == 1;
215     }
216
217     private function _feature_frow_row ($row) {
218         // XXX: should I remove invalid features from database ?
219         if (!preg_match ('/^POINT\(([0-9\-\.]+)\s+([0-9\-\.]+)\)$/',
220                         $row ["location"], $matches)) {
221             return null;
222         }
223         $lon = $matches [1];
224         $lat = $matches [2];
225         try {
226             $feature = new feature ($row ["imgurl"], $row ["title"], $row
227                                     ["description"], $lon, $lat);
228         } catch (Exception $e) {
229             return null;
230         }
231         return $feature;
232     }
233
234     private function _execute_query ($query) {
235         if (!function_exists ("mysql_query")) {
236             throw new Exception (anydbConnection::err_driver_unavailable);
237         }
238         if (!$this->link) {
239             throw new Exception (anydbConnection::err_query);
240         }
241         $res = mysql_query ($query, $this->link);
242         if ($res == false) {
243             throw new Exception (anydbConnection::err_query);
244         }
245         return $res;
246     }
247 }
248
249 $connection = new mysqlConnection();
250 ?>