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';
29 class BackendMailmanList {
31 protected $_mailinglistdao = null;
34 * Hold an instance of the class
36 protected static $_instance;
39 * Backends are singletons
41 public static function instance() {
43 if (!isset(self::$_instance)) {
45 self::$_instance = new $c;
47 return self::$_instance;
51 * @return MailingListDao
53 protected function _getMailingListDao() {
54 if (!$this->_mailinglistdao) {
55 $this->_mailinglistdao = new MailmanListDao(CodendiDataAccess::instance());
57 return $this->_mailinglistdao;
62 * Update mailman configuration for the given list
63 * Write configuration in temporary file, and load it with mailman config_list tool
64 * @return true on success, false otherwise
66 protected function updateListConfig($list) {
67 // write configuration in temporary file
68 $config_file=$GLOBALS['tmp_dir']."/mailman_config_".$list->getID().".in";
70 if ($fp = fopen($config_file, 'w')) {
71 // Define encoding of this file for Python. See SR #764
72 // Please note that this allows config_list to run with UTF-8 strings, but if the
73 // description contains non-ascii chars, they will be displayed badly in mailman config web page.
74 fwrite($fp, "# coding=UTF-8\n\n");
75 // Deactivate monthly reminders by default
76 fwrite($fp, "send_reminders = 0\n");
77 // Setup the description
78 fwrite($fp, "description = '".addslashes($list->getDescription())."'\n");
79 // Allow up to 200 kB messages
80 fwrite($fp, "max_message_size = 200\n");
82 if ($list->isPublic() == 0) { // Private lists
83 // Don't advertise this list when people ask what lists are on this machine
84 fwrite($fp, "advertised = False\n");
86 fwrite($fp, "archive_private = 1\n");
87 // Subscribe requires approval
88 fwrite($fp, "subscribe_policy = 2\n");
92 if (system($GLOBALS['mailman_bin_dir']."/config_list -i $config_file ".$list->getName()) !== false) {
93 if (unlink($config_file)) {
103 * Create new mailing list with mailman 'newlist' tool
104 * then update the list configuration according to list settings
105 * @return true on success, false otherwise
107 public function createList($group_list_id) {
109 $dar = $this->_getMailingListDao()->searchByGroupListId($group_list_id);
111 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_list_dir']."/".$list->getName();
118 if ((! is_dir($list_dir))&&($list->isPublic() != 9)) {
120 echo "execution system";
121 system($GLOBALS['mailman_bin_dir']."/newlist -q ".$list->getName()." ".$list_admin_email." ".$list->getPassword()." >/dev/null");
122 echo "ok pour system";
123 //$list->update($list->getDescription(),$list->isPublic(),'1');
125 // Then update configuraion
126 return $this->updateListConfig($list);
133 * Delete mailing list
134 * - list and archives are deleted
135 * - backup first in temp directory
136 * @return true on success, false otherwise
138 public function deleteList($group_list_id) {
139 $dar = $this->_getMailingListDao()->searchByGroupListId($group_list_id);
141 if ($row = $dar->getRow()) {
142 $list=new MailmanList($row['group_id'],$group_list_id);
143 $list_dir = $GLOBALS['mailman_list_dir']."/".$list->getName();
144 if ((is_dir($list_dir))&&($list->isPublic() == 9)) {
147 $list_archive_dir = $GLOBALS['mailman_list_dir']."/../archives/private/".$list->getName(); // Does it work? TODO
148 $backupfile=$GLOBALS['tmp_dir']."/".$list->getName()."-mailman.tgz";
149 system("tar cfz $backupfile $list_dir $list_archive_dir");
150 chmod($backupfile,0600);
152 // Delete the mailing list if asked to and the mailing exists (archive deleted as well)
153 system($GLOBALS['mailman_bin_dir']. '/rmlist -a '. $list->getName() .' >/dev/null');
162 * Check if the list exists on the file system
163 * @return true if list exists, false otherwise
165 public function listExists($list) {
166 // Is this the best test?
167 $list_dir = $GLOBALS['mailman_list_dir']."/".$list->getName();
168 if (! is_dir($list_dir)) return false;