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

namespace Icewind\SMB;

class Share {
	/**
	 * @var Server $server
	 */
	private $server;

	/**
	 * @var string $name
	 */
	private $name;

	/**
	 * @var Connection $connection
	 */
	private $connection;

	private $serverTimezone;

	/**
	 * @param Server $server
	 * @param string $name
	 * @throws ConnectionError
	 */
	public function __construct($server, $name) {
		$this->server = $server;
		$this->name = $name;
	}

	public function connect() {
		if ($this->connection and $this->connection->isValid()) {
			return;
		}
		$command = Server::CLIENT . ' -U ' . escapeshellarg($this->server->getUser()) .
			' //' . $this->server->getHost() . '/' . $this->name;
		$this->connection = new Connection($command);
		$this->connection->write($this->server->getPassword());
		$this->connection->readLine(); // discard password prompt
		if (!$this->connection->isValid()) {
			throw new ConnectionError();
		}
	}

	public function getName() {
		return $this->name;
	}

	protected function simpleCommand($command, $path) {
		$path = $this->escapePath($path);
		$cmd = $command . ' ' . $path;
		$output = $this->execute($cmd);
		return $this->parseOutput($output);
	}

	private function getServerTimeZone() {
		if (!$this->serverTimezone) {
			$this->serverTimezone = $this->server->getTimeZone();
		}
		return $this->serverTimezone;
	}

	/**
	 * List the content of a remote folder
	 *
	 * @param $path
	 * @return array
	 */
	public function dir($path) {
		$path = $this->escapePath($path);
		$output = $this->execute('cd ' . $path);
		//check output for errors
		$this->parseOutput($output);
		$output = $this->execute('dir');
		$this->execute('cd /');

		//last line is used space
		array_pop($output);
		$regex = '/^\s*(.*?)\s\s\s\s+(?:([NDHARS]*)\s+)?([0-9]+)\s+(.*)$/';
		//2 spaces, filename, optional type, size, date<commit_msg>Better phpdoc for Share::dir<commit_after><?php
/**
 * Copyright (c) 2013 <NAME> <<EMAIL>>
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 * See the COPYING-README file.
 */

namespace Icewind\SMB;

class Share {
	/**
	 * @var Server $server
	 */
	private $server;

	/**
	 * @var string $name
	 */
	private $name;

	/**
	 * @var Connection $connection
	 */
	private $connection;

	private $serverTimezone;

	/**
	 * @param Server $server
	 * @param string $name
	 * @throws ConnectionError
	 */
	public function __construct($server, $name) {
		$this->server = $server;
		$this->name = $name;
	}

	public function connect() {
		if ($this->connection and $this->connection->isValid()) {
			return;
		}
		$command = Server::CLIENT . ' -U ' . escapeshellarg($this->server->getUser()) .
			' //' . $this->server->getHost() . '/' . $this->name;
		$this->connection = new Connection($command);
		$this->connection->write($this->server->getPassword());
		$this->connection->readLine(); // discard password prompt
		if (!$this->connection->isValid()) {
			throw new ConnectionError();
		}
	}

	public function getName() {
		return $this->name;
	}

	protected function simpleCommand($command, $path) {
		$path = $this->escapePath($path);
		$cmd = $command . ' ' . $path;
		$output = $this->execute($cmd);
		return $this->parseOutput($output);
	}

	private function getServerTimeZone() {
		if (!$this->serverTimezone) {
			$this->serverTimezone = $this->server->getTimeZone();
		}
		return $this->serverTimezone;
	}

	/**
	 * List the content of a remote folder
	 *
	 * Returns a nested array in the format of
	 * [
	 *    $name => [
	 *        'size' => $size,
	 *        'type' => $type,
	 *        'time' => $mtime
	 *    ],
	 *    ...
	 * ]
	 *
	 * @param $path
	 * @return array[]
	 */
	public function dir($path) {
		$path = $this->escapePath($path);
		$output = $this->execute('cd ' . $path);
		//check output for errors
		$this->parseOutput($output);
		$output = $this->execute('dir');
		$this->execute('cd /');

		//last line is used space
		array_pop($output);
		$regex = '/^\s*(.*?)\s\s\s\s+(?:([NDHARS]*)\s+)?([0-9]+)\s+(.*)$/';
		//2 spaces, filename, optional type, size, date
{'fraction_non_alphanumeric': 0.19764150943396228, 'fraction_numerical': 0.0037735849056603774, 'mean_word_length': 3.288169868554095, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 79, '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': '1491702', 'n_tokens_mistral': 1535, 'n_tokens_neox': 1394, 'n_words': 331}