<?php

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

File:         edit_folder.php
Description:  Administrative module to edit folder contents

*/

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

    $cFile = new cSystemFile;
    $errorString = "";

    // Check privileges
    check_login();
    if (user_is_customer() == true)
    {
        log_out();
        return;
    }
    if (user_is_admin() == false)
    {
        header('location: ' . $ADMIN_MAIN_SITE);
        return;
    }

    // Check folderExtension
    $folderExtension = "";
    if (isset ($_POST['folder_extension']) == true)
        $folderExtension = $_POST['folder_extension'];

    // Check folderName
    $folderName = "../";
    $folderWritable = false;
    if (isset ($_POST['folder_name']) == true)
        $folderName = $_POST['folder_name'];
    else if (isset ($_GET['foldername']) == true)
        $folderName = $_GET['foldername'];
    if (file_exists($folderName) == false)
        $errorString = get_translation('admin_foldedt_errorload') . ': ' . $folderName;
    if ($errorString == "")
        $folderWritable = is_writable($folderName);

    // Check folderSort
    $folderSort = 1;
    if (isset($_POST['folder_sort']) == true)
        $folderSort = $_POST['folder_sort'];

    // Back to main?
    if (isset ($_POST['folder_OK']))
    {
        header ("Location: " . $ADMIN_MAIN_SITE);
        return;
    }
    
    // File upload?
    if (isset ($_POST['folder_Upload']) && isset($_FILES['folder_UploadFile']['name']) && $_FILES['folder_UploadFile']['name'] != "")
    {
        $destName = $folderName . $_FILES['folder_UploadFile']['name'];
        if (@ copy ($_FILES['folder_UploadFile']['tmp_name'], $destName) == false)
        {
            $errorString = get_translation('admin_foldedt_errorupload') . ': ' . $_FILES['folder_UploadFile']['name'] . '!';
            $b_ErrorOccured = true;
        }
        else
        {
            log_message ('User ' . get_user_name() . ' uploaded file ' . $destName);
        }
    }
    
    // File delete?
    if (isset($_POST['folder_Delete']) == true)
    {
        if (isset($_POST['folderfile_name']) == true && file_exists($_POST['folderfile_name']) == true)
        {
            if ($cFile->DeleteFile($_POST['folderfile_name']) == false)
                $errorString = get_translation('admin_foldedt_errordelete') . ": " . $_POST['folderfile_name'];
            else
                log_message ('User ' . get_user_name() . ' deleted file ' . $_POST['folderfile_name']);
        }
    }

    // File edit?
    if (isset($_POST['folder_Edit']) == true)
    {
        if (isset($_POST['folderfile_name']) == true && file_exists($_POST['folderfile_name']) == true)
        {
            header ("location: " . $ADMIN_EDITFILE_SITE . "?filename=" . $_POST['folderfile_name'] . "&backto=" . $ADMIN_EDITFOLDER_SITE . "?foldername=" . $folderName);
            return;
        }
    }

    // File rename?
    if (isset($_POST['folder_Rename']) == true)
    {
        if (isset($_POST['folderfile_name']) == true && file_exists($_POST['folderfile_name']) == true &&
            isset($_POST['folderfile_newname']) == true && file_exists($_POST['folderfile_newname']) == false)
        {
            if ($cFile->RenameFile($_POST['folderfile_name'], $_POST['folderfile_newname']) == false)
                $errorString = get_translation('admin_foldedt_errorrename') . ": " . $_POST['folderfile_name'] . " -> " . $_POST['folderfile_newname'];
            else
                log_message ('User ' . get_user_name() . ' renamed file ' . $_POST['folderfile_name'] . ' to ' . $_POST['folderfile_newname']);
        }
    }

    // Delete folder?
    if (isset($_POST['folder_DeleteFolder']) == true)
    {
        if (isset($_POST['folderfile_name']) == true && file_exists($_POST['folderfile_name']) == true)
        {
            if ($cFile->DeleteFolder($_POST['folderfile_name']) == true)
                log_message ('User ' . get_user_name() .': Deleted folder ' . $_POST['folderfile_name']);
            else
                $errorString = get_translation('admin_foldedt_errordelete') . ": " . $_POST['folderfile_name'];
        }
    }

    // Clean folder?
    if (isset($_POST['folder_Clean']) == true)
    {
        if (isset($_POST['folderfile_name']) == true && file_exists($_POST['folderfile_name']) == true)
        {
            if ($cFile->CleanFolderContent($_POST['folderfile_name'], $folderExtension) == true)
                log_message ('User ' . get_user_name() . ' cleaned folder ' . $_POST['folderfile_name']);
            else
                $errorString = get_translation('admin_foldedt_errorcleanfolder') . ": " . $_POST['folderfile_name'];
        }
    }

    // Create folder?
    if (isset($_POST['folder_CreateFolder']) == true)
    {
        if (isset($_POST['folder_CreateFolderName']) == true && $_POST['folder_CreateFolderName'] != "")
        {
            if ($cFile->CreateFolder($folderName . $_POST['folder_CreateFolderName']) == true)
                log_message ('User ' . get_user_name() .': Created folder ' . $folderName . $_POST['folder_CreateFolderName']);
            else
                $errorString = get_translation('admin_foldedt_errorcreatefolder') . ": " . $folderName . $_POST['folder_CreateFolderName'];
        }
    }

    // Display folder content
    print_admin_header(get_translation('admin_foldedt_head') . ':<br>' . $folderName, true, get_translation('admin_login_logout'), $ADMIN_HELP_FILE . '#n1-16', true, get_translation('admin_backtomain'), "", "", false);
    echo_r('<script type="text/javascript" language="JavaScript">');
    echo_r('function DeleteFile(file_name)');
    echo_r('{');
    echo_r('  if (confirm(\'' . get_translation('admin_foldedt_suretodeletefile') . ': \' + file_name + \'?\') == true)');
    echo_r('  {');
    echo_r('     document.getElementById(\'folderfile_Name\').value=file_name;');
    echo_r('     document.getElementById(\'folderFormDeleteBtn\').click();');
    echo_r('  }');
    echo_r('}');
    echo_r('function EditFile(file_name)');
    echo_r('{');
    echo_r('  document.getElementById(\'folderfile_Name\').value=file_name;');
    echo_r('  document.getElementById(\'folderFormEditBtn\').click();');
    echo_r('}');
    echo_r('function RenameFile(file_name)');
    echo_r('{');
    echo_r('  file_newname = prompt(\'' . get_translation('admin_foldedt_suretorename') . ' (\' + file_name + \'):\', file_name);');
    echo_r('  if (file_newname != \'\' && file_newname != null)');
    echo_r('  {');
    echo_r('    document.getElementById(\'folderfile_Name\').value=file_name;');
    echo_r('    document.getElementById(\'folderfile_NewName\').value=file_newname;');
    echo_r('    document.getElementById(\'folderFormRenameBtn\').click();');
    echo_r('  }');
    echo_r('}');
    echo_r('function DeleteFolder(folder_name)');
    echo_r('{');
    echo_r('  if (confirm(\'' . get_translation('admin_foldedt_suretodeletefolder') . ': \' + folder_name + \'?\') == true)');
    echo_r('  {');
    echo_r('     document.getElementById(\'folderfile_Name\').value=folder_name;');
    echo_r('     document.getElementById(\'folderFormDeleteFolderBtn\').click();');
    echo_r('  }');
    echo_r('}');
    echo_r('function CleanFolder(folder_name)');
    echo_r('{');
    echo_r('  if (confirm(\'' . get_translation('admin_foldedt_suretocleanfolder') . ': \' + folder_name + \'?\') == true)');
    echo_r('  {');
    echo_r('     document.getElementById(\'folderfile_Name\').value=folder_name;');
    echo_r('     document.getElementById(\'folderFormCleanBtn\').click();');
    echo_r('  }');
    echo_r('}');
    echo_r('</script>');
    finish_admin_header(get_translation('admin_foldedt_head') . ':<br>' . $folderName, true, get_translation('admin_login_logout'), $ADMIN_HELP_FILE . '#n1-15', true, get_translation('admin_backtomain'));

    if ($errorString != "")
        print_error_message($errorString);

    echo_r('<form action="' . $_SERVER['PHP_SELF'] . '" method="post" enctype="multipart/form-data">');
    echo_r('<table border="0" width="100%">');
    echo_r('  <tr>');
    echo_r('    <td>' . get_translation('admin_foldedt_foldercontent') . ' ' . '<input type="text" size="70" name="folder_name" value="' . $folderName . '"> <input type="submit" name="folder_Refresh" value="' . get_translation('admin_foldedt_refresh') . '" class="bottom_btn">:</td>');
    echo_r('  </tr>');
    echo_r('  <tr>');
    echo_r('    <td>' . get_translation('admin_foldedt_sortby') . ': ');
    echo_r('  <select name="folder_sort" onChange="submit();">');
    echo  ('  <option value="1"');
    if ($folderSort == 1)
        echo (' selected');
    echo_r('>' . get_translation('admin_foldedt_nameasc') . '</option>');
    echo  ('  <option value="-1"');
    if ($folderSort == -1)
        echo (' selected');
    echo_r('>' . get_translation('admin_foldedt_namedesc') . '</option>');
    echo  ('  <option value="2"');
    if ($folderSort == 2)
        echo (' selected');
    echo_r('>' . get_translation('admin_foldedt_sizeasc') . '</option>');
    echo  ('  <option value="-2"');
    if ($folderSort == -2)
        echo (' selected');
    echo_r('>' . get_translation('admin_foldedt_sizedesc') . '</option>');
    echo  ('  <option value="3"');
    if ($folderSort == 3)
        echo (' selected');
    echo_r('>' . get_translation('admin_foldedt_dateasc') . '</option>');
    echo  ('  <option value="-3"');
    if ($folderSort == -3)
        echo (' selected');
    echo_r('>' . get_translation('admin_foldedt_datedesc') . '</option>');
    echo_r ('  </select>&nbsp;' . get_translation('admin_foldedt_extension') . ': <input type="text" size="10" name="folder_extension" value="' . $folderExtension . '">');
    echo_r('    </td>');
    echo_r('  </tr>');
    echo_r('  <tr>');
    echo_r('    <td><div class="folder_content_div">');
    $files = $cFile->GetFolderContentTable($folderName, $folderExtension, "");
    $folders = $cFile->GetFolderDirsTable($folderName, "", "");
    $totalKB = 0;
    $oldDir = getcwd();
    echo_r('      <table border="0" width="100%">');
    // Backlink
    if ($folderName != "../")
    {
        echo_r('      <tr style="height: 25px">');
        echo_r('        <td width="35%"><a href="' . $_SERVER['PHP_SELF'] . '?foldername=' . $cFile->GetPrevFolder($folderName) . '"><img src="' . $FOLDERUP_IMAGE . '" alt=""><b>..</b></a></td>');
        echo_r('        <td width="10%" style="text-align: right"></td>');
        echo_r('        <td width="22%" style="text-align: right"></td>');
        echo_r('        <td width="33%" style="text-align: right"></td>');
        echo_r('      </tr>');
    }
    // Folders
    if ($folderSort > 0)
        $folders = sort_table($folders, $folderSort - 1, true);
    else
    {
        $folders = sort_table($folders, abs($folderSort + 1), false);
    }
    for ($i = 0; $i < count($folders); $i ++)
    {
        echo_r('      <tr style="height: 25px; vertical-align: center">');
        echo_r('        <td width="35%"><a href="' . $_SERVER['PHP_SELF'] . '?foldername=' . $folderName . $folders[$i][0] . '/"><img src="' . $FOLDER_IMAGE . '" alt=""><b>' . $folders[$i][0] . '</b></a></td>');
        echo_r('        <td width="10%" style="text-align: right"></td>');
        echo_r('        <td width="22%" style="text-align: right">' . date('Y-m-d H:i:s', $folders[$i][2]) . '</td>');
        echo('        <td width="33%" style="text-align: right">');
        // Rename, delete button...
        if ($folderWritable == true)
            echo ('<input type="button" class="file_btn" value="' . get_translation('admin_rename') . '" onClick="RenameFile(\'' . $folderName . $folders[$i][0].'\')"> <input type="button" class="file_btn" value="' . get_translation('admin_delete') . '" onClick="DeleteFolder(\'' . $folderName . $folders[$i][0].'\')">');
        echo_r('</td>');
        echo_r('      </tr>');
    }
    // Files
    if ($folderSort > 0)
        $files = sort_table($files, $folderSort - 1, true);
    else
        $files = sort_table($files, abs($folderSort + 1), false);
    for ($i = 0; $i < count($files); $i ++)
    {
        echo_r('      <tr style="height: 25px">');
        $currentKB = $files[$i][1] / 1024;
        $totalKB += $currentKB;
        $fileIcon = $FOLDERFILE_IMAGE;
        if (check_image_extension($files[$i][0]) == true)
            $fileIcon = $FOLDERIMAGE_IMAGE;
        echo_r('        <td width="35%"><a href="' . $folderName . $files[$i][0] . '"><img src="' . $fileIcon . '" alt="">' . $files[$i][0] . '</a></td>');
        echo_r('        <td width="10%" style="text-align: right">' . sprintf("%01.2f", $currentKB) . ' KB</td>');
        echo_r('        <td width="22%" style="text-align: right">' . date('Y-m-d H:i:s', $files[$i][2]) . '</td>');
        echo('        <td width="33%" style="text-align: right">');
        // Edit, rename, delete buttons...
        if ($folderWritable == true)
            echo ('<input type="button" class="file_btn" value="' . get_translation('admin_rename') . '" onClick="RenameFile(\'' . $folderName . $files[$i][0].'\')"> <input type="button" class="file_btn" value="' . get_translation('admin_edit') . '" onClick="EditFile(\'' . $folderName . $files[$i][0].'\')"> <input type="button" class="file_btn" value="' . get_translation('admin_delete') . '" onClick="DeleteFile(\'' . $folderName . $files[$i][0].'\')">');
        echo_r('</td>');
        echo_r('      </tr>');
    }
    echo_r('      </table>');
    echo_r('    </div></td>');
    echo_r('  </tr>');
    echo_r('  <tr>');
    echo_r('    <td>' . count($files) . ' ' . get_translation('admin_foldedt_files') . ' (' . sprintf("%01.2f", $totalKB) . ' KB), ' . count($folders) . ' ' . get_translation('admin_foldedt_folders') . '.</td>');
    echo_r('  </tr>');
    // Clean / upload / create folder buttons
    if ($folderWritable == true)
    {
       echo_r('  <tr>');
       echo_r('    <td>' . get_translation('admin_foldedt_uploadfile') . ': <input type="file" size="60" name="folder_UploadFile">&nbsp;<input type="submit" class="bottom_btn" name="folder_Upload" value="' . get_translation('admin_upload') . '"></td>');
       echo_r('  </tr>');
       echo_r('  <tr>');
       echo_r('    <td>' . get_translation('admin_foldedt_createfolder') . ': <input type="text" size="40" name="folder_CreateFolderName"> <input type="submit" class="bottom_btn" name="folder_CreateFolder" value="' . get_translation('admin_create') . '"></td>');
       echo_r('  </tr>');
       echo_r('  <tr>');
       echo_r('    <td>' . get_translation('admin_foldedt_cleanfolder') . ': <input type="button" class="bottom_btn" name="folder_CleanPre" value="' . get_translation('admin_clean') . '" onClick="CleanFolder(\'' . $folderName . '\')"></td>');
       echo_r('  </tr>');
    }
    echo_r('  </table>');

    print_bottom_links("folder_OK", get_translation('admin_ok'),
                       "", '', false);
    echo_r('</form>');

    echo ('<form id="folderForm" method="post" action="' . $_SERVER['PHP_SELF'] . '">');
    echo ('<input type="hidden" name="folder_name" value="' . $folderName . '">');
    echo ('<input type="hidden" name="folder_extension" value="' . $folderExtension . '">');
    echo ('<input type="hidden" id="folderfile_Name" name="folderfile_name" value="">');
    echo ('<input type="hidden" id="folderfile_NewName" name="folderfile_newname" value="">');
    echo ('<input type="submit" name="folder_Delete" id="folderFormDeleteBtn" style="visibility: hidden; width: 5px; height: 2px">');
    echo ('<input type="submit" name="folder_Edit" id="folderFormEditBtn" style="visibility: hidden; width: 5px; height: 2px">');
    echo ('<input type="submit" name="folder_Rename" id="folderFormRenameBtn" style="visibility: hidden; width: 5px; height: 2px">');
    echo ('<input type="submit" name="folder_DeleteFolder" id="folderFormDeleteFolderBtn" style="visibility: hidden; width: 5px; height: 2px">');
    echo ('<input type="submit" name="folder_Clean" id="folderFormCleanBtn" style="visibility: hidden; width: 5px; height: 2px">');
    echo_r ('</form>');

    print_admin_footer();

?>