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