1<?php
2/**
3 * MyBB 1.8
4 * Copyright 2014 MyBB Group, All Rights Reserved
5 *
6 * Website: http://www.mybb.com
7 * License: http://www.mybb.com/about/license
8 *
9 */
10
11define("IN_MYBB", 1);
12define("IN_ARCHIVE", 1);
13
14require_once "./global.php";
15require_once MYBB_ROOT."inc/functions_post.php";
16// Load global language phrases
17$lang->load("index");
18
19$plugins->run_hooks("archive_start");
20
21switch($action)
22{
23	// Display an announcement.
24	case "announcement":
25		// Fetch the forum this thread is in
26		if($announcement['fid'] != -1)
27		{
28			$forum = get_forum($announcement['fid']);
29			if(!$forum['fid'] || $forum['password'] !== '')
30			{
31				archive_error($lang->error_invalidforum);
32			}
33
34			// Check if we have permission to view this thread
35			$forumpermissions = forum_permissions($forum['fid']);
36			if($forumpermissions['canview'] != 1 || $forumpermissions['canviewthreads'] != 1)
37			{
38				archive_error_no_permission();
39			}
40
41			check_forum_password_archive($forum['fid']);
42		}
43
44		$announcement['subject'] = htmlspecialchars_uni($parser->parse_badwords($announcement['subject']));
45
46		$parser_options = array(
47			"allow_html" => $mybb->settings['announcementshtml'] && $announcement['allowhtml'],
48			"allow_mycode" => $announcement['allowmycode'],
49			"allow_smilies" => $announcement['allowsmilies'],
50			"allow_imgcode" => 1,
51			"allow_videocode" => 1,
52			"me_username" => $announcement['username'],
53			"filter_badwords" => 1
54		);
55
56		$announcement['message'] = $parser->parse_message($announcement['message'], $parser_options);
57
58		$profile_link = build_profile_link(htmlspecialchars_uni($announcement['username']), $announcement['uid']);
59
60		// Build the navigation
61		add_breadcrumb($announcement['subject']);
62		archive_header($announcement['subject'], $announcement['subject'], $mybb->settings['bburl']."/announcements.php?aid={$id}");
63
64		// Format announcement contents.
65		$announcement['startdate'] = my_date('relative', $announcement['startdate']);
66
67		$plugins->run_hooks("archive_announcement_start");
68
69		echo "<div class=\"post\">\n<div class=\"header\">\n<h2>{$announcement['subject']} - {$profile_link}</h2>";
70		echo "<div class=\"dateline\">{$announcement['startdate']}</div>\n</div>\n<div class=\"message\">{$announcement['message']}</div>\n</div>\n";
71
72		$plugins->run_hooks("archive_announcement_end");
73
74		archive_footer();
75		break;
76
77	// Display a thread.
78	case "thread":
79		$thread['subject'] = htmlspecialchars_uni($parser->parse_badwords($thread['subject']));
80
81		// Fetch the forum this thread is in
82		$forum = get_forum($thread['fid']);
83		if(!$forum['fid'] || $forum['password'] !== '')
84		{
85			archive_error($lang->error_invalidforum);
86		}
87
88		// Check if we have permission to view this thread
89		$forumpermissions = forum_permissions($forum['fid']);
90		if($forumpermissions['canview'] != 1 || $forumpermissions['canviewthreads'] != 1)
91		{
92			archive_error_no_permission();
93		}
94
95		if($thread['visible'] != 1)
96		{
97			if(is_moderator($forum['fid'], "canviewunapprove"))
98			{
99				archive_error($lang->sprintf($lang->error_unapproved_thread, $mybb->settings['bburl']."/".get_thread_link($thread['tid'], $page)));
100			}
101			else
102			{
103				archive_error($lang->error_invalidthread);
104			}
105		}
106
107		if(isset($forumpermissions['canonlyviewownthreads']) && $forumpermissions['canonlyviewownthreads'] == 1 && $thread['uid'] != $mybb->user['uid'])
108		{
109			archive_error_no_permission();
110		}
111
112		check_forum_password_archive($forum['fid']);
113
114		// Build the navigation
115		build_forum_breadcrumb($forum['fid'], 1);
116		add_breadcrumb($thread['subject']);
117
118		archive_header($thread['subject'], $thread['subject'], $mybb->settings['bburl']."/".get_thread_link($thread['tid'], $page));
119
120		$plugins->run_hooks("archive_thread_start");
121
122		// Paginate this thread
123		if(!$mybb->settings['postsperpage'] || (int)$mybb->settings['postsperpage'] < 1)
124		{
125			$mybb->settings['postsperpage'] = 20;
126		}
127		$perpage = $mybb->settings['postsperpage'];
128		$postcount = (int)$thread['replies']+1;
129		$pages = ceil($postcount/$perpage);
130
131		if($page > $pages)
132		{
133			$page = 1;
134		}
135		if($page)
136		{
137			$start = ($page-1) * $perpage;
138		}
139		else
140		{
141			$start = 0;
142			$page = 1;
143		}
144
145		$pids = array();
146		// Fetch list of post IDs to be shown
147		$query = $db->simple_select("posts", "pid", "tid='{$id}' AND visible='1'", array('order_by' => 'dateline, pid', 'limit_start' => $start, 'limit' => $perpage));
148		while($post = $db->fetch_array($query))
149		{
150			$pids[$post['pid']] = $post['pid'];
151		}
152
153		if(empty($pids))
154		{
155			archive_error($lang->error_invalidthread);
156		}
157
158		archive_multipage($postcount, $perpage, $page, "{$base_url}thread-$id");
159
160		$pids = implode(",", $pids);
161
162		if($pids && $mybb->settings['enableattachments'] == 1)
163		{
164			// Build attachments cache
165			$query = $db->simple_select("attachments", "*", "pid IN ({$pids})");
166			while($attachment = $db->fetch_array($query))
167			{
168				$acache[$attachment['pid']][$attachment['aid']] = $attachment;
169			}
170		}
171
172		// Start fetching the posts
173		$query = $db->query("
174			SELECT u.*, u.username AS userusername, p.*
175			FROM ".TABLE_PREFIX."posts p
176			LEFT JOIN ".TABLE_PREFIX."users u ON (u.uid=p.uid)
177			WHERE p.pid IN ({$pids})
178			ORDER BY p.dateline, p.pid
179		");
180		while($post = $db->fetch_array($query))
181		{
182			$post['date'] = my_date('relative', $post['dateline']);
183			if($post['userusername'])
184			{
185				$post['username'] = $post['userusername'];
186			}
187
188			// Parse the message
189			$parser_options = array(
190				"allow_html" => $forum['allowhtml'],
191				"allow_mycode" => $forum['allowmycode'],
192				"allow_smilies" => $forum['allowsmilies'],
193				"allow_imgcode" => $forum['allowimgcode'],
194				"allow_videocode" => $forum['allowvideocode'],
195				"me_username" => $post['username'],
196				"filter_badwords" => 1
197			);
198			if($post['smilieoff'] == 1)
199			{
200				$parser_options['allow_smilies'] = 0;
201			}
202
203			$post['message'] = $parser->parse_message($post['message'], $parser_options);
204
205			// Is there an attachment in this post?
206			if($mybb->settings['enableattachments'] == 1 && isset($acache[$post['pid']]) && is_array($acache[$post['pid']]))
207			{
208				foreach($acache[$post['pid']] as $aid => $attachment)
209				{
210					$post['message'] = str_replace("[attachment={$attachment['aid']}]", "[<a href=\"".$mybb->settings['bburl']."/attachment.php?aid={$attachment['aid']}\">attachment={$attachment['aid']}</a>]", $post['message']);
211				}
212			}
213
214			// Damn thats a lot of parsing, now to determine which username to show..
215			if($post['userusername'])
216			{
217				$post['username'] = $post['userusername'];
218			}
219			$post['username'] = build_profile_link(htmlspecialchars_uni($post['username']), $post['uid']);
220
221			$plugins->run_hooks("archive_thread_post");
222
223			// Finally show the post
224			echo "<div class=\"post\">\n<div class=\"header\">\n<div class=\"author\"><h2>{$post['username']}</h2></div>";
225			echo "<div class=\"dateline\">{$post['date']}</div>\n</div>\n<div class=\"message\">{$post['message']}</div>\n</div>\n";
226		}
227		archive_multipage($postcount, $perpage, $page, "{$base_url}thread-$id");
228
229		$plugins->run_hooks("archive_thread_end");
230
231		archive_footer();
232		break;
233
234	// Display a category or a forum.
235	case "forum":
236		// Check if we have permission to view this forum
237		$forumpermissions = forum_permissions($forum['fid']);
238		if($forumpermissions['canview'] != 1)
239		{
240			archive_error_no_permission();
241		}
242
243		check_forum_password_archive($forum['fid']);
244
245		$useronly = "";
246		if(isset($forumpermissions['canonlyviewownthreads']) && $forumpermissions['canonlyviewownthreads'] == 1)
247		{
248			$useronly = "AND uid={$mybb->user['uid']}";
249		}
250
251		// Paginate this forum
252		$query = $db->simple_select("threads", "COUNT(tid) AS threads", "fid='{$id}' AND visible='1' {$useronly}");
253		$threadcount = $db->fetch_field($query, "threads");
254
255		// Build the navigation
256		build_forum_breadcrumb($forum['fid'], 1);
257
258		// No threads and not a category? Error!
259		if($forum['type'] != 'c')
260		{
261			if($forumpermissions['canviewthreads'] != 1)
262			{
263				archive_header(strip_tags($forum['name']), $forum['name'], $mybb->settings['bburl']."/".get_forum_link($id, $page)."");
264				archive_error($lang->error_nopermission);
265			}
266
267			if($threadcount < 1 && $forumpermissions['canviewthreads'] == 1)
268			{
269				archive_header(strip_tags($forum['name']), $forum['name'], $mybb->settings['bburl']."/".get_forum_link($id, $page)."");
270				archive_error($lang->error_nothreads);
271			}
272		}
273
274		// Build the archive header.
275		archive_header(strip_tags($forum['name']), $forum['name'], $mybb->settings['bburl']."/".get_forum_link($id, $page), 1);
276
277		$plugins->run_hooks("archive_forum_start");
278
279		if(!$mybb->settings['threadsperpage'] || (int)$mybb->settings['threadsperpage'] < 1)
280		{
281			$mybb->settings['threadsperpage'] = 20;
282		}
283
284		$perpage = $mybb->settings['threadsperpage'];
285		$pages = ceil($threadcount/$perpage);
286		if($page > $pages)
287		{
288			$page = 1;
289		}
290
291		if($page > 0)
292		{
293			$start = ($page-1) * $perpage;
294		}
295		else
296		{
297			$start = 0;
298			$page = 1;
299		}
300
301		// Decide what type of listing to show.
302		if($forum['type'] == 'f')
303		{
304			echo "<div class=\"listing\">\n<div class=\"header\"><h2>{$forum['name']}</h2></div>\n";
305		}
306		elseif($forum['type'] == 'c')
307		{
308			echo "<div class=\"listing\">\n<div class=\"header\"><h2>{$forum['name']}</h2></div>\n";
309		}
310
311		// Show subforums.
312		$query = $db->simple_select("forums", "COUNT(fid) AS subforums", "pid='{$id}'");
313		$subforumcount = $db->fetch_field($query, "subforums");
314		if($subforumcount > 0)
315		{
316			echo "<div class=\"forumlist\">\n";
317			echo "<h3>{$lang->subforums}</h3>\n";
318			echo "<ol>\n";
319			$forums = build_archive_forumbits($forum['fid']);
320			echo $forums;
321			echo "</ol>\n</div>\n";
322		}
323
324		archive_multipage($threadcount, $perpage, $page, "{$base_url}forum-$id");
325
326		// Get the announcements if the forum is not a category.
327		if($forum['type'] == 'f')
328		{
329			$sql = build_parent_list($forum['fid'], "fid", "OR", $forum['parentlist']);
330			$time = TIME_NOW;
331			$query = $db->simple_select("announcements", "*", "startdate < '{$time}' AND (enddate > '{$time}' OR enddate=0) AND ({$sql} OR fid='-1')");
332			if($db->num_rows($query) > 0)
333			{
334				echo "<div class=\"announcementlist\">\n";
335				echo "<h3>{$lang->forumbit_announcements}</h3>";
336				echo "<ol>\n";
337				while($announcement = $db->fetch_array($query))
338				{
339					$announcement['subject'] = $parser->parse_badwords($announcement['subject']);
340					echo "<li><a href=\"{$base_url}announcement-{$announcement['aid']}.html\">".htmlspecialchars_uni($announcement['subject'])."</a></li>";
341				}
342				echo "</ol>\n</div>\n";
343			}
344
345		}
346
347		// Get the stickies if the forum is not a category.
348		if($forum['type'] == 'f')
349		{
350			$options = array(
351				'order_by' => 'sticky, lastpost',
352				'order_dir' => 'desc',
353				'limit_start' => $start,
354				'limit' => $perpage
355			);
356			$query = $db->simple_select("threads", "*", "fid='{$id}' AND visible='1' AND sticky='1' AND closed NOT LIKE 'moved|%' {$useronly}", $options);
357			if($db->num_rows($query) > 0)
358			{
359				echo "<div class=\"threadlist\">\n";
360				echo "<h3>{$lang->forumbit_stickies}</h3>";
361				echo "<ol>\n";
362				while($sticky = $db->fetch_array($query))
363				{
364					$sticky['subject'] = htmlspecialchars_uni($parser->parse_badwords($sticky['subject']));
365					if($sticky['replies'] != 1)
366					{
367						$lang_reply_text = $lang->archive_replies;
368					}
369					else
370					{
371						$lang_reply_text = $lang->archive_reply;
372					}
373
374					$plugins->run_hooks("archive_forum_thread");
375
376					$sticky['replies'] = my_number_format($sticky['replies']);
377
378					echo "<li><a href=\"{$base_url}thread-{$sticky['tid']}.html\">{$sticky['subject']}</a>";
379					echo "<span class=\"replycount\"> ({$sticky['replies']} {$lang_reply_text})</span></li>";
380				}
381				echo "</ol>\n</div>\n";
382			}
383		}
384
385		// Get the threads if the forum is not a category.
386		if($forum['type'] == 'f')
387		{
388			$options = array(
389				'order_by' => 'sticky, lastpost',
390				'order_dir' => 'desc',
391				'limit_start' => $start,
392				'limit' => $perpage
393			);
394			$query = $db->simple_select("threads", "*", "fid='{$id}' AND visible='1' AND sticky='0' AND closed NOT LIKE 'moved|%' {$useronly}", $options);
395			if($db->num_rows($query) > 0)
396			{
397				echo "<div class=\"threadlist\">\n";
398				echo "<h3>{$lang->forumbit_threads}</h3>";
399				echo "<ol>\n";
400				while($thread = $db->fetch_array($query))
401				{
402					$thread['subject'] = htmlspecialchars_uni($parser->parse_badwords($thread['subject']));
403					if($thread['replies'] != 1)
404					{
405						$lang_reply_text = $lang->archive_replies;
406					}
407					else
408					{
409						$lang_reply_text = $lang->archive_reply;
410					}
411
412					$plugins->run_hooks("archive_forum_thread");
413
414					$thread['replies'] = my_number_format($thread['replies']);
415
416					echo "<li><a href=\"{$base_url}thread-{$thread['tid']}.html\">{$thread['subject']}</a>";
417					echo "<span class=\"replycount\"> ({$thread['replies']} {$lang_reply_text})</span></li>";
418				}
419				echo "</ol>\n</div>\n";
420			}
421		}
422
423		echo "</div>\n";
424
425		archive_multipage($threadcount, $perpage, $page, "{$base_url}forum-$id");
426
427		$plugins->run_hooks("archive_forum_end");
428
429		archive_footer();
430		break;
431
432	// Display the board home.
433	case "index":
434		// Build our forum listing
435		$forums = build_archive_forumbits(0);
436		archive_header("", $mybb->settings['bbname_orig'], $mybb->settings['bburl']."/index.php");
437
438		$plugins->run_hooks("archive_index_start");
439
440		echo "<div class=\"listing forumlist\">\n<div class=\"header\">{$mybb->settings['bbname']}</div>\n<div class=\"forums\">\n<ul>\n";
441		echo $forums;
442		echo "\n</ul>\n</div>\n</div>";
443
444		$plugins->run_hooks("archive_index_end");
445
446		archive_footer();
447		break;
448	default:
449		header("HTTP/1.0 404 Not Found");
450		switch($action2)
451		{
452			case "announcement":
453				archive_error($lang->error_invalidannouncement);
454			case "thread":
455				archive_error($lang->error_invalidthread);
456			case "forum":
457				archive_error($lang->error_invalidforum);
458			default:
459				archive_error($lang->archive_not_found);
460		}
461}
462
463$plugins->run_hooks("archive_end");
464
465/**
466* Gets a list of forums and possibly subforums.
467*
468* @param int $pid The parent forum to get the childforums for.
469* @return array Array of information regarding the child forums of this parent forum
470*/
471function build_archive_forumbits($pid=0)
472{
473	global $db, $forumpermissions, $mybb, $base_url;
474
475	// Sort out the forum cache first.
476	static $fcache;
477	if(!is_array($fcache))
478	{
479		// Fetch forums
480		$query = $db->simple_select("forums", "*", "active!=0 AND password=''", array('order_by' =>'pid, disporder'));
481		while($forum = $db->fetch_array($query))
482		{
483			$fcache[$forum['pid']][$forum['disporder']][$forum['fid']] = $forum;
484		}
485		$forumpermissions = forum_permissions();
486	}
487
488	$forums = '';
489
490	// Start the process.
491	if(is_array($fcache[$pid]))
492	{
493		foreach($fcache[$pid] as $key => $main)
494		{
495			foreach($main as $key => $forum)
496			{
497				$perms = $forumpermissions[$forum['fid']];
498				if(($perms['canview'] == 1 || $mybb->settings['hideprivateforums'] == 0) && $forum['active'] != 0)
499				{
500					if($forum['linkto'])
501					{
502						$forums .= "<li><a href=\"{$forum['linkto']}\">{$forum['name']}</a>";
503					}
504					elseif($forum['type'] == "c")
505					{
506						$forums .= "<li><strong><a href=\"{$base_url}forum-{$forum['fid']}.html\">{$forum['name']}</a></strong>";
507					}
508					else
509					{
510						$forums .= "<li><a href=\"{$base_url}forum-{$forum['fid']}.html\">{$forum['name']}</a>";
511					}
512					if(!empty($fcache[$forum['fid']]))
513					{
514						$forums .= "\n<ol>\n";
515						$forums .= build_archive_forumbits($forum['fid']);
516						$forums .= "</ol>\n";
517					}
518					$forums .= "</li>\n";
519				}
520			}
521		}
522	}
523	return $forums;
524}
525