Including security review as a submodule - with patched for Yaffs.
[yaffs-website] / web / modules / contrib / security_review / security_review.module
1 <?php
2
3 /**
4  * @file
5  * Site security review and reporting Drupal module.
6  */
7
8 use Drupal\Core\Logger\RfcLogLevel;
9 use Drupal\Core\Url;
10 use Drupal\security_review\Check;
11 use Drupal\security_review\CheckResult;
12 use Drupal\security_review\Checks\AdminPermissions;
13 use Drupal\security_review\Checks\ErrorReporting;
14 use Drupal\security_review\Checks\ExecutablePhp;
15 use Drupal\security_review\Checks\FailedLogins;
16 use Drupal\security_review\Checks\Field;
17 use Drupal\security_review\Checks\FilePermissions;
18 use Drupal\security_review\Checks\InputFormats;
19 use Drupal\security_review\Checks\PrivateFiles;
20 use Drupal\security_review\Checks\QueryErrors;
21 use Drupal\security_review\Checks\TemporaryFiles;
22 use Drupal\security_review\Checks\TrustedHosts;
23 use Drupal\security_review\Checks\UploadExtensions;
24 use Drupal\security_review\Checks\ViewsAccess;
25
26 /**
27  * Implements hook_security_review_checks().
28  */
29 function security_review_security_review_checks() {
30   return [
31     new AdminPermissions(),
32     new ErrorReporting(),
33     new ExecutablePhp(),
34     new FailedLogins(),
35     new Field(),
36     new FilePermissions(),
37     new InputFormats(),
38     new PrivateFiles(),
39     new QueryErrors(),
40     new TemporaryFiles(),
41     new TrustedHosts(),
42     new UploadExtensions(),
43     new ViewsAccess(),
44   ];
45 }
46
47 /**
48  * Implements hook_security_review_log().
49  */
50 function security_review_security_review_log(Check $check, $message, array $context, $level) {
51   Drupal::logger('security_review')->log($level, $message, $context);
52 }
53
54 /**
55  * Implements hook_modules_uninstalled().
56  */
57 function security_review_modules_uninstalled($modules) {
58   /** @var \Drupal\security_review\SecurityReview $security_review */
59   $security_review = Drupal::service('security_review');
60
61   // Clean orphaned checks.
62   $security_review->cleanStorage();
63 }
64
65 /**
66  * Implements hook_modules_installed().
67  */
68 function security_review_modules_installed($modules) {
69   // Post-install hook.
70   if (in_array('security_review', $modules)) {
71
72     /** @var \Drupal\security_review\SecurityReview $security_review */
73     $security_review = Drupal::service('security_review');
74
75     // Clean orphaned checks.
76     $security_review->cleanStorage();
77
78     // Store the web server's user.
79     $security_review->setServerData();
80   }
81 }
82
83 /**
84  * Implements hook_theme().
85  */
86 function security_review_theme($existing, $type, $theme, $path) {
87   return [
88     'check_evaluation' => [
89       'template' => 'check_evaluation',
90       'variables' => [
91         'paragraphs' => [],
92         'items' => [],
93       ],
94     ],
95     'check_help' => [
96       'template' => 'check_help',
97       'variables' => [
98         'title' => [],
99         'paragraphs' => [],
100       ],
101     ],
102     'general_help' => [
103       'template' => 'general_help',
104       'variables' => [
105         'paragraphs' => [],
106         'checks' => [],
107       ],
108     ],
109     'run_and_review' => [
110       'template' => 'run_and_review',
111       'variables' => [
112         'date' => [],
113         'checks' => [],
114       ],
115     ],
116   ];
117 }
118
119 /**
120  * Preprocesses variables for template 'run_and_review'.
121  */
122 function template_preprocess_run_and_review(&$variables) {
123   // Get icon list.
124   $icons_root = '/core/misc/icons/';
125   $variables['icons'] = [
126     'success' => $icons_root . '73b355/check.svg',
127     'warning' => $icons_root . 'e29700/warning.svg',
128     'fail' => $icons_root . 'e32700/error.svg',
129   ];
130
131   // Generate full URLs.
132   foreach ($variables['icons'] as $icon => $path) {
133     $variables['icons'][$icon] = Url::fromUserInput($path)->setAbsolute()
134       ->toString();
135   }
136
137   // Format date.
138   $variables['date'] = format_date($variables['date']);
139
140   // Convert check result integers to strings.
141   foreach ($variables['checks'] as &$check) {
142     if (isset($check['result'])) {
143       switch ($check['result']) {
144         case CheckResult::SUCCESS:
145           $check['result'] = 'success';
146           break;
147
148         case CheckResult::FAIL:
149           $check['result'] = 'fail';
150           break;
151
152         case CheckResult::WARN:
153           $check['result'] = 'warning';
154           break;
155
156         case CheckResult::INFO:
157           $check['result'] = 'info';
158           break;
159       }
160     }
161   }
162 }
163
164 /**
165  * Implements hook_cron().
166  */
167 function security_review_cron() {
168   // Store the web server's user.
169   Drupal::service('security_review')->setServerData();
170 }
171
172 /**
173  * Batch operation: runs a single check.
174  *
175  * @param \Drupal\security_review\Check $check
176  *   The check to run.
177  * @param array $context
178  *   The Batch context.
179  */
180 function _security_review_batch_run_op(Check $check, array &$context) {
181   // Inform the user about the progress.
182   $context['message'] = $check->getTitle();
183
184   // Run the check.
185   $results = Drupal::service('security_review.checklist')
186     ->runChecks([$check]);
187
188   // Store the results.
189   $context['results'] = array_merge($context['results'], $results);
190 }
191
192 /**
193  * Callback for finishing the batch job of running the checklist.
194  *
195  * @param bool $success
196  *   Whether the batch job was successful.
197  * @param \Drupal\security_review\CheckResult[] $results
198  *   The results of the batch job.
199  * @param array $operations
200  *   The array of batch operations.
201  */
202 function _security_review_batch_run_finished($success, array $results, array $operations) {
203   /** @var \Drupal\security_review\SecurityReview $security_review */
204   $security_review = Drupal::service('security_review');
205
206   /** @var \Drupal\security_review\Checklist $checklist */
207   $checklist = Drupal::service('security_review.checklist');
208
209   $security_review->setLastRun(time());
210   if ($success) {
211     if (!empty($results)) {
212       $checklist->storeResults($results);
213     }
214     drupal_set_message(t('Review complete'));
215   }
216   else {
217     // Show error information.
218     $error_operation = reset($operations);
219     $message = t(
220       'An error occurred while processing %error_operation with arguments: @arguments',
221       [
222         '%error_operation' => $error_operation[0],
223         '@arguments' => print_r($error_operation[1], TRUE),
224       ]
225     );
226     $security_review->log(NULL, $message, [], RfcLogLevel::ERROR);
227     drupal_set_message(t('The review did not store all results, please run again or check the logs for details.'));
228   }
229 }