Completed
Push — master ( 0be3f5...793ac3 )
by Jan-Petter
02:16
created

DirectiveClientTrait::checkPathsCallback()   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 35
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 35
rs 6.7272
cc 7
eloc 13
nc 8
nop 2
1
<?php
2
/**
3
 * vipnytt/RobotsTxtParser
4
 *
5
 * @link https://github.com/VIPnytt/RobotsTxtParser
6
 * @license https://github.com/VIPnytt/RobotsTxtParser/blob/master/LICENSE The MIT License (MIT)
7
 */
8
9
namespace vipnytt\RobotsTxtParser\Client\Directives;
10
11
use DateTime;
12
use DateTimeZone;
13
14
/**
15
 * Trait DirectiveClientTrait
16
 *
17
 * @package vipnytt\RobotsTxtParser\Client\Directives
18
 */
19
trait DirectiveClientTrait
20
{
21
    /**
22
     * Is time between
23
     *
24
     * @param int $timestamp
25
     * @param string $fromTime
26
     * @param string $toTime
27
     * @param string $format
28
     * @return bool
29
     */
30
    private function isBetween($timestamp, $fromTime, $toTime, $format = 'Hi')
31
    {
32
        $dateTime = new DateTime();
33
        $timezone = new DateTimeZone('UTC');
34
        $dtRef = $dateTime->createFromFormat('U', $timestamp, $timezone);
35
        $dtFrom = $dateTime->createFromFormat($format, $fromTime, $timezone);
36
        $dtTo = $dateTime->createFromFormat($format, $toTime, $timezone);
37
        if ($dtFrom > $dtTo) {
38
            $dtTo->modify('+1 day');
39
        }
40
        return (
41
            $dtFrom <= $dtRef &&
42
            $dtRef <= $dtTo
43
        ) || (
44
            $dtFrom <= $dtRef->modify('+1 day') &&
45
            $dtRef <= $dtTo
46
        );
47
    }
48
49
    /**
50
     * Check path rule
51
     *
52
     * @param string $path
53
     * @param string[] $paths
54
     * @return bool
55
     */
56
    private function checkPaths($path, array $paths)
57
    {
58
        foreach ($paths as $rule) {
59
            $escape = [
60
                '?' => '\?',
61
                '.' => '\.',
62
                '*' => '.*',
63
            ];
64
            foreach ($escape as $search => $replace) {
65
                $rule = str_replace($search, $replace, $rule);
66
            }
67
            if ($this->checkPathsCallback($rule, $path)) {
68
                return true;
69
            }
70
        }
71
        return false;
72
    }
73
74
    /**
75
     * Callback for CheckPath
76
     *
77
     * @param string $rule
78
     * @param string $path
79
     * @return bool
80
     */
81
    private function checkPathsCallback($rule, $path)
82
    {
83
        /**
84
         * Warning: preg_match need to be replaced
85
         *
86
         * Bug report
87
         * @link https://github.com/t1gor/Robots.txt-Core-Class/issues/62
88
         *
89
         * An robots.txt parser, where a bug-fix is planned
90
         * @link https://github.com/diggin/Diggin_RobotRules
91
         *
92
         * The solution?
93
         * PHP PEG (parsing expression grammar)
94
         * @link https://github.com/hafriedlander/php-peg
95
         */
96
        try {
97
            if (!preg_match('#' . $rule . '#', $path)) {
98
                // Rule does not match
99
                return false;
100
            } elseif (mb_strpos($rule, '$') === false || // No special parsing required
101
                mb_substr($rule, 0, -1) == $path // Rule does contain an end anchor, and matches
102
            ) {
103
                return true;
104
            } elseif (($wildcardPos = mb_strrpos($rule, '*')) !== false) {
105
                // Rule contains both an end anchor ($) and wildcard (*)
106
                $afterWildcard = mb_substr($rule, $wildcardPos + 1, mb_strlen($rule) - $wildcardPos - 2);
107
                if ($afterWildcard == mb_substr($path, -mb_strlen($afterWildcard))) {
108
                    return true;
109
                }
110
            }
111
        } catch (\Exception $e) {
112
            // An preg_match bug has occurred
113
        }
114
        return false;
115
    }
116
}
117