Added the Porter Stemmer module to improve searches. This doesn't deal with some...
[yaffs-website] / web / modules / contrib / security_review / src / Controller / HelpController.php
1 <?php
2
3 namespace Drupal\security_review\Controller;
4
5 use Drupal\Core\Controller\ControllerBase;
6 use Drupal\Core\Datetime\DateFormatterInterface;
7 use Drupal\Core\Link;
8 use Drupal\security_review\Checklist;
9 use Drupal\security_review\CheckResult;
10 use Drupal\security_review\SecurityReview;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
13
14 /**
15  * The class of the Help pages' controller.
16  */
17 class HelpController extends ControllerBase {
18
19   /**
20    * The security_review.checklist service.
21    *
22    * @var \Drupal\security_review\Checklist
23    */
24   protected $checklist;
25
26   /**
27    * The security_review service.
28    *
29    * @var \Drupal\security_review\SecurityReview
30    */
31   protected $securityReview;
32
33   /**
34    * The date.formatter service.
35    *
36    * @var \Drupal\Core\Datetime\DateFormatterInterface
37    */
38   private $dateFormatter;
39
40   /**
41    * Constructs a HelpController.
42    *
43    * @param \Drupal\security_review\SecurityReview $security_review
44    *   The security_review service.
45    * @param \Drupal\security_review\Checklist $checklist
46    *   The security_review.checklist service.
47    * @param \Drupal\Core\Datetime\DateFormatterInterface $dateFormatter
48    *   The date.formatter service.
49    */
50   public function __construct(SecurityReview $security_review, Checklist $checklist, DateFormatterInterface $dateFormatter) {
51     // Store the dependencies.
52     $this->checklist = $checklist;
53     $this->securityReview = $security_review;
54     $this->dateFormatter = $dateFormatter;
55   }
56
57   /**
58    * {@inheritdoc}
59    */
60   public static function create(ContainerInterface $container) {
61     return new static(
62       $container->get('security_review'),
63       $container->get('security_review.checklist'),
64       $container->get('date.formatter')
65     );
66   }
67
68   /**
69    * Serves as an entry point for the help pages.
70    *
71    * @param string|NULL $namespace
72    *   The namespace of the check (null if general page).
73    * @param string $title
74    *   The name of the check.
75    *
76    * @return array
77    *   The requested help page.
78    */
79   public function index($namespace, $title) {
80     // If no namespace is set, print the general help page.
81     if ($namespace === NULL) {
82       return $this->generalHelp();
83     }
84
85     // Print check-specific help.
86     return $this->checkHelp($namespace, $title);
87   }
88
89   /**
90    * Returns the general help page.
91    *
92    * @return array
93    *   The general help page.
94    */
95   private function generalHelp() {
96     $paragraphs = [];
97
98     // Print the general help.
99     $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.');
100     $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.');
101     $paragraphs[] = $this->t('<a href="http://drupal.org/node/382752">Drupal.org Handbook: Introduction to security-related contrib modules</a>');
102
103     // Print the list of security checks with links to their help pages.
104     $checks = [];
105     foreach ($this->checklist->getChecks() as $check) {
106       // Get the namespace array's reference.
107       $check_namespace = &$checks[$check->getMachineNamespace()];
108
109       // Set up the namespace array if not set.
110       if (!isset($check_namespace)) {
111         $check_namespace['namespace'] = $check->getNamespace();
112         $check_namespace['check_links'] = [];
113       }
114
115       // Add the link pointing to the check-specific help.
116       $check_namespace['check_links'][] = Link::createFromRoute(
117         $this->t('@title', ['@title' => $check->getTitle()]),
118         'security_review.help',
119         [
120           'namespace' => $check->getMachineNamespace(),
121           'title' => $check->getMachineTitle(),
122         ]
123       );
124     }
125
126     return [
127       '#theme' => 'general_help',
128       '#paragraphs' => $paragraphs,
129       '#checks' => $checks,
130     ];
131   }
132
133   /**
134    * Returns a check-specific help page.
135    *
136    * @param string $namespace
137    *   The namespace of the check.
138    * @param string $title
139    *   The name of the check.
140    *
141    * @return array
142    *   The check's help page.
143    *
144    * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
145    *   If the check is not found.
146    */
147   private function checkHelp($namespace, $title) {
148     // Get the requested check.
149     $check = $this->checklist->getCheck($namespace, $title);
150
151     // If the check doesn't exist, throw 404.
152     if ($check == NULL) {
153       throw new NotFoundHttpException();
154     }
155
156     // Print the help page.
157     $output = [];
158     $output[] = $check->help();
159
160     // If the check is skipped print the skip message, else print the
161     // evaluation.
162     if ($check->isSkipped()) {
163
164       if ($check->skippedBy() != NULL) {
165         $user = $check->skippedBy()->link();
166       }
167       else {
168         $user = 'Anonymous';
169       }
170
171       $skip_message = $this->t(
172         'Check marked for skipping on @date by @user',
173         [
174           '@date' => $this->dateFormatter->format($check->skippedOn()),
175           '@user' => $user,
176         ]
177       );
178
179       $output[] = [
180         '#type' => 'markup',
181         '#markup' => "<p>$skip_message</p>",
182       ];
183     }
184     else {
185       // Evaluate last result, if any.
186       $last_result = $check->lastResult(TRUE);
187       if ($last_result instanceof CheckResult) {
188         // Separator.
189         $output[] = [
190           '#type' => 'markup',
191           '#markup' => '<div />',
192         ];
193
194         // Evaluation page.
195         $output[] = $check->evaluate($last_result);
196       }
197     }
198
199     // Return the completed page.
200     return $output;
201   }
202
203 }