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 image_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))) {
101 $mini_dest = getthumbsdir () . "/mini_" . basename_safe ($dest);
104 $thumbnail_ok = create_thumbnail ($dest, $mini_dest);
105 } catch (Exception $e) {
106 $thumbnail_ok = false;
108 if (!$thumbnail_ok) {
109 if (!copy ($dest, $mini_dest)) {
115 return basename_safe ($dest);
118 function img_check_upload ($file) {
119 if (!is_uploaded_file ($file ["tmp_name"])) {
120 if ($file ["error"] == UPLOAD_ERR_INI_SIZE) {
121 file_too_big_error ();
126 if (!getimagesize ($file ["tmp_name"])) {
131 function delete_image_if_unused ($imgpath, $con) {
132 if (!isset ($imgpath) || (strlen ($imgpath) == 0)) {
135 if ($con->imgpath_exists ($imgpath)) {
139 $path = UPLOADDIR . "/" . $imgpath;
140 if (file_exists ($path)) {
144 $thumb_path = getthumbsdir () . "/mini_" . $imgpath;
145 if (file_exists ($thumb_path)) {
146 unlink ($thumb_path);
150 function unique_file ($dirname, $relpath, $con) {
151 $relpath = str_replace ('/', '', $relpath); // strip slashes from path
152 $relpath = str_replace ('\\', '', $relpath); // strip antislashes from path
153 $filename = $dirname . '/' . $relpath;
156 $dotpos = strrpos ($relpath, '.');
158 $base = substr ($relpath, 0, $dotpos);
159 $ext = substr ($relpath, $dotpos + 1);
165 while ($counter < 1000) {
166 if (!file_exists ($filename) &&
167 !($con->imgpath_exists (basename_safe ($filename)))) {
171 $filename = $dirname . '/' . $base . '_' . $counter . '.' . $ext;
174 // we tried to find an unused filename 1000 times. Give up now.
178 function check_auth ($con, $pwd, $cookie_name, $auth_only) {
179 $authentificated = false;
183 if ($con->checkpwdmd5 ($user, md5 ($pwd))) {
184 // cookie will be valid for 2 weeks. I've chosen that value
185 // arbitrarily, and it may change in the future.
186 $time = time () + 14 * 60 * 24 * 60;
187 setcookie ($cookie_name, md5 ($pwd), $time, "" , "", false, true);
188 $authentificated = true;
193 unauthorized_error ();
197 if (!$authentificated && !($con->checkpwdmd5 ($user,
198 $_COOKIE [$cookie_name]))) {
199 unauthorized_error ();
203 function main ($con) {
204 if (!isset ($_POST ["request"])) {
208 $pwd = unquote ($_POST["password"]);
209 $cookie_name = sprintf ("%sauth", DBPREFIX);
210 // does user only want authentication or does he want to do other things
211 $auth_only = ($_POST ["request"] == "auth");
212 check_auth ($con, $pwd, $cookie_name, $auth_only);
214 switch ($_POST ["request"]) {
216 $id = $_POST ["fid"];
217 $feature = $con->getfeature ($id);
218 if (!isset ($feature)) {
219 unreferenced_error ($id);
222 // no file uploaded, but editor currently has an image: it means
223 // image was not changed
224 if ($_POST ["keep_img"] == "yes") {
225 $imgpath = $feature->imgpath;
227 $imgpath = save_uploaded_file ($_FILES ["image_file"], $con);
230 $lon = $_POST ["lon"];
231 $lat = $_POST ["lat"];
232 $title = unquote ($_POST ["title"]);
233 $description = unquote ($_POST ["description"]);
236 $new_feature = new feature ($id, $lon, $lat, $imgpath, $title, $description, 0);
237 } catch (Exception $e) {
241 if (($new_feature->lon == $feature->lon) &&
242 ($new_feature->lat == $feature->lat) &&
243 ($new_feature->title == $feature->title) &&
244 ($new_feature->imgpath == $feature->imgpath) &&
245 ($new_feature->description == $feature->description)) {
246 nochange_error ($feature->id);
250 if ($feature->imgpath && ($feature->imgpath != $new_feature->imgpath)) {
251 $old_imgpath = $feature->imgpath;
255 $con->save_feature ($new_feature);
256 } catch (Exception $e) {
261 delete_image_if_unused ($old_imgpath, $con);
262 } catch (Exception $e) {}
264 success_feature ($new_feature, "update");
267 $imgpath = save_uploaded_file ($_FILES ["image_file"], $con);
269 $lon = $_POST ["lon"];
270 $lat = $_POST ["lat"];
271 $title = unquote ($_POST ["title"]);
272 $description = unquote ($_POST ["description"]);
274 $feature = new feature (null, $lon, $lat, $imgpath, $title, $description, 0);
275 } catch (Exception $e) {
279 $feature = $con->save_feature ($feature);
280 } catch (Exception $e) {
283 success_feature ($feature, "add");
286 $id = $_POST ["fid"];
287 $feature = $con->getfeature ($id);
288 if (!isset ($feature)) {
289 unreferenced_error ($id);
291 $imgpath = $feature->imgpath;
294 $con->delete_feature ($feature);
295 } catch (Exception $e) {
300 delete_image_if_unused ($imgpath, $con);
301 } catch (Exception $e) {}
303 success_delete_feature ($feature);
313 $connection->connect (DBHOST, DBUSER, DBPWD, DBNAME, DBPREFIX);
314 } catch (Exception $e) {