378cdfbc5e922375921ce81a735a0d023fba4c2a
[citadel.git] / webcit / graphics.c
1 /*
2  * $Id$
3  *
4  * Handles HTTP upload of graphics files into the system.
5  * \ingroup WebcitHttpServer
6  */
7
8 #include "webcit.h"
9
10 void display_graphics_upload(char *description, char *filename, char *uplurl)
11 {
12         StrBuf *Buf;
13         char buf[SIZ];
14
15
16         snprintf(buf, SIZ, "UIMG 0||%s", filename);
17         serv_puts(buf);
18         serv_getln(buf, sizeof buf);
19         if (buf[0] != '2') {
20                 strcpy(WC->ImportantMessage, &buf[4]);
21                 display_main_menu();
22                 return;
23         }
24         output_headers(1, 1, 0, 0, 0, 0);
25
26         output_headers(1, 1, 1, 0, 0, 0);
27
28         Buf = NewStrBufPlain(_("Image upload"), -1);
29         DoTemplate(HKEY("beginbox"), NULL, Buf, CTX_STRBUF);
30
31         FreeStrBuf(&Buf);
32
33         wprintf("<form enctype=\"multipart/form-data\" action=\"%s\" "
34                 "method=\"post\" name=\"graphicsupload\">\n", uplurl);
35
36         wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
37         wprintf("<input type=\"hidden\" name=\"which_room\" value=\"");
38         urlescputs(bstr("which_room"));
39         wprintf("\">\n");
40
41         wprintf(_("You can upload an image directly from your computer"));
42         wprintf("<br /><br />\n");
43
44         wprintf(_("Please select a file to upload:"));
45         wprintf("<input type=\"file\" name=\"filename\" size=\"35\">\n");
46
47         wprintf("<div class=\"uploadpic\"><img src=\"image&name=%s\"></div>\n", filename);
48
49         wprintf("<div class=\"buttons\">");
50         wprintf("<input type=\"submit\" name=\"upload_button\" value=\"%s\">\n", _("Upload"));
51         wprintf("&nbsp;");
52         wprintf("<input type=\"reset\" value=\"%s\">\n", _("Reset form"));
53         wprintf("&nbsp;");
54         wprintf("<input type=\"submit\" name=\"cancel_button\" value=\"%s\">\n", _("Cancel"));
55         wprintf("</div>\n");
56         wprintf("</form>\n");
57
58         do_template("endbox", NULL);
59
60         wDumpContent(1);
61 }
62
63 void do_graphics_upload(char *filename)
64 {
65         const char *MimeType;
66         char buf[SIZ];
67         int bytes_remaining;
68         int pos = 0;
69         int thisblock;
70         bytes_remaining = WC->upload_length;
71
72         if (havebstr("cancel_button")) {
73                 strcpy(WC->ImportantMessage,
74                         _("Graphics upload has been cancelled."));
75                 display_main_menu();
76                 return;
77         }
78
79         if (WC->upload_length == 0) {
80                 strcpy(WC->ImportantMessage,
81                         _("You didn't upload a file."));
82                 display_main_menu();
83                 return;
84         }
85         
86         MimeType = GuessMimeType(&WC->upload[0], bytes_remaining);
87         snprintf(buf, SIZ, "UIMG 1|%s|%s", MimeType, filename);
88         serv_puts(buf);
89
90         serv_getln(buf, sizeof buf);
91         if (buf[0] != '2') {
92                 strcpy(WC->ImportantMessage, &buf[4]);
93                 display_main_menu();
94                 return;
95         }
96         while (bytes_remaining) {
97                 thisblock = ((bytes_remaining > 4096) ? 4096 : bytes_remaining);
98                 serv_printf("WRIT %d", thisblock);
99                 serv_getln(buf, sizeof buf);
100                 if (buf[0] != '7') {
101                         strcpy(WC->ImportantMessage, &buf[4]);
102                         serv_puts("UCLS 0");
103                         serv_getln(buf, sizeof buf);
104                         display_main_menu();
105                         return;
106                 }
107                 thisblock = extract_int(&buf[4], 0);
108                 serv_write(&WC->upload[pos], thisblock);
109                 pos = pos + thisblock;
110                 bytes_remaining = bytes_remaining - thisblock;
111         }
112
113         serv_puts("UCLS 1");
114         serv_getln(buf, sizeof buf);
115         if (buf[0] != 'x') {
116                 display_success(&buf[4]);
117                 return;
118         }
119 }
120
121
122 void edithellopic(void)    { do_graphics_upload("hello"); }
123 void editpic(void)         { do_graphics_upload("_userpic_"); }
124 void editgoodbuyepic(void) { do_graphics_upload("UIMG 1|%s|goodbuye"); }
125
126 /* The users photo display / upload facility */
127 void display_editpic(void) {
128         display_graphics_upload(_("your photo"),
129                                 "_userpic_",
130                                 "editpic");
131 }
132 /* room picture dispay / upload facility */
133 void display_editroompic(void) {
134         display_graphics_upload(_("the icon for this room"),
135                                 "_roompic_",
136                                 "editroompic");
137 }
138
139 /* the greetingpage hello pic */
140 void display_edithello(void) {
141         display_graphics_upload(_("the Greetingpicture for the login prompt"),
142                                 "hello",
143                                 "edithellopic");
144 }
145
146 /* the logoff banner */
147 void display_editgoodbyepic(void) {
148         display_graphics_upload(_("the Logoff banner picture"),
149                                 "UIMG 0|%s|goodbuye",
150                                 "editgoodbuyepic");
151 }
152
153 void display_editfloorpic(void) {
154         char buf[SIZ];
155         snprintf(buf, SIZ, "UIMG 0|_floorpic_|%s",
156                  bstr("which_floor"));
157         display_graphics_upload(_("the icon for this floor"),
158                                 buf,
159                                 "editfloorpic");
160 }
161
162 void editfloorpic(void){
163         char buf[SIZ];
164         snprintf(buf, SIZ, "UIMG 1|_floorpic_|%s",
165                  bstr("which_floor"));
166         do_graphics_upload(buf);
167 }
168
169 void 
170 InitModule_GRAPHICS
171 (void)
172 {
173         WebcitAddUrlHandler(HKEY("display_editpic"), display_editpic, 0);
174         WebcitAddUrlHandler(HKEY("editpic"), editpic, 0);
175         WebcitAddUrlHandler(HKEY("display_editroompic"), display_editroompic, 0);
176         WebcitAddUrlHandler(HKEY("display_edithello"), display_edithello, 0);
177         WebcitAddUrlHandler(HKEY("edithellopic"), edithellopic, 0);
178         WebcitAddUrlHandler(HKEY("display_editgoodbyepic"), display_editgoodbyepic, 0);
179         WebcitAddUrlHandler(HKEY("editgoodbuyepic"), editgoodbuyepic, 0);
180         WebcitAddUrlHandler(HKEY("display_editfloorpic"), display_editfloorpic, 0);
181         WebcitAddUrlHandler(HKEY("editfloorpic"), editfloorpic, 0);
182 }