Advertisement
Guest User

Untitled

a guest
Apr 9th, 2014
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.03 KB | None | 0 0
  1. <?php
  2.  
  3. #####################
  4. # Online editor #####
  5. #####################
  6. # Version: 0.1 ######
  7. # Author: tryy3 #####
  8. # Credits: bonney12 #
  9. #####################
  10.  
  11.  
  12.  
  13. #############################################################################################################
  14. # Debug function ############################################################################################
  15. #############################################################################################################
  16. # $value = The message, usually an array! ###################################################################
  17. # $var_dump = If the function is going to use vardump() or print_r() ########################################
  18. # Description: Makes debug messages look nicer. #############################################################
  19. # Future: Planning on adding a few security features such as checking if the $value is an array or not etc. #
  20. ######### Nicer output style! ###############################################################################
  21. #############################################################################################################
  22.  
  23. function debug($value, $var_dump = false)
  24. {
  25. echo "<pre>";
  26. if($var_dump)
  27. {
  28. var_dump($value);
  29. }
  30. else
  31. {
  32. print_r($value);
  33. }
  34. echo "</pre>";
  35. }
  36.  
  37.  
  38. ###############################################################################################
  39. # Size Function ###############################################################################
  40. ###############################################################################################
  41. # $size = Size in bytes! ######################################################################
  42. # Description: Converts bytes to the next size up, kb, mb, gb. ################################
  43. # Future: Planning on making it reversed too and larger sizes, and able to choose the format! #
  44. ###############################################################################################
  45.  
  46. function size($size)
  47. {
  48. if($size < 1024)
  49. {
  50. $returnSize = $size / 1024;
  51. $returnFormat = "KB";
  52. }
  53. else if($size < 1048576)
  54. {
  55. $returnSize = $size / 1024 / 1024;
  56. $returnFormat = "MB";
  57. }
  58. else
  59. {
  60. $returnSize = $size / 1024 / 1024 / 1024;
  61. $returnFormat = "GB";
  62. }
  63.  
  64. return $returnSize . " " . $returnFormat;
  65. }
  66.  
  67.  
  68. ###################################################################################
  69. # Error function ##################################################################
  70. ###################################################################################
  71. # $message = The error message! ###################################################
  72. # Description: Sends a nice error message in the form of an alert. ################
  73. # Future: Planning on maybe expanding it to success, alert, multiline etc. later! #
  74. ###################################################################################
  75.  
  76. function error($message)
  77. {
  78. echo '<div class="alert alert-danger">Error!!!<br>';
  79. echo $message;
  80. echo '</div>';
  81. }
  82.  
  83.  
  84. # Just a some Variables that need to be used later!
  85.  
  86. $currentPathIsDir = false;
  87. $filePath = "";
  88.  
  89.  
  90. # Checks if the file is in a different folder to the current one
  91. # If it is, then add the path from ?file= to the default home dir
  92. # If not, just save the default home dir in $folderpath
  93.  
  94. if (isset($_GET["file"]) && $_GET != "")
  95. {
  96. $filePath = $_GET["file"];
  97. $folderPath = "/home/ExpandHosting/www" . $filePath;
  98. }
  99. else
  100. {
  101. $folderPath = "/home/ExpandHosting/www";
  102. }
  103.  
  104.  
  105. # The start of the upload process
  106. #
  107. # Checks if the global variable $_FILES has the key uploadfield
  108.  
  109. if(isset($_FILES["uploadfield"]))
  110. {
  111.  
  112.  
  113. # Adds a variable in case there is an error
  114.  
  115. $error = false;
  116.  
  117.  
  118. # Checks if there were any errors uploading the file,
  119. # if there were, it will send the error message to the error function and set $error to true
  120. # else it will continue with the uploading process
  121.  
  122. if($_FILES["uploadfield"]["error"] > 0)
  123. {
  124. error($_FILES["uploadfield"]["error"]);
  125. $error = true;
  126. }
  127. else
  128. {
  129.  
  130.  
  131. # Sets the $file variable so I don't need to type $_FILES["uploadfield"] all the time!
  132. # Adds the $fileSize variable so people don't upload files that are too big, default 5gb!
  133.  
  134. $file = $_FILES["uploadfield"];
  135. $fileSize = pow(1024, 3) * 5;
  136.  
  137.  
  138. # Checks the uploaded file size if its less then $fileSize!
  139. # Else it continue wit the uploading process!
  140.  
  141. if ($file["size"] > $fileSize)
  142. {
  143. error("File is too large!");
  144. $error = true;
  145. }
  146. else
  147. {
  148.  
  149.  
  150. # Adds the variable $customFileName so that it can output the file name even if they set a custom name or not!
  151.  
  152. $customFileName = "";
  153.  
  154.  
  155. # Checks if the user set a upload name and if its empty!
  156. # If its set and not empty it will change the uploaded name to name that the user wants!
  157. # Else it will use the uploaded files name!
  158.  
  159. if(isset($_POST["uploadname"]) && $_POST["uploadname"] != "")
  160. {
  161.  
  162.  
  163. # Checks if the file exists or not!
  164. # If it exists, send an error!
  165. # If it doesn't exist then move the file temporarily to the folder the user is in
  166. # and name the file as the user specified
  167.  
  168. if(file_exists($folderPath . "/" . $_POST["uploadname"]))
  169. {
  170. error("File already exists!!!");
  171. $error = true;
  172. }
  173. else
  174. {
  175. move_uploaded_file($file["tmp_name"], $folderPath . "/" . $_POST["uploadname"]);
  176. $customFileName = $_POST["uploadname"];
  177. }
  178. }
  179. else
  180. {
  181.  
  182.  
  183. # Checks if the file exists or not!
  184. # If it exists, send an error!
  185. # If it doesn't exist then move the file temporarily to the folder the user in
  186. # and name the file as the user specified
  187. if (file_exists($folderPath . "/" . $file["name"]))
  188. {
  189. error("File already exists!!!");
  190. $error = true;
  191. }
  192. else
  193. {
  194. move_uploaded_file($file["tmp_name"], $folderPath . "/" . $file["name"]);
  195. $customFileName = $file["name"];
  196. }
  197. }
  198.  
  199.  
  200. # Checks if there are no errors!
  201. # If there is an error then just don't do anything because there should already be an error popping up!
  202. # If there is no error then output a success alert with some information about the file (currently only the name and the file size)!
  203.  
  204. if(!$error)
  205. {
  206. echo '<div class="alert alert-success"> SUCCESS!!! <br>';
  207. echo "File uploaded! <br>";
  208. echo 'Name: ' . $customFileName . "<br>";
  209. echo 'Size: ' . $file["size"];
  210. echo "</div>";
  211. }
  212. }
  213. }
  214. }
  215.  
  216.  
  217. # The start of the rename process!
  218. #
  219. # Checks if the renameFile and renameName key is in the global variable $_POST
  220.  
  221. if (isset($_POST["renameFile"]) && isset($_POST["renameName"]))
  222. {
  223.  
  224.  
  225. # Sets some variables to replace the use of $_POST
  226.  
  227. $file = $_POST["renameFile"];
  228. $fileName = $_POST["renameName"];
  229.  
  230.  
  231. # Sets the error variable so I can cancel the process more easily!
  232.  
  233. $error = false;
  234.  
  235.  
  236. # Checks if the file $fileName exists!
  237. # If it does exist then send an error and set $error to true!
  238. # if it doesn't exist then continue with the process!
  239.  
  240. if(file_exists($folderPath . "/" . $fileName))
  241. {
  242. error($fileName . " already exists!!!");
  243. $error = true;
  244.  
  245.  
  246.  
  247. # Checks if $file doesn't exist!
  248. # If it does exist then just continue with the process!
  249. # If it doesn't exist then send an error and set $error to true!
  250.  
  251. if(!file_exists($folderPath . "/" . $file))
  252. {
  253. error($file . " does not exist!!!");
  254. $error = true;
  255. }
  256.  
  257.  
  258. # Checks if there were any errors!
  259. # If there were no errors, then rename the file and send a success alert!
  260. # If there were errors then don't do anything, because there should already be errors popping up!
  261.  
  262. if(!$error)
  263. {
  264. rename($folderPath . "/" . $file, $folderPath . "/" . $fileName);
  265.  
  266. echo '<div class="alert alert-success"> SUCCESS!!! <br>';
  267. echo "Renamed file " . $file . " to " . $fileName . "!";
  268. echo '</div>';
  269. }
  270. }
  271.  
  272.  
  273. # The start of the file creation process!
  274. #
  275. # Checks if the createFile key is in the global variable $_POST
  276.  
  277. if (isset($_POST["createFile"]))
  278. {
  279.  
  280.  
  281. # Sets a variable for the createFile key, to make it easier for us!
  282.  
  283. $file = $_POST["createFile"];
  284.  
  285.  
  286. # Checks if the file you want to create already exists!
  287. # If it exists then send an error!
  288. # if not then continue with the creation process!
  289.  
  290. if(file_exists($folderPath . "/" . $file))
  291. {
  292. error("File already exists!!!");
  293. }
  294.  
  295.  
  296. # If there were no errors above, then, open a file handler (if the file doesn't exist it will automatically create a file)
  297. # when the handler is open, close it to save the file and send a success alert!
  298.  
  299. else
  300. {
  301. $createHandle = fopen($folderPath . "/" . $file, "w");
  302. fclose($createHandle);
  303.  
  304. echo '<div class="alert alert-success"> SUCCESS!!! <br>';
  305. echo "Created file " . $file . "!";
  306. echo '</div>';
  307. }
  308. }
  309.  
  310.  
  311.  
  312. # The start of file removal process!
  313. #
  314. # Checks if the key deleteFile is in the global variable $_POST
  315.  
  316. if (isset($_POST["deleteFile"]))
  317. {
  318.  
  319.  
  320. # Sets a variable to make it easier to work with
  321.  
  322. $file = $_POST["deleteFile"];
  323.  
  324.  
  325. # Checks if the file doesn't exist!
  326. # If it doesn't exist then send an error!
  327. # If it does exist then unlink the file (unlink will make the system forget about the file, aka delete it)!
  328.  
  329. if(!file_exists($folderPath . "/" . $file))
  330. {
  331. error("File does not exist!!!");
  332. }
  333. else
  334. {
  335. unlink($folderPath . "/" . $file);
  336. }
  337. }
  338.  
  339.  
  340. # The start of the file writing procedure
  341. #
  342. # Checks if the key textarea in the global variable $_POST is set!
  343. # If it is, then open a file handler, write to the file and then close it to save the file!
  344.  
  345. if(isset($_POST["textarea"]))
  346. {
  347. $fileWrite = fopen($folderPath, 'w');
  348.  
  349. fwrite($fileWrite, $_POST["textarea"]);
  350.  
  351. fclose($fileWrite);
  352. }
  353.  
  354.  
  355. # Checks if the path you're in is a file or a dir!
  356.  
  357. if (is_dir($folderPath))
  358. {
  359. $currentPathIsDir = true;
  360. }
  361.  
  362.  
  363. # If the current path is a dir then start the process of showing the directory process!
  364.  
  365. if($currentPathIsDir)
  366. {
  367.  
  368.  
  369. # Adds a few array variables that need to be used later!
  370.  
  371. $fileResult = array();
  372. $folderResult = array();
  373. $newFileResult = array();
  374. $newFolderResult = array();
  375.  
  376.  
  377. # Opens a directory handler so I can read every file/directory in a directory!
  378.  
  379. $handler = opendir($folderPath);
  380.  
  381.  
  382. # Runs a while loop to read every line of the directory handler!
  383.  
  384. while($file = readdir($handler))
  385. {
  386.  
  387.  
  388. # Checks if the file/dir the handler currently looking at is . (current dir) or .. (the parent directory)!
  389.  
  390. if ($file != "." && $file != "..")
  391. {
  392.  
  393.  
  394. # Checks if $file is a directory or a file!
  395. # If it's a folder, then add a {folder} tag to the $file variable and then add it to the $folderResult array!
  396. # If it's a file, then add a {file} tag to the $file variable and then add it to the $fileResult array!
  397.  
  398. if (is_dir($folderPath . "/" . $file))
  399. {
  400. $folderResult[] = "{folder}" . $file;
  401. }
  402. else
  403. {
  404. $fileResult[] = "{file}" . $file;
  405. }
  406. }
  407. }
  408.  
  409.  
  410. # Closes the directory handler (no need to use it again)!
  411.  
  412. closedir($handler);
  413.  
  414.  
  415. # Runs a foreach loop through every $folderResult key to replace {folder} with nothing!
  416. # The commented stuff was used in an earlier version and I might add it again in the future, for now they will stay where they are!
  417.  
  418. foreach ($folderResult as $key => $value)
  419. {
  420. //$value = str_replace('{blue}', '', $value);
  421. $value = str_replace('{folder}', '', $value);
  422.  
  423. //$newValue = str_replace('{blue}', '<span style="color:#2B2BFF">', $value) . "</span>";
  424. $newFolderResult[$value] = str_replace('{folder}', '', $value);
  425. }
  426.  
  427.  
  428. # Same as above except folder is replaced with file!
  429.  
  430. foreach ($fileResult as $key => $value)
  431. {
  432. //$value = str_replace('{yellow}', '', $value);
  433. $value = str_replace('{file}', '', $value);
  434.  
  435. //$newValue = str_replace('{yellow}', '<span style="color:#D9D900">', $value) . "</span>";
  436. $newFileResult[$value] = str_replace('{file}', '', $value);
  437. }
  438. }
  439.  
  440.  
  441. # If the path is currently a file then add a file handler to $fileContext so the code can read the file
  442. # and then add some extension that can't be read in a raw format (images, zip files etc.)!
  443.  
  444. else
  445. {
  446. $fileContext = file($folderPath);
  447.  
  448. $imageExtension = array("png", "jpg", "jpeg", "ico",
  449. "gif", "bmp", "tiff", "svg"
  450. );
  451.  
  452. $downloadExtension = array("zip", "jar", "rar", "pdf",
  453. "avi", "ai"
  454. );
  455. }
  456.  
  457. ?>
  458.  
  459. <html>
  460. <head>
  461.  
  462.  
  463. <!-- Just some css stuff -->
  464.  
  465. <style type="text/css">
  466. body
  467. {
  468. background-color:grey;
  469. color:white;
  470. }
  471. </style>
  472. <?php include("lib/css.php"); ?>
  473. </head>
  474. <body>
  475. <?php
  476.  
  477.  
  478. # Checks if the current path is a dir!
  479. # If it is then start the process of showing the dir UI!
  480.  
  481. if($currentPathIsDir)
  482. {
  483. ?>
  484. <div class="row">
  485. <div class="col-md-6">
  486. <?php
  487.  
  488.  
  489. # Checks if you are in the top directory or not, if you are then add a .. link so you can get back to the previous folder!
  490.  
  491. if (isset($_GET['file']) && $_GET['file'] != "")
  492. {
  493. echo '<img src="img/directory_icon.png" width="24" height="24"> <a href="?file=' . substr($filePath, 0, strrpos($filePath, "/")) . '">..</a><br>';
  494. }
  495.  
  496.  
  497. # foreach loops on $newFolderResult and $newFileResult and output it in a nice UI view!
  498.  
  499. foreach ($newFolderResult as $key => $value)
  500. {
  501. echo '<img src="img/directory_icon.png" width="24" height="24"> <a href="?file=' . $filePath . "/" . $key . '">' . $value . '</a><br>';
  502. }
  503.  
  504. foreach ($newFileResult as $key => $value)
  505. {
  506. echo '<img src="img/file_icon.png" width="24" height="24"> <a href="?file=' . $filePath . "/" . $key . '">' . $value . '</a><br>';
  507. }
  508.  
  509. ?>
  510. </div>
  511. <div class="col-md-6">
  512.  
  513.  
  514. <!-- The form for uploading a file!-->
  515.  
  516. <form action="" method="post" enctype="multipart/form-data">
  517. File: <input type="file" name="uploadfield">
  518. Name: <input type="text" name="uploadname"><br>
  519. <input type="submit" value="Upload File">
  520. </form>
  521.  
  522. <br>
  523. <br>
  524.  
  525.  
  526. <!-- The form for renaming a file! -->
  527.  
  528. <form action="" method="post" name="rename">
  529. File/folder: <select name="renameFile">
  530. <?php
  531.  
  532.  
  533. # Performs a foreach loop on $newFolderResult and $newFileResult to make it easier to pick the file/dir you want to rename!
  534.  
  535. foreach ($newFolderResult as $key => $value)
  536. {
  537. echo '<option value="' . $value . '">' . $value . "</option>";
  538. }
  539. foreach ($newFileResult as $key => $value)
  540. {
  541. echo '<option value="' . $value . '">' . $value . "</option>";
  542. }
  543. ?>
  544. </select> <br>
  545. Name: <input type="text" name="renameName"> <br>
  546. <input type="submit" value="rename">
  547. </form>
  548.  
  549. <br>
  550. <br>
  551.  
  552.  
  553. <!-- The form for deleting a file! -->
  554.  
  555. <form action="" method="post" name="delete">
  556. File/folder: <select name="deleteFile">
  557. <?php
  558.  
  559.  
  560. # Performs a foreach loop on $newFolderResult and $newFileResult to make it easier to pick the file/dir you want to delete!
  561.  
  562. foreach ($newFolderResult as $key => $value)
  563. {
  564. echo '<option value="' . $value . '">' . $value . "</option>";
  565. }
  566. foreach ($newFileResult as $key => $value)
  567. {
  568. echo '<option value="' . $value . '">' . $value . "</option>";
  569. }
  570. ?>
  571. </select><br>
  572. <input type="submit" value="delete">
  573. </form>
  574.  
  575. <br>
  576. <br>
  577.  
  578.  
  579. <!-- The form for creating a file! -->
  580.  
  581. <form action="" method="post" name="create">
  582. File name: <input type="text" name="createFile"> <br>
  583. <input type="submit" value="create">
  584. </form>
  585.  
  586. </div>
  587. </div>
  588. <?php
  589. }
  590.  
  591.  
  592. # If the current path is a file then start the process of showing the file UI!
  593.  
  594. else
  595. {
  596.  
  597.  
  598. # Checks if the file extension is in the $imageExtension array!
  599. # If the file is in the array then output it as an image using the html img tag!
  600. # If the file is not in an array then start the editing a file UI!
  601.  
  602. if(in_array(pathinfo($folderPath)["extension"], $imageExtension))
  603. {
  604. echo '<img src="' . $filePath . '">';
  605. }
  606. else
  607. {
  608. ?>
  609.  
  610.  
  611. <!-- Setup a textarea form and prints every line from the current file -->
  612.  
  613. <form name="text" action="" method="post">
  614. <textarea name="textarea" rows="50" cols="200">
  615. <?php
  616.  
  617. foreach($fileContext as $key => $value)
  618. {
  619. echo ($value);
  620. }
  621. ?>
  622. </textarea>
  623. <br>
  624. <input type="submit" value="Submit">
  625. </form>
  626. <?php
  627. }
  628. }
  629. ?>
  630.  
  631.  
  632. <!-- Adds the js libs -->
  633.  
  634. <?php include("lib/js.php"); ?>
  635. </body>
  636. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement