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