<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
require_once dirname(dirname(__FILE__)) . '/init.php';
ini_set('Memory', '750M');

$mTable = Members::table();
$mfTable = Memberfields::table();
$bTable = Book::table();
$bfTable = BookForum::table();
$bmTable = BookManagers::table();
$beTable = BookMembers::table();

$db->query('UPDATE `'.$bTable."` SET `day` = 0, d_red = 0, d_black = 0, u_d_click = 0, u_v_d_click = 0");

$db->query('UPDATE `'.$bfTable.'` SET `todayposts` = 0');

$db->query('UPDATE `'.$bmTable.'` SET `digest` = 10 WHERE status = 1');

$db->query("UPDATE `".Ad::table()."` SET `day` = 0, day_valid = 0");

$db->query('UPDATE '.Forums::table().' SET todayposts = 0,todaythreads =0');

$db->query('UPDATE '.BbsInfo::table().' SET yposts = tposts, tposts = 0 WHERE id = 1');

$db->query('UPDATE '.SoftContents::table().' SET day_download = 0');

$options = 'a:3:{i:5;a:2:{s:4:"time";i:30;s:3:"num";i:800;}i:10;a:2:{s:4:"time";i:60;s:3:"num";i:480;}i:20;a:2:{s:4:"time";i:300;s:3:"num";i:100;}}';
$db->query('UPDATE `'.Property::table()."` SET `option` = '$options' WHERE propertyid = 3");

if (date("D") == 'Mon')
{
    $db->query('UPDATE '.$mfTable.' SET hp = 2 WHERE uid in (SELECT uid FROM '.$mTable.' WHERE verify = 1 AND groupid not in (4,5))');
    
	$db->query('UPDATE `'.$bTable.'` SET `week` = 0, `w_red` = 0, `w_black` = 0, u_w_click = 0, u_v_w_click = 0');
    
    $timestr = strtotime('-1 week');
    $db->query('DELETE FROM ig_feed WHERE createdate <= '.$timestr);

    $db->query('UPDATE '.SoftContents::table().' SET week_download = 0');
}

$db->query("UPDATE {$mTable} SET ads = ".setting('adsnum'));

$db->query('UPDATE '.$mfTable.' SET todaylogin = 2');

# 撤销两周内不上线的书管职位
//TODO 暂时屏蔽，等在线功能重新上线
/**
$time = time();
$expire = 14 * 24 * 3600;
$sql = "SELECT BM.`bookid`,BM.`uid`
        FROM `{$bmTable}` AS BM
        INNER JOIN `{$mTable}` AS CM ON BM.`uid` = CM.`uid`
        INNER JOIN `{$bTable}` AS B ON B.`bookid` = BM.`bookid`
        WHERE $time - CM.lastvisit > $expire
        AND BM.status = 1";
$query = $db->query($sql);
while ($row = $db->getArr($query))
{
    $uid = $row['uid'];
    $bookid = $row['bookid'];
    $bookname = $row['bookname'];

    $remarks = '长期不登录，取消书管权限';
    $db->update($bmTable)->value(array('status'=>2,'updatedate'=>date("Y-m-d H:i:s"),'remarks'=>$remarks))->where(array('bookid'=>$bookid,'uid'=>$uid))->q();
    updateBookManage($bookid);

    $subject = '[系统消息]取消书会管理员权限';
    $content = '由于您超过两周未登录，故取消您《' . $bookname . '》书会书管权限，如有疑问，请发短信联系admin';
    $did = MessageData::single()->create($subject,$content);
    Message::single()->send($did, 1, 'admin', $uid);
}
/**/