4d3e48e1d2f4854fc03165b7318fd11c2c9f58bd
[citadel.git] / webcit / graphics.c
1 /*
2  * $Id$
3  *
4  * Handles HTTP upload of graphics files into the system.
5  *
6  * Copyright (c) 1996-2010 by the citadel.org team
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include "webcit.h"
24
25 void display_graphics_upload(char *description, char *filename, char *uplurl)
26 {
27         WCTemplputParams SubTP;
28         StrBuf *Buf;
29         char buf[SIZ];
30
31
32         snprintf(buf, SIZ, "UIMG 0||%s", filename);
33         serv_puts(buf);
34         serv_getln(buf, sizeof buf);
35         if (buf[0] != '2') {
36                 strcpy(WC->ImportantMessage, &buf[4]);
37                 display_main_menu();
38                 return;
39         }
40         /*output_headers(1, 1, 0, 0, 0, 0); */
41
42         output_headers(1, 1, 1, 0, 0, 0);
43
44         Buf = NewStrBufPlain(_("Image upload"), -1);
45         memset(&SubTP, 0, sizeof(WCTemplputParams));
46         SubTP.Filter.ContextType = CTX_STRBUF;
47         SubTP.Context = Buf;
48         DoTemplate(HKEY("beginbox"), NULL, &SubTP);
49
50         FreeStrBuf(&Buf);
51
52         wc_printf("<form enctype=\"multipart/form-data\" action=\"%s\" "
53                 "method=\"post\" name=\"graphicsupload\">\n", uplurl);
54
55         wc_printf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
56         wc_printf("<input type=\"hidden\" name=\"which_room\" value=\"");
57         urlescputs(bstr("which_room"));
58         wc_printf("\">\n");
59
60         wc_printf(_("You can upload an image directly from your computer"));
61         wc_printf("<br /><br />\n");
62
63         wc_printf(_("Please select a file to upload:"));
64         wc_printf("<input type=\"file\" name=\"filename\" size=\"35\">\n");
65
66         wc_printf("<div class=\"uploadpic\"><img src=\"image?name=%s\"></div>\n", filename);
67
68         wc_printf("<div class=\"buttons\">");
69         wc_printf("<input type=\"submit\" name=\"upload_button\" value=\"%s\">\n", _("Upload"));
70         wc_printf("&nbsp;");
71         wc_printf("<input type=\"reset\" value=\"%s\">\n", _("Reset form"));
72         wc_printf("&nbsp;");
73         wc_printf("<input type=\"submit\" name=\"cancel_button\" value=\"%s\">\n", _("Cancel"));
74         wc_printf("</div>\n");
75         wc_printf("</form>\n");
76
77         do_template("endbox", NULL);
78
79         wDumpContent(1);
80 }
81
82 void do_graphics_upload(char *filename)
83 {
84         const char *MimeType;
85         wcsession *WCC = WC;
86         char buf[SIZ];
87         int bytes_remaining;
88         int pos = 0;
89         int thisblock;
90         bytes_remaining = WCC->upload_length;
91
92         if (havebstr("cancel_button")) {
93                 strcpy(WC->ImportantMessage,
94                         _("Graphics upload has been cancelled."));
95                 display_main_menu();
96                 return;
97         }
98
99         if (WCC->upload_length == 0) {
100                 strcpy(WC->ImportantMessage,
101                         _("You didn't upload a file."));
102                 display_main_menu();
103                 return;
104         }
105         
106         MimeType = GuessMimeType(ChrPtr(WCC->upload), bytes_remaining);
107         snprintf(buf, SIZ, "UIMG 1|%s|%s", MimeType, filename);
108         serv_puts(buf);
109
110         serv_getln(buf, sizeof buf);
111         if (buf[0] != '2') {
112                 strcpy(WCC->ImportantMessage, &buf[4]);
113                 display_main_menu();
114                 return;
115         }
116         while (bytes_remaining) {
117                 thisblock = ((bytes_remaining > 4096) ? 4096 : bytes_remaining);
118                 serv_printf("WRIT %d", thisblock);
119                 serv_getln(buf, sizeof buf);
120                 if (buf[0] != '7') {
121                         strcpy(WCC->ImportantMessage, &buf[4]);
122                         serv_puts("UCLS 0");
123                         serv_getln(buf, sizeof buf);
124                         display_main_menu();
125                         return;
126                 }
127                 thisblock = extract_int(&buf[4], 0);
128                 serv_write(&ChrPtr(WCC->upload)[pos], thisblock);
129                 pos = pos + thisblock;
130                 bytes_remaining = bytes_remaining - thisblock;
131         }
132
133         serv_puts("UCLS 1");
134         serv_getln(buf, sizeof buf);
135         if (buf[0] != 'x') {
136                 display_success(&buf[4]);
137                 return;
138         }
139 }
140
141
142 void edithellopic(void)    { do_graphics_upload("hello"); }
143 void editpic(void)         { do_graphics_upload("_userpic_"); }
144 void editgoodbuyepic(void) { do_graphics_upload("UIMG 1|%s|goodbuye"); }
145
146 /* The users photo display / upload facility */
147 void display_editpic(void) {
148         display_graphics_upload(_("your photo"),
149                                 "_userpic_",
150                                 "editpic");
151 }
152 /* room picture dispay / upload facility */
153 void display_editroompic(void) {
154         display_graphics_upload(_("the icon for this room"),
155                                 "_roompic_",
156                                 "editroompic");
157 }
158
159 /* the greetingpage hello pic */
160 void display_edithello(void) {
161         display_graphics_upload(_("the Greetingpicture for the login prompt"),
162                                 "hello",
163                                 "edithellopic");
164 }
165
166 /* the logoff banner */
167 void display_editgoodbyepic(void) {
168         display_graphics_upload(_("the Logoff banner picture"),
169                                 "UIMG 0|%s|goodbuye",
170                                 "editgoodbuyepic");
171 }
172
173 void display_editfloorpic(void) {
174         char buf[SIZ];
175         snprintf(buf, SIZ, "_floorpic_|%s",
176                  bstr("which_floor"));
177         display_graphics_upload(_("the icon for this floor"),
178                                 buf,
179                                 "editfloorpic");
180 }
181
182 void editroompic(void) {
183         char buf[SIZ];
184         snprintf(buf, SIZ, "_roompic_|%s",
185                  bstr("which_room"));
186         do_graphics_upload(buf);
187 }
188
189 void editfloorpic(void){
190         char buf[SIZ];
191         snprintf(buf, SIZ, "_floorpic_|%s",
192                  bstr("which_floor"));
193         do_graphics_upload(buf);
194 }
195
196 void 
197 InitModule_GRAPHICS
198 (void)
199 {
200         WebcitAddUrlHandler(HKEY("display_editpic"), "", 0, display_editpic, 0);
201         WebcitAddUrlHandler(HKEY("editpic"), "", 0, editpic, 0);
202         WebcitAddUrlHandler(HKEY("display_editroompic"), "", 0, display_editroompic, 0);
203         WebcitAddUrlHandler(HKEY("editroompic"), "", 0, editroompic, 0);
204         WebcitAddUrlHandler(HKEY("display_edithello"), "", 0, display_edithello, 0);
205         WebcitAddUrlHandler(HKEY("edithellopic"), "", 0, edithellopic, 0);
206         WebcitAddUrlHandler(HKEY("display_editgoodbuye"), "", 0, display_editgoodbyepic, 0);
207         WebcitAddUrlHandler(HKEY("editgoodbuyepic"), "", 0, editgoodbuyepic, 0);
208         WebcitAddUrlHandler(HKEY("display_editfloorpic"), "", 0, display_editfloorpic, 0);
209         WebcitAddUrlHandler(HKEY("editfloorpic"), "", 0, editfloorpic, 0);
210 }