<?php
/*
 * JohnCMS NEXT Mobile Content Management System (http://johncms.com)
 *
 * For copyright and license information, please see the LICENSE.md
 * Installing the system or redistributions of files must retain the above copyright notice.
 *
 * @link        http://johncms.com JohnCMS Project
 * @copyright   Copyright (C) JohnCMS Community
 * @license     GPL-3
 */

defined('_IN_JOHNCMS') or die('Error: restricted access');

/** @var Psr\Container\ContainerInterface $container */
$container = App::getContainer();

/** @var PDO $db */
$db = $container->get(PDO::class);

/** @var Johncms\Api\UserInterface $systemUser */
$systemUser = $container->get(Johncms\Api\UserInterface::class);

/** @var Johncms\Api\ConfigInterface $config */
$config = $container->get(Johncms\Api\ConfigInterface::class);

$req_down = $db->query("SELECT * FROM `download__files` WHERE `id` = '" . $id . "' AND (`type` = 2 OR `type` = 3)  LIMIT 1");
$res_down = $req_down->fetch();

if (!$req_down->rowCount() || !is_file($res_down['dir'] . '/' . $res_down['name']) || ($res_down['type'] == 3 && $systemUser->rights < 6 && $systemUser->rights != 4)) {
    $error = true;
} else {
    $link = $res_down['dir'] . '/' . $res_down['name'];
}

$more = isset($_GET['more']) ? abs(intval($_GET['more'])) : false;

if ($more) {
    $req_more = $db->query("SELECT * FROM `download__more` WHERE `refid` = '" . $id . "' AND `id` = '$more' LIMIT 1");
    $res_more = $req_more->fetch();

    if (!$req_more->rowCount() || !is_file($res_down['dir'] . '/' . $res_more['name'])) {
        $error = true;
    } else {
        $link = $res_down['dir'] . '/' . $res_more['name'];
    }
}

if ($error) {
    header('Location: ' . $config['homeurl'] . '/404');
} else {
    if (!isset($_SESSION['down_' . $id])) {
        $db->exec("UPDATE `download__files` SET `field`=`field`+1 WHERE `id`=" . $id);
        $_SESSION['down_' . $id] = 1;
    }
    if ($systemUser->isValid()) {
      if ($systemUser->premium == '1') {
          header('Location: ' . $link);
      } else {
            $server = $_SERVER['DOCUMENT_ROOT'];
             require($server.'/system/head.php');
             echo '<div class="mainblok">';
             echo '<div class="phdr">Download File</div>';
             echo '<div class="gmenu">';
             echo '<p align="center">';
             echo 'Download file will be show after 60 seconds if you dont like waiting <font color="red">upgrade your account to premium</font><br/><br/>';
            echo '<font color="blue"><font size="60"><span id="countdown"></span></font></font>' .
                '</p><p align="center"><br/><a id="download_link" href="'.$link.'" style="display:none;"><button class="aan-black"> <i class="icon-download icon-large"></i>  Download Now</button></a></p>' .
                '<noscript>JavaScript needs to be enabled in order to be able to download.</noscript>';
echo '<script type="application/javascript">
    (function(){
       var message = "%d seconds before download link appears";
       var count = 60;
       var countdown_element = document.getElementById("countdown");
       var download_link = document.getElementById("download_link");
       var timer = setInterval(function(){
          if (count) {
              countdown_element.innerHTML = "%d".replace("%d", count);
              count--;
          } else {
              clearInterval(timer);
              countdown_element.style.display = "none";
              download_link.style.display = "";
          }
       }, 1000);
    })();
    </script>
'; 
echo '</div>';
echo '<div class="phdr"><b><a href="javascript:history.back(2)">Back</a></b></div>';
echo '</div>';
require($server . '/system/end.php');
    }
   } else {
            $server = $_SERVER['DOCUMENT_ROOT'];
             require($server.'/system/head.php');
             echo '<div class="mainblok">';
             echo '<div class="phdr">Download File</div>';
             echo '<div class="gmenu">';
             echo '<p align="center">';
             echo 'Download file will be show after 120 seconds if you dont like waiting <font color="red">Please login to wait 60 seccond and upgrade to premium if you not like waiting</font><br/><br/>';
            echo '<font color="blue"><font size="60"><span id="countdown"></span></font></font>' .
                '</p><p align="center"><br/><a id="download_link" href="'.$link.'" style="display:none;"><button class="aan-black"> <i class="icon-download icon-large"></i>  Download Now</button></a></p>' .
                '<noscript>JavaScript needs to be enabled in order to be able to download.</noscript>';
echo '<script type="application/javascript">
    (function(){
       var message = "%d seconds before download link appears";
       var count = 120;
       var countdown_element = document.getElementById("countdown");
       var download_link = document.getElementById("download_link");
       var timer = setInterval(function(){
          if (count) {
              countdown_element.innerHTML = "%d".replace("%d", count);
              count--;
          } else {
              clearInterval(timer);
              countdown_element.style.display = "none";
              download_link.style.display = "";
          }
       }, 1000);
    })();
    </script>
'; 
echo '</div>';
echo '<div class="phdr"><b><a href="javascript:history.back(2)">Back</a></b></div>';
echo '</div>';
require($server . '/system/end.php');
}
}
