You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NativeShare.php 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. <?php
  2. /**
  3. * Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Licensed under the MIT license:
  5. * http://opensource.org/licenses/MIT
  6. */
  7. namespace Icewind\SMB\Native;
  8. use Icewind\SMB\AbstractShare;
  9. use Icewind\SMB\Exception\DependencyException;
  10. use Icewind\SMB\Exception\InvalidPathException;
  11. use Icewind\SMB\Exception\InvalidResourceException;
  12. use Icewind\SMB\INotifyHandler;
  13. use Icewind\SMB\IServer;
  14. use Icewind\SMB\Wrapped\Server;
  15. use Icewind\SMB\Wrapped\Share;
  16. class NativeShare extends AbstractShare {
  17. /**
  18. * @var IServer $server
  19. */
  20. private $server;
  21. /**
  22. * @var string $name
  23. */
  24. private $name;
  25. /**
  26. * @var NativeState $state
  27. */
  28. private $state;
  29. /**
  30. * @param IServer $server
  31. * @param string $name
  32. */
  33. public function __construct($server, $name) {
  34. parent::__construct();
  35. $this->server = $server;
  36. $this->name = $name;
  37. }
  38. /**
  39. * @throws \Icewind\SMB\Exception\ConnectionException
  40. * @throws \Icewind\SMB\Exception\AuthenticationException
  41. * @throws \Icewind\SMB\Exception\InvalidHostException
  42. */
  43. protected function getState() {
  44. if ($this->state and $this->state instanceof NativeState) {
  45. return $this->state;
  46. }
  47. $this->state = new NativeState();
  48. $this->state->init($this->server->getAuth(), $this->server->getOptions());
  49. return $this->state;
  50. }
  51. /**
  52. * Get the name of the share
  53. *
  54. * @return string
  55. */
  56. public function getName() {
  57. return $this->name;
  58. }
  59. private function buildUrl($path) {
  60. $this->verifyPath($path);
  61. $url = sprintf('smb://%s/%s', $this->server->getHost(), $this->name);
  62. if ($path) {
  63. $path = trim($path, '/');
  64. $url .= '/';
  65. $url .= implode('/', array_map('rawurlencode', explode('/', $path)));
  66. }
  67. return $url;
  68. }
  69. /**
  70. * List the content of a remote folder
  71. *
  72. * @param string $path
  73. * @return \Icewind\SMB\IFileInfo[]
  74. *
  75. * @throws \Icewind\SMB\Exception\NotFoundException
  76. * @throws \Icewind\SMB\Exception\InvalidTypeException
  77. */
  78. public function dir($path) {
  79. $files = [];
  80. $dh = $this->getState()->opendir($this->buildUrl($path));
  81. while ($file = $this->getState()->readdir($dh)) {
  82. $name = $file['name'];
  83. if ($name !== '.' and $name !== '..') {
  84. $fullPath = $path . '/' . $name;
  85. $files [] = new NativeFileInfo($this, $fullPath, $name);
  86. }
  87. }
  88. $this->getState()->closedir($dh);
  89. return $files;
  90. }
  91. /**
  92. * @param string $path
  93. * @return \Icewind\SMB\IFileInfo
  94. */
  95. public function stat($path) {
  96. $info = new NativeFileInfo($this, $path, self::mb_basename($path));
  97. // trigger attribute loading
  98. $info->getSize();
  99. return $info;
  100. }
  101. /**
  102. * Multibyte unicode safe version of basename()
  103. *
  104. * @param string $path
  105. * @link https://www.php.net/manual/en/function.basename.php#121405
  106. * @return string
  107. */
  108. protected static function mb_basename($path) {
  109. if (preg_match('@^.*[\\\\/]([^\\\\/]+)$@s', $path, $matches)) {
  110. return $matches[1];
  111. } elseif (preg_match('@^([^\\\\/]+)$@s', $path, $matches)) {
  112. return $matches[1];
  113. }
  114. return '';
  115. }
  116. /**
  117. * Create a folder on the share
  118. *
  119. * @param string $path
  120. * @return bool
  121. *
  122. * @throws \Icewind\SMB\Exception\NotFoundException
  123. * @throws \Icewind\SMB\Exception\AlreadyExistsException
  124. */
  125. public function mkdir($path) {
  126. return $this->getState()->mkdir($this->buildUrl($path));
  127. }
  128. /**
  129. * Remove a folder on the share
  130. *
  131. * @param string $path
  132. * @return bool
  133. *
  134. * @throws \Icewind\SMB\Exception\NotFoundException
  135. * @throws \Icewind\SMB\Exception\InvalidTypeException
  136. */
  137. public function rmdir($path) {
  138. return $this->getState()->rmdir($this->buildUrl($path));
  139. }
  140. /**
  141. * Delete a file on the share
  142. *
  143. * @param string $path
  144. * @return bool
  145. *
  146. * @throws \Icewind\SMB\Exception\NotFoundException
  147. * @throws \Icewind\SMB\Exception\InvalidTypeException
  148. */
  149. public function del($path) {
  150. return $this->getState()->unlink($this->buildUrl($path));
  151. }
  152. /**
  153. * Rename a remote file
  154. *
  155. * @param string $from
  156. * @param string $to
  157. * @return bool
  158. *
  159. * @throws \Icewind\SMB\Exception\NotFoundException
  160. * @throws \Icewind\SMB\Exception\AlreadyExistsException
  161. */
  162. public function rename($from, $to) {
  163. return $this->getState()->rename($this->buildUrl($from), $this->buildUrl($to));
  164. }
  165. /**
  166. * Upload a local file
  167. *
  168. * @param string $source local file
  169. * @param string $target remove file
  170. * @return bool
  171. *
  172. * @throws \Icewind\SMB\Exception\NotFoundException
  173. * @throws \Icewind\SMB\Exception\InvalidTypeException
  174. */
  175. public function put($source, $target) {
  176. $sourceHandle = fopen($source, 'rb');
  177. $targetUrl = $this->buildUrl($target);
  178. $targetHandle = $this->getState()->create($targetUrl);
  179. while ($data = fread($sourceHandle, NativeReadStream::CHUNK_SIZE)) {
  180. $this->getState()->write($targetHandle, $data, $targetUrl);
  181. }
  182. $this->getState()->close($targetHandle, $targetUrl);
  183. return true;
  184. }
  185. /**
  186. * Download a remote file
  187. *
  188. * @param string $source remove file
  189. * @param string $target local file
  190. * @return bool
  191. *
  192. * @throws \Icewind\SMB\Exception\NotFoundException
  193. * @throws \Icewind\SMB\Exception\InvalidTypeException
  194. * @throws \Icewind\SMB\Exception\InvalidPathException
  195. * @throws \Icewind\SMB\Exception\InvalidResourceException
  196. */
  197. public function get($source, $target) {
  198. if (!$target) {
  199. throw new InvalidPathException('Invalid target path: Filename cannot be empty');
  200. }
  201. $sourceHandle = $this->getState()->open($this->buildUrl($source), 'r');
  202. if (!$sourceHandle) {
  203. throw new InvalidResourceException('Failed opening remote file "' . $source . '" for reading');
  204. }
  205. $targetHandle = @fopen($target, 'wb');
  206. if (!$targetHandle) {
  207. $error = error_get_last();
  208. if (is_array($error)) {
  209. $reason = $error['message'];
  210. } else {
  211. $reason = 'Unknown error';
  212. }
  213. $this->getState()->close($sourceHandle, $this->buildUrl($source));
  214. throw new InvalidResourceException('Failed opening local file "' . $target . '" for writing: ' . $reason);
  215. }
  216. while ($data = $this->getState()->read($sourceHandle, NativeReadStream::CHUNK_SIZE)) {
  217. fwrite($targetHandle, $data);
  218. }
  219. $this->getState()->close($sourceHandle, $this->buildUrl($source));
  220. return true;
  221. }
  222. /**
  223. * Open a readable stream to a remote file
  224. *
  225. * @param string $source
  226. * @return resource a read only stream with the contents of the remote file
  227. *
  228. * @throws \Icewind\SMB\Exception\NotFoundException
  229. * @throws \Icewind\SMB\Exception\InvalidTypeException
  230. */
  231. public function read($source) {
  232. $url = $this->buildUrl($source);
  233. $handle = $this->getState()->open($url, 'r');
  234. return NativeReadStream::wrap($this->getState(), $handle, 'r', $url);
  235. }
  236. /**
  237. * Open a writeable stream to a remote file
  238. * Note: This method will truncate the file to 0bytes first
  239. *
  240. * @param string $source
  241. * @return resource a writeable stream
  242. *
  243. * @throws \Icewind\SMB\Exception\NotFoundException
  244. * @throws \Icewind\SMB\Exception\InvalidTypeException
  245. */
  246. public function write($source) {
  247. $url = $this->buildUrl($source);
  248. $handle = $this->getState()->create($url);
  249. return NativeWriteStream::wrap($this->getState(), $handle, 'w', $url);
  250. }
  251. /**
  252. * Open a writeable stream and set the cursor to the end of the stream
  253. *
  254. * @param string $source
  255. * @return resource a writeable stream
  256. *
  257. * @throws \Icewind\SMB\Exception\NotFoundException
  258. * @throws \Icewind\SMB\Exception\InvalidTypeException
  259. */
  260. public function append($source) {
  261. $url = $this->buildUrl($source);
  262. $handle = $this->getState()->open($url, "a+");
  263. return NativeWriteStream::wrap($this->getState(), $handle, "a", $url);
  264. }
  265. /**
  266. * Get extended attributes for the path
  267. *
  268. * @param string $path
  269. * @param string $attribute attribute to get the info
  270. * @return string the attribute value
  271. */
  272. public function getAttribute($path, $attribute) {
  273. return $this->getState()->getxattr($this->buildUrl($path), $attribute);
  274. }
  275. /**
  276. * Set extended attributes for the given path
  277. *
  278. * @param string $path
  279. * @param string $attribute attribute to get the info
  280. * @param string|int $value
  281. * @return mixed the attribute value
  282. */
  283. public function setAttribute($path, $attribute, $value) {
  284. if ($attribute === 'system.dos_attr.mode' and is_int($value)) {
  285. $value = '0x' . dechex($value);
  286. }
  287. return $this->getState()->setxattr($this->buildUrl($path), $attribute, $value);
  288. }
  289. /**
  290. * Set DOS comaptible node mode
  291. *
  292. * @param string $path
  293. * @param int $mode a combination of FileInfo::MODE_READONLY, FileInfo::MODE_ARCHIVE, FileInfo::MODE_SYSTEM and FileInfo::MODE_HIDDEN, FileInfo::NORMAL
  294. * @return mixed
  295. */
  296. public function setMode($path, $mode) {
  297. return $this->setAttribute($path, 'system.dos_attr.mode', $mode);
  298. }
  299. /**
  300. * Start smb notify listener
  301. * Note: This is a blocking call
  302. *
  303. * @param string $path
  304. * @return INotifyHandler
  305. */
  306. public function notify($path) {
  307. // php-smbclient does not support notify (https://github.com/eduardok/libsmbclient-php/issues/29)
  308. // so we use the smbclient based backend for this
  309. if (!Server::available($this->server->getSystem())) {
  310. throw new DependencyException('smbclient not found in path for notify command');
  311. }
  312. $share = new Share($this->server, $this->getName(), $this->server->getSystem());
  313. return $share->notify($path);
  314. }
  315. public function getServer(): IServer {
  316. return $this->server;
  317. }
  318. public function __destruct() {
  319. unset($this->state);
  320. }
  321. }