669c865418a1c187c4d0f9128380cd45df422664
[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 User Image (see their avatar or photo or whatever)
26  * If this command succeeds, it follows the same protocol as the DLAT command.
27  */
28 void cmd_dlui(char *cmdbuf)
29 {
30         struct ctdluser ruser;
31         char buf[SIZ];
32
33         if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
34         extract_token(buf, cmdbuf, 0, '|', sizeof buf);
35         if (CtdlGetUser(&ruser, buf) != 0) {
36                 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
37                 return;
38         }
39         if (ruser.msgnum_pic < 1) {
40                 cprintf("%d No image found.\n", ERROR + FILE_NOT_FOUND);
41                 return;
42         }
43
44         struct CtdlMessage *msg = CtdlFetchMessage(ruser.msgnum_pic, 1, 1);
45         if (msg != NULL) {
46                 // The call to CtdlOutputPreLoadedMsg() with MT_SPEW_SECTION will cause the DLUI command
47                 // to have the same output format as the DLAT command, because it calls the same code.
48                 // For example: 600 402132|-1||image/gif|
49                 safestrncpy(CC->download_desired_section, "1", sizeof CC->download_desired_section);
50                 CtdlOutputPreLoadedMsg(msg, MT_SPEW_SECTION, HEADERS_NONE, 1, 0, 0);
51                 CM_Free(msg);
52         }
53         else {
54                 cprintf("%d No image found.\n", ERROR + MESSAGE_NOT_FOUND);
55                 return;
56         }
57 }
58
59
60 /*
61  * DownLoad User Image (avatar or photo or whatever)
62  */
63 void cmd_ului(char *cmdbuf)
64 {
65         long data_length;
66         char mimetype[SIZ];
67         char username[USERNAME_SIZE];
68         char userconfigroomname[ROOMNAMELEN];
69
70         if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
71
72         if (num_parms(cmdbuf) < 2)
73         {
74                 cprintf("%d Usage error\n", ERROR + ILLEGAL_VALUE);
75                 return;
76         }
77
78         data_length = extract_long(cmdbuf, 0);
79         extract_token(mimetype, cmdbuf, 1, '|', sizeof mimetype);
80         extract_token(username, cmdbuf, 2, '|', sizeof username);
81
82         if (data_length < 20) {
83                 cprintf("%d That's an awfully small file.  Try again.\n", ERROR + ILLEGAL_VALUE);
84                 return;
85         }
86
87         if (strncasecmp(mimetype, "image/", 6)) {
88                 cprintf("%d Only image files are permitted.\n", ERROR + ILLEGAL_VALUE);
89                 return;
90         }
91
92         if (IsEmptyStr(username)) {
93                 safestrncpy(username, CC->curr_user, sizeof username);
94         }
95
96         // Normal users can only change their own photo
97         if ( (strcasecmp(username, CC->curr_user)) && (CC->user.axlevel < AxAideU) && (!CC->internal_pgm) ) {
98                 cprintf("%d Higher access required to change another user's photo.\n", ERROR + HIGHER_ACCESS_REQUIRED);
99         }
100
101         // Check to make sure the user exists
102         struct ctdluser usbuf;
103         if (CtdlGetUser(&usbuf, username) != 0) {               // check for existing user, don't lock it yet
104                 cprintf("%d %s not found.\n", ERROR + NO_SUCH_USER , username);
105                 return;
106         }
107         CtdlMailboxName(userconfigroomname, sizeof userconfigroomname, &usbuf, USERCONFIGROOM);
108
109         char *unencoded_data = malloc(data_length + 1);
110         if (!unencoded_data) {
111                 cprintf("%d Could not allocate %ld bytes of memory\n", ERROR + INTERNAL_ERROR , data_length);
112                 return;
113         }
114
115         cprintf("%d %ld\n", SEND_BINARY, data_length);
116         client_read(unencoded_data, data_length);
117
118         // We've got the data read from the client, now save it.
119         char *encoded_data = malloc((data_length * 2) + 100);
120         if (encoded_data) {
121                 sprintf(encoded_data, "Content-type: %s\nContent-transfer-encoding: base64\n\n", mimetype);
122                 CtdlEncodeBase64(&encoded_data[strlen(encoded_data)], unencoded_data, data_length, 1);
123                 long new_msgnum = quickie_message("Citadel", NULL, NULL, userconfigroomname, encoded_data, FMT_RFC822, "Photo uploaded by user");
124
125                 if (CtdlGetUserLock(&usbuf, username) == 0) {   // lock it this time
126                         long old_msgnum = usbuf.msgnum_pic;
127                         syslog(LOG_DEBUG, "Message %ld is now the photo for %s", new_msgnum, username);
128                         usbuf.msgnum_pic = new_msgnum;
129                         CtdlPutUserLock(&usbuf);
130                         if (old_msgnum > 0) {
131                                 syslog(LOG_DEBUG, "Deleting old message %ld from %s", old_msgnum, userconfigroomname);
132                                 CtdlDeleteMessages(userconfigroomname, &old_msgnum, 1, "");
133                         }
134                 }
135
136                 free(encoded_data);
137         }
138
139         free(unencoded_data);
140 }
141
142
143 /*
144  * Import function called by import_old_userpic_files() for a single user
145  */
146 void import_one_userpic_file(char *username, long usernum, char *path)
147 {
148         syslog(LOG_DEBUG, "Import legacy userpic for %s, usernum=%ld, filename=%s", username, usernum, path);
149
150         FILE *fp = fopen(path, "r");
151         if (!fp) return;
152
153         fseek(fp, 0, SEEK_END);
154         long data_length = ftell(fp);
155
156         if (data_length >= 1) {
157                 rewind(fp);
158                 char *unencoded_data = malloc(data_length);
159                 if (unencoded_data) {
160                         fread(unencoded_data, data_length, 1, fp);
161                         char *encoded_data = malloc((data_length * 2) + 100);
162                         if (encoded_data) {
163                                 sprintf(encoded_data, "Content-type: %s\nContent-transfer-encoding: base64\n\n", GuessMimeByFilename(path, strlen(path)));
164                                 CtdlEncodeBase64(&encoded_data[strlen(encoded_data)], unencoded_data, data_length, 1);
165
166                                 char userconfigroomname[ROOMNAMELEN];
167                                 struct ctdluser usbuf;
168
169                                 if (CtdlGetUser(&usbuf, username) == 0) {       // no need to lock it , we are still initializing
170                                         long old_msgnum = usbuf.msgnum_pic;
171                                         CtdlMailboxName(userconfigroomname, sizeof userconfigroomname, &usbuf, USERCONFIGROOM);
172                                         long new_msgnum = quickie_message("Citadel", NULL, NULL, userconfigroomname, encoded_data, FMT_RFC822, "Photo imported from file");
173                                         syslog(LOG_DEBUG, "Message %ld is now the photo for %s", new_msgnum, username);
174                                         usbuf.msgnum_pic = new_msgnum;
175                                         CtdlPutUser(&usbuf);
176                                         unlink(path);                           // delete the old file , it's in the database now
177                                         if (old_msgnum > 0) {
178                                                 syslog(LOG_DEBUG, "Deleting old message %ld from %s", old_msgnum, userconfigroomname);
179                                                 CtdlDeleteMessages(userconfigroomname, &old_msgnum, 1, "");
180                                         }
181                                 }
182                                 free(encoded_data);
183                         }
184                         free(unencoded_data);
185                 }
186         }
187         fclose(fp);
188 }
189
190
191 /*
192  * Look for old-format "userpic" files and import them into the message base
193  */
194 void import_old_userpic_files(void)
195 {
196         DIR *filedir = NULL;
197         struct dirent *filedir_entry;
198         struct dirent *d;
199         size_t d_namelen;
200         struct ctdluser usbuf;
201         long usernum = 0;
202         int d_type = 0;
203         struct stat s;
204         char path[PATH_MAX];
205
206
207         syslog(LOG_DEBUG, "Importing old style userpic files into the message base");
208         d = (struct dirent *)malloc(offsetof(struct dirent, d_name) + PATH_MAX + 2);
209         if (d == NULL) {
210                 return;
211         }
212
213         filedir = opendir (ctdl_usrpic_dir);
214         if (filedir == NULL) {
215                 free(d);
216                 return;
217         }
218         while ((readdir_r(filedir, d, &filedir_entry) == 0) &&
219                (filedir_entry != NULL))
220         {
221 #ifdef _DIRENT_HAVE_D_NAMLEN
222                 d_namelen = filedir_entry->d_namlen;
223
224 #else
225                 d_namelen = strlen(filedir_entry->d_name);
226 #endif
227
228 #ifdef _DIRENT_HAVE_D_TYPE
229                 d_type = filedir_entry->d_type;
230 #else
231
232 #ifndef DT_UNKNOWN
233 #define DT_UNKNOWN     0
234 #define DT_DIR         4
235 #define DT_REG         8
236 #define DT_LNK         10
237
238 #define IFTODT(mode)   (((mode) & 0170000) >> 12)
239 #define DTTOIF(dirtype)        ((dirtype) << 12)
240 #endif
241                 d_type = DT_UNKNOWN;
242 #endif
243                 if ((d_namelen == 1) && 
244                     (filedir_entry->d_name[0] == '.'))
245                         continue;
246
247                 if ((d_namelen == 2) && 
248                     (filedir_entry->d_name[0] == '.') &&
249                     (filedir_entry->d_name[1] == '.'))
250                         continue;
251
252                 snprintf(path, PATH_MAX, "%s/%s", ctdl_usrpic_dir, filedir_entry->d_name);
253                 if (d_type == DT_UNKNOWN) {
254                         if (lstat(path, &s) == 0) {
255                                 d_type = IFTODT(s.st_mode);
256                         }
257                 }
258                 switch (d_type)
259                 {
260                 case DT_DIR:
261                         break;
262                 case DT_LNK:
263                 case DT_REG:
264                         usernum = atol(filedir_entry->d_name);
265                         if (CtdlGetUserByNumber(&usbuf, usernum) == 0) {
266                                 import_one_userpic_file(usbuf.fullname, usernum, path);
267                         }
268                 }
269         }
270         free(d);
271         closedir(filedir);
272         rmdir(ctdl_usrpic_dir);
273 }
274
275
276
277 CTDL_MODULE_INIT(image)
278 {
279         if (!threading)
280         {
281                 import_old_userpic_files();
282                 CtdlRegisterProtoHook(cmd_dlui, "DLUI", "DownLoad User Image");
283                 CtdlRegisterProtoHook(cmd_ului, "ULUI", "UpLoad User Image");
284         }
285         /* return our module name for the log */
286         return "image";
287 }