5 * Mailing List Creation Cronjob
7 * Copyright 2000-2010, Fusionforge Team
8 * Copyright 2011, Franck Villaume - Capgemini
9 * Copyright 2012, Franck Villaume - TrivialDev
10 * http://fusionforge.org
12 * This file is part of FusionForge. FusionForge is free software;
13 * you can redistribute it and/or modify it under the terms of the
14 * GNU General Public License as published by the Free Software
15 * Foundation; either version 2 of the Licence, or (at your option)
18 * FusionForge is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 // This script will read in a list existing mailing lists, then add the new lists
30 // and, finally, create the lists in a /var/lib/gforge/dumps/mailman-aliases file
31 // The /var/lib/gforge/dumps/mailman-aliases file will then be read by the mailaliases.php file
34 require dirname(__FILE__).'/../../www/env.inc.php';
35 require_once $gfcommon.'include/pre.php';
36 require $gfcommon.'include/cron_utils.php';
40 if (is_dir(forge_get_config('mailman_path'))) {
41 $path_to_mailman = forge_get_config('mailman_path');
42 } elseif (is_dir("/usr/lib/mailman")) {
43 $path_to_mailman = "/usr/lib/mailman";
45 $err .= "\npath_to_mailman path is not set right for this script!!";
50 $res = db_query_params('SELECT users.user_name,email,mail_group_list.list_name,
51 mail_group_list.password,mail_group_list.status,
52 mail_group_list.group_list_id,mail_group_list.is_public,
53 mail_group_list.description
54 FROM mail_group_list,users
55 WHERE mail_group_list.list_admin=users.user_id',
59 $rows = db_numrows($res);
60 //$err .= "$rows rows returned from query\n";
62 if (!is_dir(forge_get_config('data_path').'/dumps')) {
63 mkdir(forge_get_config('data_path').'/dumps', 0755, true);
65 $h1 = fopen(forge_get_config('data_path').'/dumps/mailman-aliases', "w");
67 $mailingListIds = array();
68 for ($i=0; $i<$rows; $i++) {
69 $listadmin = db_result($res,$i,'user_name');
70 $email = db_result($res,$i,'email');
71 $listname = strtolower(db_result($res,$i,'list_name'));
72 $listpassword = db_result($res,$i,'password');
73 $grouplistid = db_result($res,$i,'group_list_id');
74 $public = db_result($res,$i,'is_public');
75 $status = db_result($res,$i,'status');
76 $description = db_result($res, $i, 'description');
78 $listname = trim($listname);
80 $err .= "Empty name for a mailing list in 'mail_group_list' table\n";
83 if (!preg_match('/^[a-z0-9\-_\.]*$/', $listname) || $listname == '.' || $listname == '..') {
84 $err .= 'Invalid List Name: ' . $listname;
88 if ($status == MAIL__MAILING_LIST_IS_REQUESTED) { // New list?
89 $err .= "Creating Mailing List: $listname\n";
90 //$lcreate_cmd = $path_to_mailman."/bin/newlist -q $listname@".forge_get_config('lists_host')." $email $listpassword &> /dev/null";
91 $lcreate_cmd = $path_to_mailman."/bin/newlist -q $listname $email $listpassword";
92 $err .= "Command to be executed is $lcreate_cmd\n";
93 passthru($lcreate_cmd, $failed);
95 $err .= 'Failed to create '.$listname.", skipping\n";
98 db_query_params('UPDATE mail_group_list set status=$1 WHERE status=$2 and group_list_id=$3',
99 array(MAIL__MAILING_LIST_IS_CREATED,
100 MAIL__MAILING_LIST_IS_REQUESTED,
103 $tmp = tempnam(forge_get_config('data_path'), "tmp");
104 $fh = fopen($tmp,'w');
105 $listConfig = "description = \"$description\"\n" ;
106 $listConfig .= "host_name = '".forge_get_config('lists_host')."'\n" ;
108 $listConfig .= "archive_private = True\n" ;
109 $listConfig .= "advertised = False\n" ;
110 $listConfig .= "subscribe_policy = 3\n" ;
111 ## Reject mails sent by non-members
112 $listConfig .= "generic_nonmember_action = 2\n";
113 ## Do not forward auto discard message
114 $listConfig .= "forward_auto_discards = 0\n";
116 $listConfig .= "archive_private = False\n" ;
117 $listConfig .= "advertised = True\n" ;
118 $listConfig .= "subscribe_policy = 1\n" ;
120 fwrite($fh, $listConfig);
122 $config_cmd = escapeshellcmd($path_to_mailman."/bin/config_list -i $tmp $listname");
123 passthru($config_cmd, $failed);
126 $err .= 'Failed to configure '.$listname.", skipping\n";
129 $fixurl_cmd = escapeshellcmd($path_to_mailman."/bin/withlist -l -r fix_url $listname -u ".forge_get_config('lists_host'));
130 passthru($fixurl_cmd, $failed);
132 db_query_params('UPDATE mail_group_list set status=$1 WHERE status=$2 and group_list_id=$3',
133 array(MAIL__MAILING_LIST_IS_CONFIGURED,
134 MAIL__MAILING_LIST_IS_CREATED,
138 $err .= 'Failed to configure '.$listname."\n";
142 $mailingListIds[] = $grouplistid;
143 } elseif ($status == MAIL__MAILING_LIST_IS_CREATED) {
144 $tmp = tempnam(forge_get_config('data_path'), "tmp");
145 $fh = fopen($tmp,'w');
146 $listConfig = "description = \"$description\"\n" ;
147 $listConfig .= "host_name = '".forge_get_config('lists_host')."'\n";
149 $listConfig .= "archive_private = True\n";
150 $listConfig .= "advertised = False\n";
151 $listConfig .= "subscribe_policy = 3\n";
152 ## Reject mails sent by non-members
153 $listConfig .= "generic_nonmember_action = 2\n";
154 ## Do not forward auto discard message
155 $listConfig .= "forward_auto_discards = 0\n";
157 $listConfig .= "archive_private = False\n";
158 $listConfig .= "advertised = True\n";
159 $listConfig .= "subscribe_policy = 1\n";
161 fwrite($fh, $listConfig);
163 $config_cmd = escapeshellcmd($path_to_mailman."/bin/config_list -i $tmp $listname");
164 passthru($config_cmd, $failed);
167 db_query_params('UPDATE mail_group_list set status=$1 WHERE status=$2 and group_list_id=$3',
168 array(MAIL__MAILING_LIST_IS_CONFIGURED,
169 MAIL__MAILING_LIST_IS_CREATED,
173 $err .= 'Failed to configure '.$listname."\n";
176 } elseif ($status == MAIL__MAILING_LIST_IS_UPDATED) {
177 $tmp = tempnam(forge_get_config('data_path'), "tmp");
178 $tmp = tempnam(forge_get_config('data_path'), "tmp");
179 $fh = fopen($tmp,'w');
180 $listConfig = "description = \"$description\"\n" ;
181 $listConfig .= "host_name = '".forge_get_config('lists_host')."'\n" ;
183 $listConfig .= "archive_private = True\n" ;
184 $listConfig .= "advertised = False\n" ;
185 $listConfig .= "subscribe_policy = 3\n" ;
186 ## Reject mails sent by non-members
187 $listConfig .= "generic_nonmember_action = 2\n";
188 ## Do not forward auto discard message
189 $listConfig .= "forward_auto_discards = 0\n";
191 $listConfig .= "archive_private = False\n" ;
192 $listConfig .= "advertised = True\n" ;
193 $listConfig .= "subscribe_policy = 1\n" ;
195 fwrite($fh, $listConfig);
197 $config_cmd = escapeshellcmd($path_to_mailman."/bin/config_list -i $tmp $listname");
198 passthru($config_cmd, $failed);
201 db_query_params('UPDATE mail_group_list set status=$1 WHERE status=$2 and group_list_id=$3',
202 array(MAIL__MAILING_LIST_IS_CONFIGURED,
203 MAIL__MAILING_LIST_IS_UPDATED,
207 $err .= 'Failed to configure '.$listname."\n";
210 } elseif ($status == MAIL__MAILING_LIST_PW_RESET_REQUESTED) {
211 $change_pw_cmd = escapeshellcmd($path_to_mailman.'/bin/change_pw -l '.$listname);
212 $err .= "Resetting password of ".$listname."\n";
213 exec($change_pw_cmd, $returnnewpasswd, $failed);
215 $err .= 'Failed to reset password of '.$listname."\n";
217 $arrayReturnNewPasswd = explode(' ', $returnnewpasswd[0]);
218 $newpasswd = trim(end($arrayReturnNewPasswd));
219 db_query_params('UPDATE mail_group_list set (status, password) = ($1, $2) WHERE status=$3 and group_list_id=$4',
220 array(MAIL__MAILING_LIST_IS_CONFIGURED,
222 MAIL__MAILING_LIST_PW_RESET_REQUESTED,
228 // Privatize only if it is marked as private
229 $err .= "Privatizing ".$listname."\n";
230 $tmp = tempnam(forge_get_config('data_path'), "tmp");
231 $fh = fopen($tmp,'w');
232 $listConfig = "description = \"$description\"\n" ;
233 $listConfig .= "host_name = '".forge_get_config('lists_host')."'\n" ;
235 $listConfig .= "archive_private = True\n" ;
236 $listConfig .= "advertised = False\n" ;
237 $listConfig .= "subscribe_policy = 3\n" ;
238 ## Reject mails sent by non-members
239 $listConfig .= "generic_nonmember_action = 2\n";
240 ## Do not forward auto discard message
241 $listConfig .= "forward_auto_discards = 0\n";
243 $listConfig .= "archive_private = False\n" ;
244 $listConfig .= "advertised = True\n" ;
245 $listConfig .= "subscribe_policy = 1\n" ;
247 fwrite($fh, $listConfig);
249 $privatize_cmd = escapeshellcmd($path_to_mailman."/bin/config_list -i $tmp $listname");
250 passthru($privatize_cmd, $privatizeFailed);
251 if ($privatizeFailed) {
252 $err .= 'Failed to privatize '.$listname."\n";
258 if(file_exists($path_to_mailman.'/mail/mailman')) {
261 $listname.': "|'.$path_to_mailman.'/mail/mailman post '.$listname.'"'."\n"
262 .$listname.'-admin: "|'.$path_to_mailman.'/mail/mailman admin '.$listname.'"'."\n"
263 .$listname.'-bounces: "|'.$path_to_mailman.'/mail/mailman bounces '.$listname.'"'."\n"
264 .$listname.'-confirm: "|'.$path_to_mailman.'/mail/mailman confirm '.$listname.'"'."\n"
265 .$listname.'-join: "|'.$path_to_mailman.'/mail/mailman join '.$listname.'"'."\n"
266 .$listname.'-leave: "|'.$path_to_mailman.'/mail/mailman leave '.$listname.'"'."\n"
267 .$listname.'-owner: "|'.$path_to_mailman.'/mail/mailman owner '.$listname.'"'."\n"
268 .$listname.'-request: "|'.$path_to_mailman.'/mail/mailman request '.$listname.'"'."\n"
269 .$listname.'-subscribe: "|'.$path_to_mailman.'/mail/mailman subscribe '.$listname.'"'."\n"
270 .$listname.'-unsubscribe: "|'.$path_to_mailman.'/mail/mailman unsubscribe '.$listname.'"'."\n\n"
275 $listname.': "|'.$path_to_mailman.'/mail/wrapper post '.$listname.'"'."\n"
276 .$listname.'-admin: "|'.$path_to_mailman.'/mail/wrapper mailowner '.$listname.'"'."\n"
277 .$listname.'-request: "|'.$path_to_mailman.'/mail/wrapper mailcmd '.$listname.'"'."\n"
278 .$listname.'-owner: '.$listname.'-admin'."\n\n";
281 fwrite($h1, $list_str);
286 //delete mailing lists
288 $res = db_query_params('SELECT mailing_list_name FROM deleted_mailing_lists WHERE isdeleted = 0',
291 $rows = db_numrows($res);
293 for($k = 0; $k < $rows; $k++) {
294 $deleted_mail_list = db_result($res, $k, 'mailing_list_name');
296 $deleted_mail_list = trim($deleted_mail_list);
297 if (!$deleted_mail_list) {
298 $err .= "Empty name for a mailing list in 'deleted_mailing_lists' table\n";
301 if (!preg_match('/^[a-z0-9\-_\.]*$/', $deleted_mail_list) || $deleted_mail_list == '.' || $deleted_mail_list == '..') {
302 $err .= 'Invalid List Name: ' . $deleted_mail_list;
306 $rm_cmd = forge_get_config('mailman_path')."/bin/rmlist -a $deleted_mail_list";
307 $err .= "Command to be executed is $rm_cmd";
308 passthru($rm_cmd, $failed);
310 $err .= 'Failed to remove '.$listname.", skipping\n";
315 if (!file_exists(forge_get_config('mailman_data_path')."/lists/$deleted_mail_list")) {
320 $res1 = db_query_params('UPDATE deleted_mailing_lists SET isdeleted = 1 WHERE mailing_list_name = $1',
321 array($deleted_mail_list));
324 $err .= "Could not remove the list $deleted_mail_list \n";
328 cron_entry(18, $err);