2a2a856a5e0192cb47881691bcd30b02d4e862a8
[citadel.git] / webcit / graphics.c
1 /*
2  * Handles HTTP upload of graphics files into the system.
3  *
4  * Copyright (c) 1996-2011 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 extern void output_static(const char* What);
24
25 void display_graphics_upload(char *filename)
26 {
27         char buf[SIZ];
28
29         snprintf(buf, SIZ, "UIMG 0||%s", filename);
30         serv_puts(buf);
31         serv_getln(buf, sizeof buf);
32         if (buf[0] != '2') {
33                 strcpy(WC->ImportantMessage, &buf[4]);
34                 display_main_menu();
35                 return;
36         }
37         output_headers(1, 0, 0, 0, 1, 0);
38         do_template("files_graphicsupload");
39         end_burst();
40 }
41
42 void do_graphics_upload(char *filename)
43 {
44         const char *MimeType;
45         wcsession *WCC = WC;
46         char buf[SIZ];
47         int bytes_remaining;
48         int pos = 0;
49         int thisblock;
50         bytes_remaining = WCC->upload_length;
51
52         if (havebstr("cancel_button")) {
53                 strcpy(WC->ImportantMessage,
54                         _("Graphics upload has been cancelled."));
55                 display_main_menu();
56                 return;
57         }
58
59         if (WCC->upload_length == 0) {
60                 strcpy(WC->ImportantMessage,
61                         _("You didn't upload a file."));
62                 display_main_menu();
63                 return;
64         }
65         
66         MimeType = GuessMimeType(ChrPtr(WCC->upload), bytes_remaining);
67         snprintf(buf, SIZ, "UIMG 1|%s|%s", MimeType, filename);
68         serv_puts(buf);
69
70         serv_getln(buf, sizeof buf);
71         if (buf[0] != '2') {
72                 strcpy(WCC->ImportantMessage, &buf[4]);
73                 display_main_menu();
74                 return;
75         }
76         while (bytes_remaining) {
77                 thisblock = ((bytes_remaining > 4096) ? 4096 : bytes_remaining);
78                 serv_printf("WRIT %d", thisblock);
79                 serv_getln(buf, sizeof buf);
80                 if (buf[0] != '7') {
81                         strcpy(WCC->ImportantMessage, &buf[4]);
82                         serv_puts("UCLS 0");
83                         serv_getln(buf, sizeof buf);
84                         display_main_menu();
85                         return;
86                 }
87                 thisblock = extract_int(&buf[4], 0);
88                 serv_write(&ChrPtr(WCC->upload)[pos], thisblock);
89                 pos = pos + thisblock;
90                 bytes_remaining = bytes_remaining - thisblock;
91         }
92
93         serv_puts("UCLS 1");
94         serv_getln(buf, sizeof buf);
95         if (buf[0] != 'x') {
96                 display_success(&buf[4]);
97                 return;
98         }
99 }
100
101
102 void edithellopic(void)    { do_graphics_upload("hello"); }
103 void editpic(void)         { do_graphics_upload("_userpic_"); }
104 void editgoodbuyepic(void) { do_graphics_upload("UIMG 1|%s|goodbuye"); }
105
106 /* The users photo display / upload facility */
107 void display_editpic(void) {
108         putbstr("__WHICHPIC", NewStrBufPlain(HKEY("_userpic_")));
109         putbstr("__PICDESC", NewStrBufPlain(_("your photo"), -1));
110         putbstr("__UPLURL", NewStrBufPlain(HKEY("editpic")));
111         display_graphics_upload("editpic");
112 }
113 /* room picture dispay / upload facility */
114 void display_editroompic(void) {
115         putbstr("__WHICHPIC", NewStrBufPlain(HKEY("_roompic_")));
116         putbstr("__PICDESC", NewStrBufPlain(_("the icon for this room"), -1));
117         putbstr("__UPLURL", NewStrBufPlain(HKEY("editroompic")));
118         display_graphics_upload("editroompic");
119 }
120
121 /* the greetingpage hello pic */
122 void display_edithello(void) {
123         putbstr("__WHICHPIC", NewStrBufPlain(HKEY("hello")));
124         putbstr("__PICDESC", NewStrBufPlain(_("the Greetingpicture for the login prompt"), -1));
125         putbstr("__UPLURL", NewStrBufPlain(HKEY("edithellopic")));
126         display_graphics_upload("edithellopic");
127 }
128
129 /* the logoff banner */
130 void display_editgoodbyepic(void) {
131         putbstr("__WHICHPIC", NewStrBufPlain(HKEY("UIMG 0|%s|goodbuye")));
132         putbstr("__PICDESC", NewStrBufPlain(_("the Logoff banner picture"), -1));
133         putbstr("__UPLURL", NewStrBufPlain(HKEY("editgoodbuyepic")));
134         display_graphics_upload("editgoodbuyepic");
135 }
136
137 void display_editfloorpic(void) {
138         StrBuf *PicAction;
139
140         PicAction = NewStrBuf();
141         StrBufPrintf(PicAction, "_floorpic_|%s", bstr("which_floor"));
142         putbstr("__WHICHPIC", PicAction);
143         putbstr("__PICDESC", NewStrBufPlain(_("the icon for this floor"), -1));
144         putbstr("__UPLURL", NewStrBufPlain(HKEY("editfloorpic")));
145         display_graphics_upload("editfloorpic");
146 }
147
148 void editroompic(void) {
149         char buf[SIZ];
150         snprintf(buf, SIZ, "_roompic_|%s",
151                  bstr("which_room"));
152         do_graphics_upload(buf);
153 }
154
155 void editfloorpic(void){
156         char buf[SIZ];
157         snprintf(buf, SIZ, "_floorpic_|%s",
158                  bstr("which_floor"));
159         do_graphics_upload(buf);
160 }
161
162 void 
163 InitModule_GRAPHICS
164 (void)
165 {
166         WebcitAddUrlHandler(HKEY("display_editpic"), "", 0, display_editpic, 0);
167         WebcitAddUrlHandler(HKEY("editpic"), "", 0, editpic, 0);
168         WebcitAddUrlHandler(HKEY("display_editroompic"), "", 0, display_editroompic, 0);
169         WebcitAddUrlHandler(HKEY("editroompic"), "", 0, editroompic, 0);
170         WebcitAddUrlHandler(HKEY("display_edithello"), "", 0, display_edithello, 0);
171         WebcitAddUrlHandler(HKEY("edithellopic"), "", 0, edithellopic, 0);
172         WebcitAddUrlHandler(HKEY("display_editgoodbuye"), "", 0, display_editgoodbyepic, 0);
173         WebcitAddUrlHandler(HKEY("editgoodbuyepic"), "", 0, editgoodbuyepic, 0);
174         WebcitAddUrlHandler(HKEY("display_editfloorpic"), "", 0, display_editfloorpic, 0);
175         WebcitAddUrlHandler(HKEY("editfloorpic"), "", 0, editfloorpic, 0);
176 }