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;
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) {
108 $dar = $this->_getMailingListDao()->searchByGroupListId($group_list_id);
109 if ($row = $dar->getRow()) {
110 $list = new MailmanList($row['group_id'],$row['group_list_id']);
111 $user=UserManager::instance()->getUserByID($list->getListAdminId());
112 $list_admin_email= $user->getEmail();
113 $list_dir = $GLOBALS['mailman_lib_dir']."/lists/".$list->getName();
115 if($list->isPublic() != 9) {
116 if (! is_dir($list_dir)) {
118 system($GLOBALS['mailman_bin_dir']."/newlist -q ".$list->getName()." ".$list_admin_email." ".$list->getPassword()." >/dev/null");
119 // Then update configuraion
120 if( is_dir($list_dir) && $this->updateListConfig($list) !=false ) {
121 $result = $this->_getMailingListDao() -> updateList($list->getID(),$row['group_id'], $list->getDescription(), $list->isPublic(),'3');
123 printf('Unable to update the list status: '.db_error());
132 $result = $this->_getMailingListDao() -> updateList($list->getID(),$row['group_id'], $list->getDescription(), $list->isPublic(),'3');
134 printf('Unable to update the list status: '.db_error());
146 * Delete mailing list
147 * - list and archives are deleted
148 * - backup first in temp directory
149 * @return true on success, false otherwise
151 public function deleteList($group_list_id) {
152 $dar = $this->_getMailingListDao()->searchByGroupListId($group_list_id);
154 if ($row = $dar->getRow()) {
155 $list=new MailmanList($row['group_id'],$group_list_id);
156 $list_dir = $GLOBALS['mailman_lib_dir']."/lists/".$list->getName();
157 if ((is_dir($list_dir))&&($list->isPublic() == 9)) {
160 $list_archive_dir = $GLOBALS['mailman_lib_dir']."/archives/private/".$list->getName(); // Does it work? TODO
161 $backupfile=$GLOBALS['mailman_lib_dir']."/archives/".$list->getName()."-mailman.tgz";
162 system("tar cfz $backupfile $list_dir $list_archive_dir");
163 chmod($backupfile,0600);
165 // Delete the mailing list if asked to and the mailing exists (archive deleted as well)
166 system($GLOBALS['mailman_bin_dir']. '/rmlist -a '. $list->getName() .' >/dev/null');
175 * Check if the list exists on the file system
176 * @return true if list exists, false otherwise
178 public function listExists($list) {
179 // Is this the best test?
180 $list_dir = $GLOBALS['mailman_lib_dir']."/lists/".$list->getName();
181 if (! is_dir($list_dir)) return false;