Changed 'the Greetingpicture for the login prompt' to 'graphics to be displayed on...
[citadel.git] / webcit / graphics.c
1
2 /*
3  * Handles HTTP upload of graphics files into the system.
4  *
5  * Copyright (c) 1996-2016 by the citadel.org team
6  *
7  * This program is open source software.  You can redistribute it and/or
8  * modify it under the terms of the GNU General Public License, version 3.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15
16 #include "webcit.h"
17
18 extern void output_static(const char *What);
19
20
21 // display the picture (icon, photo, whatever) associated with the current room
22 void display_roompic(void) {
23         off_t bytes;
24         StrBuf *Buf = NewStrBuf();
25         serv_printf("DLRI");
26         StrBuf_ServGetln(Buf);
27         if (GetServerStatus(Buf, NULL) == 6) {
28                 StrBufCutLeft(Buf, 4);
29                 bytes = StrBufExtract_long(Buf, 0, '|');
30                 StrBuf *content_type = NewStrBuf();
31                 StrBufExtract_token(content_type, Buf, 3, '|');
32                 WC->WBuf = NewStrBuf();
33                 StrBuf_ServGetBLOBBuffered(WC->WBuf, bytes);
34                 http_transmit_thing(ChrPtr(content_type), 0);
35                 FreeStrBuf(&content_type);
36         }
37         else {
38                 output_error_pic("", "");
39         }
40         FreeStrBuf(&Buf);
41 }
42
43
44 // upload the picture (icon, photo, whatever) associated with the current room
45 void common_code_for_editroompic_and_editpic(char *servcmd) {
46         if (havebstr("cancel_button")) {
47                 AppendImportantMessage(_("Graphics upload has been cancelled."), -1);
48                 display_main_menu();
49                 return;
50         }
51
52         if (WC->upload_length == 0) {
53                 AppendImportantMessage(_("You didn't upload a file."), -1);
54                 display_main_menu();
55                 return;
56         }
57
58         serv_printf("%s %ld|%s", servcmd, (long) WC->upload_length, GuessMimeType(ChrPtr(WC->upload), WC->upload_length));
59         StrBuf *Line = NewStrBuf();
60         StrBuf_ServGetln(Line);
61         if (GetServerStatusMsg(Line, NULL, 0, 0) == 7) {
62                 serv_write(ChrPtr(WC->upload), WC->upload_length);
63                 display_success(ChrPtr(Line) + 4);
64         }
65         else {
66                 AppendImportantMessage((ChrPtr(Line) + 4), -1);
67                 display_main_menu();
68         }
69         FreeStrBuf(&Line);
70 }
71
72
73 // upload the picture (icon, photo, whatever) associated with the current room
74 void editroompic(void) {
75         common_code_for_editroompic_and_editpic("ULRI");
76 }
77
78
79 // upload the picture (icon, photo, whatever) associated with the current user
80 void editpic(void) {
81         common_code_for_editroompic_and_editpic("ULUI");
82 }
83
84
85 // display the screen for uploading graphics to the server
86 void display_graphics_upload(char *filename) {
87         StrBuf *Line;
88
89         Line = NewStrBuf();
90         serv_printf("UIMG 0||%s", filename);
91         StrBuf_ServGetln(Line);
92         if (GetServerStatusMsg(Line, NULL, 1, 2) != 2) {
93                 display_main_menu();
94                 return;
95         }
96         else {
97                 output_headers(1, 0, 0, 0, 1, 0);
98                 do_template("files_graphicsupload");
99                 end_burst();
100         }
101         FreeStrBuf(&Line);
102 }
103
104
105 void do_graphics_upload(char *filename) {
106         StrBuf *Line;
107         const char *MimeType;
108         wcsession *WCC = WC;
109         int bytes_remaining;
110         int pos = 0;
111         int thisblock;
112         bytes_remaining = WCC->upload_length;
113
114         if (havebstr("cancel_button")) {
115                 AppendImportantMessage(_("Graphics upload has been cancelled."), -1);
116                 display_main_menu();
117                 return;
118         }
119
120         if (WCC->upload_length == 0) {
121                 AppendImportantMessage(_("You didn't upload a file."), -1);
122                 display_main_menu();
123                 return;
124         }
125
126         MimeType = GuessMimeType(ChrPtr(WCC->upload), bytes_remaining);
127         serv_printf("UIMG 1|%s|%s", MimeType, filename);
128
129         Line = NewStrBuf();
130         StrBuf_ServGetln(Line);
131         if (GetServerStatusMsg(Line, NULL, 1, 2) != 2) {
132                 display_main_menu();
133                 FreeStrBuf(&Line);
134                 return;
135         }
136         while (bytes_remaining) {
137                 thisblock = ((bytes_remaining > 4096) ? 4096 : bytes_remaining);
138                 serv_printf("WRIT %d", thisblock);
139                 StrBuf_ServGetln(Line);
140                 if (GetServerStatusMsg(Line, NULL, 1, 7) != 7) {
141                         serv_puts("UCLS 0");
142                         StrBuf_ServGetln(Line);
143                         display_main_menu();
144                         FreeStrBuf(&Line);
145                         return;
146                 }
147                 thisblock = extract_int(ChrPtr(Line) + 4, 0);
148                 serv_write(&ChrPtr(WCC->upload)[pos], thisblock);
149                 pos += thisblock;
150                 bytes_remaining -= thisblock;
151         }
152
153         serv_puts("UCLS 1");
154         StrBuf_ServGetln(Line);
155         if (*ChrPtr(Line) != 'x') {
156                 display_success(ChrPtr(Line) + 4);
157
158         }
159         FreeStrBuf(&Line);
160
161 }
162
163
164 void edithellopic(void) {
165         do_graphics_upload("hello");
166 }
167 void editgoodbuyepic(void) {
168         do_graphics_upload("UIMG 1|%s|goodbuye");
169 }
170
171 /* The users photo display / upload facility */
172 void display_editpic(void) {
173         putbstr("__PICDESC", NewStrBufPlain(_("your photo"), -1));
174         putbstr("__UPLURL", NewStrBufPlain(HKEY("editpic")));
175         display_graphics_upload("editpic");
176 }
177
178 /* room picture dispay / upload facility */
179 void display_editroompic(void) {
180         putbstr("__PICDESC", NewStrBufPlain(_("the icon for this room"), -1));
181         putbstr("__UPLURL", NewStrBufPlain(HKEY("editroompic")));
182         display_graphics_upload("editroompic");
183 }
184
185 /* the greetingpage hello pic */
186 void display_edithello(void) {
187         putbstr("__WHICHPIC", NewStrBufPlain(HKEY("hello")));
188         putbstr("__PICDESC", NewStrBufPlain(_("graphics to be displayed on the login screen"), -1));
189         putbstr("__UPLURL", NewStrBufPlain(HKEY("edithellopic")));
190         display_graphics_upload("edithellopic");
191 }
192
193 /* the logoff banner */
194 void display_editgoodbyepic(void) {
195         putbstr("__WHICHPIC", NewStrBufPlain(HKEY("UIMG 0|%s|goodbuye")));
196         putbstr("__PICDESC", NewStrBufPlain(_("the Logoff banner picture"), -1));
197         putbstr("__UPLURL", NewStrBufPlain(HKEY("editgoodbuyepic")));
198         display_graphics_upload("editgoodbuyepic");
199 }
200
201
202 void InitModule_GRAPHICS(void) {
203         WebcitAddUrlHandler(HKEY("display_editpic"), "", 0, display_editpic, 0);
204         WebcitAddUrlHandler(HKEY("editpic"), "", 0, editpic, 0);
205         WebcitAddUrlHandler(HKEY("display_editroompic"), "", 0, display_editroompic, 0);
206         WebcitAddUrlHandler(HKEY("editroompic"), "", 0, editroompic, 0);
207         WebcitAddUrlHandler(HKEY("display_edithello"), "", 0, display_edithello, 0);
208         WebcitAddUrlHandler(HKEY("edithellopic"), "", 0, edithellopic, 0);
209         WebcitAddUrlHandler(HKEY("display_editgoodbuye"), "", 0, display_editgoodbyepic, 0);
210         WebcitAddUrlHandler(HKEY("editgoodbuyepic"), "", 0, editgoodbuyepic, 0);
211         WebcitAddUrlHandler(HKEY("roompic"), "", 0, display_roompic, 0);
212 }