Project

General

Profile

1 433 doc
<?php
2
3
// $Id$
4
5
/*
6
7
 Website Baker Project <http://www.websitebaker.org/>
8
 Copyright (C) 2004-2007, 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
	exit(0);
35
}
36
37
38
require_once(WB_PATH.'/framework/class.wb.php');
39
40
class frontend extends wb {
41
	// defaults
42
	var $default_link,$default_page_id;
43
	// when multiple blocks are used, show home page blocks on
44
	// pages where no content is defined (search, login, ...)
45
	var $default_block_content=true;
46
47
	// page details
48
	// page database row
49
	var $page;
50
	var $page_id,$page_title,$menu_title,$parent,$root_parent,$level,$visibility;
51
	var $page_description,$page_keywords,$page_link;
52
	var $page_trail=array();
53
54
	var $page_access_denied;
55
56
	// website settings
57
	var $website_title,$website_description,$website_keywords,$website_header,$website_footer;
58
59
	// ugly database stuff
60
	var $extra_where_sql, $sql_where_language;
61
62
	function page_select() {
63
		global $page_id,$no_intro;
64
		global $database;
65
		// We have no page id and are supposed to show the intro page
66
		if((INTRO_PAGE AND !isset($no_intro)) AND (!isset($page_id) OR !is_numeric($page_id))) {
67
			// Since we have no page id check if we should go to intro page or default page
68
			// Get intro page content
69
			$filename = WB_PATH.PAGES_DIRECTORY.'/intro.php';
70
			if(file_exists($filename)) {
71 434 doc
				$handle = @fopen($filename, "r");
72
				$content = @fread($handle, filesize($filename));
73
				@fclose($handle);
74 433 doc
				$this->preprocess($content);
75 442 Ruebenwurz
				header("Location: pages/intro.php");   // send intro.php as header to allow parsing of php statements
76 433 doc
				echo ($content);
77
				return false;
78
			}
79
		}
80
		// Check if we should add page language sql code
81
		if(PAGE_LANGUAGES) {
82
			$this->sql_where_language = " AND language = '".LANGUAGE."'";
83
		}
84
		// Get default page
85
		// Check for a page id
86
		$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";
87
		$get_default = $database->query($query_default);
88
		$default_num_rows = $get_default->numRows();
89
		if(!isset($page_id) OR !is_numeric($page_id)){
90
			// Go to or show default page
91
			if($default_num_rows > 0) {
92
				$fetch_default = $get_default->fetchRow();
93
				$this->default_link = $fetch_default['link'];
94
				$this->default_page_id = $fetch_default['page_id'];
95
				// Check if we should redirect or include page inline
96
				if(HOMEPAGE_REDIRECTION) {
97
					// Redirect to page
98
					header("Location: ".$this->page_link($this->default_link));
99
					exit();
100
				} else {
101
					// Include page inline
102
					$this->page_id = $this->default_page_id;
103
				}
104
			} else {
105
		   		// No pages have been added, so print under construction page
106
				$this->print_under_construction();
107
				exit();
108
			}
109
		} else {
110
			$this->page_id=$page_id;
111
		}
112
		// Get default page link
113
		if(!isset($fetch_default)) {
114
		  	$fetch_default = $get_default->fetchRow();
115
	 		$this->default_link = $fetch_default['link'];
116
			$this->default_page_id = $fetch_default['page_id'];
117
		}
118
		return true;
119
	}
120
121
	function get_page_details() {
122
		global $database;
123
	    if($this->page_id != 0) {
124
			// Query page details
125
			$query_page = "SELECT * FROM ".TABLE_PREFIX."pages WHERE page_id = '{$this->page_id}'";
126
			$get_page = $database->query($query_page);
127
			// Make sure page was found in database
128
			if($get_page->numRows() == 0) {
129
				// Print page not found message
130
				exit("Page not found");
131
			}
132
			// Fetch page details
133
			$this->page = $get_page->fetchRow();
134
			// Check if the page language is also the selected language. If not, send headers again.
135
			if ($this->page['language']!=LANGUAGE) {
136
				header('Location: '.$this->page_link($this->page['link']).'?lang='.$this->page['language']);
137
				exit();
138
			}
139
			// Begin code to set details as either variables of constants
140
			// Page ID
141
			define('PAGE_ID', $this->page['page_id']);
142
			// Page Title
143 442 Ruebenwurz
			define('PAGE_TITLE', $this->page['page_title']);
144 433 doc
			$this->page_title=PAGE_TITLE;
145
			// Menu Title
146 442 Ruebenwurz
			$menu_title = $this->page['menu_title'];
147 433 doc
			if($menu_title != '') {
148
				define('MENU_TITLE', $menu_title);
149
			} else {
150
				define('MENU_TITLE', PAGE_TITLE);
151
			}
152
			$this->menu_title=MENU_TITLE;
153
			// Page parent
154
			define('PARENT', $this->page['parent']);
155
			$this->parent=$this->page['parent'];
156
			// Page root parent
157
			define('ROOT_PARENT', $this->page['root_parent']);
158
			$this->root_parent=$this->page['root_parent'];
159
			// Page level
160
			define('LEVEL', $this->page['level']);
161
			$this->level=$this->page['level'];
162
			// Page visibility
163
			define('VISIBILITY', $this->page['visibility']);
164
			$this->visibility=$this->page['visibility'];
165
			// Page trail
166
			foreach(explode(',', $this->page['page_trail']) AS $pid) {
167
				$this->page_trail[$pid]=$pid;
168
			}
169
			// Page description
170
			$this->page_description=$this->page['description'];
171
			if($this->page_description != '') {
172
				define('PAGE_DESCRIPTION', $this->page_description);
173
			} else {
174
				define('PAGE_DESCRIPTION', WEBSITE_DESCRIPTION);
175
			}
176
			// Page keywords
177
			$this->page_keywords=$this->page['keywords'];
178
			// Page link
179
			$this->link=$this->page_link($this->page['link']);
180
181
		// End code to set details as either variables of constants
182
		}
183
184
		// Figure out what template to use
185
		if(!defined('TEMPLATE')) {
186
			if(isset($this->page['template']) AND $this->page['template'] != '') {
187
				if(file_exists(WB_PATH.'/templates/'.$this->page['template'].'/index.php')) {
188
					define('TEMPLATE', $this->page['template']);
189
				} else {
190
					define('TEMPLATE', DEFAULT_TEMPLATE);
191
				}
192
			} else {
193
				define('TEMPLATE', DEFAULT_TEMPLATE);
194
			}
195
		}
196
		// Set the template dir
197
		define('TEMPLATE_DIR', WB_URL.'/templates/'.TEMPLATE);
198
199
		// Check if user is allowed to view this page
200
		if(VISIBILITY == 'private' OR VISIBILITY == 'registered') {
201
			// Check if the user is authenticated
202
			if($this->is_authenticated() == false) {
203
				// User needs to login first
204
				header("Location: ".WB_URL."/account/login".PAGE_EXTENSION.'?redirect='.$this->link);
205
				exit(0);
206
			}
207
			// Check if we should show this page
208
			if($this->show_page($this->page) == false) {
209
				$this->page_access_denied=true;
210
			}
211
		} elseif(VISIBILITY == 'deleted' OR VISIBILITY == 'none') {
212
			// User isnt allowed on this page so tell them
213
			$this->page_access_denied=true;
214
		}
215
	}
216
217
	function get_website_settings() {
218
		global $database;
219
220
		// set visibility SQL code
221
		// never show no-vis, hidden or deleted pages
222
		$this->extra_where_sql = "visibility != 'none' AND visibility != 'hidden' AND visibility != 'deleted'";
223
		// Set extra private sql code
224
		if($this->is_authenticated()==false) {
225
			// if user is not authenticated, don't show private pages either
226
			$this->extra_where_sql .= " AND visibility != 'private'";
227
			// and 'registered' without frontend login doesn't make much sense!
228
			if (FRONTEND_LOGIN==false) {
229
				$this->extra_where_sql .= " AND visibility != 'registered'";
230
			}
231
		}
232
		$this->extra_where_sql .= $this->sql_where_language;
233
234
		// Work-out if any possible in-line search boxes should be shown
235
		if(SEARCH == 'public') {
236
			define('SHOW_SEARCH', true);
237
		} elseif(SEARCH == 'private' AND VISIBILITY == 'private') {
238
			define('SHOW_SEARCH', true);
239
		} elseif(SEARCH == 'private' AND $wb->is_authenticated() == true) {
240
			define('SHOW_SEARCH', true);
241
		} else {
242
			define('SHOW_SEARCH', false);
243
		}
244
		// Work-out if menu should be shown
245
		if(!defined('SHOW_MENU')) {
246
			define('SHOW_MENU', true);
247
		}
248
		// Work-out if login menu constants should be set
249
		if(FRONTEND_LOGIN) {
250
			// Set login menu constants
251
			define('LOGIN_URL', WB_URL.'/account/login'.PAGE_EXTENSION);
252
			define('LOGOUT_URL', WB_URL.'/account/logout'.PAGE_EXTENSION);
253
			define('FORGOT_URL', WB_URL.'/account/forgot'.PAGE_EXTENSION);
254
			define('PREFERENCES_URL', WB_URL.'/account/preferences'.PAGE_EXTENSION);
255
			define('SIGNUP_URL', WB_URL.'/account/signup'.PAGE_EXTENSION);
256
		}
257
	}
258
259
	function preprocess(&$content) {
260
		global $database;
261
		// Replace [wblink--PAGE_ID--] with real link
262
		$pattern = '/\[wblink(.+?)\]/s';
263
		preg_match_all($pattern,$content,$ids);
264
		foreach($ids[1] AS $page_id) {
265
			$pattern = '/\[wblink'.$page_id.'\]/s';
266
			// Get page link
267
			$get_link = $database->query("SELECT link FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id' LIMIT 1");
268
			$fetch_link = $get_link->fetchRow();
269
			$link = $this->page_link($fetch_link['link']);
270
			$content = preg_replace($pattern,$link,$content);
271
		}
272
	}
273
274
	function menu() {
275
		global $wb;
276
	   if (!isset($wb->menu_number)) {
277
	   	$wb->menu_number = 1;
278
	   }
279
	   if (!isset($wb->menu_start_level)) {
280
	   	$wb->menu_start_level = 0;
281
	   }
282
	   if (!isset($wb->menu_recurse)) {
283
	   	$wb->menu_recurse = -1;
284
	   }
285
	   if (!isset($wb->menu_collapse)) {
286
	   	$wb->menu_collapse = true;
287
	   }
288
	   if (!isset($wb->menu_item_template)) {
289
	   	$wb->menu_item_template = '<li><span[class]>[a] [menu_title] [/a]</span>';
290
	   }
291
	   if (!isset($wb->menu_item_footer)) {
292
	   	$wb->menu_item_footer = '</li>';
293
	   }
294
	   if (!isset($wb->menu_header)) {
295
	   	$wb->menu_header = '<ul>';
296
	   }
297
	   if (!isset($wb->menu_footer)) {
298
	   	$wb->menu_footer = '</ul>';
299
	   }
300
	   if (!isset($wb->menu_default_class)) {
301
	   	$wb->menu_default_class = ' class="menu_default"';
302
	   }
303
	   if (!isset($wb->menu_current_class)) {
304
	   	$wb->menu_current_class = ' class="menu_current"';
305
	   }
306
	   if (!isset($wb->menu_parent)) {
307
	   	$wb->menu_parent = 0;
308
	   }
309
	   $wb->show_menu();
310
	}
311
312
	function show_menu() {
313
		global $database;
314
		if ($this->menu_start_level>0) {
315
			$key_array=array_keys($this->page_trail);
316
			if (isset($key_array[$this->menu_start_level-1])) {
317
				$real_start=$key_array[$this->menu_start_level-1];
318
				$this->menu_parent=$real_start;
319
				$this->menu_start_level=0;
320
			} else {
321
				return;
322
			}
323
		}
324
	   if ($this->menu_recurse==0)
325
	       return;
326
	   // Check if we should add menu number check to query
327
	   if($this->menu_parent == 0) {
328
	       $menu_number = "menu = '$this->menu_number'";
329
	   } else {
330
	      $menu_number = '1';
331
	   }
332
	   // Query pages
333
	   $query_menu = $database->query("SELECT page_id,menu_title,page_title,link,target,level,visibility FROM ".
334
	TABLE_PREFIX."pages WHERE parent = '$this->menu_parent' AND $menu_number AND $this->extra_where_sql ORDER BY position ASC");
335
	   // Check if there are any pages to show
336
	   if($query_menu->numRows() > 0) {
337
	   	  // Print menu header
338
	   	  echo "\n".$this->menu_header;
339
	      // Loop through pages
340
	      while($page = $query_menu->fetchRow()) {
341
	      	 // Check if this page should be shown
342
	         // Create vars
343
	         $vars = array('[class]','[a]', '[/a]', '[menu_title]', '[page_title]');
344
	         // Work-out class
345
	         if($page['page_id'] == PAGE_ID) {
346
	            $class = $this->menu_current_class;
347
	         } else {
348
	            $class = $this->menu_default_class;
349
	         }
350
	         // Check if link is same as first page link, and if so change to WB URL
351
	         if($page['link'] == $this->default_link AND !INTRO_PAGE) {
352
	            $link = WB_URL;
353
	         } else {
354
	            $link = $this->page_link($page['link']);
355
	         }
356
	         // Create values
357 442 Ruebenwurz
	         $values = array($class,'<a href="'.$link.'" target="'.$page['target'].'" '.$class.'>', '</a>', $page['menu_title'], $page['page_title']);
358 433 doc
	         // Replace vars with value and print
359
	         echo "\n".str_replace($vars, $values, $this->menu_item_template);
360
	         // Generate sub-menu
361
	         if($this->menu_collapse==false OR ($this->menu_collapse==true AND isset($this->page_trail[$page['page_id']]))) {
362
	            $this->menu_recurse--;
363
	            $this->menu_parent=$page['page_id'];
364
	            $this->show_menu();
365
	         }
366
	         echo "\n".$this->menu_item_footer;
367
	      }
368
	      // Print menu footer
369
	      echo "\n".$this->menu_footer;
370
	   }
371
	}
372
373
374
	// Function to show the "Under Construction" page
375
	function print_under_construction() {
376
		global $MESSAGE;
377
		require_once(WB_PATH.'/languages/'.DEFAULT_LANGUAGE.'.php');
378
		echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
379
		<head><title>'.$MESSAGE['GENERIC']['WEBSITE_UNDER_CONSTRUCTION'].'</title>
380
		<style type="text/css"><!-- body { font-family: Verdana, Arial, Helvetica, sans-serif;
381
		font-size: 12px; color: #000000;	background-color: #FFFFFF;	margin: 20px; text-align: center; }
382
		h1 { margin: 0; padding: 0; }--></style></head><body>
383
		<h1>'.$MESSAGE['GENERIC']['WEBSITE_UNDER_CONSTRUCTION'].'</h1><br />
384
		'.$MESSAGE['GENERIC']['PLEASE_CHECK_BACK_SOON'].'</body></html>';
385
	}
386
}
387
388 399 Ruebenwurz
?>