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);
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 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) {
143 return $this->_feature_frow_row ($row);
146 public function listfeatures () {
147 $query = sprintf ("SELECT id, imgpath, title, description, AsText(location)
148 AS location FROM %sitems;",
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;
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;
169 public function mbr () {
176 return array ($minlon, $minlat, $maxlon, $maxlat);
179 $features = $this->listfeatures ();
180 } catch (Exception $e) {
181 return array ($minlon, $minlat, $maxlon, $maxlat);
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
189 $feature = $features [0];
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);
196 return array ($minlon, $minlat, $maxlon, $maxlat);
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);
205 return array ($minlon, $minlat, $maxlon, $maxlat);
209 public function getdbname () {
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;
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)) {
228 $feature = new feature ($row ["id"], $lon, $lat, $row ["imgpath"],
229 $row ["title"], $row ["description"]);
230 } catch (Exception $e) {
236 private function _execute_query ($query) {
237 if (!function_exists ("mysql_query")) {
238 throw new Exception (anydbConnection::err_driver_unavailable);
241 throw new Exception (anydbConnection::err_query);
243 $res = mysql_query ($query, $this->link);
245 throw new Exception (anydbConnection::err_query);
251 $connection = new mysqlConnection();