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)) {
62 $this->setError(sprintf(_('%1$s:: No Valid Group Object'), 'MailingList'));
65 if ($Group->isError()) {
66 $this->setError('MailingList:: '.$Group->getErrorMessage());
69 $this->Group =& $Group;
72 $this->groupMailingListId = $groupListId;
73 if (!$dataArray || !is_array($dataArray)) {
74 if (!$this->fetchData($groupListId)) {
78 $this->dataArray =& $dataArray;
79 if ($this->dataArray['group_id'] != $this->Group->getID()) {
80 $this->setError(_('Group_id in db result does not match Group Object'));
81 $this->dataArray = null;
85 if (!$this->isPublic()) {
86 $perm =& $this->Group->getPermission ();
88 if (!$perm || !is_object($perm) || !$perm->isMember()) {
89 $this->setPermissionDeniedError();
90 $this->dataArray = null;
100 * create - use this function to create a new entry in the database.
102 * @param string The name of the mailing list
103 * @param string The description of the mailing list
104 * @param int Pass (1) if it should be public (0) for private.
106 * @return boolean success.
108 function create($listName, $description, $isPublic = MAIL__MAILING_LIST_IS_PUBLIC,$creator_id=false) {
110 // During the group creation, the current user_id will not match the admin's id
113 $creator_id=user_getid();
114 if(!forge_check_perm ('project_admin', $this->Group->getID())) {
115 $this->setPermissionDeniedError();
120 if(!$listName || strlen($listName) < MAIL__MAILING_LIST_NAME_MIN_LENGTH) {
121 $this->setError(_('Must Provide List Name That Is 4 or More Characters Long'));
125 $realListName = strtolower($this->Group->getUnixName().'-'.$listName);
126 if (!preg_match('/^[a-z0-9\-_\.]*$/', $realListName)) {
127 $this->setError(_('Invalid List Name') . ': ' .$realListName);
131 // '|' or '/' are valid chars in emails but are not allowed by mailman.
132 if( preg_match('/[|\/]/', $realListName) ||
133 !validate_email($realListName.'@'.forge_get_config('lists_host'))) {
134 $this->setError(_('Invalid List Name') . ': ' .
135 $realListName.'@'.forge_get_config('lists_host'));
139 $result = db_query_params ('SELECT 1 FROM mail_group_list WHERE lower(list_name)=$1',
140 array ($realListName)) ;
142 if (db_numrows($result) > 0) {
143 $this->setError(_('List Already Exists'));
147 $result_forum_samename = db_query_params ('SELECT 1 FROM forum_group_list WHERE forum_name=$1 AND group_id=$2',
149 $this->Group->getID())) ;
151 if (db_numrows($result_forum_samename) > 0){
152 $this->setError(_('Forum exists with the same name'));
156 $listPassword = substr(md5(time() . util_randbytes()), 0, 16);
159 $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)',
160 array ($this->Group->getID(),
165 MAIL__MAILING_LIST_IS_REQUESTED,
169 $this->setError(_('Error Creating mailing list: ').db_error());
174 $this->groupMailingListId = db_insertid($result, 'mail_group_list', 'group_list_id');
175 $this->fetchData($this->groupMailingListId);
177 $user = &user_get_object($creator_id);
178 $userEmail = $user ? $user->getEmail() : "";
179 if(empty($userEmail) || !validate_email($userEmail)) {
180 $this->setInvalidEmailError();
184 $mailBody = sprintf(_('A mailing list will be created on %1$s in 6-24 hours
185 and you are the list administrator.
187 This list is: %3$s@%2$s .
189 Your mailing list info is at:
192 List administration can be found at:
195 Your list password is: %6$s .
196 You are encouraged to change this password as soon as possible.
198 Thank you for registering your project with %1$s.
201 '), forge_get_config ('forge_name'), forge_get_config('lists_host'), $realListName, $this->getExternalInfoUrl(), $this->getExternalAdminUrl(), $listPassword);
202 $mailSubject = sprintf(_('%1$s New Mailing List'), forge_get_config ('forge_name'));
204 util_send_message($userEmail, $mailSubject, $mailBody);
212 * fetchData - re-fetch the data for this mailing list from the database.
214 * @param int The list_id.
215 * @return boolean success.
217 function fetchData($groupListId) {
218 $res = db_query_params ('SELECT * FROM mail_group_list WHERE group_list_id=$1 AND group_id=$2',
220 $this->Group->getID())) ;
221 if (!$res || db_numrows($res) < 1) {
222 $this->setError(_('Error Getting mailing list'));
225 $this->dataArray = db_fetch_array($res);
226 db_free_result($res);
231 * update - use this function to update an entry in the database.
233 * @param string The description of the mailing list
234 * @param int Pass (1) if it should be public (0) for private
235 * @return boolean success.
237 function update($description, $isPublic = MAIL__MAILING_LIST_IS_PUBLIC, $status = 'xyzzy') {
238 if(! forge_check_perm('project_admin', $this->Group->getID())) {
239 $this->setPermissionDeniedError();
243 // do not update the status if the mailing-list is not created yet
244 if ($status == 'xyzzy' || $this->getStatus() == MAIL__MAILING_LIST_IS_REQUESTED) {
245 $status = $this->getStatus();
248 $res = db_query_params ('UPDATE mail_group_list SET is_public=$1, description=$2, status=$3
249 WHERE group_list_id=$4 AND group_id=$5',
253 $this->groupMailingListId,
254 $this->Group->getID())) ;
256 if (!$res || db_affected_rows($res) < 1) {
257 $this->setError(_('Error On Update:').db_error());
264 * getGroup - get the Group object this mailing list is associated with.
266 * @return object The Group object.
268 function &getGroup() {
273 * getID - The id of this mailing list
275 * @return int The group_list_id #.
278 return $this->dataArray['group_list_id'];
283 * isPublic - Is this mailing list open to the general public.
285 * @return boolean is_public.
287 function isPublic() {
288 return $this->dataArray['is_public'];
292 * getName - get the name of this mailing list
294 * @return string The name of this mailing list
297 return $this->dataArray['list_name'];
302 * getDescription - get the description of this mailing list
304 * @return string The description.
306 function getDescription() {
307 return $this->dataArray['description'];
311 * getPassword - get the password to administrate the mailing list
313 * @return string The password
315 function getPassword() {
316 return $this->dataArray['password'];
320 * getListAdmin - get the user who is the admin of this mailing list
322 * @return User The admin user
324 function getListAdmin() {
325 return user_get_object($this->dataArray['list_admin']);
329 * getStatus - get the status of this mailing list
331 * @return int The status
333 function getStatus() {
334 return $this->dataArray['status'];
337 function getListEmail() {
338 return $this->getName().'@'.forge_get_config('lists_host');
342 * getArchivesUrl - get the url to see the archives of the list
344 * @return string url of the archives
346 function getArchivesUrl() {
347 $host = util_url_prefix() . forge_get_config('lists_host');
348 if ($this->isPublic()) {
349 return $host . '/pipermail/' . $this->getName() . '/';
351 return $host . '/mailman/private/' . $this->getName() . '/';
356 * getExternalInfoUrl - get the url to subscribe/unsubscribe
358 * @return string url of the info page
360 function getExternalInfoUrl() {
361 return util_url_prefix() . forge_get_config('lists_host') .
362 '/mailman/listinfo/' . $this->getName();
366 * getExternalAdminUrl - get the url to admin the list with the external tools used
368 * @return string url of the admin
370 function getExternalAdminUrl() {
371 return util_url_prefix() . forge_get_config('lists_host') .
372 '/mailman/admin/' . $this->getName();
376 * delete - permanently delete this mailing list
378 * @param boolean I'm Sure.
379 * @param boolean I'm Really Sure.
380 * @return boolean success;
382 function delete($sure,$really_sure) {
384 if (!$sure || !$really_sure) {
385 $this->setMissingParamsError(_('Please tick all checkboxes.'));
388 if (!forge_check_perm ('project_admin', $this->Group->getID())) {
389 $this->setPermissionDeniedError();
392 if (!$this->getID() || !$this->getName()) {
393 $this->setError('ID or Name null in MailingList object');
396 $res = db_query_params ('INSERT INTO deleted_mailing_lists (mailing_list_name,delete_date,isdeleted) VALUES ($1,$2,$3)',
397 array ($this->getName(),
401 $this->setError('Could Not Insert Into Delete Queue: '.db_error());
404 $res = db_query_params ('DELETE FROM mail_group_list WHERE group_list_id=$1',
405 array ($this->getID())) ;
407 $this->setError('Could Not Delete List: '.db_error());
417 // c-file-style: "bsd"