Advertisement
zefie

Simple crappy apache greyhole trash manager

Aug 17th, 2013
420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.58 KB | None | 0 0
  1. <?php
  2. header('Content-Type: text/html; charset=utf-8');
  3. set_time_limit(0);
  4. /* Notes:
  5.    This code not designed for public consumption. It may work, but the code is quick and dirty.
  6.    The coding style used in this code is not reflective of my professional work, but rather my
  7.    "lets get it done now" style of work. It is only used by me in my LAN environment. This is not
  8.    secure, so do not use it in a production environment. Protect it with .htaccess/.htpasswd and
  9.    use common sense. The apache user (or suPHP user) will need write access to the Greyhole shares,
  10.    and read access to /etc/greyhole.conf
  11.  
  12.    "Copies", when the result is a directory, is more like "Spanned across X drives".
  13.    "Copies", when the result is a file, is an accurate representation of how many drives
  14.    that specific file is on.
  15.  
  16.    This was merely created to suit my needs and make it easier to purge/restore deleted items
  17.    from a greyhole share's trash. It is not a complete management system, nor does it have all the
  18.    features that it could. Basically, it does what I need. I share the code in case someone else
  19.    desires the same functionality, or would like to expand it (either for personal or commercial
  20.    use. My only request is if you expand this code and sell it, that I get a free license ;)
  21.  
  22.    An example gallery can be found here: http://imgur.com/a/rTg3x
  23.  
  24.    Created by Zefie of Zefie Networks. BSD License, or whatever. Do what you want with it.
  25.    
  26.    Updated 2014-02-11, because I started using Greyhole again and noticed some missing features.
  27.    Added: Last Modified, Size, and UTF-8 support.
  28. */
  29.  
  30. $drives = getDrives();
  31.  
  32. if (@$_GET['path']) $ext = $_GET['path'];
  33. else $ext = "";
  34.  
  35. if (@$_GET['view']) $view = $_GET['view'];
  36. else $view = "";
  37.  
  38. if (@$_GET['drive']) $drive = $_GET['drive'];
  39. else $drive = "";
  40.  
  41. if (@$_GET['action']) $action = $_GET['action'];
  42. else $action = "";
  43.  
  44. if (@$_GET['confirm']) $confirm = $_GET['confirm'];
  45. else $confirm = "";
  46.  
  47. if (($drive == "" || $view == "") && $action == "") {
  48.     $ptitle = "Greyhole Trash Manager";
  49.     if ($ext != "") {
  50.         $ptitle .= " - ".$ext;
  51.     }
  52.  
  53.     echo "<html>\n";
  54.     echo "<head>\n";
  55.     echo "<title>".$ptitle."</title>\n";
  56.     echo "<style type=\"text/css\">\n";
  57.     echo "td, th {\n";
  58.     echo "\tpadding: 7px 5px 7px 5px;\n";
  59.     echo "\tborder: 1px dotted #000;\n";
  60.     echo "}\n";
  61.     echo "th {\n";
  62.     echo "\ttext-align: left;\n";
  63.     echo "\tfont-weight: bold;\n";
  64.     echo "\tfont-style: italic;\n";
  65.     echo "}\n";
  66.     echo "form {\n";
  67.     echo "\tdisplay: inline;\n";
  68.     echo "}\n";
  69.     echo ".ral {\n";
  70.     echo "\ttext-align: right\n";
  71.     echo "}\n";
  72.     echo "</style>\n";
  73.     echo "</head>\n";
  74.     echo "<body>\n";
  75.     echo "<h2>".$ptitle."</h2>\n";
  76.     echo "<table>\n";
  77.     echo "<tr>\n";
  78.     echo "<th>Path</th>\n";
  79.     echo "<th>Size</th>\n";
  80.     echo "<th>Modified</th>\n";
  81.     echo "<th>Copies</th>\n";
  82.     echo "<th>Actions</th>\n";
  83.     echo "</tr>\n";
  84.  
  85.     if ($ext != "") {
  86.         $meh = split("/",$ext);
  87.         $c = (count($meh) - 1);
  88.         $meh2 = "";
  89.         for ($i = 0; $i < $c; $i++) {
  90.             $meh2 .= $meh[$i]."/";
  91.         }
  92.         $meh2 = rtrim($meh2,"/");
  93.         echo "<tr>\n";
  94.         echo "<td><a href=\"?path=".rawurlencode($meh2)."\">Parent Directory</a></td>\n";
  95.         echo "<td> - </td>\n";
  96.         echo "<td> - </td>\n";
  97.         echo "<td> - </td>\n";
  98.         echo "<td> - </td>\n";
  99.         echo "</tr>\n";
  100.     }
  101.  
  102.     foreach ($drives as $d) {
  103.         $path = $d."/.gh_trash/".$ext;
  104.         if ($h = @opendir($path)) {
  105.             while (false !== ($e = readdir($h))) {
  106.             if ($e != "." && $e != "..") {
  107.                 $list[] = array($e,$d);
  108.             }
  109.             }
  110.         closedir($h);
  111.         }
  112.     }
  113.  
  114.     if (@count($list) > 0) {
  115.         foreach ($list as $l) {
  116.             if (@in_array($l[0],@$r)) {
  117.                 $q[$l[0]] = (@$q[$l[0]] + 1);
  118.             } else {
  119.                 $r[] = $l[0];
  120.                 // First drive is fine
  121.                 if (!isset($z[$l[0]])) {
  122.                     $z[$l[0]] = $l[1];
  123.                     }
  124.             }
  125.         }
  126.     }
  127.  
  128.     if (@count($r) > 0) {
  129.         foreach ($r as $k) {
  130.             echo "<tr>\n";
  131.             if (is_dir($z[$k]."/.gh_trash/".$ext."/".$k)) {
  132.                 echo "<td><a href=\"?path=".rawurlencode($ext."/".$k)."\">".$k."</a></td>\n";
  133.                 echo "<td>-</td>\n";
  134.                 echo "<td>-</td>\n";
  135.                 echo "<td>".(@$q[$k]+1)."</td>\n";
  136.             } else {
  137.                 $dr = (array_search($z[$k],$drives)+1);
  138.                 echo "<td><a href=\"?view=".rawurlencode($ext."/".$k)."&drive=".$dr."\">".$k."</a></td>\n";
  139.                 $fsize = filesize($z[$k]."/.gh_trash/".$ext."/".$k);
  140.                 echo "<td>".$fsize." bytes (".friendlyBytes($fsize).")</td>\n";
  141.                 $ftime = filemtime($z[$k]."/.gh_trash/".$ext."/".$k);
  142.                 echo "<td>".strftime("%m/%e/%Y %r",$ftime)."</td>\n";
  143.                 echo "<td>".(@$q[$k]+1)."</td>\n";
  144.             }
  145.             echo "<td>\n";
  146.             echo "<form method=\"GET\"><input type=\"hidden\" name=\"path\" value=\"".$ext."/".$k."\" /><input type=\"hidden\" name=\"action\" value=\"remove\" /><input type=\"submit\" value=\"Delete Permanently\" /></form>\n";
  147.             echo "<form method=\"GET\"><input type=\"hidden\" name=\"path\" value=\"".$ext."/".$k."\" /><input type=\"hidden\" name=\"action\" value=\"restore\" /><input type=\"submit\" value=\"Restore\" /></form>\n";
  148.                 echo "</td>\n";
  149.                 echo "</tr>";
  150.             }
  151.         }
  152.     echo "<tr>\n";
  153.     echo "<td colspan=\"3\" class=\"ral\">\n";
  154.     $space = getSpace(true);
  155.     echo "<strong>Disk Space Status:</strong><br />\n";
  156.     echo "<table class=\"ral\" width=\"100%\">\n";
  157.     echo "<tr><td>Total:</td><td>".$space['total']."</td></tr>\n";
  158.     echo "<tr><td>Used:</td><td>".$space['used']."</td></tr>\n";
  159.     echo "<tr><td>Available:</td><td>".$space['avail']."</td></tr>\n";
  160.     echo "<tr><td>Trash:</td><td>".$space['trash']."</td></tr>\n";
  161.     echo "<tr><td>Possible:</td><td>".$space['possible']."</td></tr>\n";
  162.     echo "</body>\n";
  163.     echo "</table>\n";
  164.     echo "</table>\n";
  165.     echo "</html>\n";
  166. } else {
  167.     if ($view != "" && $drive != "") {
  168.         $dpath = $drives[($drive-1)]."/.gh_trash";
  169.         $fpath = $dpath.$view;
  170.         $meh = split("/",$fpath);
  171.         $file = $meh[(count($meh)-1)];
  172.         $mime = mime_content_type($fpath);
  173.         http_send_content_disposition($file,true);
  174.         http_send_content_type($mime);
  175.         http_send_file($fpath);
  176.         exit();
  177.     }
  178.     if ($action == "remove" && $ext != "") {
  179.         if ($confirm != "Yes") {
  180.             echo "<h2>Removing ".$ext."</h2>\n";
  181.             if (is_dir($ext)) {
  182.                 echo "Are you sure you want to recursively destroy all copies of this directory, and the files within it?<br />\n";
  183.             } else {
  184.                 echo "Are you sure you want to destroy all copies of this file in the trash?<br />\n";
  185.             }
  186.             echo "This action is not reversable!<br />\n";
  187.             echo "<form method=\"GET\">\n";
  188.             echo "<input type=\"hidden\" name=\"path\" value=\"".$ext."\" />\n";
  189.             echo "<input type=\"hidden\" name=\"action\" value=\"remove\" />\n";
  190.             echo "<input type=\"submit\" name=\"confirm\" value=\"Yes\" />\n";
  191.             echo "<input type=\"button\" onclick=\"history.go(-1)\" value=\"No\" />\n";
  192.             echo "</form>\n";
  193.         } else {
  194.             foreach ($drives as $driv) {
  195.                                 $file = $driv."/.gh_trash".$ext;
  196.                                 if (is_dir($file)) {
  197.                                         rrmdir($file);
  198.                                         $removed[] = $file;
  199.                                 } else {
  200.                                         if (file_exists($file)) {
  201.                                                 unlink($file);
  202.                                                 $removed[] = $file;
  203.                                         }
  204.                                 }
  205.             }
  206.             if (count(@$removed) > 0) {
  207.                 echo "<h2>Removed ".$ext."</h2>\n";
  208.                 echo "The file has been removed from all Greyhole drives.<br />\n";
  209.                 echo "<br />\n";
  210.                 echo "Removed from:<br />\n";
  211.                 foreach ($removed as $r) {
  212.                     echo $r."<br />\n";
  213.                 }
  214.             } else {
  215.                 echo "No such file. Perhaps it was already removed, or you refreshed the page.<br />";
  216.             }
  217.             echo "<br />\n";
  218.             $meh = split("/",$ext);
  219.             $c = (count($meh) - 1);
  220.             $meh2 = "";
  221.             for ($i = 0; $i < $c; $i++) {
  222.                 $meh2 .= $meh[$i]."/";
  223.             }
  224.             $meh2 = rtrim($meh2,"/");
  225.             echo "<a href=\"?path=".$meh2."\">Back to directory</a>";
  226.         }
  227.     }
  228.     if ($action == "restore" && $ext != "") {
  229.         $dest = "/media/share".$ext;
  230.         foreach ($drives as $driv) {
  231.             $file = $driv."/.gh_trash".$ext;
  232.             if (file_exists($file)) {
  233.                 $sources[] = $file;
  234.             }
  235.         }
  236.  
  237.         $meh = split("/",$ext);
  238.         $c = (count($meh) - 1);
  239.         $meh2 = "";
  240.         for ($i = 0; $i < $c; $i++) {
  241.             $meh2 .= $meh[$i]."/";
  242.         }
  243.         $meh2 = rtrim($meh2,"/");
  244.  
  245.         // First one is fine
  246.         $source = @$sources[0];
  247.  
  248.         // Unless its an entire folder...
  249.         if (is_dir($source)) {
  250.             foreach ($sources as $s) {
  251.                 xcopy($s,$dest);
  252.             }
  253.         } elseif (file_exists($source)) {
  254.             if (!is_dir(dirname($dest))) {
  255.                 mkdir(dirname($dest),0770,true);
  256.             }
  257.             copy($source,$dest);
  258.         } else {
  259.             echo "No such file. If you restored the file, it has been purged from the trash. Did you refresh the page?<br />";
  260.         }
  261.  
  262.  
  263.         // Delete all copies from trash after restoring
  264.         if (file_exists($dest)) {
  265.             foreach ($sources as $s) {
  266.                 if (is_dir($s)) {
  267.                     rrmdir($s);
  268.                 }
  269.                 else {
  270.                     if (file_exists($s)) {
  271.                         unlink($s);
  272.                     }
  273.                 }
  274.             }
  275.         } else {
  276.             echo "Internal error: failed to restore file. Trash untouched.\n";
  277.             echo "<br />\n";
  278.             echo "<a href=\"?path=".$meh2."\">Back to directory</a>";
  279.             exit();
  280.         }
  281.         echo "<h2>Restored ".$ext."</h2>\n";
  282.         echo "The file has been restored to the share. It will no longer appear in the trash.<br />\n";
  283.         echo "<br />\n";
  284.         echo "<a href=\"?path=".$meh2."\">Back to directory</a>";
  285.     }
  286. }
  287.  
  288. function rrmdir($dir) {
  289.     if (is_dir($dir)) {
  290.         $objects = scandir($dir);
  291.         foreach ($objects as $object) {
  292.             if ($object != "." && $object != "..") {
  293.                 if (filetype($dir."/".$object) == "dir") {
  294.                     rrmdir($dir."/".$object);
  295.                 } else {
  296.                     unlink($dir."/".$object);
  297.                 }
  298.             }
  299.         }
  300.         reset($objects);
  301.         rmdir($dir);
  302.     }
  303. }
  304.  
  305. function xcopy($src,$dest) {
  306.     if (!is_dir($dest)) {
  307.         mkdir($dest);
  308.     }
  309.     foreach (scandir($src) as $file) {
  310.         if (!is_readable($src.'/'.$file)) continue;
  311.         if (is_dir($src.'/'.$file)) {
  312.             if (($file!='.') && ($file!='..')) {
  313.                 if(!is_dir($dest.'/'.$file)) {
  314.                     mkdir($dest . '/' . $file);
  315.                 }
  316.                 xcopy($src.'/'.$file, $dest.'/'.$file);
  317.             }
  318.         } else {
  319.             if (!file_exists($dest.'/'.$file)) {
  320.                 copy($src.'/'.$file, $dest.'/'.$file);
  321.             }
  322.         }
  323.      }
  324. }
  325.  
  326. function getSpace($friendlyBytes = false) {
  327.     $dat = `/usr/bin/greyhole-dfree`;
  328.     $dat = split(" ",$dat);
  329.     $space['total'] = $dat[0];
  330.     $space['avail'] = $dat[1];
  331.     $space['used'] = ($space['total'] - $space['avail']);
  332.     $space['trash'] = 0;
  333.     $drives = getDrives();
  334.     foreach ($drives as $d) {
  335.         $dudat = `du -sk $d/.gh_trash/`;
  336.         $space['trash'] = ($space['trash'] + $dudat);
  337.     }
  338.     $space['possible'] = ($space['avail'] + $space['trash']);
  339.     if ($friendlyBytes) {
  340.         foreach ($space as $k => $v) {
  341.             $space[$k] = friendlyBytes($v * 1024);
  342.         }
  343.     }
  344.     return $space;
  345. }
  346.  
  347. function getDrives() {
  348.     $gh = '/etc/greyhole.conf';
  349.     $dat = "";
  350.     $fp = fopen($gh,'r');
  351.     if (!$fp) die("Access to /etc/greyhole.conf denied");
  352.     while (!feof($fp)) {
  353.         $dat .= fread($fp,128);
  354.     }
  355.     fclose($fp);
  356.     $dat = split("\n",$dat);
  357.     foreach ($dat as $l) {
  358.         if (substr($l,0,10) == "storage_po") {
  359.             $m = split(" = ",$l);
  360.             $n = split(",",$m[1]);
  361.             $drives[] = $n[0];
  362.         }
  363.     }
  364.     return $drives;
  365. }
  366.  
  367. function friendlyBytes($size) {
  368.  
  369.     // Uncomment below to enable rounding to Terabytes.
  370.     // Leave commented to function as shown in example screenshots.
  371.  
  372. //  if ($size > 1099511627776) {
  373. //      return round(($size / 1099511627776),2)."TB";
  374. //  }
  375.  
  376.     if ($size > 1073741824) {
  377.         return round(($size / 1073741824),2)."GB";
  378.     }
  379.     if ($size > 1048576) {
  380.         return round(($size / 1048576),2)."MB";
  381.     }
  382.     if ($size > 1024) {
  383.         return round(($size / 1024),2)."KB";
  384.     }
  385.     return $size."B";
  386. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement