Project

General

Profile

1 219 stefan
<?php
2
3
// $Id$
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 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 219 stefan
	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 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: ".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 51 stefan
	 		$this->default_link = $fetch_default['link'];
114 219 stefan
			$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
				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
			define('PAGE_TITLE', ($this->page['page_title']));
143
			$this->page_title=PAGE_TITLE;
144
			// Menu Title
145
			$menu_title = ($this->page['menu_title']);
146
			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
		// Check if user is allowed to view this page
194
		if(VISIBILITY == 'private' OR VISIBILITY == 'registered') {
195
			// Check if the user is authenticated
196
			if($this->is_authenticated() == false) {
197
				// User needs to login first
198
				header("Location: ".WB_URL."/account/login".PAGE_EXTENSION.'?redirect='.$this->link);
199
			}
200
			// Check if we should show this page
201
			if($this->show_page($this->page) == false) {
202
				$this->page_access_denied=true;
203
			}
204
		} elseif(VISIBILITY == 'deleted' OR VISIBILITY == 'none') {
205
			// User isnt allowed on this page so tell them
206
			$this->page_access_denied=true;
207
		}
208
	}
209
210
	function get_website_settings() {
211
		global $database;
212
213
		// set visibility SQL code
214
		// 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
			}
224
		}
225
		$this->extra_where_sql .= $this->sql_where_language;
226
227
		// 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
		}
237
		// 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
	}
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
		global $database;
263
		// 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
	function menu() {
277
		global $wb;
278
	   if (!isset($wb->menu_number)) {
279
	   	$wb->menu_number = 1;
280
	   }
281
	   if (!isset($wb->menu_start_level)) {
282
	   	$wb->menu_start_level = 0;
283
	   }
284
	   if (!isset($wb->menu_recurse)) {
285
	   	$wb->menu_recurse = -1;
286
	   }
287
	   if (!isset($wb->menu_collapse)) {
288
	   	$wb->menu_collapse = true;
289
	   }
290
	   if (!isset($wb->menu_item_template)) {
291
	   	$wb->menu_item_template = '<li><span[class]>[a][menu_title][/a]</span>';
292
	   }
293
	   if (!isset($wb->menu_item_footer)) {
294
	   	$wb->menu_item_footer = '</li>';
295
	   }
296
	   if (!isset($wb->menu_header)) {
297
	   	$wb->menu_header = '<ul>';
298
	   }
299
	   if (!isset($wb->menu_footer)) {
300
	   	$wb->menu_footer = '</ul>';
301
	   }
302
	   if (!isset($wb->menu_default_class)) {
303
	   	$wb->menu_default_class = ' class="menu_default"';
304
	   }
305
	   if (!isset($wb->menu_current_class)) {
306
	   	$wb->menu_current_class = ' class="menu_current"';
307
	   }
308 220 stefan
	   if (!isset($wb->menu_parent)) {
309
	   	$wb->menu_parent = 0;
310 219 stefan
	   }
311
	   $wb->show_menu();
312
	}
313
314
	function show_menu() {
315
	   global $database;
316 220 stefan
	   if ($this->menu_start_level>0) {
317
	       $key_array=array_keys($this->page_trail);
318
	       $real_start=$key_array[$this->menu_start_level-1];
319
	       if (isset($real_start)) {
320
	       	$this->menu_parent=$real_start;
321
		$this->menu_start_level=0;
322
	       } else {
323
	       	 return;
324
	       }
325
	   }
326
	   if ($this->menu_recurse==0)
327 219 stefan
	       return;
328
	   // Check if we should add menu number check to query
329 220 stefan
	   if($this->menu_parent == 0) {
330
	       $menu_number = "menu = '$this->menu_number'";
331 219 stefan
	   } else {
332
	      $menu_number = '1';
333
	   }
334
	   // Query pages
335
	   $query_menu = $database->query("SELECT page_id,menu_title,page_title,link,target,level,visibility FROM ".
336 220 stefan
	TABLE_PREFIX."pages WHERE parent = '$this->menu_parent' AND $menu_number AND $this->extra_where_sql ORDER BY position ASC");
337 219 stefan
	   // Check if there are any pages to show
338
	   if($query_menu->numRows() > 0) {
339
	   	  // Print menu header
340 220 stefan
	   	  echo "\n".$this->menu_header;
341 219 stefan
	      // Loop through pages
342
	      while($page = $query_menu->fetchRow()) {
343
	      	 // Check if this page should be shown
344
	         // Create vars
345
	         $vars = array('[class]','[a]', '[/a]', '[menu_title]', '[page_title]');
346
	         // Work-out class
347
	         if($page['page_id'] == PAGE_ID) {
348 220 stefan
	            $class = $this->menu_current_class;
349 219 stefan
	         } else {
350 220 stefan
	            $class = $this->menu_default_class;
351 219 stefan
	         }
352
	         // Check if link is same as first page link, and if so change to WB URL
353 220 stefan
	         if($page['link'] == $this->default_link AND !INTRO_PAGE) {
354 219 stefan
	            $link = WB_URL;
355
	         } else {
356 220 stefan
	            $link = $this->page_link($page['link']);
357 219 stefan
	         }
358
	         // Create values
359
	         $values = array($class,'<a href="'.$link.'" target="'.$page['target'].'" '.$class.'>', '</a>', ($page['menu_title']), ($page['page_title']));
360
	         // Replace vars with value and print
361 220 stefan
	         echo "\n".str_replace($vars, $values, $this->menu_item_template);
362 219 stefan
	         // Generate sub-menu
363 220 stefan
	         if($this->menu_collapse==false OR ($this->menu_collapse==true AND isset($this->page_trail[$page['page_id']]))) {
364
	            $this->menu_recurse--;
365
	            $this->menu_parent=$page['page_id'];
366
	            $this->show_menu();
367 219 stefan
	         }
368 220 stefan
	         echo "\n".$this->menu_item_footer;
369 219 stefan
	      }
370
	      // Print menu footer
371 220 stefan
	      echo "\n".$this->menu_footer;
372 219 stefan
	   }
373
	}
374
375
376
	// Function to show the "Under Construction" page
377
	function print_under_construction() {
378
		global $MESSAGE;
379
		require_once(WB_PATH.'/languages/'.DEFAULT_LANGUAGE.'.php');
380
		echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
381
		<head><title>'.$MESSAGE['GENERIC']['WEBSITE_UNDER_CONTRUCTION'].'</title>
382
		<style type="text/css"><!-- body { font-family: Verdana, Arial, Helvetica, sans-serif;
383
		font-size: 12px; color: #000000;	background-color: #FFFFFF;	margin: 20px; text-align: center; }
384
		h1 { margin: 0; padding: 0; }--></style></head><body>
385
		<h1>'.$MESSAGE['GENERIC']['WEBSITE_UNDER_CONTRUCTION'];'.</h1><br />
386
		'.$MESSAGE['GENERIC']['PLEASE_CHECK_BACK_SOON'].'</body></html>';
387
	}
388
}
389
390
?>