PageRenderTime 69ms CodeModel.GetById 42ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/patchwork/utf8/class/Patchwork/Utf8/WinFsStreamWrapper.php

https://bitbucket.org/davide_grobberio/laravel-angular
PHP | 400 lines | 323 code | 59 blank | 18 comment | 39 complexity | b4db9bd4b6f2253183ace850d13aa376 MD5 | raw file
Possible License(s): MIT, BSD-3-Clause
  1. <?php // vi: set fenc=utf-8 ts=4 sw=4 et:
  2. /*
  3. * Copyright (C) 2014 Nicolas Grekas - p@tchwork.com
  4. *
  5. * This library is free software; you can redistribute it and/or modify it
  6. * under the terms of the (at your option):
  7. * Apache License v2.0 (http://apache.org/licenses/LICENSE-2.0.txt), or
  8. * GNU General Public License v2.0 (http://gnu.org/licenses/gpl-2.0.txt).
  9. */
  10. namespace Patchwork\Utf8;
  11. /**
  12. * Unicode UTF-8 aware stream based filesystem access on MS-Windows.
  13. *
  14. * Based on COM Scripting.FileSystemObject object and short paths.
  15. * Enabled by e.g.: stream_wrapper_register('win', 'Patchwork\Utf8\WinFsStreamWrapper');
  16. *
  17. * See also https://code.google.com/p/php-wfio/ for a PHP extension
  18. * and comments on http://www.rooftopsolutions.nl/blog/filesystem-encoding-and-php
  19. */
  20. class WinFsStreamWrapper
  21. {
  22. public $context;
  23. protected $handle;
  24. static function hide($path)
  25. {
  26. list($fs, $path) = self::fs($path);
  27. if ($fs->FileExists($path)) $fs->GetFile($path)->Attributes |= 2;
  28. else if ($fs->FolderExists($path)) $f = $fs->GetFolder($path)->Attributes |= 2;
  29. else return false;
  30. return true;
  31. }
  32. static function fs($path, $is_utf8 = true)
  33. {
  34. static $fs;
  35. isset($fs) or $fs = new \COM('Scripting.FileSystemObject', null, CP_UTF8);
  36. $path = explode('://', $path, 2);
  37. $path = $path[(int) isset($path[1])];
  38. $path = strtr($path, '/', '\\');
  39. $pre = '';
  40. if (! isset($path[0])
  41. || ( '/' !== $path[0]
  42. && '\\' !== $path[0]
  43. && false === strpos($path, ':') ) )
  44. {
  45. $pre = getcwd() . '\\';
  46. }
  47. $pre = new \VARIANT($pre);
  48. if ($is_utf8) $path = new \VARIANT($path, VT_BSTR, CP_UTF8);
  49. else $path = new \VARIANT($path);
  50. return array($fs, $fs->getAbsolutePathName(variant_cat($pre, $path)));
  51. }
  52. function dir_closedir()
  53. {
  54. $this->handle = null;
  55. return true;
  56. }
  57. function dir_opendir($path, $options)
  58. {
  59. list($fs, $path) = self::fs($path);
  60. if (! $fs->FolderExists($path)) return false;
  61. $dir = $fs->GetFolder($path);
  62. try
  63. {
  64. $f = array('.', '..');
  65. foreach ($dir->SubFolders() as $v) $f[] = $v->Name;
  66. foreach ($dir->Files as $v) $f[] = $v->Name;
  67. }
  68. catch (\Exception $f)
  69. {
  70. $f = array();
  71. }
  72. $this->handle = $f;
  73. return true;
  74. }
  75. function dir_readdir()
  76. {
  77. if (list(, $c) = each($this->handle)) return $c;
  78. return false;
  79. }
  80. function dir_rewinddir()
  81. {
  82. reset($this->handle);
  83. return true;
  84. }
  85. function mkdir($path, $mode, $options)
  86. {
  87. list($fs, $path) = self::fs($path);
  88. try
  89. {
  90. if ($options & STREAM_MKDIR_RECURSIVE)
  91. {
  92. $path = $fs->GetAbsolutePathName($path);
  93. $path = explode('\\', $path);
  94. if (isset($path[3]) && '' === $path[0] . $path[1])
  95. {
  96. $pre = '\\\\' . $path[2] . '\\' . $path[3] . '\\';
  97. $i = 4;
  98. }
  99. else if (isset($path[1]))
  100. {
  101. $pre = $path[0] . '\\';
  102. $i = 1;
  103. }
  104. else
  105. {
  106. $pre = '';
  107. $i = 0;
  108. }
  109. while (isset($path[$i]) && $fs->FolderExists($pre . $path[$i]))
  110. {
  111. $pre .= $path[$i++] . '\\';
  112. }
  113. if (! isset($path[$i])) return false;
  114. while (isset($path[$i]))
  115. {
  116. $fs->CreateFolder($pre .= $path[$i++] . '\\');
  117. }
  118. return true;
  119. }
  120. else
  121. {
  122. $fs->CreateFolder($path);
  123. }
  124. return true;
  125. }
  126. catch (\Exception $e)
  127. {
  128. return false;
  129. }
  130. }
  131. function rename($from, $to)
  132. {
  133. list($fs, $to) = self::fs($to);
  134. if ($fs->FileExists($to) || $fs->FolderExists($to))
  135. {
  136. return false;
  137. }
  138. list(,$from) = self::fs($from);
  139. try
  140. {
  141. if ($fs->FileExists($from))
  142. {
  143. $fs->MoveFile($from, $to);
  144. return true;
  145. }
  146. if ($fs->FolderExists($from))
  147. {
  148. $fs->MoveFolder($from, $to);
  149. return true;
  150. }
  151. }
  152. catch (\Exception $e) {}
  153. return false;
  154. }
  155. function rmdir($path, $options)
  156. {
  157. list($fs, $path) = self::fs($path);
  158. if ($fs->FolderExists($path)) return rmdir($fs->GetFolder($path)->ShortPath);
  159. return false;
  160. }
  161. // @todo function stream_cast($cast_as)
  162. function stream_close()
  163. {
  164. fclose($this->handle);
  165. $this->handle = null;
  166. }
  167. function stream_eof()
  168. {
  169. return feof($this->handle);
  170. }
  171. function stream_flush()
  172. {
  173. return fflush($this->handle);
  174. }
  175. function stream_lock($operation)
  176. {
  177. return flock($this->handle, $operation);
  178. }
  179. function stream_metadata($path, $option, $value)
  180. {
  181. list($fs, $path) = self::fs($path);
  182. if ($fs->FileExists($path)) $f = $fs->GetFile($path);
  183. else if ($fs->FileExists($path)) $f = $fs->GetFolder($path);
  184. else $f = false;
  185. if (STREAM_META_TOUCH === $option)
  186. {
  187. if ($f) return touch($f->ShortPath);
  188. try
  189. {
  190. $fs->OpenTextFile($path, 8, true, 0)->Close();
  191. return true;
  192. }
  193. catch (\Exception $e) {}
  194. }
  195. if (! $f) return false;
  196. switch ($option)
  197. {
  198. case STREAM_META_ACCESS: return chmod($f->ShortPath, $value);
  199. case STREAM_META_OWNER:
  200. case STREAM_META_OWNER_NAME: return chown($f->ShortPath, $value);
  201. case STREAM_META_GROUP:
  202. case STREAM_META_GROUP_NAME: return chgrp($f->ShortPath, $value);
  203. default: return false;
  204. }
  205. }
  206. function stream_open($path, $mode, $options, &$opened_path)
  207. {
  208. $mode .= '';
  209. list($fs, $path) = self::fs($path);
  210. if ($fs->FolderExists($path)) return false;
  211. try
  212. {
  213. if ('x' === $m = substr($mode, 0, 1))
  214. {
  215. $fs->CreateTextFile($path, false)->Close();
  216. $f = $fs->GetFile($path);
  217. $mode[0] = 'w';
  218. }
  219. else
  220. {
  221. $f = $fs->GetFile($path);
  222. }
  223. }
  224. catch (\Exception $f)
  225. {
  226. try
  227. {
  228. switch ($m)
  229. {
  230. case 'w':
  231. case 'c':
  232. case 'a':
  233. $h = $fs->CreateTextFile($path, true);
  234. $f = $fs->GetFile($path);
  235. $h->Close();
  236. break;
  237. default: return false;
  238. }
  239. }
  240. catch (\Exception $e)
  241. {
  242. return false;
  243. }
  244. }
  245. if (! (STREAM_REPORT_ERRORS & $options))
  246. {
  247. set_error_handler('var_dump', 0);
  248. $e = error_reporting(0);
  249. }
  250. $this->handle = fopen($f->ShortPath, $mode);
  251. if (! (STREAM_REPORT_ERRORS & $options))
  252. {
  253. error_reporting($e);
  254. restore_error_handler();
  255. }
  256. if ($this->handle) return true;
  257. if (isset($h)) $f->Delete(true);
  258. return false;
  259. }
  260. function stream_read($count)
  261. {
  262. return fread($this->handle, $count);
  263. }
  264. function stream_seek($offset, $whence = SEEK_SET)
  265. {
  266. return fseek($this->handle, $offset, $whence);
  267. }
  268. function stream_set_option($option, $arg1, $arg2)
  269. {
  270. switch ($option)
  271. {
  272. case STREAM_OPTION_BLOCKING: return stream_set_blocking($this->handle, $arg1);
  273. case STREAM_OPTION_READ_TIMEOUT: return stream_set_timeout($this->handle, $arg1, $arg2);
  274. case STREAM_OPTION_WRITE_BUFFER: return stream_set_write_buffer($this->handle, $arg1, $arg2);
  275. default: return false;
  276. }
  277. }
  278. function stream_stat()
  279. {
  280. return fstat($this->handle);
  281. }
  282. function stream_tell()
  283. {
  284. return ftell($this->handle);
  285. }
  286. function stream_truncate($new_size)
  287. {
  288. return ftruncate($this->handle, $new_size);
  289. }
  290. function stream_write($data)
  291. {
  292. return fwrite($this->handle, $data, strlen($data));
  293. }
  294. function unlink($path)
  295. {
  296. list($fs, $path) = self::fs($path);
  297. if ($fs->FileExists($path)) return unlink($fs->GetFile($path)->ShortPath);
  298. return false;
  299. }
  300. function url_stat($path, $flags)
  301. {
  302. list($fs, $path) = self::fs($path);
  303. if ($fs->FileExists($path)) $f = $fs->GetFile($path);
  304. else if ($fs->FolderExists($path)) $f = $fs->GetFolder($path);
  305. else return false;
  306. if (STREAM_URL_STAT_QUIET & $flags)
  307. {
  308. set_error_handler('var_dump', 0);
  309. $e = error_reporting(0);
  310. }
  311. if (STREAM_URL_STAT_LINK & $flags) $f = lstat($f->ShortPath);
  312. else $f = stat($f->ShortPath);
  313. if (STREAM_URL_STAT_QUIET & $flags)
  314. {
  315. error_reporting($e);
  316. restore_error_handler();
  317. }
  318. return $f;
  319. }
  320. }