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 page_select() {
62
		global $page_id,$no_intro;
63 95 stefan
		global $database;
64 5 stefan
		// 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 106 stefan
				echo ($content);
75 5 stefan
				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 51 stefan
				$this->default_page_id = $fetch_default['page_id'];
93 5 stefan
				// Check if we should redirect or include page inline
94
				if(HOMEPAGE_REDIRECTION) {
95
					// Redirect to page
96
					header("Location: ".page_link($this->default_link));
97
					exit();
98
				} else {
99
					// Include page inline
100 51 stefan
					$this->page_id = $this->default_page_id;
101 5 stefan
				}
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 51 stefan
	 		$this->default_link = $fetch_default['link'];
114
			$this->default_page_id = $fetch_default['page_id'];
115 5 stefan
		}
116
		return true;
117
	}
118
119
	function get_page_details() {
120 95 stefan
		global $database;
121 5 stefan
	    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
				require_once(WB_PATH.'/framework/functions.php');
135
				header('Location: '.page_link($this->page['link']).'?lang='.$this->page['language']);
136
				exit();
137
			}
138
			// Begin code to set details as either variables of constants
139
			// Page ID
140
			define('PAGE_ID', $this->page['page_id']);
141
			// Page Title
142 106 stefan
			define('PAGE_TITLE', ($this->page['page_title']));
143 5 stefan
			$this->page_title=PAGE_TITLE;
144
			// Menu Title
145 106 stefan
			$menu_title = ($this->page['menu_title']);
146 5 stefan
			if($menu_title != '') {
147
				define('MENU_TITLE', $menu_title);
148
			} else {
149
				define('MENU_TITLE', PAGE_TITLE);
150
			}
151
			$this->menu_title=MENU_TITLE;
152
			// Page parent
153
			define('PARENT', $this->page['parent']);
154
			$this->parent=$this->page['parent'];
155
			// Page root parent
156
			define('ROOT_PARENT', $this->page['root_parent']);
157
			$this->root_parent=$this->page['root_parent'];
158
			// Page level
159
			define('LEVEL', $this->page['level']);
160
			$this->level=$this->page['level'];
161
			// Page visibility
162
			define('VISIBILITY', $this->page['visibility']);
163
			$this->visibility=$this->page['visibility'];
164
			// Page trail
165
			foreach(explode(',', $this->page['page_trail']) AS $pid) {
166
				$this->page_trail[$pid]=$pid;
167
			}
168
			// Page description
169
			$this->page_description=$this->page['description'];
170
			// Page keywords
171
			$this->page_keywords=$this->page['keywords'];
172
			// Page link
173
			$this->link=$this->page_link($this->page['link']);
174
175
		// End code to set details as either variables of constants
176
		}
177
178
		// Figure out what template to use
179
		if(!defined('TEMPLATE')) {
180
			if(isset($this->page['template']) AND $this->page['template'] != '') {
181
				if(file_exists(WB_PATH.'/templates/'.$this->page['template'].'/index.php')) {
182
					define('TEMPLATE', $this->page['template']);
183
				} else {
184
					define('TEMPLATE', DEFAULT_TEMPLATE);
185
				}
186
			} else {
187
				define('TEMPLATE', DEFAULT_TEMPLATE);
188
			}
189
		}
190
		// Set the template dir
191
		define('TEMPLATE_DIR', WB_URL.'/templates/'.TEMPLATE);
192
193 95 stefan
		// Check if user is allowed to view this page
194 27 stefan
		if(VISIBILITY == 'private' OR VISIBILITY == 'registered') {
195 5 stefan
			// Check if the user is authenticated
196
			if($this->is_authenticated() == false) {
197
				// User needs to login first
198 43 stefan
				header("Location: ".WB_URL."/account/login".PAGE_EXTENSION.'?redirect='.$this->link);
199 5 stefan
			}
200
			// Check if we should show this page
201
			if($this->show_page($this->page) == false) {
202
				$this->page_access_denied=true;
203
			}
204 27 stefan
		} elseif(VISIBILITY == 'deleted' OR VISIBILITY == 'none') {
205 5 stefan
			// User isnt allowed on this page so tell them
206
			$this->page_access_denied=true;
207
		}
208 95 stefan
	}
209
210
	function get_website_settings() {
211
		global $database;
212
213
		// set visibility SQL code
214 27 stefan
		// never show no-vis, hidden or deleted pages
215
		$this->extra_where_sql = "visibility != 'none' AND visibility != 'hidden' AND visibility != 'deleted'";
216
		// Set extra private sql code
217
		if($this->is_authenticated()==false) {
218
			// if user is not authenticated, don't show private pages either
219
			$this->extra_where_sql .= " AND visibility != 'private'";
220
			// and 'registered' without frontend login doesn't make much sense!
221
			if (FRONTEND_LOGIN==false) {
222
				$this->extra_where_sql .= " AND visibility != 'registered'";
223 5 stefan
			}
224
		}
225
		$this->extra_where_sql .= $this->sql_where_language;
226
227 95 stefan
		// Work-out if any possible in-line search boxes should be shown
228
		if(SEARCH == 'public') {
229
			define('SHOW_SEARCH', true);
230
		} elseif(SEARCH == 'private' AND VISIBILITY == 'private') {
231
			define('SHOW_SEARCH', true);
232
		} elseif(SEARCH == 'private' AND $wb->is_authenticated() == true) {
233
			define('SHOW_SEARCH', true);
234
		} else {
235
			define('SHOW_SEARCH', false);
236 5 stefan
		}
237 95 stefan
		// Work-out if menu should be shown
238
		if(!defined('SHOW_MENU')) {
239
			define('SHOW_MENU', true);
240
		}
241
		// Work-out if login menu constants should be set
242
		if(FRONTEND_LOGIN) {
243
			// Set login menu constants
244
			define('LOGIN_URL', WB_URL.'/account/login'.PAGE_EXTENSION);
245
			define('LOGOUT_URL', WB_URL.'/account/logout'.PAGE_EXTENSION);
246
			define('FORGOT_URL', WB_URL.'/account/forgot'.PAGE_EXTENSION);
247
			define('PREFERENCES_URL', WB_URL.'/account/preferences'.PAGE_EXTENSION);
248
			define('SIGNUP_URL', WB_URL.'/account/signup'.PAGE_EXTENSION);
249
		}
250 5 stefan
	}
251
252
	function page_link($link){
253
		// Check for :// in the link (used in URL's)
254
		if(strstr($link, '://') == '') {
255
			return WB_URL.PAGES_DIRECTORY.$link.PAGE_EXTENSION;
256
		} else {
257
			return $link;
258
		}
259
	}
260
261
	function preprocess(&$content) {
262 95 stefan
		global $database;
263 5 stefan
		// Replace [wblink--PAGE_ID--] with real link
264
		$pattern = '/\[wblink(.+?)\]/s';
265
		preg_match_all($pattern,$content,$ids);
266
		foreach($ids[1] AS $page_id) {
267
			$pattern = '/\[wblink'.$page_id.'\]/s';
268
			// Get page link
269
			$get_link = $database->query("SELECT link FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id' LIMIT 1");
270
			$fetch_link = $get_link->fetchRow();
271
			$link = page_link($fetch_link['link']);
272
			$content = preg_replace($pattern,$link,$content);
273
		}
274
	}
275
276 20 stefan
	function menu() {
277 106 stefan
		global $wb;
278
	   if (!isset($wb->menu_number)) {
279
	   	$wb->menu_number = 1;
280 20 stefan
	   }
281 106 stefan
	   if (!isset($wb->menu_start_level)) {
282
	   	$wb->menu_start_level = 0;
283 20 stefan
	   }
284 106 stefan
	   if (!isset($wb->menu_recurse)) {
285
	   	$wb->menu_recurse = -1;
286 20 stefan
	   }
287 106 stefan
	   if (!isset($wb->menu_collapse)) {
288
	   	$wb->menu_collapse = true;
289 20 stefan
	   }
290 106 stefan
	   if (!isset($wb->menu_item_template)) {
291
	   	$wb->menu_item_template = '<li><span[class]>[a][menu_title][/a]</span>';
292 20 stefan
	   }
293 106 stefan
	   if (!isset($wb->menu_item_footer)) {
294
	   	$wb->menu_item_footer = '</li>';
295 20 stefan
	   }
296 106 stefan
	   if (!isset($wb->menu_header)) {
297
	   	$wb->menu_header = '<ul>';
298 20 stefan
	   }
299 106 stefan
	   if (!isset($wb->menu_footer)) {
300
	   	$wb->menu_footer = '<ul>';
301 20 stefan
	   }
302 106 stefan
	   if (!isset($wb->menu_default_class)) {
303
	   	$wb->menu_default_class = ' class="menu_default"';
304 20 stefan
	   }
305 106 stefan
	   if (!isset($wb->menu_current_class)) {
306
	   	$wb->menu_current_class = ' class="menu_current"';
307 20 stefan
	   }
308 106 stefan
       if (!isset($wb->menu_parent)) {
309
     	$wb->menu_parent = 0;
310 20 stefan
	   }
311 106 stefan
	   $wb->show_menu();
312 5 stefan
	   if ($start_level>0) {
313 106 stefan
	       $key_array=array_keys($wb->page_trail);
314 5 stefan
	       $real_start=$key_array[$start_level-1];
315
	       if (isset($real_start))
316
	       {
317 106 stefan
	       	$wb->menu_parent=$real_start;
318
	        $wb->show_menu();
319 20 stefan
	       }
320 5 stefan
	       return;
321
	   }
322 20 stefan
323
	}
324
325
	function show_menu() {
326 95 stefan
	   global $database;
327 106 stefan
	   global $wb;
328
	   if ($wb->menu_recurse==0)
329 20 stefan
	       return;
330 5 stefan
	   // Check if we should add menu number check to query
331 20 stefan
	   if($menu_parent == 0) {
332 106 stefan
	       $menu_number = "menu = '$wb->menu_number'";
333 5 stefan
	   } else {
334
	      $menu_number = '1';
335
	   }
336
	   // Query pages
337 27 stefan
	   $query_menu = $database->query("SELECT page_id,menu_title,page_title,link,target,level,visibility FROM ".
338 106 stefan
	TABLE_PREFIX."pages WHERE parent = '$wb->menu_parent' AND $menu_number AND $wb->extra_where_sql ORDER BY position ASC");
339 5 stefan
	   // Check if there are any pages to show
340
	   if($query_menu->numRows() > 0) {
341 20 stefan
	   	  // Print menu header
342 106 stefan
	   	  echo "\n".$wb->menu_header;
343 5 stefan
	      // Loop through pages
344
	      while($page = $query_menu->fetchRow()) {
345 20 stefan
	      	 // Check if this page should be shown
346 5 stefan
	         // Create vars
347
	         $vars = array('[class]','[a]', '[/a]', '[menu_title]', '[page_title]');
348
	         // Work-out class
349
	         if($page['page_id'] == PAGE_ID) {
350 106 stefan
	            $class = $wb->menu_current_class;
351 5 stefan
	         } else {
352 106 stefan
	            $class = $wb->menu_default_class;
353 5 stefan
	         }
354
	         // Check if link is same as first page link, and if so change to WB URL
355 106 stefan
	         if($page['link'] == $wb->default_link AND !INTRO_PAGE) {
356 5 stefan
	            $link = WB_URL;
357
	         } else {
358 106 stefan
	            $link = $wb->page_link($page['link']);
359 5 stefan
	         }
360
	         // Create values
361 106 stefan
	         $values = array($class,'<a href="'.$link.'" target="'.$page['target'].'" '.$class.'>', '</a>', ($page['menu_title']), ($page['page_title']));
362 5 stefan
	         // Replace vars with value and print
363 106 stefan
	         echo "\n".str_replace($vars, $values, $wb->menu_item_template);
364 5 stefan
	         // Generate sub-menu
365 106 stefan
	         if($wb->menu_collapse==false OR ($wb->menu_collapse==true AND isset($wb->page_trail[$page['page_id']]))) {
366
	            $wb->menu_recurse--;
367
	            $wb->menu_parent=$page['page_id'];
368
	            $wb->show_menu();
369 5 stefan
	         }
370 106 stefan
	         echo "\n".$wb->menu_item_footer;
371 5 stefan
	      }
372
	      // Print menu footer
373 106 stefan
	      echo "\n".$wb->menu_footer;
374 5 stefan
	   }
375
	}
376
377
378
	// Function to show the "Under Construction" page
379
	function print_under_construction() {
380
		global $MESSAGE;
381
		require_once(WB_PATH.'/languages/'.DEFAULT_LANGUAGE.'.php');
382
		echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
383
		<head><title>'.$MESSAGE['GENERIC']['WEBSITE_UNDER_CONTRUCTION'].'</title>
384
		<style type="text/css"><!-- body { font-family: Verdana, Arial, Helvetica, sans-serif;
385
		font-size: 12px; color: #000000;	background-color: #FFFFFF;	margin: 20px; text-align: center; }
386
		h1 { margin: 0; padding: 0; }--></style></head><body>
387
		<h1>'.$MESSAGE['GENERIC']['WEBSITE_UNDER_CONTRUCTION'];'.</h1><br />
388
		'.$MESSAGE['GENERIC']['PLEASE_CHECK_BACK_SOON'].'</body></html>';
389
	}
390
}
391
392
?>