<?php 
if (isset($_GET['tariff']) && isset($_GET['days_id'])) 
{
  $tariff_id = (preg_match('/([A-z0-9])/', $_GET['tariff']) ? $_GET['tariff'] : 'all');
  $days_id = (int) $_GET['days_id'];
  $tariff = $ads_tariffs['ads_list'][$days_id];
  $days = $ads_tariffs['ads_list'][$days_id]['days'];
  
  if (!isset($ads_tariffs['tariffs'][$tariff_id])) {
    $tariff_id = 'all';
  } 
  
  if (!isset($ads_tariffs['ads_list'][$days_id])) {
    $days_id = '1';
  }
  
  if (isset($ads_tariffs['tariffs'][$tariff_id]['ads_head_course'])) {
    $wmr_head = ($days * $ads_tariffs['tariffs'][$tariff_id]['ads_head_course']);
  }
  
  if (isset($ads_tariffs['tariffs'][$tariff_id]['ads_foot_course'])) {
    $wmr_foot = ($days * $ads_tariffs['tariffs'][$tariff_id]['ads_foot_course']);
  }
  
  if (isset($ads_tariffs['tariffs'][$tariff_id]['fat_course']) && $ads_tariffs['confirm_fat'] == 1) {
    $wmr_fat = ($days * $ads_tariffs['tariffs'][$tariff_id]['fat_course']);
  }
  
  if (isset($ads_tariffs['tariffs'][$tariff_id]['color_course']) && $ads_tariffs['confirm_color'] == 1) {
    $wmr_color = ($days * $ads_tariffs['tariffs'][$tariff_id]['color_course']);
  }
  
  if (isset($ads_tariffs['tariffs'][$tariff_id]['redirect']) && $ads_tariffs['confirm_redirect'] == 1) {
    $wmr_redirect = ($days * $ads_tariffs['tariffs'][$tariff_id]['redirect']);
  }
  
  if (isset($_POST['name'])) {
    
    if ($_POST['type'] == 'ads_head_course' || $_POST['type'] == 'ads_foot_course') {
      $wmr = ($days * $ads_tariffs['tariffs'][$tariff_id][$_POST['type']]);
    }
    
    if (isset($_POST['fat']) && $_POST['fat'] == 1) {
      if (isset($wmr_fat)) {
        $wmr = $wmr + $wmr_fat;
      }
      $fat = 1;
    }
    
    if (isset($_POST['set_color']) && $_POST['set_color'] == 1) {
      if (isset($wmr_color)) {
        $wmr = $wmr + $wmr_color;
      }
      $color = 1;
    }
    
    if (isset($_POST['redirect']) && $_POST['redirect'] == 1) {
      if (isset($wmr_redirect)) {
        $wmr = $wmr + $wmr_redirect;
      }
      $redirect = 1;
    }
    
    $check = array();
    $check['color'] = (isset($color) && $color == 1 ? $_POST['color'] : 0);
    $check['set_color'] = (isset($color) && $color == 1 ? 1 : 0);
    $check['fat'] = (isset($fat) && $fat == 1 ? 1 : 0);
    $check['redirect'] = (isset($redirect) && $redirect == 1 ? 1 : 0);
    $check['name'] = my_esc($_POST['name']);
    $check['url'] = my_esc($_POST['url']);
    $check['days'] = $days;
    $check['type'] = ($_POST['type'] == 'ads_head_course' ? 'ads_head_course' : 'ads_foot_course');
    $check['wmr'] = round(isset($_POST['wmr']) ? $_POST['wmr'] : $wmr, 2);
    $check['key'] = $ads_tariffs['confirm_key'];
    if ($check['name'] && !preg_match('/([A-z0-9А-я\-\_ ]+)/i', $check['name'])) {
      $err[] = __('В %s присутствуют запрещенные символы', 'названии');
    }
    
    if (strlen2($check['name']) > $ads_tariffs['confirm_charset_name']) {
      $err[] = __('Название слишком длинное');
    } elseif (strlen2($check['name']) < 3) {
      $err[] = __('Название слишком короткое');
    }
    
    if (strlen2($check['url']) > $ads_tariffs['confirm_charset_url']) {
      $err[] = __('URL слишком длинный');
    } elseif (strlen2($check['url']) < 2) {
      $err[] = __('URL слишком короткий');
    }
    
    if ($check['url'] && !preg_match('/([A-z0-9А-я\-\_\:\/\.])/i', $check['url'])) {
      $err[] = __('В %s присутствуют запрещенные символы', 'URL');
    }
    
    if ($check['color'] != 0 && !preg_match('/^#?([a-f0-9]{6}|[a-f0-9]{3})$/', $check['color'])) {
      $err[] = __('В %s присутствуют запрещенные символы', 'цвете');
    }
    
    $sign = hash('sha256', implode(':', $check));
    unset($check['key']);
    
    if (isset($_POST['sign'])) {
      
      
      if ($sign == $_POST['sign']) 
      {
        $check['type'] = ($_POST['type'] == 'ads_head_course' ? 'head' : 'foot');
        mysql_query("INSERT INTO `ads` (`name`, `url`, `color`, `days`, `type`, `fat`, `redirect`, `id_user`, `time`, `time_end`, `str`) VALUES ('$check[name]','$check[url]','$check[color]','$check[days]','$check[type]','$check[fat]','$check[redirect]','" . (isset($user) ? $user['id'] : 0) . "','" . $time . "','" . ($time + 60 * 60 * 24 * $check['days']) . "', '$tariff_id')");
        
        $ads = mysql_fetch_assoc(mysql_query("SELECT * FROM `ads` WHERE `time` = '$time' LIMIT 1"));

        $description = "Название: $check[name]\nСсылка: $check[url]\nСрок: " . des2num($check['days'], $config['days_name']) . "\nМесто: " . ($check['type'] == 'ads_head_course' ? 'Верх' : 'Низ') . " сайта\nПоказ: " . $ads_tariffs['tariffs'][$tariff_id]['name'] . "\nЦвет: " . ($check['color'] ? $check['color'] : 'Нет') . "\nЖирным: " . ($check['fat'] ? 'Да' : 'Нет') . "\nПрямая: " . ($check['redirect'] ? 'Да' : 'Нет') . "\n";
        
        mysql_query("INSERT INTO `payment` (`client_id`, `time`, `name`, `service`, `service_id`, `wmr`, `count`, `discount`, `description`) VALUES ('" . (isset($user) ? $user['id'] : 0) . "', '$time', 'Покупка рекламы для $check[url]', 'ads', '$ads[id]', '$check[wmr]', '$check[days]', '$tariff[discount]', '$description')");
      
        $pay_id = mysql_insert_id();
        header('Location: /payment/?service=ads&id=' . $pay_id);
        exit;  
      } 
      
      else {
        $err[] = __('Цифровая подпись неверна');
      }
    }
    
    $check['sign'] = $sign;
  }
}
$set['title'] = __('Покупка рекламы'); 
include_once H.'sys/inc/thead.php';
title();
aut();
err();
?>
<ol class="breadcrumb">
  <li><img src="/<?= $config['patch_icons']?>/arrow-180.png" /> <a href="?func=menu"><?= __('Биллинг')?></a></li>
  <li class="active"><?= __('Реклама')?></li>
</ol>

<? 
if (isset($check) && !isset($err)) {
  ?>
  <div class="nav2">
    Название: <?= text($check['name'])?><br />
    Ссылка: <?= text($check['url'])?><br />
    Срок: <?= des2num($check['days'], $config['days_name'])?><br />
    Место: <?= text($check['type'] == 'ads_head_course' ? 'Верх' : 'Низ')?> сайта<br />
    Показ: <?= $ads_tariffs['tariffs'][$tariff_id]['name']?><br />
    Цвет: <?= ($check['color'] ? '<span style="color: ' . text($check['color']) . ';">' . text($check['color']) . '</span>' : 'Нет')?><br />
    Жирным: <?= ($check['fat'] ? 'Да' : 'Нет')?><br />
    Прямая: <?= ($check['redirect'] ? 'Да' : 'Нет')?><br />
    <b>Итого: <?= des2num($check['wmr'], array('рубль', 'рубля', 'рублей'))?></b>
  </div>
  
  <form action="?func=ads.tariffs&amp;tariff=<?= $tariff_id?>&amp;days_id=<?= $days_id?>" method="POST">
    <? foreach ($check AS $key => $value) { ?>
      <input type="hidden" name="<?= $key?>" value="<?= text($value)?>">
    <? } ?>
    <button type="submit" class="btn btn-success"><?= __('Перейти к оплате')?></button>
  </form>
  <?
} 

elseif (isset($_GET['tariff']) && isset($_GET['days_id'])) {
  
  $check = (isset($check) ? $check : array(
                                        'name' => '', 
                                        'url' => '', 
                                        'type' => '0', 
                                        'color' => '0',
                                        'fat' => 0, 
                                        'set_color' => 0, 
                                        'redirect' => '0',
                                        ));
  
  ?>
  <div class="mess">
    <b>&laquo;<?= $ads_tariffs['tariffs'][$tariff_id]['name']?>&raquo;</b><br />
    <?= $ads_tariffs['tariffs'][$tariff_id]['desc']?><br />
    <?= __('Стоимость')?>:<br />
    
    <? if (isset($ads_tariffs['tariffs'][$tariff_id]['ads_head_course'])) { ?>
      <?= __('Верх сайта')?>: <?= des2num($days, $config['days_name'])?> = <?= des2num($wmr_head, array('рубль', 'рубля', 'рублей'))?><br />
    <? } ?>
    
    <? if (isset($ads_tariffs['tariffs'][$tariff_id]['ads_foot_course'])) { ?>
      <?= __('Низ сайта')?>: <?= des2num($days, $config['days_name'])?> = <?= des2num($wmr_foot, array('рубль', 'рубля', 'рублей'))?><br />
    <? } ?>
    
    <? if (isset($wmr_fat)) { ?>
      <?= __('Жирность')?>: +<?= des2num($wmr_fat, array('рубль', 'рубля', 'рублей'))?><br />
    <? } ?>
    
    <? if (isset($wmr_color)) { ?>
      <?= __('Цвет ссылки')?>: +<?= des2num($wmr_color, array('рубль', 'рубля', 'рублей'))?><br />
    <? } ?>
    
    <? if (isset($wmr_redirect)) { ?>
      <?= __('Прямая ссылка')?>: +<?= des2num($wmr_redirect, array('рубль', 'рубля', 'рублей'))?><br />
    <? } ?>
  </div>
  
  <form action="?func=ads.tariffs&amp;tariff=<?= $tariff_id?>&amp;days_id=<?= $days_id?>" method="POST">
    URL <?= __('ссылки')?>[<?= $ads_tariffs['confirm_charset_url']?>]:<br />
    <input type="text" name="url" maxlength="<?= $ads_tariffs['confirm_charset_url']?>" value="<?= ($check['url'] ? text($check['url']) : '')?>"/><br />
    
    <?= __('Название ссылки')?>[<?= $ads_tariffs['confirm_charset_name']?>]:<br />
    <input type="text" name="name" maxlength="<?= $ads_tariffs['confirm_charset_name']?>" value="<?= ($check['name'] ? text($check['name']) : '')?>"/><br />
    
    <?= __('Срок')?>[<?= __('дней')?>]:<br />
    <input type="text" name="days" value="<?= $days?>" disabled="" /><br />
    
    <?= __('Место размещения')?>:<br />
    <select name="type"> 
      <? if (isset($wmr_foot)) { ?>
        <option value="ads_foot_course" <?= ($check['type'] == 'ads_foot_course' ? 'selected' : '')?>><?= __('Низ сайта')?></option>
      <? } ?>
      <? if (isset($wmr_head)) { ?>
        <option value="ads_head_course" <?= ($check['type'] == 'ads_head_course' ? 'selected' : '')?>><?= __('Верх сайта')?></option>
      <? } ?>
    </select>
    
    <? if ($ads_tariffs['confirm_color'] == 1) { ?>
      <?= __('Цвет ссылки')?>:<br />
        <input type="color" name="color" value="<?= ($check['color'] ? text($check['color']) : '#ff0000')?>" />
    <? } ?>
    
    <? if ($ads_tariffs['confirm_fat'] == 1) { ?>
      <label><input type="checkbox" name="fat" value="1" <?= ($check['fat'] ? 'checked' : '')?> /> <?= __('Жирность')?></label><br />
    <? } ?>
    
    <? if ($ads_tariffs['confirm_color'] == 1) { ?>
      <label><input type="checkbox" name="set_color" value="1" <?= ($check['color'] ? 'checked' : '')?> /> <?= __('Цвет ссылки')?></label><br />
    <? } ?>
    
    <? if ($ads_tariffs['confirm_redirect'] == 1) { ?>
      <label><input type="checkbox" name="redirect" value="1" <?= ($check['redirect'] ? 'checked' : '')?> /> <?= __('Прямая ссылка')?></label><br />
    <? } ?>
    
    <button type="submit" class="btn btn-success"><?= __('Продолжить')?></button>
  </form>
  <?
}

elseif (isset($_GET['tariff'])) {
  
  $tariff_id = (preg_match('/([A-z0-9])/', $_GET['tariff']) ? $_GET['tariff'] : 'all');
  
  if (!isset($ads_tariffs['tariffs'][$tariff_id])) {
    $tariff_id = 'all';
  }
  
  foreach($ads_tariffs['ads_list'] AS $key => $post) { ?>
    <div class="nav2">
      <a href="?func=ads.tariffs&amp;tariff=<?= $tariff_id?>&amp;days_id=<?= $key?>">
        <img src="/<?= $config['patch_icons']?>/globe-plus.png" /> <?= __('Cрок показа')?> <?= des2num($post['days'], $config['days_name'])?>
      </a>
      
      <? if ($post['discount'] > 0) { ?>
        скидка <span class="on"><?= ($post['discount'])?>%</span>
      <? } ?>
    </div>
  <? 
  } 
}

else {
  foreach($ads_tariffs['tariffs'] AS $key => $post) { ?>
    <div class="nav2">
      <a href="?func=ads.tariffs&amp;tariff=<?= $key?>">
        <img src="/<?= $config['patch_icons']?>/globe-plus.png" /> <?= $post['name']?><br />
      </a>
      
      <?= $post['desc']?>
    </div>
  <? 
  }   
}
?>


<ol class="breadcrumb">
  <li><img src="/<?= $config['patch_icons']?>/arrow-180.png" /> <a href="?func=menu"><?= __('Биллинг')?></a></li>
  <li class="active"><?= __('Реклама')?></li>
</ol>