<?php
ob_start();
if(!(is_readable(realpath(dirname(__FILE__)).'/include/includesroutines.inc'))) {
	die('Error - Could not open or read file '.realpath(dirname(__FILE__)).'/include/includesroutines.inc');
} else {
	include_once(realpath(dirname(__FILE__)).'/include/includesroutines.inc');
}
if(!(is_readable(realpath(dirname(__FILE__)).'/include/downloads.inc'))) {
	die('Error - Could not open or read file '.realpath(dirname(__FILE__)).'/include/downloads.inc');
} else {
	include_once(realpath(dirname(__FILE__)).'/include/downloads.inc');
}

if(!(is_readable(realpath(dirname(__FILE__)).'/include/pagination.inc'))) {
	die('Error - Could not open or read file '.realpath(dirname(__FILE__)).'/include/pagination.inc');
} else {
	include_once(realpath(dirname(__FILE__)).'/include/pagination.inc');
}
checkUserAndRedirectToLogin();

if (isset($_COOKIE['mobilemode']) && $_COOKIE['mobilemode'] == 0) {
	header("Location: businessmodel.php?".$_SERVER["QUERY_STRING"]);
	ob_end_flush();
	exit;
}
$cn = db_connect();
$lng = ( isset($_REQUEST['lang']) ? 'lang='.$_REQUEST['lang'] : '');
$lngid = db_getLanguageID($cn, getLanguage());
$lngshort = db_getLanguageShortByID($cn, $lngid);
$contlng = $lng;
$contlngid = $lngid;
if (getSetting('portal','useRights',0) == 1){
	$contlng = 'lang='.db_getLanguageShortByID($cn, $_SESSION['user_idlanguagecontent']);
	$contlngid = $_SESSION['user_idlanguagecontent'];
}
$hideheader = (isset($_COOKIE['hideheader']) ?  $_COOKIE['hideheader'] : 0);

if ( getSetting('portal','useRights',0) == 1 && !IsUserLogin()){
	header("Location: ".getCurPageURLWO()."/login.php?".$lng);
	ob_end_flush();
	db_disconnect($cn);
	exit;
} else if ( GetSetting('portal','useRights',0) == 0 ) {
	header("Location: ".getCurPageURLWO()."/businessmodel.php?".$lng);
	ob_end_flush();
	db_disconnect($cn);
	exit;
}

$id = 0;

if (isset($_REQUEST['idbranch'])) {
	$idbranch = $_REQUEST['idbranch'];
	$skbranch = db_getSKBranch($cn,$idbranch);	
} else {
	$skbranch = db_getMainSKBranch($cn);
	$idbranch = db_getMainIDBranch($cn);	
}

if (!isset($_REQUEST['idbranch'])){
	$target = "businessmodel.php?";
	if  (!isset($_REQUEST['oguid']) || !db_isObjectExits($cn, $_REQUEST['oguid'], $skbranch) || db_getUserRightsToObject($cn, GetUserIDs(), $_REQUEST['oguid'], $skbranch) === null ){
			db_disconnect($cn);
			header("Location: ".getCurPageURLWO()."/pagenotfound.php?url=".getCurPageWithEncodeParams());
			ob_end_flush();
			exit;
	}
	$oguid = $_REQUEST['oguid'];
} else {
	if (!isset($_REQUEST['skpollingobject']) || !isset($_REQUEST['skpolltype']) || !is_numeric($_REQUEST['skpollingobject']) || !is_numeric($_REQUEST['skpolltype'])) {
			header("Location: ".getCurPageURLWO()."/pagenotfound.php?url=".getCurPageWithEncodeParams());
			ob_end_flush();
			exit;
	}
	$target = "pollobject.php?skpollingobject=".$_REQUEST['skpollingobject']."&skpolltype=".$_REQUEST['skpolltype'].(isset($_REQUEST['rguid']) ? '&rguid='.$_REQUEST['rguid'] : '').(isset($_REQUEST['skproject']) ? '&skproject='.$_REQUEST['skproject'] : '').(isset($_REQUEST['skbranch']) ? '&skbranch='.$_REQUEST['skbranch'] : '').'&idbranch='.$idbranch.''.(isset($_REQUEST['idpolltype']) ? '&idpolltype='.$_REQUEST['idpolltype'] : '')."&";
	$poguids = db_getGuidsByPollingObject($cn, $_REQUEST['skpollingobject']);
	if (is_array($poguids) && sizeof($poguids) > 0 && sizeof($poguids[0]) > 0){
			$oguid = $poguids[0]['o_guid'];
			$rguid = $poguids[0]['r_guid'];
	} else {
			db_disconnect($cn);
			header("Location: ".getCurPageURLWO()."/pagenotfound.php?url=".getCurPageWithEncodeParams());
			ob_end_flush();
			exit;
	}
	
}
$url = getCurPageURL();

if  (!isset($_REQUEST['oguid']) || !db_isObjectExits($cn, $_REQUEST['oguid'], $skbranch) || db_getUserRightsToObject($cn, GetUserIDs(), $_REQUEST['oguid'], $skbranch) === null ){
		db_disconnect($cn);
		header("Location: ".getCurPageURLWO()."/pagenotfound.php?".$lng."&url=".getCurPageWithEncodeParams());
		ob_end_flush();
		exit;
}

$oguid = $_REQUEST['oguid'];
$rguid = (isset($_REQUEST['rguid']) ? $_REQUEST['rguid'] : null);
$usr_arr = GetUserIDs();
$usrlink = db_getUsersToObjectlinks($cn,$oguid, $skbranch );

$frmm_body = '';
if (isset($_REQUEST['forummessage']) && $_REQUEST['forummessage'] != ''){
	$frmm_body = $_REQUEST['forummessage'];
}

if (isset($_FILES["file"]) && ($_FILES["file"]["size"] > 0) && ($frmm_body == '')){
	$frmm_body = '  ';
}

if(isset($_REQUEST['forumadd']) && !(isset($_REQUEST['rep'])) && ($frmm_body != '' )) {
	$idforummessageparetn = 0;
	$id = db_addForumMessages($cn, $idforummessageparetn, $oguid, $usr_arr[0] , $frmm_body);
}

if(isset($_REQUEST['forumedit']) && (IsUserAdmin() || in_array($usr_arr[0], db_getUsersIsAuthorsToForumMessage($cn, $_REQUEST['edit'])))) {
		db_editForumMessages($cn, $_REQUEST['edit'], $usr_arr[0] , $frmm_body);
}

if(isset($_REQUEST['forumrep']) && isset($_REQUEST['rep']) && ($frmm_body != '' )) {
	$idforummessageparetn = $_REQUEST['rep'];
	$id = db_addForumMessages($cn, $idforummessageparetn, $oguid, $usr_arr[0] , $frmm_body);
}

if (isset($_FILES["file"]) && ($_FILES["file"]["size"] > 0)){
   if( is_uploaded_file($_FILES["file"]["tmp_name"]))
   {
     // Если файл загружен успешно, перемещаем его
     // из временной директории в конечную
     $dir = realpath(dirname(__FILE__))."/files/".$id;
   	 if (!file_exists ( $dir )) {
	     mkdir($dir,0777);
		}
	$file_name = $_FILES["file"]["name"];
	$file_path = $dir."/".$_FILES["file"]["name"];
        move_uploaded_file($_FILES["file"]["tmp_name"], $file_path);
  	db_addForumFile($cn, $id, $file_name, $file_path);
   } else {
      getResource("message.errorLoadFile");
   }
}

if (isset($id) && $id > 0 ) {
	stream_context_set_default( [
		'ssl' => [
			'verify_peer' => false,
			'verify_peer_name' => false,
		],
	]);
	$key =  $usr_arr[0].$_REQUEST['oguid'].$id.$idforummessageparetn;
	get_headers(getCurPageURLWO().'/sendnotificationfromforum.php?'.$_SERVER["QUERY_STRING"].'&uid='.$usr_arr[0].'&id='.$id.'&idforummessageparetn='.$idforummessageparetn.'&key='.md5 ($key));
}

if(isset($_REQUEST['forumrep']) ) {
	header("Location: ".urlReplaceParam(urlReplaceParam($url,'forumid'),'rep').'&forumid='.$id.'#'.$id);
	ob_end_flush();
	exit;
}

if(isset($_REQUEST['forumedit']) ) {
	header("Location: ".urlReplaceParam(urlReplaceParam($url,'forumid'),'edit').'&forumid='.$_REQUEST['edit'].'#'.$_REQUEST['edit']);
	ob_end_flush();
	exit;
}

if(isset($_REQUEST['forumadd']) ) {
	header("Location: ".urlReplaceParam($url,'forumid').'&forumid='.$id.'#'.$id);
	ob_end_flush();
	exit;
}

if(isset($_REQUEST['delete']) && (IsUserAdmin() || in_array($usr_arr[0],$usrlink)) ) {
	db_markForumMessageAsDelete($cn, $_REQUEST['delete']);
	header("Location: ".urlReplaceParam($url,'delete'));
	ob_end_flush();
	exit;
}

$objectName = db_getObjectName($cn, $oguid, $lngid);
$reportName = '';
if ($oguid !== $rguid) {
	db_getReportName($cn, $rguid, $lngid);
}
   if (isset($_REQUEST['oguid'])){
	$url = urlReplaceParam($url,'forumid', null);
        db_getNewForumMessages($cn, $_REQUEST['oguid'], $usr_arr[0], $skbranch, 1);
	$tr_arr = db_getForumMessages($cn, $_REQUEST['oguid'], $usr_arr[0], $skbranch);
	$fl_arr = null;
	$size=sizeof($tr_arr);
	$fllnk = '';

	$total = $size;
	$onpage = getSetting('page','forum.messageonpage',10);
	$total = ceil($total/$onpage); //Также вычисляем сколько у нас будет ссылок
        $total = $total == 0 ? 1 : $total;
        if (session_status() == PHP_SESSION_NONE) {
                session_start();
        }
        if (isset($_SESSION['page'.$_REQUEST['oguid']]) && is_numeric($_SESSION['page'.$_REQUEST['oguid']]) && !isset($_REQUEST['page'])){ /* 62659 */
                $page = $_SESSION['page'.$_REQUEST['oguid']]; 
        } else {
                $page = (isset($_REQUEST['page']) && $_REQUEST['page'] != 'last' ? $_REQUEST['page'] : $total ); /*61889*/
        }
        $page = (($page > $total) ? $total : $page);

        $_SESSION['page'.$_REQUEST['oguid']] = $page;
        session_write_close();
	$msgexists = true;
        if (isset($_REQUEST['forumid']) && $_REQUEST['forumid'] != "" && $_REQUEST['forumid'] != 0) {
    	        $msgexists = false;
                for($i=0; $i< $size; $i++){
                        if ($_REQUEST['forumid'] == $tr_arr[$i]['idforummessage']){
                                $page = (ceil(($i+1) / $onpage) < 1 ? 1 : ceil(($i+1) / $onpage));
                                $msgexists = true;
                                db_setForumMessageWasSaw($cn, $tr_arr[$i]['idforummessage'], TRUE);
                                break;
                        }
                }
        }
}
?>
<!doctype html>
<html>
<head>
	<title><?php echo renderTitle();?></title>
	<? 
	renderCommonMeta(renderTitle());
	renderCommonCSS(); 
	?>
</head>
<script src="js/libs/jquery.js"></script>
<script type="text/javascript" src="js/script.js"></script>
<body>
	<?php renderModalHourGlassDiv();?>
	<div id="container<?php echo ($size > 0 ? "" : "_m");?>">
	<table class="self-binding-width" style="width: 100%;"><tr><td>
	<?php renderLoginBlock(); ?>
	</td></tr><tr><td>
	<div id="main" role="main" class="ui-layout-center">
		<?php renderMainMenu(); ?>
		<a id="btn-tree-open" class="btn-tree-open-usual" style="display: inline;text-decoration: none;" href="mnavigator.php?<?php echo $lng.'&'.(($oguid === null) ? "" : 'oguid='.$oguid.''); ?>"><span <span class="btn-left-open-span"><?php getResource("field.navigator");?></span></a>
		<section id="box-tree-full" class="clearfix ui-layout-button-close ui-layout-button-close-east">
			<a style="text-decoration: none;" href="<?php  echo getCurPageURLWO()."/businessmodel.php?".$lng."&".(($oguid === null) ? "" : 'oguid='.$oguid.'').( ( $oguid === null && !($rguid === null) ) ? "" : "&").(($rguid === null) ? "" : 'rguid='.$rguid); ?>" title="<?php getResource('button.showreport.hint'); ?>">
				<header>
					<h1 style="background-image: url(<?php  echo getCurPageURLWO();?>/img/btn-layout-east-close.png);background-position: right center;background-repeat: no-repeat;"><?php getResource("button.showforum");?></h1>
				</header>
			</a>

			<div class="ui-layout-center" style="padding: 10px;" >
<?php if (!$msgexists) { ?>
<section id="box-paginator" class="box-paginator-forum box-paginator-clearborder">
<?php getResource("message.messageWasDelete");?>
</section>
<?php } ?>
   				<section id="box-paginator" class="box-paginator-forum box-paginator-clearborder">
<?php
	// check is new messages has present
	$hasnewmsg = -1;
	$linkisactive = false;
	$fm_idleft = -1;
	$issetforumid = isset($_REQUEST['forumid']) && $_REQUEST['forumid'] > 0 && strlen($_REQUEST['forumid']) > 0;
	$issetforumrepid = isset($_REQUEST['rep']) && $_REQUEST['rep'] > 0 && strlen($_REQUEST['rep']) > 0;
	for($i=($page == 1 ? 0 : ($page - 1) * $onpage); $i < $size; $i++){
	 	if ($issetforumid && $fm_idleft < 0 && $_REQUEST['forumid'] == $tr_arr[$i]['idforummessage'] ){
				$fm_idleft = $tr_arr[$i]['frmm_ileft'];
		}
	 	if ($issetforumrepid && $fm_idleft < 0 && $_REQUEST['rep'] == $tr_arr[$i]['idforummessage'] ){
				$fm_idleft = $tr_arr[$i]['frmm_ileft'];
		}
		if ($tr_arr[$i]['isnewmessage'] == 1) {
			$hasnewmsg = $i;
			if (!$issetforumid && !$issetforumrepid) {
				$linkisactive = true;
				break;
			} else if ($fm_idleft > 0 && $fm_idleft < $tr_arr[$hasnewmsg]['frmm_ileft'] ){
				$linkisactive = true;
				break;
			}
		}
        }
        if (!(null === $total || $total == 0) && $total > 1) {
                echo_panel_page($total, $page, $url);
        } else {
                echo "<ul/>";
        }
        if ($hasnewmsg > -1 ) {
                echo '<a class="box-paginator-hasnewmessages-link '.($linkisactive ? '' : 'link-disabled').' " target="_parent" href="'.($linkisactive ? ''.$target.'oguid='.$_REQUEST['oguid'].'&forumid='.$tr_arr[$hasnewmsg]['idforummessage'].'#'.$tr_arr[$hasnewmsg]['idforummessage'] : 'javascript:void(0)').'">'.getResourceByLang('field.forum.gotonewmessagelink').' ></a>&#8203;&#8203;';
        }
?>
	</section>
			<table style="width: 100%;"><tr><td>
<?php
if (isset($_REQUEST['oguid'])){
    if ($size > 0 ) {
		echo "<div class='box-forum'>";
                        $rownumb = 1;
                        for($i=($page == 1 ? 0 : ($page - 1) * $onpage); $i< ($size < $page * $onpage  ? $size : $page * $onpage) ; $i++){
                                $fllnk = '';
                                echo '<div class="b-forum-message clearfix" style="margin-left: '.(30 * ( $tr_arr[$i]['frmm_depth'] - 1 )).'px ">';
                                $bodyoriginal = linkTextReplacer($tr_arr[$i]['frmm_body']);
                                $ind_1 = 0;
                                $ind_2 = 0;
                                $l_oguid = null;
                                $l_rguid = null;
                                $offset = 0;
                                $lpos = 1;
                                $body = '';
                                $isinLink = FALSE;
                                while ($lpos !== FALSE) {
                                        $lpos = strpos($bodyoriginal, 'businessmodel', $offset);
                                        if ( $lpos !== FALSE ){
                                                $ind_1 = 0;
                                                $ind_2 = 0;
                                                $isinLink = FALSE;
                                                for($n1=$lpos; $n1 > -1; $n1--){
                                                        $ind_1 = $n1;
                                                        if ($bodyoriginal[$n1] == ' ' || $bodyoriginal[$n1] == '<' || $bodyoriginal[$n1] == '>' || $bodyoriginal[$n1] ==';' || $n1 == 0){
                                                                $ind_1 = ($n1 == 0 ? $n1 : $n1 + 1);
                                                                break;
                                                        }
                                                        if ($bodyoriginal[$n1] == '"' || $bodyoriginal[$n1] == "'" ){
                                                                if ($n1 > 5 && $bodyoriginal[$n1 - 1] == "=" && strcasecmp($bodyoriginal[$n1 - 1], "=") == 0 && strcasecmp($bodyoriginal[$n1 - 2], "f") == 0 ){
                                                                        $isinLink = TRUE;
                                                                }
                                                                break;
                                                        }
                                                }
                
                                                for($n1=$lpos; $n1 < strlen($bodyoriginal); $n1++){
                                                        $ind_2 = $n1;
                                                        if ($bodyoriginal[$n1] == ' ' || $bodyoriginal[$n1] == '<' || $bodyoriginal[$n1] == '>'){
                                                                break;
                                                        }
                                                        if ($bodyoriginal[$n1] == '"' || $bodyoriginal[$n1] == "'" ){
                                                                $isinLink = $isinLink && TRUE;
                                                                if ($isinLink){
                                                                        for($endIndx=$lpos; $endIndx < strlen($bodyoriginal); $endIndx++){
                                                                                if ($bodyoriginal[$endIndx] == '<'){
                                                                                        $ind_2 = $endIndx - 1;
                                                                                        break;
                                                                                }
                                                                        } 
                                                                }
                                                                break;
                                                        }
                                                }
                
                                                if ($ind_2 == strlen($bodyoriginal) - 1) {
                                                        $ind_2++;
                                                }

                                                if ($isinLink) {
                                                        $body = $body.substr($bodyoriginal, $offset, $ind_2 - $offset); 
                                                        $offset = $ind_2;     
                                                } else {   
                                                        $bslnk = substr($bodyoriginal, $ind_1 + 1, $ind_2 - $ind_1 - 1 );
                                                        $parts = parse_url($bslnk);
                                                        if(isset($parts['query'])) {
                                                                parse_str(urldecode($parts['query']), $parts['query']);
                                                        }
                        
                                                        if (isset($parts['query']['oguid'])) {
                                                                $l_oguid = $parts['query']['oguid'];
                                                        } else if (isset($parts['query']['amp;oguid'])) {
                                                                $l_oguid = $parts['query']['amp;oguid'];
                                                        }
                        
                                                        if (isset($parts['query']['rguid'])) {
                                                                $l_rguid = $parts['query']['rguid'];
                                                        } else if (isset($parts['query']['amp;rguid'])) {
                                                                $l_rguid = $parts['query']['amp;rguid'];
                                                        }
                        
                                                        if (isset($parts['query']['lang'])) {
                                                                $l_lang = 'lang='.$parts['query']['lang'];
                                                        } else if (isset($parts['query']['amp;lang'])) {
                                                                $l_lang = 'lang='.$parts['query']['amp;lang'];
                                                        } else{
                                                                $l_lang = '';
                                                        }	
                                                        
                                                        $href = './businessmodel.php?'.(strlen($l_lang)>0 ? $l_lang : $contlng).'&oguid='.$l_oguid.($l_rguid === null || $l_rguid == '' ? ' ' : '&rguid='.$l_rguid);
                                                        $rname = db_getReportName($cn, $l_rguid, $contlngid);
                                                        $oname = db_getObjectName($cn, $l_oguid, $contlngid);
                        
                                                        $bslnk = '<a target="_parent" href="'.$href.'">'.$oname.($rname === null || $rname == '' ? ' ' : ' ('.$rname.')').'</a>';
                                                        $body = $body.substr($bodyoriginal, $offset, $ind_1 - $offset ).' '.$bslnk.' '; 
                                                        $offset = $ind_2;     
                                                }
                                        } else {
                                                $body = $body.substr($bodyoriginal, $offset, strlen($bodyoriginal) - $offset);
                                                $offset = strlen($bodyoriginal);
                                        }
                                }
                                echo '<a name="'.$tr_arr[$i]['idforummessage'].'"></a>';

                                // link for first message in chain
                                if (($tr_arr[$i]['frmm_iright'] - $tr_arr[$i]['frmm_ileft'] > 1 && $tr_arr[$i]['parent_idforummessage'] > 0 ) || $tr_arr[$i]['frmm_depth'] > 1){
                                        if ($rownumb == 1){
                                                $m_root = db_getForumMessageRoot($cn, $tr_arr[$i]['idforummessage']);
                                                //64016
                                                $targetself =  urlReplaceParam(getCurPageURL(), "forumid", $m_root);
                                                $targetself =  urlReplaceParam($targetself, "rep", null);                                                        
                                                echo '<a style="" target="_parent" class="self-event forum-selfopen-link" jshref="'.$targetself.'#'.$m_root.'" href="'.$target.'oguid='.$_REQUEST['oguid'].'&forumid='.$m_root.'#'.$m_root.'">'.getResourceByLang("link.msginchain.first").'</a> <br/><br/>';
                                        }
                                }
                                if (isset($_REQUEST['rep']) && ($_REQUEST['rep'] == $tr_arr[$i]['idforummessage'])) {
                                        echo '<div class="b-forum-message__header"><span class="b-forum-message__name"><i id="selected-forumid" class="fa fa-play forum-fa-play" aria-hidden="TRUE"></i><a href="mailto:'.$tr_arr[$i]['user_email'].'">'.$tr_arr[$i]['user_name'].'</a></span> <span class="b-forum-message__date">'.date( GetSetting('portal','dataFormat','m.d.y H:i:s'), strtotime( $tr_arr[$i]['frmm_datechange'] ) ).'</span></div><div class="b-forum-message__body">'.$body.'<div class="cleaner"></div></div>';
                                        if ($tr_arr[$i]['file_name'] === null ) {
                                                echo '';
                                        } else {
                                                if (is_image_byidforum($cn, $tr_arr[$i]['idforummessage'], $tr_arr[$i]['idfile']) ){
                                                        echo '<img class="forum-inline-message" style="max-width:400px; max-height:300px" src="downloadforumfile.php?idfile='.$tr_arr[$i]['idfile'].'&idforummessage='.$tr_arr[$i]['idforummessage'].'"/><br/>'.$tr_arr[$i]['file_name'].'<br/><br/>';
                                                } else {
                                                        echo '<div class="b-forum-message__file">'.getResourceByLang('field.WasAttachAFile').':&nbsp;';
                                                        echo '<a target="_blank" href="downloadforumfile.php?idfile='.$tr_arr[$i]['idfile'].'&idforummessage='.$tr_arr[$i]['idforummessage'].'">'.$tr_arr[$i]['file_name'].'</a>';
                                                        echo '</div>';
                                                }
                                        }
                                        $linkLine = '';
                                        echo '<div class="b-forum-message__form"><form name="sendForm" id="sendForm"  action="" method="post" enctype="multipart/form-data">';
                                        echo '  <textarea id="forummessage" name="forummessage" rows="4" style="display:none;width:99%;"></textarea><br/><br/> ';
                                        echo '  <input type="hidden" name="forumrep" value="TRUE"> ';
                                        echo '  <input type="hidden" name="forumid" value="'.$tr_arr[$i]['idforummessage'].'#'.$tr_arr[$i]['idforummessage'].'">                                 ';
                                        echo '  '.getResourceByLang('field.AttachAFile').': <input type="file" id="file" name="file"><br/><br/>                               ';
                                        echo '  <span><button name="buttonSendForm" id="buttonSendForm" class="btn-top btn-top-text" title="'.getResourceByLang('button.Send.hint').'"><span>'.getResourceByLang('button.Send').'</span></button></span> ';
                                        echo ' </form></div>';

                                }else if (isset($_REQUEST['edit']) && ($_REQUEST['edit'] == $tr_arr[$i]['idforummessage']) ){
                                        echo '<div class="b-forum-message__form"><form name="sendForm" id="sendForm"  action="" method="post" enctype="multipart/form-data">';
                                        echo '  <textarea id="forummessage" name="forummessage" rows="4" style="display:none;width:99%;">'.$bodyoriginal.'</textarea><br/><br/> ';
                                        echo '  <input type="hidden" name="forumedit" value="TRUE">                                 ';
                                        echo '  <input type="hidden" name="forumid" value="'.$tr_arr[$i]['idforummessage'].'#'.$tr_arr[$i]['idforummessage'].'">                                 ';
                                        echo '  <span><button name="buttonSendForm" id="buttonSendForm" class="btn-top btn-top-text" title="'.getResourceByLang('button.Send.hint').'"><span>'.getResourceByLang('button.Send').'</span></button></span> ';
                                        echo ' </form></div>';
                                } else {
                                        $candelete = ($i == ($size - 1)) || (($i < ($size - 1)) && ($tr_arr[$i]['idforummessage'] <> $tr_arr[$i + 1]['parent_idforummessage']));
                                        $cnfrm = getResourceByLang("confirm.deletemessage");
                                        $delbtn = getResourceByLang("button.delete");
                                        $editbtn = getResourceByLang("button.edit");
                                        //64016
                                        $targetself =  urlReplaceParam(getCurPageURL(), "forumid", $tr_arr[$i]['idforummessage']);
                                        $targetself =  urlReplaceParam($targetself, "rep", null);

                                        echo '<div class="b-forum-message__header"><span class="b-forum-message__name">'
                                                .((isset($_REQUEST['forumid']) && ($_REQUEST['forumid'] == $tr_arr[$i]['idforummessage']) ) ? '<i id="selected-forumid" class="fa fa-play forum-fa-play" aria-hidden="TRUE"></i>' : '' )
                                                .($tr_arr[$i]['isnewmessage'] == 1 ? '<span class="b-forum-message__new">'.getResourceByLang('field.forum.isnew').'</span>&nbsp;&nbsp;' : '')
                                                .'<a href="mailto:'.$tr_arr[$i]['user_email'].'">'
                                                .$tr_arr[$i]['user_name']
                                                .'</a></span> <span class="b-forum-message__date"><a target="_parent" class="self-event forum-selfopen-link" jshref="'.$targetself.'" href="mforum.php?oguid='.$_REQUEST['oguid'].'&forumid='.$tr_arr[$i]['idforummessage'].'#'.$tr_arr[$i]['idforummessage'].'">'.date( GetSetting('portal','dataFormat','m.d.y H:i:s'), strtotime( $tr_arr[$i]['frmm_datechange'] ) )
                                                .($tr_arr[$i]['frmm_ischanged'] == 1 ? '&nbsp;('.getResourceByLang('field.forum.wasedit').')' : '').'</a></span>'
                                                .'</div>'
                                                .'<div class="b-forum-message__body">'.$body.'<div class="cleaner"></div></div>';
                                                                
                                        $linkLine = '<a href="'.urlReplaceParam(urlReplaceParam(urlReplaceParam($url,'edit'),'page',$page),'rep',$tr_arr[$i]['idforummessage']).'#'.$tr_arr[$i]['idforummessage'].'">'.getResourceByLang('button.Reply').'</a>'
                                                .($usr_arr[0] == $tr_arr[$i]['iduser'] ? '&nbsp;&nbsp;<a href="'.urlReplaceParam(urlReplaceParam(urlReplaceParam($url,'rep'),'page',$page),'edit',$tr_arr[$i]['idforummessage']).'&forumid='.$tr_arr[$i]['idforummessage'].'#'.$tr_arr[$i]['idforummessage'].'" >'.$editbtn.'</a>' : '' )
                                                .(((isUserAdmin() || in_array($usr_arr[0], $usrlink)) && $candelete ) ? '&nbsp;&nbsp;<a href="'.urlReplaceParam(urlReplaceParam(urlReplaceParam(urlReplaceParam($url,'rep'),'edit'),'page',$page),'delete',$tr_arr[$i]['idforummessage']).'&forumid='.$tr_arr[$i]['parent_idforummessage'].'#'.$tr_arr[$i]['parent_idforummessage'].'" onClick="return confirm(\''.$cnfrm.'\')">'.$delbtn.'</a>' : '' );

                                        if ($tr_arr[$i]['file_name'] === null ) {
                                                echo '';
                                        } else {
                                                if (is_image_byidforum($cn, $tr_arr[$i]['idforummessage'], $tr_arr[$i]['idfile']) ){
                                                        echo '<img class="forum-inline-message" style="max-width:400px; max-height:300px" src="downloadforumfile.php?idfile='.$tr_arr[$i]['idfile'].'&idforummessage='.$tr_arr[$i]['idforummessage'].'"/><br/>'.$tr_arr[$i]['file_name'].'<br/><br/>';
                                                } else {
                                                        echo '<div class="b-forum-message__file">'.getResourceByLang('field.WasAttachAFile').':&nbsp;';
                                                        echo '<a target="_blank" href="downloadforumfile.php?idfile='.$tr_arr[$i]['idfile'].'&idforummessage='.$tr_arr[$i]['idforummessage'].'">'.$tr_arr[$i]['file_name'].'</a>';
                                                        echo '</div>';
                                                }
                                        }
                                        echo $linkLine;
                                }
                                // link for next message in chain
                                if ($tr_arr[$i]['frmm_iright'] - $tr_arr[$i]['frmm_ileft'] > 1 || $tr_arr[$i]['frmm_depth'] > 1){
                                        if ($rownumb == $onpage){
                                                if ($i+1 <  $size && $tr_arr[$i + 1]['frmm_depth'] > 1) {
                                                        $m_root = $tr_arr[$i + 1]['idforummessage'];
                                                        //64016
                                                        $targetself =  urlReplaceParam(getCurPageURL(), "forumid", $m_root);
                                                        $targetself =  urlReplaceParam($targetself, "rep", null);                                                        
                                                        echo '<br/><br/><a target="_parent" class="self-event forum-selfopen-link" jshref="'.$targetself.'#'.$m_root.'" href="'.$target.'oguid='.$_REQUEST['oguid'].'&forumid='.$m_root.'#'.$m_root.'">'.getResourceByLang('link.msginchain.next').'</a><br/><br/>';
                                                }
                                        }
                                }
                                $rownumb++;
                                echo '</div>';
                                }
                        }
                        echo "</div>";
}
?>
                <section id="box-paginator" class="box-paginator-forum box-paginator-clearborder">
                        <?php
                                if (!(null === $total || $total == 0) && $total > 1) {
                                        echo_panel_page($total, $page, $url);
                                }
                                if ($hasnewmsg > -1) {
                                        echo '<a class="box-paginator-hasnewmessages-link '.($linkisactive ? '' : 'link-disabled').' " target="_parent" href="'.($linkisactive ? ''.$target.'oguid='.$_REQUEST['oguid'].'&forumid='.$tr_arr[$hasnewmsg]['idforummessage'].'#'.$tr_arr[$hasnewmsg]['idforummessage'] :'javascript:void(0)').'">'.getResourceByLang('field.forum.gotonewmessagelink').' ></a>&#8203;&#8203;';
                                }
                        ?>
                </section>

<?php if (!isset($_REQUEST['rep']) && !isset($_REQUEST['edit'])){ ?>
<br/>
<br/>
		<a name="new"></a>
		<section class="box-front front-menu clearfix">
<h2><?php getResource("field.forum.newMessage");?>:</h2>
<br/>
<form name="sendForm" id="sendForm" action="" method="post" enctype="multipart/form-data">
 <textarea id="forummessage" name="forummessage" cols="80" rows="4" style="width: 100%;"></textarea><br/><br/>
 <input type="hidden" name="forumadd" value="true">
 <?php getResource("field.AttachAFile");?> : <input id="file" type="file" name="file"><br/><br/>
 <span><button name="buttonSendForm" id="buttonSendForm" class="btn-top btn-top-text" title="<?php getResource("button.Send.hint");?>"><span><?php getResource("button.Send");?></span></button></span><!--STU-->
 <br/>
 <br/>
 </form>

</div>
<?php } else { ?>
<a class="btn-top btn-top-text" href="<?php echo urlReplaceParam(urlReplaceParam($url,'edit'),'rep').'#new';?>"><span> <?php getResource("field.forum.newMessage");?> </span></a>
<br /><br />
<?php } ?>
</td></tr></table>
<hr/>
</div>
</td></tr></table></div><br/>
</td></tr>
</table>
</div>
<?php
	if(!(is_readable(realpath(dirname(__FILE__)).'/templates/languagesbox.template'))) {
		die('Error - Could not open or read file '.realpath(dirname(__FILE__)).'/templates/languagesbox.template');
	} else {
		include_once(realpath(dirname(__FILE__)).'/templates/languagesbox.template');
	}
?>
<?php renderFooterTemplate(); ?>
</body>
<script src="js/libs/jquery.cookie.js"></script>
<script src="ckeditor/ckeditor.js"></script>
<script src="js/libs/jquery.scrollTo.js"></script>
<script>
var GForumOGUID = '<?php echo $oguid;?>';
var GForumLocation = '<?php echo urlReplaceParam(urlReplaceParam($url,'edit'),'rep');?>';
var GNewPageOpen = <?php if (isset($_REQUEST['rep']) || isset($_REQUEST['edit'])) {echo 'true'; } else { echo 'window.location.hash.substring(1) == "new"';} ?> ;
var GIsReply = <?php if (isset($_REQUEST['rep'])) {echo 'true'; } else { echo 'window.location.hash.substring(1) == "new"';} ?> ;
var GForumEmprtyMessageAlert = '<?php getResource("message.messageIsEmpty");?>';
var gShortLang = '<?php echo $lngshort;?>';
var gConfirmsavelesValue ="<?php getResource("confirm.savelesValue");?>";

$(document).ready(function () {
	$("#btn-tree-open").css({ top: ($("#box-tree-full").offset().top ) + 'px' });  
});
</script>
<script src="js/forum.js"></script>
<script src="js/close-confirm.js"></script>
</html>
<?php
db_disconnect($cn);
ob_end_flush(); ?>