]> dev.renevier.net Git - syp.git/blob - inc/db/mysql.php
interface to change password
[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 user_exists ($user_name) {
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         return ($res [0] == 1);
63     }
64
65     public function setpwd ($user_name, $pwd) {
66         $usrname_escaped = mysql_real_escape_string ($user_name);
67         if ($this->user_exists ($user_name)) {
68             $query = sprintf ("UPDATE %susers SET pwd='%s' WHERE name like '%s';", 
69                     $this->dbprefix, md5 ($pwd), $usrname_escaped);
70         } else {
71             $query = sprintf ("INSERT INTO %susers VALUES ('%s', '%s');", 
72                                $this->dbprefix, $usrname_escaped, md5 ($pwd));
73         }
74         $this->_execute_query ($query);
75     }
76
77     public function checkpwdmd5 ($user_name, $pwd_md5) {
78         $query = sprintf ("SELECT COUNT(*) FROM %susers WHERE name LIKE '%s'
79                            AND pwd LIKE '%s';",
80                            $this->dbprefix, 
81                            mysql_real_escape_string ($user_name),
82                            mysql_real_escape_string ($pwd_md5));
83         $res = mysql_fetch_array ($this->_execute_query ($query), MYSQL_NUM);
84         if ($res [0] >= 1) {
85             return true;
86         } else {
87             return false;
88         }
89     }
90
91     public function save_feature ($feature) {
92         try {
93             $id = $feature->id;
94         } catch (Exception $e) {}
95         if (isset ($id)) {
96             $query = sprintf ("UPDATE %sitems SET
97                                     imgpath='%s', 
98                                     title='%s', 
99                                     description='%s', 
100                                     location=GeomFromText('POINT(%s %s)')
101                             WHERE id = '%s';",
102                             $this->dbprefix,
103                             mysql_real_escape_string ($feature->imgpath),
104                             mysql_real_escape_string ($feature->title),
105                             mysql_real_escape_string ($feature->description),
106                             $feature->lon,
107                             $feature->lat,
108                             $id);
109                 $this->_execute_query ($query);
110                 return $feature;
111         } else {
112               $query = sprintf ("INSERT INTO %sitems
113                               (imgpath, title, description, location, date, user)
114                                 VALUES ('%s', '%s', '%s', 
115                                GeomFromText('POINT(%s %s)'), NOW(), '%s')", 
116                               $this->dbprefix,
117                               mysql_real_escape_string ($feature->imgpath),
118                               mysql_real_escape_string ($feature->title),
119                               mysql_real_escape_string ($feature->description),
120                               $feature->lon,
121                               $feature->lat,
122                               mysql_real_escape_string ($feature->user)
123                     );
124
125                 $this->_execute_query ($query);
126                 $id = mysql_insert_id ();
127                 return new feature ($id, $feature->lon, $feature->lat,
128                                     $feature->imgpath, $feature->title,
129                                     $feature->description, $feature->date,
130                                     $feature->user);
131         }
132     }
133
134     public function delete_feature ($feature) {
135         $query = sprintf ("DELETE from %sitems WHERE id = '%s'",
136                         $this->dbprefix,
137                         mysql_real_escape_string ($feature->id));
138         $this->_execute_query ($query);
139         return true;
140     }
141
142     public function getfeature ($id) {
143         $query = sprintf ("SELECT id, imgpath, title, description, AsText(location)
144                            AS location, UNIX_TIMESTAMP(date) AS date, user
145                            FROM %sitems WHERE id = '%s';", 
146                         $this->dbprefix, mysql_real_escape_string ($id));
147         $row = mysql_fetch_assoc ($this->_execute_query ($query));
148         if ($row === false) {
149             return null;
150         }
151         return $this->_feature_frow_row ($row);
152     }
153
154     public function listfeatures ($user) {
155         if ($user && ($user != "admin")) {
156             $from_user_query = sprintf (" WHERE user = '%s' ",
157                                         mysql_real_escape_string ($user));
158         } else {
159             $from_user_query = "";
160         }
161
162         $query = sprintf ("SELECT id, imgpath, title, description, AsText(location)
163                             AS location, UNIX_TIMESTAMP(date) AS date, user
164                             FROM %sitems %s;",
165                           $this->dbprefix, $from_user_query);
166
167         $features = array ();
168         $res = $this->_execute_query ($query);
169         while ($row = mysql_fetch_assoc ($res)) {
170             $feature = $this->_feature_frow_row ($row);
171             if (isset ($feature)) {
172                 $features[] = $feature;
173             }
174         }
175         return $features;
176     }
177
178     public function mostrecentfeatures ($num_features) {
179         $query = sprintf ("SELECT id, imgpath, title, description,
180                            AsText(location) AS location, UNIX_TIMESTAMP(date)
181                            AS date, user FROM %sitems ORDER BY date DESC",
182                            $this->dbprefix);
183         if ($num_features) {
184             $query .= sprintf (" LIMIT %d", $num_features);
185         }
186         $features = array ();
187         $res = $this->_execute_query ($query);
188         while ($row = mysql_fetch_assoc ($res)) {
189             $feature = $this->_feature_frow_row ($row);
190             if (isset ($feature)) {
191                 $features[] = $feature;
192             }
193         }
194         return $features;
195     }
196
197     public function imgpath_exists ($imgpath) {
198         $query = sprintf ("SELECT COUNT(*) FROM %sitems WHERE imgpath LIKE '%s';",
199                            $this->dbprefix, mysql_real_escape_string ($imgpath));
200         $res = mysql_fetch_array  ($this->_execute_query ($query), MYSQL_NUM);
201         return ($res [0] >= 1) ? true : false;
202     }
203
204     public function getdbname () {
205         return "Mysql";
206     }
207
208     private function _tblexists ($tblname) {
209         $query = sprintf ("SHOW TABLES LIKE '%s%s';",
210                             $this->dbprefix, $tblname);
211         return mysql_num_rows ($this->_execute_query ($query)) == 1;
212     }
213
214     private function _feature_frow_row ($row) {
215         // XXX: should I remove invalid features from database ?
216         if (!preg_match ('/^POINT\(([0-9\-\.]+)\s+([0-9\-\.]+)\)$/',
217                         $row ["location"], $matches)) {
218             return null;
219         }
220         $lon = $matches [1];
221         $lat = $matches [2];
222         try {
223             $feature = new feature ($row ["id"], $lon, $lat, $row ["imgpath"],
224                                     $row ["title"], $row ["description"],
225                                     $row ["date"], $row ["user"]);
226         } catch (Exception $e) {
227             return null;
228         }
229         return $feature;
230     }
231
232     private function _execute_query ($query) {
233         if (!function_exists ("mysql_query")) {
234             throw new Exception (anydbConnection::err_driver_unavailable);
235         }
236         if (!$this->link) {
237             throw new Exception (anydbConnection::err_query);
238         }
239         $res = mysql_query ($query, $this->link);
240         if ($res == false) {
241             throw new Exception (anydbConnection::err_query);
242         }
243         return $res;
244     }
245 }
246
247 $connection = new mysqlConnection();
248 ?>