"Fossies" - the Fresh Open Source Software Archive

Member "papayacms-core-6.9.4/src/system/Papaya/URL/Transformer/Absolute.php" (13 Dec 2019, 2822 Bytes) of package /linux/www/papayacms-core-6.9.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "Absolute.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 /**
    3  * papaya CMS
    4  *
    5  * @copyright 2000-2018 by papayaCMS project - All rights reserved.
    6  * @link http://www.papaya-cms.com/
    7  * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU General Public License, version 2
    8  *
    9  *  You can redistribute and/or modify this script under the terms of the GNU General Public
   10  *  License (GPL) version 2, provided that the copyright and license notes, including these
   11  *  lines, remain unmodified. papaya is distributed in the hope that it will be useful, but
   12  *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
   13  *  FOR A PARTICULAR PURPOSE.
   14  */
   15 namespace Papaya\URL\Transformer;
   16 
   17 use Papaya\URL;
   18 
   19 /**
   20  * Papaya URL Transformer, calculates new absolute url from an absolute url and a relative url
   21  *
   22  * @package Papaya-Library
   23  * @subpackage URL
   24  */
   25 class Absolute {
   26   /**
   27    * Calculates an absolute url from a url and a (possibly relative) path
   28    *
   29    * @param URL|string $currentURL current url
   30    * @param string $target url to transform
   31    *
   32    * @return string
   33    */
   34   public static function transform($currentURL, $target) {
   35     $result = NULL;
   36     if (\is_string($currentURL)) {
   37       $currentURL = new URL($currentURL);
   38     }
   39     if (($url = \parse_url($target)) && isset($url['host'])) {
   40       return $target;
   41     }
   42     if (0 === \strpos($target, '/')) {
   43       $newPath = $target;
   44     } else {
   45       $currentPath = $currentURL->getPath();
   46       // remove any potential trailing file name from the path
   47       $basePath = \substr($currentPath, 0, \strrpos($currentPath, '/'));
   48       $newPath = $basePath.'/'.$target;
   49     }
   50     if (!empty($newPath)) {
   51       $result = $currentURL->getHostURL().self::_calculateRealPath($newPath);
   52     }
   53     return $result;
   54   }
   55 
   56   /**
   57    * This method calculates /../ occurrences and removes // and /./ occurrences from a path
   58    *
   59    * @param string $path
   60    *
   61    * @return string
   62    */
   63   private static function _calculateRealPath($path) {
   64     // in order to keep leading/trailing slashes, remember them
   65     $leadingSlash = (0 === \strpos($path, '/'));
   66     $trailingSlash = ('/' === \substr($path, -1));
   67 
   68     $pathElements = \explode('/', $path);
   69     $outputElements = [];
   70     foreach ($pathElements as $element) {
   71       if ('..' === $element) {
   72         if (\count($outputElements) > 0) {
   73           // going one level up, we drop the last valid folder element
   74           \array_pop($outputElements);
   75         }
   76       } elseif ('.' !== $element && '' !== $element) {
   77         // ignoring same folder and empty elements, adding valid folders to output
   78         $outputElements[] = $element;
   79       }
   80     }
   81 
   82     $result = $leadingSlash ? '/' : '';
   83     $result .= \implode('/', $outputElements);
   84     if ('/' !== $result && $trailingSlash) {
   85       $result .= '/';
   86     }
   87 
   88     return $result;
   89   }
   90 }