Location: PHPKode > scripts > WebNight Commander > webnight-commander-v2/source/includes/12_class_dir.php
<?php

class dir {

    protected $full_path;
    protected $is_readable;
    protected $is_writable;
    protected $is_executable;
    protected $size = 0;
    protected $modified;
    protected $content = array();

    public function __get($property) {
        if (property_exists($this, $property)) {
            eval("\$return = \$this->$property;");
            return $return;
        } else
            return;
    }

    public function __construct($path) {

        $this->full_path = $path;
        $this->is_readable = is_readable($path);
        $this->is_writable = is_writable($path);
        $this->is_executable = is_executable($path);
        $this->modified = @filectime($path);
        $this->size = 0;

        if ($this->is_readable && $this->is_executable) {
            $dir = getcwd();
            chdir($path);
            $this->full_path = $path = getcwd();
            chdir($dir);

            $this->is_readable = is_readable($path);
            $this->is_writable = is_writable($path);
            $this->is_executable = is_executable($path);
            $this->modified = filectime($path);

            $files = $dirs = array();

            foreach (glob("$path/*") as $item) {
                $element = $this->get_file_info($item);
                if (($element['type'] == "l") && (trim(`whoami`) == $element['owner']))
                    $element['readable'] = $element['writable'] = 1;
                if (($element['type'] == "d") ||
                    (($element['type'] == "l") && ($element['target_info']['type'] == "d"))
                )
                    $dirs[$element['name']] = $element;
                else {
                    $files[$element['name']] = $element;
                    $this->size += $element['size'];
                }
            }

            ksort($dirs);
            ksort($files);

            $parent = $this->get_file_info(dirname($path));
            $parent['name'] = "..";
            $this->content = array_values(array($parent) + $dirs + $files);
        }
    }

    public function search_hash($hash) {
        foreach ($this->content as $i => $item)
            if ($hash == $item['hash'])
                return $i;
        return false;
    }

    public function refresh() {
        $this->__construct($this->full_path);
    }

    public function rrmdir($path) {
        if (preg_match('/^\d+$/', $path)) {
            if (!$path || !isset($this->content[$path]) || ($this->content[$path]['type'] != "d"))
                return false;

            $path = $this->full_path . "/" . $this->content[$path]['name'];

        } elseif (!is_dir($path))
            return false;

        $files = glob("$path/*");
        if ($files === false)
            return false;

        foreach ($files as $file)
            if (is_dir($file)) {
                if (!$this->rrmdir($file))
                    return false;
            } elseif (!@unlink($file))
                return false;

        return @rmdir($path);
    }

    protected function get_file_info($filename) {
        $info = array(
            'name' => basename($filename),
            'size' => filesize($filename),
            'modified' => filectime($filename),
            'readable' => is_readable($filename),
            'writable' => is_writable($filename),
            'executable' => is_executable($filename),
            'perms' => self::get_perms($filename),
            'owner' => self::get_owner($filename),
            'group' => self::get_group($filename),
            'hash' => md5(basename($filename))
        );
        $info['type'] = substr($info['perms'], 0, 1);

        if (is_link($filename) && ($info['type'] != "l")) { // Wrong Link Stat
            $info['type'] = "l";
            $info['perms'] = "lrwxr-xr-x";
            $shellname = str_replace('"', "\\\"", $filename);
            $stat = trim(`stat "$shellname"`);

            // Linux stat
            $regex = '/access\:[^\d]*\d{4}\/([a-z\-]{10,}).*uid\:\s*\(?\s*\d*\s*\/?\s*([^\)]+).*gid\:\s*\(?\s*\d*\s*\/?\s*([^\)]+)/i';
            if (preg_match($regex, $stat, $patt)) {
                $info['perms'] = $patt[1];
                $info['owner'] = $patt[2];
                $info['group'] = $patt[3];

            // BSD stat
            } else {
                $regex = '/^\d+\s+\d+\s+([a-z\-]{10,})\s+\d+\s+([a-z\-]+)\s+([a-z\-]+).*$/';
                if (preg_match($regex, $stat, $patt)) {
                    $info['perms'] = $patt[1];
                    $info['owner'] = $patt[2];
                    $info['group'] = $patt[3];
                }
            }

            $target = self::resolve_link($filename);
            $info['target'] = $target;
            $info['target_info'] = $this->get_file_info($target);
        }

        if (($info['type'] == "d") || ($info['type'] == "l"))
            $info['size'] = 0;

        return $info;
    }

    static function rcopy($src, $dst) {

        if (is_dir($src)) {
            if (file_exists($dst))
                return "File \"$dst\" already exists!";
            if (!@mkdir($dst))
                return "Cannot create \"$dst\" directory!";
            $items = glob("$src/*");
            if ($items === false)
                return "Cannot access \"$src\" directory";
            $return = "";
            foreach ($items as $item) {
                $res = self::rcopy($item, "$dst/" . basename($item));
                if ($res !== true)
                    $return .= "<br />$res";
            }
        } elseif (file_exists($dst))
            return "File \"$dst\" already exists!";
        elseif (!@copy($src, $dst))
            return "Error copying \"$src\" file";

        return (isset($return) && strlen($return))
            ? substr($return, 6)
            : true;
    }

    static function resolve_link($filename) {
        $target = $filename;

        do {
            $link = readlink($target);

            if ($link == ".")
                $link = dirname($target);
            elseif ($link == "..")
                $link = dirname(dirname($target));
            elseif (substr($link, 0, 2) == "./")
                $link = dirname($target) . substr($link, 1);
            elseif (substr($link, 0, 3) == "../")
                $link = dirname(dirname($target)) . substr($link, 2);
            elseif (substr($link, 0, 1) != "/")
                $link = dirname($target) . ((dirname($target) == "/") ? "" : "/") . $link;

            $target = $link;
        } while (is_link($link));

        return $link;
    }

    static function get_perms($filename) {
        $prm = fileperms($filename);

        if (($prm & 0xC000) == 0xC000)      // Socket
            $perms = 's';
        elseif (($prm & 0xA000) == 0xA000)  // Symbolic Link
            $perms = 'l';
        elseif (($prm & 0x8000) == 0x8000)  // Regular
            $perms = '-';
        elseif (($prm & 0x6000) == 0x6000)  // Block special
            $perms = 'b';
        elseif (($prm & 0x4000) == 0x4000)  // Directory
            $perms = 'd';
        elseif (($prm & 0x2000) == 0x2000)  // Character special
            $perms = 'c';
        elseif (($prm & 0x1000) == 0x1000)  // FIFO pipe
            $perms = 'p';
        else                                // Unknown
            $perms = 'u';

        // Owner
        $perms .= ($prm & 0x0100) ? 'r' : '-';
        $perms .= ($prm & 0x0080) ? 'w' : '-';
        $perms .= ($prm & 0x0040)
            ? (($prm & 0x0800) ? 's' : 'x')
            : (($prm & 0x0800) ? 'S' : '-');

        // Group
        $perms .= ($prm & 0x0020) ? 'r' : '-';
        $perms .= ($prm & 0x0010) ? 'w' : '-';
        $perms .= ($prm & 0x0008)
            ? (($prm & 0x0400) ? 's' : 'x')
            : (($prm & 0x0400) ? 'S' : '-');

        // World
        $perms .= ($prm & 0x0004) ? 'r' : '-';
        $perms .= ($prm & 0x0002) ? 'w' : '-';
        $perms .= ($prm & 0x0001)
            ? (($prm & 0x0200) ? 't' : 'x')
            : (($prm & 0x0200) ? 'T' : '-');

        return $perms;
    }

    static function get_owner($filename) {
        $pwuid = posix_getpwuid(fileowner($filename));
        return $pwuid['name'];
    }

    static function get_group($filename) {
        $grgid = posix_getgrgid(filegroup($filename));
        return $grgid['name'];
    }

    static function human_size($size) {
        if ($size < 1024) {
            $size = "$size";
        } elseif (($size < 1048576)) {
            $size /= 1024;
            $size = intval($size) . "K";
        } elseif ($size < 1073741824) {
            $size /= 1048576;
            $size = intval($size) . "M";
        } elseif ($size < 1099511627776) {
            $size /= 1073741824;
            $size = intval($size) . "G";
        } else {
            $size /= 1099511627776;
            $size = intval($size) . "T";
        }
        return $size;
    }

}

?>
Return current item: WebNight Commander