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 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);
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);
71 $query = sprintf ("INSERT INTO %susers VALUES ('%s', '%s');",
72 $this->dbprefix, $usrname_escaped, md5 ($pwd));
74 $this->_execute_query ($query);
77 public function checkpwdmd5 ($user_name, $pwd_md5) {
78 $query = sprintf ("SELECT COUNT(*) FROM %susers WHERE name LIKE '%s'
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);
91 public function save_feature ($feature) {
94 } catch (Exception $e) {}
96 $query = sprintf ("UPDATE %sitems SET
100 location=GeomFromText('POINT(%s %s)')
103 mysql_real_escape_string ($feature->imgpath),
104 mysql_real_escape_string ($feature->title),
105 mysql_real_escape_string ($feature->description),
109 $this->_execute_query ($query);
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')",
117 mysql_real_escape_string ($feature->imgpath),
118 mysql_real_escape_string ($feature->title),
119 mysql_real_escape_string ($feature->description),
122 mysql_real_escape_string ($feature->user)
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,
134 public function delete_feature ($feature) {
135 $query = sprintf ("DELETE from %sitems WHERE id = '%s'",
137 mysql_real_escape_string ($feature->id));
138 $this->_execute_query ($query);
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) {
151 return $this->_feature_frow_row ($row);
154 public function listfeatures ($user) {
155 if ($user && ($user != "admin")) {
156 $from_user_query = sprintf (" WHERE user = '%s' ",
157 mysql_real_escape_string ($user));
159 $from_user_query = "";
162 $query = sprintf ("SELECT id, imgpath, title, description, AsText(location)
163 AS location, UNIX_TIMESTAMP(date) AS date, user
165 $this->dbprefix, $from_user_query);
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;
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",
184 $query .= sprintf (" LIMIT %d", $num_features);
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;
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;
204 public function getdbname () {
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;
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)) {
223 $feature = new feature ($row ["id"], $lon, $lat, $row ["imgpath"],
224 $row ["title"], $row ["description"],
225 $row ["date"], $row ["user"]);
226 } catch (Exception $e) {
232 private function _execute_query ($query) {
233 if (!function_exists ("mysql_query")) {
234 throw new Exception (anydbConnection::err_driver_unavailable);
237 throw new Exception (anydbConnection::err_query);
239 $res = mysql_query ($query, $this->link);
241 throw new Exception (anydbConnection::err_query);
247 $connection = new mysqlConnection();