PageRenderTime 28ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/Branch_4_6dev/gforge/cronjobs/mail/mailaliases.php

https://gitlab.com/oslc-cm-server/olbergers-ff5-oslc
PHP | 207 lines | 100 code | 38 blank | 69 comment | 21 complexity | a8bda48c424ed5be0cdf2c8e192d626b MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception
  1. #! /usr/bin/php4 -f
  2. <?php
  3. /**
  4. * GForge Mail Aliases Facility
  5. *
  6. * Copyright 2002-2004 GForge, LLC
  7. * http://gforge.org/
  8. *
  9. * @version $Id$
  10. *
  11. * This file is part of GForge.
  12. *
  13. * GForge is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * GForge 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.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with GForge; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. */
  27. require ('squal_pre.php');
  28. require ('common/include/cron_utils.php');
  29. define('FILE_EXTENSION','.new'); // use .new when testing
  30. /*
  31. NOTE - THIS SQL CAN BE USED ON A SECOND SERVER TO GRANT ONLY THE NEEDED PERMS
  32. CREATE USER listsuser WITH ENCRYPTED PASSWORD 'password';
  33. GRANT SELECT ON mail_group_list, users, deleted_mailing_lists, forum_group_list, groups, artifact_group_list TO listsuser;
  34. GRANT INSERT, UPDATE ON deleted_mailing_lists TO listsuser;
  35. GRANT UPDATE ON mail_group_list TO listsuser;
  36. GRANT ALL ON project_sums_agg TO listsuser;
  37. */
  38. // This works only if this file is in cronjobs/mail/
  39. $path_to_cronjobs = dirname(dirname(__FILE__));
  40. // You should also modify this to the correct PHP path and extra configuration (if needed)
  41. $php_command = "/usr/bin/php -d include_path=".ini_get("include_path");
  42. $aliases_orig = file("/etc/aliases");
  43. $aliases = array();
  44. for ($i=0; $i < count($aliases_orig); $i++) {
  45. $line = trim($aliases_orig[$i]);
  46. // Skip the GForge aliases (will be written later)
  47. if (preg_match("/^[[:blank:]]*#GFORGEBEGIN/", $line)) {
  48. do {
  49. $i++;
  50. $line = trim($aliases_orig[$i]);
  51. } while ($i < count($aliases_orig) && !preg_match("/^[[:blank:]]*#GFORGEEND/", $line));
  52. // Got to end of file (shouldn't happen, means #GFORGEEND wasn't found on file
  53. if ($i >= (count($aliases_orig)-1)) break;
  54. // read next line
  55. $i++;
  56. $line = trim($aliases_orig[$i]);
  57. }
  58. // empty line or comment
  59. if (empty($line) || preg_match('/^#/', $line)) continue;
  60. list($alias_name, $alias) = explode(':', $line, 2);
  61. $alias_name = trim($alias_name);
  62. $alias = trim($alias);
  63. $aliases[$alias_name] = $alias;
  64. }
  65. // Now generate the GForge aliases
  66. $gforge_aliases = array();
  67. //
  68. // Set up the forum aliases
  69. //
  70. if ($sys_use_forum) {
  71. $resforum=db_query("SELECT groups.unix_group_name,lower(fgl.forum_name) AS forum_name
  72. FROM forum_group_list fgl,groups
  73. WHERE fgl.group_id=groups.group_id
  74. AND groups.status='A'");
  75. for ($forres=0; $forres<db_numrows($resforum); $forres++) {
  76. $forname=strtolower(db_result($resforum,$forres,'unix_group_name').'-'.strtolower(db_result($resforum,$forres,'forum_name')));
  77. if (array_key_exists($forname, $aliases)) {
  78. // A GForge alias was found outside the markers
  79. unset($aliases[$forname]);
  80. }
  81. $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')).'"';
  82. }
  83. }
  84. //
  85. // Set up the tracker aliases
  86. //
  87. if ($sys_use_tracker) {
  88. $restracker=db_query("SELECT groups.unix_group_name,lower(agl.name) AS tracker_name,group_artifact_id
  89. FROM artifact_group_list agl, groups
  90. WHERE agl.group_id=groups.group_id
  91. AND groups.status='A'");
  92. for ($forres=0; $forres<db_numrows($restracker); $forres++) {
  93. // first we remove non-alphanumeric characters (spaces and other stuff)
  94. $formatted_tracker_name = preg_replace('/[^[:alnum:]]/','',db_result($restracker,$forres,'tracker_name'));
  95. $formatted_tracker_name = strtolower($formatted_tracker_name);
  96. $trackername=strtolower(db_result($restracker,$forres,'unix_group_name'))."-".$formatted_tracker_name;
  97. // enclose tracker name with quotes if it has whitespaces
  98. if (strpos($trackername, ' ') !== false) {
  99. $trackername = '"'.$trackername.'"';
  100. }
  101. if (array_key_exists($trackername, $aliases)) {
  102. // A GForge alias was found outside the markers
  103. unset($aliases[$trackername]);
  104. }
  105. $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')).'"';
  106. }
  107. }
  108. if ($sys_use_mail && file_exists("/tmp/mailman-aliases")) {
  109. //
  110. // Read in the mailman aliases
  111. //
  112. $h2 = fopen("/tmp/mailman-aliases","r");
  113. $mailmancontents = fread($h2,filesize("/tmp/mailman-aliases"));
  114. $mailmanlines = explode("\n",$mailmancontents);
  115. for ($k = 0; $k < count($mailmanlines); $k++) {
  116. $mailmanline = explode(":",$mailmanlines[$k], 2);
  117. $alias = trim($mailmanline[0]);
  118. $command = trim($mailmanline[1]);
  119. if (empty($alias)) continue;
  120. if (array_key_exists($alias, $aliases)) {
  121. // A GForge alias was found outside the markers
  122. unset($aliases[$alias]);
  123. }
  124. $gforge_aliases[$alias] = $command;
  125. }
  126. $err .= "\n$k Mailman Lines";
  127. fclose($h2);
  128. }
  129. //
  130. // Write out the user aliases
  131. //
  132. $res=db_query("SELECT user_name,email FROM users WHERE status = 'A' AND email != ''");
  133. $err .= db_error();
  134. $rows=db_numrows($res);
  135. for ($i=0; $i<$rows; $i++) {
  136. $user = db_result($res,$i,0);
  137. $email = db_result($res,$i,1);
  138. if (array_key_exists($user, $aliases)) {
  139. // A GForge alias was found outside the markers
  140. unset($aliases[$user]);
  141. }
  142. $gforge_aliases[$user] = $email;
  143. }
  144. //
  145. // Now write all the aliases
  146. //
  147. $fh = fopen("/etc/aliases".FILE_EXTENSION, "w");
  148. foreach ($aliases as $aliasname => $alias) {
  149. fwrite($fh, "$aliasname: \t\t $alias\n");
  150. }
  151. fputs($fh, "#GFORGEBEGIN\n");
  152. foreach ($gforge_aliases as $aliasname => $alias) {
  153. fwrite($fh, "$aliasname: \t\t $alias\n");
  154. }
  155. fputs($fh, "#GFORGEEND\n");
  156. fclose($fh);
  157. db_free_result($res);
  158. $ok = `newaliases`;
  159. $err .= $ok;
  160. cron_entry(17,$err);
  161. ?>