* When no icon is present on the server for a room, display the default
[citadel.git] / webcit / graphics.c
1 /*
2  * $Id$
3  *
4  * Handles HTTP upload of graphics files into the system.
5  */
6
7 #include <ctype.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <stdio.h>
11 #include <fcntl.h>
12 #include <signal.h>
13 #include <sys/types.h>
14 #include <sys/wait.h>
15 #include <sys/socket.h>
16 #include <sys/time.h>
17 #include <limits.h>
18 #include <netinet/in.h>
19 #include <netdb.h>
20 #include <string.h>
21 #include <pwd.h>
22 #include <errno.h>
23 #include <stdarg.h>
24 #include <pthread.h>
25 #include <signal.h>
26 #include "webcit.h"
27
28 void display_graphics_upload(char *description, char *check_cmd, char *uplurl)
29 {
30         char buf[SIZ];
31
32         serv_puts(check_cmd);
33         serv_getln(buf, sizeof buf);
34         if (buf[0] != '2') {
35                 strcpy(WC->ImportantMessage, &buf[4]);
36                 display_main_menu();
37                 return;
38         }
39         output_headers(1, 1, 0, 0, 0, 0, 0);
40
41         output_headers(1, 1, 2, 0, 0, 0, 0);
42         wprintf("<div id=\"banner\">\n"
43                 "<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#444455\"><TR><TD>"
44                 "<SPAN CLASS=\"titlebar\">Image upload</SPAN>"
45                 "</TD></TR></TABLE>\n"
46                 "</div>\n<div id=\"content\">\n"
47         );
48
49         wprintf("<div id=\"fix_scrollbar_bug\">"
50                 "<table border=0 width=100%% bgcolor=\"#ffffff\"><tr><td>\n");
51
52         wprintf("<CENTER>\n");
53
54         wprintf("<FORM ENCTYPE=\"multipart/form-data\" ACTION=\"%s\" "
55                 "METHOD=\"POST\" NAME=\"graphicsupload\">\n", uplurl);
56
57         wprintf("<INPUT TYPE=\"hidden\" NAME=\"which_room\" VALUE=\"");
58         urlescputs(bstr("which_room"));
59         wprintf("\">\n");
60
61         wprintf("You can upload any image directly from your computer,\n");
62         wprintf("as long as it is in GIF format (JPEG, PNG, etc. won't\n");
63         wprintf("work).<br /><br />\n");
64
65         wprintf("Please select a file to upload:<br /><br />\n");
66         wprintf("<INPUT TYPE=\"FILE\" NAME=\"filename\" SIZE=\"35\">\n");
67         wprintf("<br /><br />");
68         wprintf("<INPUT TYPE=\"SUBMIT\" NAME=\"sc\" VALUE=\"Upload\">\n");
69         wprintf("&nbsp;");
70         wprintf("<INPUT TYPE=\"RESET\" VALUE=\"Reset Form\">\n");
71         wprintf("&nbsp;");
72         wprintf("<INPUT TYPE=\"SUBMIT\" NAME=\"sc\" VALUE=\"Cancel\">\n");
73         wprintf("</FORM>\n");
74         wprintf("</CENTER>\n");
75         wprintf("</td></tr></table></div>\n");
76         wDumpContent(1);
77 }
78
79 void do_graphics_upload(char *upl_cmd)
80 {
81         char buf[SIZ];
82         int bytes_remaining;
83         int pos = 0;
84         int thisblock;
85
86         if (!strcasecmp(bstr("sc"), "Cancel")) {
87                 strcpy(WC->ImportantMessage,
88                         "Graphics upload cancelled.");
89                 display_main_menu();
90                 return;
91         }
92
93         if (WC->upload_length == 0) {
94                 strcpy(WC->ImportantMessage,
95                         "You didn't upload a file.");
96                 display_main_menu();
97                 return;
98         }
99         serv_puts(upl_cmd);
100         serv_getln(buf, sizeof buf);
101         if (buf[0] != '2') {
102                 strcpy(WC->ImportantMessage, &buf[4]);
103                 display_main_menu();
104                 return;
105         }
106         bytes_remaining = WC->upload_length;
107         while (bytes_remaining) {
108                 thisblock = ((bytes_remaining > 4096) ? 4096 : bytes_remaining);
109                 serv_printf("WRIT %d", thisblock);
110                 serv_getln(buf, sizeof buf);
111                 if (buf[0] != '7') {
112                         strcpy(WC->ImportantMessage, &buf[4]);
113                         serv_puts("UCLS 0");
114                         serv_getln(buf, sizeof buf);
115                         display_main_menu();
116                         return;
117                 }
118                 thisblock = extract_int(&buf[4], 0);
119                 serv_write(&WC->upload[pos], thisblock);
120                 pos = pos + thisblock;
121                 bytes_remaining = bytes_remaining - thisblock;
122         }
123
124         serv_puts("UCLS 1");
125         serv_getln(buf, sizeof buf);
126         if (buf[0] != 'x') {
127                 display_success(&buf[4]);
128                 return;
129         }
130 }