4 * GForge Mail Aliases Facility
6 * Copyright 2002-2004 GForge, LLC
7 * http://fusionforge.org/
9 * This file is part of FusionForge. FusionForge is free software;
10 * you can redistribute it and/or modify it under the terms of the
11 * GNU General Public License as published by the Free Software
12 * Foundation; either version 2 of the Licence, or (at your option)
15 * FusionForge is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 require dirname(__FILE__).'/../../common/include/env.inc.php';
26 require_once $gfcommon.'include/pre.php';
27 require $gfcommon.'include/cron_utils.php';
29 //define('FILE_EXTENSION','.new'); // use .new when testing
30 define('FILE_EXTENSION',''); // use .new when testing
34 NOTE - THIS SQL CAN BE USED ON A SECOND SERVER TO GRANT ONLY THE NEEDED PERMS
37 CREATE USER listsuser WITH ENCRYPTED PASSWORD 'password';
39 GRANT SELECT ON mail_group_list, users, deleted_mailing_lists, forum_group_list, groups, artifact_group_list TO listsuser;
41 GRANT INSERT, UPDATE ON deleted_mailing_lists TO listsuser;
43 GRANT UPDATE ON mail_group_list TO listsuser;
45 GRANT ALL ON project_sums_agg TO listsuser;
49 // This works only if this file is in cronjobs/mail/
50 $path_to_cronjobs = dirname(dirname(__FILE__));
52 // You should also modify this to the correct PHP path and extra configuration (if needed)
53 $php_command = '/usr/bin/php';
56 $aliases_orig = file("/etc/aliases");
60 for ($i=0; $i < count($aliases_orig); $i++) {
61 $line = trim($aliases_orig[$i]);
62 // Skip the GForge aliases (will be written later)
63 if (preg_match("/^[[:blank:]]*#GFORGEBEGIN/", $line)) {
66 $line = trim($aliases_orig[$i]);
67 } while ($i < count($aliases_orig) && !preg_match("/^[[:blank:]]*#GFORGEEND/", $line));
69 // Got to end of file (shouldn't happen, means #GFORGEEND wasn't found on file
70 if ($i >= (count($aliases_orig)-1)) break;
74 $line = trim($aliases_orig[$i]);
77 // empty line or comment
78 if (empty($line) || preg_match('/^#/', $line)) continue;
80 list($alias_name, $alias) = explode(':', $line, 2);
81 $alias_name = trim($alias_name);
82 $alias = trim($alias);
83 $aliases[$alias_name] = $alias;
86 // Now generate the GForge aliases
87 $gforge_aliases = array();
90 // Set up the forum aliases
92 if (forge_get_config('use_forum')) {
93 $resforum = db_query_params ('SELECT groups.unix_group_name,lower(fgl.forum_name) AS forum_name
94 FROM forum_group_list fgl,groups
95 WHERE fgl.group_id=groups.group_id
96 AND groups.status=$1',
98 for ($forres=0; $forres<db_numrows($resforum); $forres++) {
99 $forname=strtolower(db_result($resforum,$forres,'unix_group_name').'-'.strtolower(db_result($resforum,$forres,'forum_name')));
101 if (array_key_exists($forname, $aliases)) {
102 // A GForge alias was found outside the markers
103 unset($aliases[$forname]);
106 $gforge_aliases[$forname] = '"|'.$php_command." ".$path_to_cronjobs."/forum_gateway.php ".db_result($resforum,$forres,'unix_group_name')." ".strtolower(db_result($resforum,$forres,'forum_name')).'"';
112 // Set up the tracker aliases
114 if (forge_get_config('use_tracker')) {
115 $restracker = db_query_params ('SELECT groups.unix_group_name,lower(agl.name) AS tracker_name,group_artifact_id
116 FROM artifact_group_list agl, groups
117 WHERE agl.group_id=groups.group_id
118 AND groups.status=$1',
120 for ($forres=0; $forres<db_numrows($restracker); $forres++) {
121 // first we remove non-alphanumeric characters (spaces and other stuff)
122 $formatted_tracker_name = preg_replace('/[^[:alnum:]]/','',db_result($restracker,$forres,'tracker_name'));
123 $formatted_tracker_name = strtolower($formatted_tracker_name);
125 $trackername=strtolower(db_result($restracker,$forres,'unix_group_name'))."-".$formatted_tracker_name;
126 // enclose tracker name with quotes if it has whitespaces
127 if (strpos($trackername, ' ') !== false) {
128 $trackername = '"'.$trackername.'"';
131 if (array_key_exists($trackername, $aliases)) {
132 // A GForge alias was found outside the markers
133 unset($aliases[$trackername]);
136 $gforge_aliases[$trackername] = '"|'.$php_command." ".$path_to_cronjobs."/tracker_gateway.php ".db_result($restracker,$forres,'unix_group_name')." ".strtolower(db_result($restracker,$forres,'group_artifact_id')).'"';
140 if (forge_get_config('use_mail') && file_exists(forge_get_config('data_path').'/dumps/mailman-aliases')
141 && filesize(forge_get_config('data_path').'/dumps/mailman-aliases') > 0) {
143 // Read in the mailman aliases
145 $h2 = fopen(forge_get_config('data_path').'/dumps/mailman-aliases',"r");
146 $mailmancontents = fread($h2,filesize(forge_get_config('data_path').'/dumps/mailman-aliases'));
147 $mailmanlines = explode("\n",$mailmancontents);
148 for ($k = 0; $k < count($mailmanlines); $k++) {
149 $mailmanline = explode(":",$mailmanlines[$k], 2);
151 $alias = trim($mailmanline[0]);
152 if (empty($alias)) continue;
153 $command = trim($mailmanline[1]);
155 if (array_key_exists($alias, $aliases)) {
156 // A GForge alias was found outside the markers
157 unset($aliases[$alias]);
160 $gforge_aliases[$alias] = $command;
162 $err .= "$k Mailman Lines\n";
167 // Write out the user aliases
169 $res = db_query_params ('SELECT user_name,email FROM users WHERE status = $1 AND email != $2',
174 $rows=db_numrows($res);
177 for ($i=0; $i<$rows; $i++) {
178 $user = db_result($res,$i,0);
179 if (preg_match('/@/', $user)) {
182 $email = db_result($res,$i,1);
184 if (array_key_exists($user, $aliases)) {
185 // A GForge alias was found outside the markers
186 unset($aliases[$user]);
189 $gforge_aliases[$user] = $email;
194 // Now write all the aliases
196 $fh = fopen("/etc/aliases".FILE_EXTENSION, "w");
197 foreach ($aliases as $aliasname => $alias) {
198 fwrite($fh, "$aliasname: \t\t $alias\n");
200 fputs($fh, "#GFORGEBEGIN\n");
201 foreach ($gforge_aliases as $aliasname => $alias) {
202 fwrite($fh, "$aliasname: \t\t $alias\n");
204 fputs($fh, "#GFORGEEND\n");
208 db_free_result($res);