<?php
/* ====================
[BEGIN_COT_EXT]
Hooks=moderation.page.mfind
Order=1
[END_COT_EXT]
==================== */

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

if($m == 'banlist' && in_array('banlist', $moderauth)) {
  $mfind = true;

  $t = new XTemplate(cot_tplfile('moderation.banlist', 'module'));
  require_once cot_langfile('banlist', 'plug');

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

  $maxperpage = ($cfg['maxrowsperpage'] && is_numeric($cfg['maxrowsperpage']) && $cfg['maxrowsperpage'] > 0) ? $cfg['maxrowsperpage'] : 15;
  list($pg, $d, $durl) = cot_import_pagenav('d', $maxperpage);

  /* === Hook === */
  foreach (cot_getextplugins('banlist.admin.first') as $pl)
  {
  	include $pl;
  }
  /* ===== */

  if ($a == 'update')
  {
  	$id = cot_import('id', 'G', 'INT');
  	$rbanlistip = cot_import('rbanlistip', 'P', 'TXT');
  	$rbanlistemail = $db->prep(cot_import('rbanlistemail', 'P', 'TXT'));
  	$rbanlistreason = $db->prep(cot_import('rbanlistreason', 'P', 'TXT'));
    $nbanlistreason_foruser = $db->prep(cot_import('nbanlistreason_foruser', 'P', 'HTM'));

  	$sql = (!empty($rbanlistip) || !empty($rbanlistemail))
  		? $db->update($db_banlist, array(
  			'banlist_ip' => $rbanlistip,
  			'banlist_email' => $rbanlistemail,
  			'banlist_reason' => $rbanlistreason,

        'banlist_iswarning' => (cot_import('niswarning', 'P', 'BOL') ? 1 : 0),

        'banlist_reason_foruser' => $nbanlistreason_foruser,
        'banlist_show_support_btn' => (cot_import('nbanshow_support_btn', 'P', 'BOL') ? 1 : 0),

        'banlist_block_ds' => (cot_import('nblock_ds', 'P', 'BOL') ? 1 : 0),
        'banlist_block_market_buyer' => (cot_import('nblock_market_buyer', 'P', 'BOL') ? 1 : 0),
        'banlist_block_market_seller' => (cot_import('nblock_market_seller', 'P', 'BOL') ? 1 : 0),
        'banlist_block_projects_buyer' => (cot_import('nblock_projects_buyer', 'P', 'BOL') ? 1 : 0),
        'banlist_block_projects_seller' => (cot_import('nblock_projects_seller', 'P', 'BOL') ? 1 : 0),
  			), "banlist_id=$id")
  		: '';

    cot_message('Запись о блокировке успешно обновлена');
    $sqlusrs = $db->query("SELECT * FROM $db_users WHERE user_name='".$rbanlistemail."' OR user_email='".$rbanlistemail."'")->fetchAll();
    foreach($sqlusrs as $urr) {
      /* === Hook === */
      foreach (cot_getextplugins('banlist.admin.update.urrlist') as $pl)
      {
      	include $pl;
      }
      /* ===== */
    }
  }
  elseif ($a == 'add')
  {
  	$nbanlistip = cot_import('nbanlistip', 'P', 'TXT');
  	$nbanlistemail = $db->prep(cot_import('nbanlistemail', 'P', 'TXT'));
  	$nbanlistreason = $db->prep(cot_import('nbanlistreason', 'P', 'TXT'));
    $nbanlistreason_foruser = $db->prep(cot_import('nbanlistreason_foruser', 'P', 'TXT'));
  	$nexpire = cot_import('nexpire', 'P', 'INT');

  	$nbanlistip_cnt = explode('.', $nbanlistip);
  	$nbanlistip = (count($nbanlistip_cnt)==4) ? $nbanlistip : '';

  	$sql = (!empty($nbanlistip) || !empty($nbanlistemail))
  		? $db->insert($db_banlist, array(
  			'banlist_ip' => $nbanlistip,
        'banlist_date' => $sys['now'],
  			'banlist_email' => $nbanlistemail,
  			'banlist_reason' => $nbanlistreason,
  			'banlist_howlong' => (int) $nexpire,
        'banlist_iswarning' => (cot_import('niswarning', 'P', 'BOL') ? 1 : 0),
        'banlist_expire' => (int) ($nexpire > 0 ? ($nexpire + $sys['now']) : 0),
        'banlist_active' => 1,

        'banlist_reason_foruser' => $nbanlistreason_foruser,
        'banlist_show_support_btn' => (cot_import('nbanshow_support_btn', 'P', 'BOL') ? 1 : 0),

        'banlist_block_ds' => (cot_import('nblock_ds', 'P', 'BOL') ? 1 : 0),
        'banlist_block_market_buyer' => (cot_import('nblock_market_buyer', 'P', 'BOL') ? 1 : 0),
        'banlist_block_market_seller' => (cot_import('nblock_market_seller', 'P', 'BOL') ? 1 : 0),
        'banlist_block_projects_buyer' => (cot_import('nblock_projects_buyer', 'P', 'BOL') ? 1 : 0),
        'banlist_block_projects_seller' => (cot_import('nblock_projects_seller', 'P', 'BOL') ? 1 : 0),
  			))
  		: '';

  	if($sql) {
      cot_message('Запись о блокировке успешно добавлена');
      $sqlusrs = $db->query("SELECT * FROM $db_users WHERE user_name='".$rbanlistemail."' OR user_email='".$rbanlistemail."'")->fetchAll();
      foreach($sqlusrs as $urr) {
        /* === Hook === */
        foreach (cot_getextplugins('banlist.admin.add.urrlist') as $pl)
        {
        	include $pl;
        }
        /* ===== */
      }
    } else {
      cot_message('Error');
    }
  }
  elseif ($a == 'delete')
  {
  	cot_check_xg();
  	$id = cot_import('id', 'G', 'INT');

  	$db->delete($db_banlist, "banlist_id=$id") ? cot_message('Запись о блокировке успешно удалена') : cot_message('Error');
  }
  elseif ($a == 'msgdelete')
  {
  	cot_check_xg();
  	$id = cot_import('id', 'G', 'INT');

  	$db->delete($db_banlist_msg, "banmsg_id=$id") ? cot_message('Жалоба успешно добавлена') : cot_message('Error');
  }

  $totalitems = $db->countRows($db_banlist);

  $pagenav = cot_pagenav('moderation', 'm=banlist', $d, $totalitems, $maxperpage, 'd', '', $cfg['jquery'] && $cfg['turnajax']);

  $sql = $db->query("SELECT * FROM $db_banlist ORDER by banlist_expire DESC, banlist_ip LIMIT $d, ".$maxperpage);

  $ii = 0;

  /* === Hook - Part1 : Set === */
  $extp = cot_getextplugins('banlist.admin.loop');
  /* ===== */

  foreach ($sql->fetchAll() as $row)
  {
  	$t->assign(array(
  		'ADMIN_BANLIST_ROW_ID' => $row['banlist_id'],
  		'ADMIN_BANLIST_ROW_URL' => cot_url('moderation', 'm=banlist&a=update&id='.$row['banlist_id'].'&d='.$durl),
  		'ADMIN_BANLIST_ROW_DELURL' => cot_url('moderation', 'm=banlist&a=delete&id='.$row['banlist_id'].'&'.cot_xg()),
  		'ADMIN_BANLIST_ROW_EXPIRE' => ($row['banlist_iswarning'] == 1 ? $L['banlist_iswarning'] : (($row['banlist_expire'] > 0) ? cot_date('datetime_medium', $row['banlist_expire']) : $L['banlist_neverexpire'])),
  		'ADMIN_BANLIST_ROW_EXPIRE_STAMP' => ($row['banlist_expire'] > 0) ? $row['banlist_expire'] : '',
  		'ADMIN_BANLIST_ROW_IP' => cot_inputbox('text', 'rbanlistip', $row['banlist_ip'], 'size="18" maxlength="16"'),
  		'ADMIN_BANLIST_ROW_EMAIL' => cot_inputbox('text', 'rbanlistemail', $row['banlist_email'], 'size="10" maxlength="64"'),
  		'ADMIN_BANLIST_ROW_REASON' => cot_inputbox('text', 'rbanlistreason', $row['banlist_reason'], 'size="22" maxlength="64"'),
      'ADMIN_BANLIST_ROW_ISWARNING' => cot_checkbox($row['banlist_iswarning'], 'niswarning', ''),

      'ADMIN_BANLIST_ROW_BLOCK_DS' => cot_checkbox($row['banlist_block_ds'], 'nblock_ds', ''),
      'ADMIN_BANLIST_ROW_BLOCK_MARKET_BUYER' => cot_checkbox($row['banlist_block_market_buyer'], 'nblock_market_buyer', ''),
      'ADMIN_BANLIST_ROW_BLOCK_MARKET_SELLER' => cot_checkbox($row['banlist_block_market_seller'], 'nblock_market_seller', ''),
      'ADMIN_BANLIST_ROW_BLOCK_PROJECTS_BUYER' => cot_checkbox($row['banlist_block_projects_buyer'], 'nblock_projects_buyer', ''),
      'ADMIN_BANLIST_ROW_BLOCK_PROJECTS_SELLER' => cot_checkbox($row['banlist_block_projects_seller'], 'nblock_projects_seller', ''),
      'ADMIN_BANLIST_ROW_REASON_FROUSER' => cot_inputbox('text', 'nbanlistreason_foruser', $row['banlist_reason_foruser'], ''),
      'ADMIN_BANLIST_ROW_SHOW_SUPPORT_BTN' => cot_checkbox($row['banlist_show_support_btn'], 'nbanshow_support_btn', ''),

  		'ADMIN_BANLIST_ROW_ODDEVEN' => cot_build_oddeven($ii)
  	));

  	/* === Hook - Part2 : Include === */
  	foreach ($extp as $pl)
  	{
  		include $pl;
  	}
  	/* ===== */

  	$t->parse('MAIN.ADMIN_BANLIST_ROW');
  	$ii++;
  }

  $sql = $db->query("SELECT * FROM $db_banlist_msg ORDER by banmsg_date DESC");

  $ii = 0;

  foreach ($sql->fetchAll() as $row)
  {
    $t->assign(cot_generate_usertags($row['banmsg_userid'], 'ADMIN_BANLIST_ROW_USER_'));
  	$t->assign(array(
  		'ADMIN_BANLIST_ROW_ID' => $row['banmsg_id'],
  		'ADMIN_BANLIST_ROW_DELURL' => cot_url('moderation', 'm=banlist&a=msgdelete&id='.$row['banmsg_id'].'&'.cot_xg()),
      'ADMIN_BANLIST_ROW_DATE' => cot_date('d.m.Y H:i', $row['banmsg_date']),
  		'ADMIN_BANLIST_ROW_REASON' => $row['banmsg_reason'],
  		'ADMIN_BANLIST_ROW_ODDEVEN' => cot_build_oddeven($ii)
  	));

  	$t->parse('MAIN.ADMIN_BANLIST_MSG_ROW');
  	$ii++;
  }

  $time_array = array('0', '3600', '7200', '14400', '28800', '57600', '86400',
  		'172800', '345600', '604800', '1209600', '1814400', '2592000', '5184000',
      '7776000', '15552000');
  $time_values = array(
  	$L['banlist_neverexpire'],
  	cot_declension(1,$Ls['Hours']),
  	cot_declension(2,$Ls['Hours']),
  	cot_declension(4,$Ls['Hours']),
  	cot_declension(8,$Ls['Hours']),
  	cot_declension(16,$Ls['Hours']),
  	cot_declension(1,$Ls['Days']),
  	cot_declension(2,$Ls['Days']),
  	cot_declension(4,$Ls['Days']),
  	cot_declension(1,$Ls['Weeks']),
  	cot_declension(2,$Ls['Weeks']),
  	cot_declension(3,$Ls['Weeks']),
  	cot_declension(1,$Ls['Months']),
    cot_declension(2,$Ls['Months']),
    cot_declension(3,$Ls['Months']),
    cot_declension(6,$Ls['Months'])
  );

  $t->assign(array(
  	'ADMIN_BANLIST_PAGINATION_PREV' => $pagenav['prev'],
  	'ADMIN_BANLIST_PAGNAV' => $pagenav['main'],
  	'ADMIN_BANLIST_PAGINATION_NEXT' => $pagenav['next'],
  	'ADMIN_BANLIST_TOTALITEMS' => $totalitems,
  	'ADMIN_BANLIST_COUNTER_ROW' => $ii,
  	'ADMIN_BANLIST_URLFORMADD' => cot_url('moderation', 'm=banlist&a=add'),
  	'ADMIN_BANLIST_EXPIRE' => cot_selectbox('0', 'nexpire', $time_array, $time_values, false),
  	'ADMIN_BANLIST_IP' => cot_inputbox('text', 'nbanlistip', '', 'size="18" maxlength="16"'),
  	'ADMIN_BANLIST_EMAIL' => cot_inputbox('text', 'nbanlistemail', '', 'size="24" maxlength="64"'),
  	'ADMIN_BANLIST_REASON' => cot_inputbox('text', 'nbanlistreason', '', 'size="48"'),
    'ADMIN_BANLIST_ISWARNING' => cot_checkbox(0, 'niswarning', ''),

    'ADMIN_BANLIST_BLOCK_DS' => cot_checkbox(0, 'nblock_ds', ''),
    'ADMIN_BANLIST_BLOCK_MARKET_BUYER' => cot_checkbox(0, 'nblock_market_buyer', ''),
    'ADMIN_BANLIST_BLOCK_MARKET_SELLER' => cot_checkbox(0, 'nblock_market_seller', ''),
    'ADMIN_BANLIST_BLOCK_PROJECTS_BUYER' => cot_checkbox(0, 'nblock_projects_buyer', ''),
    'ADMIN_BANLIST_BLOCK_PROJECTS_SELLER' => cot_checkbox(0, 'nblock_projects_seller', ''),

    'ADMIN_BANLIST_REASON_FROUSER' => cot_inputbox('text', 'nbanlistreason_foruser', '', ''),
    'ADMIN_BANLIST_SHOW_SUPPORT_BTN' => cot_checkbox(0, 'nbanshow_support_btn', ''),
  ));


  cot_display_messages($t);

  /* === Hook  === */
  foreach (cot_getextplugins('banlist.admin.tags') as $pl)
  {
  	include $pl;
  }
  /* ===== */

  $t->parse('MAIN');

  echo $t->text('MAIN');
}