<?php

include '../system/sys.php';
include '../system/funcs.php';

if (!isset ($us)) {
        header ('location:/');
        exit;
}

if (!isset ($_GET['id'])) {
        header ('location: /zc');
        exit;
}

$query = DB :: $dbs -> query ("SELECT * FROM `zc_f` WHERE (`id`=?)", array ((int) $_GET['id']));
if ($query -> rowCount ()==0) {
        header ('location: /zc');
        exit;
}

$f = $query -> fetch ();

if ($f['reit']>$us['reit']) {
        header ('location: ');
        exit;
}

$ext = substr ($f['filename'], strrpos ($f['filename'], '.') + 1);
if ($ext != 'zip') {
        header ('location: /zc');
        exit;
}

$u = DB :: $dbs -> queryFetch ("SELECT * FROM `us` WHERE (`id`='" . $f['us'] . "')");

if ($f['reit']>$us['reit']) {
        header ('location: /zc');
        exit;
}

$query = DB::$dbs->query ("SELECT * FROM `zc_kat` WHERE (`id`='" . $f['id_kat'] . "')");
if ($query -> rowCount ()==0) {
        header ('location:/zc');
        exit;
}

$kat = $query -> fetch ();

if (empty ($f['dop'])) {
        if ($kat['mod']!=$us['id']) {
                header ('location: /zc');
                exit;
        }
}


$query = DB :: $dbs -> query ("SELECT * FROM `zc_pk` WHERE (`id`='" . $f['id_pk'] . "')");
if ($query -> rowCount ()==0) {
        header ('location:/zc');
        exit;
}

$pk = $query -> fetch ();

H ($f['name'],'<a href="/zc" style="color:white;">ЗЦ</a> / <a href="/zc/pk' . $pk['id'] . '" style="color:white;">' . $pk['name'] . '</a> / ' . $f['name']);

$f_ext = $ext = substr ('../files/zc/' . $f['filename'], strrpos ('../files/zc/' . $f['filename'], '.') + 1);
if (!in_array ($f_ext, array ('zip','rar','7z'))) {
        echo '<div class="list1">Файл не является архивом!</div>';
}
else {

        $text_exts = array (
                'htaccess',
                'html',
                'wml',
                'php',
                'txt',
                'dat',
                'sql',
                'asp',
                'aspx',
                'jsp',
                'js',
                'pl',
                'css'
        );
        $image_exts = array (
                'jpg',
                'gif',
                'png',
                'jpeg',
                'bmp'
        );



        $allow_exts = array_merge ($text_exts, $image_exts);

        $count_files = 0;
        $size  = 0;
        $files = array ();
                
        $archive = zip_open ('../files/zc/' . $f['filename']);
        while ($file = zip_read ($archive)) {
                $count_files++;
                $size += zip_entry_filesize ($file);
                $files[] = array (
                        'name' => zip_entry_name ($file),
                        'size' => zip_entry_filesize ($file),
                        'content' => zip_entry_read ($file, zip_entry_filesize ($file))
                );
        }
                
        if (isset ($_GET['i'])) {
                $i = intval ($_GET['i']);        
                if (!isset ($files[ $i ]) || empty ($files[ $i ]['name'])) {
                        header ('location: /zc/zip.php?id=' . $f['id']);
                }
                $ext = substr ($files[ $i ]['name'], strrpos ($files[ $i ]['name'], '.') + 1);
                if (!in_array ($ext, $allow_exts)) {
                        header ('location: /zc/zip.php?id=' . $f['id']);
                }
                
                $name = substr ($files[ $i ]['name'], strrpos ($files[ $i ]['name'], '/'));
                $name = str_replace ('/', '', $name);
                echo '<div class="list1">';
                echo 'Файл: ' . $name . '<br/>';
                echo 'Размер: ' . sizef ($files[ $i ]['size']);
                echo '</div>';
                echo '<div class="list1">';
                        
                    if (in_array ($ext, $text_exts)) {
                        echo highlight_code ($files [$i]['content']);
                }
                elseif (in_array ($ext, $image_exts)) {
                        echo '<img src="data:image/' . $ext . ';base64, ' . base64_encode ($files[ $i ]['content']) . '" alt=""/>';
                }
                echo '</div>';
                echo '<div class="list1"><a href="/zc/zip.php?id=' . $f['id'] . '">Вернуться</a></div>';
        }
        else {
        
                echo '<div class="list1">';
                echo 'Всего файлов: ' . $count_files . '<br/>';
                echo 'Размер: ' . sizef($size);
                echo '</div>';

                $items_per_page = 11;
                $pages = ceil ( $count_files / $items_per_page );
                $page = intval ($_GET['page']);

                if ($page < 1) {
                        $page = 1;
                }
                
                if ($page > $pages) {
                        $page = $pages;
                }
                
                $start = $page * $items_per_page - $items_per_page;

                if ($count_files != 0) {
                        for ($i = $start; $i < $start + $items_per_page; $i++) {
                                if (!empty ($files[ $i ]['name'])) {
                                        echo '<div class="list1">';
                                        $ext = substr ($files[ $i ]['name'], strrpos ($files[ $i ]['name'], '.') + 1);
                                        if (!in_array ($ext, $allow_exts)) {
                                                $name = $files[ $i ]['name'];

                                                echo '<img src="" alt=""/> ' . $name;
                                        }
                                        else {
                                                $name = substr ($files[ $i ]['name'], strrpos ($files[ $i ]['name'], '/'));
                                                $name = str_replace ('/', '', $name);
                                                echo '<img src="" alt=""/> <a href="/zc/zip.php?id=' . $f['id'] . '&i=' . $i . '">' . $name . '</a> (' . sizef ($files[ $i ]['size']) . ')';
                                        }
                                        echo '</div>';
                                }
                        }
                        echo '<div class="list1">' . pages ('/zc/zip' . $f['id']) . '</div>';
                }
                else {
                        echo 'Файлов нет!';
                }
                echo '<div class="list1"><a href="/zc/file.php?id=' . $f['id'] . '">К файлу</a></div>';
        }
}

F ();


?>