Project

General

Profile

1
<?php
2

    
3
// $Id: class.frontend.php 280 2006-01-15 09:07:24Z 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 $default_block_content=true;
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, $sql_where_language;
60

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

    
119
	function get_page_details() {
120
		global $database;
121
	    if($this->page_id != 0) {
122
			// Query page details
123
			$query_page = "SELECT * FROM ".TABLE_PREFIX."pages WHERE page_id = '{$this->page_id}'";
124
			$get_page = $database->query($query_page);
125
			// Make sure page was found in database
126
			if($get_page->numRows() == 0) {
127
				// Print page not found message
128
				exit("Page not found");
129
			}
130
			// Fetch page details
131
			$this->page = $get_page->fetchRow();
132
			// Check if the page language is also the selected language. If not, send headers again.
133
			if ($this->page['language']!=LANGUAGE) {
134
				header('Location: '.$this->page_link($this->page['link']).'?lang='.$this->page['language']);
135
				exit();
136
			}
137
			// Begin code to set details as either variables of constants
138
			// Page ID
139
			define('PAGE_ID', $this->page['page_id']);
140
			// Page Title
141
			define('PAGE_TITLE', ($this->page['page_title']));
142
			$this->page_title=PAGE_TITLE;
143
			// Menu Title
144
			$menu_title = ($this->page['menu_title']);
145
			if($menu_title != '') {
146
				define('MENU_TITLE', $menu_title);
147
			} else {
148
				define('MENU_TITLE', PAGE_TITLE);
149
			}
150
			$this->menu_title=MENU_TITLE;
151
			// Page parent
152
			define('PARENT', $this->page['parent']);
153
			$this->parent=$this->page['parent'];
154
			// Page root parent
155
			define('ROOT_PARENT', $this->page['root_parent']);
156
			$this->root_parent=$this->page['root_parent'];
157
			// Page level
158
			define('LEVEL', $this->page['level']);
159
			$this->level=$this->page['level'];
160
			// Page visibility
161
			define('VISIBILITY', $this->page['visibility']);
162
			$this->visibility=$this->page['visibility'];
163
			// Page trail
164
			foreach(explode(',', $this->page['page_trail']) AS $pid) {
165
				$this->page_trail[$pid]=$pid;
166
			}
167
			// Page description
168
			$this->page_description=$this->page['description'];
169
			if($this->page_description != '') {
170
				define('PAGE_DESCRIPTION', $this->page_description);
171
			} else {
172
				define('PAGE_DESCRIPTION', WEBSITE_DESCRIPTION);
173
			}
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 preprocess(&$content) {
257
		global $database;
258
		// Replace [wblink--PAGE_ID--] with real link
259
		$pattern = '/\[wblink(.+?)\]/s';
260
		preg_match_all($pattern,$content,$ids);
261
		foreach($ids[1] AS $page_id) {
262
			$pattern = '/\[wblink'.$page_id.'\]/s';
263
			// Get page link
264
			$get_link = $database->query("SELECT link FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id' LIMIT 1");
265
			$fetch_link = $get_link->fetchRow();
266
			$link = $this->page_link($fetch_link['link']);
267
			$content = preg_replace($pattern,$link,$content);
268
		}
269
	}
270
	
271
	function menu() {
272
		global $wb;
273
	   if (!isset($wb->menu_number)) {
274
	   	$wb->menu_number = 1;
275
	   }
276
	   if (!isset($wb->menu_start_level)) {
277
	   	$wb->menu_start_level = 0;
278
	   }
279
	   if (!isset($wb->menu_recurse)) {
280
	   	$wb->menu_recurse = -1;
281
	   }
282
	   if (!isset($wb->menu_collapse)) {
283
	   	$wb->menu_collapse = true;
284
	   }
285
	   if (!isset($wb->menu_item_template)) {
286
	   	$wb->menu_item_template = '<li><span[class]>[a][menu_title][/a]</span>';
287
	   }
288
	   if (!isset($wb->menu_item_footer)) {
289
	   	$wb->menu_item_footer = '</li>';
290
	   }
291
	   if (!isset($wb->menu_header)) {
292
	   	$wb->menu_header = '<ul>';
293
	   }
294
	   if (!isset($wb->menu_footer)) {
295
	   	$wb->menu_footer = '</ul>';
296
	   }
297
	   if (!isset($wb->menu_default_class)) {
298
	   	$wb->menu_default_class = ' class="menu_default"';
299
	   }
300
	   if (!isset($wb->menu_current_class)) {
301
	   	$wb->menu_current_class = ' class="menu_current"';
302
	   }
303
	   if (!isset($wb->menu_parent)) {
304
	   	$wb->menu_parent = 0;
305
	   }
306
	   $wb->show_menu();
307
	}
308
	
309
	function show_menu() {
310
	   global $database;
311
	   if ($this->menu_start_level>0) {
312
	       $key_array=array_keys($this->page_trail);
313
	       $real_start=$key_array[$this->menu_start_level-1];
314
	       if (isset($real_start)) {
315
	       	$this->menu_parent=$real_start;
316
		$this->menu_start_level=0;
317
	       } else {
318
	       	 return;
319
	       }
320
	   }
321
	   if ($this->menu_recurse==0)
322
	       return;
323
	   // Check if we should add menu number check to query
324
	   if($this->menu_parent == 0) {
325
	       $menu_number = "menu = '$this->menu_number'";
326
	   } else {
327
	      $menu_number = '1';
328
	   }
329
	   // Query pages
330
	   $query_menu = $database->query("SELECT page_id,menu_title,page_title,link,target,level,visibility FROM ".
331
	TABLE_PREFIX."pages WHERE parent = '$this->menu_parent' AND $menu_number AND $this->extra_where_sql ORDER BY position ASC");
332
	   // Check if there are any pages to show
333
	   if($query_menu->numRows() > 0) {
334
	   	  // Print menu header
335
	   	  echo "\n".$this->menu_header;
336
	      // Loop through pages
337
	      while($page = $query_menu->fetchRow()) {
338
	      	 // Check if this page should be shown
339
	         // Create vars
340
	         $vars = array('[class]','[a]', '[/a]', '[menu_title]', '[page_title]');
341
	         // Work-out class
342
	         if($page['page_id'] == PAGE_ID) {
343
	            $class = $this->menu_current_class;
344
	         } else {
345
	            $class = $this->menu_default_class;
346
	         }
347
	         // Check if link is same as first page link, and if so change to WB URL
348
	         if($page['link'] == $this->default_link AND !INTRO_PAGE) {
349
	            $link = WB_URL;
350
	         } else {
351
	            $link = $this->page_link($page['link']);
352
	         }
353
	         // Create values
354
	         $values = array($class,'<a href="'.$link.'" target="'.$page['target'].'" '.$class.'>', '</a>', ($page['menu_title']), ($page['page_title']));
355
	         // Replace vars with value and print
356
	         echo "\n".str_replace($vars, $values, $this->menu_item_template);
357
	         // Generate sub-menu
358
	         if($this->menu_collapse==false OR ($this->menu_collapse==true AND isset($this->page_trail[$page['page_id']]))) {
359
	            $this->menu_recurse--;
360
	            $this->menu_parent=$page['page_id'];
361
	            $this->show_menu();
362
	         }
363
	         echo "\n".$this->menu_item_footer;
364
	      }
365
	      // Print menu footer
366
	      echo "\n".$this->menu_footer;
367
	   }
368
	}
369

    
370

    
371
	// Function to show the "Under Construction" page
372
	function print_under_construction() {
373
		global $MESSAGE;
374
		require_once(WB_PATH.'/languages/'.DEFAULT_LANGUAGE.'.php');
375
		echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
376
		<head><title>'.$MESSAGE['GENERIC']['WEBSITE_UNDER_CONSTRUCTION'].'</title>
377
		<style type="text/css"><!-- body { font-family: Verdana, Arial, Helvetica, sans-serif;
378
		font-size: 12px; color: #000000;	background-color: #FFFFFF;	margin: 20px; text-align: center; }
379
		h1 { margin: 0; padding: 0; }--></style></head><body>
380
		<h1>'.$MESSAGE['GENERIC']['WEBSITE_UNDER_CONSTRUCTION'].'</h1><br />
381
		'.$MESSAGE['GENERIC']['PLEASE_CHECK_BACK_SOON'].'</body></html>';
382
	}
383
}
384

    
385
?>
(3-3/11)