/sys/classes/org/tubepress/impl/filesystem/FsExplorer.class.php

https://github.com/tengle1080/tubepress · PHP · 163 lines · 89 code · 16 blank · 58 comment · 25 complexity · 6cd28b59fe3fae7bedd7b2e233c58368 MD5 · raw file

  1. <?php
  2. /**
  3. * Copyright 2006 - 2011 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. /**
  33. * Finds the absolute path of the TubePress installation on the filesystem.
  34. *
  35. * @return string The absolute filesystem path of this TubePress installation.
  36. */
  37. public function getTubePressBaseInstallationPath()
  38. {
  39. return realpath(dirname(__FILE__) . '/../../../../../../');
  40. }
  41. /**
  42. * Find the directory name of the TubePress base installation.
  43. *
  44. * @return string The base name of the TubePress installation directory.
  45. */
  46. function getTubePressInstallationDirectoryBaseName()
  47. {
  48. return basename($this->getTubePressBaseInstallationPath());
  49. }
  50. /**
  51. * Find the directories contained in the given directory (non-recursive).
  52. *
  53. * @param string $dir The absolute filesystem path of the directory to examine.
  54. * @param string $prefix The logging prefix.
  55. *
  56. * @return array The names of the directories in the given directory (non-recursive).
  57. */
  58. public function getDirectoriesInDirectory($dir, $prefix)
  59. {
  60. $realDir = $dir;
  61. if (!is_dir($dir)) {
  62. org_tubepress_impl_log_Log::log($prefix, '<tt>%s</tt> is not a directory', $realDir);
  63. return array();
  64. }
  65. $toReturn = array();
  66. if ($handle = opendir($dir)) {
  67. org_tubepress_impl_log_Log::log($prefix, 'Successfully opened <tt>%s</tt> to read contents.', $realDir);
  68. while (($file = readdir($handle)) !== false) {
  69. if ($file === '.' || $file === '..' || strpos($file, ".") === 0) {
  70. continue;
  71. }
  72. if (!is_dir($dir . '/' . $file)) {
  73. continue;
  74. }
  75. array_push($toReturn, realpath($dir . '/' . $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. org_tubepress_impl_log_Log::log($prefix, 'Successfully opened <tt>%s</tt> to read contents.', $realDir);
  101. while (($file = readdir($handle)) !== false) {
  102. if ($file === '.' || $file === '..') {
  103. continue;
  104. }
  105. if (is_dir($dir . '/' . $file)) {
  106. continue;
  107. }
  108. array_push($toReturn, realpath($dir . '/' . $file));
  109. }
  110. closedir($handle);
  111. } else {
  112. org_tubepress_impl_log_Log::log($prefix, 'Could not open <tt>%s</tt>', $realDir);
  113. }
  114. return $toReturn;
  115. }
  116. /**
  117. * Attempt to get temporary directory.
  118. *
  119. * @return string The absolute path of a temporary directory, preferably the system directory.
  120. */
  121. public function getSystemTempDirectory()
  122. {
  123. if (function_exists('sys_get_temp_dir')) {
  124. return sys_get_temp_dir();
  125. }
  126. // Try to get from environment variable
  127. if (!empty($_ENV['TMP'])) {
  128. return realpath($_ENV['TMP']);
  129. } else if (!empty($_ENV['TMPDIR'])) {
  130. return realpath($_ENV['TMPDIR']);
  131. } else if (!empty($_ENV['TEMP'])) {
  132. return realpath($_ENV['TEMP']);
  133. } else {
  134. // Detect by creating a temporary file
  135. // Try to use system's temporary directory
  136. // as random name shouldn't exist
  137. $tempfile = @tempnam(md5(uniqid(rand(), true)), '');
  138. if ( $tempfile ) {
  139. $tempdir = realpath(dirname($tempfile));
  140. @unlink($tempfile);
  141. return $tempdir;
  142. } else {
  143. return false;
  144. }
  145. }
  146. }
  147. }