<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
function checkall()
 {
     $('form#form input[type=checkbox]').attr('checked','checked');
     document.cookie = "allfiles=1";
     return false;
 }

 function uncheckall()
 {
     $('form#form input[type=checkbox]').removeAttr('checked');
     return false;
 }
</script>
<?php
if(isset($_POST['checkall']))
{
$_SESSION['all']=1;
}

if (isset($_GET['plid']) and isset($_GET['elid'])) {
    $content = api_query('https://' . $server . '/manager/ispmgr?func=file&elid=' . urlencode($_GET['elid']) . '&plid=' . urlencode($_GET['plid']) . '&out=xml&authinfo=' . urlencode($_SESSION['username']) . ':' . urlencode($_SESSION['password']));
} elseif (isset($_GET['elid'])) {
    $content = api_query('https://' . $server . '/manager/ispmgr?func=file&elid=' . urlencode($_GET['elid']) . '&out=xml&authinfo=' . urlencode($_SESSION['username']) . ':' . urlencode($_SESSION['password']));
} else {
    $content = api_query('https://' . $server . '/manager/ispmgr?func=file&out=xml&authinfo=' . urlencode($_SESSION['username']) . ':' . urlencode($_SESSION['password']));
}
$parse_xml = simplexml_load_string($content);

echo '<div class="post">Путь: /';
if (isset($_GET['plid'])) {
    echo htmlentities($_GET['plid'], ENT_QUOTES, 'UTF-8');
}
if (isset($_GET['elid']) and isset($_GET['plid'])) {
    echo '/';
}
if (isset($_GET['elid'])) {
    echo htmlentities($_GET['elid'], ENT_QUOTES, 'UTF-8');
}
echo "\n" . '</div>' . "\n";

if (isset($_GET['plid']) and isset($_GET['elid'])) {
    $elid = strrchr($_GET['plid'], '/');
    if ($elid !== false) {
        $elid = substr($elid, 1);
    }
    $plid = array_reverse(explode('/', $_GET['plid']));
    $count_plid = sizeof($plid);
    if ($count_plid > 1) {
        unset($plid[0]);
        $plid = implode('/', array_reverse($plid));
    } else {
        $plid = $plid[0];
    }
    echo '<a href="?func=file';
    if ($elid !== false) {
        echo '&amp;elid=' . urlencode($elid) . '&amp;plid=' . urlencode($plid);
    } else {
        echo '&amp;elid=' . urlencode($_GET['plid']);
    }
    echo '"><img src="images/back.gif" alt=".." /></a><br />' . "\n";
} elseif (isset($_GET['elid'])) {
    echo '<a href="?func=file"><img src="images/back.gif" alt=".." /></a>' . "\n";
}

// --------------- Функция форматированого вывода размера объектов ------------ //
function formatsize($object_size)
{
    if ($object_size >= 1048576000) {
        $object_size = round(($object_size / 1073741824), 2) . 'Gb';
    } elseif ($object_size >= 1024000) {
        $object_size = round(($object_size / 1048576), 2) . 'Mb';
    } elseif ($object_size >= 1000) {
        $object_size = round(($object_size / 1024), 2) . 'Kb';
    } else {
        $object_size = round($object_size) . 'b';
    }
    return $object_size;
}
// --------------- Функция форматированого вывода размера объектов ------------ //

$list_dir = array();
$list_symlink = array();
$list_tar = array();
$list_gz = array();
$list_bz = array();
$list_zip = array();
$list_arj = array();
$list_rar = array();
$list_file = array();
$list_log = array();

foreach($parse_xml->elem as $var) {
    $dname_attributes = $var->dname->attributes();
    if ($dname_attributes['xprop'] == '0') {
        $list_dir["'.(string)$var->name.'"] = array((string)$var->name, (string)$var->dname, '0', (string)$var->attr);
    } elseif ($dname_attributes['xprop'] == '99') {
        $list_symlink["'.(string)$var->name.'"] = array((string)$var->name, (string)$var->dname, '99', (string)$var->attr);
    } elseif ($dname_attributes['xprop'] == '1') {
        $list_tar["'.(string)$var->name.'"] = array((string)$var->name, (string)$var->dname, '1', (string)$var->attr, (int)$var->size);
    } elseif ($dname_attributes['xprop'] == '2') {
        $list_gz["'.(string)$var->name.'"] = array((string)$var->name, (string)$var->dname, '2', (string)$var->attr, (int)$var->size);
    } elseif ($dname_attributes['xprop'] == '3') {
        $list_bz["'.(string)$var->name.'"] = array((string)$var->name, (string)$var->dname, '3', (string)$var->attr, (int)$var->size);
    } elseif ($dname_attributes['xprop'] == '4') {
        $list_zip["'.(string)$var->name.'"] = array((string)$var->name, (string)$var->dname, '4', (string)$var->attr, (int)$var->size);
    } elseif ($dname_attributes['xprop'] == '5') {
        $list_arj["'.(string)$var->name.'"] = array((string)$var->name, (string)$var->dname, '5', (string)$var->attr, (int)$var->size);
    } elseif ($dname_attributes['xprop'] == '6') {
        $list_rar["'.(string)$var->name.'"] = array((string)$var->name, (string)$var->dname, '6', (string)$var->attr, (int)$var->size);
    } elseif ($dname_attributes['xprop'] == '100') {
        $list_file["'.(string)$var->name.'"] = array((string)$var->name, (string)$var->dname, '100', (string)$var->attr, (int)$var->size);
    } elseif ($dname_attributes['xprop'] == '101') {
        $list_log["'.(string)$var->name.'"] = array((string)$var->name, (string)$var->dname, '101', (string)$var->attr, (int)$var->size);
    }
}

ksort($list_dir);
ksort($list_symlink);
ksort($list_tar);
ksort($list_gz);
ksort($list_bz);
ksort($list_zip);
ksort($list_arj);
ksort($list_rar);
ksort($list_file);
ksort($list_log);

$list_all = array_merge($list_dir, $list_symlink, $list_tar, $list_gz, $list_bz, $list_zip, $list_arj, $list_rar, $list_file, $list_log);
foreach($list_all as $var) {
    $list_all_num[] = $var;
}
unset($list_all);

if (isset($list_all_num)) {
    $count_all_num = sizeof($list_all_num);
    $s_max = ceil($count_all_num / $_SESSION['rows']);

    if (isset($_GET['s'])) {
        $s = (int)$_GET['s'];
        if ($s > $s_max) {
            $s = $s_max;
        } elseif ($s < 1) {
            $s = 1;
        }
    } else {
        $s = 1;
    }

    $rows_max = $s * $_SESSION['rows']-1;
    $rows_min = $rows_max - $_SESSION['rows'] + 1;

    if ($rows_max > $count_all_num-1) {
        $rows_max = $count_all_num-1;
    }

    echo '<div class="post"><form id="form" action="?func=file.act';
    if (isset($_GET['plid']) and isset($_GET['elid'])) {
        echo '&amp;elid=' . urlencode($_GET['elid']) . '&amp;plid=' . urlencode($_GET['plid']);
    } elseif (isset($_GET['elid'])) {
        echo '&amp;elid=' . urlencode($_GET['elid']);
    }
    echo '" method="post">' . "\n";
    for($i = $rows_min; $i <= $rows_max; $i++) {

        // Папки
        if ($list_all_num[$i][2] == '0') {
            echo '<input type="checkbox" name="check[]" value="' . htmlentities($list_all_num[$i][0], ENT_QUOTES, 'UTF-8') . '" /> <a href="?func=file.inside&amp;file=' . urlencode($list_all_num[$i][0]) . '&amp;xprop=0';
            if (isset($_GET['elid'])) {
                echo '&amp;elid=' . urlencode($_GET['elid']);
            }
            if (isset($_GET['plid'])) {
                echo '&amp;plid=' . urlencode($_GET['plid']);
            }
            echo '"><img src="images/p-file-0.png" alt="d" /></a> <a href="?func=file&amp;elid=' . urlencode($list_all_num[$i][0]);
            if (isset($_GET['elid']) and isset($_GET['plid'])) {
                echo '&amp;plid=' . urlencode($_GET['plid'] . '/' . $_GET['elid']);
            } elseif (isset($_GET['elid'])) {
                echo '&amp;plid=' . urlencode($_GET['elid']);
            }
            echo '">' . htmlentities($list_all_num[$i][1], ENT_QUOTES, 'UTF-8') . '</a>';
            if ($list_all_num[$i][3] <> '') {
                echo ' [' . $list_all_num[$i][3] . ']';
            }
            echo '<br />' . "\n";
        }

        // Ссылки
        elseif ($list_all_num[$i][2] == '99') {
            echo '<input type="checkbox" name="check[]" value="' . htmlentities($list_all_num[$i][0], ENT_QUOTES, 'UTF-8') . '" /> <a href="?func=file.inside&amp;file=' . urlencode($list_all_num[$i][0]) . '&amp;xprop=99';
            if (isset($_GET['elid'])) {
                echo '&amp;elid=' . urlencode($_GET['elid']);
            }
            if (isset($_GET['plid'])) {
                echo '&amp;plid=' . urlencode($_GET['plid']);
            }
            echo '"><img src="images/p-file-99.png" alt="s" /></a> <b>' . htmlentities($list_all_num[$i][1], ENT_QUOTES, 'UTF-8') . '</b>';
            if ($list_all_num[$i][3] <> '') {
                echo ' [' . $list_all_num[$i][3] . ']';
            }
            echo '<br />' . "\n";
        }

        // Tar
        elseif ($list_all_num[$i][2] == '1') {
            echo '<input type="checkbox" name="check[]" value="' . htmlentities($list_all_num[$i][0], ENT_QUOTES, 'UTF-8') . '" /> <a href="?func=file.inside&amp;file=' . urlencode($list_all_num[$i][0]) . '&amp;xprop=1';
            if (isset($_GET['elid'])) {
                echo '&amp;elid=' . urlencode($_GET['elid']);
            }
            if (isset($_GET['plid'])) {
                echo '&amp;plid=' . urlencode($_GET['plid']);
            }
            echo '"><img src="images/p-file-1.png" alt="t" /></a> <b>' . htmlentities($list_all_num[$i][1], ENT_QUOTES, 'UTF-8') . '</b>';
            if ($list_all_num[$i][3] <> '') {
                echo ' [' . $list_all_num[$i][3] . ']';
            }
            echo ' ' . formatsize($list_all_num[$i][4]) . '<br />' . "\n";
        }

        // Gz
        elseif ($list_all_num[$i][2] == '2') {
            echo '<input type="checkbox" name="check[]" value="' . htmlentities($list_all_num[$i][0], ENT_QUOTES, 'UTF-8') . '" /> <a href="?func=file.inside&amp;file=' . urlencode($list_all_num[$i][0]) . '&amp;xprop=2';
            if (isset($_GET['elid'])) {
                echo '&amp;elid=' . urlencode($_GET['elid']);
            }
            if (isset($_GET['plid'])) {
                echo '&amp;plid=' . urlencode($_GET['plid']);
            }
            echo '"><img src="images/p-file-2.png" alt="g" /></a> <b>' . htmlentities($list_all_num[$i][1], ENT_QUOTES, 'UTF-8') . '</b>';
            if ($list_all_num[$i][3] <> '') {
                echo ' [' . $list_all_num[$i][3] . ']';
            }
            echo ' ' . formatsize($list_all_num[$i][4]) . '<br />' . "\n";
        }

        // Bz
        elseif ($list_all_num[$i][2] == '3') {
            echo '<input type="checkbox" name="check[]" value="' . htmlentities($list_all_num[$i][0], ENT_QUOTES, 'UTF-8') . '" /> <a href="?func=file.inside&amp;file=' . urlencode($list_all_num[$i][0]) . '&amp;xprop=3';
            if (isset($_GET['elid'])) {
                echo '&amp;elid=' . urlencode($_GET['elid']);
            }
            if (isset($_GET['plid'])) {
                echo '&amp;plid=' . urlencode($_GET['plid']);
            }
            echo '"><img src="images/p-file-3.png" alt="b" /></a> <b>' . htmlentities($list_all_num[$i][1], ENT_QUOTES, 'UTF-8') . '</b>';
            if ($list_all_num[$i][3] <> '') {
                echo ' [' . $list_all_num[$i][3] . ']';
            }
            echo ' ' . formatsize($list_all_num[$i][4]) . '<br />' . "\n";
        }

        // Zip
        elseif ($list_all_num[$i][2] == '4') {
            echo '<input type="checkbox" name="check[]" value="' . htmlentities($list_all_num[$i][0], ENT_QUOTES, 'UTF-8') . '" /> <a href="?func=file.inside&amp;file=' . urlencode($list_all_num[$i][0]) . '&amp;xprop=4';
            if (isset($_GET['elid'])) {
                echo '&amp;elid=' . urlencode($_GET['elid']);
            }
            if (isset($_GET['plid'])) {
                echo '&amp;plid=' . urlencode($_GET['plid']);
            }
            echo '"><img src="images/p-file-4.png" alt="z" /> <b>' . htmlentities($list_all_num[$i][1], ENT_QUOTES, 'UTF-8') . '</b></a>';
            if ($list_all_num[$i][3] <> '') {
                echo ' [' . $list_all_num[$i][3] . ']';
            }
            echo ' ' . formatsize($list_all_num[$i][4]) . '<br />' . "\n";
        }

        // Arj
        elseif ($list_all_num[$i][2] == '5') {
            echo '<input type="checkbox" name="check[]" value="' . htmlentities($list_all_num[$i][0], ENT_QUOTES, 'UTF-8') . '" /> <a href="?func=file.inside&amp;file=' . urlencode($list_all_num[$i][0]) . '&amp;xprop=5';
            if (isset($_GET['elid'])) {
                echo '&amp;elid=' . urlencode($_GET['elid']);
            }
            if (isset($_GET['plid'])) {
                echo '&amp;plid=' . urlencode($_GET['plid']);
            }
            echo '"><img src="images/p-file-5.png" alt="a" /></a> <b>' . htmlentities($list_all_num[$i][1], ENT_QUOTES, 'UTF-8') . '</b>';
            if ($list_all_num[$i][3] <> '') {
                echo ' [' . $list_all_num[$i][3] . ']';
            }
            echo ' ' . formatsize($list_all_num[$i][4]) . '<br />' . "\n";
        }

        // Rar
        elseif ($list_all_num[$i][2] == '6') {
            echo '<input type="checkbox" name="check[]" value="' . htmlentities($list_all_num[$i][0], ENT_QUOTES, 'UTF-8') . '" /> <a href="?func=file.inside&amp;file=' . urlencode($list_all_num[$i][0]) . '&amp;xprop=6';
            if (isset($_GET['elid'])) {
                echo '&amp;elid=' . urlencode($_GET['elid']);
            }
            if (isset($_GET['plid'])) {
                echo '&amp;plid=' . urlencode($_GET['plid']);
            }
            echo '"><img src="images/p-file-6.png" alt="r" /> <b>' . htmlentities($list_all_num[$i][1], ENT_QUOTES, 'UTF-8') . '</b></a>';
            if ($list_all_num[$i][3] <> '') {
                echo ' [' . $list_all_num[$i][3] . ']';
            }
            echo ' ' . formatsize($list_all_num[$i][4]) . '<br />' . "\n";
        }

        // Файлы
        elseif ($list_all_num[$i][2] == '100') {
            echo '<input type="checkbox" name="check[]" value="' . htmlentities($list_all_num[$i][0], ENT_QUOTES, 'UTF-8') . '" /> <a href="?func=file.inside&amp;file=' . urlencode($list_all_num[$i][0]) . '&amp;xprop=100';
            if (isset($_GET['elid'])) {
                echo '&amp;elid=' . urlencode($_GET['elid']);
            }
            if (isset($_GET['plid'])) {
                echo '&amp;plid=' . urlencode($_GET['plid']);
            }
            echo '"><img src="images/p-file-100.png" alt="f" /> <b>' . htmlentities($list_all_num[$i][1], ENT_QUOTES, 'UTF-8') . '</b></a>';
            if ($list_all_num[$i][3] <> '') {
                echo ' [' . $list_all_num[$i][3] . ']';
            }
            echo ' ' . formatsize($list_all_num[$i][4]) . '<br />' . "\n";
        }

        // Логи
        elseif ($list_all_num[$i][2] == '101') {
            echo '<input type="checkbox" name="check[]" value="' . htmlentities($list_all_num[$i][0], ENT_QUOTES, 'UTF-8') . '" /> <a href="?func=file.inside&amp;file=' . urlencode($list_all_num[$i][0]) . '&amp;xprop=101';
            if (isset($_GET['elid'])) {
                echo '&amp;elid=' . urlencode($_GET['elid']);
            }
            if (isset($_GET['plid'])) {
                echo '&amp;plid=' . urlencode($_GET['plid']);
            }
            echo '"><img src="images/p-file-101.png" alt="l" /></a> <b>' . htmlentities($list_all_num[$i][1], ENT_QUOTES, 'UTF-8') . '</b>';
            if ($list_all_num[$i][3] <> '') {
                echo ' [' . $list_all_num[$i][3] . ']';
            }
            echo ' ' . formatsize($list_all_num[$i][4]) . '<br />' . "\n";
        }
    }

    echo ' <button onclick="return checkall();">Отметить все</button><br/>';
//     echo '<input type="submit" name="checkall" value="Отметить все" /><br />';
 /*   echo 'Действие:<br />' . "\n";
    echo '<select name="act">' . "\n";
    echo '<option value="">-</option>' . "\n";
    echo '<option value="edit">Редактировать</option>' . "\n";
    echo '<option value="pack">Запаковать</option>' . "\n";
    echo '<option value="delete">Удалить</option>' . "\n";
    echo '<option value="pastec">Копировать</option>' . "\n";
    echo '<option value="pastex">Вырезать</option>' . "\n";

    if (isset($_SESSION['paste'])) {
        echo '<option value="paste">Вставить</option>' . "\n";
    }

    echo '</select>' . "\n";*/
    echo '<input type="submit" value="Выполнить" />' . "\n";
    echo '</form>' . "\n";
    echo '</div>' . "\n";

    echo 'Папок: ' . sizeof($list_dir) . ', Ссылок: ' . sizeof($list_symlink) . ', Файлов: ' . (sizeof($list_tar) + sizeof($list_gz) + sizeof($list_bz) + sizeof($list_zip) + sizeof($list_arj) + sizeof($list_rar) + sizeof($list_file) + sizeof($list_log)) . "\n";
} else {
    echo '<br />' . "\n";
    echo 'Папка пуста<br />' . "\n";

    if (isset($_SESSION['paste'])) {
        echo '<div class="post"><form action="?func=file.act';
        if (isset($_GET['plid']) and isset($_GET['elid'])) {
            echo '&amp;elid=' . urlencode($_GET['elid']) . '&amp;plid=' . urlencode($_GET['plid']);
        } elseif (isset($_GET['elid'])) {
            echo '&amp;elid=' . urlencode($_GET['elid']);
        }
        echo '" method="post">' . "\n";

        echo 'Действие:<br />' . "\n";
        echo '<select name="act">' . "\n";
        echo '<option value="paste">Вставить</option>' . "\n";
        echo '</select>' . "\n";
        echo '<input type="submit" value="Выполнить" /><br />' . "\n";
        echo '</form></div>' . "\n";
    }
}

// --------------------------- Постраничная навигация ------------------------- //
if (isset($list_all_num)) {
    if ($s_max > 1) {
        echo '<div class="post">' . "\n";
        echo 'Страницы:<br />' . "\n";

        $s_prev = $s-2;
        $s_next = $s + 2;
        if ($s_prev < 1) {
            $s_prev = 1;
        }
        if ($s_next > $s_max) {
            $s_next = $s_max;
        }
        if ($s_prev > 2) {
            echo '<a href="?func=file&amp;';
            if (isset($_GET['elid'])) {
                echo 'elid=' . urlencode($_GET['elid']) . '&amp;';
            }
            if (isset($_GET['plid'])) {
                echo 'plid=' . urlencode($_GET['plid']) . '&amp;';
            }
            echo 's=1">1</a> ... ';
        } elseif ($s_prev == 2) {
            echo '<a href="?func=file&amp;';
            if (isset($_GET['elid'])) {
                echo 'elid=' . urlencode($_GET['elid']) . '&amp;';
            }
            if (isset($_GET['plid'])) {
                echo 'plid=' . urlencode($_GET['plid']) . '&amp;';
            }
            echo 's=1">1</a> ';
        }
        for($i = $s_prev; $i <= $s_next; $i++) {
            if ($i == $s) {
                echo $i . ' ';
            } else {
                echo '<a href="?func=file&amp;';
                if (isset($_GET['elid'])) {
                    echo 'elid=' . urlencode($_GET['elid']) . '&amp;';
                }
                if (isset($_GET['plid'])) {
                    echo 'plid=' . urlencode($_GET['plid']) . '&amp;';
                }
                echo 's=' . $i . '">' . $i . '</a> ';
            }
        }
        if ($s_next < $s_max-1) {
            echo '... <a href="?func=file&amp;';
            if (isset($_GET['elid'])) {
                echo 'elid=' . urlencode($_GET['elid']) . '&amp;';
            }
            if (isset($_GET['plid'])) {
                echo 'plid=' . urlencode($_GET['plid']) . '&amp;';
            }
            echo 's=' . $s_max . '">' . $s_max . '</a>';
        } elseif ($s_next == $s_max-1) {
            echo '<a href="?func=file&amp;';
            if (isset($_GET['elid'])) {
                echo 'elid=' . urlencode($_GET['elid']) . '&amp;';
            }
            if (isset($_GET['plid'])) {
                echo 'plid=' . urlencode($_GET['plid']) . '&amp;';
            }
            echo 's=' . $s_max . '">' . $s_max . '</a>';
        }

        echo "\n</div>";
    }
}
// --------------------------- Постраничная навигация ------------------------- //

echo '<div class="post">' . "\n";
echo 'Меню папки:<br />' . "\n";
echo '&#187; <a href="?func=file.new';
if (isset($_GET['elid'])) {
    echo '&amp;elid=' . urlencode($_GET['elid']);
}
if (isset($_GET['plid'])) {
    echo '&amp;plid=' . urlencode($_GET['plid']);
}
echo '">Создать</a><br />' . "\n";
echo '&#187; <a href="?func=file.upload';
if (isset($_GET['elid'])) {
    echo '&amp;elid=' . urlencode($_GET['elid']);
}
if (isset($_GET['plid'])) {
    echo '&amp;plid=' . urlencode($_GET['plid']);
}
echo '">Загрузить</a><br />' . "\n";

echo '&#187; <a href="?func=file.import';
if (isset($_GET['elid'])) {
    echo '&amp;elid=' . urlencode($_GET['elid']);
}
if (isset($_GET['plid'])) {
    echo '&amp;plid=' . urlencode($_GET['plid']);
}
echo '">Импортировать</a><br />' . "\n";

echo '</div>' . "\n";
echo '<div class="menu"><img src="images/sb-menu.png" alt="." /><a href="?func=menu">Вернуться в меню</a>' . "\n</div>";

?>