<?php

if (isset($_POST['submit'])) {
    if (isset($_GET['plid']) and isset($_GET['elid'])) {
        $content = api_query('https://' . $server . '/manager/ispmgr?failed=' . urlencode($_POST['failed']) . '&cr=' . urlencode($_POST['cr']) . '&func=file.edit&fdata=' . urlencode($_POST['fdata']) . '&encoding=' . urlencode($_POST['encoding']) . '&elid=' . urlencode($_GET['file']) . '&plid=' . urlencode($_GET['plid'] . '/' . $_GET['elid']) . '&sok=ok&out=xml&authinfo=' . urlencode($_SESSION['username']) . ':' . urlencode($_SESSION['password']));
    } elseif (isset($_GET['elid'])) {
        $content = api_query('https://' . $server . '/manager/ispmgr?failed=' . urlencode($_POST['failed']) . '&cr=' . urlencode($_POST['cr']) . '&func=file.edit&fdata=' . urlencode($_POST['fdata']) . '&encoding=' . urlencode($_POST['encoding']) . '&elid=' . urlencode($_GET['file']) . '&plid=' . urlencode($_GET['elid']) . '&sok=ok&out=xml&authinfo=' . urlencode($_SESSION['username']) . ':' . urlencode($_SESSION['password']));
    } else {
        $content = api_query('https://' . $server . '/manager/ispmgr?failed=' . urlencode($_POST['failed']) . '&cr=' . urlencode($_POST['cr']) . '&func=file.edit&fdata=' . urlencode($_POST['fdata']) . '&encoding=' . urlencode($_POST['encoding']) . '&elid=' . urlencode($_GET['file']) . '&sok=ok&out=xml&authinfo=' . urlencode($_SESSION['username']) . ':' . urlencode($_SESSION['password']));
    }
    echo 'Сохранение успешно!<br />' . "\n";
}

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

$parse_xml = simplexml_load_string($content);

echo '<div class="post"><b>Файл:</b> ' . htmlentities($_GET['file'], ENT_QUOTES, '437') . '<br />' . "\n";
if (isset($parse_xml->error)) {
    echo '<b>Ошибка:</b> ' . htmlentities((string)$parse_xml->error, ENT_QUOTES, 'UTF-8') . "\n";
} else {
    echo '<b>Кодировка:</b> ' . htmlentities((string)$parse_xml->encoding, ENT_QUOTES, 'UTF-8');

    if ((string)$parse_xml->encoding == 'UTF-8') {
        echo '-&gt;<a href="?func=file.edit&amp;file=' . urlencode($_GET['file']) . '&amp;encoding=CP1251';
        if (isset($_GET['elid'])) {
            echo '&amp;elid=' . urlencode($_GET['elid']);
        }
        if (isset($_GET['plid'])) {
            echo '&amp;plid=' . urlencode($_GET['plid']);
        }
        echo '">CP1251</a>';
    } elseif ((string)$parse_xml->encoding == 'CP1251') {
        echo '-&gt;<a href="?func=file.edit&amp;file=' . urlencode($_GET['file']) . '&amp;encoding=UTF-8';
        if (isset($_GET['elid'])) {
            echo '&amp;elid=' . urlencode($_GET['elid']);
        }
        if (isset($_GET['plid'])) {
            echo '&amp;plid=' . urlencode($_GET['plid']);
        }
        echo '">UTF-8</a>';
    }

    echo '<br />' . "\n";
    echo '<form action="?func=file.edit&amp;file=' . urlencode($_GET['file']);
    if (isset($_GET['elid'])) {
        echo '&amp;elid=' . urlencode($_GET['elid']);
    }
    if (isset($_GET['plid'])) {
        echo '&amp;plid=' . urlencode($_GET['plid']);
    }
    echo '" method="post">' . "\n";
    echo '<textarea style="width:98%;font-size:9pt;padding:5px" cols="25" rows="10" name="fdata">' . htmlentities((string)$parse_xml->fdata, ENT_QUOTES, 'UTF-8') . '</textarea><br />' . "\n";
    echo '<input name="encoding" type="hidden" value="' . htmlentities((string)$parse_xml->encoding, ENT_QUOTES, 'UTF-8') . '" />' . "\n";
    echo '<input name="cr" type="hidden" value="' . htmlentities((string)$parse_xml->cr, ENT_QUOTES, 'UTF-8') . '" />' . "\n";
    echo '<input name="failed" type="hidden" value="' . htmlentities((string)$parse_xml->failed, ENT_QUOTES, 'UTF-8') . '" />' . "\n";
    echo '<input name="submit" type="submit" value="Сохранить" />' . "\n";
    echo '</form>' . "\n</div>";
}

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

?>