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.
11 * FusionForge is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published
13 * by the Free Software Foundation; either version 2 of the License,
14 * or (at your option) any later version.
16 * FusionForge is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with FusionForge; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
27 require_once $gfcommon.'include/Error.class.php';
29 class MailingList extends Error {
32 * Associative array of data from db.
34 * @var array $dataArray.
48 * @var int $groupMailingListId
50 var $groupMailingListId;
55 * @param object The Group object to which this mailing list is associated.
56 * @param int The group_list_id.
57 * @param array The associative array of data.
58 * @return boolean success.
60 function MailingList(&$Group, $groupListId = false, $dataArray = false) {
62 if (!$Group || !is_object($Group)) {
63 $this->setError(sprintf(_('%1$s:: No Valid Group Object'), 'MailingList'));
66 if ($Group->isError()) {
67 $this->setError('MailingList:: '.$Group->getErrorMessage());
70 $this->Group =& $Group;
73 $this->groupMailingListId = $groupListId;
74 if (!$dataArray || !is_array($dataArray)) {
75 if (!$this->fetchData($groupListId)) {
79 $this->dataArray =& $dataArray;
80 if ($this->dataArray['group_id'] != $this->Group->getID()) {
81 $this->setError(_('Group_id in db result does not match Group Object'));
82 $this->dataArray = null;
86 if (!$this->isPublic()) {
87 $perm =& $this->Group->getPermission ();
89 if (!$perm || !is_object($perm) || !$perm->isMember()) {
90 $this->setPermissionDeniedError();
91 $this->dataArray = null;
101 * create - use this function to create a new entry in the database.
103 * @param string The name of the mailing list
104 * @param string The description of the mailing list
105 * @param int Pass (1) if it should be public (0) for private.
107 * @return boolean success.
109 function create($listName, $description, $isPublic = MAIL__MAILING_LIST_IS_PUBLIC,$creator_id=false) {
111 // During the group creation, the current user_id will not match the admin's id
114 $creator_id=user_getid();
115 if(!forge_check_perm ('project_admin', $this->Group->getID())) {
116 $this->setPermissionDeniedError();
121 if(!$listName || strlen($listName) < MAIL__MAILING_LIST_NAME_MIN_LENGTH) {
122 $this->setError(_('Must Provide List Name That Is 4 or More Characters Long'));
126 $realListName = strtolower($this->Group->getUnixName().'-'.$listName);
128 if(!validate_email($realListName.'@'.forge_get_config('lists_host'))) {
129 $this->setError(_('Invalid List Name') . ': ' .
130 $realListName.'@'.forge_get_config('lists_host'));
134 $result = db_query_params ('SELECT 1 FROM mail_group_list WHERE lower(list_name)=$1',
135 array ($realListName)) ;
137 if (db_numrows($result) > 0) {
138 $this->setError(_('List Already Exists'));
142 $result_forum_samename = db_query_params ('SELECT 1 FROM forum_group_list WHERE forum_name=$1 AND group_id=$2',
144 $this->Group->getID())) ;
146 if (db_numrows($result_forum_samename) > 0){
147 $this->setError(_('Forum exists with the same name'));
151 $listPassword = substr(md5($GLOBALS['session_ser'] . time() . rand(0,40000)), 0, 16);
154 $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)',
155 array ($this->Group->getID(),
160 MAIL__MAILING_LIST_IS_REQUESTED,
164 $this->setError(sprintf(_('Error Creating %1$s'), _('Error Creating %1$s')).db_error());
169 $this->groupMailingListId = db_insertid($result, 'mail_group_list', 'group_list_id');
170 $this->fetchData($this->groupMailingListId);
172 $user = &user_get_object($creator_id);
173 $userEmail = $user->getEmail();
174 if(empty($userEmail) || !validate_email($userEmail)) {
175 $this->setInvalidEmailError();
179 $mailBody = sprintf(_('A mailing list will be created on %1$s in 6-24 hours
180 and you are the list administrator.
182 This list is: %3$s@%2$s .
184 Your mailing list info is at:
187 List administration can be found at:
190 Your list password is: %6$s .
191 You are encouraged to change this password as soon as possible.
193 Thank you for registering your project with %1$s.
196 '), forge_get_config ('forge_name'), forge_get_config('lists_host'), $realListName, $this->getExternalInfoUrl(), $this->getExternalAdminUrl(), $listPassword);
197 $mailSubject = sprintf(_('%1$s New Mailing List'), forge_get_config ('forge_name'));
199 util_send_message($userEmail, $mailSubject, $mailBody, 'admin@'.forge_get_config('web_host'));
207 * fetchData - re-fetch the data for this mailing list from the database.
209 * @param int The list_id.
210 * @return boolean success.
212 function fetchData($groupListId) {
213 $res = db_query_params ('SELECT * FROM mail_group_list WHERE group_list_id=$1 AND group_id=$2',
215 $this->Group->getID())) ;
216 if (!$res || db_numrows($res) < 1) {
217 $this->setError(sprintf(_('Error Getting %1$s'), _('Error Getting %1$s')));
220 $this->dataArray = db_fetch_array($res);
221 db_free_result($res);
226 * update - use this function to update an entry in the database.
228 * @param string The description of the mailing list
229 * @param int Pass (1) if it should be public (0) for private
230 * @return boolean success.
232 function update($description, $isPublic = MAIL__MAILING_LIST_IS_PUBLIC, $status = 'xyzzy') {
233 if(! forge_check_perm ('project_admin', $this->Group->getID())) {
234 $this->setPermissionDeniedError();
238 if ($status == 'xyzzy') {
239 $status = $this->getStatus();
242 $res = db_query_params ('UPDATE mail_group_list SET is_public=$1, description=$2, status=$3
243 WHERE group_list_id=$4 AND group_id=$5',
247 $this->groupMailingListId,
248 $this->Group->getID())) ;
250 if (!$res || db_affected_rows($res) < 1) {
251 $this->setError(_('Error On Update:').db_error());
258 * getGroup - get the Group object this mailing list is associated with.
260 * @return object The Group object.
262 function &getGroup() {
267 * getID - The id of this mailing list
269 * @return int The group_list_id #.
272 return $this->dataArray['group_list_id'];
277 * isPublic - Is this mailing list open to the general public.
279 * @return boolean is_public.
281 function isPublic() {
282 return $this->dataArray['is_public'];
286 * getName - get the name of this mailing list
288 * @return string The name of this mailing list
291 return $this->dataArray['list_name'];
296 * getDescription - get the description of this mailing list
298 * @return string The description.
300 function getDescription() {
301 return $this->dataArray['description'];
305 * getPassword - get the password to administrate the mailing list
307 * @return string The password
309 function getPassword() {
310 return $this->dataArray['password'];
314 * getListAdmin - get the user who is the admin of this mailing list
316 * @return User The admin user
318 function getListAdmin() {
319 return user_get_object($this->dataArray['list_admin']);
323 * getStatus - get the status of this mailing list
325 * @return int The status
327 function getStatus() {
328 return $this->dataArray['status'];
332 * getArchivesUrl - get the url to see the archives of the list
334 * @return string url of the archives
336 function getArchivesUrl() {
337 if ($this->isPublic()) {
338 return 'http://'.forge_get_config('lists_host').'/pipermail/'.$this->getName().'/';
340 return 'http://'.forge_get_config('lists_host').'/mailman/private/'.$this->getName().'/';
345 * getExternalInfoUrl - get the url to subscribe/unsubscribe
347 * @return string url of the info page
349 function getExternalInfoUrl() {
350 return 'http://'.forge_get_config('lists_host').'/mailman/listinfo/'.$this->getName();
354 * getExternalAdminUrl - get the url to admin the list with the external tools used
356 * @return string url of the admin
358 function getExternalAdminUrl() {
359 return 'http://'.forge_get_config('lists_host').'/mailman/admin/'.$this->getName();
363 * delete - permanently delete this mailing list
365 * @param boolean I'm Sure.
366 * @param boolean I'm Really Sure.
367 * @return boolean success;
369 function delete($sure,$really_sure) {
371 if (!$sure || !$really_sure) {
372 $this->setMissingParamsError();
375 if (!forge_check_perm ('project_admin', $this->Group->getID())) {
376 $this->setPermissionDeniedError();
379 $res = db_query_params ('INSERT INTO deleted_mailing_lists (mailing_list_name,delete_date,isdeleted) VALUES ($1,$2,$3)',
380 array ($this->getName(),
384 $this->setError('Could Not Insert Into Delete Queue: '.db_error());
387 $res = db_query_params ('DELETE FROM mail_group_list WHERE group_list_id=$1',
388 array ($this->getID())) ;
390 $this->setError('Could Not Delete List: '.db_error());
400 // c-file-style: "bsd"