]> dev.renevier.net Git - syp.git/blob - api.php
fixe setcookie calls for php < 5.2.0
[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     if (version_compare (PHP_VERSION, '5.2.0', '>=')) {
196         setcookie (sprintf ("%sauth", DBPREFIX), md5 ($pwd), $time, "" , "", false, true);
197         setcookie (sprintf ("%suser", DBPREFIX), $user, $time, "" , "", false, true);
198     } else {
199         setcookie (sprintf ("%sauth", DBPREFIX), md5 ($pwd), $time, "" , "", false);
200         setcookie (sprintf ("%suser", DBPREFIX), $user, $time, "" , "", false);
201     }
202
203 }
204
205 function check_auth ($con, $user, $pwd, $auth_only) {
206     $authentificated = false;
207
208     if (isset ($pwd)) {
209         if ($con->checkpwdmd5 ($user, md5 ($pwd))) {
210             setcookies ($user, $pwd);
211             $authentificated = true;
212             if ($auth_only) {
213                 success_auth ($user);
214             }
215         } else {
216             error_unauthorized ();
217         }
218     }
219
220     if (!$authentificated && !($con->checkpwdmd5 (
221                              $_COOKIE [sprintf ("%suser",  DBPREFIX)],
222                              $_COOKIE [sprintf ("%sauth",  DBPREFIX)]))) {
223         error_unauthorized ();
224     }
225 }
226
227 function main ($con) {
228     if (!isset ($_POST ["request"])) {
229         error_request ();
230     }
231
232     $pwd = unquote ($_POST ["password"]);
233     $user = unquote ($_POST ["user"]);
234     // does user only want authentication or does he want to do other things
235     $auth_only = ($_POST ["request"] == "auth");
236     check_auth ($con, $user, $pwd, $auth_only);
237     if (!$user) {
238         $user = $_COOKIE [sprintf ("%suser",  DBPREFIX)];
239     }
240
241     switch ($_POST ["request"]) {
242         case "update":
243             $id = $_POST ["fid"];
244             $feature = $con->getfeature ($id);
245             if (!isset ($feature)) {
246                 error_unreferenced ($id);
247             }
248             if (($feature->user != $user) && ($user != "admin")) {
249                 error_unauthorized ();
250             }
251
252             // no file uploaded, but editor currently has an image: it means
253             // image was not changed
254             if ($_POST ["keep_img"] == "yes") {
255                 $imgpath = $feature->imgpath;
256             } else {
257                 $imgpath = save_uploaded_file ($_FILES ["image_file"], $con);
258             }
259
260             $lon = $_POST ["lon"];
261             $lat = $_POST ["lat"];
262             $title = unquote ($_POST ["title"]);
263             $description = unquote ($_POST ["description"]);
264
265             try {
266                 $new_feature = new feature ($id, $lon, $lat, $imgpath, $title, $description, 0, $user);
267             } catch (Exception $e) {
268                 error_request ();
269             }
270
271             if (($new_feature->lon == $feature->lon) &&
272                 ($new_feature->lat == $feature->lat) &&
273                 ($new_feature->title == $feature->title) &&
274                 ($new_feature->imgpath == $feature->imgpath) &&
275                 ($new_feature->description == $feature->description)) {
276                 error_nochange ($feature->id);
277             }
278
279             $old_imgpath = "";
280             if ($feature->imgpath && ($feature->imgpath != $new_feature->imgpath)) {
281                 $old_imgpath = $feature->imgpath;
282             }
283
284             try {
285                 $con->save_feature ($new_feature);
286             } catch (Exception $e) {
287                 error_server ();
288             }
289             if ($old_imgpath) {
290                 try {
291                     delete_image_if_unused ($old_imgpath, $con); 
292                 } catch (Exception $e) {}
293             }
294             success_feature ($new_feature, "update");
295         break;
296         case "add":
297             $imgpath = save_uploaded_file ($_FILES ["image_file"], $con);
298
299             $lon = $_POST ["lon"];
300             $lat = $_POST ["lat"];
301             $title = unquote ($_POST ["title"]);
302             $description = unquote ($_POST ["description"]);
303             try {
304                 $feature = new feature (null, $lon, $lat, $imgpath, $title, $description, 0, $user);
305             } catch (Exception $e) {
306                 error_request ();
307             }
308             try {
309                 $feature = $con->save_feature ($feature);
310             } catch (Exception $e) {
311                 error_server ();
312             }
313             success_feature ($feature, "add");
314         break;
315         case "del":
316             $id = $_POST ["fid"];
317             $feature = $con->getfeature ($id);
318             if (!isset ($feature)) {
319                 error_unreferenced ($id);
320             }
321             if ($feature->user != $user) {
322                 error_unauthorized ();
323             }
324             $imgpath = $feature->imgpath;
325
326             try {
327                 $con->delete_feature ($feature);
328             } catch (Exception $e) {
329                 error_server ();
330             }
331
332             try {
333                 delete_image_if_unused ($imgpath, $con);
334             } catch (Exception $e) {}
335
336             success_delete_feature ($feature);
337         case "changepass":
338             $currpass = unquote ($_POST ["pass_current"]);
339             if (!$con->checkpwdmd5 ($user, md5 ($currpass))) {
340                 error_wrongpass ();
341             }
342             $newpass = unquote ($_POST ["pass_new"]);
343             try {
344                 $con->setpwd ($user, $newpass);
345             } catch (Exception $e) {
346                 error_server ();
347             }
348             setcookies ($user, $newpass);
349             success_changepass ($user);
350         break;
351         case "newuser":
352             if ($user != "admin") {
353                 error_unauthorized ();
354             }
355             $newuser_name = unquote ($_POST ["newuser_name"]);
356             if (!$newuser_name) {
357                 error_request ();
358             }
359             if ($con->user_exists ($newuser_name)) {
360                 error_newuser_exists ();
361             }
362             $newuser_password = unquote ($_POST ["newuser_password"]);
363             try {
364                 $con->setpwd ($newuser_name, $newuser_password);
365             } catch (Exception $e) {
366                 error_server ();
367             }
368             success_newuser ($newuser_name);
369         break;
370         default:
371             error_request();
372         break;
373     }
374
375     error_server ();
376 }
377
378 if (!@include_once ("./inc/settings.php")) {
379     error_server ();
380 }
381 require_once ("./inc/db/mysql.php");
382 require_once ("./inc/utils.php");
383
384 try {
385     $connection->connect (DBHOST, DBUSER, DBPWD, DBNAME, DBPREFIX);
386 } catch (Exception $e) {
387     error_server ();
388 }
389
390 main ($connection);
391 ?>