1: <?php
2:
3: /*
4: * The MIT License (MIT)
5: *
6: * Copyright (c) 2013 Jonathan Vollebregt (jnvsor@gmail.com), Rokas Šleinius (raveren@gmail.com)
7: *
8: * Permission is hereby granted, free of charge, to any person obtaining a copy of
9: * this software and associated documentation files (the "Software"), to deal in
10: * the Software without restriction, including without limitation the rights to
11: * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
12: * the Software, and to permit persons to whom the Software is furnished to do so,
13: * subject to the following conditions:
14: *
15: * The above copyright notice and this permission notice shall be included in all
16: * copies or substantial portions of the Software.
17: *
18: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
20: * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
21: * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
22: * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23: * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24: */
25:
26: namespace Kint\Parser;
27:
28: use Kint\Object\BasicObject;
29: use Kint\Object\Representation\Representation;
30:
31: class Base64Plugin extends Plugin
32: {
33: /**
34: * The minimum length before a string will be considered for base64 decoding.
35: *
36: * @var int
37: */
38: public static $min_length_hard = 16;
39:
40: /**
41: * The minimum length before the base64 decoding will take precedence.
42: *
43: * @var int
44: */
45: public static $min_length_soft = 50;
46:
47: public function getTypes()
48: {
49: return array('string');
50: }
51:
52: public function getTriggers()
53: {
54: return Parser::TRIGGER_SUCCESS;
55: }
56:
57: public function parse(&$var, BasicObject &$o, $trigger)
58: {
59: if (\strlen($var) < self::$min_length_hard || \strlen($var) % 4) {
60: return;
61: }
62:
63: if (\preg_match('/^[A-Fa-f0-9]+$/', $var)) {
64: return;
65: }
66:
67: if (!\preg_match('/^[A-Za-z0-9+\\/=]+$/', $var)) {
68: return;
69: }
70:
71: /** @var false|string */
72: $data = \base64_decode($var, true);
73:
74: if (false === $data) {
75: return;
76: }
77:
78: $base_obj = new BasicObject();
79: $base_obj->depth = $o->depth + 1;
80: $base_obj->name = 'base64_decode('.$o->name.')';
81:
82: if ($o->access_path) {
83: $base_obj->access_path = 'base64_decode('.$o->access_path.')';
84: }
85:
86: $r = new Representation('Base64');
87: $r->contents = $this->parser->parse($data, $base_obj);
88:
89: if (\strlen($var) > self::$min_length_soft) {
90: $o->addRepresentation($r, 0);
91: } else {
92: $o->addRepresentation($r);
93: }
94: }
95: }
96: