]> dev.renevier.net Git - syp.git/blob - api.php
forbird creation or deletion of item; forbids adding or deleting an image
[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_imgpath ($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_safe ($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 (!isset ($imgpath) || (strlen ($imgpath) == 0)) {
120         return;
121     }
122     if ($con->imgpath_exists ($imgpath)) {
123         return false;
124     }
125     $path = UPLOADDIR . "/" . $imgpath;
126     if (file_exists($path)) {
127         unlink ($path);
128         return true;
129     } else {
130         return false;
131     }
132 }
133
134 function unique_file ($dirname, $relpath, $con) {
135    $relpath = str_replace ('/', '', $relpath); // strip slashes from path
136    $relpath = str_replace ('\\', '', $relpath); // strip antislashes from path
137    $filename = $dirname . '/' . $relpath;
138    $counter = 1;
139
140    $dotpos = strrpos ($relpath, '.');
141    if ($dotpos) {
142        $base = substr ($relpath, 0, $dotpos);
143        $ext = substr ($relpath, $dotpos + 1);
144    } else {
145        $base = $relpath;
146        $ext = "";
147    }
148
149    while ($counter < 1000) {
150        if (!file_exists ($filename) && 
151            !($con->imgpath_exists (basename_safe ($filename)))) {
152            return $filename;
153        } else {
154             $counter++;
155             $filename = $dirname . '/' . $base . '_' . $counter . '.' . $ext;
156        }
157    }
158    // we tried to find an unused filename 1000 times. Give up now.
159    return null;
160 }
161
162 function main ($con) {
163     if (!isset ($_POST ["request"])) {
164         request_error ();
165     }
166     if ($_POST ["request"] == "auth") {
167         $pwd = unquote ($_POST["password"]);
168         $user = "admin";
169         if ($con->checkpwdmd5 ($user, md5 ($pwd))) {
170             // cookie will be valid for 2 weeks. I've chosen that value
171             // arbitrarily, and it may change in the future.
172             $time = time () + 14 * 60 * 24 * 60;
173             $cookie_name = sprintf ("%sauth", DBPREFIX);
174             setcookie ($cookie_name, md5 ($pwd), $time, "" , "", false, true);
175             success_auth ();
176         } else {
177             unauthorized_error ();
178         }
179     }
180     if (!($con->checkpwdmd5 ("admin",
181                              $_COOKIE [sprintf ("%sauth", DBPREFIX)]))) {
182         unauthorized_error ();
183     }
184
185     switch ($_POST ["request"]) {
186         case "update":
187             $id = $_POST ["fid"];
188             $feature = $con->getfeature ($id);
189             if (!isset ($feature)) {
190                 unreferenced_error ($id);
191             }
192
193             // no file uploaded, but editor currently has an image: it means
194             // image was not changed
195             if ($_POST ["keep_img"] == "yes") {
196                 $imgpath = $feature->imgpath;
197             } else {
198                 request_error ();
199                 $imgpath = save_uploaded_file ($_FILES ["image_file"], $con);
200             }
201
202             $lon = $_POST ["lon"];
203             $lat = $_POST ["lat"];
204             $title = unquote ($_POST ["title"]);
205             $description = unquote ($_POST ["description"]);
206
207             try {
208                 $new_feature = new feature ($id, $lon, $lat, $imgpath, $title, $description, 0);
209             } catch (Exception $e) {
210                 request_error ();
211             }
212
213             if (($new_feature->lon == $feature->lon) &&
214                 ($new_feature->lat == $feature->lat) &&
215                 ($new_feature->title == $feature->title) &&
216                 ($new_feature->imgpath == $feature->imgpath) &&
217                 ($new_feature->description == $feature->description)) {
218                 nochange_error ($feature->id);
219             }
220
221             $old_imgpath = "";
222             if ($feature->imgpath && ($feature->imgpath != $new_feature->imgpath)) {
223                 $old_imgpath = $feature->imgpath;
224             }
225
226             try {
227                 $con->save_feature ($new_feature);
228             } catch (Exception $e) {
229                 server_error ();
230             }
231             if ($old_imgpath) {
232                 try {
233                     delete_image_if_unused ($old_imgpath, $con); 
234                 } catch (Exception $e) {}
235             }
236             success_feature ($new_feature, "update");
237         break;
238         case "add":
239             request_error ();
240             $imgpath = save_uploaded_file ($_FILES ["image_file"], $con);
241
242             $lon = $_POST ["lon"];
243             $lat = $_POST ["lat"];
244             $title = unquote ($_POST ["title"]);
245             $description = unquote ($_POST ["description"]);
246             try {
247                 $feature = new feature (null, $lon, $lat, $imgpath, $title, $description, 0);
248             } catch (Exception $e) {
249                 request_error ();
250             }
251             try {
252                 $feature = $con->save_feature ($feature);
253             } catch (Exception $e) {
254                 server_error ();
255             }
256             success_feature ($feature, "add");
257         break;
258         case "del":
259             request_error ();
260             $id = $_POST ["fid"];
261             $feature = $con->getfeature ($id);
262             if (!isset ($feature)) {
263                 unreferenced_error ($id);
264             }
265             $imgpath = $feature->imgpath;
266
267             try {
268                 $con->delete_feature ($feature);
269             } catch (Exception $e) {
270                 server_error ();
271             }
272
273             try {
274                 delete_image_if_unused ($imgpath, $con);
275             } catch (Exception $e) {}
276
277             success_delete_feature ($feature);
278         default:
279             request_error();
280         break;
281     }
282
283     server_error ();
284 }
285
286 try {
287     $connection->connect (DBHOST, DBUSER, DBPWD, DBNAME, DBPREFIX);
288 } catch (Exception $e) {
289     server_error ();
290 }
291
292 main ($connection);
293 ?>