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 ($user) {
14 $res = "<success request=\"$reason\"><user>" .
15 htmlspecialchars ($user) .
20 function success_feature ($feature, $request) {
21 $res = "<success request=\"$request\"><feature>";
22 $res .= "<id>" . $feature->id . "</id>";
26 image_url_from_imgpath ($feature->imgpath)
30 $res .= "<description>" .
31 htmlspecialchars ($feature->description) .
34 // XXX: we do not use <title> because that would be interpreted and
35 // altered by browers html parser
37 htmlspecialchars ($feature->title) .
40 $res .= "<lon>" . $feature->lon . "</lon>";
41 $res .= "<lat>" . $feature->lat . "</lat>";
42 $res .= "</feature></success>";
46 function success_delete_feature ($feature) {
47 $res = "<success request=\"del\"><feature>";
48 $res .= "<id>" . $feature->id . "</id>";
49 $res .= "</feature></success>";
53 function success ($reason) {
54 exit_document ("<success request=\"$reason\"></success>");
57 function error ($reason) {
58 exit_document ("<error reason=\"$reason\"></error>");
61 function error_feature ($id, $reason) {
62 $res = "<error reason=\"$reason\"><feature>";
63 $res .= "<id>" . $id . "</id>";
64 $res .= "</feature></error>";
68 function nochange_error ($id) {
69 error_feature ($id, "nochange");
71 function unreferenced_error ($id) {
72 error_feature ($id, "unreferenced");
75 function server_error () {
79 function unauthorized_error () {
80 error ("unauthorized");
83 function request_error () {
87 function file_too_big_error () {
91 function notanimage_error () {
95 function save_uploaded_file ($file, $con) {
97 if (isset ($file) && ($file ["error"] != UPLOAD_ERR_NO_FILE)) {
98 img_check_upload ($file);
99 $dest = unique_file (UPLOADDIR, $file ["name"], $con);
100 if (!isset ($dest) ||
101 (!move_uploaded_file ($file ["tmp_name"], $dest))) {
104 $mini_dest = getthumbsdir () . "/mini_" . basename_safe ($dest);
106 if (!create_thumbnail_or_copy ($dest, $mini_dest)) {
110 return basename_safe ($dest);
113 function img_check_upload ($file) {
114 if (!is_uploaded_file ($file ["tmp_name"])) {
115 if ($file ["error"] == UPLOAD_ERR_INI_SIZE) {
116 file_too_big_error ();
121 if (!getimagesize ($file ["tmp_name"])) {
126 function delete_image_if_unused ($imgpath, $con) {
127 if (!isset ($imgpath) || (strlen ($imgpath) == 0)) {
130 if ($con->imgpath_exists ($imgpath)) {
134 $path = UPLOADDIR . "/" . $imgpath;
135 if (file_exists ($path)) {
139 $thumb_path = getthumbsdir () . "/mini_" . $imgpath;
140 if (file_exists ($thumb_path)) {
141 unlink ($thumb_path);
145 function unique_file ($dirname, $relpath, $con) {
146 $relpath = str_replace ('/', '', $relpath); // strip slashes from path
147 $relpath = str_replace ('\\', '', $relpath); // strip antislashes from path
148 $filename = $dirname . '/' . $relpath;
151 $dotpos = strrpos ($relpath, '.');
153 $base = substr ($relpath, 0, $dotpos);
154 $ext = substr ($relpath, $dotpos + 1);
160 while ($counter < 1000) {
161 if (!file_exists ($filename) &&
162 !($con->imgpath_exists (basename_safe ($filename)))) {
166 $filename = $dirname . '/' . $base . '_' . $counter . '.' . $ext;
169 // we tried to find an unused filename 1000 times. Give up now.
173 function check_auth ($con, $user, $pwd, $auth_only) {
174 $authentificated = false;
177 if ($con->checkpwdmd5 ($user, md5 ($pwd))) {
178 // cookie will be valid for 2 weeks. I've chosen that value
179 // arbitrarily, and it may change in the future.
180 $time = time () + 14 * 60 * 24 * 60;
181 setcookie (sprintf ("%sauth", DBPREFIX), md5 ($pwd), $time, "" , "", false, true);
182 setcookie (sprintf ("%suser", DBPREFIX), $user, $time, "" , "", false, true);
183 $authentificated = true;
185 success_auth ($user);
188 unauthorized_error ();
192 if (!$authentificated && !($con->checkpwdmd5 (
193 $_COOKIE [sprintf ("%suser", DBPREFIX)],
194 $_COOKIE [sprintf ("%sauth", DBPREFIX)]))) {
195 unauthorized_error ();
199 function main ($con) {
200 if (!isset ($_POST ["request"])) {
204 $pwd = unquote ($_POST ["password"]);
205 $user = unquote ($_POST ["user"]);
206 // does user only want authentication or does he want to do other things
207 $auth_only = ($_POST ["request"] == "auth");
208 check_auth ($con, $user, $pwd, $auth_only);
210 $user = $_COOKIE [sprintf ("%suser", DBPREFIX)];
213 switch ($_POST ["request"]) {
215 $id = $_POST ["fid"];
216 $feature = $con->getfeature ($id);
217 if (!isset ($feature)) {
218 unreferenced_error ($id);
220 if ($feature->user != $user) {
221 unauthorized_error ();
224 // no file uploaded, but editor currently has an image: it means
225 // image was not changed
226 if ($_POST ["keep_img"] == "yes") {
227 $imgpath = $feature->imgpath;
229 $imgpath = save_uploaded_file ($_FILES ["image_file"], $con);
232 $lon = $_POST ["lon"];
233 $lat = $_POST ["lat"];
234 $title = unquote ($_POST ["title"]);
235 $description = unquote ($_POST ["description"]);
238 $new_feature = new feature ($id, $lon, $lat, $imgpath, $title, $description, 0, $user);
239 } catch (Exception $e) {
243 if (($new_feature->lon == $feature->lon) &&
244 ($new_feature->lat == $feature->lat) &&
245 ($new_feature->title == $feature->title) &&
246 ($new_feature->imgpath == $feature->imgpath) &&
247 ($new_feature->description == $feature->description)) {
248 nochange_error ($feature->id);
252 if ($feature->imgpath && ($feature->imgpath != $new_feature->imgpath)) {
253 $old_imgpath = $feature->imgpath;
257 $con->save_feature ($new_feature);
258 } catch (Exception $e) {
263 delete_image_if_unused ($old_imgpath, $con);
264 } catch (Exception $e) {}
266 success_feature ($new_feature, "update");
269 $imgpath = save_uploaded_file ($_FILES ["image_file"], $con);
271 $lon = $_POST ["lon"];
272 $lat = $_POST ["lat"];
273 $title = unquote ($_POST ["title"]);
274 $description = unquote ($_POST ["description"]);
276 $feature = new feature (null, $lon, $lat, $imgpath, $title, $description, 0, $user);
277 } catch (Exception $e) {
281 $feature = $con->save_feature ($feature);
282 } catch (Exception $e) {
285 success_feature ($feature, "add");
288 $id = $_POST ["fid"];
289 $feature = $con->getfeature ($id);
290 if (!isset ($feature)) {
291 unreferenced_error ($id);
293 if ($feature->user != $user) {
294 unauthorized_error ();
296 $imgpath = $feature->imgpath;
299 $con->delete_feature ($feature);
300 } catch (Exception $e) {
305 delete_image_if_unused ($imgpath, $con);
306 } catch (Exception $e) {}
308 success_delete_feature ($feature);
318 $connection->connect (DBHOST, DBUSER, DBPWD, DBNAME, DBPREFIX);
319 } catch (Exception $e) {