File: MailingList.class

package info (click to toggle)
gforge 4.5.14-22etch13
  • links: PTS
  • area: main
  • in suites: etch
  • size: 13,004 kB
  • ctags: 11,918
  • sloc: php: 36,047; sql: 29,050; sh: 10,538; perl: 6,496; xml: 3,810; makefile: 341; python: 263; ansic: 256
file content (400 lines) | stat: -rw-r--r-- 10,696 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
<?php
/**
 * GForge Mailing Lists Facility
 *
 * Copyright 2003 Guillaume Smet
 * http://gforge.org/
 *
 * @version   $Id: MailingList.class 5378 2006-03-20 13:08:53Z danper $
 *
 * This file is part of GForge.
 *
 * GForge is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * GForge is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GForge; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


 /*
 
 This work is based on Tim Perdue's work on the forum stuff
 
 */

require_once('common/include/Error.class');

class MailingList extends Error {

	/**
	 * Associative array of data from db.
	 *
	 * @var	 array   $dataArray.
	 */
	var $dataArray;

	/**
	 * The Group object.
	 *
	 * @var	 object  $Group.
	 */
	var $Group;
	
	/**
	 * The mailing list id
	 *
	 * @var int $groupMailingListId
	 */
	var $groupMailingListId;

	/**
	 *  Constructor.
	 *
	 * @param	object	The Group object to which this mailing list is associated.
	 * @param	int		The group_list_id.
	 * @param	array		The associative array of data.
	 * @return	boolean	success.
	 */
	function MailingList(&$Group, $groupListId = false, $dataArray = false) {
		global $Language;
		$this->Error();
		if (!$Group || !is_object($Group)) {
			$this->setError($Language->getText('general', 'error_no_valid_group_object', array('MailingList')));
			return false;
		}
		if ($Group->isError()) {
			$this->setError('MailingList:: '.$Group->getErrorMessage());
			return false;
		}
		$this->Group =& $Group;

		if ($groupListId) {
			$this->groupMailingListId = $groupListId;
			if (!$dataArray || !is_array($dataArray)) {
				if (!$this->fetchData($groupListId)) {
					return false;
				}
			} else {
				$this->dataArray =& $dataArray;
				if ($this->dataArray['group_id'] != $this->Group->getID()) {
					$this->setError($Language->getText('general', 'error_group_id'));
					$this->dataArray = null;
					return false;
				}
			}
			if (!$this->isPublic()) {
				$perm =& $this->Group->getPermission(session_get_user());

				if (!$perm || !is_object($perm) || !$perm->isMember()) {
					$this->setPermissionDeniedError();
					$this->dataArray = null;
					return false;
				}
			}
		}

		return true;
	}

	/**
	 *	create - use this function to create a new entry in the database.
	 *
	 *	@param	string	The name of the mailing list
	 *	@param	string	The description of the mailing list
	 *	@param	int	Pass (1) if it should be public (0) for private.
	 *
	 *	@return	boolean	success.
	 */
	function create($listName, $description, $isPublic = MAIL__MAILING_LIST_IS_PUBLIC,$creator_id=false) {
		global $Language;
		
		//
		//	During the group creation, the current user_id will not match the admin's id
		//
		if (!$creator_id) {
			$creator_id=user_getid();
			if(!$this->userIsAdmin()) {
				$this->setPermissionDeniedError();
				return false;
			}
		}
		
		if(!$listName || strlen($listName) < MAIL__MAILING_LIST_NAME_MIN_LENGTH) {
			$this->setError($Language->getText('mail_common', 'error_min_name_length'));
			return false;
		}
		
		$realListName = strtolower($this->Group->getUnixName().'-'.$listName);
		
		if(!validate_email($realListName.'@'.$GLOBALS['sys_lists_host'])) {
			$this->setError($Language->getText('mail_common', 'error_invalid_name') . ': ' .
			$realListName.'@'.$GLOBALS['sys_lists_host']);
			return false;
		}

		$result = db_query('SELECT 1 FROM mail_group_list WHERE lower(list_name)=\''.$realListName.'\'');

		if (db_numrows($result) > 0) {
			$this->setError($Language->getText('mail_common', 'error_list_already_exists'));
			return false;
		}
			
		$listPassword = substr(md5($GLOBALS['session_hash'] . time() . rand(0,40000)), 0, 16);
		
		$sql = 'INSERT INTO mail_group_list '
			. '(group_id, list_name, is_public, password, list_admin, status, description) VALUES ('
			. $this->Group->getID(). ', '
			. "'".$realListName."',"
			. "'".$isPublic."',"
			. "'".$listPassword."',"
			. "'".$creator_id."',"
			. "'".MAIL__MAILING_LIST_IS_REQUESTED."',"
			. "'".$description."')";
		
		db_begin();
		$result = db_query($sql);
		
		if (!$result) {
			db_rollback();
			$this->setError($Language->getText('general', 'error_creating', array($Language->getText('mail_common', 'mailing_list'))).db_error());
			return false;
		}
			
		$this->groupMailingListId = db_insertid($result, 'mail_group_list', 'group_list_id');
		$this->fetchData($this->groupMailingListId);
		
		$user = &user_get_object($creator_id);
		$userEmail = $user->getEmail();
		if(empty($userEmail) || !validate_email($userEmail)) {
			db_rollback();
			$this->setInvalidEmailError();
			return false;
		} else {
			$mailBody = stripcslashes($Language->getText('mail_common', 'creation_mail_body', array($GLOBALS['sys_name'], $GLOBALS['sys_lists_host'], $realListName, $this->getExternalInfoUrl(), $this->getExternalAdminUrl(), $listPassword)));
			$mailSubject = $Language->getText('mail_common', 'creation_mail_subject', array($GLOBALS['sys_name']));
			
			util_send_message($userEmail, $mailSubject, $mailBody, 'admin@'.$GLOBALS['sys_default_domain']);
		}
		
		db_commit();
		return true;
	}

	/**
	 *  fetchData - re-fetch the data for this mailing list from the database.
	 *
	 *  @param  int	 The list_id.
	 *	@return	boolean	success.
	 */
	function fetchData($groupListId) {
		global $Language;
		$res=db_query("SELECT * FROM mail_group_list "
			. "WHERE group_list_id='".$groupListId."' "
			. "AND group_id='". $this->Group->getID() ."'");
		if (!$res || db_numrows($res) < 1) {
			$this->setError($Language->getText('general', 'error_getting', array($Language->getText('mail_common', 'mailing_list'))));
			return false;
		}
		$this->dataArray =& db_fetch_array($res);
		db_free_result($res);
		return true;
	}

	/**
	 *	update - use this function to update an entry in the database.
	 *
	 *	@param	string	The description of the mailing list
	 *	@param	int	Pass (1) if it should be public (0) for private
	 *	@return	boolean	success.
	 */
	function update($description, $isPublic = MAIL__MAILING_LIST_IS_PUBLIC) {
		global $Language;
		
		if(! $this->userIsAdmin()) {
			$this->setPermissionDeniedError();
			return false;
		}
		
		$sql = "UPDATE mail_group_list 
			SET is_public='".$isPublic."', 
			description='". $description ."' 
			WHERE group_list_id='".$this->groupMailingListId."' 
			AND group_id='".$this->Group->getID()."'";
			
		$res = db_query($sql);

		if (!$res || db_affected_rows($res) < 1) {
			$this->setError($Language->getText('general', 'error_on_update').db_error());
			return false;
		}
		return true;
	}

	/**
	 *	getGroup - get the Group object this mailing list is associated with.
	 *
	 *	@return	object	The Group object.
	 */
	function &getGroup() {
		return $this->Group;
	}

	/**
	 *	getID - The id of this mailing list
	 *
	 *	@return	int	The group_list_id #.
	 */
	function getID() {
		return $this->dataArray['group_list_id'];
	}


	/**
	 *	isPublic - Is this mailing list open to the general public.
	 *
	 *	@return boolean	is_public.
	 */
	function isPublic() {
		return $this->dataArray['is_public'];
	}

	/**
	 *	getName - get the name of this mailing list
	 *
	 *	@return string	The name of this mailing list
	 */
	function getName() {
		return $this->dataArray['list_name'];
	}


	/**
	 *	getDescription - get the description of this mailing list
	 *
	 *	@return string	The description.
	 */
	function getDescription() {
		return $this->dataArray['description'];
	}
	
	/**
	 * getPassword - get the password to administrate the mailing list
	 *
	 * @return string The password
	 */
	function getPassword() {
		return $this->dataArray['password'];
	}
	
	/**
	 * getListAdmin - get the user who is the admin of this mailing list
	 *
	 * @return User The admin user
	 */
	function getListAdmin() {
		return user_get_object($this->dataArray['list_admin']);
	}
	
	/**
	 * getStatus - get the status of this mailing list
	 *
	 * @return int The status
	 */
	function getStatus() {
		return $this->dataArray['status'];
	}
	
	/**
	 * getArchivesUrl - get the url to see the archives of the list
	 *
	 * @return string url of the archives
	 */
	function getArchivesUrl() {
		if ($this->isPublic()) {
			return 'http://'.$GLOBALS['sys_lists_host'].'/pipermail/'.$this->getName().'/';
		} else {
			return 'http://'.$GLOBALS['sys_lists_host'].'/mailman/private/'.$this->getName().'/';
		}
	}
	
	/**
	 * getExternalInfoUrl - get the url to subscribe/unsubscribe
	 *
	 * @return string url of the info page
	 */
	function getExternalInfoUrl() {
		return 'http://'.$GLOBALS['sys_lists_host'].'/mailman/listinfo/'.$this->getName();
	}
	
	/**
	 * getExternalAdminUrl - get the url to admin the list with the external tools used
	 *
	 * @return string url of the admin
	 */
	function getExternalAdminUrl() {
		return 'http://'.$GLOBALS['sys_lists_host'].'/mailman/admin/'.$this->getName();
	}

	/**
	 *	delete - permanently delete this mailing list
	 *
	 *	@param	boolean	I'm Sure.
	 *	@param	boolean	I'm Really Sure.
	 *	@return	boolean success;
	 */
	function delete($sure,$really_sure) {

		if (!$sure || !$really_sure) {
			$this->setMissingParamsError();
			return false;
		}
		if (!$this->userIsAdmin()) {
			$this->setPermissionDeniedError();
			return false;
		}
		$sql="INSERT INTO deleted_mailing_lists (mailing_list_name,
			delete_date,isdeleted) VALUES ('".$this->getName()."','".time()."','0')";
		$res=db_query($sql);
		if (!$res) {
			$this->setError('Could Not Insert Into Delete Queue: '.db_error());
			return false;
		}
		$res=db_query("DELETE FROM mail_group_list WHERE 
			group_list_id='".$this->getID()."'");
		if (!$res) {
			$this->setError('Could Not Delete List: '.db_error());
			return false;
		}
		return true;
		
	}

	/**
	 * userIsAdmin - use this function to know if the user can administrate mailing lists
	 *
	 * This is a static method. Currently the user must be a project or a sitewide admin to administrate the mailing lists
	 *
	 * @return boolean true if the user can administrate mailing lists
	 */
	function userIsAdmin() {
		$perm = & $this->Group->getPermission(session_get_user());
		if (!$perm || !is_object($perm)) {
			return false;
		} elseif ($perm->isAdmin()) {
			return true;
		} else {
			return false;
		}
	}
}

?>