Including security review as a submodule - with patched for Yaffs.
[yaffs-website] / web / modules / contrib / security_review / src / Controller / HelpController.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\security_review\Controller\HelpController.
6  */
7
8 namespace Drupal\security_review\Controller;
9
10 use Drupal\Core\Controller\ControllerBase;
11 use Drupal\Core\Url;
12 use Drupal\security_review\Checklist;
13 use Drupal\security_review\CheckResult;
14 use Drupal\security_review\SecurityReview;
15 use Symfony\Component\DependencyInjection\ContainerInterface;
16 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
17
18 /**
19  * The class of the Help pages' controller.
20  */
21 class HelpController extends ControllerBase {
22
23   /**
24    * The security_review.checklist service.
25    *
26    * @var \Drupal\security_review\Checklist
27    */
28   protected $checklist;
29
30   /**
31    * The security_review service.
32    *
33    * @var \Drupal\security_review\SecurityReview
34    */
35   protected $securityReview;
36
37   /**
38    * Constructs a HelpController.
39    *
40    * @param \Drupal\security_review\SecurityReview $security_review
41    *   The security_review service.
42    * @param \Drupal\security_review\Checklist $checklist
43    *   The security_review.checklist service.
44    */
45   public function __construct(SecurityReview $security_review, Checklist $checklist) {
46     // Store the dependencies.
47     $this->checklist = $checklist;
48     $this->securityReview = $security_review;
49   }
50
51   /**
52    * {@inheritdoc}
53    */
54   public static function create(ContainerInterface $container) {
55     return new static(
56       $container->get('security_review'),
57       $container->get('security_review.checklist')
58     );
59   }
60
61   /**
62    * Serves as an entry point for the help pages.
63    *
64    * @param string|NULL $namespace
65    *   The namespace of the check (null if general page).
66    * @param string $title
67    *   The name of the check.
68    *
69    * @return array
70    *   The requested help page.
71    */
72   public function index($namespace, $title) {
73     // If no namespace is set, print the general help page.
74     if ($namespace === NULL) {
75       return $this->generalHelp();
76     }
77
78     // Print check-specific help.
79     return $this->checkHelp($namespace, $title);
80   }
81
82   /**
83    * Returns the general help page.
84    *
85    * @return array
86    *   The general help page.
87    */
88   private function generalHelp() {
89     $paragraphs = [];
90
91     // Print the general help.
92     $paragraphs[] = $this->t('You should take the security of your site very seriously. Fortunately, Drupal is fairly secure by default. The Security Review module automates many of the easy-to-make mistakes that render your site insecure, however it does not automatically make your site impenetrable. You should give care to what modules you install and how you configure your site and server. Be mindful of who visits your site and what features you expose for their use.');
93     $paragraphs[] = $this->t('You can read more about securing your site in the <a href="http://drupal.org/security/secure-configuration">drupal.org handbooks</a> and on <a href="http://crackingdrupal.com">CrackingDrupal.com</a>. There are also additional modules you can install to secure or protect your site. Be aware though that the more modules you have running on your site the greater (usually) attack area you expose.');
94     $paragraphs[] = $this->t('<a href="http://drupal.org/node/382752">Drupal.org Handbook: Introduction to security-related contrib modules</a>');
95
96     // Print the list of security checks with links to their help pages.
97     $checks = [];
98     foreach ($this->checklist->getChecks() as $check) {
99       // Get the namespace array's reference.
100       $check_namespace = &$checks[$check->getMachineNamespace()];
101
102       // Set up the namespace array if not set.
103       if (!isset($check_namespace)) {
104         $check_namespace['namespace'] = $check->getNamespace();
105         $check_namespace['check_links'] = [];
106       }
107
108       // Add the link pointing to the check-specific help.
109       $check_namespace['check_links'][] = $this->l(
110         $this->t('@title', ['@title' => $check->getTitle()]),
111         Url::fromRoute('security_review.help', [
112           'namespace' => $check->getMachineNamespace(),
113           'title' => $check->getMachineTitle(),
114         ])
115       );
116     }
117
118     return [
119       '#theme' => 'general_help',
120       '#paragraphs' => $paragraphs,
121       '#checks' => $checks,
122     ];
123   }
124
125   /**
126    * Returns a check-specific help page.
127    *
128    * @param string $namespace
129    *   The namespace of the check.
130    * @param string $title
131    *   The name of the check.
132    *
133    * @return array
134    *   The check's help page.
135    *
136    * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
137    *   If the check is not found.
138    */
139   private function checkHelp($namespace, $title) {
140     // Get the requested check.
141     $check = $this->checklist->getCheck($namespace, $title);
142
143     // If the check doesn't exist, throw 404.
144     if ($check == NULL) {
145       throw new NotFoundHttpException();
146     }
147
148     // Print the help page.
149     $output = [];
150     $output[] = $check->help();
151
152     // If the check is skipped print the skip message, else print the
153     // evaluation.
154     if ($check->isSkipped()) {
155
156       if ($check->skippedBy() != NULL) {
157         $user = $this->l(
158           $check->skippedBy()->getUsername(),
159           $check->skippedBy()->urlInfo()
160         );
161       }
162       else {
163         $user = 'Anonymous';
164       }
165
166       $skip_message = $this->t(
167         'Check marked for skipping on @date by @user',
168         [
169           '@date' => format_date($check->skippedOn()),
170           '@user' => $user,
171         ]
172       );
173
174       $output[] = [
175         '#type' => 'markup',
176         '#markup' => "<p>$skip_message</p>",
177       ];
178     }
179     else {
180       // Evaluate last result, if any.
181       $last_result = $check->lastResult(TRUE);
182       if ($last_result instanceof CheckResult) {
183         // Separator.
184         $output[] = [
185           '#type' => 'markup',
186           '#markup' => '<div />',
187         ];
188
189         // Evaluation page.
190         $output[] = $check->evaluate($last_result);
191       }
192     }
193
194     // Return the completed page.
195     return $output;
196   }
197
198 }