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