2 /* Copyright (c) 2009 Arnaud Renevier, Inc, published under the modified BSD
5 require_once ("./inc/db/anydb.php");
7 class mysqlConnection implements anydbConnection {
11 public function connect ($host, $user, $pwd, $dbname, $dbprefix) {
12 if (!function_exists ("mysql_connect")) {
13 throw new Exception (anydbConnection::err_driver_unavailable);
15 if ($this->link) { // connection has already been opened
18 $this->link = @mysql_connect ($host,$user,$pwd,true);
20 throw new Exception (anydbConnection::err_connection);
22 if (!mysql_select_db ($dbname, $this->link)) {
23 throw new Exception (anydbConnection::err_unknown_database);
25 $this->dbprefix = $dbprefix;
28 public function users_table_exists () {
29 return $this->_tblexists ("users");
31 public function create_users_table () {
32 $query = sprintf ("CREATE TABLE " .
34 name VARCHAR(255) NOT NULL, pwd CHAR(32),
35 PRIMARY KEY (name));", $this->dbprefix);
36 $this->_execute_query ($query);
39 public function items_table_exists () {
40 return $this->_tblexists ("items");
42 public function create_items_table () {
43 $query = sprintf ("CREATE TABLE " .
45 id MEDIUMINT NOT NULL AUTO_INCREMENT,
53 );", $this->dbprefix);
54 $this->_execute_query ($query);
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);
63 $query = sprintf ("UPDATE %susers SET pwd='%s' WHERE name like '%s';",
64 $this->dbprefix, md5 ($pwd), $usrname_escaped);
66 $query = sprintf ("INSERT INTO %susers VALUES ('%s', '%s');",
67 $this->dbprefix, $usrname_escaped, md5 ($pwd));
69 $this->_execute_query ($query);
72 public function checkpwdmd5 ($user_name, $pwd_md5) {
73 $query = sprintf ("SELECT COUNT(*) FROM %susers WHERE name LIKE '%s'
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);
86 public function save_feature ($feature) {
89 } catch (Exception $e) {}
91 $query = sprintf ("UPDATE %sitems SET
95 location=GeomFromText('POINT(%s %s)')
98 mysql_real_escape_string ($feature->imgpath),
99 mysql_real_escape_string ($feature->title),
100 mysql_real_escape_string ($feature->description),
104 $this->_execute_query ($query);
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')",
112 mysql_real_escape_string ($feature->imgpath),
113 mysql_real_escape_string ($feature->title),
114 mysql_real_escape_string ($feature->description),
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);
127 public function delete_feature ($feature) {
128 $query = sprintf ("DELETE from %sitems WHERE id = '%s'",
130 mysql_real_escape_string ($feature->id));
131 $this->_execute_query ($query);
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
139 $this->dbprefix, mysql_real_escape_string ($id));
140 $row = mysql_fetch_assoc ($this->_execute_query ($query));
141 if ($row === false) {
144 return $this->_feature_frow_row ($row);
147 public function listfeatures () {
148 $query = sprintf ("SELECT id, imgpath, title, description, AsText(location)
149 AS location, UNIX_TIMESTAMP(date) AS date FROM %sitems;",
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;
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",
169 $query .= sprintf (" LIMIT %d", $num_features);
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;
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;
189 public function mbr () {
196 return array ($minlon, $minlat, $maxlon, $maxlat);
199 $features = $this->listfeatures ();
200 } catch (Exception $e) {
201 return array ($minlon, $minlat, $maxlon, $maxlat);
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
209 $feature = $features [0];
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);
216 return array ($minlon, $minlat, $maxlon, $maxlat);
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);
225 return array ($minlon, $minlat, $maxlon, $maxlat);
229 public function getdbname () {
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;
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)) {
248 $feature = new feature ($row ["id"], $lon, $lat, $row ["imgpath"],
249 $row ["title"], $row ["description"],
251 } catch (Exception $e) {
257 private function _execute_query ($query) {
258 if (!function_exists ("mysql_query")) {
259 throw new Exception (anydbConnection::err_driver_unavailable);
262 throw new Exception (anydbConnection::err_query);
264 $res = mysql_query ($query, $this->link);
266 throw new Exception (anydbConnection::err_query);
272 $connection = new mysqlConnection();