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