xref: /plugin/pureldap/vendor/freedsx/ldap/src/FreeDSx/Ldap/LdapUrlTrait.php (revision dad993c57a70866aa1db59c43f043769c2eb7ed0)
10b3fd2d3SAndreas Gohr<?php
2*dad993c5SAndreas Gohr
30b3fd2d3SAndreas Gohr/**
4*dad993c5SAndreas Gohr * This file is part of the FreeDSx LDAP package.
5*dad993c5SAndreas Gohr *
6*dad993c5SAndreas Gohr * (c) Chad Sikorra <Chad.Sikorra@gmail.com>
7*dad993c5SAndreas Gohr *
8*dad993c5SAndreas Gohr * For the full copyright and license information, please view the LICENSE
9*dad993c5SAndreas Gohr * file that was distributed with this source code.
100b3fd2d3SAndreas Gohr */
110b3fd2d3SAndreas Gohr
120b3fd2d3SAndreas Gohrnamespace FreeDSx\Ldap;
130b3fd2d3SAndreas Gohr
14*dad993c5SAndreas Gohruse function array_keys;
15*dad993c5SAndreas Gohruse function array_values;
16*dad993c5SAndreas Gohruse function str_ireplace;
17*dad993c5SAndreas Gohruse function str_replace;
18*dad993c5SAndreas Gohr
190b3fd2d3SAndreas Gohr/**
200b3fd2d3SAndreas Gohr * Some common methods for LDAP URLs and URL Extensions.
210b3fd2d3SAndreas Gohr *
220b3fd2d3SAndreas Gohr * @author Chad Sikorra <Chad.Sikorra@gmail.com>
230b3fd2d3SAndreas Gohr */
240b3fd2d3SAndreas Gohrtrait LdapUrlTrait
250b3fd2d3SAndreas Gohr{
260b3fd2d3SAndreas Gohr    /**
270b3fd2d3SAndreas Gohr     * @var array
280b3fd2d3SAndreas Gohr     */
290b3fd2d3SAndreas Gohr    protected static $escapeMap = [
300b3fd2d3SAndreas Gohr        '%' => '%25',
310b3fd2d3SAndreas Gohr        '?' => '%3f',
320b3fd2d3SAndreas Gohr        ' ' => '%20',
330b3fd2d3SAndreas Gohr        '<' => '%3c',
340b3fd2d3SAndreas Gohr        '>' => '%3e',
350b3fd2d3SAndreas Gohr        '"' => '%22',
360b3fd2d3SAndreas Gohr        '#' => '%23',
370b3fd2d3SAndreas Gohr        '{' => '%7b',
380b3fd2d3SAndreas Gohr        '}' => '%7d',
390b3fd2d3SAndreas Gohr        '|' => '%7c',
400b3fd2d3SAndreas Gohr        '\\' => '%5c',
410b3fd2d3SAndreas Gohr        '^' => '%5e',
420b3fd2d3SAndreas Gohr        '~' => '%7e',
430b3fd2d3SAndreas Gohr        '[' => '%5b',
440b3fd2d3SAndreas Gohr        ']' => '%5d',
450b3fd2d3SAndreas Gohr        '`' => '%60',
460b3fd2d3SAndreas Gohr    ];
470b3fd2d3SAndreas Gohr
480b3fd2d3SAndreas Gohr    /**
490b3fd2d3SAndreas Gohr     * Percent-encode certain values in the URL.
500b3fd2d3SAndreas Gohr     *
51*dad993c5SAndreas Gohr     * @param null|string $value
520b3fd2d3SAndreas Gohr     * @return string
530b3fd2d3SAndreas Gohr     */
540b3fd2d3SAndreas Gohr    protected static function encode(?string $value): string
550b3fd2d3SAndreas Gohr    {
56*dad993c5SAndreas Gohr        return str_replace(
57*dad993c5SAndreas Gohr            array_keys(self::$escapeMap),
58*dad993c5SAndreas Gohr            array_values(self::$escapeMap),
590b3fd2d3SAndreas Gohr            (string) $value
600b3fd2d3SAndreas Gohr        );
610b3fd2d3SAndreas Gohr    }
620b3fd2d3SAndreas Gohr
630b3fd2d3SAndreas Gohr    /**
640b3fd2d3SAndreas Gohr     * Percent-decode values from the URL.
650b3fd2d3SAndreas Gohr     *
660b3fd2d3SAndreas Gohr     * @param string $value
670b3fd2d3SAndreas Gohr     * @return string
680b3fd2d3SAndreas Gohr     */
690b3fd2d3SAndreas Gohr    protected static function decode(string $value): string
700b3fd2d3SAndreas Gohr    {
71*dad993c5SAndreas Gohr        return str_ireplace(
72*dad993c5SAndreas Gohr            array_values(self::$escapeMap),
73*dad993c5SAndreas Gohr            array_keys(self::$escapeMap),
740b3fd2d3SAndreas Gohr            $value
750b3fd2d3SAndreas Gohr        );
760b3fd2d3SAndreas Gohr    }
770b3fd2d3SAndreas Gohr}
78