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
32 This work is based on Tim Perdue's work on the forum stuff
36 require_once 'MailmanListDao.class.php';
37 require_once 'ProjectManager.class.php';
38 require_once 'UserManager.class.php';
39 require_once 'common/dao/CodendiDataAccess.class.php';
40 require_once 'common/system_event/SystemEventManager.class.php';
41 require_once 'common/system_event/SystemEvent.class.php';
43 class MailmanList extends Error {
47 * @var MailingListDao $mailingDAO.
52 * Associative array of data from db.
54 * @var array $dataArray.
68 * @var int $groupMailmanListId
70 var $groupMailmanListId;
75 * @param object The Group object to which this mailing list is associated.
76 * @param int The group_list_id.
77 * @param array The associative array of data.
78 * @return boolean success.
80 function MailmanList($group_id, $groupListId = false, $dataArray = false) {
81 $pm = ProjectManager::instance();
82 $Group = $pm->getProject($group_id);
83 $this->_mailingDAO = new MailmanListDao(CodendiDataAccess::instance());
85 if (!$Group || !is_object($Group)) {
88 if ($Group->isError()) {
89 $this->setError('MailmanList:: '.$Group->getErrorMessage());
92 $this->Group =& $Group;
95 $this->groupMailmanListId = $groupListId;
96 if (!$dataArray || !is_array($dataArray)) {
97 if (!$this->fetchData($groupListId)) {
101 $this->dataArray =& $dataArray;
102 if ($this->dataArray['group_id'] != $this->Group->getID()) {
103 $this->setError(_('Group_id in db result does not match Group Object'));
104 $this->dataArray = null;
108 if (!$this->isPublic()) {
110 $this->dataArray = null;
120 * create - use this function to create a new entry in the database.
122 * @param string The name of the mailing list
123 * @param string The description of the mailing list
124 * @param int Pass (1) if it should be public (0) for private.
126 * @return boolean success.
129 function create($listName, $description, $isPublic = '1',$creator_id=false) {
130 $current_user=UserManager::instance()->getCurrentUser();
132 // During the group creation, the current user_id will not match the admin's id
135 $creator_id=$current_user->getID();
136 if(!$current_user->isMember($this->Group->getID(),'A')) {
137 exit_permission_denied();
142 if(!$listName || strlen($listName) < 4) {
143 $this->setError(_('Must Provide List Name That Is 4 or More Characters Long'));
147 $realListName = strtolower($this->Group->getUnixName().'-'.$listName);
149 if(!validate_email($realListName.'@'.forge_get_config('lists_host'))) {
150 $this->setError(_('Invalid List Name') . ': ' .
151 $realListName.'@'.forge_get_config('lists_host'));
154 $result=&$this->_mailingDAO->searchByName($realListName);
156 if ($result->valid()) {
157 $this->setError(_('List Already Exists'));
162 $listPassword = substr(md5($GLOBALS['session_hash'] . time() . rand(0,40000)), 0, 16);
163 $result = $this->_mailingDAO->insertNewList($this->Group->getID(), $realListName,$isPublic,$listPassword,$creator_id,'1',$description);
165 $this->setError(_('Error Creating mailing list')._(': ').db_error());
169 $this->groupMailmanListId = $result;
171 require_once 'mailman/include/events/SystemEvent_MAILMAN_LIST_CREATE.class.php';
172 $systemevent = SystemEventManager::instance();
173 $systemevent->createEvent('MAILMAN_LIST_CREATE', $this->groupMailmanListId,SystemEvent::PRIORITY_MEDIUM);
174 $this->fetchData($this->groupMailmanListId);
175 $user=UserManager::instance()->getUserByID($creator_id);
177 $userEmail = $user->getEmail();
178 if(empty($userEmail) || !validate_email($userEmail)) {
179 $this->setInvalidEmailError();
182 sendCreationMail($userEmail,$this);
189 * activationRequested - LEt us know if an event is present to create this list
193 function activationRequested()
195 $systemevent = SystemEventManager::instance();
196 $result1 = $systemevent->fetchEvents(0,10,false,SystemEvent::STATUS_NEW,'MAILMAN_LIST_CREATE',$this->getID());
197 $result2 = $systemevent->fetchEvents(0,10,false,SystemEvent::STATUS_RUNNING,'MAILMAN_LIST_CREATE',$this->getID());
198 if(count($result1)+count($result2)<1) {
207 * recreate - let the admin recrate a list which had a problem during creation
214 $systemevent = SystemEventManager::instance();
215 $systemevent->createEvent('MAILMAN_LIST_CREATE', $this->getID(),SystemEvent::PRIORITY_MEDIUM);
218 * fetchData - re-fetch the data for this mailing list from the database.
220 * @param int The list_id.
221 * @return boolean success.
223 function fetchData($groupListId) {
224 $res =& $this->_mailingDAO->searchListFromGroup($groupListId, $this->Group->getID());
227 $this->setError(_('Error Getting mailing list'));
230 $this->dataArray =& $res->getRow();
235 * update - use this function to update an entry in the database.
237 * @param string The description of the mailing list
238 * @param int Pass (1) if it should be public (0) for private
239 * @return boolean success.
241 function update($description, $isPublic ='1',$status='1') {
242 $current_user=UserManager::instance()->getCurrentUser();
243 if(!$current_user->isMember($this->Group->getID(),'A')) {
244 exit_permission_denied();
247 $res = $this->_mailingDAO->updateList($this->groupMailmanListId, $this->Group->getID(),$description , $isPublic,$status);
249 $this->setError(_('Update failed')._(': ').db_error());
256 * getGroup - get the Group object this mailing list is associated with.
258 * @return object The Group object.
260 function &getGroup() {
265 * getID - The id of this mailing list
267 * @return int The group_list_id #.
270 return $this->dataArray['group_list_id'];
275 * isPublic - Is this mailing list open to the general public.
277 * @return boolean is_public.
279 function isPublic() {
280 return $this->dataArray['is_public'];
284 * getName - get the name of this mailing list
286 * @return string The name of this mailing list
289 return $this->dataArray['list_name'];
294 * getDescription - get the description of this mailing list
296 * @return string The description.
298 function getDescription() {
299 return $this->dataArray['description'];
303 * getPassword - get the password to administrate the mailing list
305 * @return string The password
307 function getPassword() {
308 return $this->dataArray['password'];
312 * getListAdminID - get the user id who is the admin of this mailing list
314 * @return id The admin user
316 function getListAdminID() {
317 return $this->dataArray['list_admin'];
321 * getStatus - get the status of this mailing list
323 * @return int The status
325 function getStatus() {
326 return $this->dataArray['status'];
330 * getArchivesUrl - get the url to see the archives of the list
332 * @return string url of the archives
334 function getArchivesUrl() {
335 if ($this->isPublic()) {
336 $iframe_url = '/pipermail/'.$this->getName().'/';
338 $iframe_url = '/mailman/private/'.$this->getName().'/';
340 htmlIframe($iframe_url, array('class' => 'iframe_service'));
344 * getExternalInfoUrl - get the url to subscribe/unsubscribe
346 * @return string url of the info page
348 function getExternalInfoUrl() {
349 return util_make_url('/mailman/listinfo/'.$this->getName());
352 * getOptionsUrl - get the url to manage options for user
354 * @return string url of the info page
356 function getOptionsUrl() {
357 $current_user=UserManager::instance()->getCurrentUser();
358 $user=$current_user->getEmail();
359 $iframe_url = '/mailman/options/'.$this->getName().'/'.$user;
360 htmlIframe($iframe_url, array('class' => 'iframe_service'));
363 * subscribeUrl - add the user to the mailinglist
365 * @return string url of the info page
367 function subscribe() {
368 $current_user=UserManager::instance()->getCurrentUser();
369 if(isLogged() && $current_user->isMember($this->Group->getID()) && !$this->isMonitoring())
371 $user=$current_user->getEmail();
372 $passwd= $current_user->getUserPw();
373 $name= $current_user->getRealName();
374 $res = $this->_mailingDAO->newSubscriber($user,$name,$passwd,$this->getName());
376 $this->setError(_('Update failed')._(': ').db_error());
379 htmlRedirect('/plugins/mailman/index.php?group_id='.$this->Group->getId());
384 * unsubscribeUrl - delete the user from the mailing list
386 * @return string url of the info page
388 function unsubscribe() {
389 $current_user=UserManager::instance()->getCurrentUser();
390 $user=$current_user->getEmail();
391 $res = $this->_mailingDAO->deleteSubscriber($user,$this->getName());
393 $this->setError(_('Update failed')._(': ').db_error());
396 htmlRedirect('/plugins/mailman/index.php?group_id='.$this->Group->getId());
399 * isMonitoring - See if the current user is in the list of people monitoring the forum.
401 * @return boolean is_monitoring.
403 function isMonitoring() {
407 $current_user=UserManager::instance()->getCurrentUser();
408 $user=$current_user->getEmail();
409 $res = $this->_mailingDAO->userIsMonitoring($user,$this->getName());
411 $this->setError(_('Error On Query:').db_error());
414 $row_count = $res->getRow();
415 return $row_count['count'] > 0;
421 * getExternalAdminUrl - get the url to admin the list with the external tools used
423 * @return string url of the admin
426 function getExternalAdminUrl() {
427 $iframe_url = '/mailman/admin/'.$this->getName();
428 htmlIframe($iframe_url, array('class' => 'iframe_service'));
432 * delete - permanently delete this mailing list
434 * @param boolean I'm Sure.
435 * @param boolean I'm Really Sure.
436 * @return boolean success;
438 function deleteList($sure,$really_sure) {
439 $current_user=UserManager::instance()->getCurrentUser();
440 if (!$sure || !$really_sure) {
441 $this->setError('Missing params');
445 if (!$current_user->isMember($this->Group->getID(),'A')) {
446 exit_permission_denied();
449 $res = $this->_mailingDAO->deleteList($this->Group->getID(),$this->getID());
451 $this->setError('Could Not Delete List: '.db_error());
454 require_once 'mailman/include/events/SystemEvent_MAILMAN_LIST_DELETE.class.php';
455 $systemevent = SystemEventManager::instance();
456 $systemevent->createEvent('MAILMAN_LIST_DELETE', $this->groupMailmanListId,SystemEvent::PRIORITY_MEDIUM);
467 // c-file-style: "bsd"