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;
55 * @param bool $groupListId
56 * @param bool $dataArray
57 * @internal param \The $object Group object to which this mailing list is associated.
58 * @internal param \The $int group_list_id.
59 * @internal param \The $array associative array of data.
60 * @return \MailingList
62 function __construct(&$Group, $groupListId = false, $dataArray = false) {
64 if (!$Group || !is_object($Group)) {
65 $this->setError(_('No Valid Group Object'));
68 if ($Group->isError()) {
69 $this->setError('MailingList:: '.$Group->getErrorMessage());
72 $this->Group =& $Group;
75 $this->groupMailingListId = $groupListId;
76 if (!$dataArray || !is_array($dataArray)) {
77 if (!$this->fetchData($groupListId)) {
81 $this->dataArray =& $dataArray;
82 if ($this->dataArray['group_id'] != $this->Group->getID()) {
83 $this->setError(_('group_id in db result does not match Group Object'));
84 $this->dataArray = null;
88 if (!$this->isPublic()) {
89 $perm =& $this->Group->getPermission ();
91 if (!$perm || !is_object($perm) || !$perm->isMember()) {
92 $this->setPermissionDeniedError();
93 $this->dataArray = null;
101 * create - use this function to create a new entry in the database.
104 * @param $description
105 * @param string $isPublic
106 * @param bool $creator_id
107 * @param int $is_external Pass (1) if it should be public (0) for private.
109 * @internal param \The $string name of the mailing list
110 * @internal param \The $string description of the mailing list
111 * @return boolean success.
113 function create($listName, $description, $isPublic = MAIL__MAILING_LIST_IS_PUBLIC,$creator_id=false, $is_external=0) {
115 // During the group creation, the current user_id will not match the admin's id
118 $creator_id=user_getid();
119 if(!forge_check_perm ('project_admin', $this->Group->getID())) {
120 $this->setPermissionDeniedError();
125 if(!$listName || strlen($listName) < MAIL__MAILING_LIST_NAME_MIN_LENGTH) {
126 $this->setError(_('Must Provide List Name That Is 4 or More Characters Long'));
130 $realListName = strtolower($this->Group->getUnixName().'-'.$listName);
131 if (!preg_match('/^[a-z0-9\-_\.]*$/', $realListName)) {
132 $this->setError(_('Invalid List Name') . ': ' .$realListName);
136 // '|' or '/' are valid chars in emails but are not allowed by mailman.
137 if( preg_match('/[|\/]/', $realListName) ||
138 !validate_email($realListName.'@'.forge_get_config('lists_host'))) {
139 $this->setError(_('Invalid List Name') . ': ' .
140 $realListName.'@'.forge_get_config('lists_host'));
144 $result = db_query_params ('SELECT 1 FROM mail_group_list WHERE lower(list_name)=$1',
145 array ($realListName)) ;
147 if (db_numrows($result) > 0) {
148 $this->setError(_('List Already Exists'));
152 $result_forum_samename = db_query_params ('SELECT 1 FROM forum_group_list WHERE forum_name=$1 AND group_id=$2',
154 $this->Group->getID())) ;
156 if (db_numrows($result_forum_samename) > 0){
157 $this->setError(_('Forum exists with the same name'));
161 $listPassword = substr(md5(time() . util_randbytes()), 0, 16);
164 $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)',
165 array($this->Group->getID(),
170 MAIL__MAILING_LIST_IS_REQUESTED,
174 $this->setError(_('Error Creating mailing list')._(': ').db_error());
179 $this->groupMailingListId = db_insertid($result, 'mail_group_list', 'group_list_id');
180 $this->fetchData($this->groupMailingListId);
182 $user = user_get_object($creator_id);
183 $userEmail = $user ? $user->getEmail() : "";
184 if(empty($userEmail) || !validate_email($userEmail)) {
185 $this->setInvalidEmailError();
189 $mailBody = sprintf(_('A mailing list will be created on %1$s in one hour
190 and you are the list administrator.
192 This list is: %3$s@%2$s .
194 Your mailing list info is at:
197 List administration can be found at:
200 Your list password is: %6$s .
201 You are encouraged to change this password as soon as possible.
203 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);
205 $mailBody .= sprintf(_('-- the %s staff'), forge_get_config ('forge_name'));
208 $mailSubject = sprintf(_('%s New Mailing List'), forge_get_config ('forge_name'));
210 util_send_message($userEmail, $mailSubject, $mailBody);
218 * fetchData - re-fetch the data for this mailing list from the database.
220 * @param int $groupListId The list_id.
221 * @return bool success.
223 function fetchData($groupListId) {
224 $res = db_query_params ('SELECT * FROM mail_group_list WHERE group_list_id=$1 AND group_id=$2',
226 $this->Group->getID())) ;
227 if (!$res || db_numrows($res) < 1) {
228 $this->setError(_('Error Getting mailing list'));
231 $this->dataArray = db_fetch_array($res);
232 db_free_result($res);
237 * update - use this function to update an entry in the database.
239 * @param $description
240 * @param string $isPublic
241 * @param string $status The description of the mailing list
242 * @param int $is_external Pass (1) if it should be public (0) for private
243 * @return boolean success.
245 function update($description, $isPublic = MAIL__MAILING_LIST_IS_PUBLIC, $status = 'xyzzy', $is_external=0) {
246 if(! forge_check_perm('project_admin', $this->Group->getID())) {
247 $this->setPermissionDeniedError();
251 // do not update the status if the mailing-list is not created yet
252 if ($status == 'xyzzy' || $this->getStatus() == MAIL__MAILING_LIST_IS_REQUESTED) {
253 $status = $this->getStatus();
256 $res = db_query_params ('UPDATE mail_group_list SET is_public=$1, description=$2, status=$3
257 WHERE group_list_id=$4 AND group_id=$5',
261 $this->groupMailingListId,
262 $this->Group->getID())) ;
264 if (!$res || db_affected_rows($res) < 1) {
265 $this->setError(sprintf(_('Error On Update: %s'), db_error()));
272 * getGroup - get the Group object this mailing list is associated with.
274 * @return object The Group object.
276 function &getGroup() {
281 * getID - The id of this mailing list
283 * @return int The group_list_id #.
286 return $this->dataArray['group_list_id'];
291 * isPublic - Is this mailing list open to the general public.
293 * @return boolean is_public.
295 function isPublic() {
296 return $this->dataArray['is_public'];
300 * getName - get the name of this mailing list
302 * @return string The name of this mailing list
305 return $this->dataArray['list_name'];
310 * getDescription - get the description of this mailing list
312 * @return string The description.
314 function getDescription() {
315 return $this->dataArray['description'];
319 * getPassword - get the password to administrate the mailing list
321 * @return string The password
323 function getPassword() {
324 return $this->dataArray['password'];
328 * getListAdmin - get the user who is the admin of this mailing list
330 * @return User The admin user
332 function getListAdmin() {
333 return user_get_object($this->dataArray['list_admin']);
337 * getStatus - get the status of this mailing list
339 * @return int The status
341 function getStatus() {
342 return $this->dataArray['status'];
345 function getListEmail() {
346 return $this->getName().'@'.forge_get_config('lists_host');
350 * getArchivesUrl - get the url to see the archives of the list
352 * @return string url of the archives
354 function getArchivesUrl() {
355 $host = util_url_prefix() . forge_get_config('lists_host');
356 if ($this->isPublic()) {
357 return $host . '/pipermail/' . $this->getName() . '/';
359 return $host . '/mailman/private/' . $this->getName() . '/';
364 * getExternalInfoUrl - get the url to subscribe/unsubscribe
366 * @return string url of the info page
368 function getExternalInfoUrl() {
369 return util_url_prefix() . forge_get_config('lists_host') .
370 '/mailman/listinfo/' . $this->getName();
374 * getExternalAdminUrl - get the url to admin the list with the external tools used
376 * @return string url of the admin
378 function getExternalAdminUrl() {
379 return util_url_prefix() . forge_get_config('lists_host') .
380 '/mailman/admin/' . $this->getName();
384 * delete - permanently delete this mailing list
386 * @param boolean $sure I'm Sure.
387 * @param boolean $really_sure I'm Really Sure.
388 * @return boolean success;
390 function delete($sure,$really_sure) {
392 if (!$sure || !$really_sure) {
393 $this->setMissingParamsError(_('Please tick all checkboxes.'));
396 if (!forge_check_perm ('project_admin', $this->Group->getID())) {
397 $this->setPermissionDeniedError();
400 if (!$this->getID() || !$this->getName()) {
401 $this->setError('ID or Name null in MailingList object');
404 $res = db_query_params ('INSERT INTO deleted_mailing_lists (mailing_list_name,delete_date,isdeleted) VALUES ($1,$2,$3)',
405 array ($this->getName(),
409 $this->setError('Could Not Insert Into Delete Queue: '.db_error());
412 $res = db_query_params ('DELETE FROM mail_group_list WHERE group_list_id=$1',
413 array ($this->getID())) ;
415 $this->setError('Could Not Delete List: '.db_error());
425 // c-file-style: "bsd"