]> dev.renevier.net Git - syp.git/blob - api.php
admin as default login user
[syp.git] / api.php
1 <?php
2 /* Copyright (c) 2009 Arnaud Renevier, Inc, published under the modified BSD
3    license. */
4
5 function exit_document ($body) {
6     $charset_meta = '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">';
7     exit ("<html>$charset_meta<head></head><body>$body</body></html>");
8 }
9
10 function success ($reason) {
11     exit_document ("<success request=\"$reason\"></success>");
12 }
13
14 function success_changepass ($username) {
15     $res = "<success request=\"changepass\"><user>" .
16             htmlspecialchars ($username) .
17             "</user></success>";
18     exit_document ($res);
19 }
20
21 function success_newuser ($username) {
22     $res = "<success request=\"newuser\"><user>" .
23             htmlspecialchars ($username) .
24             "</user></success>";
25     exit_document ($res);
26 }
27
28 function success_auth ($user) {
29     $res = "<success request=\"$reason\"><user>" . 
30             htmlspecialchars ($user) .
31             "</user></success>";
32     exit_document ($res);
33 }
34
35 function success_feature ($feature, $request) {
36     $res = "<success request=\"$request\"><feature>";
37     $res .= "<id>" .  $feature->id .  "</id>";
38
39     $res .= "<imgurl>" .
40              ($feature->imgpath ? 
41                     image_url_from_imgpath ($feature->imgpath)
42                     : "") .
43              "</imgurl>";
44
45     $res .= "<description>" .
46                  htmlspecialchars ($feature->description) .
47                  "</description>";
48
49     // XXX: we do not use <title> because that would be interpreted and
50     // altered by browers html parser
51     $res .= "<heading>" . 
52             htmlspecialchars ($feature->title) .
53             "</heading>";
54
55     $res .= "<lon>" . $feature->lon . "</lon>";
56     $res .= "<lat>" . $feature->lat . "</lat>";
57     $res .= "</feature></success>";
58     exit_document ($res);
59 }
60
61 function success_delete_feature ($feature) {
62     $res = "<success request=\"del\"><feature>";
63     $res .= "<id>" .  $feature->id .  "</id>";
64     $res .= "</feature></success>";
65     exit_document ($res);
66 }
67
68 function error ($reason) {
69     exit_document ("<error reason=\"$reason\"></error>");
70 }
71
72 function error_newuser_exists () {
73     error ("newuser_exists");
74 }
75
76 function error_feature ($id, $reason) {
77     $res = "<error reason=\"$reason\"><feature>";
78     $res .= "<id>" .  $id .  "</id>";
79     $res .= "</feature></error>";
80     exit_document ($res);
81 }
82
83 function error_nochange ($id) {
84     error_feature ($id, "nochange");
85 }
86 function error_unreferenced ($id) {
87     error_feature ($id, "unreferenced");
88 }
89
90 function error_server () {
91     error ("server");
92 }
93
94 function error_wrongpass () {
95     error ("wrongpass");
96 }
97
98 function error_unauthorized () {
99     error ("unauthorized");
100 }
101
102 function error_request () {
103     error ("request");
104 }
105
106 function error_file_too_big () {
107     error ("toobig");
108 }
109
110 function error_notanimage () {
111     error ("notimage");
112 }
113
114 function save_uploaded_file ($file, $con) {
115     $dest = "";
116     if (isset ($file) && ($file ["error"] != UPLOAD_ERR_NO_FILE)) {
117         img_check_upload ($file);
118         $dest = unique_file (UPLOADDIR, $file ["name"], $con);
119         if (!isset ($dest) || 
120                 (!move_uploaded_file ($file ["tmp_name"], $dest))) {
121             error_server ();
122         }
123         $mini_dest = getthumbsdir () . "/mini_" . basename_safe ($dest);
124
125         if (!create_thumbnail_or_copy ($dest, $mini_dest)) {
126             error_server ();
127         }
128     }
129     return basename_safe ($dest);
130 }
131
132 function img_check_upload ($file) {
133     if (!is_uploaded_file ($file ["tmp_name"])) {
134         if ($file ["error"] ==  UPLOAD_ERR_INI_SIZE) {
135             error_file_too_big ();
136         } else {
137             error_server ();
138         }
139     }
140     if (!getimagesize ($file ["tmp_name"])) {
141         error_notanimage ();
142     }
143 }
144
145 function delete_image_if_unused ($imgpath, $con) {
146     if (!isset ($imgpath) || (strlen ($imgpath) == 0)) {
147         return;
148     }
149     if ($con->imgpath_exists ($imgpath)) {
150         return;
151     }
152
153     $path = UPLOADDIR . "/" . $imgpath;
154     if (file_exists ($path)) {
155         unlink ($path);
156     }
157
158     $thumb_path = getthumbsdir () . "/mini_" . $imgpath;
159     if (file_exists ($thumb_path)) {
160         unlink ($thumb_path);
161     }
162 }
163
164 function unique_file ($dirname, $relpath, $con) {
165    $relpath = str_replace ('/', '', $relpath); // strip slashes from path
166    $relpath = str_replace ('\\', '', $relpath); // strip antislashes from path
167    $filename = $dirname . '/' . $relpath;
168    $counter = 1;
169
170    $dotpos = strrpos ($relpath, '.');
171    if ($dotpos) {
172        $base = substr ($relpath, 0, $dotpos);
173        $ext = substr ($relpath, $dotpos + 1);
174    } else {
175        $base = $relpath;
176        $ext = "";
177    }
178
179    while ($counter < 1000) {
180        if (!file_exists ($filename) && 
181            !($con->imgpath_exists (basename_safe ($filename)))) {
182            return $filename;
183        } else {
184             $counter++;
185             $filename = $dirname . '/' . $base . '_' . $counter . '.' . $ext;
186        }
187    }
188    // we tried to find an unused filename 1000 times. Give up now.
189    return null;
190 }
191
192 function setcookies ($user, $pwd) {
193     // cookie will be valid for 2 weeks. I've chosen that value
194     // arbitrarily, and it may change in the future.
195     $time = time () + 14 * 60 * 24 * 60;
196     if (version_compare (PHP_VERSION, '5.2.0', '>=')) {
197         setcookie (sprintf ("%sauth", DBPREFIX), md5 ($pwd), $time, "" , "", false, true);
198         setcookie (sprintf ("%suser", DBPREFIX), $user, $time, "" , "", false, true);
199     } else {
200         setcookie (sprintf ("%sauth", DBPREFIX), md5 ($pwd), $time, "" , "", false);
201         setcookie (sprintf ("%suser", DBPREFIX), $user, $time, "" , "", false);
202     }
203
204 }
205
206 function check_auth ($con, $user, $pwd, $auth_only) {
207     $authentificated = false;
208
209     if (isset ($pwd)) {
210         if ($con->checkpwdmd5 ($user, md5 ($pwd))) {
211             setcookies ($user, $pwd);
212             $authentificated = true;
213             if ($auth_only) {
214                 success_auth ($user);
215             }
216         } else {
217             error_unauthorized ();
218         }
219     }
220
221     if (!$authentificated && !($con->checkpwdmd5 (
222                              $_COOKIE [sprintf ("%suser",  DBPREFIX)],
223                              $_COOKIE [sprintf ("%sauth",  DBPREFIX)]))) {
224         error_unauthorized ();
225     }
226 }
227
228 function main ($con) {
229     if (!isset ($_POST ["request"])) {
230         error_request ();
231     }
232
233     $pwd = unquote ($_POST ["password"]);
234     $user = unquote ($_POST ["user"]);
235     // does user only want authentication or does he want to do other things
236     $auth_only = ($_POST ["request"] == "auth");
237     check_auth ($con, $user, $pwd, $auth_only);
238     if (!$user) {
239         $user = $_COOKIE [sprintf ("%suser",  DBPREFIX)];
240     }
241
242     switch ($_POST ["request"]) {
243         case "update":
244             $id = $_POST ["fid"];
245             $feature = $con->getfeature ($id);
246             if (!isset ($feature)) {
247                 error_unreferenced ($id);
248             }
249             if (($feature->user != $user) && ($user != "admin")) {
250                 error_unauthorized ();
251             }
252
253             // no file uploaded, but editor currently has an image: it means
254             // image was not changed
255             if ($_POST ["keep_img"] == "yes") {
256                 $imgpath = $feature->imgpath;
257             } else {
258                 error_request ();
259                 $imgpath = save_uploaded_file ($_FILES ["image_file"], $con);
260             }
261
262             $lon = $_POST ["lon"];
263             $lat = $_POST ["lat"];
264             $title = unquote ($_POST ["title"]);
265             $description = unquote ($_POST ["description"]);
266
267             try {
268                 $new_feature = new feature ($id, $lon, $lat, $imgpath, $title, $description, 0, $user);
269             } catch (Exception $e) {
270                 error_request ();
271             }
272
273             if (($new_feature->lon == $feature->lon) &&
274                 ($new_feature->lat == $feature->lat) &&
275                 ($new_feature->title == $feature->title) &&
276                 ($new_feature->imgpath == $feature->imgpath) &&
277                 ($new_feature->description == $feature->description)) {
278                 error_nochange ($feature->id);
279             }
280
281             $old_imgpath = "";
282             if ($feature->imgpath && ($feature->imgpath != $new_feature->imgpath)) {
283                 $old_imgpath = $feature->imgpath;
284             }
285
286             try {
287                 $con->save_feature ($new_feature);
288             } catch (Exception $e) {
289                 error_server ();
290             }
291             if ($old_imgpath) {
292                 try {
293                     delete_image_if_unused ($old_imgpath, $con); 
294                 } catch (Exception $e) {}
295             }
296             success_feature ($new_feature, "update");
297         break;
298         case "add":
299             error_request ();
300             $imgpath = save_uploaded_file ($_FILES ["image_file"], $con);
301
302             $lon = $_POST ["lon"];
303             $lat = $_POST ["lat"];
304             $title = unquote ($_POST ["title"]);
305             $description = unquote ($_POST ["description"]);
306             try {
307                 $feature = new feature (null, $lon, $lat, $imgpath, $title, $description, 0, $user);
308             } catch (Exception $e) {
309                 error_request ();
310             }
311             try {
312                 $feature = $con->save_feature ($feature);
313             } catch (Exception $e) {
314                 error_server ();
315             }
316             success_feature ($feature, "add");
317         break;
318         case "del":
319             error_request ();
320             $id = $_POST ["fid"];
321             $feature = $con->getfeature ($id);
322             if (!isset ($feature)) {
323                 error_unreferenced ($id);
324             }
325             if ($feature->user != $user) {
326                 error_unauthorized ();
327             }
328             $imgpath = $feature->imgpath;
329
330             try {
331                 $con->delete_feature ($feature);
332             } catch (Exception $e) {
333                 error_server ();
334             }
335
336             try {
337                 delete_image_if_unused ($imgpath, $con);
338             } catch (Exception $e) {}
339
340             success_delete_feature ($feature);
341         case "changepass":
342             error_request ();
343             $currpass = unquote ($_POST ["pass_current"]);
344             if (!$con->checkpwdmd5 ($user, md5 ($currpass))) {
345                 error_wrongpass ();
346             }
347             $newpass = unquote ($_POST ["pass_new"]);
348             try {
349                 $con->setpwd ($user, $newpass);
350             } catch (Exception $e) {
351                 if ($e->getMessage () == anydbConnection::err_query) {
352                     error_request ();
353                 }
354                 error_server ();
355             }
356             setcookies ($user, $newpass);
357             success_changepass ($user);
358         break;
359         case "newuser":
360             error_request ();
361             if ($user != "admin") {
362                 error_unauthorized ();
363             }
364             $newuser_name = unquote ($_POST ["newuser_name"]);
365             if (!$newuser_name) {
366                 error_request ();
367             }
368             if ($con->user_exists ($newuser_name)) {
369                 error_newuser_exists ();
370             }
371             $newuser_password = unquote ($_POST ["newuser_password"]);
372             try {
373                 $con->setpwd ($newuser_name, $newuser_password);
374             } catch (Exception $e) {
375                 if ($e->getMessage () == anydbConnection::err_query) {
376                     error_request ();
377                 }
378                 error_server ();
379             }
380             success_newuser ($newuser_name);
381         break;
382         default:
383             error_request();
384         break;
385     }
386
387     error_server ();
388 }
389
390 if (!@include_once ("./inc/settings.php")) {
391     error_server ();
392 }
393 require_once ("./inc/db/" . (defined ("DBTYPE")? DBTYPE: "mysql") . ".php");
394 require_once ("./inc/utils.php");
395
396 try {
397     $connection->connect (DBHOST, DBUSER, DBPWD, DBNAME, DBPREFIX);
398 } catch (Exception $e) {
399     error_server ();
400 }
401
402 main ($connection);
403 ?>