/** * Class constructor * * @param resource|string $stream Stream or URL * @param array $options */ public function __construct($stream, $options) { if (!is_resource($stream)) { $this->url = $stream; $context = array_chunk_assoc($options, 'context'); if (!empty($context)) { $this->context = stream_context_create($context); } $stream = fopen($stream, 'rw', null, $this->context); } else { $meta = stream_get_meta_data($stream); $this->url = $meta['url']; } $this->resource = $stream; }
/** * Class constructor. * * @param object|resource $native * @param array $settings Settings used to create connection */ public function __construct($native, $settings = array()) { $this->native = $native; if (isset($settings)) { $this->settings = $settings; } $config = array_chunk_assoc($this->settings, 'config'); $this->metadata = $config instanceof Config ? $config : Config::with($config, array('mapkey' => array('table_def' => "'#table'", 'field' => '@name', 'alias' => "'#alias:'.@name"))); if (isset($settings['metadata-cache'])) { $this->metadataCache = $settings['metadata-cache']; } if (isset($settings['log']) && load_class('Q\\Log')) { $this->log = $settings['log'] instanceof Logger ? $settings['log'] : Log::with($settings['log']); } elseif (class_exists('Q\\Log') && Log::db()->exists()) { $this->log = Log::db(); } if (isset($settings['log-columns'])) { $this->logColumns = is_string($settings['log-columns']) ? split_set(',', $settings['log-columns']) : (array) $settings['log-columns']; } }
/** * Create SSH connection */ protected function makeConnection() { $host = $this->host; // Extract from hostname $matches = null; if (!preg_match('/^(?:([^:@]++)(?:\\:([^:@]++))?@)?([^:@]++)(?:\\:([^:@]++))?$/', $host, $matches)) { throw new Exception("Could not create SSH connection: Illegal host string."); } $matches = $matches + array_fill(0, 5, null); list(, $username, $password, $host, $port) = $matches; // Get user/password if (!empty($this->options->username)) { $username = $this->options->username; } if (empty($username)) { $_tmp_ = posix_getpwuid(posix_getuid()); $username = $_tmp_['name']; } if (!empty($this->options->password)) { $password = $this->options->password; } // Get port $port = !empty($this->options->port) ? $this->options->port : 22; if (isset($port) && !is_int($port) && !ctype_digit($port)) { throw new Exception("Could not create SSH connection for '{$host}': Given port '{$port}' is not a numeric value."); } // Get methods and callbacks if (!isset($this->options->methods)) { $this->options->methods = array_chunk_assoc($this->options, 'methods'); } if (!isset($this->options->callbacks)) { $this->options->callbacks = array_chunk_assoc($this->options, 'callbacks'); } // Make the connection $this->connection = ssh2_connect($host, $port, $this->options->methods, $this->options->callbacks); if (!$this->connection) { throw new Exception("Could not create SSH connection for '{$host}:{$port}': Failed to connect to server."); } // Autenticate $auth_methods = isset($this->options->auth) ? (array) $this->options->auth : ssh2_auth_none($this->connection, $username); $authenticated = $auth_methods === true; while (!$authenticated && current($auth_methods)) { switch (current($auth_methods)) { case 'none': $authenticated = @ssh2_auth_none($this->connection, $username) === true; break; case 'password': $authenticated = isset($password) && @ssh2_auth_password($this->connection, $username, $password); break; case 'publickey': $authenticated = @ssh2_auth_pubkey_file($this->connection, $username, $this->options->pubkeyfile, $this->options->privkeyfile, $this->options->passphrase); break; case 'hostbased': $authenticated = @ssh2_auth_hostbased_file($this->connection, $username, $this->options->hostbased, $this->options->pubkeyfile, $this->options->privkeyfile, $this->options->passphrase); break; } next($auth_methods); } if (!$authenticated) { throw new Exception("Could not create SSH connection for '{$host}:{$port}': Authentication for user '{$username}' failed (" . join(', ', $auth_methods) . ")."); } }
/** * Get AUTH info from session data. */ protected function initInfo() { if (is_string($this->store)) { $this->store = extract_dsn($this->store); } switch ($this->store['driver']) { case 'none': $this->info = null; break; case 'session': session_start(); $this->info = isset($_SESSION['AUTH']) ? $_SESSION['AUTH'] : null; break; case 'cookie': $this->info = array_chunk_assoc($_COOKIE, 'AUTH', '_'); break; case 'request': $this->info = isset($_REQUEST['AUTH']) ? $_REQUEST['AUTH'] : null; break; case 'env': $this->info = split_set(';', unquote(getenv('AUTH'))); break; case 'http': $this->info = getenv('REMOTE_USER') ? array('username' => getenv('REMOTE_USER')) : null; break; case 'posix': $this->info = array('uid' => posix_getuid()); break; case 'posix_username': $this->info = array('username' => posix_getlogin()); break; default: throw new Exception("Invalid option '{$this->store['driver']}' specified for retrieving info."); } }