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)) {
75 $line = trim($aliases_orig[$i]);
78 // empty line or comment
79 if (empty($line) || preg_match('/^#/', $line)) {
82 list($alias_name, $alias) = explode(':', $line, 2);
83 $alias_name = trim($alias_name);
84 $alias = trim($alias);
85 $aliases[$alias_name] = $alias;
88 // Now generate the GForge aliases
89 $gforge_aliases = array();
92 // Set up the forum aliases
94 if (forge_get_config('use_forum')) {
95 $resforum = db_query_params ('SELECT groups.unix_group_name,lower(fgl.forum_name) AS forum_name
96 FROM forum_group_list fgl,groups
97 WHERE fgl.group_id=groups.group_id
98 AND groups.status=$1',
100 for ($forres=0; $forres<db_numrows($resforum); $forres++) {
101 $forname=strtolower(db_result($resforum,$forres,'unix_group_name').'-'.strtolower(db_result($resforum,$forres,'forum_name')));
103 if (array_key_exists($forname, $aliases)) {
104 // A GForge alias was found outside the markers
105 unset($aliases[$forname]);
108 $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')).'"';
114 // Set up the tracker aliases
116 if (forge_get_config('use_tracker')) {
117 $restracker = db_query_params ('SELECT groups.unix_group_name,lower(agl.name) AS tracker_name,group_artifact_id
118 FROM artifact_group_list agl, groups
119 WHERE agl.group_id=groups.group_id
120 AND groups.status=$1',
122 for ($forres=0; $forres<db_numrows($restracker); $forres++) {
123 // first we remove non-alphanumeric characters (spaces and other stuff)
124 $formatted_tracker_name = preg_replace('/[^[:alnum:]]/','',db_result($restracker,$forres,'tracker_name'));
125 $formatted_tracker_name = strtolower($formatted_tracker_name);
127 $trackername=strtolower(db_result($restracker,$forres,'unix_group_name'))."-".$formatted_tracker_name;
128 // enclose tracker name with quotes if it has whitespaces
129 if (strpos($trackername, ' ') !== false) {
130 $trackername = '"'.$trackername.'"';
133 if (array_key_exists($trackername, $aliases)) {
134 // A GForge alias was found outside the markers
135 unset($aliases[$trackername]);
138 $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')).'"';
142 if (forge_get_config('use_mail') && file_exists(forge_get_config('data_path').'/dumps/mailman-aliases')
143 && filesize(forge_get_config('data_path').'/dumps/mailman-aliases') > 0) {
145 // Read in the mailman aliases
147 $h2 = fopen(forge_get_config('data_path').'/dumps/mailman-aliases',"r");
148 $mailmancontents = fread($h2,filesize(forge_get_config('data_path').'/dumps/mailman-aliases'));
149 $mailmanlines = explode("\n",$mailmancontents);
150 for ($k = 0; $k < count($mailmanlines); $k++) {
151 $mailmanline = explode(":",$mailmanlines[$k], 2);
153 $alias = trim($mailmanline[0]);
157 $command = trim($mailmanline[1]);
159 if (array_key_exists($alias, $aliases)) {
160 // A GForge alias was found outside the markers
161 unset($aliases[$alias]);
164 $gforge_aliases[$alias] = $command;
166 $err .= "$k Mailman Lines\n";
171 // Write out the user aliases
173 $res = db_query_params ('SELECT user_name,email FROM users WHERE status = $1 AND email != $2',
178 $rows=db_numrows($res);
181 for ($i=0; $i<$rows; $i++) {
182 $user = db_result($res,$i,0);
183 if (preg_match('/@/', $user)) {
186 $email = db_result($res,$i,1);
188 if (array_key_exists($user, $aliases)) {
189 // A GForge alias was found outside the markers
190 unset($aliases[$user]);
193 $gforge_aliases[$user] = $email;
198 // Now write all the aliases
200 $fh = fopen("/etc/aliases".FILE_EXTENSION, "w");
201 foreach ($aliases as $aliasname => $alias) {
202 fwrite($fh, "$aliasname: \t\t $alias\n");
204 fputs($fh, "#GFORGEBEGIN\n");
205 foreach ($gforge_aliases as $aliasname => $alias) {
206 fwrite($fh, "$aliasname: \t\t $alias\n");
208 fputs($fh, "#GFORGEEND\n");
212 db_free_result($res);