-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathParser.php
196 lines (156 loc) · 5.19 KB
/
Parser.php
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
/**
* Copyright (c) 2016. GDPRProof B.V.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package Yar Parser
* @author Fabio Ros <[email protected]> - <[email protected]>
* @copyright Copyright (c) 2016 Fabio Ros - GDPRProof B.V.
* @license https://opensource.org/licenses/AGPL-3.0 GNU Affero General Public License version 3
*
*/
/**
* Remove multiline comments : \/\*[\s\S]*?\*\/|([^:]|^)\/\/.*$ || break rule md5_4c4b3d4ba5bce7191a5138efa2468679
*/
namespace Frosit\Component\Yar;
use Frosit\Component\Yar\Exception\ParseException;
use Frosit\Component\Yar\Rule\Parser as RuleParser;
/**
* Class Parser
* @package Frosit\Yarparser
*/
class Parser
{
public $originalContent;
public $parseByRegex = false;
/**
* Cleans multiline comments at start with regex
* @note, may break some strings
* @var bool
*/
public $cleanMultilineCommentsAtStart = false;
/**
*
* @var bool
*/
public $cleanSingleLineCommentsAtStart = true;
public function __construct()
{
}
/**
* @param $content
* @param $exceptionOnInvalidType
* @return array
*/
public function parse($content, $objectSupport, $exceptionOnInvalidType)
{
if (!preg_match('//u', $content)) {
throw new ParseException('The YAML value does not appear to be valid UTF-8.');
}
// set original content so we can refer back
$this->originalContent = $content;
/**
* @note when experiencing issue, change the cleancontent function
*/
$content = $this->cleanContent($content);
// @todo maybe fix encodings
// $content = $this->fixEcondings or something
$rules = array();
/**
* Strip rules
* We want each rule [head>[name] : [tags]]{ <body> }
* This should also detect inline rules!
*/
if ($ruleWithContent = Regexer::matchRuleNames($content) === 1) {
$rulesWithContent[] = $content;
} elseif ($rulesWithContent = Regexer::MatchRulesWithContent($content)) {
} else {
$rulesWithContent = false;
}
if ($rulesWithContent) {
foreach ($rulesWithContent as $ruleWithContent) { // iterate each rule
$parser = new RuleParser();
$rule = $parser->parse($ruleWithContent);
if (!$objectSupport) {
$rule = $rule->asArray();
}
$rules[] = $rule;
}
}
return $rules;
}
/**
* Cleans out unnecessary content
* @todo begin carefull
* @param $content
* @return mixed
*/
public function cleanContent($content)
{
/**
* Stripts out multiline comments
* @todo should respect quoted string
*/
if ($this->cleanMultilineCommentsAtStart) {
$content = preg_replace('!/\*.*?\*/!s', '', $content);
}
// $content = Regexer::removeBlockComments($content);
// Strip out empty lines
if ($this->cleanSingleLineCommentsAtStart) {
$content = preg_replace('/\n\s*\n/', "\n", $content);
}
return $content;
}
/**
* Resolves includes before parsing
* @param $input
* @return mixed
* @internal param $content
* @internal param $path
*/
public static function resolveIncludes($input)
{
$path = realpath($input);
$content = file_get_contents($input);
$re = "/include \"(?P<name>.*.yar)\"/";
// $re = '/^include "(?P<name>.*.yar)"/m';
preg_match_all(
$re,
$content,
$matches,
PREG_SET_ORDER
);
if (count($matches)) { // if more than one match
foreach ($matches as $match) {
$replace = $match['name'];
if (substr($replace, 0, 1) === DIRECTORY_SEPARATOR) {
$path = $replace;
} else {
$parts = explode("/", $path);
array_pop($parts);
array_push($parts, $replace);
$path = implode("/", $parts);
}
if (is_file($path)) {
$matchContent = file_get_contents($path);
$content = str_replace($match[0], $matchContent, $content);
} else {
throw new ParseException('Include could not be found.');
}
}
}
return $content;
}
}