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