Project

General

Profile

1
<?php
2

    
3
// $Id: class.frontend.php 242 2005-11-23 16:24:09Z 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 $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: ".$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
			// Page keywords
170
			$this->page_keywords=$this->page['keywords'];
171
			// Page link
172
			$this->link=$this->page_link($this->page['link']);
173

    
174
		// End code to set details as either variables of constants
175
		}
176

    
177
		// Figure out what template to use
178
		if(!defined('TEMPLATE')) {
179
			if(isset($this->page['template']) AND $this->page['template'] != '') {
180
				if(file_exists(WB_PATH.'/templates/'.$this->page['template'].'/index.php')) {
181
					define('TEMPLATE', $this->page['template']);
182
				} else {
183
					define('TEMPLATE', DEFAULT_TEMPLATE);
184
				}
185
			} else {
186
				define('TEMPLATE', DEFAULT_TEMPLATE);
187
			}
188
		}
189
		// Set the template dir
190
		define('TEMPLATE_DIR', WB_URL.'/templates/'.TEMPLATE);
191

    
192
		// Check if user is allowed to view this page
193
		if(VISIBILITY == 'private' OR VISIBILITY == 'registered') {
194
			// Check if the user is authenticated
195
			if($this->is_authenticated() == false) {
196
				// User needs to login first
197
				header("Location: ".WB_URL."/account/login".PAGE_EXTENSION.'?redirect='.$this->link);
198
			}
199
			// Check if we should show this page
200
			if($this->show_page($this->page) == false) {
201
				$this->page_access_denied=true;
202
			}
203
		} elseif(VISIBILITY == 'deleted' OR VISIBILITY == 'none') {
204
			// User isnt allowed on this page so tell them
205
			$this->page_access_denied=true;
206
		}
207
	}
208

    
209
	function get_website_settings() {
210
		global $database;
211

    
212
		// set visibility SQL code
213
		// never show no-vis, hidden or deleted pages
214
		$this->extra_where_sql = "visibility != 'none' AND visibility != 'hidden' AND visibility != 'deleted'";
215
		// Set extra private sql code
216
		if($this->is_authenticated()==false) {
217
			// if user is not authenticated, don't show private pages either
218
			$this->extra_where_sql .= " AND visibility != 'private'";
219
			// and 'registered' without frontend login doesn't make much sense!
220
			if (FRONTEND_LOGIN==false) {
221
				$this->extra_where_sql .= " AND visibility != 'registered'";
222
			}
223
		}
224
		$this->extra_where_sql .= $this->sql_where_language;
225

    
226
		// Work-out if any possible in-line search boxes should be shown
227
		if(SEARCH == 'public') {
228
			define('SHOW_SEARCH', true);
229
		} elseif(SEARCH == 'private' AND VISIBILITY == 'private') {
230
			define('SHOW_SEARCH', true);
231
		} elseif(SEARCH == 'private' AND $wb->is_authenticated() == true) {
232
			define('SHOW_SEARCH', true);
233
		} else {
234
			define('SHOW_SEARCH', false);
235
		}
236
		// Work-out if menu should be shown
237
		if(!defined('SHOW_MENU')) {
238
			define('SHOW_MENU', true);
239
		}
240
		// Work-out if login menu constants should be set
241
		if(FRONTEND_LOGIN) {
242
			// Set login menu constants
243
			define('LOGIN_URL', WB_URL.'/account/login'.PAGE_EXTENSION);
244
			define('LOGOUT_URL', WB_URL.'/account/logout'.PAGE_EXTENSION);
245
			define('FORGOT_URL', WB_URL.'/account/forgot'.PAGE_EXTENSION);
246
			define('PREFERENCES_URL', WB_URL.'/account/preferences'.PAGE_EXTENSION);
247
			define('SIGNUP_URL', WB_URL.'/account/signup'.PAGE_EXTENSION);
248
		}
249
	}
250
	
251
	function preprocess(&$content) {
252
		global $database;
253
		// Replace [wblink--PAGE_ID--] with real link
254
		$pattern = '/\[wblink(.+?)\]/s';
255
		preg_match_all($pattern,$content,$ids);
256
		foreach($ids[1] AS $page_id) {
257
			$pattern = '/\[wblink'.$page_id.'\]/s';
258
			// Get page link
259
			$get_link = $database->query("SELECT link FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id' LIMIT 1");
260
			$fetch_link = $get_link->fetchRow();
261
			$link = $this->page_link($fetch_link['link']);
262
			$content = preg_replace($pattern,$link,$content);
263
		}
264
	}
265
	
266
	function menu() {
267
		global $wb;
268
	   if (!isset($wb->menu_number)) {
269
	   	$wb->menu_number = 1;
270
	   }
271
	   if (!isset($wb->menu_start_level)) {
272
	   	$wb->menu_start_level = 0;
273
	   }
274
	   if (!isset($wb->menu_recurse)) {
275
	   	$wb->menu_recurse = -1;
276
	   }
277
	   if (!isset($wb->menu_collapse)) {
278
	   	$wb->menu_collapse = true;
279
	   }
280
	   if (!isset($wb->menu_item_template)) {
281
	   	$wb->menu_item_template = '<li><span[class]>[a][menu_title][/a]</span>';
282
	   }
283
	   if (!isset($wb->menu_item_footer)) {
284
	   	$wb->menu_item_footer = '</li>';
285
	   }
286
	   if (!isset($wb->menu_header)) {
287
	   	$wb->menu_header = '<ul>';
288
	   }
289
	   if (!isset($wb->menu_footer)) {
290
	   	$wb->menu_footer = '</ul>';
291
	   }
292
	   if (!isset($wb->menu_default_class)) {
293
	   	$wb->menu_default_class = ' class="menu_default"';
294
	   }
295
	   if (!isset($wb->menu_current_class)) {
296
	   	$wb->menu_current_class = ' class="menu_current"';
297
	   }
298
	   if (!isset($wb->menu_parent)) {
299
	   	$wb->menu_parent = 0;
300
	   }
301
	   $wb->show_menu();
302
	}
303
	
304
	function show_menu() {
305
	   global $database;
306
	   if ($this->menu_start_level>0) {
307
	       $key_array=array_keys($this->page_trail);
308
	       $real_start=$key_array[$this->menu_start_level-1];
309
	       if (isset($real_start)) {
310
	       	$this->menu_parent=$real_start;
311
		$this->menu_start_level=0;
312
	       } else {
313
	       	 return;
314
	       }
315
	   }
316
	   if ($this->menu_recurse==0)
317
	       return;
318
	   // Check if we should add menu number check to query
319
	   if($this->menu_parent == 0) {
320
	       $menu_number = "menu = '$this->menu_number'";
321
	   } else {
322
	      $menu_number = '1';
323
	   }
324
	   // Query pages
325
	   $query_menu = $database->query("SELECT page_id,menu_title,page_title,link,target,level,visibility FROM ".
326
	TABLE_PREFIX."pages WHERE parent = '$this->menu_parent' AND $menu_number AND $this->extra_where_sql ORDER BY position ASC");
327
	   // Check if there are any pages to show
328
	   if($query_menu->numRows() > 0) {
329
	   	  // Print menu header
330
	   	  echo "\n".$this->menu_header;
331
	      // Loop through pages
332
	      while($page = $query_menu->fetchRow()) {
333
	      	 // Check if this page should be shown
334
	         // Create vars
335
	         $vars = array('[class]','[a]', '[/a]', '[menu_title]', '[page_title]');
336
	         // Work-out class
337
	         if($page['page_id'] == PAGE_ID) {
338
	            $class = $this->menu_current_class;
339
	         } else {
340
	            $class = $this->menu_default_class;
341
	         }
342
	         // Check if link is same as first page link, and if so change to WB URL
343
	         if($page['link'] == $this->default_link AND !INTRO_PAGE) {
344
	            $link = WB_URL;
345
	         } else {
346
	            $link = $this->page_link($page['link']);
347
	         }
348
	         // Create values
349
	         $values = array($class,'<a href="'.$link.'" target="'.$page['target'].'" '.$class.'>', '</a>', ($page['menu_title']), ($page['page_title']));
350
	         // Replace vars with value and print
351
	         echo "\n".str_replace($vars, $values, $this->menu_item_template);
352
	         // Generate sub-menu
353
	         if($this->menu_collapse==false OR ($this->menu_collapse==true AND isset($this->page_trail[$page['page_id']]))) {
354
	            $this->menu_recurse--;
355
	            $this->menu_parent=$page['page_id'];
356
	            $this->show_menu();
357
	         }
358
	         echo "\n".$this->menu_item_footer;
359
	      }
360
	      // Print menu footer
361
	      echo "\n".$this->menu_footer;
362
	   }
363
	}
364

    
365

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

    
380
?>
(3-3/11)