Ejemplo n.º 1
0
 /**
  * Gets the relative path from a source directory to a target directory.
  * The allowed TYPO3 path is checked as well, thus it's not possible to go to upper levels.
  *
  * @param string $sourcePath Absolute source path
  * @param string $targetPath Absolute target path
  * @return NULL|string
  */
 public static function getRelativePath($sourcePath, $targetPath)
 {
     $relativePath = null;
     $sourcePath = rtrim(GeneralUtility::fixWindowsFilePath($sourcePath), '/');
     $targetPath = rtrim(GeneralUtility::fixWindowsFilePath($targetPath), '/');
     if ($sourcePath !== $targetPath) {
         $commonPrefix = self::getCommonPrefix(array($sourcePath, $targetPath));
         if ($commonPrefix !== null && GeneralUtility::isAllowedAbsPath($commonPrefix)) {
             $commonPrefixLength = strlen($commonPrefix);
             $resolvedSourcePath = '';
             $resolvedTargetPath = '';
             $sourcePathSteps = 0;
             if (strlen($sourcePath) > $commonPrefixLength) {
                 $resolvedSourcePath = (string) substr($sourcePath, $commonPrefixLength);
             }
             if (strlen($targetPath) > $commonPrefixLength) {
                 $resolvedTargetPath = (string) substr($targetPath, $commonPrefixLength);
             }
             if ($resolvedSourcePath !== '') {
                 $sourcePathSteps = count(explode('/', $resolvedSourcePath));
             }
             $relativePath = self::sanitizeTrailingSeparator(str_repeat('../', $sourcePathSteps) . $resolvedTargetPath);
         }
     }
     return $relativePath;
 }