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