<?php

/*  Alternate WebKit
(C) 2012-2014 by Alternate Tools
-----------------------------------------------------

File:         modify_pagefile.php
Description:  Administrative module to unlock/lock/delete a pagefile

*/

    include "../modules/class_pagefile.php";
    include "../modules/mod_admin.php";

    $bPageFileLoadFailed = false;
    $pgfId = "";
    $errorString = "";
    
    // Check privileges
    check_login();
    if (user_is_customer() == true)
    {
        log_out();
        return;
    }
    if (user_is_reporter() == true)
    {
        header("location: " . $ADMIN_MAIN_SITE);
        return;
    }

    // Get action
    $pgfModAction = "unlock";
    if (isset ($_POST['pgf_action']) == true)
    {
        if ($_POST['pgf_action'] == "lock")
            $pgfModAction = "lock";
        else if ($_POST['pgf_action'] == "delete")
            $pgfModAction = "delete";
    }
    else if (isset ($_GET['action']) == true)
    {
        if ($_GET['action'] == "lock")
            $pgfModAction = "lock";
        else if ($_GET['action'] == "delete")
            $pgfModAction = "delete";
    }

    $pgfPageFile = new cPageFile;
    $pgfPageFile->SetFolder($PAGEFILES_FOLDER);

    if (isset ($_POST['pgf_id']) == true)
        $pgfId = $_POST['pgf_id'];
    else if (isset ($_GET['id']) == true)
        $pgfId = $_GET['id'];

    if ($pgfPageFile->LoadPageFileFromId($pgfId) == false)
    {
        print_error_message(get_translation('admin_pagefilmod_errorload') . ', ID: ' . $pgfId . '!!');
        $bPageFileLoadFailed = true;
    }

    // Cancel modification?
    if (isset ($_POST['pgf_Cancel']))
    {
        header('location: ' . $ADMIN_LIST_PAGEFILES_SITE);
        return;
    }
    // Modify download?
    if (isset ($_POST['pgf_DoAction']) && $bPageFileLoadFailed == false)
    {
        if ($pgfModAction == "lock")
        {
            if ($pgfPageFile->SetLocked(true) == false)
                $errorString = get_translation('admin_pagefilmod_errorlock') . ', ID: ' . $pgfId . '!!';
            else
            {
                log_message ('User ' . get_user_name() . ' locked pagefile ' . $pgfPageFile->GetId());
                header('location: ' . $ADMIN_LIST_PAGEFILES_SITE);
                return;
            }
        }
        else if ($pgfModAction == "delete")
        {
            $bDone = $pgfPageFile->DeletePageFileFinal();  // Page files can be unlinked
            if ($bDone == false)
                $errorString = get_translation('admin_pagefilmod_errordelete') . ', ID: ' . $pgfId . '!!';
            else
            {
                log_message ('User ' . get_user_name() . ' deleted pagefile ' . $pgfPageFile->GetId());
                header('location: ' . $ADMIN_LIST_PAGEFILES_SITE);
                return;
            }
        }
        else
        {
            if ($pgfPageFile->SetLocked(false) == false)
                $errorString = get_translation('admin_pagefilmod_errorunlock') . ', ID: ' . $pgfId . '!!';
            else
            {
                log_message ('User ' . get_user_name() . ' unlocked pagefile ' . $pgfPageFile->GetId());
                header('location: ' . $ADMIN_LIST_PAGEFILES_SITE);
                return;
            }
        }
    }

    // Display
    $titleString = get_translation('admin_pagefilmod_unlockhead');
    if ($pgfModAction == "lock")
        $titleString = get_translation('admin_pagefilmod_lockhead');
    else if ($pgfModAction == "delete")
        $titleString = get_translation('admin_pagefilmod_deletehead');
    print_admin_header($titleString, true, get_translation('admin_login_logout'), "", true, get_translation('admin_backtomain'),
                       $ADMIN_LIST_PAGEFILES_SITE, get_translation('admin_overview'));

    if ($errorString != "")
        print_error_message($errorString);
        
    if ($pgfModAction == "lock")
        echo_r('<p>' . get_translation('admin_pagefilmod_questlock') . ':</p><br>');
    else if ($pgfModAction == "delete")
        echo_r('<p>' . get_translation('admin_pagefilmod_questdelete') . ':</p><br>');
    else
        echo_r('<p>' . get_translation('admin_pagefilmod_questunlock') . ':</p><br>');
    echo_r('<table width="100%" border="0">');
    echo_r('  <tr>');
    echo_r('    <td width="20%">');
    echo_r('       <a href="' . $pgfPageFile->GetFile() . '" target="_blank"><img src="' . $FILE_IMAGE . '"></a>');
    echo_r('    </td>');
    echo_r('    <td width="80%">');
    echo_r('       <b>' . get_translation('admin_pagefil_title') . ': ' . $pgfPageFile->GetTitle() . '</b><br>');
    echo_r('       ' . get_translation('admin_pagefil_orginname') . ': ' . $pgfPageFile->GetOriginName() . '<br>');
    echo_r('       ' . get_translation('admin_pagefil_id') . ': ' . $pgfPageFile->GetId() . '<br>');
    echo_r('       ' . get_translation('admin_pagefil_created') . ': ' . $pgfPageFile->GetDate() . '<br>');
    echo_r('    </td>');
    echo_r('  </tr>');
    echo_r('</table>');
    
    echo_r('<form method="post" action="">');
    print_bottom_links("pgf_DoAction", get_translation('admin_yes'),
                       "pgf_Cancel", get_translation('admin_cancel'), false);
    echo_r('  <input type="hidden" value="' .  $pgfId . '" name="pgf_id">  ');
    echo_r('  <input type="hidden" value="' .  $pgfModAction . '" name="pgf_action">  ');
    echo_r('</form>');

    print_admin_footer();
?>