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__).'/../../www/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
33 NOTE - THIS SQL CAN BE USED ON A SECOND SERVER TO GRANT ONLY THE NEEDED PERMS
36 CREATE USER listsuser WITH ENCRYPTED PASSWORD 'password';
38 GRANT SELECT ON mail_group_list, users, deleted_mailing_lists, forum_group_list, groups, artifact_group_list TO listsuser;
40 GRANT INSERT, UPDATE ON deleted_mailing_lists TO listsuser;
42 GRANT UPDATE ON mail_group_list TO listsuser;
44 GRANT ALL ON project_sums_agg TO listsuser;
48 // This works only if this file is in cronjobs/mail/
49 $path_to_cronjobs = dirname(dirname(__FILE__));
51 // You should also modify this to the correct PHP path and extra configuration (if needed)
52 $php_command = "/usr/bin/php -d include_path=".ini_get("include_path");
55 $aliases_orig = file("/etc/aliases");
59 for ($i=0; $i < count($aliases_orig); $i++) {
60 $line = trim($aliases_orig[$i]);
61 // Skip the GForge aliases (will be written later)
62 if (preg_match("/^[[:blank:]]*#GFORGEBEGIN/", $line)) {
65 $line = trim($aliases_orig[$i]);
66 } while ($i < count($aliases_orig) && !preg_match("/^[[:blank:]]*#GFORGEEND/", $line));
68 // Got to end of file (shouldn't happen, means #GFORGEEND wasn't found on file
69 if ($i >= (count($aliases_orig)-1)) break;
73 $line = trim($aliases_orig[$i]);
76 // empty line or comment
77 if (empty($line) || preg_match('/^#/', $line)) continue;
79 list($alias_name, $alias) = explode(':', $line, 2);
80 $alias_name = trim($alias_name);
81 $alias = trim($alias);
82 $aliases[$alias_name] = $alias;
85 // Now generate the GForge aliases
86 $gforge_aliases = array();
89 // Set up the forum aliases
91 if (forge_get_config('use_forum')) {
92 $resforum = db_query_params ('SELECT groups.unix_group_name,lower(fgl.forum_name) AS forum_name
93 FROM forum_group_list fgl,groups
94 WHERE fgl.group_id=groups.group_id
95 AND groups.status=$1',
97 for ($forres=0; $forres<db_numrows($resforum); $forres++) {
98 $forname=strtolower(db_result($resforum,$forres,'unix_group_name').'-'.strtolower(db_result($resforum,$forres,'forum_name')));
100 if (array_key_exists($forname, $aliases)) {
101 // A GForge alias was found outside the markers
102 unset($aliases[$forname]);
105 $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')).'"';
111 // Set up the tracker aliases
113 if (forge_get_config('use_tracker')) {
114 $restracker = db_query_params ('SELECT groups.unix_group_name,lower(agl.name) AS tracker_name,group_artifact_id
115 FROM artifact_group_list agl, groups
116 WHERE agl.group_id=groups.group_id
117 AND groups.status=$1',
119 for ($forres=0; $forres<db_numrows($restracker); $forres++) {
120 // first we remove non-alphanumeric characters (spaces and other stuff)
121 $formatted_tracker_name = preg_replace('/[^[:alnum:]]/','',db_result($restracker,$forres,'tracker_name'));
122 $formatted_tracker_name = strtolower($formatted_tracker_name);
124 $trackername=strtolower(db_result($restracker,$forres,'unix_group_name'))."-".$formatted_tracker_name;
125 // enclose tracker name with quotes if it has whitespaces
126 if (strpos($trackername, ' ') !== false) {
127 $trackername = '"'.$trackername.'"';
130 if (array_key_exists($trackername, $aliases)) {
131 // A GForge alias was found outside the markers
132 unset($aliases[$trackername]);
135 $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')).'"';
139 if (forge_get_config('use_mail') && file_exists(forge_get_config('data_path').'/dumps/mailman-aliases')
140 && filesize(forge_get_config('data_path').'/dumps/mailman-aliases') > 0) {
142 // Read in the mailman aliases
144 $h2 = fopen(forge_get_config('data_path').'/dumps/mailman-aliases',"r");
145 $mailmancontents = fread($h2,filesize(forge_get_config('data_path').'/dumps/mailman-aliases'));
146 $mailmanlines = explode("\n",$mailmancontents);
147 for ($k = 0; $k < count($mailmanlines); $k++) {
148 $mailmanline = explode(":",$mailmanlines[$k], 2);
150 $alias = trim($mailmanline[0]);
151 if (empty($alias)) continue;
152 $command = trim($mailmanline[1]);
154 if (array_key_exists($alias, $aliases)) {
155 // A GForge alias was found outside the markers
156 unset($aliases[$alias]);
159 $gforge_aliases[$alias] = $command;
161 $err .= "\n$k Mailman Lines";
166 // Write out the user aliases
168 $res = db_query_params ('SELECT user_name,email FROM users WHERE status = $1 AND email != $2',
173 $rows=db_numrows($res);
176 for ($i=0; $i<$rows; $i++) {
177 $user = db_result($res,$i,0);
178 if (preg_match('/@/', $user)) {
181 $email = db_result($res,$i,1);
183 if (array_key_exists($user, $aliases)) {
184 // A GForge alias was found outside the markers
185 unset($aliases[$user]);
188 $gforge_aliases[$user] = $email;
193 // Now write all the aliases
195 $fh = fopen("/etc/aliases".FILE_EXTENSION, "w");
196 foreach ($aliases as $aliasname => $alias) {
197 fwrite($fh, "$aliasname: \t\t $alias\n");
199 fputs($fh, "#GFORGEBEGIN\n");
200 foreach ($gforge_aliases as $aliasname => $alias) {
201 fwrite($fh, "$aliasname: \t\t $alias\n");
203 fputs($fh, "#GFORGEEND\n");
207 db_free_result($res);