Project

General

Profile

1 5 stefan
<?php
2
3 11 ryan
// $Id$
4
5 5 stefan
/*
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 18 stefan
if(!defined('WB_PATH')) {
33
	header('Location: ../index.php');
34
}
35 5 stefan
36
37
require_once(WB_PATH.'/framework/class.wb.php');
38
39
class frontend extends wb {
40
	// defaults
41 51 stefan
	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 55 stefan
	var $no_default_block_content=false;
45 5 stefan
46
	// page details
47
	// page database row
48
	var $page;
49
	var $page_id,$page_title,$menu_title,$parent,$root_parent,$level,$visibility;
50 95 stefan
	var $page_description,$page_keywords,$page_link;
51 5 stefan
	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 27 stefan
	var $extra_where_sql;
60 5 stefan
61
	function frontend() {
62
		$this->wb();
63
	}
64
65
	function page_select() {
66
		global $page_id,$no_intro;
67 95 stefan
		global $database;
68 5 stefan
		// 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 106 stefan
				echo ($content);
79 5 stefan
				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 51 stefan
				$this->default_page_id = $fetch_default['page_id'];
97 5 stefan
				// 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 51 stefan
					$this->page_id = $this->default_page_id;
105 5 stefan
				}
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 51 stefan
	 		$this->default_link = $fetch_default['link'];
118
			$this->default_page_id = $fetch_default['page_id'];
119 5 stefan
		}
120
		return true;
121
	}
122
123
	function get_page_details() {
124 95 stefan
		global $database;
125 5 stefan
	    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 106 stefan
			define('PAGE_TITLE', ($this->page['page_title']));
147 5 stefan
			$this->page_title=PAGE_TITLE;
148
			// Menu Title
149 106 stefan
			$menu_title = ($this->page['menu_title']);
150 5 stefan
			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 95 stefan
		// Check if user is allowed to view this page
198 27 stefan
		if(VISIBILITY == 'private' OR VISIBILITY == 'registered') {
199 5 stefan
			// Check if the user is authenticated
200
			if($this->is_authenticated() == false) {
201
				// User needs to login first
202 43 stefan
				header("Location: ".WB_URL."/account/login".PAGE_EXTENSION.'?redirect='.$this->link);
203 5 stefan
			}
204
			// Check if we should show this page
205
			if($this->show_page($this->page) == false) {
206
				$this->page_access_denied=true;
207
			}
208 27 stefan
		} elseif(VISIBILITY == 'deleted' OR VISIBILITY == 'none') {
209 5 stefan
			// User isnt allowed on this page so tell them
210
			$this->page_access_denied=true;
211
		}
212 95 stefan
	}
213
214
	function get_website_settings() {
215
		global $database;
216
217
		// set visibility SQL code
218 27 stefan
		// 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 5 stefan
			}
228
		}
229
		$this->extra_where_sql .= $this->sql_where_language;
230
231 95 stefan
		// 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 5 stefan
		}
241 95 stefan
		// 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 5 stefan
	}
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 95 stefan
		global $database;
267 5 stefan
		// 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 20 stefan
	function menu() {
281 106 stefan
		global $wb;
282
	   if (!isset($wb->menu_number)) {
283
	   	$wb->menu_number = 1;
284 20 stefan
	   }
285 106 stefan
	   if (!isset($wb->menu_start_level)) {
286
	   	$wb->menu_start_level = 0;
287 20 stefan
	   }
288 106 stefan
	   if (!isset($wb->menu_recurse)) {
289
	   	$wb->menu_recurse = -1;
290 20 stefan
	   }
291 106 stefan
	   if (!isset($wb->menu_collapse)) {
292
	   	$wb->menu_collapse = true;
293 20 stefan
	   }
294 106 stefan
	   if (!isset($wb->menu_item_template)) {
295
	   	$wb->menu_item_template = '<li><span[class]>[a][menu_title][/a]</span>';
296 20 stefan
	   }
297 106 stefan
	   if (!isset($wb->menu_item_footer)) {
298
	   	$wb->menu_item_footer = '</li>';
299 20 stefan
	   }
300 106 stefan
	   if (!isset($wb->menu_header)) {
301
	   	$wb->menu_header = '<ul>';
302 20 stefan
	   }
303 106 stefan
	   if (!isset($wb->menu_footer)) {
304
	   	$wb->menu_footer = '<ul>';
305 20 stefan
	   }
306 106 stefan
	   if (!isset($wb->menu_default_class)) {
307
	   	$wb->menu_default_class = ' class="menu_default"';
308 20 stefan
	   }
309 106 stefan
	   if (!isset($wb->menu_current_class)) {
310
	   	$wb->menu_current_class = ' class="menu_current"';
311 20 stefan
	   }
312 106 stefan
       if (!isset($wb->menu_parent)) {
313
     	$wb->menu_parent = 0;
314 20 stefan
	   }
315 106 stefan
	   $wb->show_menu();
316 5 stefan
	   if ($start_level>0) {
317 106 stefan
	       $key_array=array_keys($wb->page_trail);
318 5 stefan
	       $real_start=$key_array[$start_level-1];
319
	       if (isset($real_start))
320
	       {
321 106 stefan
	       	$wb->menu_parent=$real_start;
322
	        $wb->show_menu();
323 20 stefan
	       }
324 5 stefan
	       return;
325
	   }
326 20 stefan
327
	}
328
329
	function show_menu() {
330 95 stefan
	   global $database;
331 106 stefan
	   global $wb;
332
	   if ($wb->menu_recurse==0)
333 20 stefan
	       return;
334 5 stefan
	   // Check if we should add menu number check to query
335 20 stefan
	   if($menu_parent == 0) {
336 106 stefan
	       $menu_number = "menu = '$wb->menu_number'";
337 5 stefan
	   } else {
338
	      $menu_number = '1';
339
	   }
340
	   // Query pages
341 27 stefan
	   $query_menu = $database->query("SELECT page_id,menu_title,page_title,link,target,level,visibility FROM ".
342 106 stefan
	TABLE_PREFIX."pages WHERE parent = '$wb->menu_parent' AND $menu_number AND $wb->extra_where_sql ORDER BY position ASC");
343 5 stefan
	   // Check if there are any pages to show
344
	   if($query_menu->numRows() > 0) {
345 20 stefan
	   	  // Print menu header
346 106 stefan
	   	  echo "\n".$wb->menu_header;
347 5 stefan
	      // Loop through pages
348
	      while($page = $query_menu->fetchRow()) {
349 20 stefan
	      	 // Check if this page should be shown
350 5 stefan
	         // Create vars
351
	         $vars = array('[class]','[a]', '[/a]', '[menu_title]', '[page_title]');
352
	         // Work-out class
353
	         if($page['page_id'] == PAGE_ID) {
354 106 stefan
	            $class = $wb->menu_current_class;
355 5 stefan
	         } else {
356 106 stefan
	            $class = $wb->menu_default_class;
357 5 stefan
	         }
358
	         // Check if link is same as first page link, and if so change to WB URL
359 106 stefan
	         if($page['link'] == $wb->default_link AND !INTRO_PAGE) {
360 5 stefan
	            $link = WB_URL;
361
	         } else {
362 106 stefan
	            $link = $wb->page_link($page['link']);
363 5 stefan
	         }
364
	         // Create values
365 106 stefan
	         $values = array($class,'<a href="'.$link.'" target="'.$page['target'].'" '.$class.'>', '</a>', ($page['menu_title']), ($page['page_title']));
366 5 stefan
	         // Replace vars with value and print
367 106 stefan
	         echo "\n".str_replace($vars, $values, $wb->menu_item_template);
368 5 stefan
	         // Generate sub-menu
369 106 stefan
	         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 5 stefan
	         }
374 106 stefan
	         echo "\n".$wb->menu_item_footer;
375 5 stefan
	      }
376
	      // Print menu footer
377 106 stefan
	      echo "\n".$wb->menu_footer;
378 5 stefan
	   }
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
?>