3 * FusionForge mailing lists
5 * Copyright 2002, Tim Perdue/GForge, LLC
6 * Copyright 2003, Guillaume Smet
7 * Copyright 2009, Roland Mas
9 * This file is part of FusionForge. FusionForge is free software;
10 * you can redistribute it and/or modify it under the terms of the
11 * GNU General Public License as published by the Free Software
12 * Foundation; either version 2 of the Licence, or (at your option)
15 * FusionForge is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 require_once $gfcommon.'include/Error.class.php';
27 class MailingList extends Error {
30 * Associative array of data from db.
32 * @var array $dataArray.
46 * @var int $groupMailingListId
48 var $groupMailingListId;
53 * @param object The Group object to which this mailing list is associated.
54 * @param int The group_list_id.
55 * @param array The associative array of data.
56 * @return boolean success.
58 function MailingList(&$Group, $groupListId = false, $dataArray = false) {
60 if (!$Group || !is_object($Group)) {
61 $this->setError(sprintf(_('%1$s:: No Valid Group Object'), 'MailingList'));
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 6-24 hours
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(_('%1$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 if ($status == 'xyzzy') {
243 $status = $this->getStatus();
246 $res = db_query_params ('UPDATE mail_group_list SET is_public=$1, description=$2, status=$3
247 WHERE group_list_id=$4 AND group_id=$5',
251 $this->groupMailingListId,
252 $this->Group->getID())) ;
254 if (!$res || db_affected_rows($res) < 1) {
255 $this->setError(_('Error On Update:').db_error());
262 * getGroup - get the Group object this mailing list is associated with.
264 * @return object The Group object.
266 function &getGroup() {
271 * getID - The id of this mailing list
273 * @return int The group_list_id #.
276 return $this->dataArray['group_list_id'];
281 * isPublic - Is this mailing list open to the general public.
283 * @return boolean is_public.
285 function isPublic() {
286 return $this->dataArray['is_public'];
290 * getName - get the name of this mailing list
292 * @return string The name of this mailing list
295 return $this->dataArray['list_name'];
300 * getDescription - get the description of this mailing list
302 * @return string The description.
304 function getDescription() {
305 return $this->dataArray['description'];
309 * getPassword - get the password to administrate the mailing list
311 * @return string The password
313 function getPassword() {
314 return $this->dataArray['password'];
318 * getListAdmin - get the user who is the admin of this mailing list
320 * @return User The admin user
322 function getListAdmin() {
323 return user_get_object($this->dataArray['list_admin']);
327 * getStatus - get the status of this mailing list
329 * @return int The status
331 function getStatus() {
332 return $this->dataArray['status'];
335 function getListEmail() {
336 return $this->getName().'@'.forge_get_config('lists_host');
340 * getArchivesUrl - get the url to see the archives of the list
342 * @return string url of the archives
344 function getArchivesUrl() {
345 if ($this->isPublic()) {
346 return 'http://'.forge_get_config('lists_host').'/pipermail/'.$this->getName().'/';
348 return 'http://'.forge_get_config('lists_host').'/mailman/private/'.$this->getName().'/';
353 * getExternalInfoUrl - get the url to subscribe/unsubscribe
355 * @return string url of the info page
357 function getExternalInfoUrl() {
358 if (forge_get_config('use_ssl')) {
363 return "$proto://".forge_get_config('lists_host').'/mailman/listinfo/'.$this->getName();
367 * getExternalAdminUrl - get the url to admin the list with the external tools used
369 * @return string url of the admin
371 function getExternalAdminUrl() {
372 if (forge_get_config('use_ssl')) {
377 return "$proto://".forge_get_config('lists_host').'/mailman/admin/'.$this->getName();
381 * delete - permanently delete this mailing list
383 * @param boolean I'm Sure.
384 * @param boolean I'm Really Sure.
385 * @return boolean success;
387 function delete($sure,$really_sure) {
389 if (!$sure || !$really_sure) {
390 $this->setMissingParamsError(_('Please tick all checkboxes.'));
393 if (!forge_check_perm ('project_admin', $this->Group->getID())) {
394 $this->setPermissionDeniedError();
397 if (!$this->getID() || !$this->getName()) {
398 $this->setError('ID or Name null in MailingList object');
401 $res = db_query_params ('INSERT INTO deleted_mailing_lists (mailing_list_name,delete_date,isdeleted) VALUES ($1,$2,$3)',
402 array ($this->getName(),
406 $this->setError('Could Not Insert Into Delete Queue: '.db_error());
409 $res = db_query_params ('DELETE FROM mail_group_list WHERE group_list_id=$1',
410 array ($this->getID())) ;
412 $this->setError('Could Not Delete List: '.db_error());
422 // c-file-style: "bsd"