Your IP : 216.73.216.48


Current Path : /var/www/codedit/models/
Upload File :
Current File : //var/www/codedit/models/FileSystem.php

<?php

namespace app\models;

use Yii;

class FileSystem {

    protected $host;
    protected $port;
    protected $username;
    protected $password;
    protected $connection;
    protected $sftp;
    protected $base = null;

    protected function real($path) {
        $temp = $path;
        if (!$temp) {
            throw new Exception('Path does not exist: ' . $path);
        }
        if ($this->base && strlen($this->base)) {
            if (strpos($temp, $this->base) !== 0) {
                throw new Exception('Path is not inside base (' . $this->base . '): ' . $temp);
            }
        }
        return $temp;
    }

    protected function path($id) {
        $id = $this->real($this->base . $id);
        return $id;
    }

    protected function id($path) {
        $path = $this->real($path);
        $path = substr($path, strlen($this->base));
        $path = str_replace('/', '/', $path);
        $path = trim($path, '/');
        return strlen($path) ? $path : '/';
    }

    public function __construct($host, $port, $username, $password, $type, $dir) {
        $this->host = $host;
        $this->port = $port;
        $this->username = $username;
        $this->password = $password;
        $this->type = $type;
        if ($dir) {
            $this->dir = $dir;
        } else {
            $this->dir = './';
        }

        if ($this->type == 'SFTP') {
            $this->connection = ssh2_connect($this->host, $this->port);
            ssh2_auth_password($this->connection, $this->username, $this->password);
            if ($this->sftp = ssh2_sftp($this->connection)) {
                $this->base = $this->real('ssh2.sftp://' . $this->sftp . '/' . $this->dir . '/');
            }
        } elseif ($this->type = 'FTP') {
            $this->base = $this->real('ftp://' . $this->username . ':' . $this->password . '@' . $this->host . ':' . $this->port . '/' . $this->dir . '/');
        } else {
            throw new Exception('Unsupported connection type');
        }
    }

    public function lst($id, $with_root = false) {
        $dir = $this->path($id);
        $lst = scandir($dir, 1);
        if (!$lst) {
            throw new Exception('Could not list path: ' . $dir);
        }
        $res = array();
        foreach ($lst as $item) {
            if ($item == '.' || $item == '..' || $item === null) {
                continue;
            }
            $tmp = preg_match('([^ a-zа-я-_0-9.]+)ui', $item);
            if ($tmp === false || $tmp === 1) {
                continue;
            }
            if (is_dir($dir . '/' . $item)) {
                $res[] = array(
                    'text' => $item,
                    'children' => true,
                    'id' => $this->id($dir . '/' . $item),
                    'icon' => 'folder'
                );
            } else {
                $res[] = array(
                    'text' => $item,
                    'children' => false,
                    'id' => $this->id($dir . '/' . $item),
                    'type' => 'file',
                    'icon' => 'file file-' . substr($item, strrpos($item, '.') + 1)
                );
            }
        }
        if ($with_root && $this->id($dir) === '/') {
            $res = array(
                array(
                    'text' => basename($this->base),
                    'children' => $res,
                    'id' => '/',
                    'icon' => 'folder',
                    'state' => array(
                        'opened' => true,
                        'disabled' => true
                    )
                )
            );
        }
        return $res;
    }

    public function data($id) {
        if (strpos($id, ":")) {
            $id = array_map(array($this, 'id'), explode(':', $id));
            return array('type' => 'multiple', 'content' => 'Multiple selected: ' . implode(' ', $id));
        }
        $dir = $this->path($id);
        if (is_dir($dir)) {
            return array('type' => 'folder', 'content' => $id);
        }
        if (is_file($dir)) {
            $ext = strpos($dir, '.') !== FALSE ? substr($dir, strrpos($dir, '.') + 1) : '';
            $dat = array('type' => $ext, 'content' => '');
            switch ($ext) {
                case 'txt':
                case 'text':
                case 'md':
                case 'js':
                case 'json':
                case 'css':
                case 'html':
                case 'htm':
                case 'xml':
                case 'c':
                case 'cpp':
                case 'h':
                case 'sql':
                case 'log':
                case 'py':
                case 'rb':
                case 'htaccess':
                case 'php':
                    $dat['content'] = file_get_contents($dir);
                    break;
                case 'jpg':
                case 'jpeg':
                case 'gif':
                case 'png':
                case 'bmp':
                    $dat['content'] = 'data:' . finfo_file(finfo_open(FILEINFO_MIME_TYPE), $dir) . ';base64,' . base64_encode(file_get_contents($dir));
                    break;
                default:
                    $dat['content'] = 'File not recognized: ' . $this->id($dir);
                    break;
            }
            return $dat;
        }
        throw new Exception('Not a valid selection: ' . $dir);
    }

    public function create($id, $name, $mkdir = false) {
        $dir = $this->path($id);
        if (preg_match('([^ a-zа-я-_0-9.]+)ui', $name) || !strlen($name)) {
            throw new Exception('Invalid name: ' . $name);
        }
        if ($mkdir) {
            mkdir($dir . '/' . $name);
        } else {
            file_put_contents($dir . '/' . $name, '');
        }
        return array('id' => $this->id($dir . '/' . $name));
    }

    public function rename($id, $name) {
        $dir = $this->path($id);
        if ($dir === $this->base) {
            throw new Exception('Cannot rename root');
        }
        if (preg_match('([^ a-zа-я-_0-9.]+)ui', $name) || !strlen($name)) {
            throw new Exception('Invalid name: ' . $name);
        }
        $new = explode('/', $dir);
        array_pop($new);
        array_push($new, $name);
        $new = implode('/', $new);
        if ($dir !== $new) {
            if (is_file($new) || is_dir($new)) {
                throw new Exception('Path already exists: ' . $new);
            }
            rename($dir, $new);
        }
        return array('id' => $this->id($new));
    }

    public function remove($id) {
        $dir = $this->path($id);
        if ($dir === $this->base) {
            throw new Exception('Cannot remove root');
        }
        if (is_dir($dir)) {
            foreach (array_diff(scandir($dir), array(".", "..")) as $f) {
                $this->remove($this->id($dir . '/' . $f));
            }
            rmdir($dir);
        }
        if (is_file($dir)) {
            unlink($dir);
        }
        return array('status' => 'OK');
    }

    public function move($id, $par) {
        $dir = $this->path($id);
        $par = $this->path($par);
        $new = explode('/', $dir);
        $new = array_pop($new);
        $new = $par . '/' . $new;
        rename($dir, $new);
        return array('id' => $this->id($new));
    }

    public function copy($id, $par) {
        $dir = $this->path($id);
        $par = $this->path($par);
        $new = explode('/', $dir);
        $new = array_pop($new);
        $new = $par . '/' . $new;
        if (is_file($new) || is_dir($new)) {
            throw new Exception('Path already exists: ' . $new);
        }

        if (is_dir($dir)) {
            mkdir($new);
            foreach (array_diff(scandir($dir), array(".", "..")) as $f) {
                $this->copy($this->id($dir . '/' . $f), $this->id($new));
            }
        }
        if (is_file($dir)) {
            copy($dir, $new);
        }
        return array('id' => $this->id($new));
    }

    public function edit($file, $content) {
        
    }

}