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;
56 * @param bool $groupListId
57 * @param bool $dataArray
58 * @internal param \The $object Group object to which this mailing list is associated.
59 * @internal param \The $int group_list_id.
60 * @internal param \The $array associative array of data.
61 * @return \MailingList
63 function __construct(&$Group, $groupListId = false, $dataArray = false) {
64 parent::__construct();
65 if (!$Group || !is_object($Group)) {
66 $this->setError(_('No Valid Group Object'));
69 if ($Group->isError()) {
70 $this->setError('MailingList: '.$Group->getErrorMessage());
73 $this->Group =& $Group;
76 $this->groupMailingListId = $groupListId;
77 if (!$dataArray || !is_array($dataArray)) {
78 if (!$this->fetchData($groupListId)) {
82 $this->dataArray =& $dataArray;
83 if ($this->dataArray['group_id'] != $this->Group->getID()) {
84 $this->setError(_('group_id in db result does not match Group Object'));
85 $this->dataArray = null;
89 if (!$this->isPublic()) {
90 $perm =& $this->Group->getPermission ();
92 if (!$perm || !is_object($perm) || !$perm->isMember()) {
93 $this->setPermissionDeniedError();
94 $this->dataArray = null;
102 * create - use this function to create a new entry in the database.
105 * @param $description
106 * @param string $isPublic
107 * @param bool $creator_id
108 * @param int $is_external Pass (1) if it should be public (0) for private.
110 * @internal param \The $string name of the mailing list
111 * @internal param \The $string description of the mailing list
112 * @return boolean success.
114 function create($listName, $description, $isPublic = MAIL__MAILING_LIST_IS_PUBLIC,$creator_id=false, $is_external=0) {
116 // During the group creation, the current user_id will not match the admin's id
119 $creator_id=user_getid();
120 if(!forge_check_perm ('project_admin', $this->Group->getID())) {
121 $this->setPermissionDeniedError();
126 if(!$listName || strlen($listName) < MAIL__MAILING_LIST_NAME_MIN_LENGTH) {
127 $this->setError(_('Must Provide List Name That Is 4 or More Characters Long'));
131 $realListName = strtolower($this->Group->getUnixName().'-'.$listName);
132 if (!preg_match('/^[a-z0-9\-_\.]*$/', $realListName)) {
133 $this->setError(_('Invalid List Name')._(': ').$realListName);
137 // '|' or '/' are valid chars in emails but are not allowed by mailman.
138 if( preg_match('/[|\/]/', $realListName) ||
139 !validate_email($realListName.'@'.forge_get_config('lists_host'))) {
140 $this->setError(_('Invalid List Name')._(': ').
141 $realListName.'@'.forge_get_config('lists_host'));
145 $result = db_query_params ('SELECT 1 FROM mail_group_list WHERE lower(list_name)=$1',
146 array ($realListName)) ;
148 if (db_numrows($result) > 0) {
149 $this->setError(_('List Already Exists'));
153 $result_forum_samename = db_query_params ('SELECT 1 FROM forum_group_list WHERE forum_name=$1 AND group_id=$2',
155 $this->Group->getID())) ;
157 if (db_numrows($result_forum_samename) > 0){
158 $this->setError(_('Forum exists with the same name'));
162 $listPassword = substr(md5(time() . util_randbytes()), 0, 16);
165 $result = db_query_params('INSERT INTO mail_group_list
166 (group_id, list_name, is_public, password, list_admin, status, description)
167 VALUES ($1, $2, $3, $4, $5, $6, $7)',
169 $this->Group->getID(),
174 MAIL__MAILING_LIST_IS_REQUESTED,
178 $this->setError(_('Error Creating mailing list')._(': ').db_error());
183 // TODO: link 'mail_group_list' with 'systasks' for better reporting
184 $systasksq = new SysTasksQ();
185 $systasksq->add(SYSTASK_CORE, 'LISTS', $this->Group->getID());
187 $this->groupMailingListId = db_insertid($result, 'mail_group_list', 'group_list_id');
188 $this->fetchData($this->groupMailingListId);
190 $user = user_get_object($creator_id);
191 $userEmail = $user ? $user->getEmail() : "";
192 if(empty($userEmail) || !validate_email($userEmail)) {
193 $this->setInvalidEmailError();
197 $mailBody = sprintf(_('A mailing list will be created on %1$s in one hour
198 and you are the list administrator.
200 This list is: %3$s@%2$s .
202 Your mailing list info is at:
205 List administration can be found at:
208 Your list password is: %6$s .
209 You are encouraged to change this password as soon as possible.
211 Thank you for registering your project with %1$s.'), forge_get_config ('forge_name'), forge_get_config('lists_host'), $realListName, $this->getExternalInfoUrl(), $this->getExternalAdminUrl(), $listPassword);
213 $mailBody .= sprintf(_('-- the %s staff'), forge_get_config ('forge_name'));
216 $mailSubject = sprintf(_('%s New Mailing List'), forge_get_config ('forge_name'));
218 util_send_message($userEmail, $mailSubject, $mailBody);
226 * fetchData - re-fetch the data for this mailing list from the database.
228 * @param int $groupListId The list_id.
229 * @return bool success.
231 function fetchData($groupListId) {
232 $res = db_query_params ('SELECT * FROM mail_group_list WHERE group_list_id=$1 AND group_id=$2',
234 $this->Group->getID())) ;
235 if (!$res || db_numrows($res) < 1) {
236 $this->setError(_('Error Getting mailing list'));
239 $this->dataArray = db_fetch_array($res);
240 db_free_result($res);
245 * update - use this function to update an entry in the database.
247 * @param $description
248 * @param string $isPublic
249 * @param string $status The description of the mailing list
250 * @param int $is_external Pass (1) if it should be public (0) for private
251 * @return boolean success.
253 function update($description, $isPublic = MAIL__MAILING_LIST_IS_PUBLIC, $status = 'xyzzy', $is_external=0) {
254 if(! forge_check_perm('project_admin', $this->Group->getID())) {
255 $this->setPermissionDeniedError();
259 // do not update the status if the mailing-list is not created yet
260 if ($status == 'xyzzy' || $this->getStatus() == MAIL__MAILING_LIST_IS_REQUESTED) {
261 $status = $this->getStatus();
264 $res = db_query_params ('UPDATE mail_group_list SET is_public=$1, description=$2, status=$3
265 WHERE group_list_id=$4 AND group_id=$5',
269 $this->groupMailingListId,
270 $this->Group->getID())) ;
272 if (!$res || db_affected_rows($res) < 1) {
273 $this->setError(sprintf(_('Error On Update: %s'), db_error()));
277 // TODO: link 'mail_group_list' with 'systasks' for better reporting
278 $systasksq = new SysTasksQ();
279 $systasksq->add(SYSTASK_CORE, 'LISTS', $this->Group->getID());
285 * getGroup - get the Group object this mailing list is associated with.
287 * @return object The Group object.
289 function &getGroup() {
294 * getID - The id of this mailing list
296 * @return int The group_list_id #.
299 return $this->dataArray['group_list_id'];
304 * isPublic - Is this mailing list open to the general public.
306 * @return boolean is_public.
308 function isPublic() {
309 return $this->dataArray['is_public'];
313 * getName - get the name of this mailing list
315 * @return string The name of this mailing list
318 return $this->dataArray['list_name'];
323 * getDescription - get the description of this mailing list
325 * @return string The description.
327 function getDescription() {
328 return $this->dataArray['description'];
332 * getPassword - get the password to administrate the mailing list
334 * @return string The password
336 function getPassword() {
337 return $this->dataArray['password'];
341 * getListAdmin - get the user who is the admin of this mailing list
343 * @return User The admin user
345 function getListAdmin() {
346 return user_get_object($this->dataArray['list_admin']);
350 * getStatus - get the status of this mailing list
352 * @return int The status
354 function getStatus() {
355 return $this->dataArray['status'];
359 * getListEmail - get the email of this mailing list
361 * @return string The email
363 function getListEmail() {
364 return $this->getName().'@'.forge_get_config('lists_host');
368 * getArchivesUrl - get the url to see the archives of the list
370 * @return string url of the archives
372 function getArchivesUrl() {
373 $host = util_url_prefix() . forge_get_config('lists_host');
374 if ($this->isPublic()) {
375 return $host . '/pipermail/' . $this->getName() . '/';
377 return $host . '/mailman/private/' . $this->getName() . '/';
382 * getExternalInfoUrl - get the url to subscribe/unsubscribe
384 * @return string url of the info page
386 function getExternalInfoUrl() {
387 return util_url_prefix() . forge_get_config('lists_host') .
388 '/mailman/listinfo/' . $this->getName();
392 * getExternalAdminUrl - get the url to admin the list with the external tools used
394 * @return string url of the admin
396 function getExternalAdminUrl() {
397 return util_url_prefix() . forge_get_config('lists_host') .
398 '/mailman/admin/' . $this->getName();
402 * delete - permanently delete this mailing list
404 * @param boolean $sure I'm Sure.
405 * @param boolean $really_sure I'm Really Sure.
406 * @return boolean success;
408 function delete($sure,$really_sure) {
410 if (!$sure || !$really_sure) {
411 $this->setMissingParamsError(_('Please tick all checkboxes.'));
414 if (!forge_check_perm ('project_admin', $this->Group->getID())) {
415 $this->setPermissionDeniedError();
418 if (!$this->getID() || !$this->getName()) {
419 $this->setError('ID or Name null in MailingList object');
422 $res = db_query_params ('INSERT INTO deleted_mailing_lists (mailing_list_name,delete_date,isdeleted) VALUES ($1,$2,$3)',
423 array ($this->getName(),
427 $this->setError('Could Not Insert Into Delete Queue: '.db_error());
430 $res = db_query_params ('DELETE FROM mail_group_list WHERE group_list_id=$1',
431 array ($this->getID())) ;
433 $this->setError('Could Not Delete List: '.db_error());
437 // TODO: link 'mail_group_list' with 'systasks' for better reporting
438 $systasksq = new SysTasksQ();
439 $systasksq->add(SYSTASK_CORE, 'LISTS', $this->Group->getID());
447 // c-file-style: "bsd"