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)) {
86 $this->setError(sprintf(_('%1$s:: No Valid Group Object'), 'MailmanList'));
89 if ($Group->isError()) {
90 $this->setError('MailmanList:: '.$Group->getErrorMessage());
93 $this->Group =& $Group;
96 $this->groupMailmanListId = $groupListId;
97 if (!$dataArray || !is_array($dataArray)) {
98 if (!$this->fetchData($groupListId)) {
102 $this->dataArray =& $dataArray;
103 if ($this->dataArray['group_id'] != $this->Group->getID()) {
104 $this->setError(_('Group_id in db result does not match Group Object'));
105 $this->dataArray = null;
109 if (!$this->isPublic()) {
111 $this->dataArray = null;
121 * create - use this function to create a new entry in the database.
123 * @param string The name of the mailing list
124 * @param string The description of the mailing list
125 * @param int Pass (1) if it should be public (0) for private.
127 * @return boolean success.
130 function create($listName, $description, $isPublic = '1',$creator_id=false) {
131 $current_user=UserManager::instance()->getCurrentUser();
133 // During the group creation, the current user_id will not match the admin's id
136 $creator_id=$current_user->getID();
137 if(!$current_user->isMember($this->Group->getID(),'A')) {
138 exit_permission_denied();
143 if(!$listName || strlen($listName) < 4) {
144 $this->setError(_('Must Provide List Name That Is 4 or More Characters Long'));
148 $realListName = strtolower($this->Group->getUnixName().'-'.$listName);
150 if(!validate_email($realListName.'@'.forge_get_config('lists_host'))) {
151 $this->setError(_('Invalid List Name') . ': ' .
152 $realListName.'@'.forge_get_config('lists_host'));
155 $result=&$this->_mailingDAO->searchByName($realListName);
157 if ($result->valid()) {
158 $this->setError(_('List Already Exists'));
163 $listPassword = substr(md5($GLOBALS['session_hash'] . time() . rand(0,40000)), 0, 16);
164 $result = $this->_mailingDAO->insertNewList($this->Group->getID(), $realListName,$isPublic,$listPassword,$creator_id,'1',$description);
166 $this->setError(sprintf(_('Error Creating %1$s'), _('Error Creating %1$s')).db_error());
170 $this->groupMailmanListId = $result;
172 require_once 'mailman/include/events/SystemEvent_MAILMAN_LIST_CREATE.class.php';
173 $systemevent = SystemEventManager::instance();
174 $systemevent->createEvent('MAILMAN_LIST_CREATE', $this->groupMailmanListId,SystemEvent::PRIORITY_MEDIUM);
175 $this->fetchData($this->groupMailmanListId);
176 $user=UserManager::instance()->getUserByID($creator_id);
178 $userEmail = $user->getEmail();
179 if(empty($userEmail) || !validate_email($userEmail)) {
180 $this->setInvalidEmailError();
183 sendCreationMail($userEmail,$this);
190 * activationRequested - LEt us know if an event is present to create this list
194 function activationRequested()
196 $systemevent = SystemEventManager::instance();
197 $result1 = $systemevent->fetchEvents(0,10,false,SystemEvent::STATUS_NEW,'MAILMAN_LIST_CREATE',$this->getID());
198 $result2 = $systemevent->fetchEvents(0,10,false,SystemEvent::STATUS_RUNNING,'MAILMAN_LIST_CREATE',$this->getID());
199 if(count($result1)+count($result2)<1) {
208 * recreate - let the admin recrate a list which had a problem during creation
215 $systemevent = SystemEventManager::instance();
216 $systemevent->createEvent('MAILMAN_LIST_CREATE', $this->getID(),SystemEvent::PRIORITY_MEDIUM);
219 * fetchData - re-fetch the data for this mailing list from the database.
221 * @param int The list_id.
222 * @return boolean success.
224 function fetchData($groupListId) {
225 $res =& $this->_mailingDAO->searchListFromGroup($groupListId, $this->Group->getID());
228 $this->setError(sprintf(_('Error Getting %1$s'), _('Error Getting %1$s')));
231 $this->dataArray =& $res->getRow();
236 * update - use this function to update an entry in the database.
238 * @param string The description of the mailing list
239 * @param int Pass (1) if it should be public (0) for private
240 * @return boolean success.
242 function update($description, $isPublic ='1',$status='1') {
243 $current_user=UserManager::instance()->getCurrentUser();
244 if(!$current_user->isMember($this->Group->getID(),'A')) {
245 exit_permission_denied();
248 $res = $this->_mailingDAO->updateList($this->groupMailmanListId, $this->Group->getID(),$description , $isPublic,$status);
250 $this->setError(_('Error On Update:').db_error());
257 * getGroup - get the Group object this mailing list is associated with.
259 * @return object The Group object.
261 function &getGroup() {
266 * getID - The id of this mailing list
268 * @return int The group_list_id #.
271 return $this->dataArray['group_list_id'];
276 * isPublic - Is this mailing list open to the general public.
278 * @return boolean is_public.
280 function isPublic() {
281 return $this->dataArray['is_public'];
285 * getName - get the name of this mailing list
287 * @return string The name of this mailing list
290 return $this->dataArray['list_name'];
295 * getDescription - get the description of this mailing list
297 * @return string The description.
299 function getDescription() {
300 return $this->dataArray['description'];
304 * getPassword - get the password to administrate the mailing list
306 * @return string The password
308 function getPassword() {
309 return $this->dataArray['password'];
313 * getListAdminID - get the user id who is the admin of this mailing list
315 * @return id The admin user
317 function getListAdminID() {
318 return $this->dataArray['list_admin'];
322 * getStatus - get the status of this mailing list
324 * @return int The status
326 function getStatus() {
327 return $this->dataArray['status'];
331 * getArchivesUrl - get the url to see the archives of the list
333 * @return string url of the archives
335 function getArchivesUrl() {
336 if ($this->isPublic()) {
337 $iframe_url = '/pipermail/'.$this->getName().'/';
339 $iframe_url = '/mailman/private/'.$this->getName().'/';
341 htmlIframe($iframe_url, array('class' => 'iframe_service'));
345 * getExternalInfoUrl - get the url to subscribe/unsubscribe
347 * @return string url of the info page
349 function getExternalInfoUrl() {
350 return util_make_url('/mailman/listinfo/'.$this->getName());
353 * getOptionsUrl - get the url to manage options for user
355 * @return string url of the info page
357 function getOptionsUrl() {
358 $current_user=UserManager::instance()->getCurrentUser();
359 $user=$current_user->getEmail();
360 $iframe_url = '/mailman/options/'.$this->getName().'/'.$user;
361 htmlIframe($iframe_url, array('class' => 'iframe_service'));
364 * subscribeUrl - add the user to the mailinglist
366 * @return string url of the info page
368 function subscribe() {
369 $current_user=UserManager::instance()->getCurrentUser();
370 if(isLogged() && $current_user->isMember($this->Group->getID()) && !$this->isMonitoring())
372 $user=$current_user->getEmail();
373 $passwd= $current_user->getUserPw();
374 $name= $current_user->getRealName();
375 $res = $this->_mailingDAO->newSubscriber($user,$name,$passwd,$this->getName());
377 $this->setError(_('Error On Update:').db_error());
380 htmlRedirect('/plugins/mailman/index.php?group_id='.$this->Group->getId());
385 * unsubscribeUrl - delete the user from the mailing list
387 * @return string url of the info page
389 function unsubscribe() {
390 $current_user=UserManager::instance()->getCurrentUser();
391 $user=$current_user->getEmail();
392 $res = $this->_mailingDAO->deleteSubscriber($user,$this->getName());
394 $this->setError(_('Error On Update:').db_error());
397 htmlRedirect('/plugins/mailman/index.php?group_id='.$this->Group->getId());
400 * isMonitoring - See if the current user is in the list of people monitoring the forum.
402 * @return boolean is_monitoring.
404 function isMonitoring() {
408 $current_user=UserManager::instance()->getCurrentUser();
409 $user=$current_user->getEmail();
410 $res = $this->_mailingDAO->userIsMonitoring($user,$this->getName());
412 $this->setError(_('Error On Query:').db_error());
415 $row_count = $res->getRow();
416 return $row_count['count'] > 0;
422 * getExternalAdminUrl - get the url to admin the list with the external tools used
424 * @return string url of the admin
427 function getExternalAdminUrl() {
428 $iframe_url = '/mailman/admin/'.$this->getName();
429 htmlIframe($iframe_url, array('class' => 'iframe_service'));
433 * delete - permanently delete this mailing list
435 * @param boolean I'm Sure.
436 * @param boolean I'm Really Sure.
437 * @return boolean success;
439 function deleteList($sure,$really_sure) {
440 $current_user=UserManager::instance()->getCurrentUser();
441 if (!$sure || !$really_sure) {
442 $this->setError('Missing params');
446 if (!$current_user->isMember($this->Group->getID(),'A')) {
447 exit_permission_denied();
450 $res = $this->_mailingDAO->deleteList($this->Group->getID(),$this->getID());
452 $this->setError('Could Not Delete List: '.db_error());
455 require_once 'mailman/include/events/SystemEvent_MAILMAN_LIST_DELETE.class.php';
456 $systemevent = SystemEventManager::instance();
457 $systemevent->createEvent('MAILMAN_LIST_DELETE', $this->groupMailmanListId,SystemEvent::PRIORITY_MEDIUM);
468 // c-file-style: "bsd"