PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/tubepress/sys/classes/org/tubepress/impl/filesystem/FsExplorer.class.php

https://gitlab.com/endomorphosis/jeffersonsmithmayor
PHP | 297 lines | 168 code | 71 blank | 58 comment | 35 complexity | 83fb94d5203d172d2bf938a28fcf50b5 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright 2006 - 2012 Eric D. Hough (http://ehough.com)
  4. *
  5. * This file is part of TubePress (http://tubepress.org)
  6. *
  7. * TubePress is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * TubePress is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with TubePress. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. class_exists('org_tubepress_impl_classloader_ClassLoader') || require dirname(__FILE__) . '/../classloader/ClassLoader.class.php';
  22. org_tubepress_impl_classloader_ClassLoader::loadClasses(array(
  23. 'org_tubepress_api_filesystem_Explorer',
  24. 'org_tubepress_impl_log_Log',
  25. ));
  26. /**
  27. * Some filesystem utilities
  28. *
  29. */
  30. class org_tubepress_impl_filesystem_FsExplorer implements org_tubepress_api_filesystem_Explorer
  31. {
  32. const LOG_PREFIX = 'FS Explorer';
  33. /**
  34. * Finds the absolute path of the TubePress installation on the filesystem.
  35. *
  36. * @return string The absolute filesystem path of this TubePress installation.
  37. */
  38. public function getTubePressBaseInstallationPath()
  39. {
  40. return realpath(dirname(__FILE__) . '/../../../../../../');
  41. }
  42. /**
  43. * Find the directory name of the TubePress base installation.
  44. *
  45. * @return string The base name of the TubePress installation directory.
  46. */
  47. function getTubePressInstallationDirectoryBaseName()
  48. {
  49. return basename($this->getTubePressBaseInstallationPath());
  50. }
  51. /**
  52. * Find the directories contained in the given directory (non-recursive).
  53. *
  54. * @param string $dir The absolute filesystem path of the directory to examine.
  55. * @param string $prefix The logging prefix.
  56. *
  57. * @return array The names of the directories in the given directory (non-recursive).
  58. */
  59. public function getDirectoriesInDirectory($dir, $prefix)
  60. {
  61. $realDir = $dir;
  62. if (!is_dir($dir)) {
  63. org_tubepress_impl_log_Log::log($prefix, '<tt>%s</tt> is not a directory', $realDir);
  64. return array();
  65. }
  66. $toReturn = array();
  67. if ($handle = opendir($dir)) {
  68. while (($file = readdir($handle)) !== false) {
  69. if ($file === '.' || $file === '..' || strpos($file, ".") === 0) {
  70. continue;
  71. }
  72. if (!is_dir($dir . DIRECTORY_SEPARATOR . $file)) {
  73. continue;
  74. }
  75. array_push($toReturn, realpath($dir . DIRECTORY_SEPARATOR . $file));
  76. }
  77. closedir($handle);
  78. } else {
  79. org_tubepress_impl_log_Log::log($prefix, 'Could not open <tt>%s</tt>', $realDir);
  80. }
  81. return $toReturn;
  82. }
  83. /**
  84. * Find the files contained in the given directory (non-recursive).
  85. *
  86. * @param string $dir The absolute filesystem path of the directory to examine.
  87. * @param string $prefix The logging prefix.
  88. *
  89. * @return array The names of the files in the given directory (non-recursive).
  90. */
  91. public function getFilenamesInDirectory($dir, $prefix)
  92. {
  93. $realDir = $dir;
  94. if (!is_dir($dir)) {
  95. org_tubepress_impl_log_Log::log($prefix, '<tt>%s</tt> is not a directory', $realDir);
  96. return array();
  97. }
  98. $toReturn = array();
  99. if ($handle = opendir($dir)) {
  100. while (($file = readdir($handle)) !== false) {
  101. if ($file === '.' || $file === '..') {
  102. continue;
  103. }
  104. if (is_dir($dir . DIRECTORY_SEPARATOR . $file)) {
  105. continue;
  106. }
  107. array_push($toReturn, realpath($dir . DIRECTORY_SEPARATOR . $file));
  108. }
  109. closedir($handle);
  110. } else {
  111. org_tubepress_impl_log_Log::log($prefix, 'Could not open <tt>%s</tt>', $realDir);
  112. }
  113. return $toReturn;
  114. }
  115. /**
  116. * Attempt to get temporary directory.
  117. *
  118. * @return string The absolute path of a temporary directory, preferably the system directory.
  119. */
  120. public function getSystemTempDirectory()
  121. {
  122. if (function_exists('sys_get_temp_dir')) {
  123. return sys_get_temp_dir();
  124. }
  125. // Try to get from environment variable
  126. if (!empty($_ENV['TMP'])) {
  127. return realpath($_ENV['TMP']);
  128. } else if (!empty($_ENV['TMPDIR'])) {
  129. return realpath($_ENV['TMPDIR']);
  130. } else if (!empty($_ENV['TEMP'])) {
  131. return realpath($_ENV['TEMP']);
  132. } else {
  133. // Detect by creating a temporary file
  134. // Try to use system's temporary directory
  135. // as random name shouldn't exist
  136. $tempfile = @tempnam(md5(uniqid(rand(), true)), '');
  137. if ( $tempfile ) {
  138. $tempdir = realpath(dirname($tempfile));
  139. @unlink($tempfile);
  140. return $tempdir;
  141. } else {
  142. return false;
  143. }
  144. }
  145. }
  146. public function copyDirectory($source, $dest, $level = 0)
  147. {
  148. $source = self::_cleanPath($source);
  149. $dest = self::_cleanPath($dest);
  150. org_tubepress_impl_log_Log::log(self::LOG_PREFIX, '%sAsked to copy %s to %s', self::_spaces($level), $source, $dest);
  151. if (!is_dir($source)) {
  152. org_tubepress_impl_log_Log::log(self::LOG_PREFIX, '%s%s is not a directory', self::_spaces($level), $source);
  153. return false;
  154. }
  155. if (!is_readable($source)) {
  156. org_tubepress_impl_log_Log::log(self::LOG_PREFIX, '%sCannot read source directory at ', self::_spaces($level), $source);
  157. return false;
  158. }
  159. if (!is_dir($dest)) {
  160. org_tubepress_impl_log_Log::log(self::LOG_PREFIX, '%s%s is not a directory', self::_spaces($level), $dest);
  161. return false;
  162. }
  163. if (!is_readable($dest)) {
  164. org_tubepress_impl_log_Log::log(self::LOG_PREFIX, '%sCannot write to destination directory at ', self::_spaces($level), $dest);
  165. return false;
  166. }
  167. org_tubepress_impl_log_Log::log(self::LOG_PREFIX, '%sCopying %s to %s', self::_spaces($level), $source, $dest);
  168. return $this->_doCopyDirectory($source, $dest, $level);
  169. }
  170. private function _doCopyDirectory($source, $dest, $level)
  171. {
  172. $files = $this->getFilenamesInDirectory($source, self::LOG_PREFIX);
  173. org_tubepress_impl_log_Log::log(self::LOG_PREFIX, '%sWill try to copy %d file(s) from %s to %s', self::_spaces($level), count($files), $source, $dest);
  174. $dirs = $this->getDirectoriesInDirectory($source, self::LOG_PREFIX);
  175. org_tubepress_impl_log_Log::log(self::LOG_PREFIX, '%sWill try to copy %d directories from %s to %s', self::_spaces($level), count($dirs), $source, $dest);
  176. $finalDest = $dest . DIRECTORY_SEPARATOR . basename($source);
  177. if ($this->ensureDirectoryExists($finalDest, $level) === false) {
  178. return false;
  179. }
  180. foreach ($files as $file) {
  181. $finalFileDest = $finalDest . DIRECTORY_SEPARATOR . basename($file);
  182. $result = @copy($file, $finalFileDest);
  183. if ($result === false) {
  184. org_tubepress_impl_log_Log::log(self::LOG_PREFIX, '%sCould not copy %s to %s', self::_spaces($level), $file, $finalFileDest);
  185. return false;
  186. }
  187. org_tubepress_impl_log_Log::log(self::LOG_PREFIX, '%sSuccessfully copied %s to %s', self::_spaces($level), $file, $finalFileDest);
  188. }
  189. foreach ($dirs as $dir) {
  190. $finalDirDest = self::_cleanPath($finalDest . DIRECTORY_SEPARATOR . basename($dir));
  191. if ($this->ensureDirectoryExists($finalDest, $level) === false) {
  192. return false;
  193. }
  194. $result = $this->copyDirectory($dir, $finalDest, $level + 1);
  195. if ($result === false) {
  196. org_tubepress_impl_log_Log::log(self::LOG_PREFIX, '%sCould not copy %s to %s', self::_spaces($level), $dir, $finalDirDest);
  197. return false;
  198. }
  199. }
  200. org_tubepress_impl_log_Log::log(self::LOG_PREFIX, '%sDone copying %s to %s', self::_spaces($level), $source, $dest);
  201. return true;
  202. }
  203. public function ensureDirectoryExists($path, $level = 0)
  204. {
  205. $path = self::_cleanPath($path);
  206. if (!is_dir($path)) {
  207. org_tubepress_impl_log_Log::log(self::LOG_PREFIX, '%sAttempting to create %s', self::_spaces($level), $path);
  208. $result = @mkdir($path);
  209. if ($result === false) {
  210. org_tubepress_impl_log_Log::log(self::LOG_PREFIX, '%sCould not create directory at %s', self::_spaces($level), $path);
  211. return false;
  212. }
  213. org_tubepress_impl_log_Log::log(self::LOG_PREFIX, '%sSuccessfully created directory at %s', self::_spaces($level), $path);
  214. return true;
  215. }
  216. }
  217. private static function _cleanPath($path)
  218. {
  219. return str_replace('//', '/', str_replace('\\', '/', $path));
  220. }
  221. private static function _spaces($level)
  222. {
  223. return substr(' ', 0, 3 * $level);
  224. }
  225. }