2 /* Copyright (c) 2009 Arnaud Renevier, Inc, published under the modified BSD
5 require_once ("./inc/settings.php");
6 require_once ("./inc/db/mysql.php");
7 require_once ("./inc/utils.php");
9 function exit_document ($body) {
10 exit ("<html><head></head><body>$body</body></html>");
13 function success_auth () {
17 function success_feature ($feature, $request) {
18 $res = "<success request=\"$request\"><feature>";
19 $res .= "<id>" . $feature->id . "</id>";
23 full_url_from_imgpath ($feature->imgpath)
27 $res .= "<description>" .
28 htmlspecialchars ($feature->description) .
31 // XXX: we do not use <title> because that would be interpreted and
32 // altered by browers html parser
34 htmlspecialchars ($feature->title) .
37 $res .= "<lon>" . $feature->lon . "</lon>";
38 $res .= "<lat>" . $feature->lat . "</lat>";
39 $res .= "</feature></success>";
43 function success_delete_feature ($feature) {
44 $res = "<success request=\"del\"><feature>";
45 $res .= "<id>" . $feature->id . "</id>";
46 $res .= "</feature></success>";
50 function success ($reason) {
51 exit_document ("<success request=\"$reason\"></success>");
54 function error ($reason) {
55 exit_document ("<error reason=\"$reason\"></error>");
58 function error_feature ($id, $reason) {
59 $res = "<error reason=\"$reason\"><feature>";
60 $res .= "<id>" . $id . "</id>";
61 $res .= "</feature></error>";
65 function nochange_error ($id) {
66 error_feature ($id, "nochange");
68 function unreferenced_error ($id) {
69 error_feature ($id, "unreferenced");
72 function server_error () {
76 function unauthorized_error () {
77 error ("unauthorized");
80 function request_error () {
84 function file_too_big_error () {
88 function notanimage_error () {
92 function save_uploaded_file ($file, $con) {
94 if (isset ($file) && ($file ["error"] != UPLOAD_ERR_NO_FILE)) {
95 img_check_upload ($file);
96 $dest = unique_file (UPLOADDIR, $file ["name"], $con);
98 (!move_uploaded_file ($file ["tmp_name"], $dest))) {
102 return basename_safe ($dest);
105 function img_check_upload ($file) {
106 if (!is_uploaded_file ($file ["tmp_name"])) {
107 if ($file ["error"] == UPLOAD_ERR_INI_SIZE) {
108 file_too_big_error ();
113 if (!getimagesize ($file ["tmp_name"])) {
118 function delete_image_if_unused ($imgpath, $con) {
119 if (!isset ($imgpath) || (strlen ($imgpath) == 0)) {
122 if ($con->imgpath_exists ($imgpath)) {
125 $path = UPLOADDIR . "/" . $imgpath;
126 if (file_exists($path)) {
134 function unique_file ($dirname, $relpath, $con) {
135 $relpath = str_replace ('/', '', $relpath); // strip slashes from path
136 $relpath = str_replace ('\\', '', $relpath); // strip antislashes from path
137 $filename = $dirname . '/' . $relpath;
140 $dotpos = strrpos ($relpath, '.');
142 $base = substr ($relpath, 0, $dotpos);
143 $ext = substr ($relpath, $dotpos + 1);
149 while ($counter < 1000) {
150 if (!file_exists ($filename) &&
151 !($con->imgpath_exists (basename_safe ($filename)))) {
155 $filename = $dirname . '/' . $base . '_' . $counter . '.' . $ext;
158 // we tried to find an unused filename 1000 times. Give up now.
162 function main ($con) {
163 if (!isset ($_POST ["request"])) {
166 if ($_POST ["request"] == "auth") {
167 $pwd = unquote ($_POST["password"]);
169 if ($con->checkpwdmd5 ($user, md5 ($pwd))) {
170 // cookie will be valid for 2 weeks. I've chosen that value
171 // arbitrarily, and it may change in the future.
172 $time = time () + 14 * 60 * 24 * 60;
173 $cookie_name = sprintf ("%sauth", DBPREFIX);
174 setcookie ($cookie_name, md5 ($pwd), $time, "" , "", false, true);
177 unauthorized_error ();
180 if (!($con->checkpwdmd5 ("admin",
181 $_COOKIE [sprintf ("%sauth", DBPREFIX)]))) {
182 unauthorized_error ();
185 switch ($_POST ["request"]) {
187 $id = $_POST ["fid"];
188 $feature = $con->getfeature ($id);
189 if (!isset ($feature)) {
190 unreferenced_error ($id);
193 // no file uploaded, but editor currently has an image: it means
194 // image was not changed
195 if ($_POST ["keep_img"] == "yes") {
196 $imgpath = $feature->imgpath;
198 $imgpath = save_uploaded_file ($_FILES ["image_file"], $con);
201 $lon = $_POST ["lon"];
202 $lat = $_POST ["lat"];
203 $title = unquote ($_POST ["title"]);
204 $description = unquote ($_POST ["description"]);
207 $new_feature = new feature ($id, $lon, $lat, $imgpath, $title, $description, 0);
208 } catch (Exception $e) {
212 if (($new_feature->lon == $feature->lon) &&
213 ($new_feature->lat == $feature->lat) &&
214 ($new_feature->title == $feature->title) &&
215 ($new_feature->imgpath == $feature->imgpath) &&
216 ($new_feature->description == $feature->description)) {
217 nochange_error ($feature->id);
221 if ($feature->imgpath && ($feature->imgpath != $new_feature->imgpath)) {
222 $old_imgpath = $feature->imgpath;
226 $con->save_feature ($new_feature);
227 } catch (Exception $e) {
232 delete_image_if_unused ($old_imgpath, $con);
233 } catch (Exception $e) {}
235 success_feature ($new_feature, "update");
238 $imgpath = save_uploaded_file ($_FILES ["image_file"], $con);
240 $lon = $_POST ["lon"];
241 $lat = $_POST ["lat"];
242 $title = unquote ($_POST ["title"]);
243 $description = unquote ($_POST ["description"]);
245 $feature = new feature (null, $lon, $lat, $imgpath, $title, $description, 0);
246 } catch (Exception $e) {
250 $feature = $con->save_feature ($feature);
251 } catch (Exception $e) {
254 success_feature ($feature, "add");
257 $id = $_POST ["fid"];
258 $feature = $con->getfeature ($id);
259 if (!isset ($feature)) {
260 unreferenced_error ($id);
262 $imgpath = $feature->imgpath;
265 $con->delete_feature ($feature);
266 } catch (Exception $e) {
271 delete_image_if_unused ($imgpath, $con);
272 } catch (Exception $e) {}
274 success_delete_feature ($feature);
284 $connection->connect (DBHOST, DBUSER, DBPWD, DBNAME, DBPREFIX);
285 } catch (Exception $e) {