* we have now several bstrs:
[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         size_t tlen;
162
163         FileName = GetIconFilename(xbstr("type", &tlen), tlen);
164
165         if (FileName == NULL)
166                 snprintf (FileBuf, SIZ, "%s%s", static_dirs[0], "/diskette_24x.gif");
167         else
168                 snprintf (FileBuf, SIZ, "%s%s", static_dirs[3], FileName);
169         output_static(FileBuf);
170
171 }
172
173 void download_file(char *filename)
174 {
175         char buf[256];
176         off_t bytes;
177         char content_type[256];
178         char *content = NULL;
179
180         /* Setting to nonzero forces a MIME type of application/octet-stream */
181         int force_download = 1;
182         
183         safestrncpy(buf, filename, sizeof buf);
184         unescape_input(buf);
185         serv_printf("OPEN %s", buf);
186         serv_getln(buf, sizeof buf);
187         if (buf[0] == '2') {
188                 bytes = extract_long(&buf[4], 0);
189                 content = malloc(bytes + 2);
190                 if (force_download) {
191                         strcpy(content_type, "application/octet-stream");
192                 }
193                 else {
194                         extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
195                 }
196                 output_headers(0, 0, 0, 0, 0, 0);
197                 read_server_binary(content, bytes);
198                 serv_puts("CLOS");
199                 serv_getln(buf, sizeof buf);
200                 http_transmit_thing(content, bytes, content_type, 0);
201                 free(content);
202         } else {
203                 wprintf("HTTP/1.1 404 %s\n", &buf[4]);
204                 output_headers(0, 0, 0, 0, 0, 0);
205                 wprintf("Content-Type: text/plain\r\n");
206                 wprintf("\r\n");
207                 wprintf(_("An error occurred while retrieving this file: %s\n"), &buf[4]);
208         }
209
210 }
211
212
213
214 void upload_file(void)
215 {
216         const char *MimeType;
217         char buf[1024];
218         size_t bytes_transmitted = 0;
219         size_t blocksize;
220         struct wcsession *WCC = WC;     /* stack this for faster access (WC is a function) */
221
222         MimeType = GuessMimeType(WCC->upload, WCC->upload_length); 
223         serv_printf("UOPN %s|%s|%s", WCC->upload_filename, MimeType, bstr("description"));
224         serv_getln(buf, sizeof buf);
225         if (buf[0] != '2')
226         {
227                 strcpy(WCC->ImportantMessage, &buf[4]);
228                 display_room_directory();
229                 return;
230         }
231
232         while (bytes_transmitted < WCC->upload_length)
233         {
234                 blocksize = 4096;
235                 if (blocksize > (WCC->upload_length - bytes_transmitted))
236                 {
237                         blocksize = (WCC->upload_length - bytes_transmitted);
238                 }
239                 serv_printf("WRIT %d", blocksize);
240                 serv_getln(buf, sizeof buf);
241                 if (buf[0] == '7')
242                 {
243                         blocksize = atoi(&buf[4]);
244                         serv_write(&WCC->upload[bytes_transmitted], blocksize);
245                         bytes_transmitted += blocksize;
246                 }
247         }
248
249         serv_puts("UCLS 1");
250         serv_getln(buf, sizeof buf);
251         strcpy(WCC->ImportantMessage, &buf[4]);
252         display_room_directory();
253 }