<?php
//header("content-type:text/html;charset=utf-8");
/*
if ($_SERVER['HTTP_VIA'] != '') {
  die('不适用于使用代理服务访问。');
}
*/
@ require 'writable/set/set.php';

if ($web['addfunds'] == 0) {
  die('用户创收模式被关闭。');
}

@ require 'writable/set/set_sql.php';
@ require 'readonly/function/confirm_power.php';

define('POWER', confirm_power());

if ($session[0] == $web['manager']) {
  die('管理员不计创收。');
}

if ($_COOKIE['yourpcaddfunds'] == 1) {
  die('COOKIE:yourpcaddfunds已存在，说明今天这台机器已登陆，分已加。');
}

if (!isset($sql['db_err'])) {
  $db = db_conn();
}
if ($sql['db_err'] != '') {
  die($sql['db_err']);
}

@ $_GET['entrance'] = htmlspecialchars($_GET['entrance']);
@ $_GET['referrer'] = htmlspecialchars($_GET['referrer']);

if (empty($_GET['referrer'])) {
  die('没有来路！叫停。可通过下一步行为产生来路，防止恶意刷流量');
}

$date = gmdate("Y-m-d H:i:s", time() + floatval($web["time_pos"]) * 3600);
$ip = user_ip();
$o = 0;

$result = db_query($db, 'SELECT COUNT(id) AS total FROM '.$sql['pref'].$sql['data']['承载财务数据的表名'].' WHERE date LIKE "'.substr($date, 0, 10).'%" AND userip="'.$ip.'"');
$row = db_fetch($db, $result);
$n = abs($row['total']);
$result = NULL;
unset($row);

//description：0 提现 1 用户注册 2 成员登陆 3 推广URL来访 4 下线注册分成 5 下线登陆分成
if ($n == 0 && (is_numeric($web['loginadd']) && $web['loginadd'] > 0)) {
  $alliance_key = !empty($_GET['alliance_key']) ? $_GET['alliance_key'] : (!empty($_COOKIE['alliancekey']) ? $_COOKIE['alliancekey'] : false);
  if ($alliance_key && is_numeric($alliance_key)) {
    $result = db_query($db, 'SELECT username FROM '.$sql['pref'].$sql['data']['承载成员档案的表名'].' WHERE id="'.$alliance_key.'" AND (check_reg="0" OR check_reg="4") LIMIT 1');
    $row = db_fetch($db, $result);
    $username = $row['username'];
    $result = NULL;
    unset($row);
    if (!empty($username)) {
      if (db_exec($db, 'INSERT INTO `'.$sql['pref'].$sql['data']['承载财务数据的表名'].'` (`username`,`userip`,`description`,`date`,`money`,`other`) values ("'.$username.'","'.$ip.'","3","'.$date.'","'.$web['loginadd'].'","入口URL：'.$_GET['entrance'].'<br />来路：'.$_GET['referrer'].'")')) {
        $o = 1;
	  }
    } else {
      if (POWER > 0) {
        $result = db_query($db, 'SELECT COUNT(id) AS total FROM `'.$sql['pref'].$sql['data']['承载成员档案的表名'].'` WHERE username="'.$session[0].'" AND (check_reg="0" OR check_reg="4") LIMIT 1');
        $row = db_fetch($db, $result);
        if (abs($row['total']) == 0) {
          die('此用户名已删除或被置入黑名单！');
        }
        $result = NULL;
        unset($row);
        if (db_exec($db, 'INSERT INTO `'.$sql['pref'].$sql['data']['承载财务数据的表名'].'` (`username`,`userip`,`description`,`date`,`money`,`other`) values ("'.$session[0].'","'.$ip.'","2","'.$date.'","'.$web['loginadd'].'","入口URL：'.$_GET['entrance'].'<br />来路：'.$_GET['referrer'].'")')) {
          $o = 1;
		}
        if (!empty($session[3]) && ($session[3] != $session[0])) {
          //给上线加分
          if (is_numeric($web['loginadd2']) && $web['loginadd2'] > 0) {
            $result = db_query($db, 'SELECT username FROM '.$sql['pref'].$sql['data']['承载成员档案的表名'].' WHERE username="'.$session[3].'" AND (check_reg="0" OR check_reg="4") LIMIT 1');
            $row_f = db_fetch($db, $result);
            $username_f = $row_f['username'] ;
            $result = NULL;
            unset($row_f);
            if (!empty($username_f)) {
              db_exec($db, 'INSERT INTO `'.$sql['pref'].$sql['data']['承载财务数据的表名'].'` (`username`,`userip`,`description`,`money`,`date`,`other`) VALUES ("'.$session[3].'","'.$ip.'","5","'.$web['loginadd2'].'","'.$date.'","下线['.$session[0].']登陆<br />来路：'.$_GET['referrer'].'")');
            }
          }
        }
      }
    }
  } else {
    if (POWER > 0) {
      $result = db_query($db, 'SELECT COUNT(id) AS total FROM `'.$sql['pref'].$sql['data']['承载成员档案的表名'].'` WHERE username="'.$session[0].'" AND (check_reg="0" OR check_reg="4") LIMIT 1');
      $row = db_fetch($db, $result);
      if (abs($row['total']) == 0) {
        die('此用户名已删除或被置入黑名单或异常关停！');
      }
      $result = NULL;
      unset($row);
      if (db_exec($db, 'INSERT INTO `'.$sql['pref'].$sql['data']['承载财务数据的表名'].'` (`username`,`userip`,`description`,`date`,`money`,`other`) values ("'.$session[0].'","'.$ip.'","2","'.$date.'","'.$web['loginadd'].'","入口URL：'.$_GET['entrance'].'<br />来路：'.$_GET['referrer'].'")')) {
        $o = 1;
	  }
      if (!empty($session[3]) && $session[3] != $session[0]) {
        //给上线加分
        if (is_numeric($web['loginadd2']) && $web['loginadd2'] > 0) {
          $result = db_query($db, 'SELECT username FROM '.$sql['pref'].$sql['data']['承载成员档案的表名'].' WHERE username="'.$session[3].'" AND (check_reg="0" OR check_reg="4") LIMIT 1');
          $row_f = db_fetch($db, $result);
          $username_f = $row_f['username'] ;
          $result = NULL;
          unset($row_f);
          if (!empty($username_f)) {
            db_exec($db, 'INSERT INTO `'.$sql['pref'].$sql['data']['承载财务数据的表名'].'` (`username`,`userip`,`description`,`money`,`date`,`other`) VALUES ("'.$session[3].'","'.$ip.'","5","'.$web['loginadd2'].'","'.$date.'","下线['.$session[0].']登陆<br />来路：'.$_GET['referrer'].'")');
          }
        }
      }
    }
  }
  if ($o > 0) {
    setcookie('yourpcaddfunds', 1, time() + (86400 - gmdate('G', time() + floatval($web["time_pos"]) * 3600) * 3600 - gmdate('i', time() + floatval($web["time_pos"]) * 3600) * 60 - gmdate('s', time() + floatval($web["time_pos"]) * 3600) + floatval($web["time_pos"]) * 3600), '/');
  } else {
    echo 'err:'.$sql['db_err'];
  }

} else {
  setcookie('yourpcaddfunds', 1, time() + (86400 - gmdate('G', time() + floatval($web["time_pos"]) * 3600) * 3600 - gmdate('i', time() + floatval($web["time_pos"]) * 3600) * 60 - gmdate('s', time() + floatval($web["time_pos"]) * 3600) + floatval($web["time_pos"]) * 3600), '/');
}


db_close($db);


?>