PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/bx/helpers/file.php

https://github.com/whiletrue/fluxcms
PHP | 147 lines | 120 code | 17 blank | 10 comment | 39 complexity | a6d2bb0b2011d0e1209696774c96a583 MD5 | raw file
  1. <?php
  2. class bx_helpers_file {
  3. static private $getFileSizeData = null;
  4. static function getFileSize($src, $human = true) {
  5. if (!self::$getFileSizeData) {
  6. $sc = popoon_helpers_simplecache::getInstance();
  7. self::$getFileSizeData = $sc->simpleCacheCheck("getFileSize","bx_helpers_file",null,"serialize",3600);
  8. if (!self::$getFileSizeData) {
  9. self::$getFileSizeData = array();
  10. }
  11. }
  12. if (!isset(self::$getFileSizeData[$src])) {
  13. if (strpos($src,'http://') === 0) {
  14. $c = new HTTP_Client();
  15. $c->head( $src);
  16. $cr = $c->currentResponse() ;
  17. $size = $cr['headers']['content-length'];
  18. } else {
  19. //$filesrc = BX_PROJECT_DIR.str_replace("themes/","themes/".$GLOBALS['POOL']->config->theme."/",$src);
  20. $filesrc = BX_OPEN_BASEDIR.$src;
  21. if (file_exists($filesrc)) {
  22. $size = filesize($filesrc);
  23. } else {
  24. $size=0;
  25. }
  26. }
  27. if ($human) {
  28. $size = round($size/1024);
  29. if ($size > 1000) {
  30. $size = round($size/1024,2) . " MB";
  31. } else {
  32. $size .=" kB";
  33. }
  34. }
  35. self::$getFileSizeData[$src] = $size;
  36. if (!isset($sc)) {
  37. $sc = popoon_helpers_simplecache::getInstance();
  38. }
  39. $sc->simpleCacheWrite("getFileSize","bx_helpers_file",null,self::$getFileSizeData);
  40. }
  41. return self::$getFileSizeData[$src];
  42. }
  43. /** creates a full path...
  44. */
  45. static function mkpath($path) {
  46. $path = preg_replace("#/+#","/",$path);
  47. //bx_helpers_debug::webdump(substr($path,0,1));
  48. $dirs = explode("/",$path);
  49. $path = $dirs[0];
  50. for($i = 1;$i < count($dirs);$i++) {
  51. $parent = $path;
  52. $path .= "/".$dirs[$i];
  53. if(is_readable($parent) && !is_dir($path) ) {
  54. mkdir($path,0755);
  55. }
  56. }
  57. }
  58. /**
  59. * Recursively deletes the given directory.
  60. *
  61. * @param string $dir Directory to delete
  62. * @access public
  63. */
  64. static function rmdir($dir, $deleteitself = true) {
  65. $all = glob($dir.'/*');
  66. if ($all === false) {
  67. $all = array();
  68. }
  69. $hidden = glob($dir.'/.*');
  70. if (is_array($hidden)) {
  71. $objs = array_merge($all, $hidden);
  72. } else {
  73. $objs = $all;
  74. }
  75. if(sizeof($objs) > 0) {
  76. foreach($objs as $obj) {
  77. if($obj != $dir.'/.' AND $obj != $dir.'/..') {
  78. if(file_exists($obj) AND !is_writable($obj)) {
  79. chmod($obj, 0666);
  80. }
  81. if (is_dir($obj)) {
  82. bx_helpers_file::rmdir($obj);
  83. } else {
  84. unlink($obj);
  85. }
  86. }
  87. }
  88. }
  89. if ($deleteitself) {
  90. rmdir($dir);
  91. }
  92. }
  93. static function cpdir($dir,$todir,$noSvn = true) {
  94. $folder = opendir($dir);
  95. if (!file_exists($todir)) {
  96. mkdir($todir,0755,true);
  97. }
  98. while($file = readdir($folder)){
  99. if ($file == '.' || $file == '..') {
  100. continue;
  101. }
  102. if ($noSvn && $file == ".svn") {
  103. continue;
  104. }
  105. if(is_dir($dir.'/'.$file)){
  106. self::cpdir($dir.'/'.$file,$todir.'/'.$file);
  107. } else {
  108. if (!copy($dir.'/'.$file,$todir.'/'.$file)) {
  109. print 'Could not copy '.$dir.'/'.$file .' to ' . $todir.'/'.$file."<br/>";
  110. }
  111. }
  112. }
  113. closedir($folder);
  114. }
  115. static function getFileEncoding($src) {
  116. if (function_exists("finfo_open")) {
  117. $res = finfo_open(FILEINFO_MIME);
  118. $m = finfo_file($res, $src);
  119. finfo_close($res);
  120. } elseif(!strpos(ini_get('disable_functions'),'exec')) {
  121. exec(escapeshellcmd('file -ib '. escapeshellarg($src)), $out);
  122. $m = array_shift($out);
  123. }
  124. if ( strpos($m,'charset=') !== false) {
  125. return substr($m,strpos($m,'charset=') + 8);
  126. } else {
  127. return null;
  128. }
  129. }
  130. }