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/Error.class.php';
28 class MailingList extends Error {
31 * Associative array of data from db.
33 * @var array $dataArray.
47 * @var int $groupMailingListId
49 var $groupMailingListId;
54 * @param object The Group object to which this mailing list is associated.
55 * @param int The group_list_id.
56 * @param array The associative array of data.
57 * @return boolean success.
59 function __construct(&$Group, $groupListId = false, $dataArray = false) {
61 if (!$Group || !is_object($Group)) {
64 if ($Group->isError()) {
65 $this->setError('MailingList:: '.$Group->getErrorMessage());
68 $this->Group =& $Group;
71 $this->groupMailingListId = $groupListId;
72 if (!$dataArray || !is_array($dataArray)) {
73 if (!$this->fetchData($groupListId)) {
77 $this->dataArray =& $dataArray;
78 if ($this->dataArray['group_id'] != $this->Group->getID()) {
79 $this->setError(_('Group_id in db result does not match Group Object'));
80 $this->dataArray = null;
84 if (!$this->isPublic()) {
85 $perm =& $this->Group->getPermission ();
87 if (!$perm || !is_object($perm) || !$perm->isMember()) {
88 $this->setPermissionDeniedError();
89 $this->dataArray = null;
99 * create - use this function to create a new entry in the database.
101 * @param string The name of the mailing list
102 * @param string The description of the mailing list
103 * @param int Pass (1) if it should be public (0) for private.
105 * @return boolean success.
107 function create($listName, $description, $isPublic = MAIL__MAILING_LIST_IS_PUBLIC,$creator_id=false) {
109 // During the group creation, the current user_id will not match the admin's id
112 $creator_id=user_getid();
113 if(!forge_check_perm ('project_admin', $this->Group->getID())) {
114 $this->setPermissionDeniedError();
119 if(!$listName || strlen($listName) < MAIL__MAILING_LIST_NAME_MIN_LENGTH) {
120 $this->setError(_('Must Provide List Name That Is 4 or More Characters Long'));
124 $realListName = strtolower($this->Group->getUnixName().'-'.$listName);
125 if (!preg_match('/^[a-z0-9\-_\.]*$/', $realListName)) {
126 $this->setError(_('Invalid List Name') . ': ' .$realListName);
130 // '|' or '/' are valid chars in emails but are not allowed by mailman.
131 if( preg_match('/[|\/]/', $realListName) ||
132 !validate_email($realListName.'@'.forge_get_config('lists_host'))) {
133 $this->setError(_('Invalid List Name') . ': ' .
134 $realListName.'@'.forge_get_config('lists_host'));
138 $result = db_query_params ('SELECT 1 FROM mail_group_list WHERE lower(list_name)=$1',
139 array ($realListName)) ;
141 if (db_numrows($result) > 0) {
142 $this->setError(_('List Already Exists'));
146 $result_forum_samename = db_query_params ('SELECT 1 FROM forum_group_list WHERE forum_name=$1 AND group_id=$2',
148 $this->Group->getID())) ;
150 if (db_numrows($result_forum_samename) > 0){
151 $this->setError(_('Forum exists with the same name'));
155 $listPassword = substr(md5(time() . util_randbytes()), 0, 16);
158 $result = db_query_params ('INSERT INTO mail_group_list (group_id,list_name,is_public,password,list_admin,status,description) VALUES ($1,$2,$3,$4,$5,$6,$7)',
159 array ($this->Group->getID(),
164 MAIL__MAILING_LIST_IS_REQUESTED,
168 $this->setError(_('Error Creating mailing list: ').db_error());
173 $this->groupMailingListId = db_insertid($result, 'mail_group_list', 'group_list_id');
174 $this->fetchData($this->groupMailingListId);
176 $user = &user_get_object($creator_id);
177 $userEmail = $user ? $user->getEmail() : "";
178 if(empty($userEmail) || !validate_email($userEmail)) {
179 $this->setInvalidEmailError();
183 $mailBody = sprintf(_('A mailing list will be created on %1$s in one hour
184 and you are the list administrator.
186 This list is: %3$s@%2$s .
188 Your mailing list info is at:
191 List administration can be found at:
194 Your list password is: %6$s .
195 You are encouraged to change this password as soon as possible.
197 Thank you for registering your project with %1$s.
200 '), forge_get_config ('forge_name'), forge_get_config('lists_host'), $realListName, $this->getExternalInfoUrl(), $this->getExternalAdminUrl(), $listPassword);
201 $mailSubject = sprintf(_('%s New Mailing List'), forge_get_config ('forge_name'));
203 util_send_message($userEmail, $mailSubject, $mailBody);
211 * fetchData - re-fetch the data for this mailing list from the database.
213 * @param int The list_id.
214 * @return boolean success.
216 function fetchData($groupListId) {
217 $res = db_query_params ('SELECT * FROM mail_group_list WHERE group_list_id=$1 AND group_id=$2',
219 $this->Group->getID())) ;
220 if (!$res || db_numrows($res) < 1) {
221 $this->setError(_('Error Getting mailing list'));
224 $this->dataArray = db_fetch_array($res);
225 db_free_result($res);
230 * update - use this function to update an entry in the database.
232 * @param string The description of the mailing list
233 * @param int Pass (1) if it should be public (0) for private
234 * @return boolean success.
236 function update($description, $isPublic = MAIL__MAILING_LIST_IS_PUBLIC, $status = 'xyzzy') {
237 if(! forge_check_perm('project_admin', $this->Group->getID())) {
238 $this->setPermissionDeniedError();
242 // do not update the status if the mailing-list is not created yet
243 if ($status == 'xyzzy' || $this->getStatus() == MAIL__MAILING_LIST_IS_REQUESTED) {
244 $status = $this->getStatus();
247 $res = db_query_params ('UPDATE mail_group_list SET is_public=$1, description=$2, status=$3
248 WHERE group_list_id=$4 AND group_id=$5',
252 $this->groupMailingListId,
253 $this->Group->getID())) ;
255 if (!$res || db_affected_rows($res) < 1) {
256 $this->setError(_('Error On Update:').db_error());
263 * getGroup - get the Group object this mailing list is associated with.
265 * @return object The Group object.
267 function &getGroup() {
272 * getID - The id of this mailing list
274 * @return int The group_list_id #.
277 return $this->dataArray['group_list_id'];
282 * isPublic - Is this mailing list open to the general public.
284 * @return boolean is_public.
286 function isPublic() {
287 return $this->dataArray['is_public'];
291 * getName - get the name of this mailing list
293 * @return string The name of this mailing list
296 return $this->dataArray['list_name'];
301 * getDescription - get the description of this mailing list
303 * @return string The description.
305 function getDescription() {
306 return $this->dataArray['description'];
310 * getPassword - get the password to administrate the mailing list
312 * @return string The password
314 function getPassword() {
315 return $this->dataArray['password'];
319 * getListAdmin - get the user who is the admin of this mailing list
321 * @return User The admin user
323 function getListAdmin() {
324 return user_get_object($this->dataArray['list_admin']);
328 * getStatus - get the status of this mailing list
330 * @return int The status
332 function getStatus() {
333 return $this->dataArray['status'];
336 function getListEmail() {
337 return $this->getName().'@'.forge_get_config('lists_host');
341 * getArchivesUrl - get the url to see the archives of the list
343 * @return string url of the archives
345 function getArchivesUrl() {
346 $host = util_url_prefix() . forge_get_config('lists_host');
347 if ($this->isPublic()) {
348 return $host . '/pipermail/' . $this->getName() . '/';
350 return $host . '/mailman/private/' . $this->getName() . '/';
355 * getExternalInfoUrl - get the url to subscribe/unsubscribe
357 * @return string url of the info page
359 function getExternalInfoUrl() {
360 return util_url_prefix() . forge_get_config('lists_host') .
361 '/mailman/listinfo/' . $this->getName();
365 * getExternalAdminUrl - get the url to admin the list with the external tools used
367 * @return string url of the admin
369 function getExternalAdminUrl() {
370 return util_url_prefix() . forge_get_config('lists_host') .
371 '/mailman/admin/' . $this->getName();
375 * delete - permanently delete this mailing list
377 * @param boolean I'm Sure.
378 * @param boolean I'm Really Sure.
379 * @return boolean success;
381 function delete($sure,$really_sure) {
383 if (!$sure || !$really_sure) {
384 $this->setMissingParamsError(_('Please tick all checkboxes.'));
387 if (!forge_check_perm ('project_admin', $this->Group->getID())) {
388 $this->setPermissionDeniedError();
391 if (!$this->getID() || !$this->getName()) {
392 $this->setError('ID or Name null in MailingList object');
395 $res = db_query_params ('INSERT INTO deleted_mailing_lists (mailing_list_name,delete_date,isdeleted) VALUES ($1,$2,$3)',
396 array ($this->getName(),
400 $this->setError('Could Not Insert Into Delete Queue: '.db_error());
403 $res = db_query_params ('DELETE FROM mail_group_list WHERE group_list_id=$1',
404 array ($this->getID())) ;
406 $this->setError('Could Not Delete List: '.db_error());
416 // c-file-style: "bsd"