├── .gitignore ├── LICENSE ├── README.md ├── css └── index.css ├── index.php ├── js └── index.js └── lib ├── handlerDirectory.php ├── handlerFile.php ├── initialization.php └── userInterface.php /.gitignore: -------------------------------------------------------------------------------- 1 | configuration.php 2 | dir/ 3 | temp/ 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 DeveLoL 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # php-file-explorer-simple 2 | [PHP] Simple explorer with simple authorization 3 | 4 | ***PHP v7.2+*** 5 | 6 | ## Getting started 7 | 1. Cloning this repository 8 | 2. Launch **index.php** file for initialization 9 | 3. Update **configuration.php** file 10 | ```php 11 | "", // User name for authorization 14 | "pwd" => "", // Password for authorization 15 | "title" => "" // Text in the of the page 16 | ]; 17 | ?> 18 | ``` 19 | 20 | ## Overview of functions 21 | - Authorization 22 | - Creating a file and Upload file 23 | - Open in file editor 24 | - Open in browser 25 | - Deleting a file 26 | - Downloading files 27 | - Creating a directory 28 | - Open in explorer 29 | - Open in browser 30 | - Deleting a directory 31 | - Downloading directory 32 | 33 | ## File structure 34 | ``` 35 | / ............................ Root 36 | ├─ index.php ................. Main PHP file 37 | ├─ configuration.php ......... File with configuration 38 | ├─ css ....................... Folder with CSS files 39 | │ └─ index.css .............. Main CSS file 40 | ├─ js ........................ Folder with JavaScript files 41 | │ └─ index.js ............... Main JS file 42 | ├─ lib ....................... Folder with PHP classes 43 | │ ├─ handlerDirectory.php ... File with a class for working with directories 44 | │ ├─ handlerFile.php ........ File with a class for working with files 45 | │ ├─ initialization.php ..... File with class for first initialization 46 | │ └─ userInterface.php ...... File with a class for working with users components 47 | ├─ temp ...................... Folder with temporary files 48 | │ └─ . 49 | └─ dir ....................... Folder with explorer files and folders 50 | └─ . 51 | ``` 52 | -------------------------------------------------------------------------------- /css/index.css: -------------------------------------------------------------------------------- 1 | body{ 2 | margin: 1%; 3 | } 4 | .tdBtn{ 5 | text-align: center; 6 | } 7 | .tdHr{ 8 | margin: 0px; 9 | } 10 | .aBack{ 11 | text-decoration: none; 12 | } 13 | .tdName{ 14 | max-width: 50vw; 15 | overflow: hidden; 16 | display: inline-block; 17 | text-overflow: ellipsis; 18 | cursor: pointer; 19 | margin: 0.2em; 20 | margin-right: 1em; 21 | } 22 | .menuBtn{ 23 | margin: 0.33em; 24 | margin: 0.5em; 25 | font-weight: bold; 26 | } 27 | input[type=button]{ 28 | cursor: pointer; 29 | } 30 | input{ 31 | font-size: 1em; 32 | } 33 | .editorInp{ 34 | margin: 1%; 35 | margin-top: 2%; 36 | margin-bottom: 0px; 37 | margin-right: 0px; 38 | } 39 | body{ 40 | font-family: sans-serif; 41 | } 42 | .loginInput{ 43 | width: 10em; 44 | margin: 0.2em; 45 | } -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | <?php 2 | session_start(); 3 | include('./lib/initialization.php'); 4 | Initialization::_main(); 5 | 6 | include('./lib/handlerDirectory.php'); 7 | include('./lib/userInterface.php'); 8 | include('./lib/handlerFile.php'); 9 | 10 | $handlerDirectory = new HandlerDirectory(); 11 | $userInterface = new UserInterface(); 12 | $handlerFile = new HandlerFile(); 13 | 14 | if(isset($_SESSION["user"])){ 15 | if(isset($_GET['dwld'])) 16 | $handlerFile->downloadFile(".".$_GET['dwld']); 17 | 18 | if(isset($_GET['cDir'])) 19 | $handlerDirectory->createDirectory($_GET['link'], $_GET['cDir']); 20 | 21 | if(isset($_GET['cFile'])) 22 | $handlerFile->createFile($_GET['link'], $_GET['cFile']); 23 | 24 | if(isset($_GET['dFileName'])) 25 | $handlerFile->deleteFile($_GET['link'], $_GET['dFileName']); 26 | 27 | if(isset($_GET['dDirName'])) 28 | $handlerDirectory->deleteDirectory($_GET['link'], $_GET['dDirName']); 29 | 30 | if(isset($_GET['dwldDirName'])) 31 | $handlerFile->directoryToZip($_GET['link'], $_GET['dwldDirName']); 32 | 33 | if(isset($_FILES['uploadFile'])) 34 | $handlerFile->uploadFile($_FILES, $_GET["uploadFile"]); 35 | 36 | if(isset($_POST['sFile'])) 37 | $handlerFile->saveFile($_POST['link'], $_POST['sFile'], $_POST['text']); 38 | 39 | if(!isset($_GET["link"])) 40 | $_GET["link"]=""; 41 | } 42 | 43 | include('./configuration.php'); 44 | 45 | if(isset($_POST["userName"])) 46 | if($_POST["userName"]==$configuration["login"]) 47 | if($_POST["password"]==$configuration["pwd"]) 48 | $_SESSION["user"]=true; 49 | ?> 50 | <head> 51 | <title id="title"><?php $configuration["title"];?> 52 | 53 | 54 | 55 | 56 | editor(); 63 | echo $userInterface->explorer($_GET["link"]); 64 | echo $userInterface->uploadFile($_GET["link"]); 65 | }else{ 66 | echo $userInterface->login(); 67 | } 68 | 69 | ?> 70 | -------------------------------------------------------------------------------- /js/index.js: -------------------------------------------------------------------------------- 1 | let lastTitle = ""; 2 | 3 | function createDirectory(link){ 4 | let dname = prompt('Enter the name of the directory (only ASCII):'); 5 | if(dname!=null) 6 | location.href = '?link='+encodeURIComponent(link)+ 7 | '&cDir='+encodeURIComponent(dname); 8 | } 9 | 10 | function createFile(link){ 11 | let dname = prompt('Enter the name of the file (only ASCII):'); 12 | if(dname!=null) 13 | location.href = '?link='+encodeURIComponent(link)+ 14 | '&cFile='+encodeURIComponent(dname); 15 | } 16 | 17 | function deleteFile(link, fileName){ 18 | if(confirm('Do you really want to delete the '+fileName+' file?')) 19 | location.href = '?link='+encodeURIComponent(link)+ 20 | '&dFileName='+encodeURIComponent(fileName); 21 | } 22 | 23 | function deleteDirectory(link, dirName){ 24 | if(confirm('Do you really want to delete the '+dirName+' directory?')) 25 | location.href = '?link='+encodeURIComponent(link)+ 26 | '&dDirName='+encodeURIComponent(dirName); 27 | } 28 | 29 | function dwldDirectory(link, dirName){ 30 | location.href = '?link='+encodeURIComponent(link)+ 31 | '&dwldDirName='+encodeURIComponent(dirName); 32 | } 33 | 34 | function getFileText(link){ 35 | var xhr = new XMLHttpRequest(); 36 | xhr.open('GET', link, false); 37 | xhr.send(); 38 | if (xhr.status != 200) { 39 | alert(xhr.status+': '+xhr.statusText); 40 | } else { 41 | document.getElementById('taEditor').innerHTML = xhr.responseText; 42 | } 43 | } 44 | 45 | function closeEditor(){ 46 | editor.setValue(""); 47 | document.getElementById('inpEditor').value = ''; 48 | document.getElementById('divEditor').style.display = 'none'; 49 | document.getElementById('saveEditorBtn').onclick = function() {}; 50 | document.getElementById('title').innerHTML=lastTitle; 51 | } 52 | 53 | function editFile(link, fileName){ 54 | lastTitle = document.getElementById('title').innerHTML; 55 | closeEditor(); 56 | getFileText('?dwld=/dir'+link+'/'+fileName); 57 | document.getElementById('inpEditor').value = fileName; 58 | document.getElementById('divEditor').style.display = 'block'; 59 | document.getElementById('saveEditorBtn').onclick = function() { saveFile(link, fileName); }; 60 | document.getElementById('title').innerHTML=fileName; 61 | } 62 | 63 | function saveFile(link, fileName){ 64 | var xhr = new XMLHttpRequest(); 65 | xhr.open('POST', 'index.php', true); 66 | xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); 67 | xhr.send( 68 | 'link='+encodeURIComponent(link)+ 69 | '&sFile='+encodeURIComponent(fileName)+ 70 | '&text='+encodeURIComponent(document.getElementById('taEditor').value) 71 | ); 72 | } 73 | 74 | function uploadFile(link){ 75 | document.getElementById('uploadFile').style.display = 'block'; 76 | } 77 | 78 | function closeUploader(link){ 79 | document.getElementById('uploadFile').style.display = 'none'; 80 | } -------------------------------------------------------------------------------- /lib/handlerDirectory.php: -------------------------------------------------------------------------------- 1 | rmDirectory($dir. DIRECTORY_SEPARATOR .$object); 10 | else 11 | unlink($dir. DIRECTORY_SEPARATOR .$object); 12 | } 13 | } 14 | rmdir($dir); 15 | } 16 | } 17 | 18 | function deleteDirectory($link, $dirName){ 19 | $this->rmDirectory("./dir".$link."/".$dirName); 20 | header("Location: ?link=".urlencode($link)); 21 | } 22 | 23 | function createDirectory($link, $dirName){ 24 | $dirName = preg_replace('/[^\x20-\x7E]/','', $dirName); 25 | mkdir("./dir".$link."/".$dirName, 0755); 26 | header("Location: ?link=".urlencode($link)); 27 | } 28 | } 29 | ?> -------------------------------------------------------------------------------- /lib/handlerFile.php: -------------------------------------------------------------------------------- 1 | addFileRecursion($zip, $obj, $start); 43 | } else { 44 | $zip->addFile($obj, str_replace(dirname($start) . '/', '', $obj)); 45 | } 46 | } 47 | } 48 | } 49 | 50 | function createFile($link, $fileName){ 51 | $fileName = preg_replace('/[^\x20-\x7E]/','', $fileName); 52 | file_put_contents("./dir".$link."/".$fileName, ""); 53 | header("Location: ?link=".urlencode($link)); 54 | } 55 | 56 | function deleteFile($link, $fileName){ 57 | unlink("./dir".$link."/".$fileName); 58 | header("Location: ?link=".urlencode($link)); 59 | } 60 | 61 | 62 | function saveFile($link, $fileName, $text){ 63 | file_put_contents("dir/".$link."/".$fileName, $text); 64 | header("Location: ?link=".urlencode($link)); 65 | } 66 | 67 | function directoryToZip($link, $dirName){ 68 | $zip = new ZipArchive(); 69 | $aLink="dir/_temp/".$dirName.".zip"; 70 | $zip->open($aLink, ZipArchive::CREATE|ZipArchive::OVERWRITE); 71 | $this->addFileRecursion($zip, "./dir".$link."/".$dirName); 72 | $zip->close(); 73 | 74 | $this->downloadFile($aLink, $dirName."_".date("Y_m_d_H_i_s").".zip"); 75 | header("Location: ?link=".urlencode($link)); 76 | } 77 | } 78 | ?> -------------------------------------------------------------------------------- /lib/initialization.php: -------------------------------------------------------------------------------- 1 | \"admin\", 15 | \"pwd\" => \"admin\", 16 | \"title\" => \"php-explorer-simple\" 17 | ]; 18 | ?> 19 | "); 20 | } 21 | } -------------------------------------------------------------------------------- /lib/userInterface.php: -------------------------------------------------------------------------------- 1 | 23 | 24 |
25 | 26 |
27 | 28 | 29 | "; 30 | return $result; 31 | } 32 | 33 | function uploadFile($root){ 34 | $root=str_replace("..","?",$root); 35 | $result=" 36 |
46 | 47 |
48 | 49 | 50 |
51 |
52 | "; 53 | return $result; 54 | } 55 | 56 | function editor(){ 57 | $result=" 58 |
67 | 68 | 69 | 70 | 77 |
78 | "; 79 | return $result; 80 | } 81 | 82 | function explorer($root){ 83 | $root=str_replace("..","?",$root); 84 | $result=""; 90 | if(!is_dir("./dir".$root)) 91 | return "

error

"; 92 | $result.="

"; 93 | if(substr_count($root, "/")>0){ 94 | $elem0=explode("/", $root); 95 | array_pop($elem0); 96 | $result.="🔙 "; 97 | }else{ 98 | $result.="🔙 "; 99 | } 100 | $result.=""; 101 | $result.="

"; 102 | $result.=""; 103 | $result.=""; 104 | $result.=""; 105 | $result.=""; 106 | $result.=""; 107 | $result.=""; 110 | $result.=""; 111 | $dirs=$this->getDirs($root); 112 | if(count($dirs)==0){ 113 | $result.=""; 114 | $result.=""; 117 | $result.=""; 118 | } 119 | foreach ($dirs as $dir){ 120 | $result.=""; 121 | if($dir["type"]=="file"){ 122 | $result.=""; 125 | $result.=""; 128 | $result.=""; 131 | $result.=""; 134 | } 135 | if($dir["type"]=="dir"){ 136 | $result.=""; 139 | $result.=""; 142 | $result.=""; 145 | $result.=""; 148 | } 149 | $result.=""; 150 | $result.=""; 151 | $result.=""; 154 | $result.=""; 155 | } 156 | $result.="
"; 108 | $result.="
"; 109 | $result.="
"; 115 | $result.="Missing files and folders..."; 116 | $result.="
"; 123 | $result.="📄 ".$dir["name"]; 124 | $result.=""; 126 | $result.=""; 127 | $result.=""; 129 | $result.=""; 130 | $result.=""; 132 | $result.=""; 133 | $result.=""; 137 | $result.=""."📁 ".$dir["name"].""; 138 | $result.=""; 140 | $result.=""; 141 | $result.=""; 143 | $result.=""; 144 | $result.=""; 146 | $result.=""; 147 | $result.="
"; 152 | $result.="
"; 153 | $result.="
"; 157 | return $result; 158 | } 159 | } 160 | ?> --------------------------------------------------------------------------------