files.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. <?php
  2. /*
  3. * phpMeccano v0.2.0. Web-framework written with php programming language. Core module [files.php].
  4. * Copyright (C) 2015-2019 Alexei Muzarov
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * e-mail: azexmail@gmail.com
  21. * e-mail: azexmail@mail.ru
  22. * https://bitbucket.org/azexmail/phpmeccano
  23. */
  24. namespace core;
  25. require_once MECCANO_CORE_DIR.'/swconst.php';
  26. interface intFiles {
  27. public static function errId();
  28. public static function errExp();
  29. public static function copy($sourcePath, $destPath, $mergeDirs = false, $rewriteFiles = false, $skipExistentFiles = false, $skipNotReadable = false, $skipWriteProtected = false, $skipConflicts = false, $removeConflictFiles = false);
  30. public static function move($sourcePath, $destPath, $mergeDirs = false, $replaceFiles = false, $skipExistentFiles = false, $skipNotReadable = false, $skipWriteProtected = false, $skipConflicts = false, $removeConflictFiles = false);
  31. public static function remove($sourcePath, $skipNotReadable = false, $skipWriteProtected = false, $skipConflicts = false);
  32. }
  33. class Files implements intFiles {
  34. private static $errid = 0; // error's id
  35. private static $errexp = ''; // error's explanation
  36. private static function setError($id, $exp) {
  37. self::$errid = $id;
  38. self::$errexp = $exp;
  39. if (MECCANO_SHOW_ERRORS) {
  40. echo "<br/><span style='font-style: large; padding: 10px; background: yellow; display: inline-block; color: red'>ERROR $id<br/>$exp</span><br/>";
  41. }
  42. }
  43. private static function zeroizeError() {
  44. self::$errid = 0; self::$errexp = '';
  45. }
  46. public static function errId() {
  47. return self::$errid;
  48. }
  49. public static function errExp() {
  50. return self::$errexp;
  51. }
  52. public static function copy($sourcePath, $destPath, $mergeDirs = false, $rewriteFiles = false, $skipExistentFiles = false, $skipNotReadable = false, $skipWriteProtected = false, $skipConflicts = false, $removeConflictFiles = false) {
  53. self::zeroizeError();
  54. $sourcePath = rtrim(preg_replace('#[/]+#', '/', $sourcePath), '/');
  55. $destPath = rtrim(preg_replace('#[/]+#', '/', $destPath), '/');
  56. if (!file_exists($sourcePath)) {
  57. self::setError(ERROR_NOT_FOUND, 'copy: source was not found');
  58. return false;
  59. }
  60. elseif (!is_readable($sourcePath)) {
  61. self::setError(ERROR_RESTRICTED_ACCESS, 'copy: source is not readable');
  62. return false;
  63. }
  64. // source is symbolic link
  65. if (is_link($sourcePath)) {
  66. if (is_dir($destPath) && !is_link($destPath)) {
  67. self::setError(ERROR_NOT_EXECUTED, "copy: unable to replace directory [$destPath] with file");
  68. return false;
  69. }
  70. else {
  71. $destDirPath = dirname($destPath);
  72. if (is_dir($destDirPath)) {
  73. $destDirWriteStatus = is_writable($destDirPath);
  74. }
  75. else {
  76. self::setError(ERROR_NOT_FOUND, "copy: directory [$destDirPath] was not found");
  77. return false;
  78. }
  79. if (!$destDirWriteStatus) {
  80. self::setError(ERROR_RESTRICTED_ACCESS, "copy: destination directory [$destDirPath] is write-protected");
  81. return false;
  82. }
  83. else {
  84. if (is_file($destPath) || is_link($destPath)) {
  85. if (!$rewriteFiles) {
  86. self::setError(ERROR_NOT_EXECUTED, 'copy: rewriting of files is not allowed');
  87. return false;
  88. }
  89. unlink($destPath);
  90. $sourceLinkTarget = readlink($sourcePath);
  91. symlink($sourceLinkTarget, $destPath);
  92. return true;
  93. }
  94. else {
  95. $sourceLinkTarget = readlink($sourcePath);
  96. symlink($sourceLinkTarget, $destPath);
  97. return true;
  98. }
  99. }
  100. }
  101. }
  102. // source is file
  103. elseif (is_file($sourcePath)) {
  104. if (is_dir($destPath) && !is_link($destPath)) {
  105. self::setError(ERROR_NOT_EXECUTED, "copy: unable to replace directory [$destPath] with file");
  106. return false;
  107. }
  108. else {
  109. $destDirPath = dirname($destPath);
  110. if (is_dir($destDirPath)) {
  111. $destDirWriteStatus = is_writable($destDirPath);
  112. }
  113. else {
  114. self::setError(ERROR_NOT_FOUND, "copy: directory [$destDirPath] was not found");
  115. return false;
  116. }
  117. if (is_link($destPath)) {
  118. if (!$rewriteFiles) {
  119. self::setError(ERROR_NOT_EXECUTED, 'copy: rewriting of files is not allowed');
  120. return false;
  121. }
  122. if (!$destDirWriteStatus) {
  123. self::setError(ERROR_RESTRICTED_ACCESS, "copy: destination directory [$destDirPath] is write-protected");
  124. return false;
  125. }
  126. unlink($destPath);
  127. copy($sourcePath, $destPath);
  128. return true;
  129. }
  130. elseif (is_file($destPath)) {
  131. $destFileWriteStatus = is_writable($destPath);
  132. if (!$rewriteFiles) {
  133. self::setError(ERROR_NOT_EXECUTED, "copy: rewriting of files is not allowed");
  134. return false;
  135. }
  136. elseif ($destFileWriteStatus) {
  137. copy($sourcePath, $destPath);
  138. return true;
  139. }
  140. elseif (!$destFileWriteStatus) {
  141. self::setError(ERROR_RESTRICTED_ACCESS, "copy: destination file [$destPath] is write-protected");
  142. return false;
  143. }
  144. }
  145. else {
  146. if (!$destDirWriteStatus) {
  147. self::setError(ERROR_RESTRICTED_ACCESS, "copy: destination directory [$destDirPath] is write-protected");
  148. return false;
  149. }
  150. copy($sourcePath, $destPath);
  151. return true;
  152. }
  153. }
  154. }
  155. // source is directory
  156. else {
  157. if (is_file($destPath)) {
  158. self::setError(ERROR_NOT_EXECUTED, 'copy: unable to replace file with directory');
  159. return false;
  160. }
  161. $destDirPath = dirname($destPath);
  162. if (!is_dir($destPath)) {
  163. if (is_writable($destDirPath)) {
  164. mkdir($destPath);
  165. }
  166. else {
  167. self::setError(ERROR_RESTRICTED_ACCESS, "copy: unable to create destination directory [$destPath]");
  168. return false;
  169. }
  170. }
  171. elseif (!is_writable($destPath)) {
  172. self::setError(ERROR_RESTRICTED_ACCESS, "copy: destination directory [$destPath] is write-protected");
  173. return false;
  174. }
  175. elseif (!$mergeDirs) {
  176. self::setError(ERROR_NOT_EXECUTED, 'copy: merging of directories is not allowed');
  177. return false;
  178. }
  179. $sourceReal = realpath($sourcePath);
  180. $destReal = realpath($destPath);
  181. $sourceLen = strlen($sourceReal);
  182. $destLen = strlen($destReal);
  183. if (($sourceReal == $destReal) || (($sourceLen < $destLen) && ($sourceReal == substr($destReal, 0, $sourceLen)))) {
  184. self::setError(ERROR_NOT_EXECUTED, "copy: unable to copy directory [$sourcePath] into itself");
  185. return false;
  186. }
  187. $stack = [$sourcePath];
  188. $divPosition = strlen($sourcePath) + 1;
  189. while (count($stack)) {
  190. $sourceDirPath = array_pop($stack);
  191. $destDirPath = rtrim("$destPath/".substr($sourceDirPath, $divPosition), "/");
  192. $destDirWriteStatus = is_writable($destDirPath);
  193. $sourceDirContent = array_diff(scandir($sourceDirPath), ['.', '..']);
  194. foreach ($sourceDirContent as $sourceItemName) {
  195. $sourceFullPath = "$sourceDirPath/$sourceItemName";
  196. $destFullPath = "$destDirPath/$sourceItemName";
  197. $sourceItemReadStatus = is_readable($sourceFullPath);
  198. if (is_link($sourceFullPath)) {// link handling
  199. if ($removeConflictFiles && is_dir($destFullPath) && !is_link($destFullPath)) {
  200. if (!self::remove($destFullPath)) {
  201. return false;
  202. }
  203. }
  204. if (is_dir($destFullPath) && !is_link($destFullPath) && !$skipConflicts) {
  205. self::setError(ERROR_ALREADY_EXISTS, "copy: unable to replace directory [$destFullPath] with link [$sourceFullPath]");
  206. return false;
  207. }
  208. elseif (is_file($destFullPath) || is_link($destFullPath)) {
  209. if ($rewriteFiles && $destDirWriteStatus && $sourceItemReadStatus) {
  210. unlink($destFullPath);
  211. $sourceLinkTarget = readlink($sourceFullPath);
  212. symlink($sourceLinkTarget, $destFullPath);
  213. }
  214. elseif ($rewriteFiles && !$sourceItemReadStatus && !$skipNotReadable) {
  215. self::setError(ERROR_RESTRICTED_ACCESS, "copy: unable to read link [$sourceFullPath]");
  216. return false;
  217. }
  218. elseif ($rewriteFiles && !$destDirWriteStatus && !$skipWriteProtected) {
  219. self::setError(ERROR_RESTRICTED_ACCESS, "copy: directory [$destDirPath] is write-protected");
  220. return false;
  221. }
  222. elseif (!$skipExistentFiles) {
  223. self::setError(ERROR_NOT_EXECUTED, "copy: file [$destFullPath] already exists");
  224. return false;
  225. }
  226. }
  227. elseif (!is_dir($destFullPath) && $destDirWriteStatus && $sourceItemReadStatus) {
  228. $sourceLinkTarget = readlink($sourceFullPath);
  229. symlink($sourceLinkTarget, $destFullPath);
  230. }
  231. elseif (!$sourceItemReadStatus && !$skipNotReadable) {
  232. self::setError(ERROR_RESTRICTED_ACCESS, "copy: unable to read link [$sourceFullPath]");
  233. return false;
  234. }
  235. elseif (!$destDirWriteStatus && !$skipWriteProtected) {
  236. self::setError(ERROR_RESTRICTED_ACCESS, "copy: directory [$destDirPath] is write-protected");
  237. return false;
  238. }
  239. }
  240. elseif (is_file($sourceFullPath)) { // file handling
  241. if ($removeConflictFiles && is_dir($destFullPath) && !is_link($destFullPath)) {
  242. if (!self::remove($destFullPath)) {
  243. return false;
  244. }
  245. }
  246. if (is_dir($destFullPath) && !is_link($destFullPath) && !$skipConflicts) {
  247. self::setError(ERROR_ALREADY_EXISTS, "copy: unable to replace directory [$destFullPath] with file [$sourceFullPath]");
  248. return false;
  249. }
  250. //
  251. elseif (is_link($destFullPath)) {
  252. if ($rewriteFiles && $destDirWriteStatus && $sourceItemReadStatus) {
  253. unlink($destFullPath);
  254. $sourceLinkTarget = readlink($sourceFullPath);
  255. symlink($sourceLinkTarget, $destFullPath);
  256. }
  257. elseif ($rewriteFiles && !$sourceItemReadStatus && !$skipNotReadable) {
  258. self::setError(ERROR_RESTRICTED_ACCESS, "copy: unable to read file [$sourceFullPath]");
  259. return false;
  260. }
  261. elseif ($rewriteFiles && !$destDirWriteStatus && !$skipWriteProtected) {
  262. self::setError(ERROR_RESTRICTED_ACCESS, "copy: directory [$destDirPath] is write-protected");
  263. return false;
  264. }
  265. elseif (!$skipExistentFiles) {
  266. self::setError(ERROR_NOT_EXECUTED, "copy: file [$destFullPath] already exists");
  267. return false;
  268. }
  269. }
  270. elseif (is_file($destFullPath)) {
  271. $destFileWriteStatus = is_writable($destFullPath);
  272. if ($rewriteFiles && $sourceItemReadStatus && $destFileWriteStatus) {
  273. copy($sourceFullPath, $destFullPath);
  274. }
  275. elseif ($rewriteFiles && !$sourceItemReadStatus && !$skipNotReadable) {
  276. self::setError(ERROR_RESTRICTED_ACCESS, "copy: unable to read file [$sourceFullPath]");
  277. return false;
  278. }
  279. elseif ($rewriteFiles && !$destFileWriteStatus && !$skipWriteProtected) {
  280. self::setError(ERROR_RESTRICTED_ACCESS, "copy: file [$destFullPath] is write-protected");
  281. return false;
  282. }
  283. elseif (!$skipExistentFiles) {
  284. self::setError(ERROR_NOT_EXECUTED, "copy: file [$destFullPath] already exists");
  285. return false;
  286. }
  287. }
  288. elseif (!is_dir($destFullPath) && $destDirWriteStatus && $sourceItemReadStatus) {
  289. copy($sourceFullPath, $destFullPath);
  290. }
  291. elseif (!$destDirWriteStatus && !$skipWriteProtected) {
  292. self::setError(ERROR_RESTRICTED_ACCESS, "copy: directory [$destDirPath] is write-protected");
  293. return false;
  294. }
  295. elseif (!$sourceItemReadStatus && !$skipNotReadable) {
  296. self::setError(ERROR_RESTRICTED_ACCESS, "copy: unable to read file [$sourceFullPath]");
  297. return false;
  298. }
  299. }
  300. elseif (is_dir($sourceFullPath)) { // directory handling
  301. if ($removeConflictFiles && is_file($destFullPath)) {
  302. if (!self::remove($destFullPath)) {
  303. return false;
  304. }
  305. }
  306. $fileConflict = is_file($destFullPath);
  307. if ($fileConflict && !$skipConflicts) {
  308. self::setError(ERROR_ALREADY_EXISTS, "copy: unable to replace file [$destFullPath] with directory [$sourceFullPath]");
  309. return false;
  310. }
  311. elseif ($sourceItemReadStatus && is_dir($destFullPath)) {
  312. $stack[] = $sourceFullPath;
  313. }
  314. elseif ($sourceItemReadStatus && !is_dir($destFullPath) && !$fileConflict && $destDirWriteStatus) {
  315. $stack[] = $sourceFullPath;
  316. mkdir($destFullPath);
  317. }
  318. elseif (!$sourceItemReadStatus && !$skipNotReadable) {
  319. self::setError(ERROR_RESTRICTED_ACCESS, "copy: unable to read directory [$sourceFullPath]");
  320. return false;
  321. }
  322. elseif (is_dir($destFullPath) && !is_writable($destFullPath) && !$skipWriteProtected) {
  323. self::setError(ERROR_RESTRICTED_ACCESS, "copy: directory [$destFullPath] is write-protected");
  324. return false;
  325. }
  326. elseif (!$destDirWriteStatus && !$skipWriteProtected) {
  327. self::setError(ERROR_RESTRICTED_ACCESS, "copy: directory [$destDirPath] is write-protected");
  328. return false;
  329. }
  330. }
  331. elseif (!$skipNotReadable) {
  332. self::setError(ERROR_RESTRICTED_ACCESS, "copy: directory [$sourceDirPath] lists files only");
  333. return false;
  334. }
  335. else {
  336. $sourceDirContent = [];
  337. }
  338. }
  339. }
  340. return true;
  341. }
  342. }
  343. public static function move($sourcePath, $destPath, $mergeDirs = false, $replaceFiles = false, $skipExistentFiles = false, $skipNotReadable = false, $skipWriteProtected = false, $skipConflicts = false, $removeConflictFiles = false) {
  344. self::zeroizeError();
  345. $sourcePath = rtrim(preg_replace('#[/]+#', '/', $sourcePath), '/');
  346. $destPath = rtrim(preg_replace('#[/]+#', '/', $destPath), '/');
  347. if (!file_exists($sourcePath)) {
  348. self::setError(ERROR_NOT_FOUND, "move: source [$sourcePath] was not found");
  349. return false;
  350. }
  351. // source is file or symbolic link
  352. if (is_file($sourcePath) || is_link($sourcePath)) {
  353. if (is_dir($destPath)) {
  354. self::setError(ERROR_NOT_EXECUTED, "move: unable to replace directory [$destPath] with file");
  355. return false;
  356. }
  357. else {
  358. $sourceDirPath = dirname($sourcePath);
  359. if (!is_writable($sourceDirPath)) {
  360. self::setError(ERROR_RESTRICTED_ACCESS, "move: source directory [$sourceDirPath] is write-protected");
  361. return false;
  362. }
  363. $destDirPath = dirname($destPath);
  364. if (is_dir($destDirPath)) {
  365. if (!is_writable($destDirPath)) {
  366. self::setError(ERROR_RESTRICTED_ACCESS, "move: destination directory [$destDirPath] is write-protected");
  367. return false;
  368. }
  369. }
  370. else {
  371. self::setError(ERROR_NOT_FOUND, "move: directory [$destDirPath] was not found");
  372. return false;
  373. }
  374. if (is_file($destPath) || is_link($destPath)) {
  375. if (!$replaceFiles) {
  376. self::setError(ERROR_NOT_EXECUTED, 'move: replacing of files is not allowed');
  377. return false;
  378. }
  379. }
  380. if (@rename($sourcePath, $destPath)) {
  381. return true;
  382. }
  383. else {
  384. self::setError(ERROR_RESTRICTED_ACCESS, "move: unable to move file [$sourcePath]. Probably you tried to move not readable file to another disk partition.");
  385. return false;
  386. }
  387. }
  388. }
  389. // source is directory
  390. elseif (is_dir($sourcePath)) {
  391. if (!is_readable($sourcePath)) {
  392. self::setError(ERROR_RESTRICTED_ACCESS, "move: source directory [$sourcePath] is not readable");
  393. return false;
  394. }
  395. if (is_file($destPath)) {
  396. self::setError(ERROR_NOT_EXECUTED, "move: unable to replace file [$destPath] with directory [$sourcePath]");
  397. return false;
  398. }
  399. $destDirPath = dirname($destPath);
  400. if (!is_dir($destPath) && !is_link($destPath)) {
  401. if (is_writable($destDirPath)) {
  402. mkdir($destPath);
  403. }
  404. elseif (!is_dir($destDirPath)) {
  405. self::setError(ERROR_NOT_FOUND, "move: directory [$destDirPath] does not exist");
  406. return false;
  407. }
  408. else {
  409. self::setError(ERROR_RESTRICTED_ACCESS, "move: destination directory [$destDirPath] is write-protected");
  410. return false;
  411. }
  412. }
  413. elseif (!$mergeDirs) {
  414. self::setError(ERROR_NOT_EXECUTED, "move: merging of directories is not allowed");
  415. return false;
  416. }
  417. elseif (!is_writable($destPath)) {
  418. self::setError(ERROR_RESTRICTED_ACCESS, "move: destination directory [$destPath] is write-protected");
  419. return false;
  420. }
  421. $sourceReal = realpath($sourcePath);
  422. $destReal = realpath($destPath);
  423. $sourceLen = strlen($sourceReal);
  424. $destLen = strlen($destReal);
  425. if (($sourceReal == $destReal) || (($sourceLen < $destLen) && ($sourceReal == substr($destReal, 0, $sourceLen)))) {
  426. self::setError(ERROR_NOT_EXECUTED, "move: unable to move directory [$sourcePath] into itself");
  427. return false;
  428. }
  429. $stack = [$sourcePath];
  430. $divPosition = strlen($sourcePath) + 1;
  431. while (count($stack)) {
  432. $sourceDirPath = array_pop($stack);
  433. $sourceDirWriteStatus = is_writable($sourceDirPath);
  434. $dirTree[] = $sourceDirPath;
  435. $destDirPath = rtrim("$destPath/".substr($sourceDirPath, $divPosition), "/");
  436. $destDirWriteStatus = is_writable($destDirPath);
  437. foreach (array_diff(scandir($sourceDirPath), ['.', '..']) as $sourceItemName) {
  438. $sourceFullPath = "$sourceDirPath/$sourceItemName";
  439. $destFullPath = "$destDirPath/$sourceItemName";
  440. if ((is_file($sourceFullPath) || is_link($sourceFullPath))) {
  441. $sourceIsNotDir = 1;
  442. }
  443. else {
  444. $sourceIsNotDir = 0;
  445. }
  446. if ($sourceIsNotDir && $sourceDirWriteStatus && $destDirWriteStatus) { // file handling
  447. if (is_file($destFullPath) || is_link($destFullPath)) {
  448. $destIsFile = 1;
  449. }
  450. else {
  451. $destIsFile = 0;
  452. }
  453. if ($removeConflictFiles && is_dir($destFullPath) && !is_link($destFullPath)) {
  454. if (!self::remove($destFullPath)) {
  455. return false;
  456. }
  457. }
  458. if (is_dir($destFullPath) && !is_link($destFullPath) && !$skipConflicts) {
  459. self::setError(ERROR_ALREADY_EXISTS, "move: unable to replace directory [$destFullPath] with file [$sourceFullPath]");
  460. return false;
  461. }
  462. elseif (!$destIsFile) {
  463. if (!@rename($sourceFullPath, $destFullPath) && !$skipNotReadable) {
  464. self::setError(ERROR_RESTRICTED_ACCESS, "move: unable to move file [$sourceFullPath]. Probably you tried to move not readable file to another disk partition.");
  465. return false;
  466. }
  467. }
  468. elseif ($destIsFile && $replaceFiles) {
  469. if (!@rename($sourceFullPath, $destFullPath) && !$skipNotReadable) {
  470. self::setError(ERROR_RESTRICTED_ACCESS, "move: unable to move file [$sourceFullPath]. Probably you tried to move not readable file to another disk partition.");
  471. return false;
  472. }
  473. }
  474. elseif ($destIsFile && !$skipExistentFiles) {
  475. self::setError(ERROR_NOT_EXECUTED, "move: file [$destFullPath] already exists");
  476. return false;
  477. }
  478. }
  479. elseif (!$sourceIsNotDir) { // directory handling
  480. if ($removeConflictFiles && is_file($destFullPath)) {
  481. if (!self::remove($destFullPath)) {
  482. return false;
  483. }
  484. }
  485. if (is_file($destFullPath) && !$skipConflicts) {
  486. self::setError(ERROR_ALREADY_EXISTS, "move: unable to replace file [$destFullPath] with directory [$sourceFullPath]");
  487. return false;
  488. }
  489. elseif (is_readable($sourceFullPath) && is_dir($destFullPath)) {
  490. $stack[] = $sourceFullPath;
  491. }
  492. elseif (is_readable($sourceFullPath) && !is_dir($destFullPath) && $destDirWriteStatus) {
  493. $stack[] = $sourceFullPath;
  494. mkdir($destFullPath);
  495. }
  496. elseif (!is_readable($sourceFullPath) && !$skipNotReadable) {
  497. self::setError(ERROR_RESTRICTED_ACCESS, "move: unable to read directory [$sourceFullPath]");
  498. return false;
  499. }
  500. elseif (is_dir($destFullPath) && !is_writable($destFullPath) && !$skipWriteProtected) {
  501. self::setError(ERROR_RESTRICTED_ACCESS, "move: unable to write into [$destFullPath]");
  502. return false;
  503. }
  504. }
  505. elseif (!$sourceDirWriteStatus && !$skipWriteProtected) {
  506. self::setError(ERROR_RESTRICTED_ACCESS, "move: unable to move file [$sourceFullPath] because source directory [$sourceDirPath] is write-protected");
  507. return false;
  508. }
  509. elseif (!$destDirWriteStatus && !$skipWriteProtected) {
  510. self::setError(ERROR_RESTRICTED_ACCESS, "move: unable to move file [$sourceFullPath] because destination directory [$destDirPath] is write-protected");
  511. return false;
  512. }
  513. }
  514. }
  515. $dirTree = array_reverse($dirTree);
  516. foreach ($dirTree as $dirPath) {
  517. $parentDir = dirname($dirPath);
  518. $parentDirWriteStatus = is_writable($parentDir);
  519. $dirIsNotEmpty = array_diff(scandir($dirPath), ['.', '..']);
  520. if ($dirIsNotEmpty && !$skipConflicts) {
  521. self::setError(ERROR_NOT_EXECUTED, "move: source directory [$dirPath] is not empty");
  522. return false;
  523. }
  524. elseif (!$parentDirWriteStatus && !$skipWriteProtected) {
  525. self::setError(ERROR_RESTRICTED_ACCESS, "move: unable to remove directory [$dirPath] because parent directory [$parentDir] is write-protected");
  526. return false;
  527. }
  528. elseif (!$dirIsNotEmpty && $parentDirWriteStatus) {
  529. rmdir($dirPath);
  530. }
  531. }
  532. return true;
  533. }
  534. }
  535. public static function remove($sourcePath, $skipNotReadable = false, $skipWriteProtected = false, $skipConflicts = false) {
  536. self::zeroizeError();
  537. $sourcePath = rtrim(preg_replace('#[/]+#', '/', $sourcePath), '/');
  538. if (!file_exists($sourcePath)) {
  539. self::setError(ERROR_NOT_FOUND, "remove: source [$sourcePath] was not found");
  540. return false;
  541. }
  542. // source is file or symbolic link
  543. if (is_file($sourcePath) || is_link($sourcePath)) {
  544. $sourceDirPath = dirname($sourcePath);
  545. if (!is_writable($sourceDirPath)) {
  546. self::setError(ERROR_RESTRICTED_ACCESS, "remove: source directory [$sourceDirPath] is write-protected");
  547. return false;
  548. }
  549. unlink($sourcePath);
  550. return true;
  551. }
  552. // source is directory
  553. elseif (is_dir($sourcePath)) {
  554. if (!is_readable($sourcePath)) {
  555. self::setError(ERROR_RESTRICTED_ACCESS, "remove: source directory [$sourcePath] is not readable");
  556. return false;
  557. }
  558. $stack = [$sourcePath];
  559. $divPosition = strlen($sourcePath) + 1;
  560. while (count($stack)) {
  561. $sourceDirPath = array_pop($stack);
  562. $sourceDirWriteStatus = is_writable($sourceDirPath);
  563. $dirTree[] = $sourceDirPath;
  564. foreach (array_diff(scandir($sourceDirPath), ['.', '..']) as $sourceItemName) {
  565. $sourceFullPath = "$sourceDirPath/$sourceItemName";
  566. if ((is_file($sourceFullPath) || is_link($sourceFullPath))) {
  567. $sourceIsNotDir = 1;
  568. }
  569. else {
  570. $sourceIsNotDir = 0;
  571. }
  572. // file handling
  573. if ($sourceIsNotDir && $sourceDirWriteStatus) {
  574. unlink($sourceFullPath);
  575. }
  576. // directory handling
  577. elseif (!$sourceIsNotDir) {
  578. if (is_readable($sourceFullPath)) {
  579. $stack[] = $sourceFullPath;
  580. }
  581. elseif (!$skipNotReadable) {
  582. self::setError(ERROR_RESTRICTED_ACCESS, "remove: unable to read directory [$sourceFullPath]");
  583. return false;
  584. }
  585. }
  586. elseif (!$sourceDirWriteStatus && !$skipWriteProtected) {
  587. self::setError(ERROR_RESTRICTED_ACCESS, "remove: unable to remove file [$sourceFullPath] because source directory [$sourceDirPath] is write-protected");
  588. return false;
  589. }
  590. }
  591. }
  592. $dirTree = array_reverse($dirTree);
  593. foreach ($dirTree as $dirPath) {
  594. $parentDir = dirname($dirPath);
  595. $parentDirWriteStatus = is_writable($parentDir);
  596. $dirIsNotEmpty = array_diff(scandir($dirPath), ['.', '..']);
  597. if ($dirIsNotEmpty && !$skipConflicts) {
  598. self::setError(ERROR_NOT_EXECUTED, "remove: source directory [$dirPath] is not empty");
  599. return false;
  600. }
  601. elseif (!$parentDirWriteStatus && !$skipWriteProtected) {
  602. self::setError(ERROR_RESTRICTED_ACCESS, "remove: unable to remove directory [$dirPath] because parent directory [$parentDir] is write-protected");
  603. return false;
  604. }
  605. elseif (!$dirIsNotEmpty && $parentDirWriteStatus) {
  606. rmdir($dirPath);
  607. }
  608. }
  609. return true;
  610. }
  611. }
  612. }