]> dev.renevier.net Git - syp.git/blob - api.php
improves behaviour after unauthorized server reply
[syp.git] / api.php
1 <?php
2 /* Copyright (c) 2009 Arnaud Renevier, Inc, published under the modified BSD
3    license. */
4
5 require_once ("./inc/settings.php");
6 require_once ("./inc/db/mysql.php");
7 require_once ("./inc/utils.php");
8
9 function exit_document ($body) {
10     exit ("<html><head></head><body>$body</body></html>");
11 }
12
13 function success_auth () {
14     success ("auth");
15 }
16
17 function success_feature ($feature, $request) {
18     $res = "<success request=\"$request\"><feature>";
19     $res .= "<id>" .  $feature->id .  "</id>";
20
21     $res .= "<imgurl>" .
22              ($feature->imgpath ? 
23                     full_url_from_filename ($feature->imgpath)
24                     : "") .
25              "</imgurl>";
26
27     $res .= "<description>" .
28                  htmlspecialchars ($feature->description) .
29                  "</description>";
30
31     // XXX: we do not use <title> because that would be interpreted and
32     // altered by browers html parser
33     $res .= "<heading>" . 
34             htmlspecialchars ($feature->title) .
35             "</heading>";
36
37     $res .= "<lon>" . $feature->lon . "</lon>";
38     $res .= "<lat>" . $feature->lat . "</lat>";
39     $res .= "</feature></success>";
40     exit_document ($res);
41 }
42
43 function success_delete_feature ($feature) {
44     $res = "<success request=\"del\"><feature>";
45     $res .= "<id>" .  $feature->id .  "</id>";
46     $res .= "</feature></success>";
47     exit_document ($res);
48 }
49
50 function success ($reason) {
51     exit_document ("<success request=\"$reason\"></success>");
52 }
53
54 function error ($reason) {
55     exit_document ("<error reason=\"$reason\"></error>");
56 }
57
58 function error_feature ($id, $reason) {
59     $res = "<error reason=\"$reason\"><feature>";
60     $res .= "<id>" .  $id .  "</id>";
61     $res .= "</feature></error>";
62     exit_document ($res);
63 }
64
65 function nochange_error ($id) {
66     error_feature ($id, "nochange");
67 }
68 function unreferenced_error ($id) {
69     error_feature ($id, "unreferenced");
70 }
71
72 function server_error () {
73     error ("server");
74 }
75
76 function unauthorized_error () {
77     error ("unauthorized");
78 }
79
80 function request_error () {
81     error ("request");
82 }
83
84 function file_too_big_error () {
85     error ("toobig");
86 }
87
88 function notanimage_error () {
89     error ("notimage");
90 }
91
92 function save_uploaded_file ($file, $con) {
93     $dest = "";
94     if (isset ($file) && ($file ["error"] != UPLOAD_ERR_NO_FILE)) {
95         img_check_upload ($file);
96         $dest = unique_file (UPLOADDIR, $file ["name"], $con);
97         if (!isset ($dest) || 
98                 (!move_uploaded_file ($file ["tmp_name"], $dest))) {
99             server_error ();
100         }
101     }
102     return basename($dest);
103 }
104
105 function img_check_upload ($file) {
106     if (!is_uploaded_file ($file ["tmp_name"])) {
107         if ($file ["error"] ==  UPLOAD_ERR_INI_SIZE) {
108             file_too_big_error ();
109         } else {
110             server_error ();
111         }
112     }
113     if (!getimagesize ($file ["tmp_name"])) {
114         notanimage_error ();
115     }
116 }
117
118 function delete_image_if_unused ($imgpath, $con) {
119     if ($con->imgpath_exists ($imgpath)) {
120         return false;
121     }
122     $path = UPLOADDIR . "/" . $imgpath;
123     if (file_exists($path)) {
124         unlink ($path);
125         return true;
126     } else {
127         return false;
128     }
129 }
130
131 function unique_file ($dirname, $relpath, $con) {
132    $relpath = str_replace ('/', '', $relpath); // strip slashes from path
133    $relpath = str_replace ('\\', '', $relpath); // strip antislashes from path
134    $filename = $dirname . '/' . $relpath;
135    $counter = 1;
136
137    $dotpos = strrpos ($relpath, '.');
138    if ($dotpos) {
139        $base = substr ($relpath, 0, $dotpos);
140        $ext = substr ($relpath, $dotpos + 1);
141    } else {
142        $base = $relpath;
143        $ext = "";
144    }
145
146    while ($counter < 1000) {
147        if (!file_exists ($filename) && 
148            !($con->imgpath_exists (basename ($filename)))) {
149            return $filename;
150        } else {
151             $counter++;
152             $filename = $dirname . '/' . $base . '_' . $counter . '.' . $ext;
153        }
154    }
155    // we tried to find an unused filename 1000 times. Give up now.
156    return null;
157 }
158
159 function main ($con) {
160     if (!isset ($_POST ["request"])) {
161         request_error ();
162     }
163     if ($_POST ["request"] == "auth") {
164         $pwd = unquote ($_POST["password"]);
165         $user = "admin";
166         if ($con->checkpwdmd5 ($user, md5 ($pwd))) {
167             // cookie will be valid for 2 weeks. I've chosen that value
168             // arbitrarily, and it may change in the future.
169             $time = time () + 14 * 60 * 24 * 60;
170             $cookie_name = sprintf ("%sauth", DBPREFIX);
171             setcookie ($cookie_name, md5 ($pwd), $time, "" , "", false, true);
172             success_auth ();
173         } else {
174             unauthorized_error ();
175         }
176     }
177     if (!($con->checkpwdmd5 ("admin",
178                              $_COOKIE [sprintf ("%sauth", DBPREFIX)]))) {
179         unauthorized_error ();
180     }
181
182     switch ($_POST ["request"]) {
183         case "update":
184             $id = $_POST ["fid"];
185             $feature = $con->getfeature ($id);
186             if (!isset ($feature)) {
187                 unreferenced_error ($id);
188             }
189
190             // no file uploaded, but editor currently has an image: it means
191             // image was not changed
192             if ($_POST ["keep_img"] == "yes") {
193                 $imgpath = $feature->imgpath;
194             } else {
195                 $imgpath = save_uploaded_file ($_FILES ["image_file"], $con);
196             }
197
198             $lon = $_POST ["lon"];
199             $lat = $_POST ["lat"];
200             $title = unquote ($_POST ["title"]);
201             $description = unquote ($_POST ["description"]);
202
203             try {
204                 $new_feature = new feature ($id, $lon, $lat, $imgpath, $title, $description);
205             } catch (Exception $e) {
206                 request_error ();
207             }
208
209             if (($new_feature->lon == $feature->lon) &&
210                 ($new_feature->lat == $feature->lat) &&
211                 ($new_feature->title == $feature->title) &&
212                 ($new_feature->imgpath == $feature->imgpath) &&
213                 ($new_feature->description == $feature->description)) {
214                 nochange_error ($feature->id);
215             }
216
217             $old_imgpath = "";
218             if ($feature->imgpath && ($feature->imgpath != $new_feature->imgpath)) {
219                 $old_imgpath = $feature->imgpath;
220             }
221
222             try {
223                 $con->save_feature ($new_feature);
224             } catch (Exception $e) {
225                 server_error ();
226             }
227             if ($old_imgpath) {
228                 try {
229                     delete_image_if_unused ($old_imgpath, $con); 
230                 } catch (Exception $e) {}
231             }
232             success_feature ($new_feature, "update");
233         break;
234         case "add":
235             $imgpath = save_uploaded_file ($_FILES ["image_file"], $con);
236
237             $lon = $_POST ["lon"];
238             $lat = $_POST ["lat"];
239             $title = unquote ($_POST ["title"]);
240             $description = unquote ($_POST ["description"]);
241             try {
242                 $feature = new feature (null, $lon, $lat, $imgpath, $title, $description);
243             } catch (Exception $e) {
244                 request_error ();
245             }
246             try {
247                 $feature = $con->save_feature ($feature);
248             } catch (Exception $e) {
249                 server_error ();
250             }
251             success_feature ($feature, "add");
252         break;
253         case "del":
254             $id = $_POST ["fid"];
255             $feature = $con->getfeature ($id);
256             if (!isset ($feature)) {
257                 unreferenced_error ($id);
258             }
259             $imgpath = $feature->imgpath;
260
261             try {
262                 $con->delete_feature ($feature);
263             } catch (Exception $e) {
264                 server_error ();
265             }
266
267             try {
268                 delete_image_if_unused ($imgpath, $con);
269             } catch (Exception $e) {}
270
271             success_delete_feature ($feature);
272         default:
273             request_error();
274         break;
275     }
276
277     server_error ();
278 }
279
280 try {
281     $connection->connect (DBHOST, DBUSER, DBPWD, DBNAME, DBPREFIX);
282 } catch (Exception $e) {
283     server_error ();
284 }
285
286 main ($connection);
287 ?>