<commit_before><?php
/**
 * Copyright (c) 2014 <NAME> <<EMAIL>>
 * This file is licensed under the Licensed under the MIT license:
 * http://opensource.org/licenses/MIT
 */

namespace Icewind\SMB\Native;

use Icewind\SMB\AbstractShare;
use Icewind\SMB\Exception\DependencyException;
use Icewind\SMB\Exception\InvalidPathException;
use Icewind\SMB\Exception\InvalidResourceException;
use Icewind\SMB\INotifyHandler;
use Icewind\SMB\IServer;
use Icewind\SMB\Wrapped\Server;
use Icewind\SMB\Wrapped\Share;

class NativeShare extends AbstractShare {
	/**
	 * @var IServer $server
	 */
	private $server;

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

	/**
	 * @var NativeState $state
	 */
	private $state;

	/**
	 * @param IServer $server
	 * @param string $name
	 */
	public function __construct($server, $name) {
		parent::__construct();
		$this->server = $server;
		$this->name = $name;
	}

	/**
	 * @throws \Icewind\SMB\Exception\ConnectionException
	 * @throws \Icewind\SMB\Exception\AuthenticationException
	 * @throws \Icewind\SMB\Exception\InvalidHostException
	 */
	protected function getState() {
		if ($this->state and $this->state instanceof NativeState) {
			return $this->state;
		}

		$this->state = new NativeState();
		$this->state->init($this->server->getAuth(), $this->server->getOptions());
		return $this->state;
	}

	/**
	 * Get the name of the share
	 *
	 * @return string
	 */
	public function getName() {
		return $this->name;
	}

	private function buildUrl($path) {
		$this->verifyPath($path);
		$url = sprintf('smb://%s/%s', $this->server->getHost(), $this->name);
		if ($path) {
			$path = trim($path, '/');
			$url .= '/';
			$url .= implode('/', array_map('rawurlencode', explode('/', $path)));
		}
		return $url;
	}

	/**
	 * List the content of a remote folder
	 *
	 * @param string $path
	 * @return \Icewind\SMB\IFileInfo[]
	 *
	 * @throws \Icewind\SMB\Exception\NotFoundException
	 * @throws \Icewind\SMB\Exception\InvalidTypeException
	 */
	public function dir($path) {
		$files = [];

		$dh = $this->getState()->opendir($this->buildUrl($path));
		while ($file = $this->getState()->readdir($dh)) {
			$name = $file['name'];
			if ($name !== '.' and $name !== '..') {
				$fullPath = $path . '/' . $name;
				$files [] = new NativeFileInfo($this, $fullPath, $name, function () use ($fullPath) {
					return $this->getStat($fullPath);
				});
			}
		}

		$this->getState()->closedir($dh);
		return $files;
	}

	/**
	 * @param string $path
	 * @return \Icewind\SMB\IFileInfo
	 */
	public function stat($path) {
		return new NativeFileInfo($this, $path, self::mb_basename($path), $this->getStat($path));
	}

    /**
     * Multibyte unicode safe version of basename()
     *
     * @link http://php.net/manual/en/function.basename.php#121405
     */
    protected static function mb_basename($path): string
    {
        if (preg_match('@^.*[\\\\/]([^\\\\/]+)$@s', $path, $matches)) {
            return $matches[1];
        } else if (preg_match('@^([^\\\\/]+)$@s', $path, $matches)) {
            return $matches[1];
        }

        return '';
    }

	private function getStat($path) {
		return $this->getState()->stat($this->buildUrl($path));
	}<commit_msg>php5 compatibility<commit_after><?php
/**
 * Copyright (c) 2014 <NAME> <<EMAIL>>
 * This file is licensed under the Licensed under the MIT license:
 * http://opensource.org/licenses/MIT
 */

namespace Icewind\SMB\Native;

use Icewind\SMB\AbstractShare;
use Icewind\SMB\Exception\DependencyException;
use Icewind\SMB\Exception\InvalidPathException;
use Icewind\SMB\Exception\InvalidResourceException;
use Icewind\SMB\INotifyHandler;
use Icewind\SMB\IServer;
use Icewind\SMB\Wrapped\Server;
use Icewind\SMB\Wrapped\Share;

class NativeShare extends AbstractShare {
	/**
	 * @var IServer $server
	 */
	private $server;

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

	/**
	 * @var NativeState $state
	 */
	private $state;

	/**
	 * @param IServer $server
	 * @param string $name
	 */
	public function __construct($server, $name) {
		parent::__construct();
		$this->server = $server;
		$this->name = $name;
	}

	/**
	 * @throws \Icewind\SMB\Exception\ConnectionException
	 * @throws \Icewind\SMB\Exception\AuthenticationException
	 * @throws \Icewind\SMB\Exception\InvalidHostException
	 */
	protected function getState() {
		if ($this->state and $this->state instanceof NativeState) {
			return $this->state;
		}

		$this->state = new NativeState();
		$this->state->init($this->server->getAuth(), $this->server->getOptions());
		return $this->state;
	}

	/**
	 * Get the name of the share
	 *
	 * @return string
	 */
	public function getName() {
		return $this->name;
	}

	private function buildUrl($path) {
		$this->verifyPath($path);
		$url = sprintf('smb://%s/%s', $this->server->getHost(), $this->name);
		if ($path) {
			$path = trim($path, '/');
			$url .= '/';
			$url .= implode('/', array_map('rawurlencode', explode('/', $path)));
		}
		return $url;
	}

	/**
	 * List the content of a remote folder
	 *
	 * @param string $path
	 * @return \Icewind\SMB\IFileInfo[]
	 *
	 * @throws \Icewind\SMB\Exception\NotFoundException
	 * @throws \Icewind\SMB\Exception\InvalidTypeException
	 */
	public function dir($path) {
		$files = [];

		$dh = $this->getState()->opendir($this->buildUrl($path));
		while ($file = $this->getState()->readdir($dh)) {
			$name = $file['name'];
			if ($name !== '.' and $name !== '..') {
				$fullPath = $path . '/' . $name;
				$files [] = new NativeFileInfo($this, $fullPath, $name, function () use ($fullPath) {
					return $this->getStat($fullPath);
				});
			}
		}

		$this->getState()->closedir($dh);
		return $files;
	}

	/**
	 * @param string $path
	 * @return \Icewind\SMB\IFileInfo
	 */
	public function stat($path) {
		return new NativeFileInfo($this, $path, self::mb_basename($path), $this->getStat($path));
	}

	/**
	 * Multibyte unicode safe version of basename()
	 *
	 * @param string $path
	 * @link http://php.net/manual/en/function.basename.php#121405
	 * @return string
	 */
	protected static function mb_basename($path) {
		if (preg_match('@^.*[\\\\/]([^\\\\/]+)$@s', $path, $matches)) {
			return $matches[1];
		} elseif (preg_match('@^([^\\\\/]+)$@s', $path, $matches)) {
			return $matches[1];
		}

		return '';
	}

	private function getStat($path) {
		return $this->getState()->stat($this->buildUrl($path));
	}
{'fraction_non_alphanumeric': 0.19282793331236237, 'fraction_numerical': 0.003932054105064486, 'mean_word_length': 3.4593267882187937, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 69, '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': '1432086', 'n_tokens_mistral': 2369, 'n_tokens_neox': 2134, 'n_words': 444}