3 * FusionForge Mailing Lists Facility
5 * Copyright 2003 Guillaume Smet
6 * http://fusionforge.org/
10 * This file is part of FusionForge.
12 * FusionForge is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * FusionForge is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * Portions Copyright 2010 (c) Mélanie Le Bail
31 This work is based on Tim Perdue's work on the forum stuff
35 require_once 'MailmanListDao.class.php';
36 require_once 'ProjectManager.class.php';
37 require_once 'UserManager.class.php';
38 require_once 'common/dao/CodendiDataAccess.class.php';
39 require_once 'common/system_event/SystemEventManager.class.php';
40 require_once 'common/system_event/SystemEvent.class.php';
42 class MailmanList extends FFError {
46 * @var MailingListDao $mailingDAO.
51 * Associative array of data from db.
53 * @var array $dataArray.
67 * @var int $groupMailmanListId
69 var $groupMailmanListId;
72 * @param object The Group object to which this mailing list is associated.
73 * @param int The group_list_id.
74 * @param array The associative array of data.
76 function __construct($group_id, $groupListId = false, $dataArray = false) {
77 $pm = ProjectManager::instance();
78 $Group = $pm->getProject($group_id);
79 $this->_mailingDAO = new MailmanListDao(CodendiDataAccess::instance());
80 parent::__construct();
81 if (!$Group || !is_object($Group)) {
84 if ($Group->isError()) {
85 $this->setError('MailmanList: '.$Group->getErrorMessage());
88 $this->Group =& $Group;
91 $this->groupMailmanListId = $groupListId;
92 if (!$dataArray || !is_array($dataArray)) {
93 if (!$this->fetchData($groupListId)) {
97 $this->dataArray =& $dataArray;
98 if ($this->dataArray['group_id'] != $this->Group->getID()) {
99 $this->setError(_('group_id in db result does not match Group Object'));
100 $this->dataArray = null;
104 if (!$this->isPublic()) {
106 $this->dataArray = null;
114 * create - use this function to create a new entry in the database.
116 * @param string The name of the mailing list
117 * @param string The description of the mailing list
118 * @param int Pass (1) if it should be public (0) for private.
120 * @return boolean success.
123 function create($listName, $description, $isPublic = '1',$creator_id=false) {
124 $current_user=UserManager::instance()->getCurrentUser();
126 // During the group creation, the current user_id will not match the admin's id
129 $creator_id=$current_user->getID();
130 if(!$current_user->isMember($this->Group->getID(),'A')) {
131 exit_permission_denied();
136 if(!$listName || strlen($listName) < 4) {
137 $this->setError(_('Must Provide List Name That Is 4 or More Characters Long'));
141 $realListName = strtolower($this->Group->getUnixName().'-'.$listName);
143 if(!validate_email($realListName.'@'.forge_get_config('lists_host'))) {
144 $this->setError(_('Invalid List Name') . _(': ') .
145 $realListName.'@'.forge_get_config('lists_host'));
148 $result=&$this->_mailingDAO->searchByName($realListName);
150 if ($result->valid()) {
151 $this->setError(_('List Already Exists'));
155 $listPassword = substr(md5($GLOBALS['session_hash'] . time() . rand(0,40000)), 0, 16);
156 $result = $this->_mailingDAO->insertNewList($this->Group->getID(), $realListName,$isPublic,$listPassword,$creator_id,'1',$description);
158 $this->setError(_('Error Creating mailing list')._(': ').db_error());
162 $this->groupMailmanListId = $result;
164 require_once 'mailman/include/events/SystemEvent_MAILMAN_LIST_CREATE.class.php';
165 $systemevent = SystemEventManager::instance();
166 $systemevent->createEvent('MAILMAN_LIST_CREATE', $this->groupMailmanListId,SystemEvent::PRIORITY_MEDIUM);
167 $this->fetchData($this->groupMailmanListId);
168 $user=UserManager::instance()->getUserByID($creator_id);
170 $userEmail = $user->getEmail();
171 if(empty($userEmail) || !validate_email($userEmail)) {
172 $this->setInvalidEmailError();
175 sendCreationMail($userEmail,$this);
182 * activationRequested - LEt us know if an event is present to create this list
186 function activationRequested()
188 $systemevent = SystemEventManager::instance();
189 $result1 = $systemevent->fetchEvents(0,10,false,SystemEvent::STATUS_NEW,'MAILMAN_LIST_CREATE',$this->getID());
190 $result2 = $systemevent->fetchEvents(0,10,false,SystemEvent::STATUS_RUNNING,'MAILMAN_LIST_CREATE',$this->getID());
191 if(count($result1)+count($result2)<1) {
200 * recreate - let the admin recrate a list which had a problem during creation
207 $systemevent = SystemEventManager::instance();
208 $systemevent->createEvent('MAILMAN_LIST_CREATE', $this->getID(),SystemEvent::PRIORITY_MEDIUM);
211 * fetchData - re-fetch the data for this mailing list from the database.
213 * @param int The list_id.
214 * @return boolean success.
216 function fetchData($groupListId) {
217 $res =& $this->_mailingDAO->searchListFromGroup($groupListId, $this->Group->getID());
220 $this->setError(_('Error Getting mailing list'));
223 $this->dataArray =& $res->getRow();
228 * update - use this function to update an entry in the database.
230 * @param string The description of the mailing list
231 * @param int Pass (1) if it should be public (0) for private
232 * @return boolean success.
234 function update($description, $isPublic ='1',$status='1') {
235 $current_user=UserManager::instance()->getCurrentUser();
236 if(!$current_user->isMember($this->Group->getID(),'A')) {
237 exit_permission_denied();
240 $res = $this->_mailingDAO->updateList($this->groupMailmanListId, $this->Group->getID(),$description , $isPublic,$status);
242 $this->setError(_('Update failed')._(': ').db_error());
249 * getGroup - get the Group object this mailing list is associated with.
251 * @return object The Group object.
253 function &getGroup() {
258 * getID - The id of this mailing list
260 * @return int The group_list_id #.
263 return $this->dataArray['group_list_id'];
267 * isPublic - Is this mailing list open to the general public.
269 * @return boolean is_public.
271 function isPublic() {
272 return $this->dataArray['is_public'];
276 * getName - get the name of this mailing list
278 * @return string The name of this mailing list
281 return $this->dataArray['list_name'];
285 * getDescription - get the description of this mailing list
287 * @return string The description.
289 function getDescription() {
290 return $this->dataArray['description'];
294 * getPassword - get the password to administrate the mailing list
296 * @return string The password
298 function getPassword() {
299 return $this->dataArray['password'];
303 * getListAdminID - get the user id who is the admin of this mailing list
305 * @return id The admin user
307 function getListAdminID() {
308 return $this->dataArray['list_admin'];
312 * getStatus - get the status of this mailing list
314 * @return int The status
316 function getStatus() {
317 return $this->dataArray['status'];
321 * getArchivesUrl - get the url to see the archives of the list
323 * @return string url of the archives
325 function getArchivesUrl() {
326 if ($this->isPublic()) {
327 $iframe_url = '/pipermail/'.$this->getName().'/';
329 $iframe_url = '/mailman/private/'.$this->getName().'/';
331 htmlIframe($iframe_url, array('class' => 'iframe_service'));
335 * getExternalInfoUrl - get the url to subscribe/unsubscribe
337 * @return string url of the info page
339 function getExternalInfoUrl() {
340 return util_make_url('/mailman/listinfo/'.$this->getName());
343 * getOptionsUrl - get the url to manage options for user
345 * @return string url of the info page
347 function getOptionsUrl() {
348 $current_user=UserManager::instance()->getCurrentUser();
349 $user=$current_user->getEmail();
350 $iframe_url = '/mailman/options/'.$this->getName().'/'.$user;
351 htmlIframe($iframe_url, array('class' => 'iframe_service'));
354 * subscribeUrl - add the user to the mailinglist
356 * @return string url of the info page
358 function subscribe() {
359 $current_user=UserManager::instance()->getCurrentUser();
360 if(isLogged() && $current_user->isMember($this->Group->getID()) && !$this->isMonitoring())
362 $user=$current_user->getEmail();
363 $passwd= $current_user->getUserPw();
364 $name= $current_user->getRealName();
365 $res = $this->_mailingDAO->newSubscriber($user,$name,$passwd,$this->getName());
367 $this->setError(_('Update failed')._(': ').db_error());
370 session_redirect('/plugins/mailman/index.php?group_id='.$this->Group->getId());
375 * unsubscribeUrl - delete the user from the mailing list
377 * @return string url of the info page
379 function unsubscribe() {
380 $current_user=UserManager::instance()->getCurrentUser();
381 $user=$current_user->getEmail();
382 $res = $this->_mailingDAO->deleteSubscriber($user,$this->getName());
384 $this->setError(_('Update failed')._(': ').db_error());
387 session_redirect('/plugins/mailman/index.php?group_id='.$this->Group->getId());
390 * isMonitoring - See if the current user is in the list of people monitoring the forum.
392 * @return boolean is_monitoring.
394 function isMonitoring() {
398 $current_user=UserManager::instance()->getCurrentUser();
399 $user=$current_user->getEmail();
400 $res = $this->_mailingDAO->userIsMonitoring($user,$this->getName());
402 $this->setError(_('Error On Query:').db_error());
405 $row_count = $res->getRow();
406 return $row_count['count'] > 0;
410 * getExternalAdminUrl - get the url to admin the list with the external tools used
412 * @return string url of the admin
415 function getExternalAdminUrl() {
416 $iframe_url = '/mailman/admin/'.$this->getName();
417 htmlIframe($iframe_url, array('class' => 'iframe_service'));
421 * delete - permanently delete this mailing list
423 * @param boolean I'm Sure.
424 * @param boolean I'm Really Sure.
425 * @return boolean success;
427 function deleteList($sure,$really_sure) {
428 $current_user=UserManager::instance()->getCurrentUser();
429 if (!$sure || !$really_sure) {
430 $this->setError(_('Missing parameters'));
434 if (!$current_user->isMember($this->Group->getID(),'A')) {
435 exit_permission_denied();
438 $res = $this->_mailingDAO->deleteList($this->Group->getID(),$this->getID());
440 $this->setError(_('Could Not Delete List: ').db_error());
443 require_once 'mailman/include/events/SystemEvent_MAILMAN_LIST_DELETE.class.php';
444 $systemevent = SystemEventManager::instance();
445 $systemevent->createEvent('MAILMAN_LIST_DELETE', $this->groupMailmanListId,SystemEvent::PRIORITY_MEDIUM);
455 // c-file-style: "bsd"