4 * GForge Mail Aliases Facility
6 * Copyright 2002-2004 GForge, LLC
9 * This file is part of GForge.
11 * GForge is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * GForge is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with GForge; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 require dirname(__FILE__).'/../../www/env.inc.php';
27 require $gfwww.'include/squal_pre.php';
28 require $gfcommon.'include/cron_utils.php';
30 define('FILE_EXTENSION','.new'); // 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 -d include_path=".ini_get("include_path");
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($sys_var_path.'/dumps/mailman-aliases')) {
142 // Read in the mailman aliases
144 $h2 = fopen($sys_var_path.'/dumps/mailman-aliases',"r");
145 $mailmancontents = fread($h2,filesize($sys_var_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 $email = db_result($res,$i,1);
180 if (array_key_exists($user, $aliases)) {
181 // A GForge alias was found outside the markers
182 unset($aliases[$user]);
185 $gforge_aliases[$user] = $email;
190 // Now write all the aliases
192 $fh = fopen("/etc/aliases".FILE_EXTENSION, "w");
193 foreach ($aliases as $aliasname => $alias) {
194 fwrite($fh, "$aliasname: \t\t $alias\n");
196 fputs($fh, "#GFORGEBEGIN\n");
197 foreach ($gforge_aliases as $aliasname => $alias) {
198 fwrite($fh, "$aliasname: \t\t $alias\n");
200 fputs($fh, "#GFORGEEND\n");
204 db_free_result($res);