Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
20 kB
1
Indexable
Never
<?php



$jsonUserName = "userName.json";
$userDirectory = "userDirectory.json";
$userName = json_decode(file_get_contents($jsonUserName), true);
$UserDirectory = json_decode(file_get_contents($userDirectory), true);
$boolStatusUpload = false;
$activeJson = json_decode(file_get_contents("active.json"), true);
$target_file = '';



if (!empty($activeJson)) {
    $activeUser = $activeJson;
    $boolStatusUpload = true;
    if (isset($UserDirectory["$activeUser"]))
        $target_file = $_SERVER['DOCUMENT_ROOT'] . "/maktab/m70_hw5_mokhtari/uploads/" . $UserDirectory["$activeUser"];
    else
        $target_file = $_SERVER['DOCUMENT_ROOT'] . "/maktab/m70_hw5_mokhtari/uploads/$activeUser";
}





function show($dir)
{
    $size = 0;
    if (is_file($dir))

        $size = filesize($dir);
    else
        foreach (glob(rtrim($dir, '/') . '/*') as $each) {
            if (is_file($each))
                $size += filesize($each);
            if (is_dir($each))
                show($each);
        }
    $sizes = array("TB", "GB", "MB", "KB", "B");
    $total = count($sizes);
    while ($total-- and $size > 1024) {
        $size /= 1024;
    }
    return round($size) . " " . $sizes[$total];
}





function remove($dir, $mode)
{
    if ($mode == "delete") {
        if (is_dir($dir)) {
            foreach (glob($dir . '/*') as $FileD) {
                if (is_dir($FileD) )
                    remove($FileD, "delete");


                else {
                    @unlink($FileD);
                }
            }
            @rmdir($dir);
        } else
            @unlink($dir);


        return true;
    } elseif ($mode == "move") {
        global  $target_file;
        foreach (glob($dir . '/*') as $FileD) { ?>
            <h6 class="text-white"> <?= $FileD ?></h6>
<?php
            rename($FileD, $target_file);
        }


        return true;
    }
}

function edit($dirE, $nameE)
{
    $nameNew = '';
    $nameOld = $_SERVER['DOCUMENT_ROOT'] . "/maktab/m70_hw5_mokhtari/" . $dirE;
    $doPosition = strrpos($nameOld, "/");
    for ($i = 0; $i <= $doPosition; $i++) {
        $nameNew .= $nameOld[$i];
    }
    $nameNew .= $nameE;

    if (file_exists($nameNew)) {
        echo '<div class="alert alert-danger" role="alert">
 your name is set please set another name"
</div>';
        return;
    } else {
        rename($nameOld, $nameNew);
        return true;
    }
}


function make($dir, $name)
{
    $mk =  $dir.'/' . $name;
    if (!file_exists($mk))
        mkdir("$mk");
    else
        echo '<div class="alert alert-danger" role="alert">
 your folder name is set please set another name"
</div>';
}



?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>DRIVE</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous" />
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.8.1/font/bootstrap-icons.css">
    <style>
        a:hover {
            color: red;
            text-decoration: none;
        }

        a {
            color: orange;
            text-decoration: none;
        }
    </style>
</head>

<body class="bg-image" style="
        background-image: url('back.png');
        height: 60vh;
        background-size: cover;
        width: 100%;
        position: relative;
        ">
    <nav class="navbar navbar-expand-lg  p-3 navbar-dark bg-dark navbar-fixed-top">
        <div class="container-fluid ">
            <span class="navbar-brand ">Drive</span>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse row m-2" id="navbarSupportedContent">
                <ul class="navbar-nav me-auto mb-2 mb-lg-0
                       justify-content-end">
                    <li class="nav-item col-lg-1">
                        <a class="nav-link active " aria-current="page" href="DRIVE.php">HOME</a>
                    </li>
                    <li class="nav-item col-lg-1">
                        <a class="nav-link active " aria-current="page" href="?showFile">show file</a>
                    </li>
                    <li class="nav-item col-lg-1">
                        <a class="nav-link active " aria-current="page" href="?upload">upload</a>
                    </li>


                    <li class="nav-item col-lg-1">
                        <a class="nav-link active  " name="signIn" href="?signIn"> SignIn </a>
                    </li>
                    <li class="nav-item col-lg-1">
                        <a class="nav-link active " name="login" href="?login"> login </a>
                    </li>
                    <li class="nav-item col-lg-1">
                        <a class="nav-link active " name="login" href="?exit"> Exit </a>
                    </li>
                </ul>

            </div>
        </div>
    </nav>
    <?php

    if ((isset($_GET['signIn']) or isset($_GET['login'])) and empty($activeUser)) {
    ?>
        <article class="p-3 mt-5" id="SignIn">
            <h1 class="text-center text-dark ">
                <?php
                if (isset($_GET['signIn']))
                    echo '<div class="alert alert-success" role="alert">
 sign in
</div>';





                elseif (isset($_GET['login']))
                    echo '<div class="alert alert-success" role="alert">
log in
</div>';

                ?>
            </h1>
            <div class="container mt-5 ">

                <div class="row ml-5 " style="gap: 1rem;justify-content: center; ">

                    <div class="card col-lg-4 col-md-4 col-sm-12 ml-2 mb-2 bg-dark text-white" style="width: 18rem; ">

                        <img class=" card-img-top " src="log.png">


                        <div class="card-body ">
                            <form class="" action="" method="get">
                                <div class="form-row row justify-content-center ">
                                    <div class="form-group col-md-12 ">

                                        <input type="text " class="form-control mt-1" name="userName" placeholder="userName " required>
                                    </div>
                                    <div class="form-group col-md-12">

                                        <input type="password " class="form-control mt-1" name="password" placeholder="password" required>
                                    </div>
                                    <?php if (isset($_GET['signIn'])) { ?>
                                        <div class="form-group col-md-12 mt-1">

                                            <input type="email " class="form-control " name="email" placeholder="Email " required>
                                        </div>
                                    <?php } ?>



                                </div>

                                <button type="submit " value="submit" name="submit" class="btn btn-primary mt-3 ">Sign
                                    in</button>


                            </form>

                        </div>
                    </div>

        </article>
    <?php } elseif ((isset($_GET['signIn']) or isset($_GET['login'])) and !empty($activeUser))
        echo "<h2 class='text-center alert alert-danger'>شما وارد شدید تلاش نکنید</h2>";
    if (isset($_GET['exit'])) {

        $activeUser = '';
        file_put_contents("active.json", json_encode($activeUser));
        // sleep(2);
        // echo "<h2 class='text-center'>you log out</h2>";

        header("Location: DRIVE.php");
    }

    if (isset($_GET['submit'])) {
        if (isset($_GET['email'])) {


            $UserName = $_GET['userName'];
            $password = $_GET['password'];
            $email = $_GET['email'];
            if (isset($userName["$UserName"]))
                die("<h2 class='text-center alert alert-danger'>user is set please try agin</h2>");
            else {
                $boolStatusUpload = true;
                $userName["{$UserName}"] = array("$password", "$email");
                file_put_contents($jsonUserName, json_encode($userName));
                $activeUser = $UserName;
                file_put_contents("active.json", json_encode($activeUser));
                echo "<h2 class='text-center alert alert-success'>register</h2>";
            }
        } elseif (!isset($_GET['email'])) {

            if (isset($userName[$_GET['userName']])) {
                if ($userName[$_GET['userName']][0] == $_GET['password']) {
                    echo "<h2 class='text-center alert alert-success'>login</h2>";
                    $boolStatusUpload = true;
                    $activeUser = $_GET['userName'];
                    file_put_contents("active.json", json_encode($activeUser));
                } else
                    echo "<h2 class='text-center alert alert-danger'>incorrect password</h2>";
            } else
                echo "<h2 class='text-center alert alert-danger'>incorrect username </h2>";
        }
    }

    if ($boolStatusUpload and isset($_GET['upload'])) {
    ?>
        <article class="p-3 mt-5" id="SignIn">
            <h2 class="text-center alert alert-success">
                <?php
                if (isset($activeUser))
                    echo "hello  " . $activeUser;


                ?>
            </h2>
            <!-- <h3 class="text-center p-3 mt-5">
                 <?php
                    // echo "please choose your job";
                    ?> 
            </h3> -->

            <div class="container mt-5 ">

                <div class="row ml-5 " style="gap: 1rem;justify-content: center; ">

                    <div class="card col-lg-8 col-md-8 col-sm-12 ml-2 mb-2 bg-dark text-white " style="width: 30rem; ">

                        <svg class="card-img-top mt-5" xmlns="http://www.w3.org/2000/svg" width="35" height="24" fill="currentColor" class="bi bi-upload" viewBox="0 0 16 16">
                            <path d="M.5 9.9a.5.5 0 0 1 .5.5v2.5a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1v-2.5a.5.5 0 0 1 1 0v2.5a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2v-2.5a.5.5 0 0 1 .5-.5z" />
                            <path d="M7.646 1.146a.5.5 0 0 1 .708 0l3 3a.5.5 0 0 1-.708.708L8.5 2.707V11.5a.5.5 0 0 1-1 0V2.707L5.354 4.854a.5.5 0 1 1-.708-.708l3-3z" />
                        </svg>
                        <div class="card-body ">

                            <form class="" action="" method="POST" enctype="multipart/form-data">
                                <div class="form-row row justify-content-center ">
                                    <div class="form-group col-md-12 ">
                                        <label for="file ">file</label>
                                        <input type="file" class="form-control " id="file" name="fileU" placeholder="file upload " required>
                                    </div>
                                    <div class="form-group col-md-12 ">
                                        <label for="FileName ">name</label>
                                        <input type="text" class="form-control " id="FileName" name="FileName" placeholder="file name" required>
                                    </div>





                                </div>

                                <button type="submit " value="upload" name="upload" class="btn btn-primary mt-3 ">upload</button>


                            </form>

                        <?php }


                    $Errors = false;

                    if (isset($_POST['upload'])) {





                        if ($_FILES['fileU']['size'] <= 1024 * 1024) {
                            if (!is_dir($target_file))
                                mkdir($target_file, 0777, true);

                            $target_file .= "/" . $_POST['FileName'] . "." . pathinfo($_FILES["fileU"]["name"], PATHINFO_EXTENSION);
                            if (file_exists($target_file)) {
                                die("<h2 class='text-center alert alert-danger'>file is set pleas  select another name</h2>");
                            } else {



                                move_uploaded_file($_FILES["fileU"]["tmp_name"], $target_file);

                                $Errors = true;
                            }
                        } else echo "<h2 class='text-center alert alert-danger'>file is large</h2>";
                        if ($Errors)
                            echo "<h2 class='text-center alert alert-success'>upload shd</h2>";
                        else
                            echo "<h2 class='text-center alert alert-danger'>upload nshd</h2>";
                    } elseif ((!$boolStatusUpload or empty($activeUser)) and isset($_GET['upload']))
                        echo "<h2 class='text-center alert alert-danger'>lotfan signin/login konid </h2>";

                        ?>

        </article>




        <?php
        if (isset($_GET['showFile']) and !empty($activeUser)) { ?>
            alert alert-danger
            <ul class="list-group">
                <li class="text-center">
                    <a href='?showFile&dir=<?= "$target_file" ?>'>
                        back to root</a>
                    <a class="px-5" href="?showFile&mk=<?= $target_file ?>">make folder in root</a>
                </li>

                <?php
                if (isset($_GET['dir'])) {
                    $userFile = $_GET['dir'];
                } else
                    $userFile = "uploads/$activeUser/";

                foreach (glob($userFile . '*') as $fileName) {
                    $fileFormat = '';
                    if (is_dir($fileName)) {
                        $type = 'folder';
                    } else {
                        $type = 'file';
                        $doPosition = strrpos($fileName, ".");
                        if ($doPosition !== false)
                            $fileFormat = substr($fileName, $doPosition + 1);
                    } ?>
                    <li class="list-group-item bg-dark text-white  mt-4 d-flex justify-content-around">
                        <?php
                        if (is_dir($fileName)) { ?>


                            <a class="px-4" href='?showFile&dir=<?= "$fileName/" ?>'>
                                <?= str_replace($userFile, '', $fileName); ?></a>

                            <span class='text-info'> <?= show($fileName) ?></span>
                            <a class='' href='?showFile&mk=<?= "$fileName/" ?>'>make in folder</a>

                        <?php
                        } else { ?>
                            <span class='text-warning'><?= str_replace($userFile, '', $fileName) ?></span>
                            <span class='text-info'><?= show($fileName) ?></span>

                        <?php } ?>


                        <a href='?showFile&dir=<?= "$userFile" ?>&del=<?= $fileName ?>'>
                            delete </a>

                        <a href=' ?showFile&dir=<?= "$userFile" ?>&edit=<?= "$fileName" ?>'>
                            rename
                        </a>



                    </li>

                <?php }
                ?>








            </ul>
            <?php }

        if (isset($_GET['del'])) {
            if (!isset($_GET['promp'])) {
            ?>

                <form action="" method="get" class="text-center">



                    <fieldset class="form-group text-warning text-center py-5">
                        <div class="row">
                            <p>do you want delete your file or move to your directory</p>
                            <legend class="col-form-label col-sm-2 pt-0"></legend>
                            <div class="col-sm-10">
                                <div class="form-check">
                                    <label for="yes" class="form-check-label">delete</label>
                                    <input type="radio" name="promp" id="yes" value="delete" class="form-check-input">
                                </div>
                                <div class="form-check">
                                    <label for="no" class="form-check-label">move</label>
                                    <input type="radio" name="promp" id="no" value="move" class="form-check-input">
                                </div>
                                <input type="hidden" name="del" value="<?= $_GET['del'] ?>">
                            </div>
                        </div>
                    </fieldset>



                    <button value="mode" name="mode" class="btn btn-success">submit</button>

                </form>

            <?php
            } elseif (Remove($_GET['del'], $_GET['promp']));
            // header("Location: DRIVE.php?showFile");
            else ?>
            <h2 class="text-center alert alert-danger "> <?= "file not deleted" ?></h2>
            <?php }
        if (isset($_GET['edit'])) {

            if (!isset($_GET['nameChange'])) {
            ?>


                <form action="" method="get" class="text-center">
                    <fieldset class="form-group text-warning text-center py-5">
                        <div class="row">
                            <legend class="col-form-label col-sm-2 pt-0"></legend>
                            <div class="col-sm-10">
                                <div class="form-check text-center">

                                    <input type="text" name="nameChange" class="" placeholder="enter name for edit">

                                    <input type="hidden" name="edit" value="<?= $_GET['edit'] ?>">
                                </div>

                            </div>
                        </div>
                    </fieldset>
                    <button class="btn btn-success">submit</button>
                </form>

            <?php } else {
                edit($_GET['edit'], $_GET['nameChange']);
                header("Location: DRIVE.php?showFile");
            }
        }

        if (isset($_GET['mk'])) {

            if (!isset($_GET['nameChange'])) {
            ?>

                <form action="" method="get" class="form-group text-center text-warning py-5">

                    <input type="text" name="nameChange" class="" placeholder="enter name for make folder">

                    <input type="hidden" name="mk" value="<?= $_GET['mk'] ?>">
                    <button class="btn btn-success">submit</button>
                </form>

        <?php } else {
                make($_GET['mk'], $_GET['nameChange']);
                header("Location: DRIVE.php?showFile");
            }
        }

        ?>

        <article>
            <script src=" https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js " integrity=" sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p " crossorigin=" anonymous ">
            </script>
            <script src=" https://code.jquery.com/jquery-3.2.1.slim.min.js " integrity=" sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN " crossorigin=" anonymous ">
            </script>

        </article>

</body>

</html>