Project

General

Profile

1
<?php
2

    
3
// $Id: class.frontend.php 95 2005-09-12 23:08:46Z stefan $
4

    
5
/*
6

    
7
 Website Baker Project <http://www.websitebaker.org/>
8
 Copyright (C) 2004-2005, Ryan Djurovich
9

    
10
 Website Baker is free software; you can redistribute it and/or modify
11
 it under the terms of the GNU General Public License as published by
12
 the Free Software Foundation; either version 2 of the License, or
13
 (at your option) any later version.
14

    
15
 Website Baker 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.
19

    
20
 You should have received a copy of the GNU General Public License
21
 along with Website Baker; if not, write to the Free Software
22
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23

    
24
*/
25

    
26
/*
27

    
28
Frontend class
29

    
30
*/
31

    
32
if(!defined('WB_PATH')) {
33
	header('Location: ../index.php');
34
}
35

    
36

    
37
require_once(WB_PATH.'/framework/class.wb.php');
38

    
39
class frontend extends wb {
40
	// defaults
41
	var $default_link,$default_page_id;
42
	// when multiple blocks are used, show home page blocks on 
43
	// pages where no content is defined (search, login, ...)
44
	var $no_default_block_content=false;
45

    
46
	// page details
47
	// page database row
48
	var $page;
49
	var $page_id,$page_title,$menu_title,$parent,$root_parent,$level,$visibility;
50
	var $page_description,$page_keywords,$page_link;
51
	var $page_trail=array();
52
	
53
	var $page_access_denied;
54
	
55
	// website settings
56
	var $website_title,$website_description,$website_keywords,$website_header,$website_footer;
57

    
58
	// ugly database stuff
59
	var $extra_where_sql;
60

    
61
	function frontend() {
62
		$this->wb();
63
	}
64
	
65
	function page_select() {
66
		global $page_id,$no_intro;
67
		global $database;
68
		// We have no page id and are supposed to show the intro page
69
		if((INTRO_PAGE AND !isset($no_intro)) AND (!isset($page_id) OR !is_numeric($page_id))) {
70
			// Since we have no page id check if we should go to intro page or default page
71
			// Get intro page content
72
			$filename = WB_PATH.PAGES_DIRECTORY.'/intro.php';
73
			if(file_exists($filename)) {
74
				$handle = fopen($filename, "r");
75
				$content = fread($handle, filesize($filename));
76
				fclose($handle);
77
				$this->preprocess($content);
78
				echo $this->strip_slashes_dummy($content);
79
				return false;
80
			}
81
		}
82
		// Check if we should add page language sql code
83
		if(PAGE_LANGUAGES) {
84
			$this->sql_where_language = " AND language = '".LANGUAGE."'";
85
		}
86
		// Get default page
87
		// Check for a page id
88
		$query_default = "SELECT page_id,link FROM ".TABLE_PREFIX."pages WHERE parent = '0' AND visibility = 'public'$this->sql_where_language ORDER BY position ASC LIMIT 1";
89
		$get_default = $database->query($query_default);
90
		$default_num_rows = $get_default->numRows();
91
		if(!isset($page_id) OR !is_numeric($page_id)){
92
			// Go to or show default page
93
			if($default_num_rows > 0) {
94
				$fetch_default = $get_default->fetchRow();
95
				$this->default_link = $fetch_default['link'];
96
				$this->default_page_id = $fetch_default['page_id'];
97
				// Check if we should redirect or include page inline
98
				if(HOMEPAGE_REDIRECTION) {
99
					// Redirect to page
100
					header("Location: ".page_link($this->default_link));
101
					exit();
102
				} else {
103
					// Include page inline
104
					$this->page_id = $this->default_page_id;
105
				}
106
			} else {
107
		   		// No pages have been added, so print under construction page
108
				$this->print_under_construction();
109
				exit();
110
			}
111
		} else {
112
			$this->page_id=$page_id;
113
		}
114
		// Get default page link
115
		if(!isset($fetch_default)) {
116
		  	$fetch_default = $get_default->fetchRow();
117
	 		$this->default_link = $fetch_default['link'];
118
			$this->default_page_id = $fetch_default['page_id'];
119
		}
120
		return true;
121
	}
122

    
123
	function get_page_details() {
124
		global $database;
125
	    if($this->page_id != 0) {
126
			// Query page details
127
			$query_page = "SELECT * FROM ".TABLE_PREFIX."pages WHERE page_id = '{$this->page_id}'";
128
			$get_page = $database->query($query_page);
129
			// Make sure page was found in database
130
			if($get_page->numRows() == 0) {
131
				// Print page not found message
132
				exit("Page not found");
133
			}
134
			// Fetch page details
135
			$this->page = $get_page->fetchRow();
136
			// Check if the page language is also the selected language. If not, send headers again.
137
			if ($this->page['language']!=LANGUAGE) {
138
				require_once(WB_PATH.'/framework/functions.php');
139
				header('Location: '.page_link($this->page['link']).'?lang='.$this->page['language']);
140
				exit();
141
			}
142
			// Begin code to set details as either variables of constants
143
			// Page ID
144
			define('PAGE_ID', $this->page['page_id']);
145
			// Page Title
146
			define('PAGE_TITLE', $this->strip_slashes_dummy($this->page['page_title']));
147
			$this->page_title=PAGE_TITLE;
148
			// Menu Title
149
			$menu_title = $this->strip_slashes_dummy($this->page['menu_title']);
150
			if($menu_title != '') {
151
				define('MENU_TITLE', $menu_title);
152
			} else {
153
				define('MENU_TITLE', PAGE_TITLE);
154
			}
155
			$this->menu_title=MENU_TITLE;
156
			// Page parent
157
			define('PARENT', $this->page['parent']);
158
			$this->parent=$this->page['parent'];
159
			// Page root parent
160
			define('ROOT_PARENT', $this->page['root_parent']);
161
			$this->root_parent=$this->page['root_parent'];
162
			// Page level
163
			define('LEVEL', $this->page['level']);
164
			$this->level=$this->page['level'];
165
			// Page visibility
166
			define('VISIBILITY', $this->page['visibility']);
167
			$this->visibility=$this->page['visibility'];
168
			// Page trail
169
			foreach(explode(',', $this->page['page_trail']) AS $pid) {
170
				$this->page_trail[$pid]=$pid;
171
			}
172
			// Page description
173
			$this->page_description=$this->page['description'];
174
			// Page keywords
175
			$this->page_keywords=$this->page['keywords'];
176
			// Page link
177
			$this->link=$this->page_link($this->page['link']);
178

    
179
		// End code to set details as either variables of constants
180
		}
181

    
182
		// Figure out what template to use
183
		if(!defined('TEMPLATE')) {
184
			if(isset($this->page['template']) AND $this->page['template'] != '') {
185
				if(file_exists(WB_PATH.'/templates/'.$this->page['template'].'/index.php')) {
186
					define('TEMPLATE', $this->page['template']);
187
				} else {
188
					define('TEMPLATE', DEFAULT_TEMPLATE);
189
				}
190
			} else {
191
				define('TEMPLATE', DEFAULT_TEMPLATE);
192
			}
193
		}
194
		// Set the template dir
195
		define('TEMPLATE_DIR', WB_URL.'/templates/'.TEMPLATE);
196

    
197
		// Check if user is allowed to view this page
198
		if(VISIBILITY == 'private' OR VISIBILITY == 'registered') {
199
			// Check if the user is authenticated
200
			if($this->is_authenticated() == false) {
201
				// User needs to login first
202
				header("Location: ".WB_URL."/account/login".PAGE_EXTENSION.'?redirect='.$this->link);
203
			}
204
			// Check if we should show this page
205
			if($this->show_page($this->page) == false) {
206
				$this->page_access_denied=true;
207
			}
208
		} elseif(VISIBILITY == 'deleted' OR VISIBILITY == 'none') {
209
			// User isnt allowed on this page so tell them
210
			$this->page_access_denied=true;
211
		}
212
	}
213

    
214
	function get_website_settings() {
215
		global $database;
216

    
217
		// set visibility SQL code
218
		// never show no-vis, hidden or deleted pages
219
		$this->extra_where_sql = "visibility != 'none' AND visibility != 'hidden' AND visibility != 'deleted'";
220
		// Set extra private sql code
221
		if($this->is_authenticated()==false) {
222
			// if user is not authenticated, don't show private pages either
223
			$this->extra_where_sql .= " AND visibility != 'private'";
224
			// and 'registered' without frontend login doesn't make much sense!
225
			if (FRONTEND_LOGIN==false) {
226
				$this->extra_where_sql .= " AND visibility != 'registered'";
227
			}
228
		}
229
		$this->extra_where_sql .= $this->sql_where_language;
230

    
231
		// Work-out if any possible in-line search boxes should be shown
232
		if(SEARCH == 'public') {
233
			define('SHOW_SEARCH', true);
234
		} elseif(SEARCH == 'private' AND VISIBILITY == 'private') {
235
			define('SHOW_SEARCH', true);
236
		} elseif(SEARCH == 'private' AND $wb->is_authenticated() == true) {
237
			define('SHOW_SEARCH', true);
238
		} else {
239
			define('SHOW_SEARCH', false);
240
		}
241
		// Work-out if menu should be shown
242
		if(!defined('SHOW_MENU')) {
243
			define('SHOW_MENU', true);
244
		}
245
		// Work-out if login menu constants should be set
246
		if(FRONTEND_LOGIN) {
247
			// Set login menu constants
248
			define('LOGIN_URL', WB_URL.'/account/login'.PAGE_EXTENSION);
249
			define('LOGOUT_URL', WB_URL.'/account/logout'.PAGE_EXTENSION);
250
			define('FORGOT_URL', WB_URL.'/account/forgot'.PAGE_EXTENSION);
251
			define('PREFERENCES_URL', WB_URL.'/account/preferences'.PAGE_EXTENSION);
252
			define('SIGNUP_URL', WB_URL.'/account/signup'.PAGE_EXTENSION);
253
		}
254
	}
255
	
256
	function page_link($link){
257
		// Check for :// in the link (used in URL's)
258
		if(strstr($link, '://') == '') {
259
			return WB_URL.PAGES_DIRECTORY.$link.PAGE_EXTENSION;
260
		} else {
261
			return $link;
262
		}
263
	}
264
	
265
	function preprocess(&$content) {
266
		global $database;
267
		// Replace [wblink--PAGE_ID--] with real link
268
		$pattern = '/\[wblink(.+?)\]/s';
269
		preg_match_all($pattern,$content,$ids);
270
		foreach($ids[1] AS $page_id) {
271
			$pattern = '/\[wblink'.$page_id.'\]/s';
272
			// Get page link
273
			$get_link = $database->query("SELECT link FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id' LIMIT 1");
274
			$fetch_link = $get_link->fetchRow();
275
			$link = page_link($fetch_link['link']);
276
			$content = preg_replace($pattern,$link,$content);
277
		}
278
	}
279
	
280
	function menu() {
281
	   if (!isset($this->menu_number)) {
282
	   	$this->menu_number = 1;
283
	   }
284
	   if (!isset($this->menu_start_level)) {
285
	   	$this->menu_start_level = 0;
286
	   }
287
	   if (!isset($this->menu_recurse)) {
288
	   	$this->menu_recurse = -1;
289
	   }
290
	   if (!isset($this->menu_collapse)) {
291
	   	$this->menu_collapse = true;
292
	   }
293
	   if (!isset($this->menu_item_template)) {
294
	   	$this->menu_item_template = '<li><span[class]>[a][menu_title][/a]</span>';
295
	   }
296
	   if (!isset($this->menu_item_footer)) {
297
	   	$this->menu_item_footer = '</li>';
298
	   }
299
	   if (!isset($this->menu_header)) {
300
	   	$this->menu_header = '<ul>';
301
	   }
302
	   if (!isset($this->menu_footer)) {
303
	   	$this->menu_footer = '<ul>';
304
	   }
305
	   if (!isset($this->menu_default_class)) {
306
	   	$this->menu_default_class = ' class="menu_default"';
307
	   }
308
	   if (!isset($this->menu_current_class)) {
309
	   	$this->menu_current_class = ' class="menu_current"';
310
	   }
311
       if (!isset($this->menu_parent)) {
312
     	$this->menu_parent = 0;
313
	   }
314
	   $this->show_menu();
315
	   if ($start_level>0) {
316
	       $key_array=array_keys($this->page_trail);
317
	       $real_start=$key_array[$start_level-1];
318
	       if (isset($real_start))
319
	       {
320
	       		$this->menu_parent=$real_start;
321
	          $this->show_menu();
322
	       }
323
	       return;
324
	   }
325

    
326
	}
327
	
328
	function show_menu() {
329
	   global $database;
330
	   if ($this->menu_recurse==0)
331
	       return;
332
	   // Check if we should add menu number check to query
333
	   if($menu_parent == 0) {
334
	       $menu_number = "menu = '$this->menu_number'";
335
	   } else {
336
	      $menu_number = '1';
337
	   }
338
	   // Query pages
339
	   $query_menu = $database->query("SELECT page_id,menu_title,page_title,link,target,level,visibility FROM ".
340
	TABLE_PREFIX."pages WHERE parent = '$this->menu_parent' AND $menu_number AND $this->extra_where_sql ORDER BY position ASC");
341
	   // Check if there are any pages to show
342
	   if($query_menu->numRows() > 0) {
343
	   	  // Print menu header
344
	   	  echo "\n".$this->menu_header;
345
	      // Loop through pages
346
	      while($page = $query_menu->fetchRow()) {
347
	      	 // Check if this page should be shown
348
	         // Create vars
349
	         $vars = array('[class]','[a]', '[/a]', '[menu_title]', '[page_title]');
350
	         // Work-out class
351
	         if($page['page_id'] == PAGE_ID) {
352
	            $class = $this->menu_current_class;
353
	         } else {
354
	            $class = $this->menu_default_class;
355
	         }
356
	         // Check if link is same as first page link, and if so change to WB URL
357
	         if($page['link'] == $this->default_link AND !INTRO_PAGE) {
358
	            $link = WB_URL;
359
	         } else {
360
	            $link = $this->page_link($page['link']);
361
	         }
362
	         // Create values
363
	         $values = array($class,'<a href="'.$link.'" target="'.$page['target'].'" '.$class.'>', '</a>', $this->strip_slashes_dummy($page['menu_title']), $this->strip_slashes_dummy($page['page_title']));
364
	         // Replace vars with value and print
365
	         echo "\n".str_replace($vars, $values, $this->menu_item_template);
366
	         // Generate sub-menu
367
	         if($this->menu_collapse==false OR ($this->menu_collapse==true AND isset($this->page_trail[$page['page_id']]))) {
368
	            $this->menu_recurse--;
369
	            $this->menu_parent=$page['page_id'];
370
	            $this->show_menu();
371
	         }
372
	         echo "\n".$this->menu_item_footer;
373
	      }
374
	      // Print menu footer
375
	      echo "\n".$this->menu_footer;
376
	   }
377
	}
378

    
379
	function content($block = 1) {
380
		// Get outside objects
381
		global $TEXT,$MENU,$HEADING,$MESSAGE;
382
		global $globals;
383
		global $database;
384
		$admin = & $this;
385
		if ($this->page_access_denied==true) {
386
            echo $MESSAGE['FRONTEND']['SORRY_NO_VIEWING_PERMISSIONS'];
387
			exit();
388
		}
389
		if(isset($globals) AND is_array($globals)) { foreach($globals AS $global_name) { global $$global_name; } }
390
		// Make sure block is numeric
391
		if(!is_numeric($block)) { $block = 1; }
392
		// Include page content
393
		if(!defined('PAGE_CONTENT') OR $block!=1) {
394
			if ($this->page_id==0) {
395
				if ($this->default_block_content=='none') {
396
					return;
397
				}
398
				if (is_numeric($this->default_block_content)) {
399
					$page_id=$this->default_block_content;
400
				} else {
401
					$page_id=$this->default_page-id;
402
				}				
403
			} else {
404
				$page_id=$this->page_id;
405
			}
406
			// First get all sections for this page
407
			$query_sections = $database->query("SELECT section_id,module FROM ".TABLE_PREFIX."sections WHERE page_id = '".$page_id."' AND block = '$block' ORDER BY position");
408
			if($query_sections->numRows() > 0) {
409
				// Loop through them and include there modules file
410
				while($section = $query_sections->fetchRow()) {
411
					$section_id = $section['section_id'];
412
					$module = $section['module'];
413
					require(WB_PATH.'/modules/'.$module.'/view.php');
414
				}
415
			}
416
		} else {
417
			require(PAGE_CONTENT);
418
		}
419
	}
420

    
421
	function breadcrumbs($sep=' > ',$tier=1,$links=true) {
422
		$page_id=&$this->page_id;
423
		if ($page_id!=0)
424
		{
425
	 		global $database;
426
			$bca=&$this->page_trail;
427
			if (sizeof($bca)==0)
428
			        create_breadcrumbs($page_id);
429
			$counter=0;
430
			foreach ($bca as $temp)
431
			{
432
		        if ($counter>=(tier-1));
433
		        {
434
					if ($counter>=$tier) echo $sep;
435
					$query_menu=$database->query("SELECT menu_title,link FROM ".TABLE_PREFIX."pages WHERE page_id=$temp");
436
					$page=$query_menu->fetchRow();
437
					if ($links==true AND $temp!=$page_id)
438
						echo '<a href="'.page_link($page['link']).'">'.$page['menu_title'].'</a>';
439
					else
440
					        echo stripslashes($page['menu_title']);
441
		        }
442
                $counter++;
443
			}
444
		}
445
	}
446

    
447
	// Function for page title
448
	function page_title($spacer = ' - ', $template = '[WEBSITE_TITLE][SPACER][PAGE_TITLE]') {
449
		$vars = array('[WEBSITE_TITLE]', '[PAGE_TITLE]', '[MENU_TITLE]', '[SPACER]');
450
		$values = array(WEBSITE_TITLE, PAGE_TITLE, MENU_TITLE, $spacer);
451
		echo str_replace($vars, $values, $template);
452
	}
453

    
454
	// Function for page description
455
	function page_description() {
456
		echo WEBSITE_DESCRIPTION;
457
	}
458
	// Function for page keywords
459
	function page_keywords() {
460
		echo WEBSITE_KEYWORDS;
461
	}
462
	// Function for page header
463
	function page_header($date_format = 'Y') {
464
		echo WEBSITE_HEADER;
465
	}
466

    
467
	// Function for page footer
468
	function page_footer($date_format = 'Y') {
469
		global $starttime;
470
   		$vars = array('[YEAR]', '[PROCESSTIME]');
471
   		$processtime=(microtime()>$starttime)?microtime()-$starttime:microtime()-$starttime+1;
472
		$values = array(date($date_format),$processtime);
473
		echo str_replace($vars, $values, WEBSITE_FOOTER);
474
	}
475

    
476
	// Function to show the "Under Construction" page
477
	function print_under_construction() {
478
		global $MESSAGE;
479
		require_once(WB_PATH.'/languages/'.DEFAULT_LANGUAGE.'.php');
480
		echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
481
		<head><title>'.$MESSAGE['GENERIC']['WEBSITE_UNDER_CONTRUCTION'].'</title>
482
		<style type="text/css"><!-- body { font-family: Verdana, Arial, Helvetica, sans-serif;
483
		font-size: 12px; color: #000000;	background-color: #FFFFFF;	margin: 20px; text-align: center; }
484
		h1 { margin: 0; padding: 0; }--></style></head><body>
485
		<h1>'.$MESSAGE['GENERIC']['WEBSITE_UNDER_CONTRUCTION'];'.</h1><br />
486
		'.$MESSAGE['GENERIC']['PLEASE_CHECK_BACK_SOON'].'</body></html>';
487
	}
488
}
489

    
490
?>
(3-3/10)