Passed
Pull Request — master (#1375)
by Richard
08:11
created

Smarty3ScannerOutput::outputWrapUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 13
rs 9.9666
1
<?php
2
/*
3
 * You may not change or alter any portion of this comment or credits
4
 * of supporting developers from this source code or any supporting source code
5
 * which is considered copyrighted (c) material of the original comment or credit authors.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
namespace Xoops\Upgrade;
13
14
use ArrayObject;
15
16
/**
17
 * XOOPS Upgrade Smarty3ScannerOutput
18
 *
19
 * Used to report Smarty3 issues found in scan
20
 *
21
 * @category  Xoops\Upgrade
22
 * @package   Xoops
23
 * @author    Richard Griffith <[email protected]>
24
 * @copyright 2023 XOOPS Project (https://xoops.org)
25
 * @license   GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
26
 * @link      https://xoops.org
27
 */
28
class Smarty3ScannerOutput extends ScannerOutput
29
{
30
    /**
31
     * @var string $content accumulated output
32
     */
33
    protected $content = '';
34
35
    /**
36
     * @var ArrayObject $counts
37
     */
38
    protected $counts;
39
40
    /**
41
     * Initialize
42
     */
43
    public function __construct()
44
    {
45
        $this->content = '';
46
        $this->counts = new ArrayObject(array());
47
    }
48
49
    /**
50
     * add to count of occurrences of items by $key
51
     *
52
     * Keys used are:
53
     *  The standard rules:
54
     *  'foreachq'
55
     *  'includeq'
56
     *  'noquotes'
57
     *  'varname'
58
     *
59
     *  Also, these attributes
60
     *  'checked' - a running count of files checked
61
     *  'notwritable' - count of files with that could be fixed if permissions were corrected
62
     *
63
     * @param string $key
64
     *
65
     * @return void
66
     */
67
    public function addToCount($key)
68
    {
69
        $count = 0;
70
        if ($this->counts->offsetExists($key)) {
71
            $count = $this->counts->offsetGet($key);
72
        }
73
        $this->counts->offsetSet($key, ++$count);
74
    }
75
76
    /**
77
     * get count of occurrences of items by $key
78
     *
79
     * Keys used are:
80
     *  The standard rules:
81
     *  'foreachq'
82
     *  'includeq'
83
     *  'noquotes'
84
     *  'varname'
85
     *
86
     *  Also, these attributes
87
     *  'checked' - a running count of files checked
88
     *  'notwritable' - count of files with that could be fixed if permissions were corrected
89
     *
90
     * @param string $key
91
     *
92
     * @return int
93
     */
94
    protected function getCount($key)
95
    {
96
        $count = 0;
97
        if ($this->counts->offsetExists($key)) {
98
            $count = $this->counts->offsetGet($key);
99
        }
100
        return $count;
101
    }
102
103
    /**
104
     * Return recorded output
105
     *
106
     * @returns string
107
     */
108
    public function outputFetch()
109
    {
110
        return $this->content;
111
    }
112
113
    /**
114
     * Add item to report
115
     *
116
     * @param string $item
117
     *
118
     * @return void
119
     */
120
    public function outputAppend($item)
121
    {
122
        $this->content .= $item . "\n";
123
    }
124
125
    /**
126
     * Called to
127
     */
128
    public function outputStart()
129
    {
130
        $this->outputAppend('<h2>' . _XOOPS_SMARTY3_SCANNER_RESULTS . '</h2>');
131
        $this->outputAppend('<table class="table"><tr><th>'
132
            . _XOOPS_SMARTY3_SCANNER_RULE . '</th><th>'
133
            . _XOOPS_SMARTY3_SCANNER_MATCH . '</th><th>'
134
            . _XOOPS_SMARTY3_SCANNER_FILE . '</th></tr>');
135
    }
136
137
    public function outputWrapUp()
138
    {
139
        $this->outputAppend('</table>');
140
141
        // build summary table
142
        $this->outputAppend('<table class="table"><tr><th>' . 'Scan Summary' . '</th><th></th></tr>');
143
        $this->outputAppend('<tr><td>' . 'Files Checked' . '</td><td>' . (string) $this->getCount('checked') . '</td></tr>');
144
        $this->outputAppend('<tr class="warning"><td>' . 'Need file permission to fix' . '</td><td>' . (string) $this->getCount('notwritable') . '</td></tr>');
145
        $this->outputAppend('<tr class="danger"><td>' . 'Need manual review to fix' . '</td><td>' . (string) $this->getCount('varname') . '</td></tr>');
146
        $this->outputAppend('<tr><td>' . 'Using includeq/foreachq'     . '</td><td>' . (string) ($this->getCount('includeq') + $this->getCount('foreachq')) . '</td></tr>');
147
        $this->outputAppend('<tr><td>' . 'Missing Quotes'     . '</td><td>' . (string) ($this->getCount('noquotes')) . '</td></tr>');
148
        $this->outputAppend('<tr><td></td><td></td></tr>');
149
        $this->outputAppend('</table>');
150
    }
151
152
    /**
153
     * @param ArrayObject $args should contain these keys: rule, file, match, writable
154
     */
155
    public function outputIssue(ArrayObject $args)
156
    {
157
        $rule = $args['rule'];
158
        $file = $args['file'];
159
        $match = $args['match'];
160
        $writable = $args['writable'];
161
162
        $this->addToCount($rule);
163
164
        if (!$writable) {
165
            $message = _XOOPS_SMARTY3_SCANNER_NOT_WRITABLE;
166
            $this->addToCount('notwritable');
167
            $this->outputAppend("<tr class='warning'>"
168
                . "<td>$rule</td><td>$match</td><td>$file<br>$message</td></tr>");
169
        } elseif ($rule == 'varname') {
170
            $message = _XOOPS_SMARTY3_SCANNER_MANUAL_REVIEW            ;
171
            $this->outputAppend("<tr class='danger'>"
172
                . "<td>$rule</td><td>$match</td><td>$file<br>$message</td></tr>");
173
        } else {
174
            $this->outputAppend("<tr><td>$rule</td><td>$match</td><td>$file</td></tr>");
175
        }
176
    }
177
178
    /**
179
     * @param string $rule
180
     * @param string $file
181
     * @param string $match
182
     * @param bool   $writable
183
     *
184
     * @returns ArrayObject with keys 'rule', 'file', 'match' and 'writeable'
185
     */
186
    public function makeOutputIssue($rule, $file, $match, $writable)
187
    {
188
        return new ArrayObject(
189
            array(
190
                'rule' => $rule,
191
                'file' => $file,
192
                'match'=> $match,
193
                'writable' => $writable
194
            )
195
        );
196
    }
197
}
198