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