Passed
Pull Request — 8.x-1.x (#5)
by Frédéric G.
54s
created

Php::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\qa\Views;
4
5
/**
6
 * Find views containing PHP code
7
 */
8
class Php extends Views {
9
10
  /**
11
   * {@inheritdoc]
12
   */
13
  public function init() {
14
    $this->package_name = __NAMESPACE__;
15
    $this->title = t('PHP code within views');
0 ignored issues
show
Bug introduced by
The function t was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

15
    $this->title = /** @scrutinizer ignore-call */ t('PHP code within views');
Loading history...
16
    $this->description = t('Is there any embedded PHP within views and display definitions ? This is both a security risk and a performance issue.');
17
  }
18
19
  /**
20
   * @param string $area
21
   *   The area (header, footer, empty) being examined.
22
   * @param array $php
23
   *   The array of input formats containing PHP.
24
   * @param \stdClass $display
25
   *   The display being examined.
26
   * @param string $area_name
27
   *   The name of the area
28
   *
29
   * @return array
30
   *   The array of PHP fragments found in the area.
31
   */
32
  protected function checkViews2Php($area, array $php, $display, $area_name) {
33
    $ret = array();
34
    $area_format = $display->display_options[$area_name .'_format']; // Always set
35
    if (in_array($area_format, $php)) {
36
      $ret['text'] = $area;
37
    }
38
    return $ret;
39
  }
40
41
  /**
42
   * @param array $area
43
   *   The area (header, footer, empty) being examined.
44
   * @param array $php
45
   *   The array of input formats containing PHP.
46
   *
47
   * @return array
48
   *   The array of PHP fragments found in the area.
49
   */
50
  protected function checkViews3Php(array $area, array $php) {
51
    $ret = array();
52
    foreach ($area as $field => $field_options) {
53
      if ($field_options['field'] == 'area' && isset($field_options['format']) && in_array($field_options['format'], $php)) {
54
        $ret[$field] = $field_options['content'];
55
      }
56
    }
57
    return $ret;
58
  }
59
60
  /**
61
   * Views 2 had a single string for areas whereas Views 3 has an array for them.
62
   */
63
  public function checkViewPhp($view) {
64
    $php = $this->getPhpFormats();
65
    $areas = array('header', 'footer', 'empty');
66
    $result = array();
67
68
    foreach ($view->display as $display_name => $display) {
69
      foreach ($areas as $area_name) {
70
        if (!isset($display->display_options[$area_name])) {
71
          continue;
72
        }
73
74
        $area = $display->display_options[$area_name];
75
        $fragments = is_array($area)
76
          ? $this->checkViews3Php($area, $php)
77
          : $this->checkViews2Php($area, $php, $display, $area_name);
78
79
        if (!empty($fragments))  {
80
          $result[$display_name][$area_name] = $fragments;
81
        }
82
      } // foreach header, footer, empty...
83
    } // foreach display
84
85
    $ret = array(
86
      'name' => $view->name,
87
      'status' => empty($result),
88
      'result' => $result,
89
    );
90
    return $ret;
91
  }
92
93
  /**
94
   * Get a list of the ids of input formats containing the PHP eval filter.
95
   *
96
   * @return array
97
   */
98
  protected function getPhpFormats($reset = FALSE) {
99
    static $php = NULL;
100
101
    if (!isset($php) || $reset) {
102
      $formats = filter_formats();
0 ignored issues
show
Bug introduced by
The function filter_formats was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

102
      $formats = /** @scrutinizer ignore-call */ filter_formats();
Loading history...
103
      $php = array();
104
      foreach ($formats as $format) {
105
        $filters = filter_list_format($format->format);
0 ignored issues
show
Bug introduced by
The function filter_list_format was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

105
        $filters = /** @scrutinizer ignore-call */ filter_list_format($format->format);
Loading history...
106
        foreach ($filters as $filter) {
107
          if ($filter->module == 'php') {
108
            $php[] = $format->format;
109
            break;
110
          }
111
        }
112
      }
113
    }
114
    return $php;
115
  }
116
117
  function run() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
118
    $pass = parent::run();
119
    $views = views_get_all_views(TRUE);
0 ignored issues
show
Bug introduced by
The function views_get_all_views was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

119
    $views = /** @scrutinizer ignore-call */ views_get_all_views(TRUE);
Loading history...
120
    foreach ($views as $view) {
121
      $pass->record($this->checkViewPhp($view));
122
    }
123
    $pass->life->end();
124
125
    if ($pass->status) {
126
      $result = format_plural(count($views), '1 view checked, none containing PHP',
0 ignored issues
show
Bug introduced by
The function format_plural was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

126
      $result = /** @scrutinizer ignore-call */ format_plural(count($views), '1 view checked, none containing PHP',
Loading history...
127
        '@count views checked, none containing PHP', array());
128
    }
129
    else {
130
      $result = format_plural(count($views), '1 view checked and containing PHP',
131
        '@count views checked, @php containing PHP', array(
132
          '@php' => count($pass->result),
133
      ));
134
      $header = array(
135
        t('View'),
0 ignored issues
show
Bug introduced by
The function t was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

135
        /** @scrutinizer ignore-call */ 
136
        t('View'),
Loading history...
136
        t('Display'),
137
        t('Area'),
138
        t('Field'),
139
        t('Content'),
140
      );
141
      $data = array();
142
      foreach ($pass->result as $view_name => $displays) {
143
        $row = array();
144
        $link_title = empty($views[$view_name]->human_name)
145
          ? $view_name
146
          : $views[$view_name]->human_name;
147
        $view_link = l($link_title, "admin/structure/views/view/$view_name/edit");
0 ignored issues
show
Bug introduced by
The function l was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

147
        $view_link = /** @scrutinizer ignore-call */ l($link_title, "admin/structure/views/view/$view_name/edit");
Loading history...
148
        $row['view'] = array('data' => $view_link);
149
        foreach ($displays as $display_id => $areas) {
150
          $row['display'] = l($display_id, "admin/structure/views/view/$view_name/edit/$display_id");
151
          foreach ($areas as $area_name => $fields) {
152
            $row['area'] = l($area_name, "admin/structure/views/nojs/rearrange/$view_name/$display_id/$area_name");
153
            foreach ($fields as $field => $content) {
154
              $row['field'] = l($field,
155
                'admin/structure/views/nojs/config-item/'. $view_name .'/'. $display_id .'/'. $area_name .'/'. $field,
156
                array('query' => array('destination' => 'admin/reports/qa/results'))
157
              );
158
              $row['content'] = array(
159
                'data'  => '<pre>'. check_plain($content) .'</pre>',
0 ignored issues
show
Bug introduced by
The function check_plain was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

159
                'data'  => '<pre>'. /** @scrutinizer ignore-call */ check_plain($content) .'</pre>',
Loading history...
160
                'class' => 'pre',
161
              );
162
              $data[$view_name .'/'. $display_id .'/'. $area_name .'/'. $field] = $row;
163
            }
164
          }
165
        }
166
      }
167
      ksort($data);
168
      $result .= theme('table', array(
0 ignored issues
show
Bug introduced by
The function theme was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

168
      $result .= /** @scrutinizer ignore-call */ theme('table', array(
Loading history...
169
        'header' => $header,
170
        'rows' => $data,
171
      ));
172
    }
173
    $pass->result = $result;
174
    return $pass;
175
  }
176
}
177
178