elFinderVolumeLocalFileSystem.class.php 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442
  1. <?php
  2. // Implement similar functionality in PHP 5.2 or 5.3
  3. // http://php.net/manual/class.recursivecallbackfilteriterator.php#110974
  4. if (!class_exists('RecursiveCallbackFilterIterator', false)) {
  5. class RecursiveCallbackFilterIterator extends RecursiveFilterIterator
  6. {
  7. private $callback;
  8. public function __construct(RecursiveIterator $iterator, $callback)
  9. {
  10. $this->callback = $callback;
  11. parent::__construct($iterator);
  12. }
  13. public function accept()
  14. {
  15. return call_user_func($this->callback, parent::current(), parent::key(), parent::getInnerIterator());
  16. }
  17. public function getChildren()
  18. {
  19. return new self($this->getInnerIterator()->getChildren(), $this->callback);
  20. }
  21. }
  22. }
  23. /**
  24. * elFinder driver for local filesystem.
  25. *
  26. * @author Dmitry (dio) Levashov
  27. * @author Troex Nevelin
  28. **/
  29. class elFinderVolumeLocalFileSystem extends elFinderVolumeDriver
  30. {
  31. /**
  32. * Driver id
  33. * Must be started from letter and contains [a-z0-9]
  34. * Used as part of volume id
  35. *
  36. * @var string
  37. **/
  38. protected $driverId = 'l';
  39. /**
  40. * Required to count total archive files size
  41. *
  42. * @var int
  43. **/
  44. protected $archiveSize = 0;
  45. /**
  46. * Is checking stat owner
  47. *
  48. * @var boolean
  49. */
  50. protected $statOwner = false;
  51. /**
  52. * Path to quarantine directory
  53. *
  54. * @var string
  55. */
  56. private $quarantine;
  57. /**
  58. * Constructor
  59. * Extend options with required fields
  60. *
  61. * @author Dmitry (dio) Levashov
  62. */
  63. public function __construct()
  64. {
  65. $this->options['alias'] = ''; // alias to replace root dir name
  66. $this->options['dirMode'] = 0755; // new dirs mode
  67. $this->options['fileMode'] = 0644; // new files mode
  68. $this->options['quarantine'] = '.quarantine'; // quarantine folder name - required to check archive (must be hidden)
  69. $this->options['rootCssClass'] = 'elfinder-navbar-root-local';
  70. $this->options['followSymLinks'] = true;
  71. $this->options['detectDirIcon'] = ''; // file name that is detected as a folder icon e.g. '.diricon.png'
  72. $this->options['keepTimestamp'] = array('copy', 'move'); // keep timestamp at inner filesystem allowed 'copy', 'move' and 'upload'
  73. $this->options['substituteImg'] = true; // support substitute image with dim command
  74. $this->options['statCorrector'] = null; // callable to correct stat data `function(&$stat, $path, $statOwner, $volumeDriveInstance){}`
  75. }
  76. /*********************************************************************/
  77. /* INIT AND CONFIGURE */
  78. /*********************************************************************/
  79. /**
  80. * Prepare driver before mount volume.
  81. * Return true if volume is ready.
  82. *
  83. * @return bool
  84. **/
  85. protected function init()
  86. {
  87. // Normalize directory separator for windows
  88. if (DIRECTORY_SEPARATOR !== '/') {
  89. foreach (array('path', 'tmbPath', 'tmpPath', 'quarantine') as $key) {
  90. if (!empty($this->options[$key])) {
  91. $this->options[$key] = str_replace('/', DIRECTORY_SEPARATOR, $this->options[$key]);
  92. }
  93. }
  94. // PHP >= 7.1 Supports UTF-8 path on Windows
  95. if (version_compare(PHP_VERSION, '7.1', '>=')) {
  96. $this->options['encoding'] = '';
  97. $this->options['locale'] = '';
  98. }
  99. }
  100. if (!$cwd = getcwd()) {
  101. return $this->setError('elFinder LocalVolumeDriver requires a result of getcwd().');
  102. }
  103. // detect systemRoot
  104. if (!isset($this->options['systemRoot'])) {
  105. if ($cwd[0] === DIRECTORY_SEPARATOR || $this->root[0] === DIRECTORY_SEPARATOR) {
  106. $this->systemRoot = DIRECTORY_SEPARATOR;
  107. } else if (preg_match('/^([a-zA-Z]:' . preg_quote(DIRECTORY_SEPARATOR, '/') . ')/', $this->root, $m)) {
  108. $this->systemRoot = $m[1];
  109. } else if (preg_match('/^([a-zA-Z]:' . preg_quote(DIRECTORY_SEPARATOR, '/') . ')/', $cwd, $m)) {
  110. $this->systemRoot = $m[1];
  111. }
  112. }
  113. $this->root = $this->getFullPath($this->root, $cwd);
  114. if (!empty($this->options['startPath'])) {
  115. $this->options['startPath'] = $this->getFullPath($this->options['startPath'], $this->root);
  116. }
  117. if (is_null($this->options['syncChkAsTs'])) {
  118. $this->options['syncChkAsTs'] = true;
  119. }
  120. if (is_null($this->options['syncCheckFunc'])) {
  121. $this->options['syncCheckFunc'] = array($this, 'localFileSystemInotify');
  122. }
  123. // check 'statCorrector'
  124. if (empty($this->options['statCorrector']) || !is_callable($this->options['statCorrector'])) {
  125. $this->options['statCorrector'] = null;
  126. }
  127. return true;
  128. }
  129. /**
  130. * Configure after successfull mount.
  131. *
  132. * @return void
  133. * @throws elFinderAbortException
  134. * @author Dmitry (dio) Levashov
  135. */
  136. protected function configure()
  137. {
  138. $hiddens = array();
  139. $root = $this->stat($this->root);
  140. // check thumbnails path
  141. if (!empty($this->options['tmbPath'])) {
  142. if (strpos($this->options['tmbPath'], DIRECTORY_SEPARATOR) === false) {
  143. $hiddens['tmb'] = $this->options['tmbPath'];
  144. $this->options['tmbPath'] = $this->_abspath($this->options['tmbPath']);
  145. } else {
  146. $this->options['tmbPath'] = $this->_normpath($this->options['tmbPath']);
  147. }
  148. }
  149. // check temp path
  150. if (!empty($this->options['tmpPath'])) {
  151. if (strpos($this->options['tmpPath'], DIRECTORY_SEPARATOR) === false) {
  152. $hiddens['temp'] = $this->options['tmpPath'];
  153. $this->options['tmpPath'] = $this->_abspath($this->options['tmpPath']);
  154. } else {
  155. $this->options['tmpPath'] = $this->_normpath($this->options['tmpPath']);
  156. }
  157. }
  158. // check quarantine path
  159. if (!empty($this->options['quarantine'])) {
  160. if (strpos($this->options['quarantine'], DIRECTORY_SEPARATOR) === false) {
  161. $hiddens['quarantine'] = $this->options['quarantine'];
  162. $this->options['quarantine'] = $this->_abspath($this->options['quarantine']);
  163. } else {
  164. $this->options['quarantine'] = $this->_normpath($this->options['quarantine']);
  165. }
  166. }
  167. parent::configure();
  168. // check tmbPath
  169. if (!$this->tmbPath && isset($hiddens['tmb'])) {
  170. unset($hiddens['tmb']);
  171. }
  172. // if no thumbnails url - try detect it
  173. if ($root['read'] && !$this->tmbURL && $this->URL) {
  174. if (strpos($this->tmbPath, $this->root) === 0) {
  175. $this->tmbURL = $this->URL . str_replace(DIRECTORY_SEPARATOR, '/', substr($this->tmbPath, strlen($this->root) + 1));
  176. if (preg_match("|[^/?&=]$|", $this->tmbURL)) {
  177. $this->tmbURL .= '/';
  178. }
  179. }
  180. }
  181. // set $this->tmp by options['tmpPath']
  182. $this->tmp = '';
  183. if (!empty($this->options['tmpPath'])) {
  184. if ((is_dir($this->options['tmpPath']) || mkdir($this->options['tmpPath'], $this->options['dirMode'], true)) && is_writable($this->options['tmpPath'])) {
  185. $this->tmp = $this->options['tmpPath'];
  186. } else {
  187. if (isset($hiddens['temp'])) {
  188. unset($hiddens['temp']);
  189. }
  190. }
  191. }
  192. if (!$this->tmp && ($tmp = elFinder::getStaticVar('commonTempPath'))) {
  193. $this->tmp = $tmp;
  194. }
  195. // check quarantine dir
  196. $this->quarantine = '';
  197. if (!empty($this->options['quarantine'])) {
  198. if ((is_dir($this->options['quarantine']) || mkdir($this->options['quarantine'], $this->options['dirMode'], true)) && is_writable($this->options['quarantine'])) {
  199. $this->quarantine = $this->options['quarantine'];
  200. } else {
  201. if (isset($hiddens['quarantine'])) {
  202. unset($hiddens['quarantine']);
  203. }
  204. }
  205. }
  206. if (!$this->quarantine) {
  207. if (!$this->tmp) {
  208. $this->archivers['extract'] = array();
  209. $this->disabled[] = 'extract';
  210. } else {
  211. $this->quarantine = $this->tmp;
  212. }
  213. }
  214. if ($hiddens) {
  215. foreach ($hiddens as $hidden) {
  216. $this->attributes[] = array(
  217. 'pattern' => '~^' . preg_quote(DIRECTORY_SEPARATOR . $hidden, '~') . '$~',
  218. 'read' => false,
  219. 'write' => false,
  220. 'locked' => true,
  221. 'hidden' => true
  222. );
  223. }
  224. }
  225. if (!empty($this->options['keepTimestamp'])) {
  226. $this->options['keepTimestamp'] = array_flip($this->options['keepTimestamp']);
  227. }
  228. $this->statOwner = (!empty($this->options['statOwner']));
  229. }
  230. /**
  231. * Long pooling sync checker
  232. * This function require server command `inotifywait`
  233. * If `inotifywait` need full path, Please add `define('ELFINER_INOTIFYWAIT_PATH', '/PATH_TO/inotifywait');` into connector.php
  234. *
  235. * @param string $path
  236. * @param int $standby
  237. * @param number $compare
  238. *
  239. * @return number|bool
  240. * @throws elFinderAbortException
  241. */
  242. public function localFileSystemInotify($path, $standby, $compare)
  243. {
  244. if (isset($this->sessionCache['localFileSystemInotify_disable'])) {
  245. return false;
  246. }
  247. $path = realpath($path);
  248. $mtime = filemtime($path);
  249. if (!$mtime) {
  250. return false;
  251. }
  252. if ($mtime != $compare) {
  253. return $mtime;
  254. }
  255. $inotifywait = defined('ELFINER_INOTIFYWAIT_PATH') ? ELFINER_INOTIFYWAIT_PATH : 'inotifywait';
  256. $standby = max(1, intval($standby));
  257. $cmd = $inotifywait . ' ' . escapeshellarg($path) . ' -t ' . $standby . ' -e moved_to,moved_from,move,close_write,delete,delete_self';
  258. $this->procExec($cmd, $o, $r);
  259. if ($r === 0) {
  260. // changed
  261. clearstatcache();
  262. if (file_exists($path)) {
  263. $mtime = filemtime($path); // error on busy?
  264. return $mtime ? $mtime : time();
  265. } else {
  266. // target was removed
  267. return 0;
  268. }
  269. } else if ($r === 2) {
  270. // not changed (timeout)
  271. return $compare;
  272. }
  273. // error
  274. // cache to $_SESSION
  275. $this->sessionCache['localFileSystemInotify_disable'] = true;
  276. $this->session->set($this->id, $this->sessionCache);
  277. return false;
  278. }
  279. /*********************************************************************/
  280. /* FS API */
  281. /*********************************************************************/
  282. /*********************** paths/urls *************************/
  283. /**
  284. * Return parent directory path
  285. *
  286. * @param string $path file path
  287. *
  288. * @return string
  289. * @author Dmitry (dio) Levashov
  290. **/
  291. protected function _dirname($path)
  292. {
  293. return dirname($path);
  294. }
  295. /**
  296. * Return file name
  297. *
  298. * @param string $path file path
  299. *
  300. * @return string
  301. * @author Dmitry (dio) Levashov
  302. **/
  303. protected function _basename($path)
  304. {
  305. return basename($path);
  306. }
  307. /**
  308. * Join dir name and file name and retur full path
  309. *
  310. * @param string $dir
  311. * @param string $name
  312. *
  313. * @return string
  314. * @author Dmitry (dio) Levashov
  315. **/
  316. protected function _joinPath($dir, $name)
  317. {
  318. return rtrim($dir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $name;
  319. }
  320. /**
  321. * Return normalized path, this works the same as os.path.normpath() in Python
  322. *
  323. * @param string $path path
  324. *
  325. * @return string
  326. * @author Troex Nevelin
  327. **/
  328. protected function _normpath($path)
  329. {
  330. if (empty($path)) {
  331. return '.';
  332. }
  333. $changeSep = (DIRECTORY_SEPARATOR !== '/');
  334. if ($changeSep) {
  335. $drive = '';
  336. if (preg_match('/^([a-zA-Z]:)(.*)/', $path, $m)) {
  337. $drive = $m[1];
  338. $path = $m[2] ? $m[2] : '/';
  339. }
  340. $path = str_replace(DIRECTORY_SEPARATOR, '/', $path);
  341. }
  342. if (strpos($path, '/') === 0) {
  343. $initial_slashes = true;
  344. } else {
  345. $initial_slashes = false;
  346. }
  347. if (($initial_slashes)
  348. && (strpos($path, '//') === 0)
  349. && (strpos($path, '///') === false)) {
  350. $initial_slashes = 2;
  351. }
  352. $initial_slashes = (int)$initial_slashes;
  353. $comps = explode('/', $path);
  354. $new_comps = array();
  355. foreach ($comps as $comp) {
  356. if (in_array($comp, array('', '.'))) {
  357. continue;
  358. }
  359. if (($comp != '..')
  360. || (!$initial_slashes && !$new_comps)
  361. || ($new_comps && (end($new_comps) == '..'))) {
  362. array_push($new_comps, $comp);
  363. } elseif ($new_comps) {
  364. array_pop($new_comps);
  365. }
  366. }
  367. $comps = $new_comps;
  368. $path = implode('/', $comps);
  369. if ($initial_slashes) {
  370. $path = str_repeat('/', $initial_slashes) . $path;
  371. }
  372. if ($changeSep) {
  373. $path = $drive . str_replace('/', DIRECTORY_SEPARATOR, $path);
  374. }
  375. return $path ? $path : '.';
  376. }
  377. /**
  378. * Return file path related to root dir
  379. *
  380. * @param string $path file path
  381. *
  382. * @return string
  383. * @author Dmitry (dio) Levashov
  384. **/
  385. protected function _relpath($path)
  386. {
  387. if ($path === $this->root) {
  388. return '';
  389. } else {
  390. if (strpos($path, $this->root) === 0) {
  391. return ltrim(substr($path, strlen($this->root)), DIRECTORY_SEPARATOR);
  392. } else {
  393. // for link
  394. return $path;
  395. }
  396. }
  397. }
  398. /**
  399. * Convert path related to root dir into real path
  400. *
  401. * @param string $path file path
  402. *
  403. * @return string
  404. * @author Dmitry (dio) Levashov
  405. **/
  406. protected function _abspath($path)
  407. {
  408. if ($path === DIRECTORY_SEPARATOR) {
  409. return $this->root;
  410. } else {
  411. if (strpos($path, $this->systemRoot) === 0) {
  412. return $path;
  413. } else if (DIRECTORY_SEPARATOR !== '/' && preg_match('/^[a-zA-Z]:' . preg_quote(DIRECTORY_SEPARATOR, '/') . '/', $path)) {
  414. return $path;
  415. } else {
  416. return $this->_joinPath($this->root, $path);
  417. }
  418. }
  419. }
  420. /**
  421. * Return fake path started from root dir
  422. *
  423. * @param string $path file path
  424. *
  425. * @return string
  426. * @author Dmitry (dio) Levashov
  427. **/
  428. protected function _path($path)
  429. {
  430. return $this->rootName . ($path == $this->root ? '' : $this->separator . $this->_relpath($path));
  431. }
  432. /**
  433. * Return true if $path is children of $parent
  434. *
  435. * @param string $path path to check
  436. * @param string $parent parent path
  437. *
  438. * @return bool
  439. * @author Dmitry (dio) Levashov
  440. **/
  441. protected function _inpath($path, $parent)
  442. {
  443. $cwd = getcwd();
  444. $real_path = $this->getFullPath($path, $cwd);
  445. $real_parent = $this->getFullPath($parent, $cwd);
  446. if ($real_path && $real_parent) {
  447. return $real_path === $real_parent || strpos($real_path, rtrim($real_parent, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR) === 0;
  448. }
  449. return false;
  450. }
  451. /***************** file stat ********************/
  452. /**
  453. * Return stat for given path.
  454. * Stat contains following fields:
  455. * - (int) size file size in b. required
  456. * - (int) ts file modification time in unix time. required
  457. * - (string) mime mimetype. required for folders, others - optionally
  458. * - (bool) read read permissions. required
  459. * - (bool) write write permissions. required
  460. * - (bool) locked is object locked. optionally
  461. * - (bool) hidden is object hidden. optionally
  462. * - (string) alias for symlinks - link target path relative to root path. optionally
  463. * - (string) target for symlinks - link target path. optionally
  464. * If file does not exists - returns empty array or false.
  465. *
  466. * @param string $path file path
  467. *
  468. * @return array|false
  469. * @author Dmitry (dio) Levashov
  470. **/
  471. protected function _stat($path)
  472. {
  473. $stat = array();
  474. if (!file_exists($path) && !is_link($path)) {
  475. return $stat;
  476. }
  477. //Verifies the given path is the root or is inside the root. Prevents directory traveral.
  478. if (!$this->_inpath($path, $this->root)) {
  479. return $stat;
  480. }
  481. $stat['isowner'] = false;
  482. $linkreadable = false;
  483. if ($path != $this->root && is_link($path)) {
  484. if (!$this->options['followSymLinks']) {
  485. return array();
  486. }
  487. if (!($target = $this->readlink($path))
  488. || $target == $path) {
  489. if (is_null($target)) {
  490. $stat = array();
  491. return $stat;
  492. } else {
  493. $stat['mime'] = 'symlink-broken';
  494. $target = readlink($path);
  495. $lstat = lstat($path);
  496. $ostat = $this->getOwnerStat($lstat['uid'], $lstat['gid']);
  497. $linkreadable = !empty($ostat['isowner']);
  498. }
  499. }
  500. $stat['alias'] = $this->_path($target);
  501. $stat['target'] = $target;
  502. }
  503. $readable = is_readable($path);
  504. if ($readable) {
  505. $size = sprintf('%u', filesize($path));
  506. $stat['ts'] = filemtime($path);
  507. if ($this->statOwner) {
  508. $fstat = stat($path);
  509. $uid = $fstat['uid'];
  510. $gid = $fstat['gid'];
  511. $stat['perm'] = substr((string)decoct($fstat['mode']), -4);
  512. $stat = array_merge($stat, $this->getOwnerStat($uid, $gid));
  513. }
  514. }
  515. if (($dir = is_dir($path)) && $this->options['detectDirIcon']) {
  516. $favicon = $path . DIRECTORY_SEPARATOR . $this->options['detectDirIcon'];
  517. if ($this->URL && file_exists($favicon)) {
  518. $stat['icon'] = $this->URL . str_replace(DIRECTORY_SEPARATOR, '/', substr($favicon, strlen($this->root) + 1));
  519. }
  520. }
  521. if (!isset($stat['mime'])) {
  522. $stat['mime'] = $dir ? 'directory' : $this->mimetype($path);
  523. }
  524. //logical rights first
  525. $stat['read'] = ($linkreadable || $readable) ? null : false;
  526. $stat['write'] = is_writable($path) ? null : false;
  527. if (is_null($stat['read'])) {
  528. if ($dir) {
  529. $stat['size'] = 0;
  530. } else if (isset($size)) {
  531. $stat['size'] = $size;
  532. }
  533. }
  534. if ($this->options['statCorrector']) {
  535. call_user_func_array($this->options['statCorrector'], array(&$stat, $path, $this->statOwner, $this));
  536. }
  537. return $stat;
  538. }
  539. /**
  540. * Get stat `owner`, `group` and `isowner` by `uid` and `gid`
  541. * Sub-fuction of _stat() and _scandir()
  542. *
  543. * @param integer $uid
  544. * @param integer $gid
  545. *
  546. * @return array stat
  547. */
  548. protected function getOwnerStat($uid, $gid)
  549. {
  550. static $names = null;
  551. static $phpuid = null;
  552. if (is_null($names)) {
  553. $names = array('uid' => array(), 'gid' => array());
  554. }
  555. if (is_null($phpuid)) {
  556. if (is_callable('posix_getuid')) {
  557. $phpuid = posix_getuid();
  558. } else {
  559. $phpuid = 0;
  560. }
  561. }
  562. $stat = array();
  563. if ($uid) {
  564. $stat['isowner'] = ($phpuid == $uid);
  565. if (isset($names['uid'][$uid])) {
  566. $stat['owner'] = $names['uid'][$uid];
  567. } else if (is_callable('posix_getpwuid')) {
  568. $pwuid = posix_getpwuid($uid);
  569. $stat['owner'] = $names['uid'][$uid] = $pwuid['name'];
  570. } else {
  571. $stat['owner'] = $names['uid'][$uid] = $uid;
  572. }
  573. }
  574. if ($gid) {
  575. if (isset($names['gid'][$gid])) {
  576. $stat['group'] = $names['gid'][$gid];
  577. } else if (is_callable('posix_getgrgid')) {
  578. $grgid = posix_getgrgid($gid);
  579. $stat['group'] = $names['gid'][$gid] = $grgid['name'];
  580. } else {
  581. $stat['group'] = $names['gid'][$gid] = $gid;
  582. }
  583. }
  584. return $stat;
  585. }
  586. /**
  587. * Return true if path is dir and has at least one childs directory
  588. *
  589. * @param string $path dir path
  590. *
  591. * @return bool
  592. * @author Dmitry (dio) Levashov
  593. **/
  594. protected function _subdirs($path)
  595. {
  596. $dirs = false;
  597. if (is_dir($path) && is_readable($path)) {
  598. if (class_exists('FilesystemIterator', false)) {
  599. $dirItr = new ParentIterator(
  600. new RecursiveDirectoryIterator($path,
  601. FilesystemIterator::SKIP_DOTS |
  602. FilesystemIterator::CURRENT_AS_SELF |
  603. (defined('RecursiveDirectoryIterator::FOLLOW_SYMLINKS') ?
  604. RecursiveDirectoryIterator::FOLLOW_SYMLINKS : 0)
  605. )
  606. );
  607. $dirItr->rewind();
  608. if ($dirItr->hasChildren()) {
  609. $dirs = true;
  610. $name = $dirItr->getSubPathName();
  611. while ($dirItr->valid()) {
  612. if (!$this->attr($path . DIRECTORY_SEPARATOR . $name, 'read', null, true)) {
  613. $dirs = false;
  614. $dirItr->next();
  615. $name = $dirItr->getSubPathName();
  616. continue;
  617. }
  618. $dirs = true;
  619. break;
  620. }
  621. }
  622. } else {
  623. $path = strtr($path, array('[' => '\\[', ']' => '\\]', '*' => '\\*', '?' => '\\?'));
  624. return (bool)glob(rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . '*', GLOB_ONLYDIR);
  625. }
  626. }
  627. return $dirs;
  628. }
  629. /**
  630. * Return object width and height
  631. * Usualy used for images, but can be realize for video etc...
  632. *
  633. * @param string $path file path
  634. * @param string $mime file mime type
  635. *
  636. * @return string
  637. * @author Dmitry (dio) Levashov
  638. **/
  639. protected function _dimensions($path, $mime)
  640. {
  641. clearstatcache();
  642. return strpos($mime, 'image') === 0 && is_readable($path) && filesize($path) && ($s = getimagesize($path)) !== false
  643. ? $s[0] . 'x' . $s[1]
  644. : false;
  645. }
  646. /******************** file/dir content *********************/
  647. /**
  648. * Return symlink target file
  649. *
  650. * @param string $path link path
  651. *
  652. * @return string
  653. * @author Dmitry (dio) Levashov
  654. **/
  655. protected function readlink($path)
  656. {
  657. if (!($target = readlink($path))) {
  658. return null;
  659. }
  660. if (strpos($target, $this->systemRoot) !== 0) {
  661. $target = $this->_joinPath(dirname($path), $target);
  662. }
  663. if (!file_exists($target)) {
  664. return false;
  665. }
  666. return $target;
  667. }
  668. /**
  669. * Return files list in directory.
  670. *
  671. * @param string $path dir path
  672. *
  673. * @return array
  674. * @throws elFinderAbortException
  675. * @author Dmitry (dio) Levashov
  676. */
  677. protected function _scandir($path)
  678. {
  679. elFinder::checkAborted();
  680. $files = array();
  681. $cache = array();
  682. $dirWritable = is_writable($path);
  683. $dirItr = array();
  684. $followSymLinks = $this->options['followSymLinks'];
  685. try {
  686. $dirItr = new DirectoryIterator($path);
  687. } catch (UnexpectedValueException $e) {
  688. }
  689. foreach ($dirItr as $file) {
  690. try {
  691. if ($file->isDot()) {
  692. continue;
  693. }
  694. $files[] = $fpath = $file->getPathname();
  695. $br = false;
  696. $stat = array();
  697. $stat['isowner'] = false;
  698. $linkreadable = false;
  699. if ($file->isLink()) {
  700. if (!$followSymLinks) {
  701. continue;
  702. }
  703. if (!($target = $this->readlink($fpath))
  704. || $target == $fpath) {
  705. if (is_null($target)) {
  706. $stat = array();
  707. $br = true;
  708. } else {
  709. $_path = $fpath;
  710. $stat['mime'] = 'symlink-broken';
  711. $target = readlink($_path);
  712. $lstat = lstat($_path);
  713. $ostat = $this->getOwnerStat($lstat['uid'], $lstat['gid']);
  714. $linkreadable = !empty($ostat['isowner']);
  715. $dir = false;
  716. $stat['alias'] = $this->_path($target);
  717. $stat['target'] = $target;
  718. }
  719. } else {
  720. $dir = is_dir($target);
  721. $stat['alias'] = $this->_path($target);
  722. $stat['target'] = $target;
  723. $stat['mime'] = $dir ? 'directory' : $this->mimetype($stat['alias']);
  724. }
  725. } else {
  726. if (($dir = $file->isDir()) && $this->options['detectDirIcon']) {
  727. $path = $file->getPathname();
  728. $favicon = $path . DIRECTORY_SEPARATOR . $this->options['detectDirIcon'];
  729. if ($this->URL && file_exists($favicon)) {
  730. $stat['icon'] = $this->URL . str_replace(DIRECTORY_SEPARATOR, '/', substr($favicon, strlen($this->root) + 1));
  731. }
  732. }
  733. $stat['mime'] = $dir ? 'directory' : $this->mimetype($fpath);
  734. }
  735. $size = sprintf('%u', $file->getSize());
  736. $stat['ts'] = $file->getMTime();
  737. if (!$br) {
  738. if ($this->statOwner && !$linkreadable) {
  739. $uid = $file->getOwner();
  740. $gid = $file->getGroup();
  741. $stat['perm'] = substr((string)decoct($file->getPerms()), -4);
  742. $stat = array_merge($stat, $this->getOwnerStat($uid, $gid));
  743. }
  744. //logical rights first
  745. $stat['read'] = ($linkreadable || $file->isReadable()) ? null : false;
  746. $stat['write'] = $file->isWritable() ? null : false;
  747. $stat['locked'] = $dirWritable ? null : true;
  748. if (is_null($stat['read'])) {
  749. $stat['size'] = $dir ? 0 : $size;
  750. }
  751. if ($this->options['statCorrector']) {
  752. call_user_func_array($this->options['statCorrector'], array(&$stat, $fpath, $this->statOwner, $this));
  753. }
  754. }
  755. $cache[] = array($fpath, $stat);
  756. } catch (RuntimeException $e) {
  757. continue;
  758. }
  759. }
  760. if ($cache) {
  761. $cache = $this->convEncOut($cache, false);
  762. foreach ($cache as $d) {
  763. $this->updateCache($d[0], $d[1]);
  764. }
  765. }
  766. return $files;
  767. }
  768. /**
  769. * Open file and return file pointer
  770. *
  771. * @param string $path file path
  772. * @param string $mode
  773. *
  774. * @return false|resource
  775. * @internal param bool $write open file for writing
  776. * @author Dmitry (dio) Levashov
  777. */
  778. protected function _fopen($path, $mode = 'rb')
  779. {
  780. return fopen($path, $mode);
  781. }
  782. /**
  783. * Close opened file
  784. *
  785. * @param resource $fp file pointer
  786. * @param string $path
  787. *
  788. * @return bool
  789. * @author Dmitry (dio) Levashov
  790. */
  791. protected function _fclose($fp, $path = '')
  792. {
  793. return (is_resource($fp) && fclose($fp));
  794. }
  795. /******************** file/dir manipulations *************************/
  796. /**
  797. * Create dir and return created dir path or false on failed
  798. *
  799. * @param string $path parent dir path
  800. * @param string $name new directory name
  801. *
  802. * @return string|bool
  803. * @author Dmitry (dio) Levashov
  804. **/
  805. protected function _mkdir($path, $name)
  806. {
  807. $path = $this->_joinPath($path, $name);
  808. if (mkdir($path)) {
  809. chmod($path, $this->options['dirMode']);
  810. return $path;
  811. }
  812. return false;
  813. }
  814. /**
  815. * Create file and return it's path or false on failed
  816. *
  817. * @param string $path parent dir path
  818. * @param string $name new file name
  819. *
  820. * @return string|bool
  821. * @author Dmitry (dio) Levashov
  822. **/
  823. protected function _mkfile($path, $name)
  824. {
  825. $path = $this->_joinPath($path, $name);
  826. if (($fp = fopen($path, 'w'))) {
  827. fclose($fp);
  828. chmod($path, $this->options['fileMode']);
  829. return $path;
  830. }
  831. return false;
  832. }
  833. /**
  834. * Create symlink
  835. *
  836. * @param string $source file to link to
  837. * @param string $targetDir folder to create link in
  838. * @param string $name symlink name
  839. *
  840. * @return bool
  841. * @author Dmitry (dio) Levashov
  842. **/
  843. protected function _symlink($source, $targetDir, $name)
  844. {
  845. return symlink($source, $this->_joinPath($targetDir, $name));
  846. }
  847. /**
  848. * Copy file into another file
  849. *
  850. * @param string $source source file path
  851. * @param string $targetDir target directory path
  852. * @param string $name new file name
  853. *
  854. * @return bool
  855. * @author Dmitry (dio) Levashov
  856. **/
  857. protected function _copy($source, $targetDir, $name)
  858. {
  859. $mtime = filemtime($source);
  860. $target = $this->_joinPath($targetDir, $name);
  861. if ($ret = copy($source, $target)) {
  862. isset($this->options['keepTimestamp']['copy']) && $mtime && touch($target, $mtime);
  863. }
  864. return $ret;
  865. }
  866. /**
  867. * Move file into another parent dir.
  868. * Return new file path or false.
  869. *
  870. * @param string $source source file path
  871. * @param $targetDir
  872. * @param string $name file name
  873. *
  874. * @return bool|string
  875. * @internal param string $target target dir path
  876. * @author Dmitry (dio) Levashov
  877. */
  878. protected function _move($source, $targetDir, $name)
  879. {
  880. $mtime = filemtime($source);
  881. $target = $this->_joinPath($targetDir, $name);
  882. if ($ret = rename($source, $target) ? $target : false) {
  883. isset($this->options['keepTimestamp']['move']) && $mtime && touch($target, $mtime);
  884. }
  885. return $ret;
  886. }
  887. /**
  888. * Remove file
  889. *
  890. * @param string $path file path
  891. *
  892. * @return bool
  893. * @author Dmitry (dio) Levashov
  894. **/
  895. protected function _unlink($path)
  896. {
  897. return is_file($path) && unlink($path);
  898. }
  899. /**
  900. * Remove dir
  901. *
  902. * @param string $path dir path
  903. *
  904. * @return bool
  905. * @author Dmitry (dio) Levashov
  906. **/
  907. protected function _rmdir($path)
  908. {
  909. return rmdir($path);
  910. }
  911. /**
  912. * Create new file and write into it from file pointer.
  913. * Return new file path or false on error.
  914. *
  915. * @param resource $fp file pointer
  916. * @param string $dir target dir path
  917. * @param string $name file name
  918. * @param array $stat file stat (required by some virtual fs)
  919. *
  920. * @return bool|string
  921. * @author Dmitry (dio) Levashov
  922. **/
  923. protected function _save($fp, $dir, $name, $stat)
  924. {
  925. $path = $this->_joinPath($dir, $name);
  926. $meta = stream_get_meta_data($fp);
  927. $uri = isset($meta['uri']) ? $meta['uri'] : '';
  928. if ($uri && !preg_match('#^[a-zA-Z0-9]+://#', $uri) && !is_link($uri)) {
  929. fclose($fp);
  930. $mtime = filemtime($uri);
  931. $isCmdPaste = ($this->ARGS['cmd'] === 'paste');
  932. $isCmdCopy = ($isCmdPaste && empty($this->ARGS['cut']));
  933. if (($isCmdCopy || !rename($uri, $path)) && !copy($uri, $path)) {
  934. return false;
  935. }
  936. // keep timestamp on upload
  937. if ($mtime && $this->ARGS['cmd'] === 'upload') {
  938. touch($path, isset($this->options['keepTimestamp']['upload']) ? $mtime : time());
  939. }
  940. } else {
  941. if (file_put_contents($path, $fp, LOCK_EX) === false) {
  942. return false;
  943. }
  944. }
  945. chmod($path, $this->options['fileMode']);
  946. return $path;
  947. }
  948. /**
  949. * Get file contents
  950. *
  951. * @param string $path file path
  952. *
  953. * @return string|false
  954. * @author Dmitry (dio) Levashov
  955. **/
  956. protected function _getContents($path)
  957. {
  958. return file_get_contents($path);
  959. }
  960. /**
  961. * Write a string to a file
  962. *
  963. * @param string $path file path
  964. * @param string $content new file content
  965. *
  966. * @return bool
  967. * @author Dmitry (dio) Levashov
  968. **/
  969. protected function _filePutContents($path, $content)
  970. {
  971. return (file_put_contents($path, $content, LOCK_EX) !== false);
  972. }
  973. /**
  974. * Detect available archivers
  975. *
  976. * @return void
  977. * @throws elFinderAbortException
  978. */
  979. protected function _checkArchivers()
  980. {
  981. $this->archivers = $this->getArchivers();
  982. return;
  983. }
  984. /**
  985. * chmod availability
  986. *
  987. * @param string $path
  988. * @param string $mode
  989. *
  990. * @return bool
  991. */
  992. protected function _chmod($path, $mode)
  993. {
  994. $modeOct = is_string($mode) ? octdec($mode) : octdec(sprintf("%04o", $mode));
  995. return chmod($path, $modeOct);
  996. }
  997. /**
  998. * Recursive symlinks search
  999. *
  1000. * @param string $path file/dir path
  1001. *
  1002. * @return bool
  1003. * @throws Exception
  1004. * @author Dmitry (dio) Levashov
  1005. */
  1006. protected function _findSymlinks($path)
  1007. {
  1008. return self::localFindSymlinks($path);
  1009. }
  1010. /**
  1011. * Extract files from archive
  1012. *
  1013. * @param string $path archive path
  1014. * @param array $arc archiver command and arguments (same as in $this->archivers)
  1015. *
  1016. * @return array|string|boolean
  1017. * @throws elFinderAbortException
  1018. * @author Dmitry (dio) Levashov,
  1019. * @author Alexey Sukhotin
  1020. */
  1021. protected function _extract($path, $arc)
  1022. {
  1023. if ($this->quarantine) {
  1024. $dir = $this->quarantine . DIRECTORY_SEPARATOR . md5(basename($path) . mt_rand());
  1025. $archive = (isset($arc['toSpec']) || $arc['cmd'] === 'phpfunction') ? '' : $dir . DIRECTORY_SEPARATOR . basename($path);
  1026. if (!mkdir($dir)) {
  1027. return false;
  1028. }
  1029. // insurance unexpected shutdown
  1030. register_shutdown_function(array($this, 'rmdirRecursive'), realpath($dir));
  1031. chmod($dir, 0777);
  1032. // copy in quarantine
  1033. if (!is_readable($path) || ($archive && !copy($path, $archive))) {
  1034. return false;
  1035. }
  1036. // extract in quarantine
  1037. try {
  1038. $this->unpackArchive($path, $arc, $archive ? true : $dir);
  1039. } catch(Exception $e) {
  1040. return $this->setError($e->getMessage());
  1041. }
  1042. // get files list
  1043. try {
  1044. $ls = self::localScandir($dir);
  1045. } catch (Exception $e) {
  1046. return false;
  1047. }
  1048. // no files - extract error ?
  1049. if (empty($ls)) {
  1050. return false;
  1051. }
  1052. $this->archiveSize = 0;
  1053. // find symlinks and check extracted items
  1054. $checkRes = $this->checkExtractItems($dir);
  1055. if ($checkRes['symlinks']) {
  1056. self::localRmdirRecursive($dir);
  1057. return $this->setError(array_merge($this->error, array(elFinder::ERROR_ARC_SYMLINKS)));
  1058. }
  1059. $this->archiveSize = $checkRes['totalSize'];
  1060. if ($checkRes['rmNames']) {
  1061. foreach ($checkRes['rmNames'] as $name) {
  1062. $this->addError(elFinder::ERROR_SAVE, $name);
  1063. }
  1064. }
  1065. // check max files size
  1066. if ($this->options['maxArcFilesSize'] > 0 && $this->options['maxArcFilesSize'] < $this->archiveSize) {
  1067. $this->delTree($dir);
  1068. return $this->setError(elFinder::ERROR_ARC_MAXSIZE);
  1069. }
  1070. $extractTo = $this->extractToNewdir; // 'auto', ture or false
  1071. // archive contains one item - extract in archive dir
  1072. $name = '';
  1073. $src = $dir . DIRECTORY_SEPARATOR . $ls[0];
  1074. if (($extractTo === 'auto' || !$extractTo) && count($ls) === 1 && is_file($src)) {
  1075. $name = $ls[0];
  1076. } else if ($extractTo === 'auto' || $extractTo) {
  1077. // for several files - create new directory
  1078. // create unique name for directory
  1079. $src = $dir;
  1080. $splits = elFinder::splitFileExtention(basename($path));
  1081. $name = $splits[0];
  1082. $test = dirname($path) . DIRECTORY_SEPARATOR . $name;
  1083. if (file_exists($test) || is_link($test)) {
  1084. $name = $this->uniqueName(dirname($path), $name, '-', false);
  1085. }
  1086. }
  1087. if ($name !== '') {
  1088. $result = dirname($path) . DIRECTORY_SEPARATOR . $name;
  1089. if (!rename($src, $result)) {
  1090. $this->delTree($dir);
  1091. return false;
  1092. }
  1093. } else {
  1094. $dstDir = dirname($path);
  1095. $result = array();
  1096. foreach ($ls as $name) {
  1097. $target = $dstDir . DIRECTORY_SEPARATOR . $name;
  1098. if (self::localMoveRecursive($dir . DIRECTORY_SEPARATOR . $name, $target, true, $this->options['copyJoin'])) {
  1099. $result[] = $target;
  1100. }
  1101. }
  1102. if (!$result) {
  1103. $this->delTree($dir);
  1104. return false;
  1105. }
  1106. }
  1107. is_dir($dir) && $this->delTree($dir);
  1108. return (is_array($result) || file_exists($result)) ? $result : false;
  1109. }
  1110. //TODO: Add return statement here
  1111. return false;
  1112. }
  1113. /**
  1114. * Create archive and return its path
  1115. *
  1116. * @param string $dir target dir
  1117. * @param array $files files names list
  1118. * @param string $name archive name
  1119. * @param array $arc archiver options
  1120. *
  1121. * @return string|bool
  1122. * @throws elFinderAbortException
  1123. * @author Dmitry (dio) Levashov,
  1124. * @author Alexey Sukhotin
  1125. */
  1126. protected function _archive($dir, $files, $name, $arc)
  1127. {
  1128. return $this->makeArchive($dir, $files, $name, $arc);
  1129. }
  1130. /******************** Over write functions *************************/
  1131. /**
  1132. * File path of local server side work file path
  1133. *
  1134. * @param string $path
  1135. *
  1136. * @return string
  1137. * @author Naoki Sawada
  1138. */
  1139. protected function getWorkFile($path)
  1140. {
  1141. return $path;
  1142. }
  1143. /**
  1144. * Delete dirctory trees
  1145. *
  1146. * @param string $localpath path need convert encoding to server encoding
  1147. *
  1148. * @return boolean
  1149. * @throws elFinderAbortException
  1150. * @author Naoki Sawada
  1151. */
  1152. protected function delTree($localpath)
  1153. {
  1154. return $this->rmdirRecursive($localpath);
  1155. }
  1156. /**
  1157. * Return fileinfo based on filename
  1158. * For item ID based path file system
  1159. * Please override if needed on each drivers
  1160. *
  1161. * @param string $path file cache
  1162. *
  1163. * @return array|boolean false
  1164. */
  1165. protected function isNameExists($path)
  1166. {
  1167. $exists = file_exists($this->convEncIn($path));
  1168. // restore locale
  1169. $this->convEncOut();
  1170. return $exists ? $this->stat($path) : false;
  1171. }
  1172. /******************** Over write (Optimized) functions *************************/
  1173. /**
  1174. * Recursive files search
  1175. *
  1176. * @param string $path dir path
  1177. * @param string $q search string
  1178. * @param array $mimes
  1179. *
  1180. * @return array
  1181. * @throws elFinderAbortException
  1182. * @author Dmitry (dio) Levashov
  1183. * @author Naoki Sawada
  1184. */
  1185. protected function doSearch($path, $q, $mimes)
  1186. {
  1187. if (!empty($this->doSearchCurrentQuery['matchMethod']) || $this->encoding || !class_exists('FilesystemIterator', false)) {
  1188. // has custom match method or non UTF-8, use elFinderVolumeDriver::doSearch()
  1189. return parent::doSearch($path, $q, $mimes);
  1190. }
  1191. $result = array();
  1192. $timeout = $this->options['searchTimeout'] ? $this->searchStart + $this->options['searchTimeout'] : 0;
  1193. if ($timeout && $timeout < time()) {
  1194. $this->setError(elFinder::ERROR_SEARCH_TIMEOUT, $this->path($this->encode($path)));
  1195. return $result;
  1196. }
  1197. elFinder::extendTimeLimit($this->options['searchTimeout'] + 30);
  1198. $match = array();
  1199. try {
  1200. $iterator = new RecursiveIteratorIterator(
  1201. new RecursiveCallbackFilterIterator(
  1202. new RecursiveDirectoryIterator($path,
  1203. FilesystemIterator::KEY_AS_PATHNAME |
  1204. FilesystemIterator::SKIP_DOTS |
  1205. ((defined('RecursiveDirectoryIterator::FOLLOW_SYMLINKS') && $this->options['followSymLinks']) ?
  1206. RecursiveDirectoryIterator::FOLLOW_SYMLINKS : 0)
  1207. ),
  1208. array($this, 'localFileSystemSearchIteratorFilter')
  1209. ),
  1210. RecursiveIteratorIterator::SELF_FIRST,
  1211. RecursiveIteratorIterator::CATCH_GET_CHILD
  1212. );
  1213. foreach ($iterator as $key => $node) {
  1214. if ($timeout && ($this->error || $timeout < time())) {
  1215. !$this->error && $this->setError(elFinder::ERROR_SEARCH_TIMEOUT, $this->path($this->encode($node->getPath)));
  1216. break;
  1217. }
  1218. if ($node->isDir()) {
  1219. if ($this->stripos($node->getFilename(), $q) !== false) {
  1220. $match[] = $key;
  1221. }
  1222. } else {
  1223. $match[] = $key;
  1224. }
  1225. }
  1226. } catch (Exception $e) {
  1227. }
  1228. if ($match) {
  1229. foreach ($match as $p) {
  1230. if ($timeout && ($this->error || $timeout < time())) {
  1231. !$this->error && $this->setError(elFinder::ERROR_SEARCH_TIMEOUT, $this->path($this->encode(dirname($p))));
  1232. break;
  1233. }
  1234. $stat = $this->stat($p);
  1235. if (!$stat) { // invalid links
  1236. continue;
  1237. }
  1238. if (!empty($stat['hidden']) || !$this->mimeAccepted($stat['mime'], $mimes)) {
  1239. continue;
  1240. }
  1241. if ((!$mimes || $stat['mime'] !== 'directory')) {
  1242. $stat['path'] = $this->path($stat['hash']);
  1243. if ($this->URL && !isset($stat['url'])) {
  1244. $_path = str_replace(DIRECTORY_SEPARATOR, '/', substr($p, strlen($this->root) + 1));
  1245. $stat['url'] = $this->URL . str_replace('%2F', '/', rawurlencode($_path));
  1246. }
  1247. $result[] = $stat;
  1248. }
  1249. }
  1250. }
  1251. return $result;
  1252. }
  1253. /******************** Original local functions ************************
  1254. *
  1255. * @param $file
  1256. * @param $key
  1257. * @param $iterator
  1258. *
  1259. * @return bool
  1260. */
  1261. public function localFileSystemSearchIteratorFilter($file, $key, $iterator)
  1262. {
  1263. /* @var FilesystemIterator $file */
  1264. /* @var RecursiveDirectoryIterator $iterator */
  1265. $name = $file->getFilename();
  1266. if ($this->doSearchCurrentQuery['excludes']) {
  1267. foreach ($this->doSearchCurrentQuery['excludes'] as $exclude) {
  1268. if ($this->stripos($name, $exclude) !== false) {
  1269. return false;
  1270. }
  1271. }
  1272. }
  1273. if ($iterator->hasChildren()) {
  1274. if ($this->options['searchExDirReg'] && preg_match($this->options['searchExDirReg'], $key)) {
  1275. return false;
  1276. }
  1277. return (bool)$this->attr($key, 'read', null, true);
  1278. }
  1279. return ($this->stripos($name, $this->doSearchCurrentQuery['q']) === false) ? false : true;
  1280. }
  1281. /**
  1282. * Creates a symbolic link
  1283. *
  1284. * @param string $target The target
  1285. * @param string $link The link
  1286. *
  1287. * @return boolean ( result of symlink() )
  1288. */
  1289. protected function localFileSystemSymlink($target, $link)
  1290. {
  1291. $res = false;
  1292. $errlev = error_reporting();
  1293. error_reporting($errlev ^ E_WARNING);
  1294. if ($res = symlink(realpath($target), $link)) {
  1295. $res = is_readable($link);
  1296. }
  1297. error_reporting($errlev);
  1298. return $res;
  1299. }
  1300. } // END class