/** * Return a public path pointing to a resource. * * @param string $resource * @return string */ public static function getRelativePath($resource) { // If file is not found, resolve the path if (!is_file(PATH_site . $resource)) { $resource = substr(self::resolvePath($resource), strlen(PATH_site)); } return PathUtility::getRelativePathTo(PathUtility::dirname(PATH_site . $resource)) . PathUtility::basename($resource); }
/** * Get url * * @param bool $relativeToCurrentScript Determines whether the URL returned should be relative to the current script, in case it is relative at all. * @return string */ public function getUrl($relativeToCurrentScript = false) { $url = $this->url; if ($relativeToCurrentScript && !GeneralUtility::isValidUrl($url)) { $absolutePathToContainingFolder = PathUtility::dirname(PATH_site . $url); $pathPart = PathUtility::getRelativePathTo($absolutePathToContainingFolder); $filePart = substr(PATH_site . $url, strlen($absolutePathToContainingFolder) + 1); $url = $pathPart . $filePart; } return $url; }
/** * @param ResourceStorage $storage * @param AbstractDriver $driver * @param ResourceInterface $resource * @param boolean $relativeToCurrentScript * @param array $urlData */ public function getPublicUrl(ResourceStorage $storage, AbstractDriver $driver, ResourceInterface $resource, $relativeToCurrentScript, array $urlData) { if (!$driver instanceof LocalDriver) { // We cannot handle other files than local files yet return; } $publicUrl = $this->resourcePublisher->getResourceWebUri($resource); if ($publicUrl !== FALSE) { // If requested, make the path relative to the current script in order to make it possible // to use the relative file if ($relativeToCurrentScript) { $publicUrl = PathUtility::getRelativePathTo(PathUtility::dirname(PATH_site . $publicUrl)) . PathUtility::basename($publicUrl); } // $urlData['publicUrl'] is passed by reference, so we can change that here and the value will be taken into account $urlData['publicUrl'] = $publicUrl; } }
/** * @param string $content * @param array $conf * @return string */ public function getTemplateFromName($content, $conf) { $basename = $content; if ($conf['paths'][0] === '<') { $key = trim(substr($conf['paths'], 1)); $typoScriptParser = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\TypoScript\\Parser\\TypoScriptParser'); list($name, $conf['paths.']) = $typoScriptParser->getVal($key, $GLOBALS['TSFE']->tmpl->setup); } $paths = ArrayUtility::sortArrayWithIntegerKeys($conf['paths.']); $paths = array_reverse($paths, TRUE); foreach ($paths as $path) { // why does it have to be relative? $test_file = PathUtility::getRelativePathTo(GeneralUtility::getFileAbsFileName($path)) . $basename; if (is_file($test_file)) { return $test_file; } if (is_file($test_file . '.html')) { return $test_file . '.html'; } } return $content; }
/** * Returns the public URL to a file. * * @param \TYPO3\CMS\Core\Resource\ResourceInterface $resource * @param bool $relativeToCurrentScript Determines whether the URL returned should be relative to the current script, in case it is relative at all (only for the LocalDriver) * @return string */ public function getPublicUrl(\TYPO3\CMS\Core\Resource\ResourceInterface $resource, $relativeToCurrentScript = false) { if (\TYPO3\CMS\Core\Utility\GeneralUtility::isFirstPartOfStr($resource->getIdentifier(), '/_processed_/')) { $publicUrl = '../typo3temp/yag' . $resource->getIdentifier(); // TODO: ....!!!! } else { $item = $resource->getProperty('yagItem'); if (!$item instanceof \Tx_Yag_Domain_Model_Item) { $pathInfo = new PathInfo($resource->getIdentifier()); $item = $this->getItem($pathInfo); } $publicUrl = $this->yagFileSystemDiv->getFileRelFileName($item->getSourceuri()); } if ($relativeToCurrentScript) { $publicUrl = PathUtility::getRelativePathTo(PathUtility::dirname(PATH_site . $publicUrl)) . PathUtility::basename($publicUrl); } return $publicUrl; }
/** * This function acts as a wrapper to allow relative and paths starting with EXT: to be dealt with * in this very case to always return the absolute web path to be included directly before output. * * This is mainly added so the EXT: syntax can be resolved for PageRenderer in one central place, * and hopefully removed in the future by one standard API call. * * @param string $file the filename to process * @param bool $prepareForOutput whether the file should be prepared as version numbered file and prefixed as absolute webpath * @return string * @internal */ protected function getStreamlinedFileName($file, $prepareForOutput = true) { if (strpos($file, 'EXT:') === 0) { $file = GeneralUtility::getFileAbsFileName($file); // as the path is now absolute, make it "relative" to the current script to stay compatible $file = PathUtility::getRelativePathTo($file); $file = rtrim($file, '/'); } else { $file = GeneralUtility::resolveBackPath($file); } if ($prepareForOutput) { $file = GeneralUtility::createVersionNumberedFilename($file); $file = PathUtility::getAbsoluteWebPath($file); } return $file; }
/** * Returns a publicly accessible URL for a file. * * WARNING: Access to the file may be restricted by further means, e.g. * some web-based authentication. You have to take care of this yourself. * * @param ResourceInterface $resourceObject The file or folder object * @param bool $relativeToCurrentScript Determines whether the URL returned should be relative to the current script, in case it is relative at all (only for the LocalDriver) * @return string */ public function getPublicUrl(ResourceInterface $resourceObject, $relativeToCurrentScript = false) { $publicUrl = null; if ($this->isOnline()) { // Pre-process the public URL by an accordant slot $this->emitPreGeneratePublicUrlSignal($resourceObject, $relativeToCurrentScript, array('publicUrl' => &$publicUrl)); if ($publicUrl === null && $resourceObject instanceof File && ($helper = OnlineMediaHelperRegistry::getInstance()->getOnlineMediaHelper($resourceObject)) !== false) { $publicUrl = $helper->getPublicUrl($resourceObject, $relativeToCurrentScript); } // If slot did not handle the signal, use the default way to determine public URL if ($publicUrl === null) { if ($this->hasCapability(self::CAPABILITY_PUBLIC)) { $publicUrl = $this->driver->getPublicUrl($resourceObject->getIdentifier()); } if ($publicUrl === null && $resourceObject instanceof FileInterface) { $queryParameterArray = array('eID' => 'dumpFile', 't' => ''); if ($resourceObject instanceof File) { $queryParameterArray['f'] = $resourceObject->getUid(); $queryParameterArray['t'] = 'f'; } elseif ($resourceObject instanceof ProcessedFile) { $queryParameterArray['p'] = $resourceObject->getUid(); $queryParameterArray['t'] = 'p'; } $queryParameterArray['token'] = GeneralUtility::hmac(implode('|', $queryParameterArray), 'resourceStorageDumpFile'); $publicUrl = 'index.php?' . str_replace('+', '%20', http_build_query($queryParameterArray)); } // If requested, make the path relative to the current script in order to make it possible // to use the relative file if ($publicUrl !== null && $relativeToCurrentScript && !GeneralUtility::isValidUrl($publicUrl)) { $absolutePathToContainingFolder = PathUtility::dirname(PATH_site . $publicUrl); $pathPart = PathUtility::getRelativePathTo($absolutePathToContainingFolder); $filePart = substr(PATH_site . $publicUrl, strlen($absolutePathToContainingFolder) + 1); $publicUrl = $pathPart . $filePart; } } } return $publicUrl; }
/** * Returns the public URL to a file. For the local driver, this will always * return a path relative to PATH_site. * * @param \TYPO3\CMS\Core\Resource\ResourceInterface $fileOrFolder * @param bool $relativeToCurrentScript Determines whether the URL returned should be relative to the current script, in case it is relative at all (only for the LocalDriver) * @return string */ public function getPublicUrl(\TYPO3\CMS\Core\Resource\ResourceInterface $fileOrFolder, $relativeToCurrentScript = FALSE) { if ($this->configuration['pathType'] === 'relative' && rtrim($this->configuration['basePath'], '/') !== '') { $publicUrl = rtrim($this->configuration['basePath'], '/') . '/' . ltrim($fileOrFolder->getIdentifier(), '/'); } elseif (isset($this->baseUri)) { $publicUrl = $this->baseUri . ltrim($fileOrFolder->getIdentifier(), '/'); } else { throw new \TYPO3\CMS\Core\Resource\Exception('Public URL of file cannot be determined', 1329765518); } // If requested, make the path relative to the current script in order to make it possible // to use the relative file if ($relativeToCurrentScript) { $publicUrl = \TYPO3\CMS\Core\Utility\PathUtility::getRelativePathTo(dirname(PATH_site . $publicUrl)) . basename($publicUrl); } return $publicUrl; }
/** * Add all *.css files of the directory $path to the stylesheets * * @param string $path directory to add * @return void */ public function addStyleSheetDirectory($path) { $path = GeneralUtility::getFileAbsFileName($path); // Read all files in directory and sort them alphabetically $cssFiles = GeneralUtility::getFilesInDir($path, 'css'); foreach ($cssFiles as $cssFile) { $this->pageRenderer->addCssFile(PathUtility::getRelativePathTo($path) . $cssFile); } }
/** * Resolve path * * @param string $resourcePath * @return NULL|string */ protected function resolvePath($resourcePath) { $absoluteFilePath = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($resourcePath); $absolutePath = dirname($absoluteFilePath); $fileName = basename($absoluteFilePath); return \TYPO3\CMS\Core\Utility\PathUtility::getRelativePathTo($absolutePath) . $fileName; }
/** * Get relative path from absolute path, but don't touch if it's already a relative path * * @param string $path * @return string */ public static function getRelativeFolder($path) { if (PathUtility::isAbsolutePath($path)) { $path = PathUtility::getRelativePathTo($path); } return $path; }
/** * Collects information about the file * @param string $filename Filename * @return array Array of collected information */ private function getFileInformation($filename, $throwExeptions = TRUE) { $fileProperties = array(); $fileProperties['filename'] = $filename; if (strlen(trim($filename)) == 0 && $throwExeptions) { throw new \Exception(LocalizationUtility::translate('iconFontSelector.exception.missingFilename', 't3kit_extension_tools'), 20); } $fileProperties['absFileName'] = GeneralUtility::getFileAbsFileName($filename); $fileProperties['fileExists'] = file_exists($fileProperties['absFileName']); if (!$fileProperties['fileExists'] && $throwExeptions) { throw new \Exception(LocalizationUtility::translate('iconFontSelector.exception.fileExists', 't3kit_extension_tools') . ' (' . $filename . ')', 21); } $pathInfo = PathUtility::pathinfo($fileProperties['absFileName']); $fileProperties['dirname'] = $pathInfo['dirname']; $fileProperties['basename'] = $pathInfo['basename']; $fileProperties['extension'] = $pathInfo['extension']; $fileProperties['filename'] = $pathInfo['filename']; $fileProperties['isAllowedAbsPath'] = GeneralUtility::isAllowedAbsPath($fileProperties['absFileName']); if (!$fileProperties['isAllowedAbsPath'] && $throwExeptions) { throw new \Exception(LocalizationUtility::translate('iconFontSelector.exception.isAllowedAbsPath', 't3kit_extension_tools'), 22); } $fileProperties['relativePath'] = PathUtility::getRelativePathTo($fileProperties['dirname']); return $fileProperties; }
/** * signal slot for public url generation of a file * @see \TYPO3\CMS\Core\Resource\ResourceStorage and the function getPublicUrl * * @param \TYPO3\CMS\Core\Resource\ResourceStorage $t * @param \TYPO3\CMS\Core\Resource\Driver\LocalDriver $driver * @param object $resourceObject e.g. \TYPO3\CMS\Core\Resource\File, \TYPO3\CMS\Core\Resource\Folder, \TYPO3\CMS\Core\Resource\ProcessedFile * @param boolean $relativeToCurrentScript * @param array $urlData * * @return void */ public function preGeneratePublicUrl($t, $driver, $resourceObject, $relativeToCurrentScript, $urlData) { $this->emSettings = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][$this->extKey]); // check if resource is file if (is_a($resourceObject, 'TYPO3\\CMS\\Core\\Resource\\File')) { $fileData = $this->getFileData($resourceObject); $newPublicUrl = ''; $enable = true; // check if TYPO3 is in frontend mode if (TYPO3_MODE === 'FE') { if ($this->emSettings['disable_fe']) { $enable = false; } else { // check filetypes field if (!empty($this->emSettings['filetypes_fe'])) { $filetypes = explode(',', $this->emSettings['filetypes_fe']); if (in_array($fileData['extension'], $filetypes)) { } else { $enable = false; } } if ($enable) { $newPublicUrl = $driver->getPublicUrl($fileData['identifier']); } } } // check if TYPO3 is in backend mode and make the path relative to the current script // in order to make it possible to use the relative file if (TYPO3_MODE === 'BE' && $relativeToCurrentScript) { //DebuggerUtility::var_dump($this->emSettings); if ($this->emSettings['disable_be']) { $enable = false; } else { // check filetypes field if (!empty($this->emSettings['filetypes_be'])) { $filetypes = explode(',', $this->emSettings['filetypes_be']); if (in_array($fileData['extension'], $filetypes)) { } else { $enable = false; } } if ($enable) { $publicUrl = $driver->getPublicUrl($fileData['identifier']); $absolutePathToContainingFolder = PathUtility::dirname(PATH_site . $publicUrl); $pathPart = PathUtility::getRelativePathTo($absolutePathToContainingFolder); $filePart = substr(PATH_site . $publicUrl, strlen($absolutePathToContainingFolder) + 1); $newPublicUrl = $pathPart . $filePart; } } } if (!empty($newPublicUrl) && !empty($fileData['modDate']) && $enable) { $urlData['publicUrl'] = $newPublicUrl . '?v=' . $fileData['modDate']; } } }
/** * this method creates sprite icon names for all tables in TCA (including their possible type-icons) * where there is no "typeicon_classes" of this TCA table ctrl section * (moved form \TYPO3\CMS\Backend\Utility\IconUtility) * * @return array Array as $iconName => $fileName */ protected function collectTcaSpriteIcons() { $tcaTables = array_keys($GLOBALS['TCA']); $resultArray = array(); // Path (relative from typo3 dir) for skin-Images if (isset($GLOBALS['TBE_STYLES']['skinImgAutoCfg']['relDir'])) { $skinPath = $GLOBALS['TBE_STYLES']['skinImgAutoCfg']['relDir']; } else { $skinPath = ''; } // check every table in the TCA, if an icon is needed foreach ($tcaTables as $tableName) { // This method is only needed for TCA tables where // typeicon_classes are not configured if (is_array($GLOBALS['TCA'][$tableName]) && !is_array($GLOBALS['TCA'][$tableName]['ctrl']['typeicon_classes'])) { $tcaCtrl = $GLOBALS['TCA'][$tableName]['ctrl']; // Adding the default Icon (without types) if (isset($tcaCtrl['iconfile'])) { // In CSS we need a path relative to the css file // [TCA][ctrl][iconfile] defines icons without path info to reside in gfx/i/ if (\TYPO3\CMS\Core\Utility\StringUtility::beginsWith($tcaCtrl['iconfile'], 'EXT:')) { list($extensionKey, $relativePath) = explode('/', substr($tcaCtrl['iconfile'], 4), 2); $pathInfo = PathUtility::pathinfo(\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath($extensionKey) . $relativePath); $path = PathUtility::getRelativePathTo($pathInfo['dirname']); $icon = $path . $pathInfo['basename']; } elseif (strpos($tcaCtrl['iconfile'], '/') !== false) { $icon = $tcaCtrl['iconfile']; $icon = GeneralUtility::resolveBackPath($icon); } else { $icon = $skinPath . 'gfx/i/' . $tcaCtrl['iconfile']; $icon = GeneralUtility::resolveBackPath($icon); } $resultArray['tcarecords-' . $tableName . '-default'] = $icon; } } } return $resultArray; }