castopod/app/Helpers/url_helper.php
Yassine Doghri 5c5c6da4be
refactor: add rector to enforce type declarations, code quality + style and remove dead code
- update CI process to include quality stage (tests + code review)
- add captainhook to install git pre-commit & pre-push hooks
- remove .devcontainer Dockerfile to use project's docker-compose services: all
services can now be started automatically using vscode
- update docs/setup-development.md
2021-05-12 10:48:30 +00:00

105 lines
2.8 KiB
PHP

<?php
/**
* @copyright 2020 Podlibre
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
* @link https://castopod.org/
*/
if (!function_exists('host_url')) {
/**
* Return the host URL to use in views
*
* @return string|false
*/
function host_url()
{
if (isset($_SERVER['HTTP_HOST'])) {
$protocol =
(!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ||
$_SERVER['SERVER_PORT'] == 443
? 'https://'
: 'http://';
return $protocol . $_SERVER['HTTP_HOST'] . '/';
}
return false;
}
}
if (!function_exists('current_season_url')) {
/**
* Return the podcast URL with season number to use in views
*/
function current_season_url(): string
{
$season_query_string = '';
if (isset($_GET['season'])) {
$season_query_string = '?season=' . $_GET['season'];
} elseif (isset($_GET['year'])) {
$season_query_string = '?year=' . $_GET['year'];
}
return current_url() . $season_query_string;
}
}
if (!function_exists('location_url')) {
/**
* Returns URL to display from location info
*/
function location_url(
string $locationName,
?string $locationGeo = null,
?string $locationOsmid = null
): string {
if (!empty($locationOsmid)) {
return 'https://www.openstreetmap.org/' .
['N' => 'node', 'W' => 'way', 'R' => 'relation'][
substr($locationOsmid, 0, 1)
] .
'/' .
substr($locationOsmid, 1);
}
if (!empty($locationGeo)) {
return 'https://www.openstreetmap.org/#map=17/' .
str_replace(',', '/', substr($locationGeo, 4));
}
return 'https://www.openstreetmap.org/search?query=' .
urlencode($locationName);
}
}
//--------------------------------------------------------------------
if (!function_exists('extract_params_from_episode_uri')) {
/**
* Returns podcast name and episode slug from episode string uri
*
* @param URI $episodeUri
*/
function extract_params_from_episode_uri($episodeUri): ?array
{
preg_match(
'~@(?P<podcastName>[a-zA-Z0-9\_]{1,32})\/episodes\/(?P<episodeSlug>[a-zA-Z0-9\-]{1,191})~',
$episodeUri->getPath(),
$matches,
);
if ($matches === []) {
return null;
}
if (
!array_key_exists('podcastName', $matches) ||
!array_key_exists('episodeSlug', $matches)
) {
return null;
}
return [
'podcastName' => $matches['podcastName'],
'episodeSlug' => $matches['episodeSlug'],
];
}
}