<?php
/**
* SeekQuarry/Yioop --
* Open Source Pure PHP Search Engine, Crawler, and Indexer
*
* Copyright (C) 2009 - 2026 Chris Pollett chris@pollett.org
*
* LICENSE:
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* END LICENSE
*
* @author Chris Pollett chris@pollett.org
* @license https://www.gnu.org/licenses/ GPL3
* @link https://www.seekquarry.com/
* @copyright 2009 - 2026
* @filesource
*/
namespace seekquarry\yioop\views\helpers;
use seekquarry\yioop\configs as C;
/**
* Resolves the effective logo or favicon URL for the page,
* preferring a per-domain appearance override supplied in the
* view $data over the global profile constant. A custom-route
* domain's appearance overlay (see Controller::applyDomainAppearance)
* stores already-complete resource URLs in $data, so when one is
* present it is used as-is; otherwise the global constant is used,
* with the site's short base url prepended so the relative
* resources path resolves.
*
* @author Chris Pollett
*/
class LogoHelper extends Helper
{
/**
* Returns the effective URL for a logo or favicon field,
* using the per-domain override from $data when present
* (already a complete resource URL) and otherwise the global
* value the caller already resolved for that field.
*
* @param array $data view data possibly carrying a per-domain
* appearance override under $field
* @param string $field appearance field name, one of
* LOGO_SMALL, LOGO_MEDIUM, LOGO_LARGE, FAVICON
* @param string $global_value value to use when there is no
* per-domain override; the caller resolves this exactly as
* it did before (some sites prefix the short base url, some
* do not), so the global look is unchanged
* @return string URL to use as the image source
*/
public function render($data, $field, $global_value)
{
$url = (!empty($data[$field])) ? $data[$field] : $global_value;
return $this->absoluteResourceUrl($url);
}
/**
* Makes a logo or favicon URL resolve from the site root rather
* than the current page path. A bare value such as
* "wd/resources/name.png" or "?c=resource&a=get&n=name.png" is
* relative, so on a deep path like /group/4/Main the browser
* would look under /group/4/ and miss the image; prepending the
* site's short base url (a leading slash on a root install)
* anchors it at the root. Values that are already absolute -- an
* empty value, one beginning with a slash, a protocol-relative
* "//host" url, or one carrying a scheme -- are returned
* unchanged so an already-resolved or off-site url is never
* double-prefixed.
*
* @param string $url logo or favicon url to normalize
* @return string url anchored at the site root when it was
* relative, otherwise the url unchanged
*/
private function absoluteResourceUrl($url)
{
if ($url === '' || $url[0] === '/'
|| str_starts_with($url, '//')
|| preg_match('/^[a-zA-Z][a-zA-Z0-9+.-]*:/', $url)) {
return $url;
}
return C\SHORT_BASE_URL . $url;
}
}