f39622495cc9ca918faf1b3624a1ed4e08cd9b41
[citadel.git] / webcit / downloads.c
1 /*
2  * Copyright (c) 1996-2012 by the citadel.org team
3  *
4  * This program is open source software.  You can redistribute it and/or
5  * modify it under the terms of the GNU General Public License, version 3.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  */
12
13 #include "webcit.h"
14 #include "webserver.h"
15
16 CtxType CTX_FILELIST = CTX_NONE;
17
18 extern void output_static(const char* What);
19
20 extern char* static_dirs[];
21
22 typedef struct _FileListStruct {
23         StrBuf *Filename;
24         long FileSize;
25         StrBuf *MimeType;
26         StrBuf *Comment;
27         int IsPic;
28         int Sequence;
29 } FileListStruct;
30
31 void FreeFiles(void *vFile)
32 {
33         FileListStruct *F = (FileListStruct*) vFile;
34         FreeStrBuf(&F->Filename);
35         FreeStrBuf(&F->MimeType);
36         FreeStrBuf(&F->Comment);
37         free(F);
38 }
39
40 /* -------------------------------------------------------------------------------- */
41 void tmplput_FILE_NAME(StrBuf *Target, WCTemplputParams *TP)
42 {
43         FileListStruct *F = (FileListStruct*) CTX(CTX_FILELIST);
44         StrBufAppendTemplate(Target, TP, F->Filename, 0);
45 }
46 void tmplput_FILE_SIZE(StrBuf *Target, WCTemplputParams *TP)
47 {
48         FileListStruct *F = (FileListStruct*) CTX(CTX_FILELIST);
49         StrBufAppendPrintf(Target, "%ld", F->FileSize);
50 }
51 void tmplput_FILEMIMETYPE(StrBuf *Target, WCTemplputParams *TP)
52 {
53         FileListStruct *F = (FileListStruct*) CTX(CTX_FILELIST);
54         StrBufAppendTemplate(Target, TP, F->MimeType, 0);
55 }
56 void tmplput_FILE_COMMENT(StrBuf *Target, WCTemplputParams *TP)
57 {
58         FileListStruct *F = (FileListStruct*) CTX(CTX_FILELIST);
59         StrBufAppendTemplate(Target, TP, F->Comment, 0);
60 }
61
62 /* -------------------------------------------------------------------------------- */
63
64 int Conditional_FILE_ISPIC(StrBuf *Target, WCTemplputParams *TP)
65 {
66         FileListStruct *F = (FileListStruct*) CTX(CTX_FILELIST);
67         return F->IsPic;
68 }
69
70 /* -------------------------------------------------------------------------------- */
71 int CompareFilelistByMime(const void *vFile1, const void *vFile2)
72 {
73         FileListStruct *File1 = (FileListStruct*) GetSearchPayload(vFile1);
74         FileListStruct *File2 = (FileListStruct*) GetSearchPayload(vFile2);
75
76         if (File1->IsPic != File2->IsPic)
77                 return File1->IsPic > File2->IsPic;
78         return strcasecmp(ChrPtr(File1->MimeType), ChrPtr(File2->MimeType));
79 }
80 int CompareFilelistByMimeRev(const void *vFile1, const void *vFile2)
81 {
82         FileListStruct *File1 = (FileListStruct*) GetSearchPayload(vFile1);
83         FileListStruct *File2 = (FileListStruct*) GetSearchPayload(vFile2);
84         if (File1->IsPic != File2->IsPic)
85                 return File1->IsPic < File2->IsPic;
86         return strcasecmp(ChrPtr(File2->MimeType), ChrPtr(File1->MimeType));
87 }
88 int GroupchangeFilelistByMime(const void *vFile1, const void *vFile2)
89 {
90         FileListStruct *File1 = (FileListStruct*) vFile1;
91         FileListStruct *File2 = (FileListStruct*) vFile2;
92
93         if (File1->IsPic != File2->IsPic)
94                 return File1->IsPic > File2->IsPic;
95         return strcasecmp(ChrPtr(File1->MimeType), ChrPtr(File2->MimeType)) != 0;
96 }
97
98
99 int CompareFilelistByName(const void *vFile1, const void *vFile2)
100 {
101         FileListStruct *File1 = (FileListStruct*) GetSearchPayload(vFile1);
102         FileListStruct *File2 = (FileListStruct*) GetSearchPayload(vFile2);
103
104         if (File1->IsPic != File2->IsPic)
105                 return File1->IsPic > File2->IsPic;
106         return strcasecmp(ChrPtr(File1->Filename), ChrPtr(File2->Filename));
107 }
108 int CompareFilelistByNameRev(const void *vFile1, const void *vFile2)
109 {
110         FileListStruct *File1 = (FileListStruct*) GetSearchPayload(vFile1);
111         FileListStruct *File2 = (FileListStruct*) GetSearchPayload(vFile2);
112         if (File1->IsPic != File2->IsPic)
113                 return File1->IsPic < File2->IsPic;
114         return strcasecmp(ChrPtr(File2->Filename), ChrPtr(File1->Filename));
115 }
116 int GroupchangeFilelistByName(const void *vFile1, const void *vFile2)
117 {
118         FileListStruct *File1 = (FileListStruct*) vFile1;
119         FileListStruct *File2 = (FileListStruct*) vFile2;
120
121         return ChrPtr(File1->Filename)[0] != ChrPtr(File2->Filename)[0];
122 }
123
124
125 int CompareFilelistBySize(const void *vFile1, const void *vFile2)
126 {
127         FileListStruct *File1 = (FileListStruct*) GetSearchPayload(vFile1);
128         FileListStruct *File2 = (FileListStruct*) GetSearchPayload(vFile2);
129         if (File1->FileSize == File2->FileSize)
130                 return 0;
131         return (File1->FileSize > File2->FileSize);
132 }
133 int CompareFilelistBySizeRev(const void *vFile1, const void *vFile2)
134 {
135         FileListStruct *File1 = (FileListStruct*) GetSearchPayload(vFile1);
136         FileListStruct *File2 = (FileListStruct*) GetSearchPayload(vFile2);
137         if (File1->FileSize == File2->FileSize)
138                 return 0;
139         return (File1->FileSize < File2->FileSize);
140 }
141 int GroupchangeFilelistBySize(const void *vFile1, const void *vFile2)
142 {
143         return 0;
144 }
145
146
147 int CompareFilelistByComment(const void *vFile1, const void *vFile2)
148 {
149         FileListStruct *File1 = (FileListStruct*) GetSearchPayload(vFile1);
150         FileListStruct *File2 = (FileListStruct*) GetSearchPayload(vFile2);
151         return strcasecmp(ChrPtr(File1->Comment), ChrPtr(File2->Comment));
152 }
153 int CompareFilelistByCommentRev(const void *vFile1, const void *vFile2)
154 {
155         FileListStruct *File1 = (FileListStruct*) GetSearchPayload(vFile1);
156         FileListStruct *File2 = (FileListStruct*) GetSearchPayload(vFile2);
157         return strcasecmp(ChrPtr(File2->Comment), ChrPtr(File1->Comment));
158 }
159 int GroupchangeFilelistByComment(const void *vFile1, const void *vFile2)
160 {
161         FileListStruct *File1 = (FileListStruct*) vFile1;
162         FileListStruct *File2 = (FileListStruct*) vFile2;
163         return ChrPtr(File1->Comment)[9] != ChrPtr(File2->Comment)[0];
164 }
165
166
167 int CompareFilelistBySequence(const void *vFile1, const void *vFile2)
168 {
169         FileListStruct *File1 = (FileListStruct*) GetSearchPayload(vFile1);
170         FileListStruct *File2 = (FileListStruct*) GetSearchPayload(vFile2);
171         return (File2->Sequence >  File1->Sequence);
172 }
173 int GroupchangeFilelistBySequence(const void *vFile1, const void *vFile2)
174 {
175         return 0;
176 }
177
178 /* -------------------------------------------------------------------------------- */
179 HashList* LoadFileList(StrBuf *Target, WCTemplputParams *TP)
180 {
181         FileListStruct *Entry;
182         StrBuf *Buf;
183         HashList *Files;
184         int Done = 0;
185         int sequence = 0;
186         char buf[1024];
187         CompareFunc SortIt;
188         int HavePic = 0;
189         WCTemplputParams SubTP;
190
191         memset(&SubTP, 0, sizeof(WCTemplputParams));
192         serv_puts("RDIR");
193         serv_getln(buf, sizeof buf);
194         if (buf[0] != '1') return NULL;
195
196         Buf = NewStrBuf();             
197         Files = NewHash(1, NULL);
198         while (!Done && (StrBuf_ServGetln(Buf)>=0)) {
199                 if ( (StrLength(Buf)==3) && 
200                     !strcmp(ChrPtr(Buf), "000")) 
201                 {
202                         Done = 1;
203                         continue;
204                 }
205
206                 Entry = (FileListStruct*) malloc(sizeof (FileListStruct));
207                 Entry->Filename = NewStrBufPlain(NULL, StrLength(Buf));
208                 Entry->MimeType = NewStrBufPlain(NULL, StrLength(Buf));
209                 Entry->Comment = NewStrBufPlain(NULL, StrLength(Buf));
210
211                 Entry->Sequence = sequence++;
212
213                 StrBufExtract_token(Entry->Filename, Buf, 0, '|');
214                 Entry->FileSize = StrBufExtract_long(Buf, 1, '|');
215                 StrBufExtract_token(Entry->MimeType, Buf, 2, '|');
216                 StrBufExtract_token(Entry->Comment, Buf, 3, '|');
217
218
219
220                 Entry->IsPic = (strstr(ChrPtr(Entry->MimeType), "image") != NULL);
221                 if (Entry->IsPic) {
222                         HavePic = 1;
223                 }
224                 Put(Files, SKEY(Entry->Filename), Entry, FreeFiles);
225         }
226         if (HavePic)
227                 putbstr("__HAVE_PIC", NewStrBufPlain(HKEY("1")));
228         SubTP.Filter.ContextType = CTX_FILELIST;
229         SortIt = RetrieveSort(&SubTP, NULL, 0, HKEY("fileunsorted"), 0);
230         if (SortIt != NULL)
231                 SortByPayload(Files, SortIt);
232         else 
233                 SortByPayload(Files, CompareFilelistBySequence);
234         FreeStrBuf(&Buf);
235         return Files;
236 }
237
238 void display_mime_icon(void)
239 {
240         char FileBuf[SIZ];
241         const char *FileName;
242         char *MimeType;
243         size_t tlen;
244
245         MimeType = xbstr("type", &tlen);
246         FileName = GetIconFilename(MimeType, tlen);
247
248         if (FileName == NULL)
249                 snprintf (FileBuf, SIZ, "%s%s", static_dirs[0], "/webcit_icons/essen/16x16/file.png");
250         else
251                 snprintf (FileBuf, SIZ, "%s%s", static_dirs[3], FileName);
252         output_static(FileBuf);
253 }
254
255 void download_file(void)
256 {
257         wcsession *WCC = WC;
258         StrBuf *Buf;
259         off_t bytes;
260         StrBuf *ContentType = NewStrBufPlain(HKEY("application/octet-stream"));
261
262         /* Setting to nonzero forces a MIME type of application/octet-stream */
263         int force_download = 1;
264         
265         Buf = NewStrBuf();
266         StrBufExtract_token(Buf, WCC->Hdr->HR.ReqLine, 0, '/');
267         StrBufUnescape(Buf, 1);
268         serv_printf("OPEN %s", ChrPtr(Buf));
269         StrBuf_ServGetln(Buf);
270         if (GetServerStatus(Buf, NULL) == 2) {
271                 StrBufCutLeft(Buf, 4);
272                 bytes = StrBufExtract_long(Buf, 0, '|');
273                 if (!force_download) {
274                         StrBufExtract_token(ContentType, Buf, 3, '|');
275                 }
276                 serv_read_binary(WCC->WBuf, bytes, Buf);
277                 serv_puts("CLOS");
278                 StrBuf_ServGetln(Buf);
279                 http_transmit_thing(ChrPtr(ContentType), 0);
280         } else {
281                 StrBufCutLeft(Buf, 4);
282                 hprintf("HTTP/1.1 404 %s\n", ChrPtr(Buf));
283                 output_headers(0, 0, 0, 0, 0, 0);
284                 hprintf("Content-Type: text/plain\r\n");
285                 wc_printf(_("An error occurred while retrieving this file: %s\n"), 
286                         ChrPtr(Buf));
287                 end_burst();
288         }
289         FreeStrBuf(&ContentType);
290         FreeStrBuf(&Buf);
291 }
292
293
294
295 void delete_file(void)
296 {
297         const StrBuf *MimeType;
298         StrBuf *Line;
299         char buf[256];
300         
301         safestrncpy(buf, bstr("file"), sizeof buf);
302         unescape_input(buf);
303         serv_printf("DELF %s", buf);
304
305         StrBuf_ServGetln(Line);
306         GetServerStatusMsg(Line, NULL, 1, 0);
307
308         MimeType = DoTemplate(HKEY("files"), NULL, &NoCtx);
309         http_transmit_thing(ChrPtr(MimeType), 0);
310         FreeStrBuf(&Line);
311 }
312
313
314
315 void upload_file(void)
316 {
317         const StrBuf *RetMimeType;
318         const char *MimeType;
319         StrBuf *Line;
320         long bytes_transmitted = 0;
321         long blocksize;
322         const StrBuf *Desc;
323         wcsession *WCC = WC;     /* stack this for faster access (WC is a function) */
324
325         MimeType = GuessMimeType(ChrPtr(WCC->upload), WCC->upload_length); 
326
327                 Desc = sbstr("description");
328
329         serv_printf("UOPN %s|%s|%s", 
330                     ChrPtr(WCC->upload_filename), 
331                     MimeType, 
332                     ChrPtr(Desc));
333         Line = NewStrBuf();
334         StrBuf_ServGetln(Line);
335         if (GetServerStatusMsg(Line, NULL, 1, 2) != 2) {
336                 RetMimeType = DoTemplate(HKEY("files"), NULL, &NoCtx);
337                 http_transmit_thing(ChrPtr(RetMimeType), 0);
338                 FreeStrBuf(&Line);
339                 return;
340         }
341
342         while (bytes_transmitted < WCC->upload_length)
343         {
344                 blocksize = 4096;
345                 if (blocksize > (WCC->upload_length - bytes_transmitted))
346                 {
347                         blocksize = (WCC->upload_length - bytes_transmitted);
348                 }
349                 serv_printf("WRIT %ld", blocksize);
350                 StrBuf_ServGetln(Line);
351                 if (GetServerStatusMsg(Line, NULL, 0, 0) == 7) {
352                         blocksize = atoi(ChrPtr(Line) + 4);
353                         serv_write(&ChrPtr(WCC->upload)[bytes_transmitted], blocksize);
354                         bytes_transmitted += blocksize;
355                 }
356                 else
357                         break;
358         }
359
360         serv_puts("UCLS 1");
361         StrBuf_ServGetln(Line);
362         GetServerStatusMsg(Line, NULL, 1, 0);
363         RetMimeType = DoTemplate(HKEY("files"), NULL, &NoCtx);
364         http_transmit_thing(ChrPtr(RetMimeType), 0);
365         FreeStrBuf(&Line);
366 }
367
368
369
370 /*
371  * When the browser requests an image file from the Citadel server,
372  * this function is called to transmit it.
373  */
374 void output_image(void)
375 {
376         StrBuf *Buf;
377         wcsession *WCC = WC;
378         off_t bytes;
379         const char *MimeType;
380         
381         Buf = NewStrBuf();
382         serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
383         StrBuf_ServGetln(Buf);
384         if (GetServerStatus(Buf, NULL) == 2) {
385                 int rc;
386                 StrBufCutLeft(Buf, 4);
387                 bytes = StrBufExtract_long(Buf, 0, '|');
388
389                 /** Read it from the server */
390                 
391                 rc = serv_read_binary(WCC->WBuf, bytes, Buf);
392                 serv_puts("CLOS");
393                 StrBuf_ServGetln(Buf);
394                 
395                 if (rc > 0) {
396                         MimeType = GuessMimeType (ChrPtr(WCC->WBuf), StrLength(WCC->WBuf));
397                         /** Write it to the browser */
398                         if (!IsEmptyStr(MimeType))
399                         {
400                                 http_transmit_thing(MimeType, 0);
401                                 FreeStrBuf(&Buf);
402                                 return;
403                         }
404                 }
405                 /* hm... unknown mimetype? fallback to blank gif */
406         }
407         else { 
408                 syslog(LOG_DEBUG, "OIMG failed: %s", ChrPtr(Buf));
409         }
410
411         
412         /*
413          * Instead of an ugly 404, send a 1x1 transparent GIF
414          * when there's no such image on the server.
415          */
416         StrBufPrintf (Buf, "%s%s", static_dirs[0], "/webcit_icons/blank.gif");
417         output_static(ChrPtr(Buf));
418         FreeStrBuf(&Buf);
419 }
420
421 void 
422 InitModule_DOWNLOAD
423 (void)
424 {
425         RegisterCTX(CTX_FILELIST);
426
427         RegisterIterator("ROOM:FILES", 0, NULL, LoadFileList,
428                          NULL, DeleteHash, CTX_FILELIST, CTX_NONE, 
429                          IT_FLAG_DETECT_GROUPCHANGE);
430
431         RegisterSortFunc(HKEY("filemime"),
432                          NULL, 0,
433                          CompareFilelistByMime,
434                          CompareFilelistByMimeRev,
435                          GroupchangeFilelistByMime,
436                          CTX_FILELIST);
437         RegisterSortFunc(HKEY("filename"),
438                          NULL, 0,
439                          CompareFilelistByName,
440                          CompareFilelistByNameRev,
441                          GroupchangeFilelistByName,
442                          CTX_FILELIST);
443         RegisterSortFunc(HKEY("filesize"),
444                          NULL, 0,
445                          CompareFilelistBySize,
446                          CompareFilelistBySizeRev,
447                          GroupchangeFilelistBySize,
448                          CTX_FILELIST);
449         RegisterSortFunc(HKEY("filesubject"),
450                          NULL, 0,
451                          CompareFilelistByComment,
452                          CompareFilelistByCommentRev,
453                          GroupchangeFilelistByComment,
454                          CTX_FILELIST);
455         RegisterSortFunc(HKEY("fileunsorted"),
456                          NULL, 0,
457                          CompareFilelistBySequence,
458                          CompareFilelistBySequence,
459                          GroupchangeFilelistBySequence,
460                          CTX_FILELIST);
461
462         RegisterNamespace("FILE:NAME", 0, 2, tmplput_FILE_NAME, NULL, CTX_FILELIST);
463         RegisterNamespace("FILE:SIZE", 0, 1, tmplput_FILE_SIZE, NULL, CTX_FILELIST);
464         RegisterNamespace("FILE:MIMETYPE", 0, 2, tmplput_FILEMIMETYPE, NULL, CTX_FILELIST);
465         RegisterNamespace("FILE:COMMENT", 0, 2, tmplput_FILE_COMMENT, NULL, CTX_FILELIST);
466
467         RegisterConditional(HKEY("COND:FILE:ISPIC"), 0, Conditional_FILE_ISPIC, CTX_FILELIST);
468
469         WebcitAddUrlHandler(HKEY("image"), "", 0, output_image, ANONYMOUS);
470         WebcitAddUrlHandler(HKEY("display_mime_icon"), "", 0, display_mime_icon , ANONYMOUS);
471         WebcitAddUrlHandler(HKEY("download_file"), "", 0, download_file, NEED_URL);
472         WebcitAddUrlHandler(HKEY("delete_file"), "", 0, delete_file, NEED_URL);
473         WebcitAddUrlHandler(HKEY("upload_file"), "", 0, upload_file, 0);
474 }