PageRenderTime 135ms CodeModel.GetById 56ms RepoModel.GetById 1ms app.codeStats 0ms

/filesystemlib.php

https://github.com/ccle/Moodle-Flashcard
PHP | 399 lines | 260 code | 47 blank | 92 comment | 91 complexity | 1bffd7e52262df511610b6203b360346 MD5 | raw file
  1. <?php
  2. /**
  3. * File System Abstract Layer
  4. * Support library
  5. *
  6. * @author Valery Fremaux (France) (admin@ethnoinformatique.fr)
  7. * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  8. * @package extralibs
  9. * @category third-party libs
  10. * @date 2007/11/04
  11. *
  12. */
  13. //avoids reloading the lib when keeped in third party plugin
  14. if (!function_exists('filesystem_create_dir')) {
  15. define('FS_RECURSIVE', true);
  16. define('FS_NON_RECURSIVE', false);
  17. define('FS_SHOW_HIDDEN', true);
  18. define('FS_IGNORE_HIDDEN', false);
  19. define('FS_NO_DIRS', 2);
  20. define('FS_ONLY_DIRS', 1);
  21. define('FS_ALL_ENTRIES', 0);
  22. define('FS_FULL_DELETE', true);
  23. define('FS_CLEAR_CONTENT', false);
  24. /**
  25. * creates a dir in file system optionally creating all pathes on the way
  26. * @param string $path the relative path from dataroot
  27. * @param boolean $recursive if true, creates recursively all path elements
  28. * @param string $pathbase the base path
  29. */
  30. function filesystem_create_dir($path, $recursive = 0, $pathbase=null) {
  31. global $CFG;
  32. if (is_null($pathbase)){
  33. $pathbase = $CFG->dataroot . '/';
  34. } elseif ($pathbase === '') {
  35. $pathbase = '';
  36. } else {
  37. $pathbase = $pathbase . '/';
  38. }
  39. $result = true;
  40. if (!$recursive){
  41. if (@$CFG->filedebug) mtrace("creating dir <i>{$path}</i><br/>");
  42. $oldMask = umask(0);
  43. if(!filesystem_is_dir($path, $pathbase)) $result = @mkdir($pathbase . $path, 0777);
  44. umask($oldMask);
  45. return $result;
  46. }
  47. else {
  48. $parts = explode('/', $path);
  49. $pathTo = '';
  50. for($i = 0; $i < count($parts) && $result; $i++){
  51. $pathTo .= '/' . $parts[$i];
  52. $result = filesystem_create_dir($pathTo, 0, $pathbase);
  53. }
  54. return $result;
  55. }
  56. }
  57. /**
  58. * tests if path is a dir. A simple wrapper to is_dir
  59. * @param string $relativepath the path from dataroot
  60. * @param string $pathbase the base path
  61. */
  62. function filesystem_is_dir($relativepath, $pathbase=null){
  63. global $CFG;
  64. if (is_null($pathbase)){
  65. $pathbase = $CFG->dataroot . '/';
  66. } elseif ($pathbase === '') {
  67. $pathbase = '';
  68. } else {
  69. $pathbase = $pathbase . '/';
  70. }
  71. if (@$CFG->filedebug) mtrace("is dir <i>$pathbase$relativepath</i><br/>");
  72. return is_dir($pathbase . $relativepath);
  73. }
  74. /**
  75. * checks if file (or dir) exists. A simple wrapper to file_exists
  76. * @param string $relativepath the path from dataroot
  77. * @param string $pathbase the base path
  78. */
  79. function filesystem_file_exists($relativepath, $pathbase=null){
  80. global $CFG;
  81. if (is_null($pathbase)){
  82. $pathbase = $CFG->dataroot . '/';
  83. } elseif ($pathbase === '') {
  84. $pathbase = '';
  85. } else {
  86. $pathbase = $pathbase . '/';
  87. }
  88. if (@$CFG->filedebug) mtrace("file exists <i>$pathbase$relativepath</i><br/>");
  89. return file_exists($pathbase . $relativepath);
  90. }
  91. /**
  92. * scans for entries within a directory
  93. * @param string $relativepath the path from dataroot
  94. * @param boolean $hiddens shows or hide hidden files
  95. * @param int $what selects only dirs, files or both
  96. * @param string $pathbase the base path
  97. * @return an array of entries wich are local names in path
  98. */
  99. function filesystem_scan_dir($relativepath, $hiddens = 0, $what = 0, $pathbase=null){
  100. global $CFG;
  101. if (is_null($pathbase)){
  102. $pathbase = $CFG->dataroot . '/';
  103. } elseif ($pathbase === '') {
  104. $pathbase = '';
  105. } else {
  106. $pathbase = $pathbase . '/';
  107. }
  108. if (@$CFG->filedebug) mtrace("scanning <i>$pathbase$relativepath</i><br/>");
  109. $dir = opendir($pathbase . $relativepath);
  110. $entries = array();
  111. while ($anEntry = readdir($dir)){
  112. if ($what == FS_ONLY_DIRS){
  113. $subpath = $relativepath . '/' . $anEntry;
  114. $subpath = preg_replace("/^\//", "", $subpath);
  115. if (!filesystem_is_dir($subpath, $pathbase)) continue ;
  116. }
  117. if ($what == FS_NO_DIRS){
  118. $subpath = $relativepath . '/' . $anEntry;
  119. $subpath = preg_replace("/^\//", "", $subpath);
  120. if (filesystem_is_dir($subpath, $pathbase)) continue ;
  121. }
  122. if ($hiddens) {
  123. if (($anEntry != '.') && ($anEntry != '..')) $entries[] = $anEntry;
  124. } else {
  125. if (!preg_match("/^\./", $anEntry)) $entries[] = $anEntry;
  126. }
  127. }
  128. closedir($dir);
  129. return $entries;
  130. }
  131. /**
  132. * clears and removes an entire dir
  133. * @param string $relativepath the path from dataroot
  134. * @param boolean $fulldelete if true, removes the dir root either
  135. * @param string $pathbase the base path
  136. * @return an array of entries wich are local names in path
  137. */
  138. function filesystem_clear_dir($relativepath, $fullDelete = false, $pathbase=null) {
  139. global $CFG;
  140. if (is_null($pathbase)){
  141. $pathbase = $CFG->dataroot . '/';
  142. } elseif ($pathbase === '') {
  143. $pathbase = '';
  144. } else {
  145. $pathbase = $pathbase . '/';
  146. }
  147. if (@$CFG->filedebug) mtrace("clearing dir <i>$pathbase$relativepath</i><br/>");
  148. $exists = filesystem_is_dir($relativepath, $pathbase);
  149. if (!$exists && !$fullDelete) {
  150. return filesystem_create_dir($relativepath, $pathbase);
  151. }
  152. if (!$exists && $fullDelete) {
  153. return true;
  154. }
  155. $files = filesystem_scan_dir($relativepath, FS_SHOW_HIDDEN, FS_ALL_ENTRIES, $pathbase);
  156. foreach($files as $aFile) {
  157. if ($aFile == "." || $aFile == "..") continue ;
  158. if (filesystem_is_dir("{$relativepath}/{$aFile}", $pathbase)){
  159. filesystem_clear_dir("{$relativepath}/{$aFile}", FS_FULL_DELETE, $pathbase);
  160. // fs_removeDir("{$relativepath}/{$aFile}");
  161. } else {
  162. filesystem_delete_file("{$relativepath}/{$aFile}", $pathbase);
  163. }
  164. }
  165. if (file_exists($pathbase . $relativepath) && $fullDelete) return filesystem_remove_dir($relativepath, $pathbase);
  166. return false;
  167. }
  168. /**
  169. * copies recursively a subtree from a location to another
  170. * @param string $source the source path from dataroot
  171. * @param string $dest the dest path from dataroot
  172. * @param string $pathbase the base path
  173. * @return void
  174. */
  175. function filesystem_copy_tree($source, $dest, $pathbase=null) {
  176. global $CFG;
  177. if (is_null($pathbase)){
  178. $pathbase = $CFG->dataroot . '/';
  179. } elseif ($pathbase === '') {
  180. $pathbase = '';
  181. } else {
  182. $pathbase = $pathbase . '/';
  183. }
  184. if (@$CFG->filedebug) mtrace("copying tree <i>$pathbase$source</i> to <i>$pathbase$dest</i><br/>");
  185. if (file_exists($dest) && !filesystem_is_dir($dest, $pathbase)) {
  186. return;
  187. }
  188. if (!filesystem_is_dir($dest, $pathbase)) {
  189. filesystem_create_dir($dest, FS_RECURSIVE, $pathbase);
  190. }
  191. $files = array();
  192. $files = filesystem_scan_dir($source, FS_SHOW_HIDDEN, FS_ALL_ENTRIES, $pathbase);
  193. foreach($files as $aFile) {
  194. if ($aFile == '.' || $aFile == '..') next;
  195. if (filesystem_is_dir("{$source}/{$aFile}", $pathbase)) {
  196. filesystem_create_dir("{$dest}/{$aFile}", FS_NON_RECURSIVE, $pathbase);
  197. if (count(filesystem_is_dir("{$source}/{$aFile}", $pathbase)) != 0) {
  198. filesystem_copy_tree("{$source}/{$aFile}", "{$dest}/{$aFile}", $pathbase);
  199. }
  200. } else {
  201. filesystem_copy_file("{$source}/{$aFile}", "{$dest}/{$aFile}", $pathbase);
  202. }
  203. }
  204. }
  205. /**
  206. * stores a file content in the file system, creating on the way directories if needed
  207. * @param string $relativepath the path from dataroot
  208. * @param string $data the data to store in
  209. * @param string $pathbase the base path
  210. */
  211. function filesystem_store_file($relativepath, $data, $pathbase=null) {
  212. global $CFG;
  213. if (is_null($pathbase)){
  214. $pathbase = $CFG->dataroot . '/';
  215. } elseif ($pathbase === '') {
  216. $pathbase = '';
  217. } else {
  218. $pathbase = $pathbase . '/';
  219. }
  220. if (@$CFG->filedebug) mtrace("storing <i>$pathbase$relativepath</i><br/>");
  221. $parts = pathinfo($relativepath);
  222. if (!filesystem_is_dir($parts['dirname'], $pathbase)) filesystem_create_dir($parts['dirname'], $pathbase);
  223. $FILE = fopen($pathbase . $relativepath, "w");
  224. if (!$FILE) return false;
  225. fwrite ($FILE, $data);
  226. fclose($FILE);
  227. return true;
  228. }
  229. /**
  230. * reads a file content and returns scalar string
  231. * @param string $relativepath the path from dataroot
  232. * @param string $pathbase the base path
  233. * @return the data as a string
  234. */
  235. function filesystem_read_a_file($relativepath, $pathbase=null) {
  236. global $CFG;
  237. if (is_null($pathbase)){
  238. $pathbase = $CFG->dataroot . '/';
  239. } elseif ($pathbase === '') {
  240. $pathbase = '';
  241. } else {
  242. $pathbase = $pathbase . '/';
  243. }
  244. if (@$CFG->filedebug) mtrace("reading <i>$pathbase$relativepath</i><br/>");
  245. $fullPath = $pathbase . $relativepath;
  246. if (file_exists($fullPath)){
  247. $FILE = file($fullPath);
  248. return implode('', $FILE);
  249. }
  250. return false;
  251. }
  252. /**
  253. * deletes a file. Simple wrapper to unlink
  254. * @param string $relativepath the path from dataroot
  255. * @param string $pathbase the base path
  256. * @return the data as a string
  257. */
  258. function filesystem_delete_file($relativepath, $pathbase=null){
  259. global $CFG;
  260. if (is_null($pathbase)){
  261. $pathbase = $CFG->dataroot . '/';
  262. } elseif ($pathbase === '') {
  263. $pathbase = '';
  264. } else {
  265. $pathbase = $pathbase . '/';
  266. }
  267. if (@$CFG->filedebug) mtrace("deleting file <i>$relativepath</i><br/>");
  268. if (filesystem_file_exists($relativepath, $pathbase) && !filesystem_is_dir($relativepath, $pathbase))
  269. return unlink($pathbase . $relativepath);
  270. return false;
  271. }
  272. /**
  273. * removes an empty dir. Simple wrapper to rmdir
  274. * @param string $relativepath the path from dataroot
  275. * @param string $pathbase the base path
  276. */
  277. function filesystem_remove_dir($relativepath, $pathbase=null){
  278. global $CFG;
  279. if (is_null($pathbase)){
  280. $pathbase = $CFG->dataroot . '/';
  281. } elseif ($pathbase === '') {
  282. $pathbase = '';
  283. } else {
  284. $pathbase = $pathbase . '/';
  285. }
  286. if (@$CFG->filedebug) mtrace("deleting dir <i>$relativepath</i><br/>");
  287. if (filesystem_file_exists($relativepath, $pathbase))
  288. return rmdir($pathbase . $relativepath);
  289. }
  290. /**
  291. * renames a file. Simple wrapper to rename
  292. * @param string $relativepath the path from dataroot
  293. * @param string $pathbase the base path
  294. */
  295. function filesystem_move_file($source, $dest, $pathbase=null){
  296. global $CFG;
  297. if (is_null($pathbase)){
  298. $pathbase = $CFG->dataroot . '/';
  299. } elseif ($pathbase === '') {
  300. $pathbase = '';
  301. } else {
  302. $pathbase = $pathbase . '/';
  303. }
  304. if (filesystem_file_exists($source, $pathbase)){
  305. if (@$CFG->filedebug) mtrace("moving file/dir <i>$source</i> to <i>$dest</i><br/>");
  306. return rename($pathbase . $source, $pathbase . '/' . $dest);
  307. }
  308. return false;
  309. }
  310. /**
  311. * copy a file creating all path on the way if needed
  312. * @param string $source the source path from dataroot
  313. * @param string $dest the dest path from dataroot
  314. * @param string $pathbase the base path
  315. */
  316. function filesystem_copy_file($source, $dest, $pathbase=null) {
  317. global $CFG;
  318. if (is_null($pathbase)){
  319. $pathbase = $CFG->dataroot . '/';
  320. } elseif ($pathbase === '') {
  321. $pathbase = '';
  322. } else {
  323. $pathbase = $pathbase . '/';
  324. }
  325. if (@$CFG->filedebug) mtrace("copying file <i>$pathbase$source</i> to <i>$pathbase$dest</i><br/>");
  326. if (!filesystem_file_exists($source, $pathbase)) return -1;
  327. $parts = pathinfo($dest);
  328. if (!filesystem_is_dir($parts['dirname'], $pathbase)) filesystem_create_dir($parts['dirname'], $pathbase);
  329. return copy($pathbase . $source, $pathbase . $dest);
  330. }
  331. /**
  332. * gets a filtered list of files
  333. * @param string $path the path from dataroot
  334. * @param string $filemask the filemask for filtering
  335. * @param string $pathbase the base path
  336. */
  337. function filesystem_get_file_list($path, $filemask = "*.*", $pathbase=null) {
  338. global $CFG;
  339. if (is_null($pathbase)){
  340. $pathbase = $CFG->dataroot . '/';
  341. } elseif ($pathbase === '') {
  342. $pathbase = '';
  343. } else {
  344. $pathbase = $pathbase . '/';
  345. }
  346. if (preg_match("/(.*)\/$/", $path, $matches)) $path = $matches[1];
  347. $files = glob($pathbase . "{$path}/{$filemask}");
  348. return $files;
  349. }
  350. //
  351. }
  352. ?>