<?xml version="1.0" encoding="UTF-8"?>
<product productid="sticklimit" active="1">
    <title>主题限时置顶</title> 
    <description>主题限时置顶插件 (作者：silver)</description> 
    <version>1.0</version>
    <url><![CDATA[http://www.zdyi.com]]></url>
    <versioncheckurl><![CDATA[http://www.zdyi.com]]></versioncheckurl>
    <dependencies>
        <dependency dependencytype="vbulletin" minversion="3.6.8" maxversion="" /> 
    </dependencies>

    <codes>
        <code version="1.0">
            <installcode>
            <![CDATA[
//修改 thread 表.新增 sticklimit 字段
$vbulletin->db->hide_errors();
$vbulletin->db->query("ALTER TABLE " . TABLE_PREFIX . "thread ADD sticklimit INT NOT NULL default '0'");
$vbulletin->db->show_errors();

// 在 includes/cron/ 目录中创建 unsticklimit.php 文件
$handle = @fopen('includes/cron/unsticklimit.php', 'w');
if(!$handle) {
	exit('写入文件时发生错误，请确认 includes/cron/ 目录有可写权限！');
} 
$phpfile = "
<?php
/**********************************************************************
* sticklimit by silver
* http://www.zdyi.com
* 2008-11-5
***********************************************************************/
error_reporting(E_ALL & ~E_NOTICE);
if (!is_object(\$vbulletin->db)) {
	log_cron_action('取消限时置顶主题 [失败] ->论坛数据库连接失败', \$nextitem);
	exit;
}
\$vbulletin->db->query_write('UPDATE ' . TABLE_PREFIX . 'thread SET sticky= 0, sticklimit = 0 WHERE sticky = 1 and sticklimit > 0 and sticklimit < ' . time());
log_cron_action('取消限时置顶主题 [完成]', \$nextitem);
?>";
fwrite($handle, $phpfile);
fclose($handle);
            ]]>
            </installcode>

            <uninstallcode>
<![CDATA[
//删除字段
$vbulletin->db->hide_errors();
$vbulletin->db->query("ALTER TABLE " . TABLE_PREFIX . "thread DROP sticklimit");
//删除模板
$vbulletin->db->query("DELETE FROM " . TABLE_PREFIX . "template where title = 'threadadmin_sticklimitthread'");
$vbulletin->db->show_errors();
//删除计划任务文件
unlink('includes/cron/unsticklimit.php');
]]> 
            </uninstallcode>
        </code>
    </codes>

    <templates>
        <template name="threadadmin_sticklimitthread" templatetype="template" username="silver" version="1.0"><![CDATA[<form action="postings.php?do=dosticklimit&amp;t=$threadid" method="post" name="vbform">
<input type="hidden" name="s" value="$session[sessionhash]" />
<input type="hidden" name="securitytoken" value="$bbuserinfo[securitytoken]" />
<input type="hidden" name="t" value="$threadid" />
<input type="hidden" name="do" value="dosticklimit" />

<table class="tborder" cellpadding="$stylevar[cellpadding]" cellspacing="$stylevar[cellspacing]" border="0" width="100%" align="center">
<tr>
	<td class="tcat">$vbphrase[sticklimit_threads]</td>
</tr>
<tr>
	<td class="thead">$vbphrase[title]: <span style="font-weight:normal">$threadinfo[prefix_plain_html] $threadinfo[title]</span></td>
</tr>

<tr>
	<td class="panelsurround" align="center">
	<div class="panel">
		<div style="width:$stylevar[formwidth]" align="$stylevar[left]">


		<fieldset class="fieldset">
			<legend>$vbphrase[sticklimit_threads]</legend>
			<table cellpadding="0" cellspacing="$stylevar[formspacer]" border="0">
			<tr>
				<td>
	                $vbphrase[sticklimit_date]:
                    <input type="text" class="bginput" name="sticklimit_days" value="0" size="6" maxlength="3"> $vbphrase[days] ($vbphrase[sticklimit_explain])
				</td>
			</tr>
			</table>
		</fieldset>

		</div>
	</div>

	<div style="margin-top:$stylevar[cellpadding]px">
		<input type="submit" class="button" value="$vbphrase[sticklimit_threads]" accesskey="s" />
		<input type="reset" class="button" value="$vbphrase[reset_fields]" accesskey="r" />
	</div>
	</td>
</tr>
</table>
</form>]]>
        </template>
    </templates>
    
    <plugins>
        <plugin active="1" executionorder="5">
            <title>限时置顶主题新增动作</title>
            <hookname>threadmanage_action_switch</hookname> 
            <phpcode>
            <![CDATA[
            if($_REQUEST['do'] == 'sticklimit' || $_REQUEST['do'] == 'dosticklimit')
            {
                $handled_do = true;
            }
            ]]>
            </phpcode>
        </plugin>

        <plugin active="1" executionorder="5">
            <title>限时置顶主题模板显示</title>
            <hookname>threadmanage_start</hookname> 
            <phpcode>
            <![CDATA[
// add by silver 2008-11-4
if($_POST['do'] == 'sticklimit')
{
    $templatename = 'threadadmin_sticklimitthread';

	if(($threadinfo['isdeleted'] AND !can_moderate($threadinfo['forumid'], 'candeleteposts')) OR (!$threadinfo['visible'] AND !can_moderate($threadinfo['forumid'], 'canmoderateposts')))
	{
		if(can_moderate($threadinfo['forumid']))
		{
			print_no_permission();
		}
		else
		{
			eval(standard_error(fetch_error('invalidid', $idname, $vbulletin->options['contactuslink'])));
		}
	}

	if(!can_moderate($threadinfo['forumid'], 'canmanagethreads'))
	{
		print_no_permission();
	}
}
]]>
            </phpcode>
        </plugin>

        <plugin active="1" executionorder="6">
            <title>限时置顶主题执行</title>
            <hookname>threadmanage_start</hookname> 
            <phpcode>
            <![CDATA[
// add by silver 2008-11-4
if ($_POST['do'] == 'dosticklimit')
{
	$vbulletin->input->clean_array_gpc('p', array(
		'sticklimit_days'		=> TYPE_UINT,
	));

	if (($threadinfo['isdeleted'] AND !can_moderate($threadinfo['forumid'], 'candeleteposts')) OR (!$threadinfo['visible'] AND !can_moderate($threadinfo['forumid'], 'canmoderateposts')))
	{
		if (can_moderate($threadinfo['forumid']))
		{
			print_no_permission();
		}
		else
		{
			eval(standard_error(fetch_error('invalidid', $idname, $vbulletin->options['contactuslink'])));
		}
	}

	if (!can_moderate($threadinfo['forumid'], 'canmanagethreads'))
	{
		print_no_permission();
	}

	// check if there is a forum password and if so, ensure the user has it set
	verify_forum_password($foruminfo['forumid'], $foruminfo['password']);

    $stickylinfo = array(
		'sticklimit_days'          => $vbulletin->GPC['sticklimit_days'],
	);

	// handles mod log
	$threadman =& datamanager_init('Thread', $vbulletin, ERRTYPE_ARRAY, 'threadpost');
	$threadman->set_existing($threadinfo);
	$threadman->set('sticky', ($threadman->fetch_field('sticky') == 1 ? 0 : 1));
	$threadman->save();

    if($stickylinfo['sticklimit_days'] > 0)
    {
        $limitdate = time() + ($stickylinfo['sticklimit_days'] * 24 * 3600);
        $db->query_write("UPDATE " . TABLE_PREFIX . "thread SET sticklimit = " . $limitdate . " where threadid = " . $threadinfo['threadid']);
   }

    $action = $vbphrase['sticklimit_threads'];
	$vbulletin->url = 'showthread.php?' . $vbulletin->session->vars['sessionurl'] . "t=$threadid";
	eval(print_standard_redirect('redirect_sticky', true, true));
}
]]>
            </phpcode>
        </plugin>

        <plugin active="1" executionorder="5">
            <title>限时置顶主题 - 取消置顶时将时间清空</title>
            <hookname>threadmanage_stickunstick</hookname> 
            <phpcode>
            <![CDATA[
//silver 修改归零到期时间(为0即为永不过期)
$db->query_write("UPDATE " . TABLE_PREFIX . "thread SET sticklimit = 0 where threadid = " . $threadinfo['threadid']);
            ]]>
            </phpcode>
        </plugin>

    </plugins>

    <phrases>
        <phrasetype name="主题管理 (Thread Management)" fieldname="threadmanage">
            <phrase name="sticklimit_threads" username="silver" version="1.0"><![CDATA[限时置顶主题]]></phrase>
        </phrasetype>
        <phrasetype name="主题管理 (Thread Management)" fieldname="threadmanage">
            <phrase name="unsticklimit_threads" username="silver" version="1.0"><![CDATA[取消限时置顶]]></phrase>
        </phrasetype>
        <phrasetype name="主题管理 (Thread Management)" fieldname="threadmanage">
            <phrase name="sticklimit_date" username="silver" version="1.0"><![CDATA[置顶有效期]]></phrase>
        </phrasetype>
        <phrasetype name="主题管理 (Thread Management)" fieldname="threadmanage">
            <phrase name="sticklimit_explain" username="silver" version="1.0"><![CDATA[为0表示主题置顶永不过期]]></phrase>
        </phrasetype>

        <phrasetype name="快速管理 (Inline Moderation)" fieldname="inlinemod">
            <phrase name="sticklimit_threads" username="silver" version="1.0"><![CDATA[限时置顶主题]]></phrase>
        </phrasetype>
        <phrasetype name="快速管理 (Inline Moderation)" fieldname="inlinemod">
            <phrase name="unsticklimit_threads" username="silver" version="1.0"><![CDATA[取消限时置顶]]></phrase>
        </phrasetype>
    </phrases>

    <options></options>
    <helptopics></helptopics>
    <cronentries></cronentries>
    <faqentries></faqentries>
</product>

