Remove all references to userpic dir other than those required for ingesting legacy...
[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  * Import function called by import_old_userpic_files() for a single user
25  */
26 void import_one_userpic_file(char *username, long usernum, char *path)
27 {
28         syslog(LOG_DEBUG, "Import legacy userpic for %s, usernum=%ld, filename=%s", username, usernum, path);
29
30         FILE *fp = fopen(path, "r");
31         if (!fp) return;
32
33         fseek(fp, 0, SEEK_END);
34         long data_length = ftell(fp);
35
36         if (data_length >= 1) {
37                 rewind(fp);
38                 char *unencoded_data = malloc(data_length);
39                 if (unencoded_data) {
40                         fread(unencoded_data, data_length, 1, fp);
41                         char *encoded_data = malloc((data_length * 2) + 100);
42                         if (encoded_data) {
43                                 sprintf(encoded_data, "Content-type: image/gif\nContent-transfer-encoding: base64\n\n");
44                                 CtdlEncodeBase64(&encoded_data[strlen(encoded_data)], unencoded_data, data_length, 1);
45
46                                 char userconfigroomname[ROOMNAMELEN];
47                                 struct ctdluser usbuf;
48
49                                 if (CtdlGetUser(&usbuf, username) == 0) {       // no need to lock it , we are still initializing
50                                         long old_msgnum = usbuf.msgnum_pic;
51                                         CtdlMailboxName(userconfigroomname, sizeof userconfigroomname, &usbuf, USERCONFIGROOM);
52                                         long new_msgnum = quickie_message("Citadel", NULL, NULL, userconfigroomname, encoded_data, FMT_RFC822, "Photo imported from file");
53                                         syslog(LOG_DEBUG, "Message %ld is now the profile for %s", new_msgnum, username);
54                                         usbuf.msgnum_pic = new_msgnum;
55                                         CtdlPutUser(&usbuf);
56                                         unlink(path);                           // delete the old file , it's in the database now
57                                         if (old_msgnum > 0) {
58                                                 syslog(LOG_DEBUG, "Deleting old message %ld from %s", old_msgnum, userconfigroomname);
59                                                 CtdlDeleteMessages(userconfigroomname, &old_msgnum, 1, "");
60                                         }
61                                 }
62                                 free(encoded_data);
63                         }
64                         free(unencoded_data);
65                 }
66         }
67         fclose(fp);
68 }
69
70
71 /*
72  * Look for old-format "userpic" files and import them into the message base
73  */
74 void import_old_userpic_files(void)
75 {
76         DIR *filedir = NULL;
77         struct dirent *filedir_entry;
78         struct dirent *d;
79         size_t d_namelen;
80         struct ctdluser usbuf;
81         long usernum = 0;
82         int d_type = 0;
83         struct stat s;
84         char path[PATH_MAX];
85
86
87         syslog(LOG_DEBUG, "Importing old style userpic files into the message base");
88         d = (struct dirent *)malloc(offsetof(struct dirent, d_name) + PATH_MAX + 2);
89         if (d == NULL) {
90                 return;
91         }
92
93         filedir = opendir (ctdl_usrpic_dir);
94         if (filedir == NULL) {
95                 free(d);
96                 return;
97         }
98         while ((readdir_r(filedir, d, &filedir_entry) == 0) &&
99                (filedir_entry != NULL))
100         {
101 #ifdef _DIRENT_HAVE_D_NAMLEN
102                 d_namelen = filedir_entry->d_namlen;
103
104 #else
105                 d_namelen = strlen(filedir_entry->d_name);
106 #endif
107
108 #ifdef _DIRENT_HAVE_D_TYPE
109                 d_type = filedir_entry->d_type;
110 #else
111
112 #ifndef DT_UNKNOWN
113 #define DT_UNKNOWN     0
114 #define DT_DIR         4
115 #define DT_REG         8
116 #define DT_LNK         10
117
118 #define IFTODT(mode)   (((mode) & 0170000) >> 12)
119 #define DTTOIF(dirtype)        ((dirtype) << 12)
120 #endif
121                 d_type = DT_UNKNOWN;
122 #endif
123                 if ((d_namelen == 1) && 
124                     (filedir_entry->d_name[0] == '.'))
125                         continue;
126
127                 if ((d_namelen == 2) && 
128                     (filedir_entry->d_name[0] == '.') &&
129                     (filedir_entry->d_name[1] == '.'))
130                         continue;
131
132                 snprintf(path, PATH_MAX, "%s/%s", ctdl_usrpic_dir, filedir_entry->d_name);
133                 if (d_type == DT_UNKNOWN) {
134                         if (lstat(path, &s) == 0) {
135                                 d_type = IFTODT(s.st_mode);
136                         }
137                 }
138                 switch (d_type)
139                 {
140                 case DT_DIR:
141                         break;
142                 case DT_LNK:
143                 case DT_REG:
144                         usernum = atol(filedir_entry->d_name);
145                         if (CtdlGetUserByNumber(&usbuf, usernum) == 0) {
146                                 import_one_userpic_file(usbuf.fullname, usernum, path);
147                         }
148                 }
149         }
150         free(d);
151         closedir(filedir);
152         rmdir(ctdl_usrpic_dir);
153 }
154
155
156
157 CTDL_MODULE_INIT(image)
158 {
159         if (!threading)
160         {
161                 import_old_userpic_files();
162         }
163         /* return our module name for the log */
164         return "image";
165 }