Example #1
0
function backup_jstree_list_dir($id, $path = '')
{
    //sanitize path
    $path = trim($path, '/');
    $path = str_replace(array('..', ':'), '', trim($path, '/'));
    $path = escapeshellcmd($path);
    $ret = array();
    $s = backup_get_server($id);
    if (!$s) {
        $ret[] = array('data' => _('error/not found!'));
    }
    switch ($s['type']) {
        case 'local':
            $s['path'] = backup__($s['path']);
            if (!is_dir($s['path'] . "/{$path}")) {
                return array();
            }
            $dir = scandir($s['path'] . "/{$path}");
            foreach ($dir as $file) {
                //keep out the dots!
                if (in_array($file, array('.', '..'))) {
                    continue;
                }
                //if this file is a directory, set it to be expandable
                if (is_dir($s['path'] . '/' . $path . '/' . $file)) {
                    $ret[] = array('attr' => array('data-path' => $path . '/' . $file), 'data' => $file, 'state' => 'closed');
                } else {
                    if (substr($file, -7) == '.tar.gz' || substr($file, -4) == '.tgz') {
                        $ret[] = array('attr' => array('data-manifest' => json_encode(backup_get_manifest_db($file)), 'data-path' => $path . '/' . $file), 'data' => array('title' => $file, 'icon' => 'noicon'));
                    }
                }
            }
            break;
        case 'ftp':
            //subsitute variables if nesesary
            $s['host'] = backup__($s['host']);
            $s['port'] = backup__($s['port']);
            $s['user'] = backup__($s['user']);
            $s['password'] = backup__($s['password']);
            $fstype = isset($s['fstype']) ? $s['fstype'] : 'auto';
            $connection = new Connection($s['host'], $s['user'], $s['password'], $s['port'], 90, $s['transfer'] == 'passive');
            try {
                $connection->open();
            } catch (\Exception $e) {
                $this->b['error'] = $e->getMessage();
                backup_log($this->b['error']);
                return;
            }
            $wrapper = new FTPWrapper($connection);
            $permFactory = new PermissionsFactory();
            switch ($fstype) {
                case 'auto':
                    $ftptype = $wrapper->systype();
                    if (strtolower($ftptype) == "unix") {
                        $fsFactory = new FilesystemFactory($permFactory);
                    } else {
                        $fsFactory = new WindowsFilesystemFactory();
                    }
                    break;
                case 'unix':
                    $fsFactory = new FilesystemFactory($permFactory);
                    break;
                case 'windows':
                    $fsFactory = new WindowsFilesystemFactory();
                    break;
            }
            $manager = new FTPFilesystemManager($wrapper, $fsFactory);
            $dlVoter = new DownloaderVoter();
            $ulVoter = new UploaderVoter();
            $ulVoter->addDefaultFTPUploaders($wrapper);
            $crVoter = new CreatorVoter();
            $crVoter->addDefaultFTPCreators($wrapper, $manager);
            $deVoter = new DeleterVoter();
            $deVoter->addDefaultFTPDeleters($wrapper, $manager);
            $ftp = new FTP($manager, $dlVoter, $ulVoter, $crVoter, $deVoter);
            if (!$ftp) {
                $this->b['error'] = _("Error creating the FTP object");
            }
            $ftpdirs = $ftp->findFilesystems(new Directory($s['path']));
            $ls = array();
            foreach ($ftpdirs as $thisdir) {
                $files = $ftp->findFilesystems(new Directory($thisdir->getRealPath()));
                foreach ($files as $f) {
                    $ls[] = $thisdir->getRealPath() . '/' . $f->getRealpath();
                }
            }
            foreach ($ls as $file) {
                $file = basename($file);
                //determine if we are a directory or not, rather than using rawlist
                if (substr($file, -7) == '.tar.gz' || substr($file, -4) == '.tgz') {
                    $ret[] = array('attr' => array('data-manifest' => json_encode(backup_get_manifest_db($file)), 'data-path' => $path . '/' . $file), 'data' => $file);
                }
            }
            //dbug('ftp ls', $ls);
            //dbug('ftp dir ' . $s['path'] . '/' . $path, $dir);
            //release handel
            break;
        case 'ssh':
            $s['path'] = backup__($s['path']);
            $s['user'] = backup__($s['user']);
            $s['host'] = backup__($s['host']);
            $cmd[] = 'ssh';
            //TODO: path shouldnt be hardocded
            $cmd[] = '-o StrictHostKeyChecking=no -i';
            $cmd[] = $s['key'];
            $cmd[] = $s['user'] . '\\@' . $s['host'];
            $cmd[] = '-p ' . $s['port'];
            $cmd[] = '"cd ' . $s['path'] . '/' . $path . ';';
            $cmd[] = 'find * -maxdepth 0 -type f -exec echo f:"{}" \\;;';
            $cmd[] = 'find * -maxdepth 0 -type d -exec echo d:"{}" \\;"';
            exec(implode(' ', $cmd), $ls);
            //dbug(implode(' ', $cmd), $ls);
            unset($cmd);
            foreach ($ls as $file) {
                $f = explode(':', $file);
                if ($f[0] == 'd') {
                    $ret[] = array('attr' => array('data-path' => $path . '/' . $f[1]), 'data' => $f[1], 'state' => 'closed');
                } elseif ($f[0] == 'f') {
                    if (substr($f[1], -7) == '.tar.gz' || substr($f[1], -4) == '.tgz') {
                        $ret[] = array('attr' => array('data-manifest' => json_encode(backup_get_manifest_db($f[1])), 'data-path' => $path . '/' . $f[1]), 'data' => $f[1]);
                    }
                }
            }
            break;
        case 'awss3':
            $s['bucket'] = backup__($s['bucket']);
            $s['awsaccesskey'] = backup__($s['awsaccesskey']);
            $s['awssecret'] = backup__($s['awssecret']);
            $awss3 = new S3($s['awsaccesskey'], $s['awssecret']);
            $contents = $awss3->getBucket($s['bucket']);
            foreach ($contents as $f) {
                if (substr($f['name'], -7) == '.tar.gz' || substr($f['name'], -4) == '.tgz') {
                    $ret[] = array('attr' => array('data-manifest' => json_encode(backup_get_manifest_db($f['name'])), 'data-path' => $path . '/' . $f['name']), 'data' => $f['name']);
                }
            }
            break;
    }
    return $ret;
}
Example #2
0
function backup_jstree_list_dir($id, $path = '')
{
    //sanitize path
    $path = trim($path, '/');
    $path = str_replace(array('..', ':'), '', trim($path, '/'));
    $path = escapeshellcmd($path);
    $ret = array();
    $s = backup_get_server($id);
    if (!$s) {
        $ret[] = array('data' => _('error/not found!'));
    }
    switch ($s['type']) {
        case 'local':
            $s['path'] = backup__($s['path']);
            if (!is_dir($s['path'] . "/{$path}")) {
                return array();
            }
            $dir = scandir($s['path'] . "/{$path}");
            foreach ($dir as $file) {
                //keep out the dots!
                if (in_array($file, array('.', '..'))) {
                    continue;
                }
                //if this file is a directory, set it to be expandable
                if (is_dir($s['path'] . '/' . $path . '/' . $file)) {
                    $ret[] = array('attr' => array('data-path' => $path . '/' . $file), 'data' => $file, 'state' => 'closed');
                } else {
                    if (substr($file, -7) == '.tar.gz' || substr($file, -4) == '.tgz') {
                        $ret[] = array('attr' => array('data-manifest' => json_encode(backup_get_manifest_db($file)), 'data-path' => $path . '/' . $file), 'data' => array('title' => $file, 'icon' => 'noicon'));
                    }
                }
            }
            break;
        case 'ftp':
            //subsitute variables if nesesary
            $s['host'] = backup__($s['host']);
            $s['port'] = backup__($s['port']);
            $s['user'] = backup__($s['user']);
            $s['password'] = backup__($s['password']);
            $s['path'] = backup__($s['path']);
            $path = trim($path, '/') . '/';
            $ftp = ftp_connect($s['host'], $s['port']);
            if (ftp_login($ftp, $s['user'], $s['password'])) {
                ftp_pasv($ftp, $s['transfer'] == 'passive');
                ftp_chdir($ftp, $s['path'] . '/' . $path);
                $pwd = ftp_pwd($ftp);
                $ls = ftp_nlist($ftp, '');
                $dir = ftp_rawlist($ftp, '-d1 */');
                foreach ($ls as $file) {
                    $file = basename($file);
                    //determine if we are a directory or not, rather than using rawlist
                    if (@ftp_chdir($ftp, $pwd . '/' . $file)) {
                        $ret[] = array('attr' => array('data-path' => $path . '/' . $file), 'data' => $file, 'state' => 'closed');
                    } else {
                        if (substr($file, -7) == '.tar.gz' || substr($file, -4) == '.tgz') {
                            $ret[] = array('attr' => array('data-manifest' => json_encode(backup_get_manifest_db($file)), 'data-path' => $path . '/' . $file), 'data' => $file);
                        }
                    }
                }
                //dbug('ftp ls', $ls);
                //dbug('ftp dir ' . $s['path'] . '/' . $path, $dir);
                //release handel
                ftp_close($ftp);
            } else {
                $ret[] = array('data' => _('FTP Connection error!'));
                dbug('ftp connect error');
            }
            break;
        case 'ssh':
            $s['path'] = backup__($s['path']);
            $s['user'] = backup__($s['user']);
            $s['host'] = backup__($s['host']);
            $cmd[] = 'ssh';
            //TODO: path shouldnt be hardocded
            $cmd[] = '-o StrictHostKeyChecking=no -i';
            $cmd[] = $s['key'];
            $cmd[] = $s['user'] . '\\@' . $s['host'];
            $cmd[] = '-p ' . $s['port'];
            $cmd[] = '"cd ' . $s['path'] . '/' . $path . ';';
            $cmd[] = 'find * -maxdepth 0 -type f -exec echo f:"{}" \\;;';
            $cmd[] = 'find * -maxdepth 0 -type d -exec echo d:"{}" \\;"';
            exec(implode(' ', $cmd), $ls);
            //dbug(implode(' ', $cmd), $ls);
            unset($cmd);
            foreach ($ls as $file) {
                $f = explode(':', $file);
                if ($f[0] == 'd') {
                    $ret[] = array('attr' => array('data-path' => $path . '/' . $f[1]), 'data' => $f[1], 'state' => 'closed');
                } elseif ($f[0] == 'f') {
                    if (substr($f[1], -7) == '.tar.gz' || substr($f[1], -4) == '.tgz') {
                        $ret[] = array('attr' => array('data-manifest' => json_encode(backup_get_manifest_db($f[1])), 'data-path' => $path . '/' . $f[1]), 'data' => $f[1]);
                    }
                }
            }
            break;
        case 'awss3':
            $s['bucket'] = backup__($s['bucket']);
            $s['awsaccesskey'] = backup__($s['awsaccesskey']);
            $s['awssecret'] = backup__($s['awssecret']);
            $awss3 = new S3($s['awsaccesskey'], $s['awssecret']);
            $contents = $awss3->getBucket($s['bucket']);
            foreach ($contents as $f) {
                if (substr($f['name'], -7) == '.tar.gz' || substr($f['name'], -4) == '.tgz') {
                    $ret[] = array('attr' => array('data-manifest' => json_encode(backup_get_manifest_db($f['name'])), 'data-path' => $path . '/' . $f['name']), 'data' => $f['name']);
                }
            }
            break;
    }
    return $ret;
}