Patched to Drupal 8.4.8 level. See https://www.drupal.org/sa-core-2018-004 and patch...
[yaffs-website] / web / core / lib / Drupal / Core / Installer / InstallerRedirectTrait.php
1 <?php
2
3 namespace Drupal\Core\Installer;
4
5 use Drupal\Core\Database\Connection;
6 use Drupal\Core\Database\Database;
7 use Drupal\Core\Database\DatabaseException;
8 use Drupal\Core\Database\DatabaseNotFoundException;
9 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
10
11 /**
12  * Provides methods for checking if Drupal is already installed.
13  */
14 trait InstallerRedirectTrait {
15
16   /**
17    * Returns whether the current PHP process runs on CLI.
18    *
19    * @return bool
20    */
21   protected function isCli() {
22     return PHP_SAPI === 'cli';
23   }
24
25   /**
26    * Determines if an exception handler should redirect to the installer.
27    *
28    * @param \Exception $exception
29    *   The exception to check.
30    * @param \Drupal\Core\Database\Connection|null $connection
31    *   (optional) The default database connection. If not provided, a less
32    *   comprehensive check will be performed. This can be the case if the
33    *   exception occurs early enough that a database connection object isn't
34    *   available from the container yet.
35    *
36    * @return bool
37    *   TRUE if the exception handler should redirect to the installer because
38    *   Drupal is not installed yet, or FALSE otherwise.
39    */
40   protected function shouldRedirectToInstaller(\Exception $exception, Connection $connection = NULL) {
41     // Never redirect on the command line.
42     if ($this->isCli()) {
43       return FALSE;
44     }
45
46     // Never redirect if we're already in the installer.
47     if (drupal_installation_attempted()) {
48       return FALSE;
49     }
50
51     // If the database wasn't found, assume the user hasn't entered it properly
52     // and redirect to the installer. This check needs to come first because a
53     // DatabaseNotFoundException is also an instance of DatabaseException.
54     if ($exception instanceof DatabaseNotFoundException) {
55       return TRUE;
56     }
57
58     // To avoid unnecessary queries, only act if the exception is one that is
59     // expected to occur when Drupal has not yet been installed. This includes
60     // NotFoundHttpException because an uninstalled site won't have route
61     // information available yet and therefore can return 404 errors.
62     if (!($exception instanceof \PDOException || $exception instanceof DatabaseException || $exception instanceof NotFoundHttpException)) {
63       return FALSE;
64     }
65
66     // Redirect if there isn't even any database connection information in
67     // settings.php yet, since that means Drupal is not installed.
68     if (!Database::getConnectionInfo()) {
69       return TRUE;
70     }
71
72     // Redirect if the database is empty.
73     if ($connection) {
74       try {
75         return !$connection->schema()->tableExists('sessions');
76       }
77       catch (\Exception $e) {
78         // If we still have an exception at this point, we need to be careful
79         // since we should not redirect if the exception represents an error on
80         // an already-installed site (for example, if the database server went
81         // down). Assume we shouldn't redirect, just in case.
82         return FALSE;
83       }
84     }
85
86     // When in doubt, don't redirect.
87     return FALSE;
88   }
89
90 }