<?php

if (isset($_GET['plid']) and isset($_GET['elid'])) {
    $content = api_query('https://' . $server . '/manager/ispmgr?func=diskusage&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=diskusage&elid=' . urlencode($_GET['elid']) . '&out=xml&authinfo=' . urlencode($_SESSION['username']) . ':' . urlencode($_SESSION['password']));
} else {
    $content = api_query('https://' . $server . '/manager/ispmgr?func=diskusage&out=xml&authinfo=' . urlencode($_SESSION['username']) . ':' . urlencode($_SESSION['password']));
}
$parse_xml = simplexml_load_string($content);

echo '<div class="menu"><b>Использование диска</b>' . "\n";
echo '</div>' . "\n";

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 '<div class="post"><a href="?func=diskusage';
    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></div>' . "\n";
} elseif (isset($_GET['elid'])) {
    echo '<a href="?func=diskusage"><img src="images/back.gif" alt=".." /></a><br />' . "\n";
}

$list_dir = array();
$list_file = array();

foreach($parse_xml->elem as $var) {
    $dname_attributes = $var->name->attributes();
    if ($dname_attributes['xprop'] == '0') {
        $list_dir["'.(string)$var->name.'"] = array((string)$var->name, '0', (string)$var->size);
    } elseif ($dname_attributes['xprop'] == '100') {
        $list_file["'.(string)$var->name.'"] = array((string)$var->name, '100', (string)$var->size);
    }
}

ksort($list_dir);
ksort($list_file);

$list_all = array_merge($list_dir, $list_file);
foreach($list_all as $var) {
    $list_all_num[] = $var;
}
unset($list_all);

if (isset($list_all_num)) {
echo '<div class="post">';
    $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;
    }

    for($i = $rows_min; $i <= $rows_max; $i++) {
        if ($list_all_num[$i][1] == '0') {
            echo '<img src="images/p-file-0.png" alt="d" /> <a href="?func=diskusage&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((string)$list_all_num[$i][0], ENT_QUOTES, 'UTF-8') . '</a> ' . $list_all_num[$i][2] . 'Mb<br />' . "\n";
        } elseif ($list_all_num[$i][1] == '100') {
            echo '<img src="images/p-file-100.png" alt="f" /> ' . htmlentities($list_all_num[$i][0], ENT_QUOTES, 'UTF-8') . ' ' . $list_all_num[$i][2] . 'Mb<br />' . "\n";
        }
    }
echo '</div>';
    } else {
    echo '<div class="post"><br />' . "\n";
    echo 'Папка пуста<br />' . "\n</div>";
}

// --------------------------- Постраничная навигация ------------------------- //
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=diskusage&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=diskusage&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=diskusage&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=diskusage&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=diskusage&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="menu">' . "\n";
echo '<img src="images/sb-menu.png" alt="." /><a href="?func=menu">Вернуться в меню</a>' . "\n</div>";

?>