summaryrefslogtreecommitdiff
path: root/platform/www/inc/Subscriptions/SubscriberRegexBuilder.php
blob: 959702aac59670a8345a5c2f167f95c8165fe2db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php

namespace dokuwiki\Subscriptions;

use Exception;

class SubscriberRegexBuilder
{

    /**
     * Construct a regular expression for parsing a subscription definition line
     *
     * @param string|array $user
     * @param string|array $style
     * @param string|array $data
     *
     * @return string complete regexp including delimiters
     * @throws Exception when no data is passed
     * @author Andreas Gohr <andi@splitbrain.org>
     *
     */
    public function buildRegex($user = null, $style = null, $data = null)
    {
        // always work with arrays
        $user = (array)$user;
        $style = (array)$style;
        $data = (array)$data;

        // clean
        $user = array_filter(array_map('trim', $user));
        $style = array_filter(array_map('trim', $style));
        $data = array_filter(array_map('trim', $data));

        // user names are encoded
        $user = array_map('auth_nameencode', $user);

        // quote
        $user = array_map('preg_quote_cb', $user);
        $style = array_map('preg_quote_cb', $style);
        $data = array_map('preg_quote_cb', $data);

        // join
        $user = join('|', $user);
        $style = join('|', $style);
        $data = join('|', $data);

        // any data at all?
        if ($user . $style . $data === '') {
            throw new Exception('no data passed');
        }

        // replace empty values, set which ones are optional
        $sopt = '';
        $dopt = '';
        if ($user === '') {
            $user = '\S+';
        }
        if ($style === '') {
            $style = '\S+';
            $sopt = '?';
        }
        if ($data === '') {
            $data = '\S+';
            $dopt = '?';
        }

        // assemble
        return "/^($user)(?:\\s+($style))$sopt(?:\\s+($data))$dopt$/";
    }
}