d29e7fc7ebd635f8658436332dd2ccaca2c7fbf8
[citadel.git] / webcit / downloads.c
1 /*
2  * $Id$
3  */
4 #include "webcit.h"
5
6 void display_room_directory(void)
7 {
8         char buf[1024];
9         char filename[256];
10         char filesize[256];
11         char comment[512];
12         int bg = 0;
13         char title[256];
14
15         output_headers(1, 1, 2, 0, 0, 0);
16         wprintf("<div id=\"banner\">\n"
17                 "<table class=\"downloads_banner\"><tr><td>"
18                 "<span class=\"titlebar\">");
19         snprintf(title, sizeof title, _("Files available for download in %s"), WC->wc_roomname);
20         escputs(title);
21         wprintf("</span>"
22                 "</td></tr></table>\n"
23                 "</div>\n<div id=\"content\">\n"
24         );
25
26         wprintf("<div class=\"fix_scrollbar_bug\">"
27                 "<table class=\"downloads_background\"><tr><td>\n");
28         wprintf("<tr><th>%s</th><th>%s</th><th>%s</th></tr>\n",
29                         _("Filename"),
30                         _("Size"),
31                         _("Description")
32         );
33
34         serv_puts("RDIR");
35         serv_getln(buf, sizeof buf);
36         if (buf[0] == '1') while (serv_getln(buf, sizeof buf), strcmp(buf, "000"))
37         {
38                 extract_token(filename, buf, 0, '|', sizeof filename);
39                 extract_token(filesize, buf, 1, '|', sizeof filesize);
40                 extract_token(comment, buf, 2, '|', sizeof comment);
41                 bg = 1 - bg;
42                 wprintf("<tr bgcolor=\"#%s\">", (bg ? "DDDDDD" : "FFFFFF"));
43                 wprintf("<td>"
44                         "<a href=\"download_file/");
45                 urlescputs(filename);
46                 wprintf("\"><img src=\"static/diskette_24x.gif\" border=0 align=middle>\n");
47                                         escputs(filename);      wprintf("</a></td>");
48                 wprintf("<td>");        escputs(filesize);      wprintf("</td>");
49                 wprintf("<td>");        escputs(comment);       wprintf("</td>");
50                 wprintf("</tr>\n");
51         }
52
53         wprintf("</table>\n");
54
55         /** Now offer the ability to upload files... */
56         if (WC->room_flags & QR_UPLOAD)
57         {
58                 wprintf("<hr>");
59                 wprintf("<form "
60                         "enctype=\"multipart/form-data\" "
61                         "method=\"POST\" "
62                         "accept-charset=\"UTF-8\" "
63                         "action=\"upload_file\" "
64                         "name=\"upload_file_form\""
65                         ">\n"
66                 );
67                 wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%ld\">\n", WC->nonce);
68
69                 wprintf(_("Upload a file:"));
70                 wprintf("&nbsp;<input NAME=\"filename\" SIZE=16 TYPE=\"file\">&nbsp;\n");
71                 wprintf(_("Description:"));
72                 wprintf("&nbsp;<input type=\"text\" name=\"description\" maxlength=\"64\" size=\"64\">&nbsp;");
73                 wprintf("<input type=\"submit\" name=\"attach_button\" value=\"%s\">\n", _("Upload"));
74
75                 wprintf("</form>\n");
76         }
77
78         wprintf("</div>\n");
79         wDumpContent(1);
80 }
81
82
83 void download_file(char *filename)
84 {
85         char buf[256];
86         off_t bytes;
87         char content_type[256];
88         char *content = NULL;
89
90         /* Setting to nonzero forces a MIME type of application/octet-stream */
91         int force_download = 1;
92         
93         safestrncpy(buf, filename, sizeof buf);
94         unescape_input(buf);
95         serv_printf("OPEN %s", buf);
96         serv_getln(buf, sizeof buf);
97         if (buf[0] == '2') {
98                 bytes = extract_long(&buf[4], 0);
99                 content = malloc(bytes + 2);
100                 if (force_download) {
101                         strcpy(content_type, "application/octet-stream");
102                 }
103                 else {
104                         extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
105                 }
106                 output_headers(0, 0, 0, 0, 0, 0);
107                 read_server_binary(content, bytes);
108                 serv_puts("CLOS");
109                 serv_getln(buf, sizeof buf);
110                 http_transmit_thing(content, bytes, content_type, 0);
111                 free(content);
112         } else {
113                 wprintf("HTTP/1.1 404 %s\n", &buf[4]);
114                 output_headers(0, 0, 0, 0, 0, 0);
115                 wprintf("Content-Type: text/plain\r\n");
116                 wprintf("\r\n");
117                 wprintf(_("An error occurred while retrieving this file: %s\n"), &buf[4]);
118         }
119
120 }
121
122
123
124 void upload_file(void)
125 {
126         char buf[1024];
127         size_t bytes_transmitted = 0;
128         size_t blocksize;
129
130         serv_printf("UOPN %s|%s", WC->upload_filename, bstr("description"));
131         serv_getln(buf, sizeof buf);
132         if (buf[0] != '2')
133         {
134                 strcpy(WC->ImportantMessage, &buf[4]);
135                 display_room_directory();
136                 return;
137         }
138
139         while (bytes_transmitted < WC->upload_length)
140         {
141                 blocksize = 4096;
142                 if (blocksize > (WC->upload_length - bytes_transmitted))
143                 {
144                         blocksize = (WC->upload_length - bytes_transmitted);
145                 }
146                 serv_printf("WRIT %d", blocksize);
147                 serv_getln(buf, sizeof buf);
148                 if (buf[0] == '7')
149                 {
150                         blocksize = atoi(&buf[4]);
151                         serv_write(&WC->upload[bytes_transmitted], blocksize);
152                         bytes_transmitted += blocksize;
153                 }
154         }
155
156         serv_puts("UCLS 1");
157         serv_getln(buf, sizeof buf);
158         strcpy(WC->ImportantMessage, &buf[4]);
159         display_room_directory();
160 }