PageRenderTime 39ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/kraymitchell/apex
PHP | 235 lines | 191 code | 12 blank | 32 comment | 16 complexity | 1e4baae924ee99f2ec1b9cb380f1f80d MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0, BSD-3-Clause, LGPL-2.1, GPL-3.0
  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. *
  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. $platformVars = AEPlatform::getInstance()->getPlatformVersion();
  84. $siteName = AEPlatform::getInstance()->get_site_name();
  85. $siteName = htmlentities(utf8_decode($siteName));
  86. $siteName = preg_replace(
  87. array('/&szlig;/','/&(..)lig;/', '/&([aouAOU])uml;/','/&(.)[^;]*;/'),
  88. array('ss',"$1","$1".'e',"$1"),
  89. $siteName);
  90. $siteName = trim(strtolower($siteName));
  91. $siteName = preg_replace(array('/\s+/','/[^A-Za-z0-9\-]/'), array('-',''), $siteName);
  92. if (strlen($siteName) > 50) {
  93. $siteName = substr($siteName, 0, 50);
  94. }
  95. $variables = array(
  96. '[DATE]' => AEPlatform::getInstance()->get_local_timestamp("Ymd"),
  97. '[YEAR]' => AEPlatform::getInstance()->get_local_timestamp("Y"),
  98. '[MONTH]' => AEPlatform::getInstance()->get_local_timestamp("m"),
  99. '[DAY]' => AEPlatform::getInstance()->get_local_timestamp("d"),
  100. '[TIME]' => AEPlatform::getInstance()->get_local_timestamp("His"),
  101. '[WEEK]' => AEPlatform::getInstance()->get_local_timestamp("W"),
  102. '[WEEKDAY]' => AEPlatform::getInstance()->get_local_timestamp("w"),
  103. '[HOST]' => empty($host) ? 'unknown_host' : $host,
  104. '[RANDOM]' => md5(microtime()),
  105. '[VERSION]' => $version,
  106. '[PLATFORM_NAME]' => $platformVars['name'],
  107. '[PLATFORM_VERSION]' => $platformVars['version'],
  108. '[SITENAME]' => $siteName
  109. );
  110. }
  111. return $variables;
  112. }
  113. public static function replace_archive_name_variables($source)
  114. {
  115. $tagReplacements = self::get_archive_name_variables();
  116. return str_replace( array_keys($tagReplacements), array_values($tagReplacements), $source );
  117. }
  118. /**
  119. * Returns the relative and absolute path to the archive, if defined
  120. * @param string $relative The relative path
  121. * @param string $absolute The absolute path
  122. */
  123. public static function get_archive_name( &$relative, &$absolute )
  124. {
  125. static $relative_path = null;
  126. static $absolute_path = null;
  127. if( is_null($relative_path) || is_null($absolute_path) )
  128. {
  129. $registry = AEFactory::getConfiguration();
  130. // Import volatile scripting keys to the registry
  131. AEUtilScripting::importScriptingToRegistry();
  132. // Determine the extension
  133. $force_extension = AEUtilScripting::getScriptingParameter('core.forceextension', null);
  134. if( is_null($force_extension) )
  135. {
  136. $archiver = AEFactory::getArchiverEngine();
  137. $extension = $archiver->getExtension();
  138. }
  139. else
  140. {
  141. $extension = $force_extension;
  142. }
  143. // Get the template name
  144. $templateName = $registry->get('akeeba.basic.archive_name');
  145. AEUtilLogger::WriteLog(_AE_LOG_DEBUG, "Archive template name: $templateName");
  146. // Parse all tags
  147. $templateName = self::replace_archive_name_variables($templateName);
  148. AEUtilLogger::WriteLog(_AE_LOG_DEBUG, "Expanded template name: $templateName");
  149. $ds = DIRECTORY_SEPARATOR;
  150. $relative_path = $templateName.$extension;
  151. $absolute_path = AEUtilFilesystem::TranslateWinPath( $registry->get('akeeba.basic.output_directory').$ds.$relative_path );
  152. }
  153. $relative = $relative_path;
  154. $absolute = $absolute_path;
  155. }
  156. /**
  157. * Checks if a folder (directory) exists.
  158. * @param string $folder_to_check The path to check if it exists
  159. * @return bool True if the folder is there, false if it's not or the path exists but is not a folder
  160. */
  161. static function folderExists($folder_to_check)
  162. {
  163. // Try to find the real path to the folder
  164. $folder_clean = @realpath($folder_to_check);
  165. if( ($folder_clean !== false) && (!empty($folder_clean)) )
  166. {
  167. $folder = $folder_clean;
  168. }
  169. else
  170. {
  171. $folder = $folder_to_check;
  172. }
  173. // Clear filesystem cache to avoid getting stale information
  174. if(function_exists("clearstatcache")) clearstatcache();
  175. // Check that the path is there
  176. if( !file_exists($folder) ) return false;
  177. // Check that it is a folder, indeed
  178. if( !is_dir($folder) ) return false;
  179. return true;
  180. }
  181. static function translateStockDirs( $folder, $translate_win_dirs = false, $trim_trailing_slash = false )
  182. {
  183. static $stock_dirs;
  184. if(empty($stock_dirs))
  185. {
  186. $stock_dirs = AEPlatform::getInstance()->get_stock_directories();
  187. }
  188. $temp = $folder;
  189. foreach($stock_dirs as $find => $replace)
  190. {
  191. $temp = str_replace($find, $replace, $temp);
  192. }
  193. if($translate_win_dirs)
  194. {
  195. $temp = self::TranslateWinPath( $temp );
  196. }
  197. if( $trim_trailing_slash )
  198. {
  199. $temp = self::TrimTrailingSlash( $temp );
  200. }
  201. return $temp;
  202. }
  203. }