3 * Copyright (c) Xerox Corporation, Codendi Team, 2001-2009. All rights reserved
5 * This file is a part of Codendi.
7 * Codendi is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * Codendi is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Codendi. If not, see <http://www.gnu.org/licenses/>.
20 * Portions Copyright 2010 (c) Mélanie Le Bail
23 require_once 'common/event/Event.class.php';
24 require_once 'mailman/include/MailmanListDao.class.php';
25 require_once 'common/dao/CodendiDataAccess.class.php';
26 require_once 'mailman/include/MailmanList.class.php';
27 require_once 'plugins_utils.php';
28 require_once 'plugins/mailman/config.php';
30 class BackendMailmanList {
32 protected $_mailinglistdao = null;
35 * Hold an instance of the class
37 protected static $_instance;
40 * Backends are singletons
42 public static function instance() {
44 if (!isset(self::$_instance)) {
46 self::$_instance = new $c;
48 return self::$_instance;
52 * @return MailingListDao
54 protected function _getMailingListDao() {
55 if (!$this->_mailinglistdao) {
56 $this->_mailinglistdao = new MailmanListDao(CodendiDataAccess::instance());
58 return $this->_mailinglistdao;
63 * Update mailman configuration for the given list
64 * Write configuration in temporary file, and load it with mailman config_list tool
65 * @return true on success, false otherwise
67 protected function updateListConfig($list) {
68 // write configuration in temporary file
69 $config_file=$GLOBALS['tmp_dir']."/mailman_config_".$list->getID().".in";
71 if ($fp = fopen($config_file, 'w')) {
72 // Define encoding of this file for Python. See SR #764
73 // Please note that this allows config_list to run with UTF-8 strings, but if the
74 // description contains non-ascii chars, they will be displayed badly in mailman config web page.
75 fwrite($fp, "# coding=UTF-8\n\n");
76 // Deactivate monthly reminders by default
77 fwrite($fp, "send_reminders = 0\n");
78 // Setup the description
79 fwrite($fp, "description = '".addslashes($list->getDescription())."'\n");
80 // Allow up to 200 kB messages
81 fwrite($fp, "max_message_size = 200\n");
83 if ($list->isPublic() == 0) { // Private lists
84 // Don't advertise this list when people ask what lists are on this machine
85 fwrite($fp, "advertised = False\n");
87 fwrite($fp, "archive_private = 1\n");
88 // Subscribe requires approval
89 fwrite($fp, "subscribe_policy = 2\n");
93 if (system($GLOBALS['mailman_bin_dir']."/config_list -i $config_file ".$list->getName()) !== false) {
94 if (unlink($config_file)) {
104 * Create new mailing list with mailman 'newlist' tool
105 * then update the list configuration according to list settings
106 * @return true on success, false otherwise
108 public function createList($group_list_id) {
110 $dar = $this->_getMailingListDao()->searchByGroupListId($group_list_id);
112 if ($row = $dar->getRow()) {
113 $list = new MailmanList($row['group_id'],$row['group_list_id']);
114 $user=UserManager::instance()->getUserByID($list->getListAdminId());
115 $list_admin_email= $user->getEmail();
116 $list_dir = $GLOBALS['mailman_lib_dir']."/lists/".$list->getName();
118 if($list->isPublic() != 9) {
119 if ((! is_dir($list_dir))) {
121 system($GLOBALS['mailman_bin_dir']."/newlist -q ".$list->getName()." ".$list_admin_email." ".$list->getPassword()." >/dev/null");
122 // Then update configuraion
123 if( is_dir($list_dir) && $this->updateListConfig($list) !=false ) {
124 $result = $this->_getMailingListDao() -> updateList($list->getID(),$row['group_id'], $list->getDescription(), $list->isPublic(),'3');
126 printf('Unable to update the list status: '.db_error());
138 $result = $this->_getMailingListDao() -> updateList($list->getID(),$row['group_id'], $list->getDescription(), $list->isPublic(),'3');
140 printf('Unable to update the list status: '.db_error());
153 * Delete mailing list
154 * - list and archives are deleted
155 * - backup first in temp directory
156 * @return true on success, false otherwise
158 public function deleteList($group_list_id) {
159 $dar = $this->_getMailingListDao()->searchByGroupListId($group_list_id);
161 if ($row = $dar->getRow()) {
162 $list=new MailmanList($row['group_id'],$group_list_id);
163 $list_dir = $GLOBALS['mailman_lib_dir']."/lists/".$list->getName();
164 if ((is_dir($list_dir))&&($list->isPublic() == 9)) {
167 $list_archive_dir = $GLOBALS['mailman_lib_dir']."/archives/private/".$list->getName(); // Does it work? TODO
168 $backupfile=$GLOBALS['mailman_lib_dir']."/archives/".$list->getName()."-mailman.tgz";
169 system("tar cfz $backupfile $list_dir $list_archive_dir");
170 chmod($backupfile,0600);
172 // Delete the mailing list if asked to and the mailing exists (archive deleted as well)
173 system($GLOBALS['mailman_bin_dir']. '/rmlist -a '. $list->getName() .' >/dev/null');
182 * Check if the list exists on the file system
183 * @return true if list exists, false otherwise
185 public function listExists($list) {
186 // Is this the best test?
187 $list_dir = $GLOBALS['mailman_lib_dir']."/lists/".$list->getName();
188 if (! is_dir($list_dir)) return false;