2 /* Copyright (c) 2009 Arnaud Renevier, Inc, published under the modified BSD
5 function exit_document ($body) {
6 exit ("<html><head></head><body>$body</body></html>");
9 function success ($reason) {
10 exit_document ("<success request=\"$reason\"></success>");
13 function success_newuser ($username) {
14 $res = "<success request=\"newuser\"><user>" .
15 htmlspecialchars ($user) .
20 function success_auth ($user) {
21 $res = "<success request=\"$reason\"><user>" .
22 htmlspecialchars ($user) .
27 function success_feature ($feature, $request) {
28 $res = "<success request=\"$request\"><feature>";
29 $res .= "<id>" . $feature->id . "</id>";
33 image_url_from_imgpath ($feature->imgpath)
37 $res .= "<description>" .
38 htmlspecialchars ($feature->description) .
41 // XXX: we do not use <title> because that would be interpreted and
42 // altered by browers html parser
44 htmlspecialchars ($feature->title) .
47 $res .= "<lon>" . $feature->lon . "</lon>";
48 $res .= "<lat>" . $feature->lat . "</lat>";
49 $res .= "</feature></success>";
53 function success_delete_feature ($feature) {
54 $res = "<success request=\"del\"><feature>";
55 $res .= "<id>" . $feature->id . "</id>";
56 $res .= "</feature></success>";
60 function error ($reason) {
61 exit_document ("<error reason=\"$reason\"></error>");
64 function error_newuser_exists () {
65 error ("newuser_exists");
68 function error_feature ($id, $reason) {
69 $res = "<error reason=\"$reason\"><feature>";
70 $res .= "<id>" . $id . "</id>";
71 $res .= "</feature></error>";
75 function error_nochange ($id) {
76 error_feature ($id, "nochange");
78 function error_unreferenced ($id) {
79 error_feature ($id, "unreferenced");
82 function error_server () {
86 function error_unauthorized () {
87 error ("unauthorized");
90 function error_request () {
94 function error_file_too_big () {
98 function error_notanimage () {
102 function save_uploaded_file ($file, $con) {
104 if (isset ($file) && ($file ["error"] != UPLOAD_ERR_NO_FILE)) {
105 img_check_upload ($file);
106 $dest = unique_file (UPLOADDIR, $file ["name"], $con);
107 if (!isset ($dest) ||
108 (!move_uploaded_file ($file ["tmp_name"], $dest))) {
111 $mini_dest = getthumbsdir () . "/mini_" . basename_safe ($dest);
113 if (!create_thumbnail_or_copy ($dest, $mini_dest)) {
117 return basename_safe ($dest);
120 function img_check_upload ($file) {
121 if (!is_uploaded_file ($file ["tmp_name"])) {
122 if ($file ["error"] == UPLOAD_ERR_INI_SIZE) {
123 error_file_too_big ();
128 if (!getimagesize ($file ["tmp_name"])) {
133 function delete_image_if_unused ($imgpath, $con) {
134 if (!isset ($imgpath) || (strlen ($imgpath) == 0)) {
137 if ($con->imgpath_exists ($imgpath)) {
141 $path = UPLOADDIR . "/" . $imgpath;
142 if (file_exists ($path)) {
146 $thumb_path = getthumbsdir () . "/mini_" . $imgpath;
147 if (file_exists ($thumb_path)) {
148 unlink ($thumb_path);
152 function unique_file ($dirname, $relpath, $con) {
153 $relpath = str_replace ('/', '', $relpath); // strip slashes from path
154 $relpath = str_replace ('\\', '', $relpath); // strip antislashes from path
155 $filename = $dirname . '/' . $relpath;
158 $dotpos = strrpos ($relpath, '.');
160 $base = substr ($relpath, 0, $dotpos);
161 $ext = substr ($relpath, $dotpos + 1);
167 while ($counter < 1000) {
168 if (!file_exists ($filename) &&
169 !($con->imgpath_exists (basename_safe ($filename)))) {
173 $filename = $dirname . '/' . $base . '_' . $counter . '.' . $ext;
176 // we tried to find an unused filename 1000 times. Give up now.
180 function check_auth ($con, $user, $pwd, $auth_only) {
181 $authentificated = false;
184 if ($con->checkpwdmd5 ($user, md5 ($pwd))) {
185 // cookie will be valid for 2 weeks. I've chosen that value
186 // arbitrarily, and it may change in the future.
187 $time = time () + 14 * 60 * 24 * 60;
188 setcookie (sprintf ("%sauth", DBPREFIX), md5 ($pwd), $time, "" , "", false, true);
189 setcookie (sprintf ("%suser", DBPREFIX), $user, $time, "" , "", false, true);
190 $authentificated = true;
192 success_auth ($user);
195 error_unauthorized ();
199 if (!$authentificated && !($con->checkpwdmd5 (
200 $_COOKIE [sprintf ("%suser", DBPREFIX)],
201 $_COOKIE [sprintf ("%sauth", DBPREFIX)]))) {
202 error_unauthorized ();
206 function main ($con) {
207 if (!isset ($_POST ["request"])) {
211 $pwd = unquote ($_POST ["password"]);
212 $user = unquote ($_POST ["user"]);
213 // does user only want authentication or does he want to do other things
214 $auth_only = ($_POST ["request"] == "auth");
215 check_auth ($con, $user, $pwd, $auth_only);
217 $user = $_COOKIE [sprintf ("%suser", DBPREFIX)];
220 switch ($_POST ["request"]) {
222 $id = $_POST ["fid"];
223 $feature = $con->getfeature ($id);
224 if (!isset ($feature)) {
225 error_unreferenced ($id);
227 if ($feature->user != $user) {
228 error_unauthorized ();
231 // no file uploaded, but editor currently has an image: it means
232 // image was not changed
233 if ($_POST ["keep_img"] == "yes") {
234 $imgpath = $feature->imgpath;
236 $imgpath = save_uploaded_file ($_FILES ["image_file"], $con);
239 $lon = $_POST ["lon"];
240 $lat = $_POST ["lat"];
241 $title = unquote ($_POST ["title"]);
242 $description = unquote ($_POST ["description"]);
245 $new_feature = new feature ($id, $lon, $lat, $imgpath, $title, $description, 0, $user);
246 } catch (Exception $e) {
250 if (($new_feature->lon == $feature->lon) &&
251 ($new_feature->lat == $feature->lat) &&
252 ($new_feature->title == $feature->title) &&
253 ($new_feature->imgpath == $feature->imgpath) &&
254 ($new_feature->description == $feature->description)) {
255 error_nochange ($feature->id);
259 if ($feature->imgpath && ($feature->imgpath != $new_feature->imgpath)) {
260 $old_imgpath = $feature->imgpath;
264 $con->save_feature ($new_feature);
265 } catch (Exception $e) {
270 delete_image_if_unused ($old_imgpath, $con);
271 } catch (Exception $e) {}
273 success_feature ($new_feature, "update");
276 $imgpath = save_uploaded_file ($_FILES ["image_file"], $con);
278 $lon = $_POST ["lon"];
279 $lat = $_POST ["lat"];
280 $title = unquote ($_POST ["title"]);
281 $description = unquote ($_POST ["description"]);
283 $feature = new feature (null, $lon, $lat, $imgpath, $title, $description, 0, $user);
284 } catch (Exception $e) {
288 $feature = $con->save_feature ($feature);
289 } catch (Exception $e) {
292 success_feature ($feature, "add");
295 $id = $_POST ["fid"];
296 $feature = $con->getfeature ($id);
297 if (!isset ($feature)) {
298 error_unreferenced ($id);
300 if ($feature->user != $user) {
301 error_unauthorized ();
303 $imgpath = $feature->imgpath;
306 $con->delete_feature ($feature);
307 } catch (Exception $e) {
312 delete_image_if_unused ($imgpath, $con);
313 } catch (Exception $e) {}
315 success_delete_feature ($feature);
317 if ($user != "admin") {
318 error_unauthorized ();
320 $newuser_name = unquote ($_POST ["newuser_name"]);
321 if (!$newuser_name) {
324 $newuser_password = unquote ($_POST ["newuser_password"]);
326 $con->setpwd ($newuser_name, $newuser_password, false);
327 } catch (Exception $e) {
328 if ($e->getMessage () == anydbConnection::err_query) {
329 error_newuser_exists ();
334 success_newuser ($newuser_name);
344 if (!@include_once ("./inc/settings.php")) {
347 require_once ("./inc/db/mysql.php");
348 require_once ("./inc/utils.php");
351 $connection->connect (DBHOST, DBUSER, DBPWD, DBNAME, DBPREFIX);
352 } catch (Exception $e) {