<?php

/* ====================
  [BEGIN_COT_EXT]
 * Hooks=standalone
  [END_COT_EXT]
  ==================== */

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

$sq = cot_import('sq', 'G', 'TXT', 255);
$f = cot_import('f', 'G', 'ALP', 16);

$t = new XTemplate(cot_tplfile('banlist.page', 'plug'));

$wherenames = $db->query("SELECT banlist_email
	FROM $db_banlist WHERE banlist_active=1")->fetchAll(PDO::FETCH_COLUMN);

$cntbans = 0;

if(count($wherenames) > 0) {
  $wherenames = array_unique($wherenames);
  $where['user_namess'] = "(user_name='".implode("' OR user_name='", $wherenames)."')";

  if(!empty($sq))
  {
  	$y = $sq;
  }

  if(mb_strlen($y) > 1)
  {
  	$sq = $y;
  	$title[] = $L['Search']." '".htmlspecialchars($y)."'";
    $yarr = explode(' ', trim($y));
    $namelike = array();
    foreach($yarr as $ty) {
  	 $namelike[] = "user_name LIKE '%".$db->prep($ty)."%'";
     if ($db->fieldExists($db_users, "user_FNAME"))
     {
       $namelike[] = "user_FNAME LIKE '%".$db->prep($ty)."%'";
       if ($db->fieldExists($db_users, "user_LNAME"))
       {
         $namelike[] = "user_LNAME LIKE '%".$db->prep($ty)."%'";
       }
     }
    }
    if(count($namelike) > 0) $where['namelike'] = '(('.implode(') OR (', $namelike).'))';
  }

  $sqlorder = "user_id DESC";

  $sqlusers = $db->query("SELECT u.* FROM $db_users AS u
  	WHERE ".implode(" AND ", $where)." ORDER BY $sqlorder")->fetchAll();

  foreach ($sqlusers as $urr)
  {
    $ban = $db->query("SELECT *
    	FROM $db_banlist WHERE banlist_active=1 AND
    	banlist_email='".$db->prep($urr['user_name'])."' ORDER BY banlist_date DESC LIMIT 1")->fetch();

    if ($ban['banlist_iswarning'] == 0 && $sys['now'] > $ban['banlist_expire'] && $ban['banlist_expire'] > 0)
    {
    	$sql = $db->update($db_banlist, array('banlist_active' => 0), "banlist_id='".$ban['banlist_id']."'");
    } else {
      $cntbans++;
      $t->assign(cot_generate_usertags($urr, 'BANLIST_ROW_USER_'));
      $t->assign(array(
      	'BANLIST_ROW_ID' => $ban['banlist_id'],
        'BANLIST_ROW_DATE' => $ban['banlist_date'],
      	'BANLIST_ROW_EXPIRE' => ($ban['banlist_iswarning'] == 0 ? (($ban['banlist_expire'] > 0) ? cot_date('datetime_medium', $ban['banlist_expire']) : $L['banlist_neverexpire']) : ''),
      	'BANLIST_ROW_EXPIRE_STAMP' => ($ban['banlist_expire'] > 0) ? $ban['banlist_expire'] : '',
      	'BANLIST_ROW_REASON' => $ban['banlist_reason'],
        'BANLIST_ROW_ISWARNING' => ($ban['banlist_iswarning'] == 1 ? 1 : 0)
      ));
      $t->parse("MAIN.BANS_ROW");
    }
  }
}

$t->assign('BANS_COUNT', $cntbans);

?>