<?php
/* ====================
[BEGIN_COT_EXT]
Hooks=global
Order=1
[END_COT_EXT]
==================== */

defined('COT_CODE') or die('Wrong URL');

cot::$db->registerTable('banlist');
cot::$db->registerTable('banlist_msg');

$usr['ban'] = array(
  'ban403' => 0,
  'banned' => 0,
  'banby' => '',
  'expire' => 0,
  'reason' => '',
  'reason_foruser' => '',
  'show_support_btn' => 0,
  'block' => array(
    'ds' => 0,
    'market_buyer' => 0,
    'market_seller' => 0,
    'projects_buyer' => 0,
    'projects_seller' => 0,
  )
);

if(!in_array($usr['id'], $cfg['admin_user_ids'])) {
  $userip = explode('.', $usr['ip']);
  $ipmasks = "('".$userip[0].'.'.$userip[1].'.'.$userip[2].'.'.$userip[3]."','".$userip[0].'.'.$userip[1].'.'.$userip[2].".*','".$userip[0].'.'.$userip[1].".*.*','".$userip[0].".*.*.*')";

  $sql = $db->query("SELECT * FROM $db_banlist WHERE banlist_iswarning=0 AND banlist_active=1 AND banlist_ip IN ".$ipmasks." ORDER BY banlist_date DESC LIMIT 1");
  if ($sql->rowCount() > 0)
  {
  	$row = $sql->fetch();
  	$sql->closeCursor();

  	if ($sys['now'] > $row['banlist_expire'] && $row['banlist_expire'] > 0)
  	{
  		$sql = $db->update($db_banlist, array('banlist_active' => 0), "banlist_id='".$row['banlist_id']."'");
  	}
  	else
  	{
  		require_once cot_langfile('banlist', 'plug');

  		$banlist_email_mask = mb_strpos($row['banlist_email'], '.') ? $row['banlist_email'] : $row['banlist_email'].'.';
  		if ($usr['name'] && $row['banlist_email'] == $usr['name'])
  		{
  			$reason = $L['banlist_blocked_login'];
  		}
  		elseif ($row['banlist_email'])
  		{
  			$reason = $L['banlist_blocked_email'];
  		}
  		else
  		{
  			$reason = $L['banlist_blocked_ip'];
  		}

      $bantags = array(
        'datetime' => ($row['banlist_expire'] > 0 ? cot_date('d.m.Y H:i', $row['banlist_expire']) : 'пожизненно')
      );

      $usr['ban'] = array(
        'ban403' => 1,
        'banned' => 1,
        'banby' => $reason,
        'expire' => ($row['banlist_expire'] > 0 ? $row['banlist_expire'] : 0),
        'reason' => cot_rc($row['banlist_reason'], $bantags),
        'reason_foruser' => cot_rc($row['banlist_reason_foruser'], $bantags),
        'show_support_btn' => 0,
        'block' => array(
          'ds' => $row['banlist_block_ds'],
          'market_buyer' => $row['banlist_block_market_buyer'],
          'market_seller' => $row['banlist_block_market_seller'],
          'projects_buyer' => $row['banlist_block_projects_buyer'],
          'projects_seller' => $row['banlist_block_projects_seller'],
        )
      );

      if($usr['ban']['banned'] && $usr['ban']['block']['market_seller']) {

      }

  		$expiretime = ($row['banlist_expire'] > 0) ? cot_date('datetime_medium', $row['banlist_expire']) : $L['banlist_foreverbanned'];
  		$disp = cot_rc('banlist_banned', array($reason, $row['banlist_reason'], $expiretime));
  		cot_die_message(403, true, '', $disp);
  	}
  }
}

if($usr['id'] > 0) {

$user_email = $usr['profile']['user_email'];
if ($user_email) {
	$user_email_mask = mb_strstr($user_email, '@');
	$user_email_mask_multi = explode('.', $user_email_mask);
}
else
{
	$user_email = $user_email_mask = $user_email_mask_multi = '-';
}

$sql = $db->query("SELECT * FROM $db_banlist WHERE banlist_iswarning=0 AND banlist_active=1 AND (banlist_email='".$db->prep($user_email_mask).
	"' OR banlist_email='".$db->prep($user_email_mask_multi[0]).
	"' OR banlist_email='".$db->prep($user_email).
	($usr['name'] ? "' OR banlist_email='".$db->prep($usr['name']) : '').
	"') ORDER BY banlist_date DESC LIMIT 1");

if ($sql->rowCount() > 0)
{
	$row = $sql->fetch();
	$sql->closeCursor();
	if ($sys['now'] > $row['banlist_expire'] && $row['banlist_expire'] > 0)
	{
		$sql = $db->update($db_banlist, array('banlist_active' => 0), "banlist_id='".$row['banlist_id']."'");
	}
	else
	{
		require_once cot_langfile('banlist', 'plug');

		$banlist_email_mask = mb_strpos($row['banlist_email'], '.') ? $row['banlist_email'] : $row['banlist_email'].'.';
		if ($usr['name'] && $row['banlist_email'] == $usr['name'])
		{
			$reason = $L['banlist_blocked_login'];
		}
		elseif ($row['banlist_email'])
		{
			$reason = $L['banlist_blocked_email'];
		}
		else
		{
			$reason = $L['banlist_blocked_ip'];
		}

    $bantags = array(
      'datetime' => ($row['banlist_expire'] > 0 ? cot_date('d.m.Y H:i', $row['banlist_expire']) : 'пожизненно')
    );

    $usr['ban'] = array(
      'ban403' => 0,
      'banned' => 1,
      'banby' => $reason,
      'expire' => ($row['banlist_expire'] > 0 ? $row['banlist_expire'] : 0),
      'reason' => cot_rc($row['banlist_reason'], $bantags),
      'reason_foruser' => cot_rc($row['banlist_reason_foruser'], $bantags),
      'show_support_btn' => (($usr['id'] > 0 && $row['banlist_show_support_btn']) ? 1 : 0),
      'block' => array(
        'ds' => $row['banlist_block_ds'],
        'market_buyer' => $row['banlist_block_market_buyer'],
        'market_seller' => $row['banlist_block_market_seller'],
        'projects_buyer' => $row['banlist_block_projects_buyer'],
        'projects_seller' => $row['banlist_block_projects_seller'],
      )
    );

    if($usr['ban']['banned'] && $usr['ban']['block']['market_seller']) {

    }

		//$expiretime = ($row['banlist_expire'] > 0) ? cot_date('datetime_medium', $row['banlist_expire']) : $L['banlist_foreverbanned'];
		//$disp = cot_rc('banlist_banned', array($reason, $row['banlist_reason'], $expiretime));
		//cot_die_message(403, true, '', $disp);
	}
}
}