PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/bitrix/modules/main/classes/general/file_temp.php

https://gitlab.com/Rad1calDreamer/honey
PHP | 198 lines | 169 code | 21 blank | 8 comment | 45 complexity | 80ae8a2b920a199e03572b108abd85a7 MD5 | raw file
  1. <?
  2. class CTempFile
  3. {
  4. private static $arFiles = array();
  5. public static function GetAbsoluteRoot()
  6. {
  7. $io = CBXVirtualIo::GetInstance();
  8. if(defined('BX_TEMPORARY_FILES_DIRECTORY'))
  9. {
  10. return BX_TEMPORARY_FILES_DIRECTORY;
  11. }
  12. else
  13. {
  14. return $io->CombinePath(
  15. $_SERVER["DOCUMENT_ROOT"],
  16. COption::GetOptionString("main", "upload_dir", "upload"),
  17. "tmp"
  18. );
  19. }
  20. }
  21. public static function GetFileName($file_name = '')
  22. {
  23. $dir_name = self::GetAbsoluteRoot();
  24. $file_name = rel2abs("/", "/".$file_name);
  25. $i = 0;
  26. while(true)
  27. {
  28. $i++;
  29. if($file_name == '/')
  30. $dir_add = md5(mt_rand());
  31. elseif($i < 25)
  32. $dir_add = substr(md5(mt_rand()), 0, 3);
  33. else
  34. $dir_add = md5(mt_rand());
  35. $temp_path = $dir_name."/".$dir_add.$file_name;
  36. if(!file_exists($temp_path))
  37. {
  38. //Delayed unlink
  39. if(empty(self::$arFiles))
  40. register_shutdown_function(array('CTempFile', 'Cleanup'));
  41. self::$arFiles[$temp_path] = $dir_name."/".$dir_add;
  42. //Function ends only here
  43. return $temp_path;
  44. }
  45. }
  46. }
  47. public static function GetDirectoryName($hours_to_keep_files = 0, $subdir = "")
  48. {
  49. if($hours_to_keep_files <= 0)
  50. return self::GetFileName('');
  51. if($subdir === "")
  52. {
  53. $dir_name = self::GetAbsoluteRoot().'/BXTEMP-'.date('Y-m-d/H/', time()+3600*$hours_to_keep_files);
  54. $i = 0;
  55. while(true)
  56. {
  57. $i++;
  58. $dir_add = md5(mt_rand());
  59. $temp_path = $dir_name.$dir_add."/";
  60. if(!file_exists($temp_path))
  61. break;
  62. }
  63. }
  64. else //Fixed name during the session
  65. {
  66. $subdir = implode("/", (is_array($subdir) ? $subdir : array($subdir, bitrix_sessid())))."/";
  67. while (strpos($subdir, "//") !== false)
  68. $subdir = str_replace("//", "/", $subdir);
  69. $bFound = false;
  70. for($i = $hours_to_keep_files-1; $i > 0; $i--)
  71. {
  72. $dir_name = self::GetAbsoluteRoot().'/BXTEMP-'.date('Y-m-d/H/', time()+3600*$i);
  73. $temp_path = $dir_name.$subdir;
  74. if(file_exists($temp_path) && is_dir($temp_path))
  75. {
  76. $bFound = true;
  77. break;
  78. }
  79. }
  80. if(!$bFound)
  81. {
  82. $dir_name = self::GetAbsoluteRoot().'/BXTEMP-'.date('Y-m-d/H/', time()+3600*$hours_to_keep_files);
  83. $temp_path = $dir_name.$subdir;
  84. }
  85. }
  86. //Delayed unlink
  87. if(empty(self::$arFiles))
  88. register_shutdown_function(array('CTempFile', 'Cleanup'));
  89. //Function ends only here
  90. return $temp_path;
  91. }
  92. //PHP shutdown cleanup
  93. public static function Cleanup()
  94. {
  95. foreach(self::$arFiles as $temp_path => $temp_dir)
  96. {
  97. if(file_exists($temp_path))
  98. {
  99. //Clean a file from CTempFile::GetFileName('some.jpg');
  100. if(is_file($temp_path))
  101. {
  102. unlink($temp_path);
  103. @rmdir($temp_dir);
  104. }
  105. //Clean whole temporary directory from CTempFile::GetFileName('');
  106. elseif(
  107. substr($temp_path, -1) == '/'
  108. && strpos($temp_path, "BXTEMP") === false
  109. && is_dir($temp_path)
  110. )
  111. {
  112. CTempFile::_absolute_path_recursive_delete($temp_path);
  113. }
  114. }
  115. }
  116. //Clean directories with $hours_to_keep_files > 0
  117. $dir_name = self::GetAbsoluteRoot()."/";
  118. if($handle = opendir($dir_name))
  119. {
  120. while(($day_files_dir = readdir($handle)) !== false)
  121. {
  122. if(preg_match("/^BXTEMP-(.*?)\$/", $day_files_dir, $match) && is_dir($dir_name.$day_files_dir))
  123. {
  124. $this_day_name = 'BXTEMP-'.date('Y-m-d');
  125. if($day_files_dir < $this_day_name)
  126. CTempFile::_absolute_path_recursive_delete($dir_name.$day_files_dir);
  127. elseif($day_files_dir == $this_day_name)
  128. {
  129. if($hour_handle = opendir($dir_name.$day_files_dir))
  130. {
  131. $this_hour_name = date('H');
  132. while(($hour_files_dir = readdir($hour_handle)) !== false)
  133. {
  134. if($hour_files_dir == '.' || $hour_files_dir == '..')
  135. continue;
  136. if($hour_files_dir < $this_hour_name)
  137. CTempFile::_absolute_path_recursive_delete($dir_name.$day_files_dir.'/'.$hour_files_dir);
  138. }
  139. }
  140. }
  141. }
  142. }
  143. closedir($handle);
  144. }
  145. }
  146. private static function _absolute_path_recursive_delete($path)
  147. {
  148. if(strlen($path) == 0 || $path == '/')
  149. return false;
  150. $f = true;
  151. if(is_file($path) || is_link($path))
  152. {
  153. if(@unlink($path))
  154. return true;
  155. return false;
  156. }
  157. elseif(is_dir($path))
  158. {
  159. if($handle = opendir($path))
  160. {
  161. while(($file = readdir($handle)) !== false)
  162. {
  163. if($file == "." || $file == "..")
  164. continue;
  165. if(!CTempFile::_absolute_path_recursive_delete($path."/".$file))
  166. $f = false;
  167. }
  168. closedir($handle);
  169. }
  170. if(!@rmdir($path))
  171. return false;
  172. return $f;
  173. }
  174. return false;
  175. }
  176. }
  177. ?>