<?php

/* setup includes */
require_once 'includes/master.inc.php';
/* require login */
$Auth->requireUser('login.php');
/* load the fileFolder */
if (isset($_REQUEST['u'])) {
    $fileFolder = fileFolder::loadById($_REQUEST['u']);
    if (!$fileFolder) {
        // failed lookup of the fileFolder
        redirect('http://' . _CONFIG_SITE_FULL_URL . '/account_folders.' . SITE_CONFIG_PAGE_EXTENSION);
    }
    // check current user has permission to edit the fileFolder
    if ($fileFolder->userId != $Auth->id) {
        redirect('http://' . _CONFIG_SITE_FULL_URL . '/account_folders.' . SITE_CONFIG_PAGE_EXTENSION);
    }
} else {
    redirect('http://' . _CONFIG_SITE_FULL_URL . '/account_folders.' . SITE_CONFIG_PAGE_EXTENSION);
}
/* setup page */
define("PAGE_NAME", t("edit_page_name", "Edit"));
define("PAGE_DESCRIPTION", t("edit_meta_description", "Edit existing item"));
define("PAGE_KEYWORDS", t("edit_meta_keywords", "edit, existing, item"));
/* handle submission */
if ((int) $_REQUEST['submitme']) {
    // validation
    $folderName = trim($_REQUEST['folderName']);
    $isPublic = (int) trim($_REQUEST['isPublic']);
    $accessPassword = trim($_REQUEST['accessPassword']);
    if (!strlen($folderName)) {
        $fileFolder = fileFolder::loadById($_REQUEST['d']);
        if ($fileFolder) {
            /* check user id */
            if ($fileFolder->userId == $Auth->id) {
                $fileFolder->removeByUser();
            }
        }
    }
}
/* setup page */
define("PAGE_NAME", t("account_folder_name", "File Folders"));
define("PAGE_DESCRIPTION", t("account_folder_meta_description", "Your File Folders"));
define("PAGE_KEYWORDS", t("account_folder_meta_keywords", "file, folders, home, file, your, interface, upload, download, site"));
require_once '_header.php';
// load all fileFolder for this account
$fileFolders = fileFolder::loadAllByAccount($Auth->id);
?>

<script>
    $(document).ready(function() {
        $('#fileData').dataTable( {
            "sPaginationType": "full_numbers",
            "aaSorting": [[ 1, "asc" ]],
            "aoColumns": [
                { "asSorting": [ "asc", "desc" ] },
                { "asSorting": [ "asc", "desc" ] },
                { "asSorting": [ "asc", "desc" ] },
                { "asSorting": [ "asc", "desc" ] },
                { "asSorting": [ "asc", "desc" ] },
                null
            ]
        $rs = $db->query('UPDATE file SET originalFilename = :originalFilename, folderId = :folderId WHERE id = :id', array('originalFilename' => $filename . '.' . $file->extension, 'folderId' => $folder, 'id' => $file->id));
        if ($rs) {
            // clean stats if needed
            if ($reset_stats == 1) {
                $db->query('UPDATE file SET visits = 0 WHERE id = :id', array('id' => $file->id));
                $db->query("DELETE FROM stats WHERE page_title = '" . $file->id . "'");
            }
            // redirect
            redirect(WEB_ROOT . "/account_home." . SITE_CONFIG_PAGE_EXTENSION);
        } else {
            setError(t("problem_updating_item", "There was a problem updating the item, please try again later."));
        }
    }
}
// load folders
$folders = fileFolder::getFoldersByUser($Auth->id);
require_once '_header.php';
?>

<div class="contentPageWrapper">

<?php 
if (isErrors()) {
    echo outputErrors();
}
?>

    <!-- main section -->
    <div class="pageSectionMainFull ui-corner-all">
        <div class="pageSectionMainInternal">
            <div id="pageHeader">
<?php

/* setup includes */
require_once 'includes/master.inc.php';
// initial checks
$folderId = $_REQUEST['f'];
$folderExp = explode('~', $folderId);
$folderId = (int) $folderExp[0];
// make sure it's a public folder or the owner is logged in
if ($folderId) {
    $fileFolder = fileFolder::loadById($folderId);
    if (!$fileFolder) {
        // failed lookup of the fileFolder
        redirect('http://' . _CONFIG_SITE_FULL_URL . '/index.' . SITE_CONFIG_PAGE_EXTENSION);
    }
    // check the folder is public
    if ($fileFolder->isPublic == 0 && $fileFolder->userId != $Auth->id) {
        redirect('http://' . _CONFIG_SITE_FULL_URL . '/index.' . SITE_CONFIG_PAGE_EXTENSION);
    }
} else {
    redirect('http://' . _CONFIG_SITE_FULL_URL . '/account_folders.' . SITE_CONFIG_PAGE_EXTENSION);
}
// check for password if we need it
$showFolder = true;
if (strlen($fileFolder->accessPassword) > 0) {
    /* check folder password */
    if ((int) $_REQUEST['passwordSubmit']) {
        // check password
        $folderPassword = trim($_REQUEST['folderPassword']);
        if (!strlen($folderPassword)) {
            setError(t("please_enter_the_folder_password", "Please enter the folder password"));