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