<commit_before><?php
/**
 * Copyright (c) 2012 <NAME> <<EMAIL>>
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 * See the COPYING-README file.
 */
namespace OC\Files\Storage;

set_include_path(get_include_path() . PATH_SEPARATOR . OC_App::getAppPath('files_external') . '/3rdparty/phpseclib/phpseclib');
require('Net/SFTP.php');

class SFTP extends OC\Files\Storage\Common {
	private $host;
	private $user;
	private $password;
	private $root;

	private $client;

	private static $tempFiles = array();

	public function __construct($params) {
		$this->host = $params['host'];
		$proto = strpos($this->host, '://');
		if ($proto != false) {
			$this->host = substr($this->host, $proto+3);
		}
		$this->user = $params['user'];
		$this->password = $params['password'];
		$this->root = isset($params['root']) ? $this->cleanPath($params['root']) : '/';
		if ($this->root[0] != '/') $this->root = '/' . $this->root;
		if (substr($this->root, -1, 1) != '/') $this->root .= '/';
		
		$host_keys = $this->read_host_keys();

		$this->client = new Net_SFTP($this->host);
		if (!$this->client->login($this->user, $this->password)) {
			throw new Exception('Login failed');
		}

		$current_host_key = $this->client->getServerPublicHostKey();

		if (array_key_exists($this->host, $host_keys)) {
			if ($host_keys[$this->host] != $current_host_key) {
				throw new Exception('Host public key does not match known key');
			}
		} else {
			$host_keys[$this->host] = $current_host_key;
			$this->write_host_keys($host_keys);
		}
	}
	
	public function test() {
		if (!isset($params['host']) || !isset($params['user']) || !isset($params['password'])) {
			throw new Exception("Required parameters not set");
		}	
	}

	private function abs_path($path) {
		return $this->root . $path;
	}

	private function host_keys_path() {
		try {
			$storage_view = \OCP\Files::getStorage('files_external');
			if ($storage_view) {
				return \OCP\Config::getSystemValue('datadirectory') .
					$storage_view->getAbsolutePath('') .
					'ssh_host_keys';
			}
		} catch (Exception $e) {
		}
		return false;
	}

	private function write_host_keys($keys) {
		try {
			$key_path = $this->host_keys_path();
			$fp = fopen($key_path, 'w');
			foreach ($keys as $host => $key) {
				fwrite($fp, $host . '::' . $key . "\n");
			}
			fclose($fp);
			return true;
		} catch (Exception $e) {
			return false;<commit_msg>Security bug<commit_after><?php
/**
 * Copyright (c) 2012 <NAME> <<EMAIL>>
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 * See the COPYING-README file.
 */
namespace OC\Files\Storage;

set_include_path(get_include_path() . PATH_SEPARATOR . OC_App::getAppPath('files_external') . '/3rdparty/phpseclib/phpseclib');
require('Net/SFTP.php');

class SFTP extends OC\Files\Storage\Common {
	private $host;
	private $user;
	private $password;
	private $root;

	private $client;

	private static $tempFiles = array();

	public function __construct($params) {
		$this->host = $params['host'];
		$proto = strpos($this->host, '://');
		if ($proto != false) {
			$this->host = substr($this->host, $proto+3);
		}
		$this->user = $params['user'];
		$this->password = $params['password'];
		$this->root = isset($params['root']) ? $this->cleanPath($params['root']) : '/';
		if ($this->root[0] != '/') $this->root = '/' . $this->root;
		if (substr($this->root, -1, 1) != '/') $this->root .= '/';
		
		$host_keys = $this->read_host_keys();

		$this->client = new Net_SFTP($this->host);
		if (!$this->client->login($this->user, $this->password)) {
			throw new Exception('Login failed');
		}

		$current_host_key = $this->client->getServerPublicHostKey();

		if (array_key_exists($this->host, $host_keys)) {
			if ($host_keys[$this->host] != $current_host_key) {
				throw new Exception('Host public key does not match known key');
			}
		} else {
			$host_keys[$this->host] = $current_host_key;
			$this->write_host_keys($host_keys);
		}
	}
	
	public function test() {
		if (!isset($params['host']) || !isset($params['user']) || !isset($params['password'])) {
			throw new Exception("Required parameters not set");
		}	
	}

	private function abs_path($path) {
		return $this->root . $this->cleanPath($path);
	}

	private function host_keys_path() {
		try {
			$storage_view = \OCP\Files::getStorage('files_external');
			if ($storage_view) {
				return \OCP\Config::getSystemValue('datadirectory') .
					$storage_view->getAbsolutePath('') .
					'ssh_host_keys';
			}
		} catch (Exception $e) {
		}
		return false;
	}

	private function write_host_keys($keys) {
		try {
			$key_path = $this->host_keys_path();
			$fp = fopen($key_path, 'w');
			foreach ($keys as $host => $key) {
				fwrite($fp, $host . '::' . $key . "\n");
			}
			fclose($fp);
			return true;
		} catch (Exception $e) {
			return false;
{'fraction_non_alphanumeric': 0.19711042311661506, 'fraction_numerical': 0.0041279669762641896, 'mean_word_length': 3.990731204943357, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 70, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6937455', 'n_tokens_mistral': 1879, 'n_tokens_neox': 1683, 'n_words': 348}