<?php

/***************************************************************************
 *
 *      Update to 2.0.15 by Alf (07.05.2005) 22:02
 *
 *	Title: xphpBBi - phpBB Complete Integration project
 *	Description: Project to turn the phpBB forums into a fully functioning
 *		Xoops2 Module. Based on the phpBB 2.0.9 port from BBpixel.com - 
 *		(PBBoard v1.22).
 *	Credits: Xoops2 Module Development Team - Koudanshi - phpBB Group
 *	Thanks: To Koudanshi for a great start in his port, and phpBB Group for
 *		such a great forum software.
 *	License: GNU/GPL version 2 or later.
 ****************************************************************************/

if (!defined('IN_PHPBB')) define('IN_PHPBB', true);

global $board_config;
include (XOOPS_ROOT_PATH.$board_config['script_path']."includes/functions.php");

function xphpbbi_show($options) {
	global $HTTP_COOKIE_VARS;
	global $board_config, $sid_xphpbbi, $uid_xphpbbi, $lastvisit_xphpbbi;
	global $xoopsDB, $xoopsUser, $xoopsTpl, $xoopsUserIsAdmin;
	
	$db =& Database::getInstance();
	$myts =& MyTextSanitizer::getInstance();
	$block = array();
    
	switch($options[2]) {
	case 'views':
		$order = 't.topic_views';
		break;
	case 'replies':
		$order = 't.topic_replies';
		break;
	case 'time':
	default:
		$order = 'p.post_time';
		break;
	}

	if(is_object($xoopsUser)){
		//$sql = "SELECT u.*, ue.* FROM ".USERS_TABLE." u, ".USERS_TABLE_EXT." ue WHERE u.uid='".$xoopsUser->getVar('uid')."' AND ue.uid='".$xoopsUser->getVar('uid')."'";
		$sql = "SELECT u.*, ue.* FROM (".USERS_TABLE." u LEFT JOIN " . USERS_TABLE_EXT . " ue ON u.uid=ue.uid) WHERE u.uid=".$xoopsUser->getVar('uid');
		$userinfo = $xoopsDB->fetchArray($xoopsDB->query($sql));
		//$sql1 = "SELECT 'last_login' FROM ".USERS_TABLE." WHERE uid='".$xoopsUser->getVar('uid')."'";
		//$last_login = $xoopsDB->fetchArray($xoopsDB->query($sql1));
		$last_login = $xoopsUser->getVar('last_login');
	}

	$SID = ($uid_xphpbbi > 0 ? '&amp;sid='.$sid_xphpbbi : '');

	if(is_object($xoopsUser)){
		if($userinfo['user_level']==ADMIN || $xoopsUserIsAdmin){
			$auth_level = AUTH_ADMIN;
		}elseif($userinfo['user_level']==MOD){
			$auth_level = AUTH_MOD;
		}elseif($userinfo['user_level']==USER){
			$auth_level = AUTH_REG;
		}
	}else{
		$auth_level = AUTH_ALL;
	}

	if(is_object($xoopsUser)){
		$sql = "SELECT f.forum_id
				FROM (".FORUMS_TABLE." f,".AUTH_ACCESS_TABLE." aa,".USER_GROUP_TABLE." ug)
				WHERE ug.uid = ".$xoopsUser->getVar('uid')."
				AND (f.forum_id=aa.forum_id
				AND aa.groupid=ug.groupid
				AND (aa.auth_read=1 OR aa.auth_mod=1) OR f.auth_read<='".$auth_level."')
				GROUP BY f.forum_id";
	}else{
		$sql = "SELECT f.forum_id
				FROM ".FORUMS_TABLE." f
				WHERE (f.auth_read<='".$auth_level."')
				GROUP BY f.forum_id";
	}
	$auth_forums='';
	$count = 0;
	if($result = $xoopsDB->query($sql)){
		while($auth_array = $xoopsDB->fetchArray($result)){
			if($count==0) $auth_forums.=' AND (';
			if($count>0) $auth_forums.=' OR ';
			$auth_forums .= "f.forum_id='".$auth_array['forum_id']."'";
			$count++;
		}
	}
	if($auth_forums!='') $auth_forums.=') ';
	$query = "SELECT t.topic_id, t.topic_title, t.topic_views, t.topic_replies, t.topic_status, t.topic_type,
			 p.post_username, p.poster_id, p.post_id, p.post_time, 
			 f.forum_id, f.forum_name, f.forum_status, f.auth_read
			 FROM (".TOPICS_TABLE." t, ".POSTS_TABLE." p, ".FORUMS_TABLE." f)
			 WHERE t.forum_id = f.forum_id
				AND (t.topic_last_post_id = p.post_id)
				$auth_forums
				ORDER BY ".$order." DESC";

	if (!$result = $xoopsDB->query($query,$options[0],0))
		return false;

	if ( $options[1] != 0 )
		$block['full_view'] = true;
	else 
		$block['full_view'] = false;
	
	$block['lang_forum']       = _MB_XPHPBBI_FORUM;
	$block['lang_topic']       = _MB_XPHPBBI_TOPIC;
	$block['lang_replies']     = _MB_XPHPBBI_RPLS;
	$block['lang_views']       = _MB_XPHPBBI_VIEWS;
	$block['lang_by']          = _MB_XPHPBBI_BY;
	$block['lang_view_last_post'] = _MB_XPHPBBI_VIEW_LAST_POST;
	$block['lang_lastpost']    = _MB_XPHPBBI_LPOST;
	$block['lang_visitforums'] = _MB_XPHPBBI_VSTFRMS;
	$block['lang_newposts']    = _MB_XPHPBBI_NEWPOSTS;

	//------------------------
	// Set board_config array
	//------------------------
	$board_config = array();
	$config = $xoopsDB->query("SELECT * FROM ".CONFIG_TABLE." ");
	
	while ( $bconfig = $xoopsDB->fetchArray($config))
		$board_config[$bconfig['config_name']] = $bconfig['config_value'];
		
	//------------------------
	// End set board_config
	//------------------------

	$tracking_topics = ( isset($HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_t']) ) ? unserialize($HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_t']) : '';
	$tracking_forums = ( isset($HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_f']) ) ? unserialize($HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_f']) : '';

	//------------------------
	// Set template name
	//------------------------
	$tpl_user = $xoopsDB->fetchArray($xoopsDB->query ("SELECT template_name FROM ".THEMES_TABLE." WHERE themes_id = ".$userinfo['user_style'].""));
	$tpl_default = $xoopsDB->fetchArray($xoopsDB->query ("SELECT template_name FROM ".THEMES_TABLE." WHERE themes_id = ".$board_config['default_style'].""));

	if ( $board_config['override_user_style'] or empty($tpl_user['template_name']))
		$tpl_name = $tpl_default['template_name'];
	else
		$tpl_name = $tpl_user['template_name'];

	//------------------------
	// END set template name
	//------------------------

 	while ($arr = $xoopsDB->fetchArray($result))
	{
		$sql = "SELECT uname FROM ".USERS_TABLE." WHERE uid = '".$arr['poster_id']."' LIMIT 1";
		$uname = $xoopsDB->fetchArray($xoopsDB->query($sql));

		//------------------------
		// Folder picture start
		//------------------------
		if( $arr['topic_status'] == TOPIC_MOVED ) {
			$icon_name = 'folder';
		} else {
			if( $arr['topic_type'] == POST_ANNOUNCE ) {
			    $img_name = 'folder_announce';
			    $img_name_new = 'folder_announce_new';
			} else if( $arr['topic_type'] == POST_STICKY ) {
			    $img_name = 'folder_sticky';
			    $img_name_new = 'folder_sticky_new';
			} else if( $arr['topic_status'] == TOPIC_LOCKED ) {
			    $img_name = 'folder_locked';
			    $img_name_new = 'folder_locked_new';
			} else if( $arr['topic_replies'] >= $board_config['hot_threshold']) {
			    $img_name = 'folder_hot';
			    $img_name_new = 'folder_new_hot';
			} else {
			    $img_name = 'folder';
			    $img_name_new = 'folder_new';
			}

			if (is_object($xoopsUser)) {
			    if( $arr['post_time'] > $userinfo['user_lastvisit'] ) 
			    {
				if( !empty($tracking_topics) || !empty($tracking_forums) || isset($HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_f_all']) ) 
				{
				    $unread_topics = true;

				    if( !empty($tracking_topics[$arr['topic_id']]) ) 
				    {
					if( $tracking_topics[$arr['topic_id']] >= $arr['post_time'] ) {
					    $unread_topics = false;
					}
				    }
		  
				    if( !empty($tracking_forums[$arr['forum_id']]) ) 
				    {
					if( $tracking_forums[$arr['forum_id']] >= $arr['post_time'] ) {
					    $unread_topics = false;
					}
				    }
		  
				    if( isset($HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_f_all']) ) 
				    {
					if( $HTTP_COOKIE_VARS[$board_config['cookie_name'].'_f_all'] >= $arr['post_time'] ) {
					    $unread_topics = false;
					}
				    }
		  
				    if( $unread_topics ) 
				    {
					$icon_name = $img_name_new;
					$topic['icon_newest_reply'] = '<a href="'.XOOPS_URL.$board_config['script_path'].'viewtopic.php?t='.$arr['topic_id'].'&amp;view=newest'.$SID.'"><img src="'.XOOPS_URL.$board_config['script_path'].'templates/'.$tpl_name.'/images/icon_newest_reply.gif" border="0" alt="'._MB_XPHPBBI_VIEW_NEWEST_POST.'" title="'._MB_XPHPBBI_VIEW_NEWEST_POST.'"></a>';
				    } else {
					$icon_name = $img_name;
					$topic['icon_newest_reply'] = '<a href="'.XOOPS_URL.$board_config['script_path'].'viewtopic.php?t='.$arr['topic_id'].'&amp;view=newest'.$SID.'"><img src="'.XOOPS_URL.$board_config['script_path'].'templates/'.$tpl_name.'/images/icon_newest_reply.gif" border="0" alt="'._MB_XPHPBBI_VIEW_NEWEST_POST.'" title="'._MB_XPHPBBI_VIEW_NEWEST_POST.'"></a>';
				    }
				} else {
				    $icon_name = $img_name_new;
				    $topic['icon_newest_reply'] = '<a href="'.XOOPS_URL.$board_config['script_path'].'viewtopic.php?t='.$arr['topic_id'].'&amp;view=newest'.$SID.'"><img src="'.XOOPS_URL.$board_config['script_path'].'templates/'.$tpl_name.'/images/icon_newest_reply.gif" border="0" alt="'._MB_XPHPBBI_VIEW_NEWEST_POST.'" title="'._MB_XPHPBBI_VIEW_NEWEST_POST.'"></a>';
				}
			    } else {
				$icon_name = $img_name; // End tracking = true
				$topic['icon_newest_reply'] = "<img src='".XOOPS_URL."/uploads/blank.gif'>";
			    }
			} else {
			    $icon_name = $img_name;	//End uid_bb
			    $topic['icon_newest_reply'] = "<img src='".XOOPS_URL."/uploads/blank.gif'>";
			}
		}	// End topic_movie

		//------------------------			//End hack image_icon_show
		//------------------------
		if (empty($arr['post_username'])) {
			$topic['isuser']=1;
			$topic['poster_name']    = $uname['uname'];
		}				
		else {
			$topic['isuser']=0;
			$topic['poster_name']    = $arr['post_username'];
		}							
		$topic['forum_id'] 	= $arr['forum_id'];
		$topic['forum_name'] 	= $myts->makeTboxData4Show($arr['forum_name']);
		$topic['id'] 		= $arr['topic_id'];
		$topic['title']	 	= $myts->makeTboxData4Show($arr['topic_title']);
		$topic['replies'] 	= $arr['topic_replies'];
		$topic['views'] 	= $arr['topic_views'];
//		$topic['time']          = formatTimestamp($arr['post_time'], ($userinfo['user_dateformat'] ? $userinfo['user_dateformat'] : $board_config['default_dateformat']));
		$topic['time'] 		= formatTimestamp($arr['post_time'], 'd/m/Y G:i');
//		$topic['time']          = create_date(($userinfo['user_dateformat'] ? $userinfo['user_dateformat'] : $board_config['default_dateformat']), $arr['post_time'], $board_config['board_timezone']);
		$topic['sid'] 		= $SID;
		$topic['poster_id']	= $arr['poster_id'];
		$topic['post_id']    	= $arr['post_id'];
		$topic['pages'] 	= show_page($arr['topic_replies'],$arr['topic_id']);
		$topic['img_dir'] 	= XOOPS_URL.$board_config['script_path']."templates/".$tpl_name."/images/".$icon_name.".gif";
		$topic['icon_latest_reply'] = XOOPS_URL.$board_config['script_path']."templates/".$tpl_name."/images/icon_latest_reply.gif";
		$block['topics'][] 	= & $topic;
		unset($topic);
	}	// End while

   	return $block;
}	// End function

function xphpbbi_edit($options) {
	global $xoopsDB, $xoopsUser, $xoopsTpl;
	$inputtag = "<input type='text' name='options[0]' value='".$options[0]."' />";
	$form = sprintf(_MB_XPHPBBI_DISPLAY,$inputtag);
	$form .= "<br />"._MB_XPHPBBI_DISPLAYF."&nbsp;<input type='radio' name='options[1]' value='1'";
	if ( $options[1] == 1 ) {
		$form .= " checked='checked'";
	}
	$form .= " />&nbsp;"._YES."<input type='radio' name='options[1]' value='0'";
	if ( $options[1] == 0 ) {
		$form .= " checked='checked'";
	}
	$form .= " />&nbsp;"._NO;
	$form .= '<input type="hidden" name="options[2]" value="'.$options[2].'">';
	return $form;
}


function show_page ($data,$t)
{
	global $sid_xphpbbi, $board_config;
	global $xoopsDB, $xoopsUser, $xoopsTpl;

	$pages = 1;
	if ( (($data + 1) % $board_config['posts_per_page']) == 0){
		$pages = ($data + 1) / $board_config['posts_per_page'];
	}else{		
		$number = ( ($data + 1) / $board_config['posts_per_page'] );
		$pages = ceil( $number);
	}
	$pages_link = '';
	if ($pages > 1)
	{
		$pages_link = "<span style='font-size:10px; font-weight:bold; font-family:verdana,tahoma;'>("._MB_XPHPBBI_PAGES."";
		for ($i = 0 ; $i < $pages ; ++$i )
		{
			$real_no = $i * $board_config['posts_per_page'];
			$page_no = $i + 1;

			if ($page_no ==4)
			{
				$pages_link .= " <a href='".XOOPS_URL.$board_config['script_path']."viewtopic.php?t=$t&start=".($pages - 1) * $board_config['posts_per_page']."'>... $pages</a>";
				break;
			}else{
				$pages_link .= " <a href='".XOOPS_URL.$board_config['script_path']."viewtopic.php?t=$t&start=$real_no'>$page_no</a> ";
			}
		}
		$pages_link .= " )</span>";
	}
	return $pages_link;
}
?>