19758af359d18a457bb0086b3850babf7752559b
[citadel.git] / webcit / downloads.c
1 /*
2  * $Id$
3  */
4 #include "webcit.h"
5 #include "webserver.h"
6
7 void display_room_directory(void)
8 {
9         char buf[1024];
10         char filename[256];
11         char filesize[256];
12         char mimetype[64];
13         char comment[512];
14         int bg = 0;
15         char title[256];
16         int havepics = 0;
17
18         output_headers(1, 1, 2, 0, 0, 0);
19         wprintf("<div id=\"banner\">\n");
20         wprintf("<h1>");
21         snprintf(title, sizeof title, _("Files available for download in %s"), WC->wc_roomname);
22         escputs(title);
23         wprintf("</h1>");
24         wprintf("</div>\n");
25
26         wprintf("<div id=\"content\" class=\"service\">\n");
27
28         wprintf("<div class=\"fix_scrollbar_bug\">"
29                 "<table class=\"downloads_background\"><tr><td>\n");
30         wprintf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th></tr>\n",
31                 _("Filename"),
32                 _("Size"),
33                 _("Content"),
34                 _("Description")
35         );
36
37         serv_puts("RDIR");
38         serv_getln(buf, sizeof buf);
39         if (buf[0] == '1') while (serv_getln(buf, sizeof buf), strcmp(buf, "000"))
40         {
41                 extract_token(filename, buf, 0, '|', sizeof filename);
42                 extract_token(filesize, buf, 1, '|', sizeof filesize);
43                 extract_token(mimetype, buf, 2, '|', sizeof mimetype);
44                 extract_token(comment,  buf, 3, '|', sizeof comment);
45                 bg = 1 - bg;
46                 wprintf("<tr bgcolor=\"#%s\">", (bg ? "DDDDDD" : "FFFFFF"));
47                 wprintf("<td>"
48                         "<a href=\"download_file/");
49                 urlescputs(filename);
50                 wprintf("\"><img src=\"display_mime_icon?type=%s\" border=0 align=middle>\n", mimetype);
51                                         escputs(filename);      wprintf("</a></td>");
52                 wprintf("<td>");        escputs(filesize);      wprintf("</td>");
53                 wprintf("<td>");        escputs(mimetype);      wprintf("</td>");
54                 wprintf("<td>");        escputs(comment);       wprintf("</td>");
55                 wprintf("</tr>\n");
56                 if (!havepics && (strstr(mimetype, "image") != NULL))
57                         havepics = 1;
58         }
59         wprintf("</table>\n");
60
61         /** Now offer the ability to upload files... */
62         if (WC->room_flags & QR_UPLOAD)
63         {
64                 wprintf("<hr>");
65                 wprintf("<form "
66                         "enctype=\"multipart/form-data\" "
67                         "method=\"POST\" "
68                         "accept-charset=\"UTF-8\" "
69                         "action=\"upload_file\" "
70                         "name=\"upload_file_form\""
71                         ">\n"
72                 );
73                 wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%ld\">\n", WC->nonce);
74
75                 wprintf(_("Upload a file:"));
76                 wprintf("&nbsp;<input NAME=\"filename\" SIZE=16 TYPE=\"file\">&nbsp;\n");
77                 wprintf(_("Description:"));
78                 wprintf("&nbsp;<input type=\"text\" name=\"description\" maxlength=\"64\" size=\"64\">&nbsp;");
79                 wprintf("<input type=\"submit\" name=\"attach_button\" value=\"%s\">\n", _("Upload"));
80
81                 wprintf("</form>\n");
82         }
83
84         wprintf("</div>\n");
85         if (havepics)
86                 wprintf("<div class=\"buttons\"><a href=\"display_pictureview&frame=1\">%s</a></div>", _("Slideshow"));
87         wDumpContent(1);
88 }
89
90
91 void display_pictureview(void)
92 {
93         char buf[1024];
94         char filename[256];
95         char filesize[256];
96         char mimetype[64];
97         char comment[512];
98         char title[256];
99         int n = 0;
100                 
101
102         if (lbstr("frame") == 1) {
103
104                 output_headers(1, 1, 2, 0, 0, 0);
105                 wprintf("<div id=\"banner\">\n");
106                 wprintf("<h1>");
107                 snprintf(title, sizeof title, _("Pictures in %s"), WC->wc_roomname);
108                 escputs(title);
109                 wprintf("</h1>");
110                 wprintf("</div>\n");
111                 
112                 wprintf("<div id=\"content\" class=\"service\">\n");
113
114                 wprintf("<div class=\"fix_scrollbar_bug\">"
115                         "<table class=\"downloads_background\"><tr><td>\n");
116
117
118
119                 wprintf("<script type=\"text/javascript\" language=\"JavaScript\" > \nvar fadeimages=new Array()\n");
120
121                 serv_puts("RDIR");
122                 serv_getln(buf, sizeof buf);
123                 if (buf[0] == '1') while (serv_getln(buf, sizeof buf), strcmp(buf, "000"))  {
124                                 extract_token(filename, buf, 0, '|', sizeof filename);
125                                 extract_token(filesize, buf, 1, '|', sizeof filesize);
126                                 extract_token(mimetype, buf, 2, '|', sizeof mimetype);
127                                 extract_token(comment,  buf, 3, '|', sizeof comment);
128                                 if (strstr(mimetype, "image") != NULL) {
129                                         wprintf("fadeimages[%d]=[\"download_file/", n);
130                                         escputs(filename);
131                                         wprintf("\", \"\", \"\"]\n");
132
133                                         /*
134                                                            //mimetype);
135                                            escputs(filename);   wprintf("</a></td>");
136                                            wprintf("<td>");     escputs(filesize);      wprintf("</td>");
137                                            wprintf("<td>");     escputs(mimetype);      wprintf("</td>");
138                                            wprintf("<td>");     escputs(comment);       wprintf("</td>");
139                                            wprintf("</tr>\n");
140                                         */
141                                         n++;
142                                 }
143                         }
144                 wprintf("</script>\n");
145                 wprintf("<tr><td><script type=\"text/javascript\" src=\"static/fadeshow.js\">\n</script>\n");
146                 wprintf("<script type=\"text/javascript\" >\n");
147                 wprintf("new fadeshow(fadeimages, 500, 400, 0, 3000, 1, \"R\");\n");
148                 wprintf("</script></td><th>\n");
149                 wprintf("</div>\n");
150         }
151         wDumpContent(1);
152
153
154 }
155
156 extern char* static_dirs[];
157 void display_mime_icon(void)
158 {
159         char FileBuf[SIZ];
160         const char *FileName;
161         char *MimeType;
162         size_t tlen;
163
164         MimeType = xbstr("type", &tlen);
165         FileName = GetIconFilename(MimeType, tlen);
166
167         if (FileName == NULL)
168                 snprintf (FileBuf, SIZ, "%s%s", static_dirs[0], "/diskette_24x.gif");
169         else
170                 snprintf (FileBuf, SIZ, "%s%s", static_dirs[3], FileName);
171         output_static(FileBuf);
172
173 }
174
175 void download_file(char *filename)
176 {
177         char buf[256];
178         off_t bytes;
179         char content_type[256];
180         char *content = NULL;
181
182         /* Setting to nonzero forces a MIME type of application/octet-stream */
183         int force_download = 1;
184         
185         safestrncpy(buf, filename, sizeof buf);
186         unescape_input(buf);
187         serv_printf("OPEN %s", buf);
188         serv_getln(buf, sizeof buf);
189         if (buf[0] == '2') {
190                 bytes = extract_long(&buf[4], 0);
191                 content = malloc(bytes + 2);
192                 if (force_download) {
193                         strcpy(content_type, "application/octet-stream");
194                 }
195                 else {
196                         extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
197                 }
198                 output_headers(0, 0, 0, 0, 0, 0);
199                 read_server_binary(content, bytes);
200                 serv_puts("CLOS");
201                 serv_getln(buf, sizeof buf);
202                 http_transmit_thing(content, bytes, content_type, 0);
203                 free(content);
204         } else {
205                 wprintf("HTTP/1.1 404 %s\n", &buf[4]);
206                 output_headers(0, 0, 0, 0, 0, 0);
207                 wprintf("Content-Type: text/plain\r\n");
208                 wprintf("\r\n");
209                 wprintf(_("An error occurred while retrieving this file: %s\n"), &buf[4]);
210         }
211
212 }
213
214
215
216 void upload_file(void)
217 {
218         const char *MimeType;
219         char buf[1024];
220         size_t bytes_transmitted = 0;
221         size_t blocksize;
222         struct wcsession *WCC = WC;     /* stack this for faster access (WC is a function) */
223
224         MimeType = GuessMimeType(WCC->upload, WCC->upload_length); 
225         serv_printf("UOPN %s|%s|%s", WCC->upload_filename, MimeType, bstr("description"));
226         serv_getln(buf, sizeof buf);
227         if (buf[0] != '2')
228         {
229                 strcpy(WCC->ImportantMessage, &buf[4]);
230                 display_room_directory();
231                 return;
232         }
233
234         while (bytes_transmitted < WCC->upload_length)
235         {
236                 blocksize = 4096;
237                 if (blocksize > (WCC->upload_length - bytes_transmitted))
238                 {
239                         blocksize = (WCC->upload_length - bytes_transmitted);
240                 }
241                 serv_printf("WRIT %d", blocksize);
242                 serv_getln(buf, sizeof buf);
243                 if (buf[0] == '7')
244                 {
245                         blocksize = atoi(&buf[4]);
246                         serv_write(&WCC->upload[bytes_transmitted], blocksize);
247                         bytes_transmitted += blocksize;
248                 }
249         }
250
251         serv_puts("UCLS 1");
252         serv_getln(buf, sizeof buf);
253         strcpy(WCC->ImportantMessage, &buf[4]);
254         display_room_directory();
255 }