xref: /webtrees/app/Services/HtmlService.php (revision db7ffaddabfad71b71b45384ccfaa4d25299776d)
150d6f48cSGreg Roach<?php
250d6f48cSGreg Roach/**
350d6f48cSGreg Roach * webtrees: online genealogy
450d6f48cSGreg Roach * Copyright (C) 2019 webtrees development team
550d6f48cSGreg Roach * This program is free software: you can redistribute it and/or modify
650d6f48cSGreg Roach * it under the terms of the GNU General Public License as published by
750d6f48cSGreg Roach * the Free Software Foundation, either version 3 of the License, or
850d6f48cSGreg Roach * (at your option) any later version.
950d6f48cSGreg Roach * This program is distributed in the hope that it will be useful,
1050d6f48cSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
1150d6f48cSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1250d6f48cSGreg Roach * GNU General Public License for more details.
1350d6f48cSGreg Roach * You should have received a copy of the GNU General Public License
1450d6f48cSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
1550d6f48cSGreg Roach */
1650d6f48cSGreg Roachdeclare(strict_types=1);
1750d6f48cSGreg Roach
1850d6f48cSGreg Roachnamespace Fisharebest\Webtrees\Services;
1950d6f48cSGreg Roach
2050d6f48cSGreg Roachuse HTMLPurifier;
2114267679SGreg Roachuse HTMLPurifier_AttrDef_Enum;
2250d6f48cSGreg Roachuse HTMLPurifier_Config;
2350d6f48cSGreg Roach
2450d6f48cSGreg Roach/**
2550d6f48cSGreg Roach * Filter/sanitize HTML
2650d6f48cSGreg Roach */
2750d6f48cSGreg Roachclass HtmlService
2850d6f48cSGreg Roach{
2950d6f48cSGreg Roach    /**
3050d6f48cSGreg Roach     * Take some dirty HTML (as provided by the user), and clean it before
3150d6f48cSGreg Roach     * we save/display it.
3250d6f48cSGreg Roach     *
3350d6f48cSGreg Roach     * @param string $html
3450d6f48cSGreg Roach     *
3550d6f48cSGreg Roach     * @return string
3650d6f48cSGreg Roach     */
3750d6f48cSGreg Roach    public function sanitize(string $html): string
3850d6f48cSGreg Roach    {
3950d6f48cSGreg Roach        $config = HTMLPurifier_Config::createDefault();
4050d6f48cSGreg Roach
41*db7ffaddSGreg Roach        $config->set('Cache.DefinitionImpl', null);
42*db7ffaddSGreg Roach
4314267679SGreg Roach        $config->set('HTML.TidyLevel', 'none'); // Only XSS cleaning now
4414267679SGreg Roach
4514267679SGreg Roach        $def = $config->getHTMLDefinition(true);
4614267679SGreg Roach
4714267679SGreg Roach        // Allow image maps
4814267679SGreg Roach        $def->addAttribute('img', 'usemap', 'CDATA');
4914267679SGreg Roach
5014267679SGreg Roach        $map = $def->addElement('map', 'Block', 'Flow', 'Common', [
5114267679SGreg Roach            'name'  => 'CDATA',
5214267679SGreg Roach            'id'    => 'ID',
5314267679SGreg Roach            'title' => 'CDATA',
5414267679SGreg Roach        ]);
5514267679SGreg Roach
5614267679SGreg Roach        $map->excludes = ['map' => true];
5714267679SGreg Roach
5814267679SGreg Roach        $area = $def->addElement('area', 'Block', 'Empty', 'Common', [
5914267679SGreg Roach            'name'      => 'CDATA',
6014267679SGreg Roach            'id'        => 'ID',
6114267679SGreg Roach            'alt'       => 'Text',
6214267679SGreg Roach            'coords'    => 'CDATA',
6314267679SGreg Roach            'accesskey' => 'Character',
6414267679SGreg Roach            'nohref'    => new HTMLPurifier_AttrDef_Enum(['nohref']),
6514267679SGreg Roach            'href'      => 'URI',
6614267679SGreg Roach            'shape'     => new HTMLPurifier_AttrDef_Enum(['rect', 'circle', 'poly', 'default']),
6714267679SGreg Roach            'tabindex'  => 'Number',
6814267679SGreg Roach            'target'    => new HTMLPurifier_AttrDef_Enum(['_blank', '_self', '_target', '_top']),
6914267679SGreg Roach        ]);
7014267679SGreg Roach
7114267679SGreg Roach        $area->excludes = ['area' => true];
7214267679SGreg Roach
7350d6f48cSGreg Roach        $purifier = new HTMLPurifier($config);
7450d6f48cSGreg Roach
7550d6f48cSGreg Roach        return $purifier->purify($html);
7650d6f48cSGreg Roach    }
7750d6f48cSGreg Roach}
78