Project

General

Profile

1 1365 Luisehahne
<?php
2
/**
3
 *
4
 * @category        frontend
5
 * @package         framework
6
 * @author          WebsiteBaker Project
7
 * @copyright       2004-2009, Ryan Djurovich
8 1373 Luisehahne
 * @copyright       2009-2011, Website Baker Org. e.V.
9 1365 Luisehahne
 * @link			http://www.websitebaker2.org/
10
 * @license         http://www.gnu.org/licenses/gpl.html
11
 * @platform        WebsiteBaker 2.8.x
12 1374 Luisehahne
 * @requirements    PHP 5.2.2 and higher
13 1365 Luisehahne
 * @version         $Id$
14 1457 Luisehahne
 * @filesource		$HeadURL$
15
 * @lastmodified    $Date$
16 1365 Luisehahne
 *
17
 */
18 1373 Luisehahne
19 1496 DarkViper
/* -------------------------------------------------------- */
20
// Must include code to stop this file being accessed directly
21
require_once('globalExceptionHandler.php');
22
if(!defined('WB_PATH')) { throw new IllegalFileException(); }
23
/* -------------------------------------------------------- */
24 1365 Luisehahne
// Include PHPLIB template class
25
require_once(WB_PATH."/include/phplib/template.inc");
26
27
require_once(WB_PATH.'/framework/class.database.php');
28
29
// Include new wbmailer class (subclass of PHPmailer)
30
require_once(WB_PATH."/framework/class.wbmailer.php");
31
32 1462 DarkViper
//require_once(WB_PATH."/framework/SecureForm.php");
33 1365 Luisehahne
34
class wb extends SecureForm
35
{
36
37 1457 Luisehahne
 	public $password_chars = 'a-zA-Z0-9\_\-\!\#\*\+\@\$\&\:';	// General initialization function
38 1365 Luisehahne
	// performed when frontend or backend is loaded.
39
40 1394 Luisehahne
	public function  __construct($mode = SecureForm::FRONTEND) {
41
		parent::__construct($mode);
42 1365 Luisehahne
	}
43
44 1373 Luisehahne
/* ****************
45 1440 Luisehahne
 * check if one or more group_ids are in both group_lists
46
 *
47
 * @access public
48
 * @param mixed $groups_list1: an array or a coma seperated list of group-ids
49
 * @param mixed $groups_list2: an array or a coma seperated list of group-ids
50
 * @param array &$matches: an array-var whitch will return possible matches
51
 * @return bool: true there is a match, otherwise false
52
 */
53
	function is_group_match( $groups_list1 = '', $groups_list2 = '', &$matches = null )
54
	{
55
		if( $groups_list1 == '' ) { return false; }
56
		if( $groups_list2 == '' ) { return false; }
57
		if( !is_array($groups_list1) )
58
		{
59
			$groups_list1 = explode(',', $groups_list1);
60
		}
61
		if( !is_array($groups_list2) )
62
		{
63
			$groups_list2 = explode(',', $groups_list2);
64
		}
65
		$matches = array_intersect( $groups_list1, $groups_list2);
66
		return ( sizeof($matches) != 0 );
67
	}
68
/* ****************
69 1373 Luisehahne
 * check if current user is member of at least one of given groups
70
 * ADMIN (uid=1) always is treated like a member of any groups
71
 *
72
 * @access public
73
 * @param mixed $groups_list: an array or a coma seperated list of group-ids
74
 * @return bool: true if current user is member of one of this groups, otherwise false
75
 */
76
	function ami_group_member( $groups_list = '' )
77
	{
78
		if( $this->get_user_id() == 1 ) { return true; }
79
		return $this->is_group_match( $groups_list, $this->get_groups_id() );
80
	}
81
82 1365 Luisehahne
	// Check whether a page is visible or not.
83
	// This will check page-visibility and user- and group-rights.
84
	/* page_is_visible() returns
85
		false: if page-visibility is 'none' or 'deleted', or page-vis. is 'registered' or 'private' and user isn't allowed to see the page.
86
		true: if page-visibility is 'public' or 'hidden', or page-vis. is 'registered' or 'private' and user _is_ allowed to see the page.
87
	*/
88 1373 Luisehahne
	function page_is_visible($page)
89 1365 Luisehahne
    {
90 1373 Luisehahne
		$show_it = false; // shall we show the page?
91
		$page_id = $page['page_id'];
92
		$visibility = $page['visibility'];
93
		$viewing_groups = $page['viewing_groups'];
94
		$viewing_users = $page['viewing_users'];
95
96 1372 Luisehahne
		// First check if visibility is 'none', 'deleted'
97 1373 Luisehahne
		if($visibility == 'none')
98
        {
99
			return(false);
100
		} elseif($visibility == 'deleted')
101
        {
102
			return(false);
103
		}
104
105
		// Now check if visibility is 'hidden', 'private' or 'registered'
106
		if($visibility == 'hidden') { // hidden: hide the menu-link, but show the page
107
			$show_it = true;
108
		} elseif($visibility == 'private' || $visibility == 'registered')
109
        {
110
			// Check if the user is logged in
111
			if($this->is_authenticated() == true)
112
            {
113
				// Now check if the user has perms to view the page
114
				$in_group = false;
115
				foreach($this->get_groups_id() as $cur_gid)
116
                {
117
				    if(in_array($cur_gid, explode(',', $viewing_groups)))
118
                    {
119
				        $in_group = true;
120
				    }
121
				}
122
				if($in_group || in_array($this->get_user_id(), explode(',', $viewing_users))) {
123
					$show_it = true;
124
				} else {
125
					$show_it = false;
126
				}
127
			} else {
128 1372 Luisehahne
				$show_it = false;
129 1373 Luisehahne
			}
130
		} elseif($visibility == 'public') {
131
			$show_it = true;
132
		} else {
133
			$show_it = false;
134 1365 Luisehahne
		}
135
		return($show_it);
136
	}
137
	// Check if there is at least one active section on this page
138
	function page_is_active($page)
139
    {
140
		global $database;
141 1373 Luisehahne
		$has_active_sections = false;
142
		$page_id = $page['page_id'];
143 1365 Luisehahne
		$now = time();
144 1487 DarkViper
		$sql  = 'SELECT `publ_start`, `publ_end` ';
145
		$sql .= 'FROM `'.TABLE_PREFIX.'sections` WHERE `page_id`='.(int)$page_id;
146
		$query_sections = $database->query($sql);
147
		if($query_sections->numRows() != 0) {
148
			while($section = $query_sections->fetchRow()) {
149
				if( $now<$section['publ_end'] &&
150
					($now>$section['publ_start'] || $section['publ_start']==0) ||
151
					$now>$section['publ_start'] && $section['publ_end']==0)
152
				{
153 1373 Luisehahne
					$has_active_sections = true;
154
					break;
155
				}
156
			}
157
		}
158
		return($has_active_sections);
159 1365 Luisehahne
	}
160
161
	// Check whether we should show a page or not (for front-end)
162
	function show_page($page)
163
    {
164 1487 DarkViper
		$retval = ($this->page_is_visible($page) && $this->page_is_active($page));
165
		return $retval;
166 1365 Luisehahne
	}
167
168
	// Check if the user is already authenticated or not
169
	function is_authenticated() {
170 1487 DarkViper
		$retval = ( isset($_SESSION['USER_ID']) AND
171
		            $_SESSION['USER_ID'] != "" AND
172
		            is_numeric($_SESSION['USER_ID']));
173
        return $retval;
174 1365 Luisehahne
	}
175
176
	// Modified addslashes function which takes into account magic_quotes
177
	function add_slashes($input) {
178 1487 DarkViper
		if( get_magic_quotes_gpc() || (!is_string($input)) ) {
179 1365 Luisehahne
			return $input;
180
		}
181 1487 DarkViper
		return addslashes($input);
182 1365 Luisehahne
	}
183
184
	// Ditto for stripslashes
185
	// Attn: this is _not_ the counterpart to $this->add_slashes() !
186
	// Use stripslashes() to undo a preliminarily done $this->add_slashes()
187
	// The purpose of $this->strip_slashes() is to undo the effects of magic_quotes_gpc==On
188
	function strip_slashes($input) {
189
		if ( !get_magic_quotes_gpc() || ( !is_string($input) ) ) {
190
			return $input;
191
		}
192 1487 DarkViper
		return stripslashes($input);
193 1365 Luisehahne
	}
194
195
	// Escape backslashes for use with mySQL LIKE strings
196
	function escape_backslashes($input) {
197
		return str_replace("\\","\\\\",$input);
198
	}
199
200
	function page_link($link){
201
		// Check for :// in the link (used in URL's) as well as mailto:
202 1373 Luisehahne
		if(strstr($link, '://') == '' AND substr($link, 0, 7) != 'mailto:') {
203 1365 Luisehahne
			return WB_URL.PAGES_DIRECTORY.$link.PAGE_EXTENSION;
204
		} else {
205
			return $link;
206
		}
207
	}
208
209
	// Get POST data
210
	function get_post($field) {
211 1487 DarkViper
		return (isset($_POST[$field]) ? $_POST[$field] : null);
212 1365 Luisehahne
	}
213
214
	// Get POST data and escape it
215
	function get_post_escaped($field) {
216
		$result = $this->get_post($field);
217
		return (is_null($result)) ? null : $this->add_slashes($result);
218
	}
219
220
	// Get GET data
221
	function get_get($field) {
222 1487 DarkViper
		return (isset($_GET[$field]) ? $_GET[$field] : null);
223 1365 Luisehahne
	}
224
225
	// Get SESSION data
226
	function get_session($field) {
227 1487 DarkViper
		return (isset($_SESSION[$field]) ? $_SESSION[$field] : null);
228 1365 Luisehahne
	}
229
230
	// Get SERVER data
231
	function get_server($field) {
232 1487 DarkViper
		return (isset($_SERVER[$field]) ? $_SERVER[$field] : null);
233 1365 Luisehahne
	}
234
235
	// Get the current users id
236
	function get_user_id() {
237
		return $_SESSION['USER_ID'];
238
	}
239
240 1373 Luisehahne
	// Get the current users group id
241 1365 Luisehahne
	function get_group_id() {
242
		return $_SESSION['GROUP_ID'];
243
	}
244
245
	// Get the current users group ids
246
	function get_groups_id() {
247 1373 Luisehahne
		return explode(",", $_SESSION['GROUPS_ID']);
248 1365 Luisehahne
	}
249
250
	// Get the current users group name
251
	function get_group_name() {
252
		return implode(",", $_SESSION['GROUP_NAME']);
253
	}
254
255
	// Get the current users group name
256
	function get_groups_name() {
257
		return $_SESSION['GROUP_NAME'];
258
	}
259
260
	// Get the current users username
261
	function get_username() {
262
		return $_SESSION['USERNAME'];
263
	}
264
265
	// Get the current users display name
266
	function get_display_name() {
267 1373 Luisehahne
		return ($_SESSION['DISPLAY_NAME']);
268 1365 Luisehahne
	}
269
270
	// Get the current users email address
271
	function get_email() {
272
		return $_SESSION['EMAIL'];
273
	}
274
275
	// Get the current users home folder
276
	function get_home_folder() {
277
		return $_SESSION['HOME_FOLDER'];
278
	}
279
280
	// Get the current users timezone
281
	function get_timezone() {
282 1487 DarkViper
		return (isset($_SESSION['USE_DEFAULT_TIMEZONE']) ? '-72000' : $_SESSION['TIMEZONE']);
283 1365 Luisehahne
	}
284
285 1373 Luisehahne
	// Validate supplied email address
286
	function validate_email($email) {
287
		if(function_exists('idn_to_ascii')){ /* use pear if available */
288
			$email = idn_to_ascii($email);
289
		}else {
290
			require_once(WB_PATH.'/include/idna_convert/idna_convert.class.php');
291
			$IDN = new idna_convert();
292
			$email = $IDN->encode($email);
293
			unset($IDN);
294 1372 Luisehahne
		}
295 1378 Luisehahne
		// regex from NorHei 2011-01-11
296
		$retval = preg_match("/^((([!#$%&'*+\\-\/\=?^_`{|}~\w])|([!#$%&'*+\\-\/\=?^_`{|}~\w][!#$%&'*+\\-\/\=?^_`{|}~\.\w]{0,}[!#$%&'*+\\-\/\=?^_`{|}~\w]))[@]\w+(([-.]|\-\-)\w+)*\.\w+(([-.]|\-\-)\w+)*)$/", $email);
297
		return ($retval != false);
298 1372 Luisehahne
	}
299
300
/* ****************
301 1365 Luisehahne
 * set one or more bit in a integer value
302
 *
303
 * @access public
304
 * @param int $value: reference to the integer, containing the value
305
 * @param int $bits2set: the bitmask witch shall be added to value
306
 * @return void
307
 */
308
	function bit_set( &$value, $bits2set )
309
	{
310
		$value |= $bits2set;
311
	}
312
313
/* ****************
314
 * reset one or more bit from a integer value
315
 *
316
 * @access public
317
 * @param int $value: reference to the integer, containing the value
318
 * @param int $bits2reset: the bitmask witch shall be removed from value
319
 * @return void
320
 */
321
	function bit_reset( &$value, $bits2reset)
322
	{
323
		$value &= ~$bits2reset;
324
	}
325
326
/* ****************
327
 * check if one or more bit in a integer value are set
328
 *
329
 * @access public
330
 * @param int $value: reference to the integer, containing the value
331
 * @param int $bits2set: the bitmask witch shall be added to value
332
 * @return void
333
 */
334
	function bit_isset( $value, $bits2test )
335
	{
336
		return (($value & $bits2test) == $bits2test);
337
	}
338
339 1373 Luisehahne
/*
340 1365 Luisehahne
	// Validate supplied email address
341
	function validate_email($email) {
342 1373 Luisehahne
		if(function_exists('idn_to_ascii')){ // use pear if available
343
			$email = idn_to_ascii($email);
344
		}else {
345
			require_once(WB_PATH.'/include/idna_convert/idna_convert.class.php');
346
			$IDN = new idna_convert();
347
			$email = $IDN->encode($email);
348
			unset($IDN);
349 1365 Luisehahne
		}
350 1373 Luisehahne
		return !(filter_var($email, FILTER_VALIDATE_EMAIL) == false);
351 1365 Luisehahne
	}
352 1373 Luisehahne
*/
353 1365 Luisehahne
	// Print a success message which then automatically redirects the user to another page
354 1373 Luisehahne
	function print_success( $message, $redirect = 'index.php' ) {
355 1365 Luisehahne
	    global $TEXT;
356 1443 Luisehahne
        if(is_array($message)) {
357
           $message = implode ('<br />',$message);
358
        }
359 1373 Luisehahne
	    // fetch redirect timer for sucess messages from settings table
360 1397 Luisehahne
	    $redirect_timer = ((defined( 'REDIRECT_TIMER' )) && (REDIRECT_TIMER <= 10000)) ? REDIRECT_TIMER : 0;
361 1365 Luisehahne
	    // add template variables
362
	    $tpl = new Template( THEME_PATH.'/templates' );
363
	    $tpl->set_file( 'page', 'success.htt' );
364
	    $tpl->set_block( 'page', 'main_block', 'main' );
365 1373 Luisehahne
	    $tpl->set_block( 'main_block', 'show_redirect_block', 'show_redirect' );
366
	    $tpl->set_var( 'MESSAGE', $message );
367
	    $tpl->set_var( 'REDIRECT', $redirect );
368
	    $tpl->set_var( 'REDIRECT_TIMER', $redirect_timer );
369 1372 Luisehahne
	    $tpl->set_var( 'NEXT', $TEXT['NEXT'] );
370
	    $tpl->set_var( 'BACK', $TEXT['BACK'] );
371 1397 Luisehahne
	    if ($redirect_timer == -1) {
372 1365 Luisehahne
	        $tpl->set_block( 'show_redirect', '' );
373 1373 Luisehahne
	    }
374
	    else {
375 1365 Luisehahne
	        $tpl->parse( 'show_redirect', 'show_redirect_block', true );
376
	    }
377
	    $tpl->parse( 'main', 'main_block', false );
378
	    $tpl->pparse( 'output', 'page' );
379
	}
380
381
	// Print an error message
382 1373 Luisehahne
	function print_error($message, $link = 'index.php', $auto_footer = true) {
383 1365 Luisehahne
		global $TEXT;
384 1443 Luisehahne
        if(is_array($message)) {
385
           $message = implode ('<br />',$message);
386
        }
387 1365 Luisehahne
		$success_template = new Template(THEME_PATH.'/templates');
388
		$success_template->set_file('page', 'error.htt');
389
		$success_template->set_block('page', 'main_block', 'main');
390
		$success_template->set_var('MESSAGE', $message);
391
		$success_template->set_var('LINK', $link);
392
		$success_template->set_var('BACK', $TEXT['BACK']);
393
		$success_template->parse('main', 'main_block', false);
394
		$success_template->pparse('output', 'page');
395
		if ( $auto_footer == true ) {
396
			if ( method_exists($this, "print_footer") ) {
397
				$this->print_footer();
398
			}
399
		}
400
		exit();
401
	}
402
403
	// Validate send email
404
	function mail($fromaddress, $toaddress, $subject, $message, $fromname='') {
405 1487 DarkViper
/*
406
	INTEGRATED OPEN SOURCE PHPMAILER CLASS FOR SMTP SUPPORT AND MORE
407
	SOME SERVICE PROVIDERS DO NOT SUPPORT SENDING MAIL VIA PHP AS IT DOES NOT PROVIDE SMTP AUTHENTICATION
408
	NEW WBMAILER CLASS IS ABLE TO SEND OUT MESSAGES USING SMTP WHICH RESOLVE THESE ISSUE (C. Sommer)
409 1365 Luisehahne
410 1487 DarkViper
	NOTE:
411
	To use SMTP for sending out mails, you have to specify the SMTP host of your domain
412
	via the Settings panel in the backend of Website Baker
413
*/
414 1365 Luisehahne
415
		$fromaddress = preg_replace('/[\r\n]/', '', $fromaddress);
416
		$toaddress = preg_replace('/[\r\n]/', '', $toaddress);
417
		$subject = preg_replace('/[\r\n]/', '', $subject);
418 1463 Luisehahne
		// $message_alt = $message;
419
		// $message = preg_replace('/[\r\n]/', '<br \>', $message);
420
421 1365 Luisehahne
		// create PHPMailer object and define default settings
422
		$myMail = new wbmailer();
423
		// set user defined from address
424
		if ($fromaddress!='') {
425 1487 DarkViper
			if($fromname!='') $myMail->FromName = $fromname;  // FROM-NAME
426
			$myMail->From = $fromaddress;                     // FROM:
427
			$myMail->AddReplyTo($fromaddress);                // REPLY TO:
428 1365 Luisehahne
		}
429
		// define recepient and information to send out
430 1487 DarkViper
		$myMail->AddAddress($toaddress);                      // TO:
431
		$myMail->Subject = $subject;                          // SUBJECT
432
		$myMail->Body = nl2br($message);                      // CONTENT (HTML)
433
		$myMail->AltBody = strip_tags($message);              // CONTENT (TEXT)
434 1365 Luisehahne
		// check if there are any send mail errors, otherwise say successful
435
		if (!$myMail->Send()) {
436
			return false;
437
		} else {
438
			return true;
439
		}
440
	}
441
442
}