1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\qa\Views; |
4
|
|
|
|
5
|
|
|
use Drupal\qa\Pass; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Find views containing PHP code |
9
|
|
|
*/ |
10
|
|
|
class Php extends Views { |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* {@inheritdoc] |
14
|
|
|
*/ |
15
|
|
|
public function init() { |
16
|
|
|
$this->package_name = __NAMESPACE__; |
17
|
|
|
$this->title = t('PHP code within views'); |
18
|
|
|
$this->description = t('Is there any embedded PHP within views and display definitions ? This is both a security risk and a performance issue.'); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param string $area |
23
|
|
|
* The area (header, footer, empty) being examined. |
24
|
|
|
* @param array $php |
25
|
|
|
* The array of input formats containing PHP. |
26
|
|
|
* @param \stdClass $display |
27
|
|
|
* The display being examined. |
28
|
|
|
* @param string $area_name |
29
|
|
|
* The name of the area |
30
|
|
|
* |
31
|
|
|
* @return array |
32
|
|
|
* The array of PHP fragments found in the area. |
33
|
|
|
*/ |
34
|
|
|
protected function checkViews2Php($area, array $php, $display, $area_name) { |
35
|
|
|
$ret = []; |
36
|
|
|
$area_format = $display->display_options[$area_name .'_format']; // Always set |
37
|
|
|
if (in_array($area_format, $php)) { |
38
|
|
|
$ret['text'] = $area; |
39
|
|
|
} |
40
|
|
|
return $ret; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param array $area |
45
|
|
|
* The area (header, footer, empty) being examined. |
46
|
|
|
* @param array $php |
47
|
|
|
* The array of input formats containing PHP. |
48
|
|
|
* |
49
|
|
|
* @return array |
50
|
|
|
* The array of PHP fragments found in the area. |
51
|
|
|
*/ |
52
|
|
|
protected function checkViews3Php(array $area, array $php) { |
53
|
|
|
$ret = []; |
54
|
|
|
foreach ($area as $field => $field_options) { |
55
|
|
|
if ($field_options['field'] == 'area' && isset($field_options['format']) && in_array($field_options['format'], $php)) { |
56
|
|
|
$ret[$field] = $field_options['content']; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
return $ret; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Views 2 had a single string for areas whereas Views 3 has an array for them. |
64
|
|
|
*/ |
65
|
|
|
public function checkViewPhp($view) { |
66
|
|
|
$php = $this->getPhpFormats(); |
67
|
|
|
$areas = ['header', 'footer', 'empty']; |
68
|
|
|
$result = []; |
69
|
|
|
|
70
|
|
|
foreach ($view->display as $display_name => $display) { |
71
|
|
|
foreach ($areas as $area_name) { |
72
|
|
|
if (!isset($display->display_options[$area_name])) { |
73
|
|
|
continue; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$area = $display->display_options[$area_name]; |
77
|
|
|
$fragments = is_array($area) |
78
|
|
|
? $this->checkViews3Php($area, $php) |
79
|
|
|
: $this->checkViews2Php($area, $php, $display, $area_name); |
80
|
|
|
|
81
|
|
|
if (!empty($fragments)) { |
82
|
|
|
$result[$display_name][$area_name] = $fragments; |
83
|
|
|
} |
84
|
|
|
} // foreach header, footer, empty... |
85
|
|
|
} // foreach display |
86
|
|
|
|
87
|
|
|
$ret = [ |
88
|
|
|
'name' => $view->name, |
89
|
|
|
'status' => empty($result), |
90
|
|
|
'result' => $result, |
91
|
|
|
]; |
92
|
|
|
return $ret; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get a list of the ids of input formats containing the PHP eval filter. |
97
|
|
|
* |
98
|
|
|
* @return array |
99
|
|
|
*/ |
100
|
|
|
protected function getPhpFormats($reset = FALSE) { |
101
|
|
|
static $php = NULL; |
102
|
|
|
|
103
|
|
|
if (!isset($php) || $reset) { |
104
|
|
|
$formats = filter_formats(); |
105
|
|
|
$php = []; |
106
|
|
|
foreach ($formats as $format) { |
107
|
|
|
$filters = filter_list_format($format->id()); |
|
|
|
|
108
|
|
|
foreach ($filters as $filter) { |
109
|
|
|
if ($filter->module == 'php') { |
110
|
|
|
$php[] = $format->id(); |
111
|
|
|
break; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
return $php; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function run(): Pass { |
120
|
|
|
$pass = parent::run(); |
121
|
|
|
$views = views_get_all_views(TRUE); |
|
|
|
|
122
|
|
|
foreach ($views as $view) { |
123
|
|
|
$pass->record($this->checkViewPhp($view)); |
|
|
|
|
124
|
|
|
} |
125
|
|
|
$pass->life->end(); |
126
|
|
|
|
127
|
|
|
if ($pass->status) { |
|
|
|
|
128
|
|
|
$result = format_plural(count($views), '1 view checked, none containing PHP', |
|
|
|
|
129
|
|
|
'@count views checked, none containing PHP', []); |
130
|
|
|
} |
131
|
|
|
else { |
132
|
|
|
$result = format_plural(count($views), '1 view checked and containing PHP', |
133
|
|
|
'@count views checked, @php containing PHP', [ |
134
|
|
|
'@php' => count($pass->result), |
135
|
|
|
]); |
136
|
|
|
$header = [ |
137
|
|
|
t('View'), |
138
|
|
|
t('Display'), |
139
|
|
|
t('Area'), |
140
|
|
|
t('Field'), |
141
|
|
|
t('Content'), |
142
|
|
|
]; |
143
|
|
|
$data = []; |
144
|
|
|
foreach ($pass->result as $view_name => $displays) { |
145
|
|
|
$row = []; |
146
|
|
|
$link_title = empty($views[$view_name]->human_name) |
147
|
|
|
? $view_name |
148
|
|
|
: $views[$view_name]->human_name; |
149
|
|
|
$view_link = l($link_title, "admin/structure/views/view/$view_name/edit"); |
|
|
|
|
150
|
|
|
$row['view'] = ['data' => $view_link]; |
151
|
|
|
foreach ($displays as $display_id => $areas) { |
152
|
|
|
$row['display'] = l($display_id, "admin/structure/views/view/$view_name/edit/$display_id"); |
153
|
|
|
foreach ($areas as $area_name => $fields) { |
154
|
|
|
$row['area'] = l($area_name, "admin/structure/views/nojs/rearrange/$view_name/$display_id/$area_name"); |
155
|
|
|
foreach ($fields as $field => $content) { |
156
|
|
|
$row['field'] = l($field, |
157
|
|
|
'admin/structure/views/nojs/config-item/'. $view_name .'/'. $display_id .'/'. $area_name .'/'. $field, |
158
|
|
|
['query' => ['destination' => 'admin/reports/qa/results']] |
159
|
|
|
); |
160
|
|
|
$row['content'] = [ |
161
|
|
|
'data' => '<pre>'. check_plain($content) .'</pre>', |
|
|
|
|
162
|
|
|
'class' => 'pre', |
163
|
|
|
]; |
164
|
|
|
$data[$view_name .'/'. $display_id .'/'. $area_name .'/'. $field] = $row; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
ksort($data); |
170
|
|
|
$result .= theme('table', [ |
|
|
|
|
171
|
|
|
'header' => $header, |
172
|
|
|
'rows' => $data, |
173
|
|
|
]); |
174
|
|
|
} |
175
|
|
|
$pass->result = $result; |
176
|
|
|
return $pass; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
|