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

$adList = array(
    403,
    404,
    435,
);

$friendList = array(
    170,
    240,
    387
);

$aid = $adList[rand(0, count($adList) - 1)];
$fid = $friendList[rand(0, count($friendList) - 1)];
$id = $aid;
$adUrl = url('ad', array('aid'=>$aid));
$friendUrl = url('ad', array('aid'=>$fid));

$credit = rand(10, 20);
$url = isset($_GET['back']) ? urldecode(S::gp('back')) : url('index');

if (CMOBILE)
{
    $url = $adUrl;
}
else if (CUNICOM || isUCWeb())
{
    $url = $friendUrl;
}

if (!$iorange_id)
{
    if (IS_MOBILE)
    {
        $url = $adUrl;
    }
    else
    {
        $id = $fid;
        $url = $friendUrl;
    }
}
else
{
    $check = $db->getValue('SELECT COUNT(*) FROM '.Redirect::table().' WHERE uid='.S::sqlEscape($iorange_id).' AND `date` = '.S::sqlEscape(CDATE));
    if (!$check)
    {
        iCredits::single($iorange_id, $username)->addMoney($credit);
        $db->insert(Redirect::table())->value(array('uid'=>$iorange_id, 'ua'=>S::getServer('HTTP_USER_AGENT'), 'aid'=> $id,'credit' => $credit, 'date' => CDATE, 'createdate' => CDATETIME))->q();
    }
    else
    {
        $url = isset($_GET['back']) ? urldecode(S::gp('back')) : url('index');
    }
}

iforward($url);