Advertisement
nathaynes

MainScreen Directories

Dec 19th, 2014
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.42 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <header>
  5. <h1>Home</h1>
  6. </header>
  7.  
  8. <link rel="stylesheet" type="text/css" href="MainHomescreen.css" />
  9. <script src ="Scripts.js">
  10. </script>
  11. </head>
  12.  
  13. <body>
  14. <?php
  15. session_start();
  16.  
  17. //login cookies
  18. if(!isset($_COOKIE['loggedin']))
  19. {
  20. header("location:Login.php");
  21. }
  22.  
  23. //gets username from login
  24. $username = $_SESSION['username'];
  25.  
  26. //logout button request
  27. if(isset($_REQUEST['logout']))
  28. {
  29. $time = -10 + time();
  30. setcookie(loggedin, date("F jS - g:i a"), $time);
  31. header("location:Login.php");
  32.  
  33. //ends session for security
  34. session_unset();
  35. session_destroy();
  36. }
  37.  
  38. //Upload File
  39. if(isset($_REQUEST['submitFile']))
  40. {
  41. $target_dir = "/home/a1962403/public_html/$username/";
  42. $target_file = $target_dir . basename($_FILES["fileUpload"]["name"]);
  43. $uploadOk = 1;
  44.  
  45. // Check if file already exists
  46. if (file_exists($target_file))
  47. {
  48. $error = "Sorry, file already exists.";
  49. $uploadOk = 0;
  50. }
  51.  
  52. // Check file size
  53. else if ($_FILES["fileUpload"]["size"] > 500000)
  54. {
  55. $error ="Sorry, your file is too large.";
  56. $uploadOk = 0;
  57. }
  58.  
  59. // Check if $uploadOk is set to 0 by an error
  60. else if ($uploadOk == 0)
  61. {
  62. $error = "Sorry, your file was not uploaded.";
  63. }
  64. // if everything is ok, try to upload file
  65. else
  66. {
  67. if (move_uploaded_file($_FILES["fileUpload"]["tmp_name"], $target_file))
  68. {
  69. $success = "The file ". basename( $_FILES["fileUpload"]["name"]). " has been uploaded.";
  70. }
  71. else
  72. {
  73. $error = "Sorry, there was an error uploading your file.";
  74. }
  75. }
  76. }
  77.  
  78. //delete file
  79. if(isset($_REQUEST['deleteFile']))
  80. {
  81. //delete file
  82. if(isset($_POST['checkbox']))
  83. {
  84. foreach($_POST['checkbox'] as $selected)
  85. {
  86. $path = "/home/a1962403/public_html/$username/$selected";
  87.  
  88. if(unlink($path))
  89. {
  90. $output = "$selected deleted";
  91. }
  92. else
  93. {
  94. $error = "Error deleting $selected";
  95. }
  96. }
  97. }
  98.  
  99. //delete directory
  100. if(isset($_POST['dcheckbox']))
  101. {
  102. foreach($_POST['dcheckbox'] as $selected)
  103. {
  104. rmdir("/home/a1962403/public_html/$username/$selected");
  105. }
  106. }
  107.  
  108. else
  109. {
  110. $error = "please select a file/directory to delete";
  111. }
  112. }
  113.  
  114. if(isset($_REQUEST['renameFile']))
  115. {
  116. //uses checkboxes from table for files
  117. if(isset($_POST['checkbox']))
  118. {
  119. $checkedboxes = $_POST['checkbox'];
  120. $count = count($checkedboxes);
  121.  
  122. if($count == 0)
  123. {
  124. $error = "Please select a file or directory to rename!";
  125. }
  126.  
  127. if($count == 1)
  128. {
  129. foreach($_POST['checkbox'] as $selected)
  130. {
  131. //puts selected checkbox into session
  132. $_SESSION["selectedCB"] = $selected;
  133.  
  134. echo "<script> renameFile(); </script>"; // then calls if($rename = $_GET['rename']) below
  135. }
  136. }
  137.  
  138. if($count > 1)
  139. {
  140. $error = "Please only select ONE file or directory to rename at a time!";
  141. }
  142. }
  143.  
  144. //uses checkboxes from table for directories
  145. if(isset($_POST['dcheckbox']))
  146. {
  147. $checkedboxes = $_POST['dcheckbox'];
  148. $count = count($checkedboxes);
  149.  
  150. if($count == 0)
  151. {
  152. $error = "Please select a file or directory to rename!";
  153. }
  154.  
  155. if($count == 1)
  156. {
  157. foreach($_POST['dcheckbox'] as $selected)
  158. {
  159. //puts selected checkbox into session
  160. $_SESSION["selectedCB"] = $selected;
  161.  
  162. echo "<script> renameFile(); </script>"; // then calls if($rename = $_GET['rename']) below
  163. }
  164. }
  165.  
  166. if($count > 1)
  167. {
  168. $error = "Please only select ONE file or directory to rename at a time!";
  169. }
  170. }
  171.  
  172. }
  173. //finishes off changing file name!
  174. if(isset($_GET['rename']))
  175. {
  176. $rename = $_GET['rename'];
  177. $selectedCheck = $_SESSION["selectedCB"];
  178.  
  179. //get file extension
  180. $fileExt = pathinfo($selectedCheck, PATHINFO_EXTENSION);
  181.  
  182. if($rename !== "")
  183. {
  184. if($fileExt !== "")
  185. {
  186. rename("/home/a1962403/public_html/$username/$selectedCheck", "/home/a1962403/public_html/$username/$rename.$fileExt");
  187. }
  188. else if($fileExt == "")
  189. {
  190. rename("/home/a1962403/public_html/$username/$selectedCheck", "/home/a1962403/public_html/$username/$rename");
  191. }
  192. }
  193. if($rename == "")
  194. {
  195. $error = "Please enter a name, name cannot be empty";
  196. }
  197. }
  198.  
  199. //download selected file(s)
  200. if(isset($_REQUEST['download']))
  201. {
  202. // connect and login to FTP server
  203. $ftp_server = "natcmt3313site.site50.net";
  204. $ftp_username = "a1962403";
  205. $ftp_userpass = "";
  206.  
  207. $ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
  208. $login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
  209.  
  210. if (ftp_chdir($ftp_conn, "public_html/$username")) {
  211. //echo "Current directory is now: " . ftp_pwd($ftp_conn) . "\n";
  212. } else {
  213. echo "Couldn't change directory\n";
  214. }
  215.  
  216.  
  217. if(isset($_POST['checkbox']))
  218. {
  219. foreach($_POST['checkbox'] as $selected)
  220. {
  221. $local_file = "local.zip";
  222. $server_file = "$selected";
  223.  
  224. // download server file
  225. if (ftp_get($ftp_conn, $local_file, $server_file, FTP_BINARY))
  226. {
  227. header("Content-type: application/local.zip");
  228. header("Content-length: " . filesize($local_file));
  229. header("Content-disposition: attachment; filename= $selected");
  230.  
  231. echo file_get_contents($local_file);
  232. unlink($local_file);
  233. }
  234. else
  235. {
  236. $error = "Error downloading $server_file.";
  237. }
  238. }
  239. }
  240.  
  241. else
  242. {
  243. $error = "Sorry, you can't download directories";
  244. }
  245.  
  246. // close connection
  247. ftp_close($ftp_conn);
  248. }
  249.  
  250. //Create new directory - called from Script.js
  251. if(isset($_GET['newName']))
  252. {
  253. $newName = $_GET['newName'];
  254.  
  255. if($newName !== "")
  256. {
  257. //goes through files if found then breaks else continues going through
  258. foreach($filearray as $filename)
  259. {
  260. if($newName == $filename)
  261. {
  262. $found = true;
  263. break;
  264. }
  265. else if($newName != $filename)
  266. {
  267. $found = false;
  268. }
  269. }
  270.  
  271. if($found == true)
  272. {
  273. $error = "error, directory exists already";
  274. }
  275.  
  276. else //creates new directory
  277. {
  278. mkdir("/home/a1962403/public_html/$username/$newName", 0777);
  279. }
  280. }
  281.  
  282. else if($newName == "")
  283. {
  284. $error = "Please enter a new directory name, not empty";
  285. }
  286.  
  287. }
  288.  
  289. //Share button
  290. if(isset($_REQUEST['share']))
  291. {
  292. if(isset($_POST['checkbox']))
  293. {
  294. $selectedArray = array();
  295.  
  296. foreach($_POST['checkbox'] as $selected)
  297. {
  298. $selectedArray[] = $selected;
  299. }
  300. //puts selected checkbox into session
  301. $_SESSION["selectedShareCB"] = $selectedArray;
  302.  
  303. echo "<script> shareSelected(); </script>";
  304. }
  305.  
  306. else if(isset($_POST['dcheckbox']))
  307. {
  308. $error = "Sorry you cannot share directories";
  309. }
  310. else
  311. {
  312. $error = "Please select files to share";
  313. }
  314. }
  315.  
  316. //file sharing with named member
  317. if(isset($_GET['userEmail']))
  318. {
  319. //connect to database to find email
  320. $mysql_host = "mysql4.000webhost.com";
  321. $mysql_database = "a1962403_nat";
  322. $mysql_user = "a1962403_nat";
  323. $mysql_password = "";
  324.  
  325. $con = mysql_connect($mysql_host, $mysql_user, $mysql_password, $mysql_database) or die ("Failed");
  326.  
  327. $selected = mysql_select_db("a1962403_nat", $con);
  328.  
  329. $userEmail = $_GET['userEmail'];
  330. $userEmail = stripslashes($userEmail);
  331.  
  332. $query = "SELECT Username FROM Users WHERE Email = '$userEmail'";
  333. $result = mysql_query($query) or die(mysql_error());
  334.  
  335. //gets related username to email
  336. $getRes = mysql_fetch_assoc($result);
  337. $userUName = $getRes['Username'];
  338.  
  339. $rowCount = mysql_num_rows($result);
  340.  
  341. mysql_close();
  342.  
  343. //count cannot be > 1 as system doesn't allow when registering users
  344. if($rowCount == 0)
  345. {
  346. $error = "$userEmail doesn't exist! Please try again";
  347. }
  348. if($rowCount == 1)
  349. {
  350. foreach($_SESSION["selectedShareCB"] as $key => $value)
  351. {
  352. //gets source and destination
  353. $dir = "/home/a1962403/public_html/$userUName/SharedFiles/";
  354. $source = "/home/a1962403/public_html/$username/$value";
  355. if(file_exists($dir)) //checks for sharedfiles
  356. {
  357. $dir = "/home/a1962403/public_html/$userUName/SharedFiles/$username";
  358.  
  359. if(file_exists($dir)) //checks for username file
  360. {
  361. //puts file name on end to copy to
  362. $dest = "/home/a1962403/public_html/$userUName/SharedFiles/$username/$value";
  363. copy($source, $dest);
  364. }
  365. else
  366. {
  367. mkdir("/home/a1962403/public_html/$userUName/SharedFiles/$username", 0777);
  368. $dest = "/home/a1962403/public_html/$userUName/SharedFiles/$username/$value";
  369. copy($source, $dest);
  370. }
  371. }
  372. else //if SharedFiles isn't there, creates it then sends the file
  373. {
  374. mkdir("/home/a1962403/public_html/$userUName/SharedFiles/", 0777);
  375. mkdir("/home/a1962403/public_html/$userUName/SharedFiles/$username", 0777);
  376. $dest = "/home/a1962403/public_html/$userUName/SharedFiles/$username/$value";
  377. copy($source, $dest);
  378. }
  379. }
  380. }
  381. }
  382.  
  383. ?>
  384.  
  385. <form name="mainHomescreen" action="MainHomescreen.php" method="POST">
  386. <nav>
  387. <input type="text" name="Search" value="Search" style = "color:#888;" onFocus="inputFocus(this)" onBlur="inputBlur(this)"/>
  388.  
  389. <input type="submit" value="Logout" name="logout"/> <!-- DONE -->
  390. </nav>
  391.  
  392. <sidebar>
  393. <input type="button" value="Create Directory" onClick="createDirectory()"/> <!--DONE-->
  394. <input type="submit" value="Download Selected Files/Folders" name="download"/> <!-- SMALL ISSUE -->
  395. <input type="button" value="Encrypt Selected" onClick="encrypt()"/><br>
  396. <input type="submit" value="Share Selected" name="share"/>
  397. <input type="button" name="upload" value="Upload File" onClick="showUpload()"/> <!-- brings up pop up--> <!-- DONE -->
  398. <input type="submit" value="Delete" name="deleteFile"/> <!-- DONE -->
  399. <input type="submit" value="Rename" name="renameFile"/><br> <!-- DONE -->
  400. <input type="button" value="Password Protect Folder/ File" onClick=" "/><br><br>
  401.  
  402. </sidebar>
  403.  
  404. <article>
  405. <p> <?php echo $displaypath ?> </p><br><br>
  406.  
  407. <span class="error"><?php echo $error;?></span>
  408. <span class="output"><?php echo $output;?></span>
  409.  
  410. <!-- php to create table -->
  411. <?php
  412. if(isset($_GET['directory']))
  413. {
  414. $dirName = $_GET['directory'];
  415. $_SESSION["dirName"] = $dirName;
  416.  
  417. if($dirName == '.')
  418. {
  419. $currDir = $_SESSION["currDir"];
  420. $_SESSION["path"] = $currDir;
  421. }
  422.  
  423. else //down
  424. {
  425. $currDir = $_SESSION["newDir"];
  426. $currPath = "$currDir/$newDir";
  427. $_SESSION["path"] = "$currPath";
  428. }
  429. }
  430. else
  431. {
  432. $_SESSION["path"] = getcwd();
  433. $dirName = $username;
  434. }
  435.  
  436. $path = $_SESSION["path"];
  437.  
  438. $displaypath = $path;
  439. //open file names
  440. $dir = opendir("$path/$dirName/");
  441.  
  442. //fill array with directory and file names
  443. $filearray = array();
  444. $directory = opendir("$path/$dirName");
  445. while (($file = readdir($directory)) !== false)
  446. {
  447. $filearray[] = $file;
  448. }
  449.  
  450. echo '<table cellpadding="10" cellspacing="0" style="width:100%">';
  451. echo '
  452. <tr>
  453. <th> </th>
  454. <th> </th>
  455. <th>File</th>
  456. <th>Date</th>
  457. <th>Size</th>
  458. </tr>
  459. ';
  460.  
  461.  
  462. //List files in directory
  463. while (($file = readdir($dir)) !== false)
  464. {
  465. if($file !== '..')
  466. {
  467. if($dirName == $username)
  468. {
  469. if($file !== '.')
  470. {
  471. $size = filesize("$path/$dirName/$file");
  472. if($size == '4096')
  473. {
  474. echo '<tr class="bottomline" id="$rownumber">';
  475. echo "<td><input type='checkbox' name='dcheckbox[]' value='". $file . "' </td>";
  476. echo "<td><img src='/home/a1962403/public_html/images/Document.png'></td>";
  477. echo "<td><a href='MainHomescreen.php?directory=$file'>$file</a></td>";
  478. echo "<td></td>";
  479. echo "<td></td>";
  480. echo '</tr>';
  481. }
  482. else
  483. {
  484. //gets the file date, string needed decoding otherwise throws error.
  485.  
  486. $date = @filemtime($file);
  487. $date = date("F d Y H:i:s.", filemtime(utf8_decode("$path/$dirName/$file")));
  488.  
  489. //<!--prints a table row-->
  490. echo '<tr class="bottomline">';
  491.  
  492. echo "<td><input type='checkbox' name='checkbox[]' value='". $file . "' </td>";
  493. echo "<td><img src='/home/a1962403/public_html/images/file.png'></td>";
  494. echo "<td>$file</td>";
  495. echo "<td>$date</td>";
  496. echo "<td>$size bytes</td>";
  497. echo '</tr>';
  498.  
  499. }
  500. }
  501. }
  502.  
  503. else //dir not username therefore need ..
  504. {
  505. $size = filesize("$path/$dirName/$file");
  506.  
  507. if($size == '4096')
  508. {
  509. echo '<tr class="bottomline" id="$rownumber">';
  510.  
  511. echo "<td><input type='checkbox' name='dcheckbox[]' value='". $file . "' </td>";
  512.  
  513. echo "<td><img src='/home/a1962403/public_html/images/Document.png'></td>";
  514.  
  515. echo "<td><a href='MainHomescreen.php?directory=$file'>$file</a></td>";
  516. echo "<td></td>";
  517. echo "<td></td>";
  518. echo '</tr>';
  519. }
  520.  
  521. else
  522. {
  523. //gets the file date, string needed decoding otherwise throws error.
  524.  
  525. $date = @filemtime($file);
  526. $date = date("F d Y H:i:s.", filemtime(utf8_decode("$path/$dirName/$file")));
  527.  
  528. //<!--prints a table row-->
  529. echo '<tr class="bottomline">';
  530.  
  531. echo "<td><input type='checkbox' name='checkbox[]' value='". $file . "' </td>";
  532. echo "<td><img src='/home/a1962403/public_html/images/file.png'></td>";
  533. echo "<td>$file</td>";
  534. echo "<td>$date</td>";
  535. echo "<td>$size bytes</td>";
  536. } echo '</tr>';
  537. }
  538. }
  539. }
  540. echo '</table>';
  541.  
  542. $newDir = "$path/$dirName";
  543. $_SESSION["newDir"] = $newDir;
  544.  
  545. $currDir = $path;
  546. $_SESSION["currDir"] = $currDir;
  547.  
  548. closedir($dir);
  549. ?>
  550.  
  551.  
  552.  
  553. <p> description here when you hover over something maybe with buttons too? </p>
  554. </article>
  555.  
  556. </form>
  557.  
  558. <!-- upload a file -->
  559. <div id="fileUpload">
  560. <div id="uploadPopup">
  561. <form action="<?php echo $_SERVER['PHP_SELF']; ?>" id="uploadForm" method="post" name="uploadForm" enctype="multipart/form-data">
  562. <img id="close" src="/home/a1962403/public_html/images/Exit.png" onclick="hideUpload()">
  563. Select file to upload: <input name="fileUpload" type="file" /><br />
  564. <input type="submit" value="Upload File" name="submitFile"/>
  565. </form>
  566. </div>
  567. </div>
  568.  
  569. </body>
  570. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement