PageRenderTime 37ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/ckeditor/plugins/_inactive/cyberim/includes/FileManager.php

https://bitbucket.org/seyar/kinda.local
PHP | 177 lines | 98 code | 31 blank | 48 comment | 37 complexity | 561ef2e2f84b42cefc812f373e2cef35 MD5 | raw file
  1. <?php
  2. /*
  3. Защита от прямой загрузки
  4. */
  5. defined('ACCESS') or die();
  6. class FileManager {
  7. private static $instance;
  8. /*
  9. Конструктор класса FileManager
  10. */
  11. public function __construct(){
  12. FileManager::$instance = & $this;
  13. }
  14. /*
  15. Метод возвращает ссылку на обьект или создает его
  16. */
  17. public static function & instance(){
  18. empty(FileManager::$instance) and new FileManager;
  19. return FileManager::$instance;
  20. }
  21. /*
  22. Переводим к кодировке файловой системы
  23. */
  24. public static function convertToFileSystem($string = ''){
  25. return FileManager::__convertToCharSet($string, Manager::$conf['general.char_set'], Manager::$conf['filesystem.char_set']);
  26. }
  27. /*
  28. Переводи кодировку к кодировке файловой системы
  29. */
  30. public static function convertToGeniral($string = ''){
  31. return FileManager::__convertToCharSet($string, Manager::$conf['filesystem.char_set'], Manager::$conf['general.char_set']);
  32. }
  33. /*
  34. Переводим кодировку
  35. */
  36. public static function __convertToCharSet($string = '', $in_char_set = '', $out_char_set = ''){
  37. if (empty($in_char_set) || empty($out_char_set) || strcasecmp($in_char_set, $out_char_set) == 0) {
  38. return $string;
  39. }
  40. $converted = iconv($in_char_set, $out_char_set, $string);
  41. if ($converted === false) {
  42. return $string;
  43. }
  44. return $converted;
  45. }
  46. /*
  47. Метод получает список файлов и папок
  48. */
  49. public static function get_path_list($path = '', $f = false, $d = false){
  50. $list = array();
  51. if (($dp = opendir(FileManager::convertToFileSystem(Manager::$conf['filesystem.files_path'].$path))) !== false){
  52. while (($el = readdir($dp)) !== false){
  53. if ($el != '.' && $el != '..' && !preg_match(Manager::$conf['filesystem.exclude_directory_pattern'], $el)){
  54. /*
  55. разделение директории и файла
  56. */
  57. $el = FileManager::convertToGeniral($el);
  58. $obj = array ('name' => $el, 'path' => $path.$el.'/');
  59. if (is_file(FileManager::convertToFileSystem(Manager::$conf['filesystem.files_path'].$path.$el)) && $f) {
  60. /*
  61. проверка расширеня файла
  62. */
  63. preg_match('/\.([a-z]{3,})$/i', $el, $ext);
  64. if (!in_array(strtolower($ext[1]), explode('|', Manager::$conf['filesystem.allowed_extensions']))) continue;
  65. $list[] = $obj;
  66. }
  67. if (is_dir(FileManager::convertToFileSystem(Manager::$conf['filesystem.files_path'].$obj['path'])) && $d) $list[] = $obj;
  68. }
  69. }
  70. closedir($dp);
  71. }
  72. /*
  73. сортировка списка
  74. */
  75. Manager::$conf['filesystem.sort'] ? sort($list) : rsort($list);
  76. return $list;
  77. }
  78. /*
  79. Метод создает католог
  80. */
  81. public static function create_dir($path = ''){
  82. if ($path == '') return false;
  83. return mkdir(FileManager::convertToFileSystem($path), Manager::$conf['filesystem.directory_chmod']);
  84. }
  85. public static function path_encode($path = ''){
  86. if ($path == '') return $path;
  87. preg_match('/^(http.{3,4}[a-zA-z.]+\/).*/i', $path, $url);
  88. $path = preg_replace('/^http.{3,4}[a-zA-z.]+\//i', '', $path);
  89. $elements = explode('/', $path);
  90. $path = count($url) > 1 ? $url[1] : '';
  91. foreach ($elements as $element){
  92. $path .=rawurlencode($element) . '/';
  93. }
  94. return substr($path, 0, strlen($path) - 1);
  95. }
  96. /*
  97. Метод удаляет каталог
  98. */
  99. public static function delete_dir($path = '' , $encode = true){
  100. $done = false;
  101. if ($path == '') return true;
  102. if (($dp = opendir($path)) !== false){
  103. while (($el = readdir($dp)) !== false){
  104. if ($el == '.' || $el == '..') continue;
  105. $obj = $path.DS.$el;
  106. if (is_file($obj)){
  107. FileManager::delete_file($obj);
  108. continue;
  109. }
  110. if (is_dir($obj)){
  111. FileManager::delete_dir($obj);
  112. }
  113. }
  114. closedir($dp);
  115. rmdir($path);
  116. $done = true;
  117. }
  118. return $done;
  119. }
  120. /*
  121. Метод очищает путь от лишних слешев и переводит его в верный формат фаловой системы
  122. */
  123. public static function clear_path($path = ''){
  124. return preg_replace('/\\\+|\/+/', DS, $path);
  125. }
  126. /*
  127. Метод возвращает расширения файла
  128. */
  129. public static function get_ext($filename = ''){
  130. preg_match('/\.([a-z]{3,})$/i', $filename, $ext);
  131. return $ext[1];
  132. }
  133. /*
  134. Метод удаляет файл
  135. */
  136. public static function delete_file($filename = ''){
  137. return $filename != '' && unlink($filename);
  138. }
  139. /*
  140. Метод переименновывает фил или дерикторию
  141. */
  142. public static function rename($old = '', $new = ''){
  143. return $old != '' && $new != '' && rename(FileManager::convertToFileSystem($old), FileManager::convertToFileSystem($new));
  144. }
  145. }
  146. ?>