<?php
// declare(strict_types=1);
namespace FoxHabbit\BasisBundle\Tool;
use Pimcore\Log\ApplicationLogger;
class Url
{
/**
* Add a scheme to an url
*
* @param string $url the url
* @param string $scheme the scheme
* @param bool $replace allow to replace an existing scheme
* @return string the url prefixed with the scheme or the original url, if anything goes wrong
*/
public static function addScheme(string $url, string $scheme, bool $replace = false) : string {
// check input
if (0 === strlen($scheme)) {
ApplicationLogger::info('No scheme passed. Returning.');
return $url;
}
if (0 === strlen($url)) {
ApplicationLogger::info('No URL passed. Returning.');
return $url;
}
$existingScheme = parse_url( $url , PHP_URL_SCHEME );
if (false === $existingScheme) {
ApplicationLogger::warn('Malformed URL "'.$url.'"');
return $url;
} else if ('' !== $existingScheme && false === $replace) {
ApplicationLogger::notice('Replacing scheme prohibited in "'.$url.'"');
return $url;
}
if ('' === $existingScheme) {
$url = preg_replace('@^:?/{2}@', '', $url);
return $scheme.'://'.$url;
} else {
$url = preg_replace('@^'.$existingScheme.':/{2}@', $scheme.'://', $url);
return $url;
}
}
/**
* Add http:// to an url if it is missing
*
* @param string $url The url
*
* @return string The url having http:// prefixed
*/
public static function addHttp( $url, $replace = false) {
return self::addScheme($url, 'http', $replace);
}
/**
* Add https:// to an url if it is missing
*
* @param string $url The url
*
* @return string The url having https:// prefixed
*/
public static function addHttps( $url, $replace = false) {
return self::addScheme($url, 'https', $replace);
}
/**
* Remove the http:// prefix from an url
*
* @param string $url The url
*
* @return string The url having http:// removed
*/
public static function removeHttp( $url) {
if( ! strlen( $url)) {
return $url;
}
if( preg_match( '@^https?://(.+)$@', $url, $m)) {
return $m[1];
} else {
return $url;
}
}
public static function isExternalLink( $target, $recursive=true) {
if( ! $target) {
return false;
}
// Some targets like downloads are forced to be external to be opened in a new tab
if( is_object( $target)) {
switch( get_class( $target)) {
case 'Pimcore\Model\Document\Tag\Href': // bis Pimcore 5.5
case 'Pimcore\Model\Document\Tag\Relation': // ab Pimcore 5.6
case 'Pimcore\Model\Document\Editable\Relation': // ab Pimcore 6.8
if( $recursive) {
return self::isExternalLink($target->getElement(), $recursive);
}
break;
case 'Pimcore\Model\Asset\Document':
case 'Pimcore\Model\Asset\Image':
// Diese Downloads in neuem Fenster öffnen
return true;
break;
}
}
$href = self::getHref( $target, $recursive);
$href_host = parse_url($href, PHP_URL_HOST);
$server_host = $_SERVER['HTTP_HOST'];
if( ! $href_host) {
return false;
}
return $href_host !== $server_host;
}
public static function getTarget( $in, $recursive=true) {
return ( self::isExternalLink($in) ) ? '_blank':'_self';
}
public static function isPublished( $target, $recursive=true) {
if( is_object( $target)) {
switch( get_class( $target)) {
case 'Pimcore\Model\Document\Tag\Href': // bis Pimcore 5.5
case 'Pimcore\Model\Document\Tag\Relation': // ab Pimcore 5.6
case 'Pimcore\Model\Document\Editable\Relation': // ab Pimcore 6.8
if( $recursive) {
return self::isPublished($target->getElement(), $recursive);
} else {
return $target->isPublished();
}
break;
case 'Pimcore\Model\Document\Link':
if( $target->isPublished()) {
if( $recursive) {
switch( $target->getLinktype()) {
case 'direct':
// Link enthält eine URL
return true;
break;
case 'internal':
return self::isPublished($target->getObject(), $recursive);
break;
}
} else {
return true;
}
}
break;
default:
if( $target instanceof \Pimcore\Model\Document) {
return $target->isPublished();
}
if( $target instanceof \Pimcore\Model\Asset) {
return true;
}
exit( 'Website\Tool\Url::isVisible(): unhandled target type "'.get_class( $target).'"');
}
} else if( is_string( $target)) {
return true;
}
return false;
}
public static function getHref( $target, $recursive=true) {
if( is_object( $target)) {
switch( get_class( $target)) {
case 'Pimcore\Model\Document\Tag\Href': // bis Pimcore 5.5
case 'Pimcore\Model\Document\Tag\Relation': // ab Pimcore 5.6
case 'Pimcore\Model\Document\Editable\Relation': // ab Pimcore 6.8
if( $recursive) {
return self::getHref($target->getElement(), $recursive);
} else {
return $target->getFullPath();
}
break;
case 'Pimcore\Model\Document\Link':
if( $recursive) {
switch( $target->getLinktype()) {
case 'direct':
// Link enthält eine URL
return $target->getHref();
break;
case 'internal':
return self::getHref( $target->getObject(), $recursive);
break;
}
} else {
return $target->getHref();
}
break;
case 'Pimcore\Model\Document\Page':
case 'Pimcore\Model\Document\Hardlink':
case 'Pimcore\Model\Asset\Image':
case 'Pimcore\Model\Asset\Document':
return $target->getFullPath();
break;
case 'Pimcore\Navigation\Page\Document':
if( $recursive) {
return self::getHref( $target->getDocument(), $recursive);
} else {
return $target->getHref();
}
break;
default:
return 'Website\Tool\Url::getHref(): unhandled target type "'.get_class( $target).'"';
}
}
return $target;
}
}