150d6f48cSGreg Roach<?php 23976b470SGreg Roach 350d6f48cSGreg Roach/** 450d6f48cSGreg Roach * webtrees: online genealogy 5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team 650d6f48cSGreg Roach * This program is free software: you can redistribute it and/or modify 750d6f48cSGreg Roach * it under the terms of the GNU General Public License as published by 850d6f48cSGreg Roach * the Free Software Foundation, either version 3 of the License, or 950d6f48cSGreg Roach * (at your option) any later version. 1050d6f48cSGreg Roach * This program is distributed in the hope that it will be useful, 1150d6f48cSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 1250d6f48cSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1350d6f48cSGreg Roach * GNU General Public License for more details. 1450d6f48cSGreg Roach * You should have received a copy of the GNU General Public License 1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 1650d6f48cSGreg Roach */ 17fcfa147eSGreg Roach 1850d6f48cSGreg Roachdeclare(strict_types=1); 1950d6f48cSGreg Roach 2050d6f48cSGreg Roachnamespace Fisharebest\Webtrees\Services; 2150d6f48cSGreg Roach 2250d6f48cSGreg Roachuse HTMLPurifier; 2314267679SGreg Roachuse HTMLPurifier_AttrDef_Enum; 2450d6f48cSGreg Roachuse HTMLPurifier_Config; 25deed60b3SGreg Roachuse HTMLPurifier_HTMLDefinition; 26deed60b3SGreg Roach 27deed60b3SGreg Roachuse function assert; 2850d6f48cSGreg Roach 2950d6f48cSGreg Roach/** 3050d6f48cSGreg Roach * Filter/sanitize HTML 3150d6f48cSGreg Roach */ 3250d6f48cSGreg Roachclass HtmlService 3350d6f48cSGreg Roach{ 3450d6f48cSGreg Roach /** 3550d6f48cSGreg Roach * Take some dirty HTML (as provided by the user), and clean it before 3650d6f48cSGreg Roach * we save/display it. 3750d6f48cSGreg Roach * 3850d6f48cSGreg Roach * @param string $html 3950d6f48cSGreg Roach * 4050d6f48cSGreg Roach * @return string 4150d6f48cSGreg Roach */ 4250d6f48cSGreg Roach public function sanitize(string $html): string 4350d6f48cSGreg Roach { 4450d6f48cSGreg Roach $config = HTMLPurifier_Config::createDefault(); 4550d6f48cSGreg Roach 46db7ffaddSGreg Roach $config->set('Cache.DefinitionImpl', null); 47db7ffaddSGreg Roach 4814267679SGreg Roach $config->set('HTML.TidyLevel', 'none'); // Only XSS cleaning now 4914267679SGreg Roach 50bb17e984SGreg Roach // Remove the default maximum width/height for images. This enables percentage values. 51bb17e984SGreg Roach $config->set('CSS.MaxImgLength', null); 5214267679SGreg Roach 539fc95305SGreg Roach // Allow id attributes. 549fc95305SGreg Roach $config->set('Attr.EnableID', true); 559fc95305SGreg Roach 56bb17e984SGreg Roach $def = $config->getHTMLDefinition(true); 57deed60b3SGreg Roach assert($def instanceof HTMLPurifier_HTMLDefinition); 5814267679SGreg Roach 592a80267cSGreg Roach // Allow link targets. 602a80267cSGreg Roach $def->addAttribute('a', 'target', new HTMLPurifier_AttrDef_Enum(['_blank', '_self', '_target', '_top'])); 612a80267cSGreg Roach 62bb17e984SGreg Roach // Allow image maps. 63bb17e984SGreg Roach $def->addAttribute('img', 'usemap', 'CDATA'); 64bb17e984SGreg Roach 6514267679SGreg Roach $map = $def->addElement('map', 'Block', 'Flow', 'Common', [ 6614267679SGreg Roach 'name' => 'CDATA', 6714267679SGreg Roach 'id' => 'ID', 6814267679SGreg Roach 'title' => 'CDATA', 6914267679SGreg Roach ]); 7014267679SGreg Roach 7114267679SGreg Roach $map->excludes = ['map' => true]; 7214267679SGreg Roach 7314267679SGreg Roach $area = $def->addElement('area', 'Block', 'Empty', 'Common', [ 7414267679SGreg Roach 'name' => 'CDATA', 7514267679SGreg Roach 'id' => 'ID', 7614267679SGreg Roach 'alt' => 'Text', 7714267679SGreg Roach 'coords' => 'CDATA', 7814267679SGreg Roach 'accesskey' => 'Character', 7914267679SGreg Roach 'nohref' => new HTMLPurifier_AttrDef_Enum(['nohref']), 8014267679SGreg Roach 'href' => 'URI', 8114267679SGreg Roach 'shape' => new HTMLPurifier_AttrDef_Enum(['rect', 'circle', 'poly', 'default']), 8214267679SGreg Roach 'tabindex' => 'Number', 8314267679SGreg Roach ]); 8414267679SGreg Roach 8514267679SGreg Roach $area->excludes = ['area' => true]; 8614267679SGreg Roach 87*9660ebfeSGreg Roach // Allow audio and video 88*9660ebfeSGreg Roach $audio = $def->addElement('audio', 'Block', 'Flow', 'Common', [ 89*9660ebfeSGreg Roach 'controls' => 'Bool#controls', 90*9660ebfeSGreg Roach 'src' => 'URI', 91*9660ebfeSGreg Roach ]); 92*9660ebfeSGreg Roach $audio->excludes = ['audio' => true]; 93*9660ebfeSGreg Roach 94*9660ebfeSGreg Roach $video = $def->addElement('video', 'Block', 'Flow', 'Common', [ 95*9660ebfeSGreg Roach 'controls' => 'Bool#controls', 96*9660ebfeSGreg Roach 'height' => 'Number', 97*9660ebfeSGreg Roach 'poster' => 'URI', 98*9660ebfeSGreg Roach 'src' => 'URI', 99*9660ebfeSGreg Roach 'width' => 'Number', 100*9660ebfeSGreg Roach ]); 101*9660ebfeSGreg Roach $video->excludes = ['video' => true]; 102*9660ebfeSGreg Roach 10350d6f48cSGreg Roach $purifier = new HTMLPurifier($config); 10450d6f48cSGreg Roach 10550d6f48cSGreg Roach return $purifier->purify($html); 10650d6f48cSGreg Roach } 10750d6f48cSGreg Roach} 108