xref: /webtrees/app/Html.php (revision 46c324ac91b5639f7edc143b83a34888a0868fc1)
1cd937586SGreg Roach<?php
2cd937586SGreg Roach/**
3cd937586SGreg Roach * webtrees: online genealogy
46bdf7674SGreg Roach * Copyright (C) 2017 webtrees development team
5cd937586SGreg Roach * This program is free software: you can redistribute it and/or modify
6cd937586SGreg Roach * it under the terms of the GNU General Public License as published by
7cd937586SGreg Roach * the Free Software Foundation, either version 3 of the License, or
8cd937586SGreg Roach * (at your option) any later version.
9cd937586SGreg Roach * This program is distributed in the hope that it will be useful,
10cd937586SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
11cd937586SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12cd937586SGreg Roach * GNU General Public License for more details.
13cd937586SGreg Roach * You should have received a copy of the GNU General Public License
14cd937586SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
15cd937586SGreg Roach */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees;
17cd937586SGreg Roach
18cd937586SGreg Roach/**
19cd937586SGreg Roach * Class Html - Add HTML markup to elements consistently.
20cd937586SGreg Roach */
21cd937586SGreg Roachclass Html {
22cd937586SGreg Roach	/**
2315d603e7SGreg Roach	 * Escape a string for inclusion within HTML.
2415d603e7SGreg Roach	 *
2515d603e7SGreg Roach	 * @param $string
2615d603e7SGreg Roach	 *
2715d603e7SGreg Roach	 * @return string
2815d603e7SGreg Roach	 */
29cc5ab399SGreg Roach	public static function escape($string) {
3005b65cdcSGreg Roach		return htmlspecialchars($string, ENT_QUOTES | ENT_HTML5 | ENT_SUBSTITUTE, 'UTF-8');
3115d603e7SGreg Roach	}
3215d603e7SGreg Roach
3315d603e7SGreg Roach	/**
3415d603e7SGreg Roach	 * Convert an array of HTML attributes to an HTML string.
3515d603e7SGreg Roach	 *
3615d603e7SGreg Roach	 * @param array $attributes
3715d603e7SGreg Roach	 *
3815d603e7SGreg Roach	 * @return string
3915d603e7SGreg Roach	 */
4015d603e7SGreg Roach	public static function attributes(array $attributes) {
4115d603e7SGreg Roach		$html = [];
4215d603e7SGreg Roach		foreach ($attributes as $key => $value) {
4315d603e7SGreg Roach			if (is_string($value) || is_integer($value)) {
4415d603e7SGreg Roach				$html[] = self::escape($key) . '="' . self::escape($value) . '"';
4515d603e7SGreg Roach			} elseif ($value !== false) {
4615d603e7SGreg Roach				$html[] = self::escape($key);
4715d603e7SGreg Roach			}
4815d603e7SGreg Roach		}
4915d603e7SGreg Roach
5015d603e7SGreg Roach		return implode(' ', $html);
5115d603e7SGreg Roach	}
521e582591SGreg Roach
531e582591SGreg Roach	/**
541e582591SGreg Roach	 * Encode a URL.
551e582591SGreg Roach	 *
56c347c1e5SGreg Roach	 * @param string   $path
571e582591SGreg Roach	 * @param string[] $data
581e582591SGreg Roach	 *
591e582591SGreg Roach	 * @return string
601e582591SGreg Roach	 */
61*46c324acSGreg Roach	public static function url($path, array $data) {
62530749aaSGreg Roach		$path = strtr($path, ' ', '%20');
63530749aaSGreg Roach
64*46c324acSGreg Roach		return $path . '?' . http_build_query($data, '', '&', PHP_QUERY_RFC3986);
651e582591SGreg Roach	}
661e582591SGreg Roach
6715d603e7SGreg Roach	/**
68cd937586SGreg Roach	 * Filenames are (almost?) always LTR, even on RTL systems.
69cd937586SGreg Roach	 *
70cd937586SGreg Roach	 * @param string $filename
71cd937586SGreg Roach	 *
72cd937586SGreg Roach	 * @return string
73cd937586SGreg Roach	 */
74cd937586SGreg Roach	public static function filename($filename) {
7515d603e7SGreg Roach		return '<samp class="filename" dir="ltr">' . self::escape($filename) . '</samp>';
76cd937586SGreg Roach	}
77cd937586SGreg Roach}
78