Project

General

Profile

1
<?php
2

    
3
// $Id: class.frontend.php 106 2005-09-15 19:15:43Z 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 ($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->page['page_title']));
147
			$this->page_title=PAGE_TITLE;
148
			// Menu Title
149
			$menu_title = ($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
		global $wb;
282
	   if (!isset($wb->menu_number)) {
283
	   	$wb->menu_number = 1;
284
	   }
285
	   if (!isset($wb->menu_start_level)) {
286
	   	$wb->menu_start_level = 0;
287
	   }
288
	   if (!isset($wb->menu_recurse)) {
289
	   	$wb->menu_recurse = -1;
290
	   }
291
	   if (!isset($wb->menu_collapse)) {
292
	   	$wb->menu_collapse = true;
293
	   }
294
	   if (!isset($wb->menu_item_template)) {
295
	   	$wb->menu_item_template = '<li><span[class]>[a][menu_title][/a]</span>';
296
	   }
297
	   if (!isset($wb->menu_item_footer)) {
298
	   	$wb->menu_item_footer = '</li>';
299
	   }
300
	   if (!isset($wb->menu_header)) {
301
	   	$wb->menu_header = '<ul>';
302
	   }
303
	   if (!isset($wb->menu_footer)) {
304
	   	$wb->menu_footer = '<ul>';
305
	   }
306
	   if (!isset($wb->menu_default_class)) {
307
	   	$wb->menu_default_class = ' class="menu_default"';
308
	   }
309
	   if (!isset($wb->menu_current_class)) {
310
	   	$wb->menu_current_class = ' class="menu_current"';
311
	   }
312
       if (!isset($wb->menu_parent)) {
313
     	$wb->menu_parent = 0;
314
	   }
315
	   $wb->show_menu();
316
	   if ($start_level>0) {
317
	       $key_array=array_keys($wb->page_trail);
318
	       $real_start=$key_array[$start_level-1];
319
	       if (isset($real_start))
320
	       {
321
	       	$wb->menu_parent=$real_start;
322
	        $wb->show_menu();
323
	       }
324
	       return;
325
	   }
326

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

    
381

    
382
	// Function to show the "Under Construction" page
383
	function print_under_construction() {
384
		global $MESSAGE;
385
		require_once(WB_PATH.'/languages/'.DEFAULT_LANGUAGE.'.php');
386
		echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
387
		<head><title>'.$MESSAGE['GENERIC']['WEBSITE_UNDER_CONTRUCTION'].'</title>
388
		<style type="text/css"><!-- body { font-family: Verdana, Arial, Helvetica, sans-serif;
389
		font-size: 12px; color: #000000;	background-color: #FFFFFF;	margin: 20px; text-align: center; }
390
		h1 { margin: 0; padding: 0; }--></style></head><body>
391
		<h1>'.$MESSAGE['GENERIC']['WEBSITE_UNDER_CONTRUCTION'];'.</h1><br />
392
		'.$MESSAGE['GENERIC']['PLEASE_CHECK_BACK_SOON'].'</body></html>';
393
	}
394
}
395

    
396
?>
(3-3/11)