]> dev.renevier.net Git - syp.git/blob - inc/db/mysql.php
password is defined by user in wizard + localize wizard
[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);
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 FROM %sitems WHERE id = '%s';", 
138                         $this->dbprefix, mysql_real_escape_string ($id));
139         $row = mysql_fetch_assoc ($this->_execute_query ($query));
140         if ($row === false) {
141             return null;
142         }
143         return $this->_feature_frow_row ($row);
144     }
145
146     public function listfeatures () {
147         $query = sprintf ("SELECT id, imgpath, title, description, AsText(location)
148                             AS location FROM %sitems;",
149                           $this->dbprefix);
150
151         $features = array ();
152         $res = $this->_execute_query ($query);
153         while ($row = mysql_fetch_assoc ($res)) {
154             $feature = $this->_feature_frow_row ($row);
155             if (isset ($feature)) {
156                 $features[] = $feature;
157             }
158         }
159         return $features;
160     }
161
162     public function imgpath_exists ($imgpath) {
163         $query = sprintf ("SELECT COUNT(*) FROM %sitems WHERE imgpath LIKE '%s';",
164                            $this->dbprefix, mysql_real_escape_string ($imgpath));
165         $res = mysql_fetch_array  ($this->_execute_query ($query), MYSQL_NUM);
166         return ($res [0] >= 1) ? true : false;
167     }
168
169     public function mbr () {
170         $maxlon = -180;
171         $minlon = 180;
172         $maxlat = -88;
173         $minlat = 88;
174
175         if (!$this->link) {
176             return array ($minlon, $minlat, $maxlon, $maxlat);
177         }
178         try {
179             $features = $this->listfeatures ();
180         } catch (Exception $e) {
181             return array ($minlon, $minlat, $maxlon, $maxlat);
182         }
183
184         if (count ($features) == 0) {
185             return array ($minlon, $minlat, $maxlon, $maxlat);
186         } else if (count ($features) == 1) { 
187             // in case there's only one feature, we show an area of at least 
188             // 4 x 4 degrees
189             $feature = $features [0];
190
191             $minlon = max ($feature->lon - 2, -180);
192             $maxlon = min ($feature->lon + 2, 180);
193             $minlat = max ($feature->lat - 2, -90);
194             $maxlat = min ($feature->lat + 2, 90);
195
196             return array ($minlon, $minlat, $maxlon, $maxlat);
197         } else {
198             foreach ($features as $feature) {
199                 $minlon = min ($feature->lon, $minlon);
200                 $minlat = min ($feature->lat, $minlat);
201                 $maxlon = max ($feature->lon, $maxlon);
202                 $maxlat = max ($feature->lat, $maxlat);
203             }
204
205             return array ($minlon, $minlat, $maxlon, $maxlat);
206         }
207     }
208
209     public function getdbname () {
210         return "Mysql";
211     }
212
213     private function _tblexists ($tblname) {
214         $query = sprintf ("SHOW TABLES LIKE '%s%s';",
215                             $this->dbprefix, $tblname);
216         return mysql_num_rows ($this->_execute_query ($query)) == 1;
217     }
218
219     private function _feature_frow_row ($row) {
220         // XXX: should I remove invalid features from database ?
221         if (!preg_match ('/^POINT\(([0-9\-\.]+)\s+([0-9\-\.]+)\)$/',
222                         $row ["location"], $matches)) {
223             return null;
224         }
225         $lon = $matches [1];
226         $lat = $matches [2];
227         try {
228             $feature = new feature ($row ["id"], $lon, $lat, $row ["imgpath"],
229                                     $row ["title"], $row ["description"]);
230         } catch (Exception $e) {
231             return null;
232         }
233         return $feature;
234     }
235
236     private function _execute_query ($query) {
237         if (!function_exists ("mysql_query")) {
238             throw new Exception (anydbConnection::err_driver_unavailable);
239         }
240         if (!$this->link) {
241             throw new Exception (anydbConnection::err_query);
242         }
243         $res = mysql_query ($query, $this->link);
244         if ($res == false) {
245             throw new Exception (anydbConnection::err_query);
246         }
247         return $res;
248     }
249 }
250
251 $connection = new mysqlConnection();
252 ?>