whatsnew.php

来自「速度很快的PHP论坛源程序」· PHP 代码 · 共 132 行

PHP
132
字号
<?php

/*
	[DISCUZ!] whatsnew.php - new threads list for html pages
	This is NOT a freeware, use is subject to license terms

	Version: 3.5.0
	Web: http://www.comsenz.com
	Copyright: 2001-2005 Comsenz Technology Ltd.
	Last Modified: 2003/12/12 06:21
*/

// [EN] Embed new threads in html pages for Discuz! For further information, please read utilities/whatsnew.txt
// [CH] Html 页面论坛新贴调用, 详情请参考 utilities/whatsnew.txt

// [EN] Warning!! This program may increase your server load obviously, Using is at your own risk. 
// [CH] 警告!! 本程序可能明显加重服务器负担, 使用本程序您需要自担风险.

error_reporting(E_ERROR | E_WARNING | E_PARSE);


$enabled = 0;		// Whether to enable what's new feature, 1=yes, 0=no
			// 是否启用 what's new 功能, 1=是, 0=否

$reflimit = 0;		// Only permit same site pages to include this script, 1=yes, 0=no
			// 只允许本域名下的页面调用本程序, 1=是, 0=否

$cachelife = 1800;	// Cache life span(sec.)
			// 缓存更新时间(秒),通常不需要更改

$num = 10;		// Default amount of new threads
			// 默认显示新贴数量

$length = 50;		// Max. length of title(bytes)
			// 默认标题最大字节数

$digest = 0;		// Digest Level
			// 默认显示精华帖级别. 如设置为2, 则显示精华级别为2及以上的精华帖子, 0 为不区分

$pre = '<li>';		// Prefix of title
			// 标题显示前缀


$enabled && (!$reflimit || !$HTTP_SERVER_VARS['HTTP_REFERER'] || preg_replace("/https?:\/\/([^\:\/]+).*/i", "\\1", $HTTP_SERVER_VARS['HTTP_REFERER']) == preg_replace("/([^\:]+).*/", "\\1", $HTTP_SERVER_VARS['HTTP_HOST'])) ?
	define('IN_DISCUZ', TRUE) : exit("document.write(\"Access denied. The webmaster did not enable this feature or referer restriction is taking effect.\");");

$timestamp = time();
$PHP_SELF = $HTTP_SERVER_VARS['PHP_SELF'] ? $HTTP_SERVER_VARS['PHP_SELF'] : $HTTP_SERVER_VARS['SCRIPT_NAME'];
$boardurl = 'http://'.$HTTP_SERVER_VARS['HTTP_HOST'].substr($PHP_SELF, 0, strrpos($PHP_SELF, '/'));

$fidin = addslashes(trim($HTTP_GET_VARS['fidin']));
$fidout = addslashes(trim($HTTP_GET_VARS['fidout']));

$num = $HTTP_GET_VARS['num'] ? addslashes($HTTP_GET_VARS['num']) : $num;
$length = isset($HTTP_GET_VARS['length']) ? intval($HTTP_GET_VARS['length']) : $length;
$digest = $HTTP_GET_VARS['digest'] ? addslashes($HTTP_GET_VARS['digest']) : $digest;

if(substr($timestamp, -5) == '00000' && @$dir = dir('./forumdata/')) {
	while($entry = $dir->read()) {
		if(preg_match("/whatsnew_[0-9a-f]{32}\.php/", $entry)) {
			@unlink('./forumdata/'.$entry);
		}
	}
	$dir->close();
}

$cachefile = './forumdata/whatsnew_'.md5("$fidin\t$fidout\t$num\t$length\t$digest").'.php';

if((@!include($cachefile)) || $expiration < $timestamp) {

	require_once './config.php';
	require_once './include/db_'.$database.'.class.php';

	$db = new dbstuff;
	$db->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect);
	$db->select_db($dbname);
	unset($dbhost, $dbuser, $dbpw, $dbname, $pconnect);

	if(@!$fp = fopen($cachefile, 'wb')) {
		exit("document.write(\"Unable to write to whatsnew's cache file!<br>Please chmod ./forumdata to 777, delete all<br>\\\"whatsnew_xxx.php\\\" files and try again.\");");
	}

	fwrite($fp, "<?php if(!defined('IN_DISCUZ')) exit('Access Denied'); \$expiration=".($timestamp + $cachelife)."; ?>\n");

	$pre = addslashes($pre);
	$fidin = $fidin ? ' AND fid IN (\''.str_replace('_', '\',\'', $fidin).'\')' : '';
	$fidout = $fidout ? ' AND fid NOT IN (\''.str_replace('_', '\',\'', $fidout).'\')' : '';

	$forumfilter = '';
	$forumfilter .= $fidin || $fidout ? $fidin.$fidout : '';
	$forumfilter .= $digest ? ' AND digest>=\''.$digest.'\'' : '';

	$query = $db->query("SELECT subject, tid, closed, lastpost
		FROM {$tablepre}threads WHERE displayorder>='0' $forumfilter ORDER BY lastpost DESC LIMIT $num");

	while($threads = $db->fetch_array($query)) {
		if($thread['closed'] > 2) {
			$thread['tid'] = $thread['closed'];
		}
		$threads['subject'] = cutstr($threads['subject'], $length);
		$row = "document.write(\"<a href=$boardurl/viewthread.php?tid=$threads[tid] target=_blank>$pre$threads[subject]</a><br>\");\n";
		fwrite($fp, $row);

		if(!isset($expiration)) {
			echo $row;
		}
	}

	fclose($fp);

}

function cutstr($string, $length) {

	if(strlen($string) > $length) {
		for($i = 0; $i < $length - 3; $i++) {
			if(ord($string[$i]) > 127) {
				$wordscut .= $string[$i].$string[$i + 1];
				$i++;
			} else {
				$wordscut .= $string[$i];
			}
		}
		return $wordscut.' ...';
	}
	return $string;

}

?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?