<?php
// this script gets last 5 threads, and last message in them, and puts relevant data into arrays you can use on your page
// Note. It takes the thread subject from the first message (intentional) but messagebody and author from tha threads last message
// I am not sure at the moment what the messagestatuses mean, all my messages are status 2 so I quess they a normal messages (not hidden, unapproved, deleted etc)
// I include needed phorum stuff elsewhere so I can use $PHORUM globals
// replace them with hardcoded values if you don't include common.php
// original script by various contributors. Dagon, sts, Portal of curiosity and others
// author: Panu A. no rights reserved but regognition and feedback welcome
// v. 1.1
$host 	= $PHORUM[DBCONFIG][server];
$dbuser	= $PHORUM[DBCONFIG][user];
$dbpass	= $PHORUM[DBCONFIG][password];
$dbname	= $PHORUM[DBCONFIG][name];
$table 	= $PHORUM[message_table];
//$date_format = $PHORUM[short_date]; //changes to short_date if prefered
$date_format = "j.n.Y G:i";
$number	=	5; // number of messages to show. 
$count 	= 10; //words per message to show
$preview_length = 50;

@$con=mysql_connect($host, $dbuser, $dbpass) or die ("cannot connect to MySQL");
$select_db = mysql_select_db($dbname, $con); 
$result = mysql_query("select * from $table where parent_id = 0 and status = 2 order by datestamp desc limit $number") or die("Failed Query of " . $result. mysql_error());

while($row = mysql_fetch_assoc($result)){ 

    $meta = unserialize($row['meta']);

	$newest =  max($meta['message_ids']); 

	// more than 1 message in thread?
	if($newest > $row['message_id']){
		
		$query = "select * from ".$table." where message_id = ".$newest;
		$result1 = mysql_query($query);
		while($row1 = mysql_fetch_assoc($result1)){
			
			if ($PHORUM[mods][bbcode] ==1){
				$msgbody = preg_replace( "|</*[a-z][^>]*>|i", "", $row1['body']); 
				$msg['body'] =  truncate_string(str_replace("#", "//", str_replace("/", "/<br/>", str_replace("//", "#", preg_replace( "|\[/*[a-z][^\]]*\]|i", "", $msgbody)))), $preview_length, 1);
		
			}
			
			$msg['datestamp'] = date("j.n.Y G:i", $row1['datestamp']);
			$msg['author'] = truncate_string($row1['author'], 12, 1);
			$msg['message_id'] = $newest;
			
		}
		
	} else {
		
			if ($PHORUM[mods][bbcode] ==1){
				
				$msgbody = preg_replace( "|</*[a-z][^>]*>|i", "", $row['body']); 
				$msg['body'] =  truncate_string(str_replace("#", "//", str_replace("/", "/<br/>", str_replace("//", "#", preg_replace( "|\[/*[a-z][^\]]*\]|i", "", $msgbody)))), $preview_length, 1);
		
			}
			
			$msg['datestamp'] = date($date_format, $row['datestamp']);
			$msg['author'] = truncate_string($row['author'], 12, 1);
			$msg['message_id'] = $row['message_id'];
		
	}

	$msg['subject'] = "<a href=\"".$PHORUM[SETTINGS][http_path]."/read.php?".$row['forum_id'].",".$row['thread'].",".$msg['message_id']."#".$msg['message_id']."\">" .truncate_string($row['subject'], 60, 1). "</a>";
	
	$p[] = $msg;
	
}

foreach ($p as $key => $data) {
	// in case you want to sort by date not messageid
	//$pdate[$key] = $data['datestamp'];
	$pkey[$key] = $data['message_id'];
}

array_multisort($pkey, SORT_DESC, $p);

// I use smarty templating engine to assign stuff to template, use whatever method you like.
$smarty->assign("p", $p);

function truncate_string($string, $length, $greater) {
    
    // truncated subjects end with:
    $end_string = "...";
    $i = $length - strlen($end_string);

    // where do we chop the string, earlier or later?
    if($greater == 1){

        while ($i < strlen($string)) {
            
            if ($string[$i] == " ") {

                $string = substr($string, 0, $i).$end_string;
                return $string;

            }
            $i++;
        }
    
    } else {
 
        while ($i > 0) {

            if ($string[$i] == " ") {
            
                $string = substr($string, 0, $i).$end_string;
                return $string;
            }
            $i--;
        }        
        
    }

    // if it's just one long word or first word longer than maxlength chop it in the middle anyway
    if($i == 0 || $i == strlen($string)) {

        $string = substr($string, 0, $length-strlen($end_string)).$end_string;
        
    }
    
    return $string;
}
?>
