xref: /dokuwiki/inc/Subscriptions/SubscriberRegexBuilder.php (revision 479c05b1f975e52558d9ff3234097c0c5c405d27)
1*479c05b1SMichael Große<?php
2*479c05b1SMichael Große
3*479c05b1SMichael Große
4*479c05b1SMichael Großenamespace dokuwiki\Subscriptions;
5*479c05b1SMichael Große
6*479c05b1SMichael Große
7*479c05b1SMichael Großeuse Exception;
8*479c05b1SMichael Große
9*479c05b1SMichael Großeclass SubscriberRegexBuilder
10*479c05b1SMichael Große{
11*479c05b1SMichael Große
12*479c05b1SMichael Große    /**
13*479c05b1SMichael Große     * Construct a regular expression for parsing a subscription definition line
14*479c05b1SMichael Große     *
15*479c05b1SMichael Große     * @author Andreas Gohr <andi@splitbrain.org>
16*479c05b1SMichael Große     *
17*479c05b1SMichael Große     * @param string|array $user
18*479c05b1SMichael Große     * @param string|array $style
19*479c05b1SMichael Große     * @param string|array $data
20*479c05b1SMichael Große     * @return string complete regexp including delimiters
21*479c05b1SMichael Große     * @throws Exception when no data is passed
22*479c05b1SMichael Große     */
23*479c05b1SMichael Große    public function buildRegex($user = null, $style = null, $data = null) {
24*479c05b1SMichael Große        // always work with arrays
25*479c05b1SMichael Große        $user = (array) $user;
26*479c05b1SMichael Große        $style = (array) $style;
27*479c05b1SMichael Große        $data = (array) $data;
28*479c05b1SMichael Große
29*479c05b1SMichael Große        // clean
30*479c05b1SMichael Große        $user = array_filter(array_map('trim', $user));
31*479c05b1SMichael Große        $style = array_filter(array_map('trim', $style));
32*479c05b1SMichael Große        $data = array_filter(array_map('trim', $data));
33*479c05b1SMichael Große
34*479c05b1SMichael Große        // user names are encoded
35*479c05b1SMichael Große        $user = array_map('auth_nameencode', $user);
36*479c05b1SMichael Große
37*479c05b1SMichael Große        // quote
38*479c05b1SMichael Große        $user = array_map('preg_quote_cb', $user);
39*479c05b1SMichael Große        $style = array_map('preg_quote_cb', $style);
40*479c05b1SMichael Große        $data = array_map('preg_quote_cb', $data);
41*479c05b1SMichael Große
42*479c05b1SMichael Große        // join
43*479c05b1SMichael Große        $user = join('|', $user);
44*479c05b1SMichael Große        $style = join('|', $style);
45*479c05b1SMichael Große        $data = join('|', $data);
46*479c05b1SMichael Große
47*479c05b1SMichael Große        // any data at all?
48*479c05b1SMichael Große        if($user.$style.$data === '') throw new Exception('no data passed');
49*479c05b1SMichael Große
50*479c05b1SMichael Große        // replace empty values, set which ones are optional
51*479c05b1SMichael Große        $sopt = '';
52*479c05b1SMichael Große        $dopt = '';
53*479c05b1SMichael Große        if($user === '') {
54*479c05b1SMichael Große            $user = '\S+';
55*479c05b1SMichael Große        }
56*479c05b1SMichael Große        if($style === '') {
57*479c05b1SMichael Große            $style = '\S+';
58*479c05b1SMichael Große            $sopt = '?';
59*479c05b1SMichael Große        }
60*479c05b1SMichael Große        if($data === '') {
61*479c05b1SMichael Große            $data = '\S+';
62*479c05b1SMichael Große            $dopt = '?';
63*479c05b1SMichael Große        }
64*479c05b1SMichael Große
65*479c05b1SMichael Große        // assemble
66*479c05b1SMichael Große        return "/^($user)(?:\\s+($style))$sopt(?:\\s+($data))$dopt$/";
67*479c05b1SMichael Große    }
68*479c05b1SMichael Große}
69