Project

General

Profile

1
<?php
2

    
3
// $Id: view.php 900 2009-01-03 18:45:03Z Ruebenwurzel $
4

    
5
/*
6

    
7
 Website Baker Project <http://www.websitebaker.org/>
8
 Copyright (C) 2004-2008, 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
// Must include code to stop this file being access directly
27
if(defined('WB_PATH') == false) { exit("Cannot access this file directly"); }
28

    
29
// check if frontend.css file needs to be included into the <body></body> of view.php
30
if((!function_exists('register_frontend_modfiles') || !defined('MOD_FRONTEND_CSS_REGISTERED')) &&  file_exists(WB_PATH .'/modules/news/frontend.css')) {
31
   echo '<style type="text/css">';
32
   include(WB_PATH .'/modules/news/frontend.css');
33
   echo "\n</style>\n";
34
} 
35

    
36
//overwrite php.ini on Apache servers for valid SESSION ID Separator
37
if(function_exists('ini_set')) {
38
	ini_set('arg_separator.output', '&amp;');
39
}
40

    
41
// Check if there is a start point defined
42
if(isset($_GET['p']) AND is_numeric($_GET['p']) AND $_GET['p'] >= 0) {
43
	$position = $_GET['p'];
44
} else {
45
	$position = 0;
46
}
47

    
48
// Get user's username, display name, email, and id - needed for insertion into post info
49
$users = array();
50
$query_users = $database->query("SELECT user_id,username,display_name,email FROM ".TABLE_PREFIX."users");
51
if($query_users->numRows() > 0) {
52
	while($user = $query_users->fetchRow()) {
53
		// Insert user info into users array
54
		$user_id = $user['user_id'];
55
		$users[$user_id]['username'] = $user['username'];
56
		$users[$user_id]['display_name'] = $user['display_name'];
57
		$users[$user_id]['email'] = $user['email'];
58
	}
59
}
60

    
61
// Get groups (title, if they are active, and their image [if one has been uploaded])
62
if (isset($groups)) {
63
   unset($groups);
64
}
65
$groups[0]['title'] = '';
66
$groups[0]['active'] = true;
67
$groups[0]['image'] = '';
68
$query_users = $database->query("SELECT group_id,title,active FROM ".TABLE_PREFIX."mod_news_groups WHERE section_id = '$section_id' ORDER BY position ASC");
69
if($query_users->numRows() > 0) {
70
	while($group = $query_users->fetchRow()) {
71
		// Insert user info into users array
72
		$group_id = $group['group_id'];
73
		$groups[$group_id]['title'] = ($group['title']);
74
		$groups[$group_id]['active'] = $group['active'];
75
		if(file_exists(WB_PATH.MEDIA_DIRECTORY.'/.news/image'.$group_id.'.jpg')) {
76
			$groups[$group_id]['image'] = WB_URL.MEDIA_DIRECTORY.'/.news/image'.$group_id.'.jpg';
77
		} else {
78
			$groups[$group_id]['image'] = '';
79
		}
80
	}
81
}
82

    
83
// Check if we should show the main page or a post itself
84
if(!defined('POST_ID') OR !is_numeric(POST_ID)) {
85
	
86
	// Check if we should only list posts from a certain group
87
	if(isset($_GET['g']) AND is_numeric($_GET['g'])) {
88
		$query_extra = " AND group_id = '".$_GET['g']."'";
89
	} else {
90
		$query_extra = '';
91
	}
92
	
93
	// Get settings
94
	$query_settings = $database->query("SELECT * FROM ".TABLE_PREFIX."mod_news_settings WHERE section_id = '$section_id'");
95
	if($query_settings->numRows() > 0) {
96
		$fetch_settings = $query_settings->fetchRow();
97
		$setting_header = ($fetch_settings['header']);
98
		$setting_post_loop = ($fetch_settings['post_loop']);
99
		$setting_footer = ($fetch_settings['footer']);
100
		$setting_posts_per_page = $fetch_settings['posts_per_page'];
101
	} else {
102
		$setting_header = '';
103
		$setting_post_loop = '';
104
		$setting_footer = '';
105
		$setting_posts_per_page = '';
106
	}
107
	
108
	$t = time();
109
	// Get total number of posts
110
	$query_total_num = $database->query("SELECT post_id FROM ".TABLE_PREFIX."mod_news_posts
111
		WHERE section_id = '$section_id' AND active = '1' AND title != '' $query_extra 
112
		AND (published_when = '0' OR published_when <= $t) AND (published_until = 0 OR published_until >= $t)");
113
	$total_num = $query_total_num->numRows();
114

    
115
	// Work-out if we need to add limit code to sql
116
	if($setting_posts_per_page != 0) {
117
		$limit_sql = " LIMIT $position,$setting_posts_per_page";
118
	} else {
119
		$limit_sql = "";
120
	}
121
	
122
	// Query posts (for this page)
123
	$query_posts = $database->query("SELECT * FROM ".TABLE_PREFIX."mod_news_posts
124
		WHERE section_id = '$section_id' AND active = '1' AND title != ''$query_extra
125
		AND (published_when = '0' OR published_when <= $t) AND (published_until = 0 OR published_until >= $t)
126
		ORDER BY position DESC".$limit_sql);
127
	$num_posts = $query_posts->numRows();
128
	
129
	// Create previous and next links
130
	if($setting_posts_per_page != 0) {
131
		if($position > 0) {
132
			if(isset($_GET['g']) AND is_numeric($_GET['g'])) {
133
				$pl_prepend = '<a href="?p='.($position-$setting_posts_per_page).'&amp;g='.$_GET['g'].'">&lt;&lt; ';
134
			} else {
135
				$pl_prepend = '<a href="?p='.($position-$setting_posts_per_page).'">&lt;&lt; ';
136
			}
137
			$pl_append = '</a>';
138
			$previous_link = $pl_prepend.$TEXT['PREVIOUS'].$pl_append;
139
			$previous_page_link = $pl_prepend.$TEXT['PREVIOUS_PAGE'].$pl_append;
140
		} else {
141
			$previous_link = '';
142
			$previous_page_link = '';
143
		}
144
		if($position+$setting_posts_per_page >= $total_num) {
145
			$next_link = '';
146
			$next_page_link = '';
147
		} else {
148
			if(isset($_GET['g']) AND is_numeric($_GET['g'])) {
149
				$nl_prepend = '<a href="?p='.($position+$setting_posts_per_page).'&amp;g='.$_GET['g'].'"> ';
150
			} else {
151
				$nl_prepend = '<a href="?p='.($position+$setting_posts_per_page).'"> ';
152
			}
153
			$nl_append = ' &gt;&gt;</a>';
154
			$next_link = $nl_prepend.$TEXT['NEXT'].$nl_append;
155
			$next_page_link = $nl_prepend.$TEXT['NEXT_PAGE'].$nl_append;
156
		}
157
		if($position+$setting_posts_per_page > $total_num) {
158
			$num_of = $position+$num_posts;
159
		} else {
160
			$num_of = $position+$setting_posts_per_page;
161
		}
162
		$out_of = ($position+1).'-'.$num_of.' '.strtolower($TEXT['OUT_OF']).' '.$total_num;
163
		$of = ($position+1).'-'.$num_of.' '.strtolower($TEXT['OF']).' '.$total_num;
164
		$display_previous_next_links = '';
165
	} else {
166
		$display_previous_next_links = 'none';
167
	}
168
		
169
	// Print header
170
	if($display_previous_next_links == 'none') {
171
		echo  str_replace(array('[NEXT_PAGE_LINK]','[NEXT_LINK]','[PREVIOUS_PAGE_LINK]','[PREVIOUS_LINK]','[OUT_OF]','[OF]','[DISPLAY_PREVIOUS_NEXT_LINKS]'), array('','','','','','', $display_previous_next_links), $setting_header);
172
	} else {
173
		echo str_replace(array('[NEXT_PAGE_LINK]','[NEXT_LINK]','[PREVIOUS_PAGE_LINK]','[PREVIOUS_LINK]','[OUT_OF]','[OF]','[DISPLAY_PREVIOUS_NEXT_LINKS]'), array($next_page_link, $next_link, $previous_page_link, $previous_link, $out_of, $of, $display_previous_next_links), $setting_header);
174
	}
175
	
176
	if($num_posts > 0) {
177
		if($query_extra != '') {
178
			?>
179
			<div class="selected_group_title">
180
				<?php echo '<a href="'.htmlspecialchars(strip_tags($_SERVER['PHP_SELF'])).'">'.PAGE_TITLE.'</a> &gt;&gt; '.$groups[$_GET['g']]['title']; ?>
181
			</div>
182
			<?php
183
		}
184
		while($post = $query_posts->fetchRow()) {
185
			if(isset($groups[$post['group_id']]['active']) AND $groups[$post['group_id']]['active'] != false) { // Make sure parent group is active
186
				$uid = $post['posted_by']; // User who last modified the post
187
				// Workout date and time of last modified post
188
				$post_date = gmdate(DATE_FORMAT, $post['posted_when']+TIMEZONE);
189
				$post_time = gmdate(TIME_FORMAT, $post['posted_when']+TIMEZONE);
190
				$publ_date = date(DATE_FORMAT,$post['published_when']);
191
				$publ_time = date(TIME_FORMAT,$post['published_when']);
192
				// Work-out the post link
193
				$post_link = page_link($post['link']);
194
				if(isset($_GET['p']) AND $position > 0) {
195
					$post_link .= '?p='.$position;
196
				}
197
				if(isset($_GET['g']) AND is_numeric($_GET['g'])) {
198
					if(isset($_GET['p']) AND $position > 0) { $post_link .= '&amp;'; } else { $post_link .= '?'; }
199
					$post_link .= 'g='.$_GET['g'];
200
				}
201
				// Get group id, title, and image
202
				$group_id = $post['group_id'];
203
				$group_title = $groups[$group_id]['title'];
204
				$group_image = $groups[$group_id]['image'];
205
				if($group_image == '') { $display_image = 'none'; } else { $display_image = ''; }
206
				if($group_id == 0) { $display_group = 'none'; } else { $display_group = ''; }
207
				// Replace [wblink--PAGE_ID--] with real link
208
				$short = ($post['content_short']);
209
				$wb->preprocess($short);
210
				// Replace vars with values
211
				$post_long_len = strlen($post['content_long']);
212
				$vars = array('[PAGE_TITLE]', '[GROUP_ID]', '[GROUP_TITLE]', '[GROUP_IMAGE]', '[DISPLAY_GROUP]', '[DISPLAY_IMAGE]', '[TITLE]', '[SHORT]', '[LINK]', '[MODI_DATE]', '[MODI_TIME]', '[PUBLISHED_DATE]', '[PUBLISHED_TIME]', '[USER_ID]', '[USERNAME]', '[DISPLAY_NAME]', '[EMAIL]', '[TEXT_READ_MORE]');
213
				if(isset($users[$uid]['username']) AND $users[$uid]['username'] != '') {
214
					if($post_long_len < 9) {
215
						$values = array(PAGE_TITLE, $group_id, $group_title, $group_image, $display_group, $display_image, $post['title'], $short, $post_link, $post_date, $post_time, $publ_date, $publ_time, $uid, $users[$uid]['username'], $users[$uid]['display_name'], $users[$uid]['email'], '');
216
					} else {
217
						$values = array(PAGE_TITLE, $group_id, $group_title, $group_image, $display_group, $display_image, $post['title'], $short, $post_link, $post_date, $post_time, $publ_date, $publ_time, $uid, $users[$uid]['username'], $users[$uid]['display_name'], $users[$uid]['email'], $TEXT['READ_MORE']);
218
					}
219
				} else {
220
					if($post_long_len < 9) {
221
						$values = array(PAGE_TITLE, $group_id, $group_title, $group_image, $display_group, $display_image, $post['title'], $short, $post_link, $post_date, $post_time, $publ_date, $publ_time, '', '', '', '', '');
222
					} else {
223
						$values = array(PAGE_TITLE, $group_id, $group_title, $group_image, $display_group, $display_image, $post['title'], $short, $post_link, $post_date, $post_time, $publ_date, $publ_time, '', '', '', '', $TEXT['READ_MORE']);
224
					}
225
				}
226
				echo str_replace($vars, $values, $setting_post_loop);
227
			}
228
		}
229
	}
230
	
231
	// Print footer
232
	if($display_previous_next_links == 'none') {
233
		echo  str_replace(array('[NEXT_PAGE_LINK]','[NEXT_LINK]','[PREVIOUS_PAGE_LINK]','[PREVIOUS_LINK]','[OUT_OF]','[OF]','[DISPLAY_PREVIOUS_NEXT_LINKS]'), array('','','','','','', $display_previous_next_links), $setting_footer);
234
	} else {
235
		echo str_replace(array('[NEXT_PAGE_LINK]','[NEXT_LINK]','[PREVIOUS_PAGE_LINK]','[PREVIOUS_LINK]','[OUT_OF]','[OF]','[DISPLAY_PREVIOUS_NEXT_LINKS]'), array($next_page_link, $next_link, $previous_page_link, $previous_link, $out_of, $of, $display_previous_next_links), $setting_footer);
236
	}
237
	
238
} elseif(defined('POST_ID') AND is_numeric(POST_ID)) {
239
	
240
	// Get settings
241
	$query_settings = $database->query("SELECT * FROM ".TABLE_PREFIX."mod_news_settings WHERE section_id = '$section_id'");
242
	if($query_settings->numRows() > 0) {
243
		$fetch_settings = $query_settings->fetchRow();
244
		$setting_post_header = ($fetch_settings['post_header']);
245
		$setting_post_footer = ($fetch_settings['post_footer']);
246
		$setting_comments_header = ($fetch_settings['comments_header']);
247
		$setting_comments_loop = ($fetch_settings['comments_loop']);
248
		$setting_comments_footer = ($fetch_settings['comments_footer']);
249
	} else {
250
		$setting_post_header = '';
251
		$setting_post_footer = '';
252
		$setting_comments_header = '';
253
		$setting_comments_loop = '';
254
		$setting_comments_footer = '';
255
	}
256
	
257
	// Get page info
258
	$query_page = $database->query("SELECT link FROM ".TABLE_PREFIX."pages WHERE page_id = '".PAGE_ID."'");
259
	if($query_page->numRows() > 0) {
260
		$page = $query_page->fetchRow();
261
		$page_link = page_link($page['link']);
262
		if(isset($_GET['p']) AND $position > 0) {
263
			$page_link .= '?p='.$_GET['p'];
264
		}
265
		if(isset($_GET['g']) AND is_numeric($_GET['g'])) {
266
			if(isset($_GET['p']) AND $position > 0) { $page_link .= '&amp;'; } else { $page_link .= '?'; }
267
			$page_link .= 'g='.$_GET['g'];
268
		}
269
	} else {
270
		exit('Page not found');
271
	}
272
	
273
	// Get post info
274
	$t = time();
275
	$query_post = $database->query("SELECT * FROM ".TABLE_PREFIX."mod_news_posts
276
		WHERE post_id = '".POST_ID."' AND active = '1'
277
		AND (published_when = '0' OR published_when <= $t) AND (published_until = 0 OR published_until >= $t)
278
	");
279
	if($query_post->numRows() > 0) {
280
		$post = $query_post->fetchRow();
281
		if(isset($groups[$post['group_id']]['active']) AND $groups[$post['group_id']]['active'] != false) { // Make sure parent group is active
282
			$uid = $post['posted_by']; // User who last modified the post
283
			// Workout date and time of last modified post
284
			$post_date = gmdate(DATE_FORMAT, $post['posted_when']+TIMEZONE);
285
			$post_time = gmdate(TIME_FORMAT, $post['posted_when']+TIMEZONE);
286
			$publ_date = date(DATE_FORMAT,$post['published_when']);
287
			$publ_time = date(TIME_FORMAT,$post['published_when']);
288
			// Get group id, title, and image
289
			$group_id = $post['group_id'];
290
			$group_title = $groups[$group_id]['title'];
291
			$group_image = $groups[$group_id]['image'];
292
			if($group_image == '') { $display_image = 'none'; } else { $display_image = ''; }
293
			if($group_id == 0) { $display_group = 'none'; } else { $display_group = ''; }
294
			$vars = array('[PAGE_TITLE]', '[GROUP_ID]', '[GROUP_TITLE]', '[GROUP_IMAGE]', '[DISPLAY_GROUP]', '[DISPLAY_IMAGE]', '[TITLE]', '[SHORT]', '[BACK]', '[MODI_DATE]', '[MODI_TIME]', '[PUBLISHED_DATE]', '[PUBLISHED_TIME]', '[USER_ID]', '[USERNAME]', '[DISPLAY_NAME]', '[EMAIL]');
295
			$post_short=$post['content_short'];
296
			$wb->preprocess($post_short);
297
			if(isset($users[$uid]['username']) AND $users[$uid]['username'] != '') {
298
				$values = array(PAGE_TITLE, $group_id, $group_title, $group_image, $display_group, $display_image, $post['title'], $post_short, $page_link, $post_date, $post_time, $publ_date, $publ_time, $uid, $users[$uid]['username'], $users[$uid]['display_name'], $users[$uid]['email']);
299
			} else {
300
				$values = array(PAGE_TITLE, $group_id, $group_title, $group_image, $display_group, $display_image, $post['title'], $post_short, $page_link, $post_date, $post_time, $publ_date, $publ_time, '', '', '', '');
301
			}
302
			$post_long = ($post['content_long']);
303
		}
304
	} else {
305
		$wb->print_error($MESSAGE['FRONTEND']['SORRY_NO_ACTIVE_SECTIONS'], "javascript: history.go(-1);", false);
306
		exit(0);
307
	}
308
	
309
	// Print post header
310
	echo str_replace($vars, $values, $setting_post_header);
311
	
312
	// Replace [wblink--PAGE_ID--] with real link
313
  	$wb->preprocess($post_long);
314
	// Print long
315
	echo $post_long;
316
	
317
	// Print post footer
318
	echo str_replace($vars, $values, $setting_post_footer);
319
	
320
	// Show comments section if we have to
321
	if(($post['commenting'] == 'private' AND isset($wb) AND $wb->is_authenticated() == true) OR $post['commenting'] == 'public') {
322
		
323
		// Print comments header
324
		echo str_replace('[ADD_COMMENT_URL]', WB_URL.'/modules/news/comment.php?id='.POST_ID.'&amp;sid='.$section_id, $setting_comments_header);
325
		
326
		// Query for comments
327
		$query_comments = $database->query("SELECT title,comment,commented_when,commented_by FROM ".TABLE_PREFIX."mod_news_comments WHERE post_id = '".POST_ID."' ORDER BY commented_when ASC");
328
		if($query_comments->numRows() > 0) {
329
			while($comment = $query_comments->fetchRow()) {
330
				// Display Comments without slashes, but with new-line characters
331
				$comment['comment'] = nl2br(($comment['comment']));
332
				$comment['title'] = ($comment['title']);
333
				// Print comments loop
334
				$commented_date = gmdate(DATE_FORMAT, $comment['commented_when']+TIMEZONE);
335
				$commented_time = gmdate(TIME_FORMAT, $comment['commented_when']+TIMEZONE);
336
				$uid = $comment['commented_by'];
337
				$vars = array('[TITLE]','[COMMENT]','[DATE]','[TIME]','[USER_ID]','[USERNAME]','[DISPLAY_NAME]', '[EMAIL]');
338
				if(isset($users[$uid]['username']) AND $users[$uid]['username'] != '') {
339
					$values = array(($comment['title']), ($comment['comment']), $commented_date, $commented_time, $uid, ($users[$uid]['username']), ($users[$uid]['display_name']), ($users[$uid]['email']));
340
				} else {
341
					$values = array(($comment['title']), ($comment['comment']), $commented_date, $commented_time, '0', strtolower($TEXT['UNKNOWN']), $TEXT['UNKNOWN'], '');
342
				}
343
				echo str_replace($vars, $values, $setting_comments_loop);
344
			}
345
		} else {
346
			// Say no comments found
347
			if(isset($TEXT['NONE_FOUND'])) {
348
				echo $TEXT['NONE_FOUND'].'<br />';
349
			} else {
350
				echo 'None Found<br />';
351
			}
352
		}
353
		
354
		// Print comments footer
355
		echo str_replace('[ADD_COMMENT_URL]', WB_URL.'/modules/news/comment.php?id='.POST_ID.'&amp;sid='.$section_id, $setting_comments_footer);
356
	}
357
	if(ENABLED_ASP) {
358
		$_SESSION['comes_from_view'] = POST_ID;
359
		$_SESSION['comes_from_view_time'] = time();
360
	}
361
}
362

    
363
?>
(30-30/30)