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 Error {
46 * @var MailingListDao $mailingDAO.
51 * Associative array of data from db.
53 * @var array $dataArray.
67 * @var int $groupMailmanListId
69 var $groupMailmanListId;
74 * @param object The Group object to which this mailing list is associated.
75 * @param int The group_list_id.
76 * @param array The associative array of data.
77 * @return boolean success.
79 function MailmanList($group_id, $groupListId = false, $dataArray = false) {
80 $pm = ProjectManager::instance();
81 $Group = $pm->getProject($group_id);
82 $this->_mailingDAO = new MailmanListDao(CodendiDataAccess::instance());
84 if (!$Group || !is_object($Group)) {
87 if ($Group->isError()) {
88 $this->setError('MailmanList: '.$Group->getErrorMessage());
91 $this->Group =& $Group;
94 $this->groupMailmanListId = $groupListId;
95 if (!$dataArray || !is_array($dataArray)) {
96 if (!$this->fetchData($groupListId)) {
100 $this->dataArray =& $dataArray;
101 if ($this->dataArray['group_id'] != $this->Group->getID()) {
102 $this->setError(_('group_id in db result does not match Group Object'));
103 $this->dataArray = null;
107 if (!$this->isPublic()) {
109 $this->dataArray = null;
119 * create - use this function to create a new entry in the database.
121 * @param string The name of the mailing list
122 * @param string The description of the mailing list
123 * @param int Pass (1) if it should be public (0) for private.
125 * @return boolean success.
128 function create($listName, $description, $isPublic = '1',$creator_id=false) {
129 $current_user=UserManager::instance()->getCurrentUser();
131 // During the group creation, the current user_id will not match the admin's id
134 $creator_id=$current_user->getID();
135 if(!$current_user->isMember($this->Group->getID(),'A')) {
136 exit_permission_denied();
141 if(!$listName || strlen($listName) < 4) {
142 $this->setError(_('Must Provide List Name That Is 4 or More Characters Long'));
146 $realListName = strtolower($this->Group->getUnixName().'-'.$listName);
148 if(!validate_email($realListName.'@'.forge_get_config('lists_host'))) {
149 $this->setError(_('Invalid List Name') . _(': ') .
150 $realListName.'@'.forge_get_config('lists_host'));
153 $result=&$this->_mailingDAO->searchByName($realListName);
155 if ($result->valid()) {
156 $this->setError(_('List Already Exists'));
160 $listPassword = substr(md5($GLOBALS['session_hash'] . time() . rand(0,40000)), 0, 16);
161 $result = $this->_mailingDAO->insertNewList($this->Group->getID(), $realListName,$isPublic,$listPassword,$creator_id,'1',$description);
163 $this->setError(_('Error Creating mailing list')._(': ').db_error());
167 $this->groupMailmanListId = $result;
169 require_once 'mailman/include/events/SystemEvent_MAILMAN_LIST_CREATE.class.php';
170 $systemevent = SystemEventManager::instance();
171 $systemevent->createEvent('MAILMAN_LIST_CREATE', $this->groupMailmanListId,SystemEvent::PRIORITY_MEDIUM);
172 $this->fetchData($this->groupMailmanListId);
173 $user=UserManager::instance()->getUserByID($creator_id);
175 $userEmail = $user->getEmail();
176 if(empty($userEmail) || !validate_email($userEmail)) {
177 $this->setInvalidEmailError();
180 sendCreationMail($userEmail,$this);
187 * activationRequested - LEt us know if an event is present to create this list
191 function activationRequested()
193 $systemevent = SystemEventManager::instance();
194 $result1 = $systemevent->fetchEvents(0,10,false,SystemEvent::STATUS_NEW,'MAILMAN_LIST_CREATE',$this->getID());
195 $result2 = $systemevent->fetchEvents(0,10,false,SystemEvent::STATUS_RUNNING,'MAILMAN_LIST_CREATE',$this->getID());
196 if(count($result1)+count($result2)<1) {
205 * recreate - let the admin recrate a list which had a problem during creation
212 $systemevent = SystemEventManager::instance();
213 $systemevent->createEvent('MAILMAN_LIST_CREATE', $this->getID(),SystemEvent::PRIORITY_MEDIUM);
216 * fetchData - re-fetch the data for this mailing list from the database.
218 * @param int The list_id.
219 * @return boolean success.
221 function fetchData($groupListId) {
222 $res =& $this->_mailingDAO->searchListFromGroup($groupListId, $this->Group->getID());
225 $this->setError(_('Error Getting mailing list'));
228 $this->dataArray =& $res->getRow();
233 * update - use this function to update an entry in the database.
235 * @param string The description of the mailing list
236 * @param int Pass (1) if it should be public (0) for private
237 * @return boolean success.
239 function update($description, $isPublic ='1',$status='1') {
240 $current_user=UserManager::instance()->getCurrentUser();
241 if(!$current_user->isMember($this->Group->getID(),'A')) {
242 exit_permission_denied();
245 $res = $this->_mailingDAO->updateList($this->groupMailmanListId, $this->Group->getID(),$description , $isPublic,$status);
247 $this->setError(_('Update failed')._(': ').db_error());
254 * getGroup - get the Group object this mailing list is associated with.
256 * @return object The Group object.
258 function &getGroup() {
263 * getID - The id of this mailing list
265 * @return int The group_list_id #.
268 return $this->dataArray['group_list_id'];
272 * isPublic - Is this mailing list open to the general public.
274 * @return boolean is_public.
276 function isPublic() {
277 return $this->dataArray['is_public'];
281 * getName - get the name of this mailing list
283 * @return string The name of this mailing list
286 return $this->dataArray['list_name'];
290 * getDescription - get the description of this mailing list
292 * @return string The description.
294 function getDescription() {
295 return $this->dataArray['description'];
299 * getPassword - get the password to administrate the mailing list
301 * @return string The password
303 function getPassword() {
304 return $this->dataArray['password'];
308 * getListAdminID - get the user id who is the admin of this mailing list
310 * @return id The admin user
312 function getListAdminID() {
313 return $this->dataArray['list_admin'];
317 * getStatus - get the status of this mailing list
319 * @return int The status
321 function getStatus() {
322 return $this->dataArray['status'];
326 * getArchivesUrl - get the url to see the archives of the list
328 * @return string url of the archives
330 function getArchivesUrl() {
331 if ($this->isPublic()) {
332 $iframe_url = '/pipermail/'.$this->getName().'/';
334 $iframe_url = '/mailman/private/'.$this->getName().'/';
336 htmlIframe($iframe_url, array('class' => 'iframe_service'));
340 * getExternalInfoUrl - get the url to subscribe/unsubscribe
342 * @return string url of the info page
344 function getExternalInfoUrl() {
345 return util_make_url('/mailman/listinfo/'.$this->getName());
348 * getOptionsUrl - get the url to manage options for user
350 * @return string url of the info page
352 function getOptionsUrl() {
353 $current_user=UserManager::instance()->getCurrentUser();
354 $user=$current_user->getEmail();
355 $iframe_url = '/mailman/options/'.$this->getName().'/'.$user;
356 htmlIframe($iframe_url, array('class' => 'iframe_service'));
359 * subscribeUrl - add the user to the mailinglist
361 * @return string url of the info page
363 function subscribe() {
364 $current_user=UserManager::instance()->getCurrentUser();
365 if(isLogged() && $current_user->isMember($this->Group->getID()) && !$this->isMonitoring())
367 $user=$current_user->getEmail();
368 $passwd= $current_user->getUserPw();
369 $name= $current_user->getRealName();
370 $res = $this->_mailingDAO->newSubscriber($user,$name,$passwd,$this->getName());
372 $this->setError(_('Update failed')._(': ').db_error());
375 session_redirect('/plugins/mailman/index.php?group_id='.$this->Group->getId());
380 * unsubscribeUrl - delete the user from the mailing list
382 * @return string url of the info page
384 function unsubscribe() {
385 $current_user=UserManager::instance()->getCurrentUser();
386 $user=$current_user->getEmail();
387 $res = $this->_mailingDAO->deleteSubscriber($user,$this->getName());
389 $this->setError(_('Update failed')._(': ').db_error());
392 session_redirect('/plugins/mailman/index.php?group_id='.$this->Group->getId());
395 * isMonitoring - See if the current user is in the list of people monitoring the forum.
397 * @return boolean is_monitoring.
399 function isMonitoring() {
403 $current_user=UserManager::instance()->getCurrentUser();
404 $user=$current_user->getEmail();
405 $res = $this->_mailingDAO->userIsMonitoring($user,$this->getName());
407 $this->setError(_('Error On Query:').db_error());
410 $row_count = $res->getRow();
411 return $row_count['count'] > 0;
415 * getExternalAdminUrl - get the url to admin the list with the external tools used
417 * @return string url of the admin
420 function getExternalAdminUrl() {
421 $iframe_url = '/mailman/admin/'.$this->getName();
422 htmlIframe($iframe_url, array('class' => 'iframe_service'));
426 * delete - permanently delete this mailing list
428 * @param boolean I'm Sure.
429 * @param boolean I'm Really Sure.
430 * @return boolean success;
432 function deleteList($sure,$really_sure) {
433 $current_user=UserManager::instance()->getCurrentUser();
434 if (!$sure || !$really_sure) {
435 $this->setError(_('Missing params'));
439 if (!$current_user->isMember($this->Group->getID(),'A')) {
440 exit_permission_denied();
443 $res = $this->_mailingDAO->deleteList($this->Group->getID(),$this->getID());
445 $this->setError(_('Could Not Delete List: ').db_error());
448 require_once 'mailman/include/events/SystemEvent_MAILMAN_LIST_DELETE.class.php';
449 $systemevent = SystemEventManager::instance();
450 $systemevent->createEvent('MAILMAN_LIST_DELETE', $this->groupMailmanListId,SystemEvent::PRIORITY_MEDIUM);
460 // c-file-style: "bsd"