PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_akeeba/akeeba/utils/filesystem.php

https://bitbucket.org/organicdevelopment/joomla-2.5
PHP | 218 lines | 174 code | 12 blank | 32 comment | 16 complexity | e179dea0f15f91657a0b69cd0b24d118 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-2.0, MIT, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * Akeeba Engine
  4. * The modular PHP5 site backup engine
  5. * @copyright Copyright (c)2009-2012 Nicholas K. Dionysopoulos
  6. * @license GNU GPL version 3 or, at your option, any later version
  7. * @package akeebaengine
  8. * @version $Id$
  9. */
  10. // Protection against direct access
  11. defined('AKEEBAENGINE') or die('Restricted access');
  12. /**
  13. * Utility functions related to filesystem objects, e.g. path translation
  14. */
  15. class AEUtilFilesystem
  16. {
  17. /**
  18. * Makes a Windows path more UNIX-like, by turning backslashes to forward slashes.
  19. * It takes into account UNC paths, e.g. \\myserver\some\folder becomes
  20. * \\myserver/some/folder
  21. *
  22. * @param string $p_path The path to transform
  23. * @return string
  24. */
  25. static public function TranslateWinPath( $p_path )
  26. {
  27. static $is_windows;
  28. if(empty($is_windows))
  29. {
  30. $is_windows = (DIRECTORY_SEPARATOR == '\\');
  31. }
  32. $is_unc = false;
  33. if ($is_windows)
  34. {
  35. // Is this a UNC path?
  36. $is_unc = (substr($p_path, 0, 2) == '\\\\') || (substr($p_path, 0, 2) == '//');
  37. // Change potential windows directory separator
  38. if ((strpos($p_path, '\\') > 0) || (substr($p_path, 0, 1) == '\\')){
  39. $p_path = strtr($p_path, '\\', '/');
  40. }
  41. }
  42. // Remove multiple slashes
  43. $p_path = str_replace('///','/',$p_path);
  44. $p_path = str_replace('//','/',$p_path);
  45. // Fix UNC paths
  46. if($is_unc)
  47. {
  48. $p_path = '//'.ltrim($p_path,'/');
  49. }
  50. return $p_path;
  51. }
  52. /**
  53. * Removes trailing slash or backslash from a pathname
  54. *
  55. * @param string $path The path to treat
  56. * @return string The path without the trailing slash/backslash
  57. */
  58. static public function TrimTrailingSlash($path)
  59. {
  60. $newpath = $path;
  61. if( substr($path, strlen($path)-1, 1) == '\\' )
  62. {
  63. $newpath = substr($path, 0, strlen($path)-1);
  64. }
  65. if( substr($path, strlen($path)-1, 1) == '/' )
  66. {
  67. $newpath = substr($path, 0, strlen($path)-1);
  68. }
  69. return $newpath;
  70. }
  71. public static function get_archive_name_variables()
  72. {
  73. $variables = null;
  74. $registry = AEFactory::getConfiguration();
  75. $serialized = $registry->get('volatile.core.archivenamevars', null);
  76. if(!empty($serialized)) {
  77. $variables = @unserialize($serialized);
  78. }
  79. if(empty($variables))
  80. {
  81. $host = AEPlatform::getInstance()->get_host();
  82. $version = defined('AKEEBA_VERSION') ? AKEEBA_VERSION : 'svn';
  83. $variables = array(
  84. '[DATE]' => AEPlatform::getInstance()->get_local_timestamp("%Y%m%d"),
  85. '[YEAR]' => AEPlatform::getInstance()->get_local_timestamp("%Y"),
  86. '[MONTH]' => AEPlatform::getInstance()->get_local_timestamp("%m"),
  87. '[DAY]' => AEPlatform::getInstance()->get_local_timestamp("%d"),
  88. '[TIME]' => AEPlatform::getInstance()->get_local_timestamp("%H%M%S"),
  89. '[WEEK]' => AEPlatform::getInstance()->get_local_timestamp("%U"),
  90. '[WEEKDAY]' => AEPlatform::getInstance()->get_local_timestamp("%A"),
  91. '[HOST]' => empty($host) ? 'unknown_host' : $host,
  92. '[RANDOM]' => md5(microtime()),
  93. '[VERSION]' => $version
  94. );
  95. }
  96. return $variables;
  97. }
  98. public static function replace_archive_name_variables($source)
  99. {
  100. $tagReplacements = self::get_archive_name_variables();
  101. return str_replace( array_keys($tagReplacements), array_values($tagReplacements), $source );
  102. }
  103. /**
  104. * Returns the relative and absolute path to the archive, if defined
  105. * @param string $relative The relative path
  106. * @param string $absolute The absolute path
  107. */
  108. public static function get_archive_name( &$relative, &$absolute )
  109. {
  110. static $relative_path = null;
  111. static $absolute_path = null;
  112. if( is_null($relative_path) || is_null($absolute_path) )
  113. {
  114. $registry = AEFactory::getConfiguration();
  115. // Import volatile scripting keys to the registry
  116. AEUtilScripting::importScriptingToRegistry();
  117. // Determine the extension
  118. $force_extension = AEUtilScripting::getScriptingParameter('core.forceextension', null);
  119. if( is_null($force_extension) )
  120. {
  121. $archiver = AEFactory::getArchiverEngine();
  122. $extension = $archiver->getExtension();
  123. }
  124. else
  125. {
  126. $extension = $force_extension;
  127. }
  128. // Get the template name
  129. $templateName = $registry->get('akeeba.basic.archive_name');
  130. AEUtilLogger::WriteLog(_AE_LOG_DEBUG, "Archive template name: $templateName");
  131. // Parse all tags
  132. $templateName = self::replace_archive_name_variables($templateName);
  133. AEUtilLogger::WriteLog(_AE_LOG_DEBUG, "Expanded template name: $templateName");
  134. $ds = DIRECTORY_SEPARATOR;
  135. $relative_path = $templateName.$extension;
  136. $absolute_path = AEUtilFilesystem::TranslateWinPath( $registry->get('akeeba.basic.output_directory').$ds.$relative_path );
  137. }
  138. $relative = $relative_path;
  139. $absolute = $absolute_path;
  140. }
  141. /**
  142. * Checks if a folder (directory) exists.
  143. * @param string $folder_to_check The path to check if it exists
  144. * @return bool True if the folder is there, false if it's not or the path exists but is not a folder
  145. */
  146. static function folderExists($folder_to_check)
  147. {
  148. // Try to find the real path to the folder
  149. $folder_clean = @realpath($folder_to_check);
  150. if( ($folder_clean !== false) && (!empty($folder_clean)) )
  151. {
  152. $folder = $folder_clean;
  153. }
  154. else
  155. {
  156. $folder = $folder_to_check;
  157. }
  158. // Clear filesystem cache to avoid getting stale information
  159. if(function_exists("clearstatcache")) clearstatcache();
  160. // Check that the path is there
  161. if( !file_exists($folder) ) return false;
  162. // Check that it is a folder, indeed
  163. if( !is_dir($folder) ) return false;
  164. return true;
  165. }
  166. static function translateStockDirs( $folder, $translate_win_dirs = false, $trim_trailing_slash = false )
  167. {
  168. static $stock_dirs;
  169. if(empty($stock_dirs))
  170. {
  171. $stock_dirs = AEPlatform::getInstance()->get_stock_directories();
  172. }
  173. $temp = $folder;
  174. foreach($stock_dirs as $find => $replace)
  175. {
  176. $temp = str_replace($find, $replace, $temp);
  177. }
  178. if($translate_win_dirs)
  179. {
  180. $temp = self::TranslateWinPath( $temp );
  181. }
  182. if( $trim_trailing_slash )
  183. {
  184. $temp = self::TrimTrailingSlash( $temp );
  185. }
  186. return $temp;
  187. }
  188. }