<?php

/* DCMS Special
 * Дата последнего редактирования 16.01.2016
 * Модифицировал densnet
 */

foreach (array('start', 'compress', 'sess', 'settings', 'db_connect', 'ipua', 'fnc', 'user', 'thead') as $inc) {
    require_once "../../sys/inc/$inc.php";
}

$doc->Title('Онлайн видео');

$d = (isset($_GET['d'])) ? htmlspecialchars($_GET['d']) : null;
aut();

$id = @intval($_GET['id']);

switch ($d) {
    case 'video': {
            $q = $db->query("SELECT * FROM `videos_cat` WHERE `id`='" . $id . "' ");
            while ($res = $db->get_row($q)) {

                #Навигация
                echo "<div class='list-group-item-null list-group-item-grey'><small>";
                echo "<a href='/' class='hint--right' data-hint='" . lang('На главную') . "'><i class='fa fa-home fa-lg'></i></a> <i class='fa fa-angle-right fa-fw'></i> ";
                echo "<a href='/modules/videos/'>" . lang('Видео') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
                echo "<a href='/modules/videos/'>" . cutStr($res['name'], 30) . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
                echo lang('Новое видео');
                echo "</small></div><br />";
            }

            echo "<form action='razdel.php?id=$id&amp;d=add' class='list-group-item-null' method='POST'>";

            $doc->Input('kod', 'Введите ссылку или код видео', 500);
            echo "<br />";
            $doc->Button('btn btn-success btn-sm', null, 'plus', 'Добавить видео');

            echo "</form>";

            require_once H . 'sys/inc/tfoot.php';
            exit;
        }
        break;

    case 'add':

        $kod = $_POST['kod'];
        if (utf8_strlen($kod) >= 11) {
            $kod = preg_replace('#(.*)(v=)#isU', '', $kod);
            $kod = preg_replace('/\&.*/', '', $kod);
            $str = file_get_contents('http://m.youtube.com/watch?v=' . $kod);
            $name = preg_replace('#(<)(.*)(<title>)#isU', '', $str);
            $name = preg_replace('#(</title>)(.*)(</html>)#isU', '', $name);
            $name = preg_replace('#( - YouTube)#isU', '', $name);
            $name = $db->safesql($name);

            $db->query("INSERT INTO `videos` (`name`,`kod`,`time`,`id_cat`,`id_user`) values('$name', '$kod', '$time', '" . $id . "', '$user[id]')");
            $_SESSION['message'] = lang('Видео успешно добавлено');
            header('Location: razdel.php?id=' . $id . '');
            exit;
        }
        break;
    default:

        $q = $db->query("SELECT * FROM `videos_cat` WHERE `id`='" . $id . "' ");
        while ($res = $db->get_row($q)) {

            #Навигация
            echo "<div class='list-group-item-null list-group-item-grey'><small>";
            echo "<a href='/' class='hint--right' data-hint='" . lang('На главную') . "'><i class='fa fa-home fa-lg'></i></a> <i class='fa fa-angle-right fa-fw'></i> ";
            echo "<a href='/modules/videos/'>" . lang('Видео') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
            echo cutStr($res['name'], 30);
            echo "</small></div><br />";
        }

        if (isset($user)) {
            $doc->Link('list-group-item-null list-group-item-grey', "razdel.php?id=" . $_GET['id'] . "&amp;d=video", 'youtube', 'Добавить видео');
        }

        $k_post = $db->super_query("SELECT COUNT(*) as count FROM `videos`  WHERE `id_cat`='" . $id . "'");
        $k_post = $k_post['count'];
        $k_page = k_page($k_post, $set['p_str']);
        $page = page($k_page);
        $start = $set['p_str'] * $page - $set['p_str'];

        if ($k_post == 0) {
            $doc->NoResult();
        }

        $q = $db->query("SELECT * FROM `videos` WHERE `id_cat`='" . $id . "' ORDER BY `id` DESC LIMIT $start, $set[p_str]");
        while ($res = $db->get_row($q)) {
            $k = $db->super_query("SELECT COUNT(*) as count FROM `videos_like` WHERE `id_videos` = '" . $res['id'] . "'  LIMIT 1");
            $like = $k['count'];
            $k = $db->super_query("SELECT COUNT(*) as count FROM `videos_views` WHERE `id_videos` = '" . $res['id'] . "'  LIMIT 1");
            $pokz = $k['count'];
            $k = $db->super_query("SELECT COUNT(*) as count FROM `videos_komm` WHERE `id_videos` = '" . $res['id'] . "'  LIMIT 1");
            $comm = $k['count'];
            echo "<table class='list-group-item-null'><tr><td class='icon14'>";
            echo '<img src="http://i.ytimg.com/vi/' . $res['kod'] . '/1.jpg" width="70" alt="screen" />';
            echo "</td><td class='null'>";
            echo '<a href="video.php?id=' . $res['id'] . '"> ' . $res['name'] . '</a><br/>';

            echo "<i class='fa fa-heart fa-fw'></i> $like ";
            echo "<i class='fa fa-eye fa-fw'></i> $pokz ";
            echo "<i class='fa fa-comments fa-fw'></i> $comm ";

            echo "</td></tr></table>";
            echo "<div class='hr'></div>";
        }
}


if ($k_page > 1) {
    str('?id=' . $id . '&amp;', $k_page, $page);
}

err();
require_once H . 'sys/inc/tfoot.php';
