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