a33143f574b3ad2c69afb35bad3dcfe6e9478eed
[citadel.git] / citadel / modules / image / serv_image.c
1 /*
2  * Copyright (c) 1987-2016 by the citadel.org team
3  *
4  * This program is open source software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
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 "ctdl_module.h"
16 #include "config.h"
17
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <dirent.h>
21
22
23
24 /*
25  * DownLoad Room Image (see its icon or whatever)
26  * If this command succeeds, it follows the same protocol as the DLAT command.
27  */
28 void cmd_dlri(char *cmdbuf)
29 {
30         if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
31         if (CC->room.msgnum_pic < 1) {
32                 cprintf("%d No image found.\n", ERROR + FILE_NOT_FOUND);
33                 return;
34         }
35
36         struct CtdlMessage *msg = CtdlFetchMessage(CC->room.msgnum_pic, 1, 1);
37         if (msg != NULL) {
38                 // The call to CtdlOutputPreLoadedMsg() with MT_SPEW_SECTION will cause the DLUI command
39                 // to have the same output format as the DLAT command, because it calls the same code.
40                 // For example: 600 402132|-1||image/gif|
41                 safestrncpy(CC->download_desired_section, "1", sizeof CC->download_desired_section);
42                 CtdlOutputPreLoadedMsg(msg, MT_SPEW_SECTION, HEADERS_NONE, 1, 0, 0);
43                 CM_Free(msg);
44         }
45         else {
46                 cprintf("%d No image found.\n", ERROR + MESSAGE_NOT_FOUND);
47                 return;
48         }
49 }
50
51
52 /*
53  * UpLoad Room Image (avatar or photo or whatever)
54  */
55 void cmd_ulri(char *cmdbuf)
56 {
57         long data_length;
58         char mimetype[SIZ];
59
60         if (CtdlAccessCheck(ac_room_aide)) return;
61
62         data_length = extract_long(cmdbuf, 0);
63         extract_token(mimetype, cmdbuf, 1, '|', sizeof mimetype);
64
65         if (data_length < 20) {
66                 cprintf("%d That's an awfully small file.  Try again.\n", ERROR + ILLEGAL_VALUE);
67                 return;
68         }
69
70         if (strncasecmp(mimetype, "image/", 6)) {
71                 cprintf("%d Only image files are permitted.\n", ERROR + ILLEGAL_VALUE);
72                 return;
73         }
74
75         char *unencoded_data = malloc(data_length + 1);
76         if (!unencoded_data) {
77                 cprintf("%d Could not allocate %ld bytes of memory\n", ERROR + INTERNAL_ERROR , data_length);
78                 return;
79         }
80
81         cprintf("%d %ld\n", SEND_BINARY, data_length);
82         client_read(unencoded_data, data_length);
83
84         // We've got the data read from the client, now save it.
85         char *encoded_data = malloc((data_length * 2) + 100);
86         if (encoded_data) {
87                 sprintf(encoded_data, "Content-type: %s\nContent-transfer-encoding: base64\n\n", mimetype);
88                 CtdlEncodeBase64(&encoded_data[strlen(encoded_data)], unencoded_data, data_length, 1);
89                 long new_msgnum = quickie_message("Citadel", NULL, NULL, SYSCONFIGROOM, encoded_data, FMT_RFC822, "Image uploaded by admin user");
90
91                 if (CtdlGetRoomLock(&CC->room, CC->room.QRname) == 0) {
92                         long old_msgnum = CC->room.msgnum_pic;
93                         syslog(LOG_DEBUG, "Message %ld is now the photo for %s", new_msgnum, CC->room.QRname);
94                         CC->room.msgnum_pic = new_msgnum;
95                         CtdlPutRoomLock(&CC->room);
96                         if (old_msgnum > 0) {
97                                 syslog(LOG_DEBUG, "Deleting old message %ld from %s", old_msgnum, SYSCONFIGROOM);
98                                 CtdlDeleteMessages(SYSCONFIGROOM, &old_msgnum, 1, "");
99                         }
100                 }
101                 free(encoded_data);
102         }
103
104         free(unencoded_data);
105 }
106
107
108 /*
109  * DownLoad User Image (see their avatar or photo or whatever)
110  * If this command succeeds, it follows the same protocol as the DLAT command.
111  */
112 void cmd_dlui(char *cmdbuf)
113 {
114         struct ctdluser ruser;
115         char buf[SIZ];
116
117         if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
118         extract_token(buf, cmdbuf, 0, '|', sizeof buf);
119         if (CtdlGetUser(&ruser, buf) != 0) {
120                 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
121                 return;
122         }
123         if (ruser.msgnum_pic < 1) {
124                 cprintf("%d No image found.\n", ERROR + FILE_NOT_FOUND);
125                 return;
126         }
127
128         struct CtdlMessage *msg = CtdlFetchMessage(ruser.msgnum_pic, 1, 1);
129         if (msg != NULL) {
130                 // The call to CtdlOutputPreLoadedMsg() with MT_SPEW_SECTION will cause the DLUI command
131                 // to have the same output format as the DLAT command, because it calls the same code.
132                 // For example: 600 402132|-1||image/gif|
133                 safestrncpy(CC->download_desired_section, "1", sizeof CC->download_desired_section);
134                 CtdlOutputPreLoadedMsg(msg, MT_SPEW_SECTION, HEADERS_NONE, 1, 0, 0);
135                 CM_Free(msg);
136         }
137         else {
138                 cprintf("%d No image found.\n", ERROR + MESSAGE_NOT_FOUND);
139                 return;
140         }
141 }
142
143
144 /*
145  * UpLoad User Image (avatar or photo or whatever)
146  */
147 void cmd_ului(char *cmdbuf)
148 {
149         long data_length;
150         char mimetype[SIZ];
151         char username[USERNAME_SIZE];
152         char userconfigroomname[ROOMNAMELEN];
153
154         if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
155
156         if (num_parms(cmdbuf) < 2)
157         {
158                 cprintf("%d Usage error\n", ERROR + ILLEGAL_VALUE);
159                 return;
160         }
161
162         data_length = extract_long(cmdbuf, 0);
163         extract_token(mimetype, cmdbuf, 1, '|', sizeof mimetype);
164         extract_token(username, cmdbuf, 2, '|', sizeof username);
165
166         if (data_length < 20) {
167                 cprintf("%d That's an awfully small file.  Try again.\n", ERROR + ILLEGAL_VALUE);
168                 return;
169         }
170
171         if (strncasecmp(mimetype, "image/", 6)) {
172                 cprintf("%d Only image files are permitted.\n", ERROR + ILLEGAL_VALUE);
173                 return;
174         }
175
176         if (IsEmptyStr(username)) {
177                 safestrncpy(username, CC->curr_user, sizeof username);
178         }
179
180         // Normal users can only change their own photo
181         if ( (strcasecmp(username, CC->curr_user)) && (CC->user.axlevel < AxAideU) && (!CC->internal_pgm) ) {
182                 cprintf("%d Higher access required to change another user's photo.\n", ERROR + HIGHER_ACCESS_REQUIRED);
183         }
184
185         // Check to make sure the user exists
186         struct ctdluser usbuf;
187         if (CtdlGetUser(&usbuf, username) != 0) {               // check for existing user, don't lock it yet
188                 cprintf("%d %s not found.\n", ERROR + NO_SUCH_USER , username);
189                 return;
190         }
191         CtdlMailboxName(userconfigroomname, sizeof userconfigroomname, &usbuf, USERCONFIGROOM);
192
193         char *unencoded_data = malloc(data_length + 1);
194         if (!unencoded_data) {
195                 cprintf("%d Could not allocate %ld bytes of memory\n", ERROR + INTERNAL_ERROR , data_length);
196                 return;
197         }
198
199         cprintf("%d %ld\n", SEND_BINARY, data_length);
200         client_read(unencoded_data, data_length);
201
202         // We've got the data read from the client, now save it.
203         char *encoded_data = malloc((data_length * 2) + 100);
204         if (encoded_data) {
205                 sprintf(encoded_data, "Content-type: %s\nContent-transfer-encoding: base64\n\n", mimetype);
206                 CtdlEncodeBase64(&encoded_data[strlen(encoded_data)], unencoded_data, data_length, 1);
207                 long new_msgnum = quickie_message("Citadel", NULL, NULL, userconfigroomname, encoded_data, FMT_RFC822, "Photo uploaded by user");
208
209                 if (CtdlGetUserLock(&usbuf, username) == 0) {   // lock it this time
210                         long old_msgnum = usbuf.msgnum_pic;
211                         syslog(LOG_DEBUG, "Message %ld is now the photo for %s", new_msgnum, username);
212                         usbuf.msgnum_pic = new_msgnum;
213                         CtdlPutUserLock(&usbuf);
214                         if (old_msgnum > 0) {
215                                 syslog(LOG_DEBUG, "Deleting old message %ld from %s", old_msgnum, userconfigroomname);
216                                 CtdlDeleteMessages(userconfigroomname, &old_msgnum, 1, "");
217                         }
218                 }
219
220                 free(encoded_data);
221         }
222
223         free(unencoded_data);
224 }
225
226
227 /*
228  * Import function called by import_old_userpic_files() for a single user
229  */
230 void import_one_userpic_file(char *username, long usernum, char *path)
231 {
232         syslog(LOG_DEBUG, "Import legacy userpic for %s, usernum=%ld, filename=%s", username, usernum, path);
233
234         FILE *fp = fopen(path, "r");
235         if (!fp) return;
236
237         fseek(fp, 0, SEEK_END);
238         long data_length = ftell(fp);
239
240         if (data_length >= 1) {
241                 rewind(fp);
242                 char *unencoded_data = malloc(data_length);
243                 if (unencoded_data) {
244                         fread(unencoded_data, data_length, 1, fp);
245                         char *encoded_data = malloc((data_length * 2) + 100);
246                         if (encoded_data) {
247                                 sprintf(encoded_data, "Content-type: %s\nContent-transfer-encoding: base64\n\n", GuessMimeByFilename(path, strlen(path)));
248                                 CtdlEncodeBase64(&encoded_data[strlen(encoded_data)], unencoded_data, data_length, 1);
249
250                                 char userconfigroomname[ROOMNAMELEN];
251                                 struct ctdluser usbuf;
252
253                                 if (CtdlGetUser(&usbuf, username) == 0) {       // no need to lock it , we are still initializing
254                                         long old_msgnum = usbuf.msgnum_pic;
255                                         CtdlMailboxName(userconfigroomname, sizeof userconfigroomname, &usbuf, USERCONFIGROOM);
256                                         long new_msgnum = quickie_message("Citadel", NULL, NULL, userconfigroomname, encoded_data, FMT_RFC822, "Photo imported from file");
257                                         syslog(LOG_DEBUG, "Message %ld is now the photo for %s", new_msgnum, username);
258                                         usbuf.msgnum_pic = new_msgnum;
259                                         CtdlPutUser(&usbuf);
260                                         unlink(path);                           // delete the old file , it's in the database now
261                                         if (old_msgnum > 0) {
262                                                 syslog(LOG_DEBUG, "Deleting old message %ld from %s", old_msgnum, userconfigroomname);
263                                                 CtdlDeleteMessages(userconfigroomname, &old_msgnum, 1, "");
264                                         }
265                                 }
266                                 free(encoded_data);
267                         }
268                         free(unencoded_data);
269                 }
270         }
271         fclose(fp);
272 }
273
274
275 /*
276  * Look for old-format "userpic" files and import them into the message base
277  */
278 void import_old_userpic_files(void)
279 {
280         DIR *filedir = NULL;
281         struct dirent *filedir_entry;
282         struct dirent *d;
283         size_t d_namelen;
284         struct ctdluser usbuf;
285         long usernum = 0;
286         int d_type = 0;
287         struct stat s;
288         char path[PATH_MAX];
289
290
291         syslog(LOG_DEBUG, "Importing old style userpic files into the message base");
292         d = (struct dirent *)malloc(offsetof(struct dirent, d_name) + PATH_MAX + 2);
293         if (d == NULL) {
294                 return;
295         }
296
297         filedir = opendir (ctdl_usrpic_dir);
298         if (filedir == NULL) {
299                 free(d);
300                 return;
301         }
302         while ((readdir_r(filedir, d, &filedir_entry) == 0) &&
303                (filedir_entry != NULL))
304         {
305 #ifdef _DIRENT_HAVE_D_NAMLEN
306                 d_namelen = filedir_entry->d_namlen;
307
308 #else
309                 d_namelen = strlen(filedir_entry->d_name);
310 #endif
311
312 #ifdef _DIRENT_HAVE_D_TYPE
313                 d_type = filedir_entry->d_type;
314 #else
315
316 #ifndef DT_UNKNOWN
317 #define DT_UNKNOWN     0
318 #define DT_DIR         4
319 #define DT_REG         8
320 #define DT_LNK         10
321
322 #define IFTODT(mode)   (((mode) & 0170000) >> 12)
323 #define DTTOIF(dirtype)        ((dirtype) << 12)
324 #endif
325                 d_type = DT_UNKNOWN;
326 #endif
327                 if ((d_namelen == 1) && 
328                     (filedir_entry->d_name[0] == '.'))
329                         continue;
330
331                 if ((d_namelen == 2) && 
332                     (filedir_entry->d_name[0] == '.') &&
333                     (filedir_entry->d_name[1] == '.'))
334                         continue;
335
336                 snprintf(path, PATH_MAX, "%s/%s", ctdl_usrpic_dir, filedir_entry->d_name);
337                 if (d_type == DT_UNKNOWN) {
338                         if (lstat(path, &s) == 0) {
339                                 d_type = IFTODT(s.st_mode);
340                         }
341                 }
342                 switch (d_type)
343                 {
344                 case DT_DIR:
345                         break;
346                 case DT_LNK:
347                 case DT_REG:
348                         usernum = atol(filedir_entry->d_name);
349                         if (CtdlGetUserByNumber(&usbuf, usernum) == 0) {
350                                 import_one_userpic_file(usbuf.fullname, usernum, path);
351                         }
352                 }
353         }
354         free(d);
355         closedir(filedir);
356         rmdir(ctdl_usrpic_dir);
357 }
358
359
360
361 CTDL_MODULE_INIT(image)
362 {
363         if (!threading)
364         {
365                 import_old_userpic_files();
366                 CtdlRegisterProtoHook(cmd_dlri, "DLRI", "DownLoad Room Image");
367                 CtdlRegisterProtoHook(cmd_ulri, "ULRI", "UpLoad Room Image");
368                 CtdlRegisterProtoHook(cmd_dlui, "DLUI", "DownLoad User Image");
369                 CtdlRegisterProtoHook(cmd_ului, "ULUI", "UpLoad User Image");
370         }
371         /* return our module name for the log */
372         return "image";
373 }