GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( b02eb6...eeb8ae )
by Akpé Aurelle Emmanuel Moïse
17s queued 11s
created

HtmlStripHelper::handleAttributes()   A

Complexity

Conditions 6
Paths 8

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 14
nc 8
nop 3
dl 0
loc 21
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
namespace EZAMA;
4
5
abstract class HtmlStripHelper
6
{
7
    const TAGS = 3;
8
    const ATTRIBUTES = 4;
9
    const TAGS_AND_ATTRIBUTES = 1;
10
    const TAGS_WITH_ATTRIBUTES = 2;
11
12
    protected $is_php = false;
13
    protected $is_html = false;
14
    protected $allowedTags = [];
15
    protected $allowedAttributes = [];
16
    protected $html = '';
17
    protected $doctype;
18
    protected $body;
19
    protected $head;
20
    protected $html_tag;
21
    protected static $events_attributes = [
22
    'onabort' => 1,
23
    'onafterprint' => 1,
24
    'onbeforeprint' => 1,
25
    'onbeforeunload' => 1,
26
    'onblur' => 1,
27
    'oncanplay' => 1,
28
    'oncanplaythrough' => 1,
29
    'onchange' => 1,
30
    'onclick' => 1,
31
    'oncontextmenu' => 1,
32
    'oncopy' => 1,
33
    'oncuechange' => 1,
34
    'oncut' => 1,
35
    'ondblclick' => 1,
36
    'ondrag' => 1,
37
    'ondragend' => 1,
38
    'ondragenter' => 1,
39
    'ondragleave' => 1,
40
    'ondragover' => 1,
41
    'ondragstart' => 1,
42
    'ondrop' => 1,
43
    'ondurationchange' => 1,
44
    'onemptied' => 1,
45
    'onended' => 1,
46
    'onerror' => 1,
47
    'onfocus' => 1,
48
    'onhashchange' => 1,
49
    'oninput' => 1,
50
    'oninvalid' => 1,
51
    'onkeydown' => 1,
52
    'onkeypress' => 1,
53
    'onkeyup' => 1,
54
    'onload' => 1,
55
    'onloadeddata' => 1,
56
    'onloadedmetadata' => 1,
57
    'onloadstart' => 1,
58
    'onmousedown' => 1,
59
    'onmousemove' => 1,
60
    'onmouseout' => 1,
61
    'onmouseover' => 1,
62
    'onmouseup' => 1,
63
    'onmousewheel' => 1,
64
    'onoffline' => 1,
65
    'ononline' => 1,
66
    'onpageshow' => 1,
67
    'onpaste' => 1,
68
    'onpause' => 1,
69
    'onplay' => 1,
70
    'onplaying' => 1,
71
    'onprogress' => 1,
72
    'onratechange' => 1,
73
    'onreset' => 1,
74
    'onresize' => 1,
75
    'onscroll' => 1,
76
    'onsearch' => 1,
77
    'onseeked' => 1,
78
    'onseeking' => 1,
79
    'onselect' => 1,
80
    'onstalled' => 1,
81
    'onsubmit' => 1,
82
    'onsuspend' => 1,
83
    'ontimeupdate' => 1,
84
    'ontoggle' => 1,
85
    'onunload' => 1,
86
    'onvolumechange' => 1,
87
    'onwaiting' => 1,
88
    'onwheel' => 1,
89
    ];
90
91
    protected static $attributes = [
92
    'accept' => 1,
93
    'accesskey' => 1,
94
    'action' => 1,
95
    'alt' => 1,
96
    'async' => 1,
97
    'autocomplete' => 1,
98
    'autofocus' => 1,
99
    'autoplay' => 1,
100
    'charset' => 1,
101
    'checked' => 1,
102
    'cite' => 1,
103
    'class' => 1,
104
    'cols' => 1,
105
    'colspan' => 1,
106
    'content' => 1,
107
    'contenteditable' => 1,
108
    'controls' => 1,
109
    'coords' => 1,
110
    'data' => 1,
111
    'datetime' => 1,
112
    'default' => 1,
113
    'defer' => 1,
114
    'dir' => 1,
115
    'dirname' => 1,
116
    'disabled' => 1,
117
    'download' => 1,
118
    'draggable' => 1,
119
    'dropzone' => 1,
120
    'enctype' => 1,
121
    'for' => 1,
122
    'form' => 1,
123
    'formaction' => 1,
124
    'headers' => 1,
125
    'height' => 1,
126
    'hidden' => 1,
127
    'high' => 1,
128
    'href' => 1,
129
    'hreflang' => 1,
130
    'http' => 1,
131
    'id' => 1,
132
    'ismap' => 1,
133
    'kind' => 1,
134
    'label' => 1,
135
    'lang' => 1,
136
    'list' => 1,
137
    'loop' => 1,
138
    'low' => 1,
139
    'max' => 1,
140
    'maxlength' => 1,
141
    'media' => 1,
142
    'method' => 1,
143
    'min' => 1,
144
    'multiple' => 1,
145
    'muted' => 1,
146
    'name' => 1,
147
    'novalidate' => 1,
148
    'onabort' => 1,
149
    'onafterprint' => 1,
150
    'onbeforeprint' => 1,
151
    'onbeforeunload' => 1,
152
    'onblur' => 1,
153
    'oncanplay' => 1,
154
    'oncanplaythrough' => 1,
155
    'onchange' => 1,
156
    'onclick' => 1,
157
    'oncontextmenu' => 1,
158
    'oncopy' => 1,
159
    'oncuechange' => 1,
160
    'oncut' => 1,
161
    'ondblclick' => 1,
162
    'ondrag' => 1,
163
    'ondragend' => 1,
164
    'ondragenter' => 1,
165
    'ondragleave' => 1,
166
    'ondragover' => 1,
167
    'ondragstart' => 1,
168
    'ondrop' => 1,
169
    'ondurationchange' => 1,
170
    'onemptied' => 1,
171
    'onended' => 1,
172
    'onerror' => 1,
173
    'onfocus' => 1,
174
    'onhashchange' => 1,
175
    'oninput' => 1,
176
    'oninvalid' => 1,
177
    'onkeydown' => 1,
178
    'onkeypress' => 1,
179
    'onkeyup' => 1,
180
    'onload' => 1,
181
    'onloadeddata' => 1,
182
    'onloadedmetadata' => 1,
183
    'onloadstart' => 1,
184
    'onmousedown' => 1,
185
    'onmousemove' => 1,
186
    'onmouseout' => 1,
187
    'onmouseover' => 1,
188
    'onmouseup' => 1,
189
    'onmousewheel' => 1,
190
    'onoffline' => 1,
191
    'ononline' => 1,
192
    'onpageshow' => 1,
193
    'onpaste' => 1,
194
    'onpause' => 1,
195
    'onplay' => 1,
196
    'onplaying' => 1,
197
    'onprogress' => 1,
198
    'onratechange' => 1,
199
    'onreset' => 1,
200
    'onresize' => 1,
201
    'onscroll' => 1,
202
    'onsearch' => 1,
203
    'onseeked' => 1,
204
    'onseeking' => 1,
205
    'onselect' => 1,
206
    'onstalled' => 1,
207
    'onsubmit' => 1,
208
    'onsuspend' => 1,
209
    'ontimeupdate' => 1,
210
    'ontoggle' => 1,
211
    'onunload' => 1,
212
    'onvolumechange' => 1,
213
    'onwaiting' => 1,
214
    'onwheel' => 1,
215
    'open' => 1,
216
    'optimum' => 1,
217
    'pattern' => 1,
218
    'placeholder' => 1,
219
    'poster' => 1,
220
    'preload' => 1,
221
    'readonly' => 1,
222
    'rel' => 1,
223
    'required' => 1,
224
    'reversed' => 1,
225
    'rows' => 1,
226
    'rowspan' => 1,
227
    'sandbox' => 1,
228
    'scope' => 1,
229
    'selected' => 1,
230
    'shape' => 1,
231
    'size' => 1,
232
    'sizes' => 1,
233
    'span' => 1,
234
    'spellcheck' => 1,
235
    'src' => 1,
236
    'srcdoc' => 1,
237
    'srclang' => 1,
238
    'source' => 1,
239
    'start' => 1,
240
    'step' => 1,
241
    'style' => 1,
242
    'tabindex' => 1,
243
    'target' => 1,
244
    'title' => 1,
245
    'translate' => 1,
246
    'type' => 1,
247
    'usemap' => 1,
248
    'value' => 1,
249
    'width' => 1,
250
    'wrap' => 1,
251
];
252
    protected static $special_tags = ['<doctypetag>' => '<!doctype>', '<htmltag>' => '<html>', '<headtag>' => '<head>', '<htmltag>' => '<html>', '<bodytag>' => '<body>'];
253
    protected static $tags = [
254
    '<php>' => 1,
255
    '<!-- -->' => 1,
256
    '<doctypetag>' => 1,
257
    '<a>' => 1,
258
    '<abbr>' => 1,
259
    '<acronym>' => 1,
260
    '<address>' => 1,
261
    '<applet>' => 1,
262
    '<embed>' => 1,
263
    '<object>' => 1,
264
    '<area>' => 1,
265
    '<article>' => 1,
266
    '<aside>' => 1,
267
    '<audio>' => 1,
268
    '<b>' => 1,
269
    '<base>' => 1,
270
    '<basefont>' => 1,
271
    '<bdi>' => 1,
272
    '<bdo>' => 1,
273
    '<big>' => 1,
274
    '<blockquote>' => 1,
275
    '<bodytag>' => 1,
276
    '<br>' => 1,
277
    '<button>' => 1,
278
    '<canvas>' => 1,
279
    '<caption>' => 1,
280
    '<center>' => 1,
281
    '<cite>' => 1,
282
    '<code>' => 1,
283
    '<col>' => 1,
284
    '<colgroup>' => 1,
285
    '<data>' => 1,
286
    '<datalist>' => 1,
287
    '<dd>' => 1,
288
    '<del>' => 1,
289
    '<details>' => 1,
290
    '<dfn>' => 1,
291
    '<dialog>' => 1,
292
    '<dir>' => 1,
293
    '<ul>' => 1,
294
    '<div>' => 1,
295
    '<dl>' => 1,
296
    '<dt>' => 1,
297
    '<em>' => 1,
298
    '<fieldset>' => 1,
299
    '<figcaption>' => 1,
300
    '<figure>' => 1,
301
    '<font>' => 1,
302
    '<footer>' => 1,
303
    '<form>' => 1,
304
    '<frame>' => 1,
305
    '<frameset>' => 1,
306
    '<h1>' => 1,
307
    '<h2>' => 1,
308
    '<h3>' => 1,
309
    '<h5>' => 1,
310
    '<h6>' => 1,
311
    '<hn>' => 1,
312
    '<headtag>' => 1,
313
    '<header>' => 1,
314
    '<hr>' => 1,
315
    '<htmltag>' => 1,
316
    '<i>' => 1,
317
    '<iframe>' => 1,
318
    '<img>' => 1,
319
    '<input>' => 1,
320
    '<ins>' => 1,
321
    '<kbd>' => 1,
322
    '<label>' => 1,
323
    '<legend>' => 1,
324
    '<li>' => 1,
325
    '<link>' => 1,
326
    '<main>' => 1,
327
    '<map>' => 1,
328
    '<mark>' => 1,
329
    '<meta>' => 1,
330
    '<meter>' => 1,
331
    '<nav>' => 1,
332
    '<noframes>' => 1,
333
    '<noscript>' => 1,
334
    '<ol>' => 1,
335
    '<optgroup>' => 1,
336
    '<option>' => 1,
337
    '<output>' => 1,
338
    '<p>' => 1,
339
    '<param>' => 1,
340
    '<picture>' => 1,
341
    '<pre>' => 1,
342
    '<progress>' => 1,
343
    '<q>' => 1,
344
    '<rp>' => 1,
345
    '<rt>' => 1,
346
    '<ruby>' => 1,
347
    '<s>' => 1,
348
    '<samp>' => 1,
349
    '<script>' => 1,
350
    '<section>' => 1,
351
    '<select>' => 1,
352
    '<small>' => 1,
353
    '<source>' => 1,
354
    '<span>' => 1,
355
    '<strike>' => 1,
356
    '<strong>' => 1,
357
    '<style>' => 1,
358
    '<sub>' => 1,
359
    '<summary>' => 1,
360
    '<sup>' => 1,
361
    '<svg>' => 1,
362
    '<table>' => 1,
363
    '<tbody>' => 1,
364
    '<td>' => 1,
365
    '<template>' => 1,
366
    '<textarea>' => 1,
367
    '<tfoot>' => 1,
368
    '<th>' => 1,
369
    '<thead>' => 1,
370
    '<time>' => 1,
371
    '<title>' => 1,
372
    '<tr>' => 1,
373
    '<track>' => 1,
374
    '<tt>' => 1,
375
    '<u>' => 1,
376
    '<var>' => 1,
377
    '<video>' => 1,
378
    '<wbr>' => 1,
379
];
380
381
    protected function loadHTML($html)
382
    {
383
        if (!strlen($html)) {
384
            throw new \InvalidArgumentException('Empty string given');
385
        }
386
        $xml = new \DOMDocument();
387
        //Suppress warnings: proper error handling is beyond scope of example
388
        libxml_use_internal_errors(true);
389
390
        $true = $xml->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
391
        if ($true) {
392
            $this->html = $xml;
393
        }
394
    }
395
396
    protected function handleTags($notAllowedTags, $callback, $callback1)
397
    {
398
        if (!is_array($notAllowedTags)) {
399
            return false;
400
        }
401
        if (count($notAllowedTags) !== 2) {
402
            return false;
403
        }
404
        $notAllowedTags = array_values($notAllowedTags);
405
        $keep = (bool)$notAllowedTags[1];
406
        $notAllowedTags = $notAllowedTags[0];
407
        if (is_string($notAllowedTags)) {
408
            $notAllowedTags = explode(',', $notAllowedTags);
409
        }
410
        if (is_array($notAllowedTags)) {
411
            self::checkSpecialTags($notAllowedTags);
412
            $notAllowedTags = array_filter(array_map($callback, $notAllowedTags), $callback1);
413
            $this->allowedTags = !$keep ? array_fill_keys($notAllowedTags, 1) : array_diff_key(self::$tags, array_flip($notAllowedTags));
414
        } else {
415
            return false;
416
        }
417
418
        return true;
419
    }
420
421
    protected static function checkSpecialTags(&$notAllowedTags)
422
    {
423
        foreach (self::$special_tags as $fakeTag => $trueTag) {
424
            if (false !== $key = array_search($trueTag, $notAllowedTags, true)) {
425
                $notAllowedTags[$key] = $fakeTag;
426
            }
427
        }
428
    }
429
430
    protected function handleAttributes($notAllowedAttributes, $callback, $callback2)
431
    {
432
        if (!is_array($notAllowedAttributes)) {
433
            return false;
434
        }
435
        if (count($notAllowedAttributes) !== 2) {
436
            return false;
437
        }
438
        $keep = (bool)$notAllowedAttributes[1];
439
        $notAllowedAttributes = $notAllowedAttributes[0];
440
        if (is_string($notAllowedAttributes)) {
441
            $notAllowedAttributes = explode(',', $notAllowedAttributes);
442
        }
443
        if (is_array($notAllowedAttributes)) {
444
            $notAllowedAttributes = array_filter(array_map($callback, $notAllowedAttributes), $callback2);
445
            $this->allowedAttributes = !$keep ? array_fill_keys($notAllowedAttributes, 1) : array_diff_key(self::$attributes, array_flip($notAllowedAttributes));
446
        } else {
447
            return false;
448
        }
449
450
        return true;
451
    }
452
453
    protected static function handlePhp($is_php, $domDoc, &$allowed_tags)
454
    {
455
        $result = $domDoc->saveHTML();
456
        self::handleMainHtmlTags($result, $allowed_tags);
457
458
        return substr(($is_php && isset($allowed_tags['<php>'])) ?
459
        str_replace(['<php>', '</php>'], ['<?php ', '?>'], $result) : $result, stripos($result, '<div>') + 5, -7);
460
    }
461
462
    protected static function handleMainHtmlTags(&$result, &$allowed_tags)
463
    {
464
        $result = str_replace(
465
            ['<doctypetag', '</doctypetag>', '<headtag', '</headtag', '<htmltag', '</htmltag', '<bodytag', '</bodytag'],
466
            ['<!doctype', '', '<head', '</head', '<html', '</html', '<body', '</body'],
467
            $result
468
                        );
469
        if (!isset($allowed_tags['<doctypetag>'])) {
470
            $doctypeOffset = stripos($result, '<!doctype');
471
            $result = str_replace(substr($result, $doctypeOffset, strpos($result, '>', $doctypeOffset) + 1 - $doctypeOffset), '', $result);
472
        }
473
    }
474
475
    protected static function handleComments($domDoc, &$allowed_tags)
476
    {
477
        if (!isset($allowed_tags['<!-- -->'])) {
478
            $xpath = new \DOMXPath($domDoc);
479
            $DomComments = $xpath->query('//comment()');
480
            foreach ($DomComments as $DomComment) {
481
                $DomComment->parentNode->removeChild($DomComment);
482
            }
483
        }
484
    }
485
486
    protected static function stripAttributes($tag, &$allowed_attrs, $type = 1)
487
    {
488
        if ($tag instanceof \DOMElement) {
489
            if ($type === 2) {
490
                self:: stripAttributesTypeTwo($tag, $allowed_attrs);
491
            } else {
492
                self::stripAttributesTypeOne($tag, $allowed_attrs);
493
            }
494
        }
495
    }
496
497
    protected static function stripAttributesTypeOne($tag, &$allowed_attrs)
498
    {
499
        foreach (iterator_to_array($tag->attributes) as $attr) {
500
            if (!isset($allowed_attrs[$attr->nodeName])) {
501
                $tag->removeAttribute($attr->nodeName);
502
            }
503
        }
504
    }
505
506
    protected static function stripAttributesTypeTwo($tag, &$allowed_attrs)
507
    {
508
        foreach (iterator_to_array($tag->attributes) as $attr) {
509
            if (!isset($allowed_attrs[$attr->nodeName])) {
510
                if ($tag->parentNode) {
511
                    $tag->parentNode->removeChild($tag);
512
                }
513
            }
514
        }
515
    }
516
}
517