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