3 * FusionForge mailing lists
5 * Copyright 2002, Tim Perdue/GForge, LLC
6 * Copyright 2003, Guillaume Smet
7 * Copyright 2009, Roland Mas
8 * Copyright 2012, Franck Villaume - TrivialDev
10 * This file is part of FusionForge. FusionForge is free software;
11 * you can redistribute it and/or modify it under the terms of the
12 * GNU General Public License as published by the Free Software
13 * Foundation; either version 2 of the Licence, or (at your option)
16 * FusionForge is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 require_once $gfcommon.'include/FFError.class.php';
27 require_once $gfcommon.'include/SysTasksQ.class.php';
29 class MailingList extends FFError {
32 * Associative array of data from db.
34 * @var array $dataArray.
48 * @var int $groupMailingListId
50 var $groupMailingListId;
54 * @param bool $groupListId
55 * @param bool $dataArray
57 function __construct(&$Group, $groupListId = false, $dataArray = false) {
58 parent::__construct();
59 if (!$Group || !is_object($Group)) {
60 $this->setError(_('Invalid Project'));
63 if ($Group->isError()) {
64 $this->setError('MailingList: '.$Group->getErrorMessage());
67 $this->Group =& $Group;
70 $this->groupMailingListId = $groupListId;
71 if (!$dataArray || !is_array($dataArray)) {
72 if (!$this->fetchData($groupListId)) {
76 $this->dataArray =& $dataArray;
77 if ($this->dataArray['group_id'] != $this->Group->getID()) {
78 $this->setError(_('group_id in db result does not match Group Object'));
79 $this->dataArray = null;
83 if (!$this->isPublic()) {
84 $perm =& $this->Group->getPermission ();
86 if (!$perm || !is_object($perm) || !$perm->isMember()) {
87 $this->setPermissionDeniedError();
88 $this->dataArray = null;
96 * create - use this function to create a new entry in the database.
98 * @param string $listName
99 * @param string $description
100 * @param string $isPublic
101 * @param bool $creator_id
102 * @param int $is_external Pass (1) if it should be public (0) for private.
103 * @return bool success.
105 function create($listName, $description, $isPublic = MAIL__MAILING_LIST_IS_PUBLIC, $creator_id = false, $is_external = 0) {
107 // During the group creation, the current user_id will not match the admin's id
110 $creator_id=user_getid();
111 if(!forge_check_perm ('project_admin', $this->Group->getID())) {
112 $this->setPermissionDeniedError();
117 if(!$listName || strlen($listName) < MAIL__MAILING_LIST_NAME_MIN_LENGTH) {
118 $this->setError(_('Must Provide List Name That Is 4 or More Characters Long'));
122 $realListName = strtolower($this->Group->getUnixName().'-'.$listName);
123 if (!preg_match('/^[a-z0-9\-_\.]*$/', $realListName)) {
124 $this->setError(_('Invalid List Name')._(': ').$realListName);
128 // '|' or '/' are valid chars in emails but are not allowed by mailman.
129 if( preg_match('/[|\/]/', $realListName) ||
130 !validate_email($realListName.'@'.forge_get_config('lists_host'))) {
131 $this->setError(_('Invalid List Name')._(': ').
132 $realListName.'@'.forge_get_config('lists_host'));
136 $result = db_query_params ('SELECT 1 FROM mail_group_list WHERE lower(list_name)=$1',
137 array ($realListName));
139 if (db_numrows($result) > 0) {
140 $this->setError(_('List Already Exists'));
144 $result_forum_samename = db_query_params ('SELECT 1 FROM forum_group_list WHERE forum_name=$1 AND group_id=$2',
146 $this->Group->getID()));
148 if (db_numrows($result_forum_samename) > 0){
149 $this->setError(_('Forum exists with the same name'));
153 $listPassword = substr(md5(time() . util_randbytes()), 0, 16);
156 $result = db_query_params('INSERT INTO mail_group_list
157 (group_id, list_name, is_public, password, list_admin, status, description)
158 VALUES ($1, $2, $3, $4, $5, $6, $7)',
160 $this->Group->getID(),
165 MAIL__MAILING_LIST_IS_REQUESTED,
169 $this->setError(_('Error Creating mailing list')._(': ').db_error());
174 // TODO: link 'mail_group_list' with 'systasks' for better reporting
175 $systasksq = new SysTasksQ();
176 $systasksq->add(SYSTASK_CORE, 'LISTS', $this->Group->getID());
178 $this->groupMailingListId = db_insertid($result, 'mail_group_list', 'group_list_id');
179 $this->fetchData($this->groupMailingListId);
181 $user = user_get_object($creator_id);
182 $userEmail = $user ? $user->getEmail() : "";
183 if(empty($userEmail) || !validate_email($userEmail)) {
184 $this->setInvalidEmailError();
188 $mailBody = sprintf(_('A mailing list will be created on %s in one hour and you are the list administrator.'), forge_get_config ('forge_name'))
190 . sprintf(_('This list is: %1$s@%2$s'), $realListName, forge_get_config('lists_host')) . "\n\n"
192 . _('Your mailing list info is at:') . "\n"
193 . $this->getExternalInfoUrl() . "\n\n"
195 . _('List administration can be found at:') . "\n"
196 . $this->getExternalAdminUrl() . "\n\n"
198 . _('Your list password is: ') . $listPassword . "\n"
199 . _('You are encouraged to change this password as soon as possible.') . "\n\n"
201 . sprintf(_('Thank you for registering your project with %s.'), forge_get_config ('forge_name'))
204 . sprintf(_('-- the %s staff'), forge_get_config ('forge_name'))
207 $mailSubject = sprintf(_('%s New Mailing List'), forge_get_config ('forge_name'));
209 util_send_message($userEmail, $mailSubject, $mailBody);
217 * fetchData - re-fetch the data for this mailing list from the database.
219 * @param int $groupListId The list_id.
220 * @return bool success.
222 function fetchData($groupListId) {
223 $res = db_query_params ('SELECT * FROM mail_group_list WHERE group_list_id=$1 AND group_id=$2',
225 $this->Group->getID()));
226 if (!$res || db_numrows($res) < 1) {
227 $this->setError(_('Error Getting mailing list'));
230 $this->dataArray = db_fetch_array($res);
231 db_free_result($res);
236 * update - use this function to update an entry in the database.
238 * @param string $description
239 * @param string $isPublic
240 * @param string $status The description of the mailing list
241 * @param int $is_external Pass (1) if it should be public (0) for private
242 * @return bool success.
244 function update($description, $isPublic = MAIL__MAILING_LIST_IS_PUBLIC, $status = 'xyzzy', $is_external=0) {
245 if(! forge_check_perm('project_admin', $this->Group->getID())) {
246 $this->setPermissionDeniedError();
250 // do not update the status if the mailing-list is not created yet
251 if ($status == 'xyzzy' || $this->getStatus() == MAIL__MAILING_LIST_IS_REQUESTED) {
252 $status = $this->getStatus();
255 $res = db_query_params ('UPDATE mail_group_list SET is_public=$1, description=$2, status=$3
256 WHERE group_list_id=$4 AND group_id=$5',
260 $this->groupMailingListId,
261 $this->Group->getID()));
263 if (!$res || db_affected_rows($res) < 1) {
264 $this->setError(_('Error On Update')._(': ').db_error());
268 // TODO: link 'mail_group_list' with 'systasks' for better reporting
269 $systasksq = new SysTasksQ();
270 $systasksq->add(SYSTASK_CORE, 'LISTS', $this->Group->getID());
276 * getGroup - get the Group object this mailing list is associated with.
278 * @return object The Group object.
280 function &getGroup() {
285 * getID - The id of this mailing list
287 * @return int The group_list_id #.
290 return $this->dataArray['group_list_id'];
295 * isPublic - Is this mailing list open to the general public.
297 * @return boolean is_public.
299 function isPublic() {
300 return $this->dataArray['is_public'];
304 * getName - get the name of this mailing list
306 * @return string The name of this mailing list
309 return $this->dataArray['list_name'];
314 * getDescription - get the description of this mailing list
316 * @return string The description.
318 function getDescription() {
319 return $this->dataArray['description'];
323 * getPassword - get the password to administrate the mailing list
325 * @return string The password
327 function getPassword() {
328 return $this->dataArray['password'];
332 * getListAdmin - get the user who is the admin of this mailing list
334 * @return FFUser The admin user
336 function getListAdmin() {
337 return user_get_object($this->dataArray['list_admin']);
341 * getStatus - get the status of this mailing list
343 * @return int The status
345 function getStatus() {
346 return $this->dataArray['status'];
350 * getListEmail - get the email of this mailing list
352 * @return string The email
354 function getListEmail() {
355 return $this->getName().'@'.forge_get_config('lists_host');
359 * getArchivesUrl - get the url to see the archives of the list
361 * @return string url of the archives
363 function getArchivesUrl() {
364 $host = util_url_prefix() . forge_get_config('lists_host');
365 if ($this->isPublic()) {
366 return $host . '/pipermail/' . $this->getName() . '/';
368 return $host . '/mailman/private/' . $this->getName() . '/';
373 * getExternalInfoUrl - get the url to subscribe/unsubscribe
375 * @return string url of the info page
377 function getExternalInfoUrl() {
378 return util_url_prefix() . forge_get_config('lists_host') .
379 '/mailman/listinfo/' . $this->getName();
383 * getExternalAdminUrl - get the url to admin the list with the external tools used
385 * @return string url of the admin
387 function getExternalAdminUrl() {
388 return util_url_prefix() . forge_get_config('lists_host') .
389 '/mailman/admin/' . $this->getName();
393 * delete - permanently delete this mailing list
395 * @param boolean $sure I'm Sure.
396 * @param boolean $really_sure I'm Really Sure.
397 * @return boolean success;
399 function delete($sure,$really_sure) {
401 if (!$sure || !$really_sure) {
402 $this->setMissingParamsError(_('Please tick all checkboxes.'));
405 if (!forge_check_perm ('project_admin', $this->Group->getID())) {
406 $this->setPermissionDeniedError();
409 if (!$this->getID() || !$this->getName()) {
410 $this->setError('ID or Name null in MailingList object');
413 $res = db_query_params ('INSERT INTO deleted_mailing_lists (mailing_list_name,delete_date,isdeleted) VALUES ($1,$2,$3)',
414 array ($this->getName(),
418 $this->setError('Could Not Insert Into Delete Queue: '.db_error());
421 $res = db_query_params ('DELETE FROM mail_group_list WHERE group_list_id=$1',
422 array ($this->getID()));
424 $this->setError('Could Not Delete List: '.db_error());
428 // TODO: link 'mail_group_list' with 'systasks' for better reporting
429 $systasksq = new SysTasksQ();
430 $systasksq->add(SYSTASK_CORE, 'LISTS', $this->Group->getID());
438 // c-file-style: "bsd"