filesystem.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. <?php
  2. ////////////////////////////////////////////////////////////////////////////////
  3. // Copyright (C) ReloadCMS Development Team //
  4. // http://reloadcms.sf.net //
  5. // //
  6. // This program is distributed in the hope that it will be useful, //
  7. // but WITHOUT ANY WARRANTY, without even the implied warranty of //
  8. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. //
  9. // //
  10. // This product released under GNU General Public License v2 //
  11. ////////////////////////////////////////////////////////////////////////////////
  12. /**
  13. * This function perform removing of files and directories
  14. *
  15. * @param string $file
  16. * @param bool $recursive
  17. *
  18. * @return bool
  19. */
  20. function rcms_delete_files($file, $recursive = false) {
  21. while (!IGNORE_LOCK_FILES && is_file($file . '.lock')) {
  22. //Wait for lock to release
  23. }
  24. if ($recursive && is_dir($file)) {
  25. $els = rcms_scandir($file, '', '', true);
  26. foreach ($els as $el) {
  27. if ($el != '.' && $el != '..') {
  28. rcms_delete_files($file . '/' . $el, true);
  29. }
  30. }
  31. }
  32. if (is_dir($file)) {
  33. return rmdir($file);
  34. } else {
  35. return unlink($file);
  36. }
  37. }
  38. /**
  39. * This function perform renaming of file
  40. *
  41. * @param string $oldfile
  42. * @param string $newfile
  43. *
  44. * @return boolean
  45. */
  46. function rcms_rename_file($oldfile, $newfile) {
  47. rename($oldfile, $newfile);
  48. return true;
  49. }
  50. /**
  51. * This function perform copying of file
  52. *
  53. * @param string $oldfile
  54. * @param string $newfile
  55. *
  56. * @return boolean
  57. */
  58. function rcms_copy_file($oldfile, $newfile) {
  59. copy($oldfile, $newfile);
  60. return true;
  61. }
  62. /**
  63. * This function perform creating of directory
  64. *
  65. * @param string $dir
  66. *
  67. * @return boolean
  68. */
  69. function rcms_mkdir($dir) {
  70. if (!is_dir($dir)) {
  71. if (!is_dir(dirname($dir)))
  72. rcms_mkdir(dirname($dir));
  73. }
  74. return @mkdir($dir, 0777);
  75. }
  76. /**
  77. * Parses standard INI-file structure and returns this as key=>value array
  78. *
  79. * @param string $filename Existing file name
  80. * @param bool $blocks Section parsing flag
  81. *
  82. * @return array
  83. */
  84. function rcms_parse_ini_file($filename, $blocks = false) {
  85. $array1 = file($filename);
  86. $section = '';
  87. foreach ($array1 as $filedata) {
  88. $dataline = trim($filedata);
  89. $firstchar = substr($dataline, 0, 1);
  90. if ($firstchar != ';' && !empty($dataline)) {
  91. if ($blocks && $firstchar == '[' && substr($dataline, -1, 1) == ']') {
  92. $section = strtolower(substr($dataline, 1, -1));
  93. } else {
  94. $delimiter = strpos($dataline, '=');
  95. if ($delimiter > 0) {
  96. preg_match("/^[\s]*(.*?)[\s]*[=][\s]*(\"|)(.*?)(\"|)[\s]*$/", $dataline, $matches);
  97. $key = $matches[1];
  98. $value = $matches[3];
  99. if ($blocks) {
  100. if (!empty($section)) {
  101. $array2[$section][$key] = stripcslashes($value);
  102. }
  103. } else {
  104. $array2[$key] = stripcslashes($value);
  105. }
  106. } else {
  107. if ($blocks) {
  108. if (!empty($section)) {
  109. $array2[$section][trim($dataline)] = '';
  110. }
  111. } else {
  112. $array2[trim($dataline)] = '';
  113. }
  114. }
  115. }
  116. }
  117. }
  118. return (!empty($array2)) ? $array2 : false;
  119. }
  120. /**
  121. * Changes access righrs for file or directory
  122. *
  123. * @param string $file
  124. * @param string $val
  125. * @param bool $rec
  126. *
  127. * @return bool
  128. */
  129. function rcms_chmod($file, $val, $rec = false) {
  130. $res = @chmod(realpath($file), octdec($val));
  131. if (is_dir($file) && $rec) {
  132. $els = rcms_scandir($file);
  133. foreach ($els as $el) {
  134. $res = $res && rcms_chmod($file . '/' . $el, $val, true);
  135. }
  136. }
  137. return $res;
  138. }
  139. /**
  140. * This function is php5 file_put_contents copy
  141. *
  142. * @param string $file
  143. * @param string $text
  144. * @param string $mode
  145. *
  146. * @return boolean
  147. */
  148. function file_write_contents($file, $text, $mode = 'w+') {
  149. if (!is_dir(dirname($file))) {
  150. trigger_error('Directory not found: ' . dirname($file));
  151. return false;
  152. }
  153. while (is_file($file . '.lock') && !@IGNORE_LOCK_FILES) {
  154. //Wait for lock to release
  155. }
  156. $fp = fopen($file . '.lock', 'w+');
  157. fwrite($fp, 'lock');
  158. fclose($fp);
  159. if ($fp = fopen($file, $mode)) {
  160. if (!empty($text) && !fwrite($fp, $text))
  161. return false;
  162. fclose($fp);
  163. } else
  164. return false;
  165. rcms_delete_files($file . '.lock');
  166. return true;
  167. }
  168. /**
  169. * Writes gzipped file contents
  170. *
  171. * @param string $file
  172. * @param string $text
  173. * @param string $mode
  174. *
  175. * @return boolean
  176. */
  177. function gzfile_write_contents($file, $text, $mode = 'w+') {
  178. while (is_file($file . '.lock') && !@IGNORE_LOCK_FILES) {
  179. //Wait for lock to release
  180. }
  181. $fp = fopen($file . '.lock', 'w+');
  182. fwrite($fp, 'lock');
  183. fclose($fp);
  184. if ($fp = gzopen($file, $mode)) {
  185. if (!empty($text) && !gzwrite($fp, $text))
  186. return false;
  187. gzclose($fp);
  188. } else
  189. return false;
  190. rcms_delete_files($file . '.lock');
  191. return true;
  192. }
  193. if (!function_exists('file_get_contents')) {
  194. /**
  195. * This function is created for compatibility
  196. *
  197. * @param string $file
  198. *
  199. * @return boolean
  200. */
  201. function file_get_contents($file) {
  202. if (!$file = file($file))
  203. return false;
  204. if (!$file = implode('', $file))
  205. return false;
  206. return $file;
  207. }
  208. }
  209. /**
  210. * Returns gzipped file contents
  211. *
  212. * @param string $file
  213. *
  214. * @return boolean/string
  215. */
  216. function gzfile_get_contents($file) {
  217. if (!$file = gzfile($file))
  218. return false;
  219. if (!$file = implode('', $file))
  220. return false;
  221. return $file;
  222. }
  223. /**
  224. * Function to create ini files
  225. *
  226. * @param string $data
  227. * @param string $filename
  228. * @param string $process_sections
  229. *
  230. * @return bool
  231. */
  232. function write_ini_file($data, $filename, $process_sections = false) {
  233. $ini = '';
  234. if (!$process_sections) {
  235. if (is_array($data)) {
  236. foreach ($data as $key => $value) {
  237. $ini .= $key . ' = "' . str_replace('"', '&quot;', $value) . "\"\n";
  238. }
  239. }
  240. } else {
  241. if (is_array($data)) {
  242. foreach ($data as $key => $value) {
  243. $ini .= '[' . $key . ']' . "\n";
  244. foreach ($value as $ekey => $evalue) {
  245. $ini .= $ekey . ' = "' . str_replace('"', '&quot;', $evalue) . "\"\n";
  246. }
  247. }
  248. }
  249. }
  250. return file_write_contents($filename, $ini);
  251. }
  252. /**
  253. * Advanced php5 scandir analog wit some filters
  254. *
  255. * @param string $directory Directory to scan
  256. * @param string $exp Filter expression - like *.ini or *.dat
  257. * @param string $type Filter type - all or dir
  258. * @param bool $do_not_filter
  259. *
  260. * @return array
  261. */
  262. function rcms_scandir($directory, $exp = '', $type = 'all', $do_not_filter = false) {
  263. $dir = $ndir = array();
  264. if (!empty($exp)) {
  265. $exp = '/^' . str_replace('*', '(.*)', str_replace('.', '\\.', $exp)) . '$/';
  266. }
  267. if (!empty($type) && $type !== 'all') {
  268. $func = 'is_' . $type;
  269. }
  270. if (is_dir($directory)) {
  271. $fh = opendir($directory);
  272. while (false !== ($filename = readdir($fh))) {
  273. if (substr($filename, 0, 1) != '.' || $do_not_filter) {
  274. if ((empty($type) || $type == 'all' || $func($directory . '/' . $filename)) && (empty($exp) || preg_match($exp, $filename))) {
  275. $dir[] = $filename;
  276. }
  277. }
  278. }
  279. closedir($fh);
  280. natsort($dir);
  281. }
  282. return $dir;
  283. }
  284. /**
  285. * Returns last file name in directory as current id
  286. *
  287. * @param string $directory
  288. * @param string $ending
  289. *
  290. * @return string
  291. */
  292. function rcms_get_current_id($directory, $ending) {
  293. $files = rcms_scandir($directory, '*' . $ending);
  294. $endfile = @end($files);
  295. $current = substr($endfile, 0, strlen($endfile) - strlen($ending));
  296. $current += 1;
  297. return $current . $ending;
  298. }
  299. /**
  300. * Returns file permissions in readable string view
  301. *
  302. * @param string $file
  303. * @param bool $if
  304. *
  305. * @return string
  306. */
  307. function get_rights_string($file, $if = false) {
  308. $perms = fileperms($file);
  309. $info = '';
  310. if (!$if) {
  311. if (($perms & 0xC000) == 0xC000) {
  312. // Socket
  313. $info = 's';
  314. } elseif (($perms & 0xA000) == 0xA000) {
  315. // Symbolic Link
  316. $info = 'l';
  317. } elseif (($perms & 0x8000) == 0x8000) {
  318. // Regular
  319. $info = '-';
  320. } elseif (($perms & 0x6000) == 0x6000) {
  321. // Block special
  322. $info = 'b';
  323. } elseif (($perms & 0x4000) == 0x4000) {
  324. // Directory
  325. $info = 'd';
  326. } elseif (($perms & 0x2000) == 0x2000) {
  327. // Character special
  328. $info = 'c';
  329. } elseif (($perms & 0x1000) == 0x1000) {
  330. // FIFO pipe
  331. $info = 'p';
  332. } else {
  333. // Unknown
  334. $info = 'u';
  335. }
  336. }
  337. // Owner
  338. $info .= (($perms & 0x0100) ? 'r' : '-');
  339. $info .= (($perms & 0x0080) ? 'w' : '-');
  340. $info .= (($perms & 0x0040) ?
  341. (($perms & 0x0800) ? 's' : 'x' ) :
  342. (($perms & 0x0800) ? 'S' : '-'));
  343. // Group
  344. $info .= (($perms & 0x0020) ? 'r' : '-');
  345. $info .= (($perms & 0x0010) ? 'w' : '-');
  346. $info .= (($perms & 0x0008) ?
  347. (($perms & 0x0400) ? 's' : 'x' ) :
  348. (($perms & 0x0400) ? 'S' : '-'));
  349. // World
  350. $info .= (($perms & 0x0004) ? 'r' : '-');
  351. $info .= (($perms & 0x0002) ? 'w' : '-');
  352. $info .= (($perms & 0x0001) ?
  353. (($perms & 0x0200) ? 't' : 'x' ) :
  354. (($perms & 0x0200) ? 'T' : '-'));
  355. return $info;
  356. }
  357. /**
  358. * Returns file permissions
  359. *
  360. * @param string $file
  361. *
  362. * @return string
  363. */
  364. function get_rights($file) {
  365. return substr(sprintf('%o', fileperms($file)), -4);
  366. }
  367. /**
  368. * Returns file permissions
  369. *
  370. * @param string $mode
  371. *
  372. * @return int
  373. */
  374. function convert_rights_string($mode) {
  375. $mode = str_pad($mode, 9, '-');
  376. $trans = array('-' => '0', 'r' => '4', 'w' => '2', 'x' => '1');
  377. $mode = strtr($mode, $trans);
  378. $newmode = '0';
  379. $newmode .= $mode[0] + $mode[1] + $mode[2];
  380. $newmode .= $mode[3] + $mode[4] + $mode[5];
  381. $newmode .= $mode[6] + $mode[7] + $mode[8];
  382. return intval($newmode, 8);
  383. }
  384. ?>