xref: /webtrees/app/Services/HtmlService.php (revision fcfa147e10aaa6c7ff580c29bd6e5b88666befc1)
150d6f48cSGreg Roach<?php
23976b470SGreg Roach
350d6f48cSGreg Roach/**
450d6f48cSGreg Roach * webtrees: online genealogy
550d6f48cSGreg Roach * Copyright (C) 2019 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
1550d6f48cSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
1650d6f48cSGreg Roach */
17*fcfa147eSGreg 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;
2550d6f48cSGreg Roach
2650d6f48cSGreg Roach/**
2750d6f48cSGreg Roach * Filter/sanitize HTML
2850d6f48cSGreg Roach */
2950d6f48cSGreg Roachclass HtmlService
3050d6f48cSGreg Roach{
3150d6f48cSGreg Roach    /**
3250d6f48cSGreg Roach     * Take some dirty HTML (as provided by the user), and clean it before
3350d6f48cSGreg Roach     * we save/display it.
3450d6f48cSGreg Roach     *
3550d6f48cSGreg Roach     * @param string $html
3650d6f48cSGreg Roach     *
3750d6f48cSGreg Roach     * @return string
3850d6f48cSGreg Roach     */
3950d6f48cSGreg Roach    public function sanitize(string $html): string
4050d6f48cSGreg Roach    {
4150d6f48cSGreg Roach        $config = HTMLPurifier_Config::createDefault();
4250d6f48cSGreg Roach
43db7ffaddSGreg Roach        $config->set('Cache.DefinitionImpl', null);
44db7ffaddSGreg Roach
4514267679SGreg Roach        $config->set('HTML.TidyLevel', 'none'); // Only XSS cleaning now
4614267679SGreg Roach
4714267679SGreg Roach        $def = $config->getHTMLDefinition(true);
4814267679SGreg Roach
492a80267cSGreg Roach        // Allow image maps.
5014267679SGreg Roach        $def->addAttribute('img', 'usemap', 'CDATA');
5114267679SGreg Roach
522a80267cSGreg Roach        // Allow link targets.
532a80267cSGreg Roach        $def->addAttribute('a', 'target', new HTMLPurifier_AttrDef_Enum(['_blank', '_self', '_target', '_top']));
542a80267cSGreg Roach
5514267679SGreg Roach        $map = $def->addElement('map', 'Block', 'Flow', 'Common', [
5614267679SGreg Roach            'name'  => 'CDATA',
5714267679SGreg Roach            'id'    => 'ID',
5814267679SGreg Roach            'title' => 'CDATA',
5914267679SGreg Roach        ]);
6014267679SGreg Roach
6114267679SGreg Roach        $map->excludes = ['map' => true];
6214267679SGreg Roach
6314267679SGreg Roach        $area = $def->addElement('area', 'Block', 'Empty', 'Common', [
6414267679SGreg Roach            'name'      => 'CDATA',
6514267679SGreg Roach            'id'        => 'ID',
6614267679SGreg Roach            'alt'       => 'Text',
6714267679SGreg Roach            'coords'    => 'CDATA',
6814267679SGreg Roach            'accesskey' => 'Character',
6914267679SGreg Roach            'nohref'    => new HTMLPurifier_AttrDef_Enum(['nohref']),
7014267679SGreg Roach            'href'      => 'URI',
7114267679SGreg Roach            'shape'     => new HTMLPurifier_AttrDef_Enum(['rect', 'circle', 'poly', 'default']),
7214267679SGreg Roach            'tabindex'  => 'Number',
7314267679SGreg Roach        ]);
7414267679SGreg Roach
7514267679SGreg Roach        $area->excludes = ['area' => true];
7614267679SGreg Roach
7750d6f48cSGreg Roach        $purifier = new HTMLPurifier($config);
7850d6f48cSGreg Roach
7950d6f48cSGreg Roach        return $purifier->purify($html);
8050d6f48cSGreg Roach    }
8150d6f48cSGreg Roach}
82