Source for file filesystem.php

Documentation is available at filesystem.php

  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. //---------------------------------------------------------//
  14. // This function perform removing of files and directories //
  15. //---------------------------------------------------------//
  16.  
  17. function rcms_delete_files($file, $recursive = false) {
  18. while(!IGNORE_LOCK_FILES && is_file($file . '.lock')){
  19. //Wait for lock to release
  20. }
  21. if($recursive && is_dir($file)) {
  22. $els = rcms_scandir($file, '', '', true);
  23. foreach ($els as $el) {
  24. if($el != '.' && $el != '..'){
  25. rcms_delete_files($file . '/' . $el, true);
  26. }
  27. }
  28. }
  29. if(is_dir($file)) {
  30. return rmdir($file);
  31. } else {
  32. return unlink($file);
  33. }
  34. }
  35.  
  36. //---------------------------------------------------------//
  37. // This function perform renaming of file //
  38. //---------------------------------------------------------//
  39.  
  40. function rcms_rename_file($oldfile, $newfile) {
  41. rename($oldfile, $newfile);
  42. return true;
  43. }
  44.  
  45. function rcms_copy_file($oldfile, $newfile) {
  46. copy($oldfile, $newfile);
  47. return true;
  48. }
  49.  
  50. //---------------------------------------------------------//
  51. // This function perform creating of directory //
  52. //---------------------------------------------------------//
  53.  
  54. function rcms_mkdir($dir) {
  55. if(defined('SAFEMODE_HACK') && SAFEMODE_HACK){
  56. $url = parse_url(SAFEMODE_HACK_FTP);
  57. if($url['scheme'] != 'ftp') return false;
  58. return rcms_ftp_mkdir($dir, $url['host'], $url['user'], $url['pass'], '.' . $url['path']);
  59. }
  60. if(!is_dir($dir)){
  61. if(!is_dir(dirname($dir))) rcms_mkdir(dirname($dir));
  62. }
  63. return @mkdir($dir, 0777);
  64. }
  65.  
  66. //---------------------------------------------------------//
  67. // This function perform creating of directory by FTP //
  68. //---------------------------------------------------------//
  69.  
  70. function rcms_ftp_mkdir($dir, $server, $username, $password, $path) {
  71. if(!is_dir(dirname($dir))) rcms_ftp_mkdir(dirname($dir));
  72. $ftp = ftp_connect($server);
  73. ftp_login($ftp, $username, $password);
  74. if(RCMS_ROOT_PATH == '../') $path .= 'admin/';
  75. ftp_mkdir($ftp, $path . $dir);
  76. ftp_site($ftp, 'CHMOD 0777 ' . $path . $dir);
  77. ftp_close($ftp);
  78. return true;
  79. }
  80.  
  81. function rcms_parse_ini_file($filename, $blocks = false){
  82. $array1 = file($filename);
  83. $section = '';
  84. foreach ($array1 as $filedata) {
  85. $dataline = trim($filedata);
  86. $firstchar = substr($dataline, 0, 1);
  87. if ($firstchar != ';' && !empty($dataline)) {
  88. if ($blocks && $firstchar == '[' && substr($dataline, -1, 1) == ']') {
  89. $section = strtolower(substr($dataline, 1, -1));
  90. } else {
  91. $delimiter = strpos($dataline, '=');
  92. if ($delimiter > 0) {
  93. preg_match("/^[\s]*(.*?)[\s]*[=][\s]*(\"|)(.*?)(\"|)[\s]*$/", $dataline, $matches);
  94. $key = $matches[1];
  95. $value = $matches[3];
  96.  
  97. if($blocks){
  98. if(!empty($section)){
  99. $array2[$section][$key] = stripcslashes($value);
  100. }
  101. } else {
  102. $array2[$key] = stripcslashes($value);
  103. }
  104. } else {
  105. if($blocks){
  106. if(!empty($section)){
  107. $array2[$section][trim($dataline)] = '';
  108. }
  109. } else {
  110. $array2[trim($dataline)] = '';
  111. }
  112. }
  113. }
  114. }
  115. }
  116. return (!empty($array2)) ? $array2 : false;
  117. }
  118.  
  119. function rcms_chmod($file, $val, $rec = false) {
  120. $res = @chmod(realpath($file), octdec($val));
  121. if(is_dir($file) && $rec){
  122. $els = rcms_scandir($file);
  123. foreach ($els as $el) {
  124. $res = $res && rcms_chmod($file . '/' . $el, $val, true);
  125. }
  126. }
  127. return $res;
  128. }
  129.  
  130.  
  131. //---------------------------------------------------------//
  132. // This function is php5 file_put_contents copy //
  133. //---------------------------------------------------------//
  134.  
  135. function file_write_contents($file, $text, $mode = 'w+') {
  136. if(!is_dir(dirname($file))){
  137. trigger_error('Directory not found: ' . dirname($file));
  138. return false;
  139. }
  140. while(is_file($file . '.lock') && !@IGNORE_LOCK_FILES){
  141. //Wait for lock to release
  142. }
  143. $fp = fopen($file . '.lock', 'w+'); fwrite($fp, 'lock'); fclose($fp);
  144. if($fp = fopen($file, $mode)) {
  145. if(!empty($text) && !fwrite($fp, $text)) return false;
  146. fclose($fp);
  147. } else return false;
  148. rcms_delete_files($file . '.lock');
  149. return true;
  150. }
  151.  
  152. function gzfile_write_contents($file, $text, $mode = 'w+') {
  153. while(is_file($file . '.lock') && !@IGNORE_LOCK_FILES){
  154. //Wait for lock to release
  155. }
  156. $fp = fopen($file . '.lock', 'w+'); fwrite($fp, 'lock'); fclose($fp);
  157. if($fp = gzopen($file, $mode)) {
  158. if(!empty($text) && !gzwrite($fp, $text)) return false;
  159. gzclose($fp);
  160. } else return false;
  161. rcms_delete_files($file . '.lock');
  162. return true;
  163. }
  164.  
  165. //---------------------------------------------------------//
  166. // This function is created for compatibility //
  167. //---------------------------------------------------------//
  168.  
  169. if(!function_exists('file_get_contents')){
  170. function file_get_contents($file) {
  171. if(!$file = file($file)) return false;
  172. if(!$file = implode('', $file)) return false;
  173. return $file;
  174. }
  175. }
  176.  
  177. function gzfile_get_contents($file) {
  178. if(!$file = gzfile($file)) return false;
  179. if(!$file = implode('', $file)) return false;
  180. return $file;
  181. }
  182.  
  183. //---------------------------------------------------------//
  184. // Function to create ini files //
  185. //---------------------------------------------------------//
  186.  
  187. function write_ini_file($data, $filename, $process_sections = false){
  188. $ini = '';
  189. if(!$process_sections){
  190. if(is_array($data)){
  191. foreach ($data as $key => $value){
  192. $ini .= $key . ' = "' . str_replace('"', '&quot;', $value) . "\"\n";
  193. }
  194. }
  195. } else {
  196. if(is_array($data)){
  197. foreach ($data as $key => $value){
  198. $ini .= '[' . $key . ']' . "\n";
  199. foreach ($value as $ekey => $evalue){
  200. $ini .= $ekey . ' = "' . str_replace('"', '&quot;', $evalue) . "\"\n";
  201. }
  202. }
  203. }
  204. }
  205. return file_write_contents($filename, $ini);
  206. }
  207.  
  208. //---------------------------------------------------------//
  209. // Advanced php5 scandir analog //
  210. //---------------------------------------------------------//
  211.  
  212. function rcms_scandir($directory, $exp = '', $type = 'all', $do_not_filter = false) {
  213. $dir = $ndir = array();
  214. if(!empty($exp)){
  215. $exp = '/^' . str_replace('*', '(.*)', str_replace('.', '\\.', $exp)) . '$/';
  216. }
  217. if(!empty($type) && $type !== 'all'){
  218. $func = 'is_' . $type;
  219. }
  220. if(is_dir($directory)){
  221. $fh = opendir($directory);
  222. while (false !== ($filename = readdir($fh))) {
  223. if(substr($filename, 0, 1) != '.' || $do_not_filter) {
  224. if((empty($type) || $type == 'all' || $func($directory . '/' . $filename)) && (empty($exp) || preg_match($exp, $filename))){
  225. $dir[] = $filename;
  226. }
  227. }
  228. }
  229. closedir($fh);
  230. natsort($dir);
  231. }
  232. return $dir;
  233. }
  234.  
  235. function rcms_get_current_id($directory, $ending) {
  236. $files = rcms_scandir($directory, '*' . $ending);
  237. $endfile = @end($files);
  238. $current = substr($endfile, 0, strlen($endfile)-strlen($ending));
  239. $current +=1;
  240. return $current . $ending;
  241. }
  242.  
  243. function get_rights_string($file, $if = false){
  244. $perms = fileperms($file);
  245. $info = '';
  246. if(!$if){
  247. if (($perms & 0xC000) == 0xC000) {
  248. // Socket
  249. $info = 's';
  250. } elseif (($perms & 0xA000) == 0xA000) {
  251. // Symbolic Link
  252. $info = 'l';
  253. } elseif (($perms & 0x8000) == 0x8000) {
  254. // Regular
  255. $info = '-';
  256. } elseif (($perms & 0x6000) == 0x6000) {
  257. // Block special
  258. $info = 'b';
  259. } elseif (($perms & 0x4000) == 0x4000) {
  260. // Directory
  261. $info = 'd';
  262. } elseif (($perms & 0x2000) == 0x2000) {
  263. // Character special
  264. $info = 'c';
  265. } elseif (($perms & 0x1000) == 0x1000) {
  266. // FIFO pipe
  267. $info = 'p';
  268. } else {
  269. // Unknown
  270. $info = 'u';
  271. }
  272. }
  273.  
  274.  
  275. // Owner
  276. $info .= (($perms & 0x0100) ? 'r' : '-');
  277. $info .= (($perms & 0x0080) ? 'w' : '-');
  278. $info .= (($perms & 0x0040) ?
  279. (($perms & 0x0800) ? 's' : 'x' ) :
  280. (($perms & 0x0800) ? 'S' : '-'));
  281.  
  282. // Group
  283. $info .= (($perms & 0x0020) ? 'r' : '-');
  284. $info .= (($perms & 0x0010) ? 'w' : '-');
  285. $info .= (($perms & 0x0008) ?
  286. (($perms & 0x0400) ? 's' : 'x' ) :
  287. (($perms & 0x0400) ? 'S' : '-'));
  288.  
  289. // World
  290. $info .= (($perms & 0x0004) ? 'r' : '-');
  291. $info .= (($perms & 0x0002) ? 'w' : '-');
  292. $info .= (($perms & 0x0001) ?
  293. (($perms & 0x0200) ? 't' : 'x' ) :
  294. (($perms & 0x0200) ? 'T' : '-'));
  295.  
  296. return $info;
  297. }
  298.  
  299. function get_rights($file){
  300. return substr(sprintf('%o', fileperms($file)), -4);
  301. }
  302.  
  303. function convert_rights_string($mode) {
  304. $mode = str_pad($mode,9,'-');
  305. $trans = array('-'=>'0','r'=>'4','w'=>'2','x'=>'1');
  306. $mode = strtr($mode,$trans);
  307. $newmode = '0';
  308. $newmode .= $mode[0]+$mode[1]+$mode[2];
  309. $newmode .= $mode[3]+$mode[4]+$mode[5];
  310. $newmode .= $mode[6]+$mode[7]+$mode[8];
  311. return intval($newmode, 8);
  312. }
  313.  
  314. ?>

Documentation generated on Fri, 08 Jun 2007 12:21:23 +0300 by phpDocumentor 1.3.0RC3