<?php

/*
	[Discuz!] (C)2001-2006 Comsenz Inc.
	This is NOT a freeware, use is subject to license terms

	$RCSfile: blog.php,v $
	$Revision: 1.16.2.3 $
	$Date: 2006/09/08 09:02:33 $
*/

define('CURSCRIPT', 'blog');

require_once './include/common.inc.php';
require_once DISCUZ_ROOT.'./include/blog.func.php';
require_once DISCUZ_ROOT.'./include/discuzcode.func.php';

$discuz_action = 151;
$uid = isset($uid) ? intval($uid) : 0;
$starttime = isset($starttime) ? intval($starttime) : 0;
$endtime = isset($endtime) ? intval($endtime) : 0;

$page = empty($page) || !ispage($page) ? 1 : $page;
$start_limit = ($page - 1) * $ppp;


if(!empty($uid)) {

	$query = $db->query("SELECT uid, username, groupid FROM {$tablepre}members WHERE uid='$uid'");
	if(!$member = $db->fetch_array($query)) {
		showmessage('blog_nonexistence');
	}

	$uid = $member['uid'];
	$username = $member['username'];

	$fidadd = $fid ? "AND t.fid='$fid'" : '';
	$starttimeadd = $starttime ? "AND t.dateline>='$starttime'" : '';
	$endtimeadd = $endtime ? "AND t.dateline<'$endtime'" : '';

	$query = $db->query("SELECT COUNT(*) FROM {$tablepre}threads t WHERE t.blog='1' AND t.authorid='$uid' $starttimeadd $endtimeadd $fidadd AND t.displayorder>='0'");
	$multipage = multi($db->result($query, 0), $ppp, $page, "blog.php?uid=$uid&amp;fid=$fid&amp;starttime=$starttime&amp;endtime=$endtime");

	$bloglist = array();
	$query = $db->query("SELECT t.tid, t.fid, t.authorid, t.author, t.subject, t.dateline, t.views, t.replies, t.special, t.attachment,
				f.name, f.allowsmilies, f.allowhtml, f.allowbbcode, f.allowimgcode, f.jammer,
				p.message, p.htmlon, p.smileyoff, p.bbcodeoff, p.rate, p.ratetimes
				FROM {$tablepre}threads t
				INNER JOIN {$tablepre}posts p ON p.tid=t.tid AND p.first='1'
				LEFT JOIN {$tablepre}forums f ON f.fid=t.fid
				WHERE t.blog='1' AND t.authorid='$uid' $starttimeadd $endtimeadd $fidadd AND t.displayorder>='0'
				ORDER BY t.dateline DESC LIMIT $start_limit, $ppp");

	$blognum = $db->num_rows($query);

	if(!$blognum) {

		$query = $db->query("SELECT username FROM {$tablepre}members WHERE uid='$uid'");
		if(!($username = $db->result($query, 0))) {
			showmessage('blog_nonexistence');
		}

		if($fid) {
			showmessage('blog_category_isnull');
		}

	} else {

		while($blog = $db->fetch_array($query)) {
			$blog['allowbbcode'] = $blog['allowbbcode'] ? ($_DCACHE['usergroups'][$member['groupid']]['allowcusbbcode'] ? 2 : 1) : 0;
			$blog['karma'] = karmaimg($blog['rate'], $blog['ratetimes']);
			$blog['postedon'] = gmdate($dateformat, $blog['dateline'] + $timeoffset * 3600);
			$blog['dateline'] = gmdate("$dateformat $timeformat", $blog['dateline'] + $timeoffset * 3600);
			$blog['message'] = discuzcode(cutstr($blog['message'], 800), $blog['smileyoff'], $blog['bbcodeoff'], $blog['htmlon'], $blog['allowsmilies'], $blog['allowbbcode'], $blog['allowimgcode'], $blog['allowhtml'], ($blog['jammer'] && $blog['authorid'] != $discuz_uid ? 1 : 0));

			$bloglist[] = $blog;
		}

	}

	$navigation = "&raquo; $username";
	$navtitle = "$username - ";

} elseif(!empty($tid)) {

	$attachpids = 0;
	$query = $db->query("SELECT t.fid, t.authorid, t.author, t.replies, t.closed, m.groupid, p.pid, p.subject, p.message, p.htmlon, p.smileyoff, p.bbcodeoff, p.attachment, p.rate, p.ratetimes
		FROM {$tablepre}threads t
		INNER JOIN {$tablepre}posts p ON p.tid=t.tid AND p.invisible='0'
		LEFT JOIN {$tablepre}members m ON m.uid=t.authorid
		WHERE t.tid='$tid' AND t.blog='1' AND t.displayorder>='0' ORDER BY p.dateline LIMIT 1");

	if(!$blogtopic = $db->fetch_array($query)) {
		showmessage('blog_topic_nonexistence');
	}

	$uid = $blogtopic['authorid'];
	$username = $blogtopic['author'];
	$thisbg = 'altbg1';

	$navigation = "&raquo; <a href=\"blog.php?uid=$uid&amp;starttime=$starttime&amp;endtime=$endtime\">$username</a> &raquo; $blogtopic[subject]";
	$navtitle = "$username - $blogtopic[subject]";

	$multipage = $multipage = multi($blogtopic['replies'], $ppp, $page, "blog.php?tid=$tid&amp;starttime=$starttime&amp;endtime=$endtime");

	$usesigcheck = $discuz_uid && $sigstatus ? 'checked' : '';
	$allowpostreply = (!$blogtopic['closed'] || $forum['ismoderator']) && ((!$forum['replyperm'] && $allowreply) || ($forum['replyperm'] && forumperm($forum['replyperm'])) || $forum['allowreply']);

	$forum['allowbbcode'] = $forum['allowbbcode'] ? ($_DCACHE['usergroups'][$blogtopic['groupid']]['allowcusbbcode'] ? 2 : 1) : 0;
	$blogtopic['message'] = discuzcode($blogtopic['message'], $blogtopic['smileyoff'], $blogtopic['bbcodeoff'], $blogtopic['htmlon'], $forum['allowsmilies'], $forum['allowbbcode'], $forum['allowimgcode'], $forum['allowhtml'], ($forum['jammer'] && $blogtopic['authorid'] != $discuz_uid ? 1 : 0));
	$blogtopic['karma'] = karmaimg($blogtopic['rate'], $blogtopic['ratetimes']);

	$blogtopic['attachments'] = array();
	if($blogtopic['attachment'] && (($allowgetattach && !$forum['getattachperm']) || (!$allowgetattach && forumperm($forum['getattachperm'])))) {

		require_once DISCUZ_ROOT.'./include/attachment.func.php';

		$query = $db->query("SELECT aid, pid, readperm, filename, description, filetype, attachment, filesize, downloads
					FROM {$tablepre}attachments WHERE pid='$blogtopic[pid]'");
		while($attach = $db->fetch_array($query)) {
			$extension = strtolower(fileext($attach['filename']));
			$attach['attachicon'] = attachtype($extension."\t".$attach['filetype']);
			$attach['attachsize'] = sizecount($attach['filesize']);
			$attach['attachimg'] = $attachimgpost && in_array($extension, array('jpg', 'jpeg', 'jpe', 'gif', 'png', 'bmp')) ? 1 : 0;
			$blogtopic['attachments'][$attach['aid']] = $attach;
		}
		$pid = $blogtopic['pid'];
		$temp[$pid] = $blogtopic;
		$temp[$pid]['message'] = preg_replace("/\[attach\](\d+)\[\/attach\]/ie", "attachtag($pid, \\1, \$temp)", $temp[$pid]['message']);
		$blogtopic = $temp[$pid];
		unset($temp);
		$blogtopic['attachment'] = 0;

	}
	$commentlist = array();
	if($blogtopic['replies']) {
		$query = $db->query("SELECT p.*, m.username, m.groupid, m.regdate, m.posts, m.credits
					FROM {$tablepre}posts p
					LEFT JOIN {$tablepre}members m ON m.uid=p.authorid
					LEFT JOIN {$tablepre}memberfields mf ON mf.uid=m.uid
					WHERE p.tid='$tid' AND p.invisible='0' AND p.pid<>'$blogtopic[pid]'
					ORDER BY p.dateline
					LIMIT $start_limit, $ppp");

		while($post = $db->fetch_array($query)) {

			if($post['username']) {
				if($userstatusby == 1 || $_DCACHE['usergroups'][$post['groupid']]['byrank'] === 0) {
					$post['authortitle'] = strip_tags($_DCACHE['usergroups'][$post['groupid']]['grouptitle']);
					$post['stars'] = $_DCACHE['usergroups'][$post['groupid']]['stars'];
				} elseif($userstatusby == 2) {
					foreach($_DCACHE['ranks'] as $rank) {
						if($post['posts'] > $rank['postshigher']) {
							$post['authortitle'] = $rank['ranktitle'];
							$post['stars'] = $rank['stars'];
							break;
						}
					}
				}

				$post['regdate'] = gmdate($dateformat, $post['regdate'] + $timeoffset * 3600);

			} else {

				if(!$post['authorid']) {
					$post['useip'] = substr($post['useip'], 0, strrpos($post['useip'], '.')).'.x';
				}
				$post['posts'] = $post['credits'] = $post['regdate'] = 'N/A';
			}

			$post['thisbg'] = $thisbg = isset($thisbg) && $thisbg == 'altbg1' ? 'altbg2' : 'altbg1';
			$post['dateline'] = gmdate("$dateformat $timeformat", $post['dateline'] + $timeoffset * 3600);
			$post['message'] = discuzcode($post['message'], $post['smileyoff'], $post['bbcodeoff'], $post['htmlon'], $forum['allowsmilies'], $forum['allowbbcode'], ($forum['allowimgcode'] && $showimages ? 1 : 0), $forum['allowhtml'], ($forum['jammer'] && $post['authorid'] != $discuz_uid ? 1 : 0), $pasetype, $post['authorid']);

			$post['attachments'] = array();
			if($post['attachment'] && (($allowgetattach && !$forum['getattachperm']) || (!$allowgetattach && forumperm($forum['getattachperm'])))) {
				$attachpids .= ",$post[pid]";
				$post['attachment'] = 0;
				if(preg_match("/\[attach\](\d+)\[\/attach\]/i", $post['message'])) {
					$attachtags[] = $post['pid'];
				}
			}
			$commentlist[$post['pid']] = $post;
		}

		$post['attachments'] = array();
		if($attachpids) {

			require_once DISCUZ_ROOT.'./include/attachment.func.php';

			$query = $db->query("SELECT aid, pid, readperm, filename, description, filetype, attachment, filesize, downloads
						FROM {$tablepre}attachments WHERE pid IN ($attachpids)");
			while($attach = $db->fetch_array($query)) {
				$extension = strtolower(fileext($attach['filename']));
				$attach['attachicon'] = attachtype($extension."\t".$attach['filetype']);
				$attach['attachsize'] = sizecount($attach['filesize']);
				$attach['attachimg'] = $attachimgpost && in_array($extension, array('jpg', 'jpeg', 'jpe', 'gif', 'png', 'bmp')) ? 1 : 0;
				$commentlist[$attach['pid']]['attachments'][$attach['aid']] = $attach;
				}
			foreach($attachtags as $pid) {
				$commentlist[$pid]['message'] = preg_replace("/\[attach\](\d+)\[\/attach\]/ie", "attachtag($pid, \\1, \$commentlist)", $commentlist[$pid]['message']);
			}
			$post['attachment'] = 0;
		}
	}

} else {
	showmessage('undefined_action', NULL, 'HALTED');
}

if($discuz_uid && $uid == $discuz_uid && $allowpost && $allowuseblog) {
	require_once DISCUZ_ROOT.'./include/forum.func.php';
	$forumselect = $fid ? preg_replace("/(\<option value=\"$fid\")(\>)/", "\\1 selected=\"selected\" \\2", forumselect()) : forumselect();
} else {
	$allowuseblog = 0;
}

$whosonline = array();
$membercount = $guestcount = 0;
$query = $db->query("SELECT username FROM {$tablepre}sessions WHERE bloguid='$uid' AND invisible='0'");
while($online = $db->fetch_array($query)) {
	if($online['username']) {
		$membercount++;
		$whosonline[] = $online['username'];
	} else {
		$guestcount++;
	}
}
$onlinenum = $membercount + $guestcount;
$whosonline = dhtmlspecialchars(implode(', ', $whosonline));

$query = $db->query("SELECT variable, value FROM {$tablepre}blogcaches WHERE uid='$uid'");
while($cache = $db->fetch_array($query)) {
	$_DCACHE['blog'][$cache['variable']] = unserialize($cache['value']);
}

if($timestamp - $_DCACHE['blog']['forums']['lastupdate'] > 43200) {
	updateblogcache($uid, 'forums');
}

if($timestamp - $_DCACHE['blog']['hot']['lastupdate'] > 86400) {
	updateblogcache($uid, 'hot');
}

$bloguid = $uid;
$calendar = calendar($starttime);

include template('blog');

?>