Chris@0: getRequestTime(); Chris@0: * Chris@0: * @see https://www.drupal.org/node/2785211 Chris@0: */ Chris@0: define('REQUEST_TIME', (int) $_SERVER['REQUEST_TIME']); Chris@0: Chris@0: /** Chris@0: * Regular expression to match PHP function names. Chris@0: * Chris@0: * @see http://php.net/manual/language.functions.php Chris@0: */ Chris@0: const DRUPAL_PHP_FUNCTION_PATTERN = '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'; Chris@0: Chris@0: /** Chris@0: * $config_directories key for active directory. Chris@0: * Chris@0: * @see config_get_config_directory() Chris@0: * Chris@0: * @deprecated in Drupal 8.0.x and will be removed before 9.0.0. Drupal core no Chris@0: * longer creates an active directory. Chris@0: * Chris@0: * @see https://www.drupal.org/node/2501187 Chris@0: */ Chris@0: const CONFIG_ACTIVE_DIRECTORY = 'active'; Chris@0: Chris@0: /** Chris@0: * $config_directories key for sync directory. Chris@0: * Chris@0: * @see config_get_config_directory() Chris@0: */ Chris@0: const CONFIG_SYNC_DIRECTORY = 'sync'; Chris@0: Chris@0: /** Chris@0: * $config_directories key for staging directory. Chris@0: * Chris@0: * @see config_get_config_directory() Chris@0: * @see CONFIG_SYNC_DIRECTORY Chris@0: * Chris@0: * @deprecated in Drupal 8.0.x and will be removed before 9.0.0. The staging Chris@0: * directory was renamed to sync. Chris@0: * Chris@0: * @see https://www.drupal.org/node/2574957 Chris@0: */ Chris@0: const CONFIG_STAGING_DIRECTORY = 'staging'; Chris@0: Chris@0: /** Chris@0: * Defines the root directory of the Drupal installation. Chris@0: * Chris@0: * This strips two levels of directories off the current directory. Chris@0: */ Chris@0: define('DRUPAL_ROOT', dirname(dirname(__DIR__))); Chris@0: Chris@0: /** Chris@0: * Returns the path of a configuration directory. Chris@0: * Chris@0: * Configuration directories are configured using $config_directories in Chris@0: * settings.php. Chris@0: * Chris@0: * @param string $type Chris@0: * The type of config directory to return. Drupal core provides the Chris@0: * CONFIG_SYNC_DIRECTORY constant to access the sync directory. Chris@0: * Chris@0: * @return string Chris@0: * The configuration directory path. Chris@0: * Chris@0: * @throws \Exception Chris@0: */ Chris@0: function config_get_config_directory($type) { Chris@0: global $config_directories; Chris@0: Chris@0: // @todo Remove fallback in Drupal 9. https://www.drupal.org/node/2574943 Chris@0: if ($type == CONFIG_SYNC_DIRECTORY && !isset($config_directories[CONFIG_SYNC_DIRECTORY]) && isset($config_directories[CONFIG_STAGING_DIRECTORY])) { Chris@0: $type = CONFIG_STAGING_DIRECTORY; Chris@0: } Chris@0: Chris@0: if (!empty($config_directories[$type])) { Chris@0: return $config_directories[$type]; Chris@0: } Chris@0: // @todo https://www.drupal.org/node/2696103 Throw a more specific exception. Chris@0: throw new \Exception("The configuration directory type '$type' does not exist"); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns and optionally sets the filename for a system resource. Chris@0: * Chris@0: * The filename, whether provided, cached, or retrieved from the database, is Chris@0: * only returned if the file exists. Chris@0: * Chris@0: * This function plays a key role in allowing Drupal's resources (modules Chris@0: * and themes) to be located in different places depending on a site's Chris@0: * configuration. For example, a module 'foo' may legally be located Chris@0: * in any of these three places: Chris@0: * Chris@0: * core/modules/foo/foo.info.yml Chris@0: * modules/foo/foo.info.yml Chris@0: * sites/example.com/modules/foo/foo.info.yml Chris@0: * Chris@0: * Calling drupal_get_filename('module', 'foo') will give you one of Chris@0: * the above, depending on where the module is located. Chris@0: * Chris@0: * @param $type Chris@0: * The type of the item; one of 'core', 'profile', 'module', 'theme', or Chris@0: * 'theme_engine'. Chris@0: * @param $name Chris@0: * The name of the item for which the filename is requested. Ignored for Chris@0: * $type 'core'. Chris@0: * @param $filename Chris@0: * The filename of the item if it is to be set explicitly rather Chris@0: * than by consulting the database. Chris@0: * Chris@14: * @return string Chris@0: * The filename of the requested item or NULL if the item is not found. Chris@0: */ Chris@0: function drupal_get_filename($type, $name, $filename = NULL) { Chris@0: // Type 'core' only exists to simplify application-level logic; it always maps Chris@0: // to the /core directory, whereas $name is ignored. It is only requested via Chris@0: // drupal_get_path(). /core/core.info.yml does not exist, but is required Chris@0: // since drupal_get_path() returns the dirname() of the returned pathname. Chris@0: if ($type === 'core') { Chris@0: return 'core/core.info.yml'; Chris@0: } Chris@0: Chris@18: try { Chris@17: /** @var \Drupal\Core\Extension\ExtensionList $extension_list */ Chris@18: $extension_list = \Drupal::service("extension.list.$type"); Chris@17: if (isset($filename)) { Chris@17: // Manually add the info file path of an extension. Chris@17: $extension_list->setPathname($name, $filename); Chris@17: } Chris@18: return $extension_list->getPathname($name); Chris@0: } Chris@18: catch (ServiceNotFoundException $e) { Chris@18: // Catch the exception. This will result in triggering an error. Chris@18: // If the service is unknown, create a user-level error message. Chris@18: trigger_error( Chris@18: sprintf('Unknown type specified: "%s". Must be one of: "core", "profile", "module", "theme", or "theme_engine".', $type), Chris@18: E_USER_WARNING Chris@18: ); Chris@0: } Chris@18: catch (\InvalidArgumentException $e) { Chris@18: // Catch the exception. This will result in triggering an error. Chris@18: // If the filename is still unknown, create a user-level error message. Chris@18: trigger_error( Chris@18: sprintf('The following %s is missing from the file system: %s', $type, $name), Chris@18: E_USER_WARNING Chris@18: ); Chris@18: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the path to a system item (module, theme, etc.). Chris@0: * Chris@0: * @param $type Chris@0: * The type of the item; one of 'core', 'profile', 'module', 'theme', or Chris@0: * 'theme_engine'. Chris@0: * @param $name Chris@0: * The name of the item for which the path is requested. Ignored for Chris@0: * $type 'core'. Chris@0: * Chris@14: * @return string Chris@0: * The path to the requested item or an empty string if the item is not found. Chris@0: */ Chris@0: function drupal_get_path($type, $name) { Chris@0: return dirname(drupal_get_filename($type, $name)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Translates a string to the current language or to a given language. Chris@0: * Chris@0: * In order for strings to be localized, make them available in one of the ways Chris@0: * supported by the @link i18n Localization API. @endlink When possible, use Chris@0: * the \Drupal\Core\StringTranslation\StringTranslationTrait $this->t(). Chris@0: * Otherwise create a new \Drupal\Core\StringTranslation\TranslatableMarkup Chris@0: * object directly. Chris@0: * Chris@0: * See \Drupal\Core\StringTranslation\TranslatableMarkup::__construct() for Chris@0: * important security information and usage guidelines. Chris@0: * Chris@0: * @param string $string Chris@0: * A string containing the English text to translate. Chris@0: * @param array $args Chris@0: * (optional) An associative array of replacements to make after translation. Chris@0: * Based on the first character of the key, the value is escaped and/or Chris@0: * themed. See Chris@0: * \Drupal\Component\Render\FormattableMarkup::placeholderFormat() for Chris@0: * details. Chris@0: * @param array $options Chris@0: * (optional) An associative array of additional options, with the following Chris@0: * elements: Chris@0: * - 'langcode' (defaults to the current language): A language code, to Chris@0: * translate to a language other than what is used to display the page. Chris@0: * - 'context' (defaults to the empty context): The context the source string Chris@0: * belongs to. See the @link i18n Internationalization topic @endlink for Chris@0: * more information about string contexts. Chris@0: * Chris@0: * @return \Drupal\Core\StringTranslation\TranslatableMarkup Chris@0: * An object that, when cast to a string, returns the translated string. Chris@0: * Chris@0: * @see \Drupal\Component\Render\FormattableMarkup::placeholderFormat() Chris@0: * @see \Drupal\Core\StringTranslation\StringTranslationTrait::t() Chris@0: * @see \Drupal\Core\StringTranslation\TranslatableMarkup::__construct() Chris@0: * Chris@0: * @ingroup sanitization Chris@0: */ Chris@0: function t($string, array $args = [], array $options = []) { Chris@0: return new TranslatableMarkup($string, $args, $options); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Formats a string for HTML display by replacing variable placeholders. Chris@0: * Chris@0: * @see \Drupal\Component\Render\FormattableMarkup::placeholderFormat() Chris@0: * @see \Drupal\Component\Render\FormattableMarkup Chris@0: * @see t() Chris@0: * @ingroup sanitization Chris@0: * Chris@0: * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0. Chris@0: * Use \Drupal\Component\Render\FormattableMarkup. Chris@0: * Chris@0: * @see https://www.drupal.org/node/2302363 Chris@0: */ Chris@0: function format_string($string, array $args) { Chris@17: return new FormattableMarkup($string, $args); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks whether a string is valid UTF-8. Chris@0: * Chris@0: * All functions designed to filter input should use drupal_validate_utf8 Chris@0: * to ensure they operate on valid UTF-8 strings to prevent bypass of the Chris@0: * filter. Chris@0: * Chris@0: * When text containing an invalid UTF-8 lead byte (0xC0 - 0xFF) is presented Chris@0: * as UTF-8 to Internet Explorer 6, the program may misinterpret subsequent Chris@0: * bytes. When these subsequent bytes are HTML control characters such as Chris@0: * quotes or angle brackets, parts of the text that were deemed safe by filters Chris@0: * end up in locations that are potentially unsafe; An onerror attribute that Chris@0: * is outside of a tag, and thus deemed safe by a filter, can be interpreted Chris@0: * by the browser as if it were inside the tag. Chris@0: * Chris@0: * The function does not return FALSE for strings containing character codes Chris@0: * above U+10FFFF, even though these are prohibited by RFC 3629. Chris@0: * Chris@0: * @param $text Chris@0: * The text to check. Chris@0: * Chris@0: * @return bool Chris@0: * TRUE if the text is valid UTF-8, FALSE if not. Chris@0: * Chris@0: * @see \Drupal\Component\Utility\Unicode::validateUtf8() Chris@0: * Chris@0: * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0. Chris@0: * Use \Drupal\Component\Utility\Unicode::validateUtf8(). Chris@0: * Chris@0: * @see https://www.drupal.org/node/1992584 Chris@0: */ Chris@0: function drupal_validate_utf8($text) { Chris@0: return Unicode::validateUtf8($text); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Logs an exception. Chris@0: * Chris@0: * This is a wrapper logging function which automatically decodes an exception. Chris@0: * Chris@0: * @param $type Chris@0: * The category to which this message belongs. Chris@0: * @param $exception Chris@0: * The exception that is going to be logged. Chris@0: * @param $message Chris@0: * The message to store in the log. If empty, a text that contains all useful Chris@0: * information about the passed-in exception is used. Chris@0: * @param $variables Chris@0: * Array of variables to replace in the message on display or Chris@0: * NULL if message is already translated or not possible to Chris@0: * translate. Chris@0: * @param $severity Chris@0: * The severity of the message, as per RFC 3164. Chris@0: * @param $link Chris@0: * A link to associate with the message. Chris@0: * Chris@0: * @see \Drupal\Core\Utility\Error::decodeException() Chris@0: */ Chris@0: function watchdog_exception($type, Exception $exception, $message = NULL, $variables = [], $severity = RfcLogLevel::ERROR, $link = NULL) { Chris@0: Chris@0: // Use a default value if $message is not set. Chris@0: if (empty($message)) { Chris@0: $message = '%type: @message in %function (line %line of %file).'; Chris@0: } Chris@0: Chris@0: if ($link) { Chris@0: $variables['link'] = $link; Chris@0: } Chris@0: Chris@0: $variables += Error::decodeException($exception); Chris@0: Chris@0: \Drupal::logger($type)->log($severity, $message, $variables); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets a message to display to the user. Chris@0: * Chris@0: * Messages are stored in a session variable and displayed in the page template Chris@0: * via the $messages theme variable. Chris@0: * Chris@0: * Example usage: Chris@0: * @code Chris@0: * drupal_set_message(t('An error occurred and processing did not complete.'), 'error'); Chris@0: * @endcode Chris@0: * Chris@0: * @param string|\Drupal\Component\Render\MarkupInterface $message Chris@0: * (optional) The translated message to be displayed to the user. For Chris@0: * consistency with other messages, it should begin with a capital letter and Chris@0: * end with a period. Chris@0: * @param string $type Chris@0: * (optional) The message's type. Defaults to 'status'. These values are Chris@0: * supported: Chris@0: * - 'status' Chris@0: * - 'warning' Chris@0: * - 'error' Chris@0: * @param bool $repeat Chris@0: * (optional) If this is FALSE and the message is already set, then the Chris@0: * message won't be repeated. Defaults to FALSE. Chris@0: * Chris@0: * @return array|null Chris@0: * A multidimensional array with keys corresponding to the set message types. Chris@0: * The indexed array values of each contain the set messages for that type, Chris@0: * and each message is an associative array with the following format: Chris@0: * - safe: Boolean indicating whether the message string has been marked as Chris@0: * safe. Non-safe strings will be escaped automatically. Chris@0: * - message: The message string. Chris@0: * So, the following is an example of the full return array structure: Chris@0: * @code Chris@0: * array( Chris@0: * 'status' => array( Chris@0: * array( Chris@0: * 'safe' => TRUE, Chris@0: * 'message' => 'A safe markup string.', Chris@0: * ), Chris@0: * array( Chris@0: * 'safe' => FALSE, Chris@0: * 'message' => "$arbitrary_user_input to escape.", Chris@0: * ), Chris@0: * ), Chris@0: * ); Chris@0: * @endcode Chris@0: * If there are no messages set, the function returns NULL. Chris@0: * Chris@0: * @see drupal_get_messages() Chris@0: * @see status-messages.html.twig Chris@14: * @see https://www.drupal.org/node/2774931 Chris@14: * Chris@14: * @deprecated in Drupal 8.5.0 and will be removed before Drupal 9.0.0. Chris@14: * Use \Drupal\Core\Messenger\MessengerInterface::addMessage() instead. Chris@0: */ Chris@0: function drupal_set_message($message = NULL, $type = 'status', $repeat = FALSE) { Chris@14: @trigger_error('drupal_set_message() is deprecated in Drupal 8.5.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Messenger\MessengerInterface::addMessage() instead. See https://www.drupal.org/node/2774931', E_USER_DEPRECATED); Chris@14: $messenger = \Drupal::messenger(); Chris@0: if (isset($message)) { Chris@14: $messenger->addMessage($message, $type, $repeat); Chris@0: } Chris@14: return $messenger->all(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns all messages that have been set with drupal_set_message(). Chris@0: * Chris@0: * @param string $type Chris@0: * (optional) Limit the messages returned by type. Defaults to NULL, meaning Chris@0: * all types. These values are supported: Chris@0: * - NULL Chris@0: * - 'status' Chris@0: * - 'warning' Chris@0: * - 'error' Chris@0: * @param bool $clear_queue Chris@0: * (optional) If this is TRUE, the queue will be cleared of messages of the Chris@0: * type specified in the $type parameter. Otherwise the queue will be left Chris@0: * intact. Defaults to TRUE. Chris@0: * Chris@0: * @return array Chris@0: * An associative, nested array of messages grouped by message type, with Chris@0: * the top-level keys as the message type. The messages returned are Chris@0: * limited to the type specified in the $type parameter, if any. If there Chris@0: * are no messages of the specified type, an empty array is returned. See Chris@0: * drupal_set_message() for the array structure of individual messages. Chris@0: * Chris@0: * @see drupal_set_message() Chris@0: * @see status-messages.html.twig Chris@14: * @see https://www.drupal.org/node/2774931 Chris@14: * Chris@14: * @deprecated in Drupal 8.5.0 and will be removed before Drupal 9.0.0. Chris@14: * Use \Drupal\Core\Messenger\MessengerInterface::all() or Chris@14: * \Drupal\Core\Messenger\MessengerInterface::messagesByType() instead. Chris@0: */ Chris@0: function drupal_get_messages($type = NULL, $clear_queue = TRUE) { Chris@14: @trigger_error('drupal_get_message() is deprecated in Drupal 8.5.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Messenger\MessengerInterface::all() or \Drupal\Core\Messenger\MessengerInterface::messagesByType() instead. See https://www.drupal.org/node/2774931', E_USER_DEPRECATED); Chris@14: $messenger = \Drupal::messenger(); Chris@14: if ($messages = $messenger->all()) { Chris@0: if ($type) { Chris@0: if ($clear_queue) { Chris@14: $messenger->deleteByType($type); Chris@0: } Chris@0: if (isset($messages[$type])) { Chris@0: return [$type => $messages[$type]]; Chris@0: } Chris@0: } Chris@0: else { Chris@0: if ($clear_queue) { Chris@14: $messenger->deleteAll(); Chris@0: } Chris@0: return $messages; Chris@0: } Chris@0: } Chris@0: return []; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the time zone of the current user. Chris@14: * Chris@14: * @return string Chris@14: * The name of the current user's timezone or the name of the default timezone. Chris@0: */ Chris@0: function drupal_get_user_timezone() { Chris@0: $user = \Drupal::currentUser(); Chris@0: $config = \Drupal::config('system.date'); Chris@0: Chris@0: if ($user && $config->get('timezone.user.configurable') && $user->isAuthenticated() && $user->getTimezone()) { Chris@0: return $user->getTimezone(); Chris@0: } Chris@0: else { Chris@0: // Ignore PHP strict notice if time zone has not yet been set in the php.ini Chris@0: // configuration. Chris@0: $config_data_default_timezone = $config->get('timezone.default'); Chris@0: return !empty($config_data_default_timezone) ? $config_data_default_timezone : @date_default_timezone_get(); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Provides custom PHP error handling. Chris@0: * Chris@0: * @param $error_level Chris@0: * The level of the error raised. Chris@0: * @param $message Chris@0: * The error message. Chris@0: * @param $filename Chris@17: * (optional) The filename that the error was raised in. Chris@0: * @param $line Chris@17: * (optional) The line number the error was raised at. Chris@0: * @param $context Chris@17: * (optional) An array that points to the active symbol table at the point the Chris@17: * error occurred. Chris@0: */ Chris@17: function _drupal_error_handler($error_level, $message, $filename = NULL, $line = NULL, $context = NULL) { Chris@0: require_once __DIR__ . '/errors.inc'; Chris@0: _drupal_error_handler_real($error_level, $message, $filename, $line, $context); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Provides custom PHP exception handling. Chris@0: * Chris@0: * Uncaught exceptions are those not enclosed in a try/catch block. They are Chris@0: * always fatal: the execution of the script will stop as soon as the exception Chris@0: * handler exits. Chris@0: * Chris@0: * @param \Exception|\Throwable $exception Chris@0: * The exception object that was thrown. Chris@0: */ Chris@0: function _drupal_exception_handler($exception) { Chris@0: require_once __DIR__ . '/errors.inc'; Chris@0: Chris@0: try { Chris@0: // Log the message to the watchdog and return an error page to the user. Chris@0: _drupal_log_error(Error::decodeException($exception), TRUE); Chris@0: } Chris@0: // PHP 7 introduces Throwable, which covers both Error and Chris@0: // Exception throwables. Chris@0: catch (\Throwable $error) { Chris@0: _drupal_exception_handler_additional($exception, $error); Chris@0: } Chris@0: // In order to be compatible with PHP 5 we also catch regular Exceptions. Chris@0: catch (\Exception $exception2) { Chris@0: _drupal_exception_handler_additional($exception, $exception2); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Displays any additional errors caught while handling an exception. Chris@0: * Chris@0: * @param \Exception|\Throwable $exception Chris@0: * The first exception object that was thrown. Chris@0: * @param \Exception|\Throwable $exception2 Chris@0: * The second exception object that was thrown. Chris@0: */ Chris@0: function _drupal_exception_handler_additional($exception, $exception2) { Chris@0: // Another uncaught exception was thrown while handling the first one. Chris@0: // If we are displaying errors, then do so with no possibility of a further Chris@0: // uncaught exception being thrown. Chris@0: if (error_displayable()) { Chris@0: print '

Additional uncaught exception thrown while handling exception.

'; Chris@0: print '

Original

' . Error::renderExceptionSafe($exception) . '

'; Chris@0: print '

Additional

' . Error::renderExceptionSafe($exception2) . '


'; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the test prefix if this is an internal request from SimpleTest. Chris@0: * Chris@0: * @param string $new_prefix Chris@0: * Internal use only. A new prefix to be stored. Chris@0: * Chris@0: * @return string|false Chris@0: * Either the simpletest prefix (the string "simpletest" followed by any Chris@0: * number of digits) or FALSE if the user agent does not contain a valid Chris@0: * HMAC and timestamp. Chris@0: */ Chris@0: function drupal_valid_test_ua($new_prefix = NULL) { Chris@0: static $test_prefix; Chris@0: Chris@0: if (isset($new_prefix)) { Chris@0: $test_prefix = $new_prefix; Chris@0: } Chris@0: if (isset($test_prefix)) { Chris@0: return $test_prefix; Chris@0: } Chris@0: // Unless the below User-Agent and HMAC validation succeeds, we are not in Chris@0: // a test environment. Chris@0: $test_prefix = FALSE; Chris@0: Chris@0: // A valid Simpletest request will contain a hashed and salted authentication Chris@0: // code. Check if this code is present in a cookie or custom user agent Chris@0: // string. Chris@0: $http_user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : NULL; Chris@0: $user_agent = isset($_COOKIE['SIMPLETEST_USER_AGENT']) ? $_COOKIE['SIMPLETEST_USER_AGENT'] : $http_user_agent; Chris@0: if (isset($user_agent) && preg_match("/^simple(\w+\d+):(.+):(.+):(.+)$/", $user_agent, $matches)) { Chris@0: list(, $prefix, $time, $salt, $hmac) = $matches; Chris@0: $check_string = $prefix . ':' . $time . ':' . $salt; Chris@0: // Read the hash salt prepared by drupal_generate_test_ua(). Chris@0: // This function is called before settings.php is read and Drupal's error Chris@0: // handlers are set up. While Drupal's error handling may be properly Chris@0: // configured on production sites, the server's PHP error_reporting may not. Chris@0: // Ensure that no information leaks on production sites. Chris@0: $test_db = new TestDatabase($prefix); Chris@0: $key_file = DRUPAL_ROOT . '/' . $test_db->getTestSitePath() . '/.htkey'; Chris@0: if (!is_readable($key_file)) { Chris@0: header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden'); Chris@0: exit; Chris@0: } Chris@0: $private_key = file_get_contents($key_file); Chris@0: // The file properties add more entropy not easily accessible to others. Chris@0: $key = $private_key . filectime(__FILE__) . fileinode(__FILE__); Chris@0: $time_diff = REQUEST_TIME - $time; Chris@0: $test_hmac = Crypt::hmacBase64($check_string, $key); Chris@0: // Since we are making a local request a 600 second time window is allowed, Chris@0: // and the HMAC must match. Chris@0: if ($time_diff >= 0 && $time_diff <= 600 && $hmac === $test_hmac) { Chris@0: $test_prefix = $prefix; Chris@0: } Chris@0: else { Chris@0: header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden (SIMPLETEST_USER_AGENT invalid)'); Chris@0: exit; Chris@0: } Chris@0: } Chris@0: return $test_prefix; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Generates a user agent string with a HMAC and timestamp for simpletest. Chris@0: */ Chris@0: function drupal_generate_test_ua($prefix) { Chris@0: static $key, $last_prefix; Chris@0: Chris@0: if (!isset($key) || $last_prefix != $prefix) { Chris@0: $last_prefix = $prefix; Chris@0: $test_db = new TestDatabase($prefix); Chris@0: $key_file = DRUPAL_ROOT . '/' . $test_db->getTestSitePath() . '/.htkey'; Chris@0: // When issuing an outbound HTTP client request from within an inbound test Chris@0: // request, then the outbound request has to use the same User-Agent header Chris@0: // as the inbound request. A newly generated private key for the same test Chris@0: // prefix would invalidate all subsequent inbound requests. Chris@0: // @see \Drupal\Core\Http\Plugin\SimpletestHttpRequestSubscriber Chris@0: if (DRUPAL_TEST_IN_CHILD_SITE && $parent_prefix = drupal_valid_test_ua()) { Chris@0: if ($parent_prefix != $prefix) { Chris@0: throw new \RuntimeException("Malformed User-Agent: Expected '$parent_prefix' but got '$prefix'."); Chris@0: } Chris@0: // If the file is not readable, a PHP warning is expected in this case. Chris@0: $private_key = file_get_contents($key_file); Chris@0: } Chris@0: else { Chris@0: // Generate and save a new hash salt for a test run. Chris@0: // Consumed by drupal_valid_test_ua() before settings.php is loaded. Chris@0: $private_key = Crypt::randomBytesBase64(55); Chris@0: file_put_contents($key_file, $private_key); Chris@0: } Chris@0: // The file properties add more entropy not easily accessible to others. Chris@0: $key = $private_key . filectime(__FILE__) . fileinode(__FILE__); Chris@0: } Chris@0: // Generate a moderately secure HMAC based on the database credentials. Chris@0: $salt = uniqid('', TRUE); Chris@0: $check_string = $prefix . ':' . time() . ':' . $salt; Chris@0: return 'simple' . $check_string . ':' . Crypt::hmacBase64($check_string, $key); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Enables use of the theme system without requiring database access. Chris@0: * Chris@0: * Loads and initializes the theme system for site installs, updates and when Chris@0: * the site is in maintenance mode. This also applies when the database fails. Chris@0: * Chris@0: * @see _drupal_maintenance_theme() Chris@0: */ Chris@0: function drupal_maintenance_theme() { Chris@0: require_once __DIR__ . '/theme.maintenance.inc'; Chris@0: _drupal_maintenance_theme(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns TRUE if a Drupal installation is currently being attempted. Chris@0: */ Chris@0: function drupal_installation_attempted() { Chris@0: // This cannot rely on the MAINTENANCE_MODE constant, since that would prevent Chris@0: // tests from using the non-interactive installer, in which case Drupal Chris@0: // only happens to be installed within the same request, but subsequently Chris@0: // executed code does not involve the installer at all. Chris@0: // @see install_drupal() Chris@0: return isset($GLOBALS['install_state']) && empty($GLOBALS['install_state']['installation_finished']); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the name of the currently active installation profile. Chris@0: * Chris@0: * When this function is called during Drupal's initial installation process, Chris@0: * the name of the profile that's about to be installed is stored in the global Chris@0: * installation state. At all other times, the "install_profile" setting will be Chris@0: * available in container as a parameter. Chris@0: * Chris@0: * @return string|null Chris@0: * The name of the installation profile or NULL if no installation profile is Chris@0: * currently active. This is the case for example during the first steps of Chris@0: * the installer or during unit tests. Chris@0: * Chris@0: * @deprecated in Drupal 8.3.0, will be removed before Drupal 9.0.0. Chris@0: * Use the install_profile container parameter or \Drupal::installProfile() Chris@0: * instead. If you are accessing the value before it is written to Chris@0: * configuration during the installer use the $install_state global. If you Chris@0: * need to access the value before container is available you can use Chris@0: * BootstrapConfigStorageFactory to load the value directly from Chris@0: * configuration. Chris@0: * Chris@0: * @see https://www.drupal.org/node/2538996 Chris@0: */ Chris@0: function drupal_get_profile() { Chris@0: global $install_state; Chris@0: Chris@0: if (drupal_installation_attempted()) { Chris@0: // If the profile has been selected return it. Chris@0: if (isset($install_state['parameters']['profile'])) { Chris@0: $profile = $install_state['parameters']['profile']; Chris@0: } Chris@0: else { Chris@0: $profile = NULL; Chris@0: } Chris@0: } Chris@0: else { Chris@0: if (\Drupal::hasContainer()) { Chris@0: $profile = \Drupal::installProfile(); Chris@0: } Chris@0: else { Chris@0: $profile = BootstrapConfigStorageFactory::getDatabaseStorage()->read('core.extension')['profile']; Chris@0: } Chris@0: } Chris@0: Chris@0: return $profile; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Registers an additional namespace. Chris@0: * Chris@0: * @param string $name Chris@0: * The namespace component to register; e.g., 'node'. Chris@0: * @param string $path Chris@0: * The relative path to the Drupal component in the filesystem. Chris@0: */ Chris@0: function drupal_classloader_register($name, $path) { Chris@0: $loader = \Drupal::service('class_loader'); Chris@0: $loader->addPsr4('Drupal\\' . $name . '\\', \Drupal::root() . '/' . $path . '/src'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Provides central static variable storage. Chris@0: * Chris@0: * All functions requiring a static variable to persist or cache data within Chris@0: * a single page request are encouraged to use this function unless it is Chris@0: * absolutely certain that the static variable will not need to be reset during Chris@0: * the page request. By centralizing static variable storage through this Chris@0: * function, other functions can rely on a consistent API for resetting any Chris@0: * other function's static variables. Chris@0: * Chris@0: * Example: Chris@0: * @code Chris@0: * function example_list($field = 'default') { Chris@0: * $examples = &drupal_static(__FUNCTION__); Chris@0: * if (!isset($examples)) { Chris@0: * // If this function is being called for the first time after a reset, Chris@0: * // query the database and execute any other code needed to retrieve Chris@0: * // information. Chris@0: * ... Chris@0: * } Chris@0: * if (!isset($examples[$field])) { Chris@0: * // If this function is being called for the first time for a particular Chris@0: * // index field, then execute code needed to index the information already Chris@0: * // available in $examples by the desired field. Chris@0: * ... Chris@0: * } Chris@0: * // Subsequent invocations of this function for a particular index field Chris@0: * // skip the above two code blocks and quickly return the already indexed Chris@0: * // information. Chris@0: * return $examples[$field]; Chris@0: * } Chris@0: * function examples_admin_overview() { Chris@0: * // When building the content for the overview page, make sure to get Chris@0: * // completely fresh information. Chris@0: * drupal_static_reset('example_list'); Chris@0: * ... Chris@0: * } Chris@0: * @endcode Chris@0: * Chris@0: * In a few cases, a function can have certainty that there is no legitimate Chris@0: * use-case for resetting that function's static variable. This is rare, Chris@0: * because when writing a function, it's hard to forecast all the situations in Chris@0: * which it will be used. A guideline is that if a function's static variable Chris@0: * does not depend on any information outside of the function that might change Chris@0: * during a single page request, then it's ok to use the "static" keyword Chris@0: * instead of the drupal_static() function. Chris@0: * Chris@0: * Example: Chris@0: * @code Chris@0: * function mymodule_log_stream_handle($new_handle = NULL) { Chris@0: * static $handle; Chris@0: * if (isset($new_handle)) { Chris@0: * $handle = $new_handle; Chris@0: * } Chris@0: * return $handle; Chris@0: * } Chris@0: * @endcode Chris@0: * Chris@0: * In a few cases, a function needs a resettable static variable, but the Chris@0: * function is called many times (100+) during a single page request, so Chris@0: * every microsecond of execution time that can be removed from the function Chris@0: * counts. These functions can use a more cumbersome, but faster variant of Chris@0: * calling drupal_static(). It works by storing the reference returned by Chris@0: * drupal_static() in the calling function's own static variable, thereby Chris@0: * removing the need to call drupal_static() for each iteration of the function. Chris@0: * Conceptually, it replaces: Chris@0: * @code Chris@0: * $foo = &drupal_static(__FUNCTION__); Chris@0: * @endcode Chris@0: * with: Chris@0: * @code Chris@0: * // Unfortunately, this does not work. Chris@0: * static $foo = &drupal_static(__FUNCTION__); Chris@0: * @endcode Chris@0: * However, the above line of code does not work, because PHP only allows static Chris@0: * variables to be initialized by literal values, and does not allow static Chris@0: * variables to be assigned to references. Chris@0: * - http://php.net/manual/language.variables.scope.php#language.variables.scope.static Chris@0: * - http://php.net/manual/language.variables.scope.php#language.variables.scope.references Chris@0: * The example below shows the syntax needed to work around both limitations. Chris@0: * For benchmarks and more information, see https://www.drupal.org/node/619666. Chris@0: * Chris@0: * Example: Chris@0: * @code Chris@0: * function example_default_format_type() { Chris@0: * // Use the advanced drupal_static() pattern, since this is called very often. Chris@0: * static $drupal_static_fast; Chris@0: * if (!isset($drupal_static_fast)) { Chris@0: * $drupal_static_fast['format_type'] = &drupal_static(__FUNCTION__); Chris@0: * } Chris@0: * $format_type = &$drupal_static_fast['format_type']; Chris@0: * ... Chris@0: * } Chris@0: * @endcode Chris@0: * Chris@0: * @param $name Chris@0: * Globally unique name for the variable. For a function with only one static, Chris@0: * variable, the function name (e.g. via the PHP magic __FUNCTION__ constant) Chris@0: * is recommended. For a function with multiple static variables add a Chris@0: * distinguishing suffix to the function name for each one. Chris@0: * @param $default_value Chris@0: * Optional default value. Chris@0: * @param $reset Chris@0: * TRUE to reset one or all variables(s). This parameter is only used Chris@0: * internally and should not be passed in; use drupal_static_reset() instead. Chris@0: * (This function's return value should not be used when TRUE is passed in.) Chris@0: * Chris@14: * @return array Chris@0: * Returns a variable by reference. Chris@0: * Chris@0: * @see drupal_static_reset() Chris@0: */ Chris@0: function &drupal_static($name, $default_value = NULL, $reset = FALSE) { Chris@0: static $data = [], $default = []; Chris@0: // First check if dealing with a previously defined static variable. Chris@0: if (isset($data[$name]) || array_key_exists($name, $data)) { Chris@0: // Non-NULL $name and both $data[$name] and $default[$name] statics exist. Chris@0: if ($reset) { Chris@0: // Reset pre-existing static variable to its default value. Chris@0: $data[$name] = $default[$name]; Chris@0: } Chris@0: return $data[$name]; Chris@0: } Chris@0: // Neither $data[$name] nor $default[$name] static variables exist. Chris@0: if (isset($name)) { Chris@0: if ($reset) { Chris@0: // Reset was called before a default is set and yet a variable must be Chris@0: // returned. Chris@0: return $data; Chris@0: } Chris@0: // First call with new non-NULL $name. Initialize a new static variable. Chris@0: $default[$name] = $data[$name] = $default_value; Chris@0: return $data[$name]; Chris@0: } Chris@0: // Reset all: ($name == NULL). This needs to be done one at a time so that Chris@0: // references returned by earlier invocations of drupal_static() also get Chris@0: // reset. Chris@0: foreach ($default as $name => $value) { Chris@0: $data[$name] = $value; Chris@0: } Chris@0: // As the function returns a reference, the return should always be a Chris@0: // variable. Chris@0: return $data; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Resets one or all centrally stored static variable(s). Chris@0: * Chris@0: * @param $name Chris@0: * Name of the static variable to reset. Omit to reset all variables. Chris@0: * Resetting all variables should only be used, for example, for running Chris@0: * unit tests with a clean environment. Chris@0: */ Chris@0: function drupal_static_reset($name = NULL) { Chris@0: drupal_static($name, NULL, TRUE); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Formats text for emphasized display in a placeholder inside a sentence. Chris@0: * Chris@0: * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0. Use Chris@17: * \Drupal\Component\Render\FormattableMarkup or Twig's "placeholder" filter Chris@17: * instead. Note this method should not be used to simply emphasize a string Chris@17: * and therefore has few valid use-cases. Note also, that this method does not Chris@17: * mark the string as safe. Chris@0: * Chris@0: * @see https://www.drupal.org/node/2302363 Chris@0: */ Chris@0: function drupal_placeholder($text) { Chris@0: return '' . Html::escape($text) . ''; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Registers a function for execution on shutdown. Chris@0: * Chris@0: * Wrapper for register_shutdown_function() that catches thrown exceptions to Chris@0: * avoid "Exception thrown without a stack frame in Unknown". Chris@0: * Chris@14: * @param callable $callback Chris@0: * The shutdown function to register. Chris@0: * @param ... Chris@0: * Additional arguments to pass to the shutdown function. Chris@0: * Chris@14: * @return array Chris@0: * Array of shutdown functions to be executed. Chris@0: * Chris@0: * @see register_shutdown_function() Chris@0: * @ingroup php_wrappers Chris@0: */ Chris@0: function &drupal_register_shutdown_function($callback = NULL) { Chris@0: // We cannot use drupal_static() here because the static cache is reset during Chris@0: // batch processing, which breaks batch handling. Chris@0: static $callbacks = []; Chris@0: Chris@0: if (isset($callback)) { Chris@0: // Only register the internal shutdown function once. Chris@0: if (empty($callbacks)) { Chris@0: register_shutdown_function('_drupal_shutdown_function'); Chris@0: } Chris@0: $args = func_get_args(); Chris@0: // Remove $callback from the arguments. Chris@0: unset($args[0]); Chris@0: // Save callback and arguments Chris@0: $callbacks[] = ['callback' => $callback, 'arguments' => $args]; Chris@0: } Chris@0: return $callbacks; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Executes registered shutdown functions. Chris@0: */ Chris@0: function _drupal_shutdown_function() { Chris@0: $callbacks = &drupal_register_shutdown_function(); Chris@0: Chris@0: // Set the CWD to DRUPAL_ROOT as it is not guaranteed to be the same as it Chris@0: // was in the normal context of execution. Chris@0: chdir(DRUPAL_ROOT); Chris@0: Chris@0: try { Chris@17: reset($callbacks); Chris@17: // Do not use foreach() here because it is possible that the callback will Chris@17: // add to the $callbacks array via drupal_register_shutdown_function(). Chris@17: while ($callback = current($callbacks)) { Chris@0: call_user_func_array($callback['callback'], $callback['arguments']); Chris@17: next($callbacks); Chris@0: } Chris@0: } Chris@0: // PHP 7 introduces Throwable, which covers both Error and Chris@0: // Exception throwables. Chris@0: catch (\Throwable $error) { Chris@0: _drupal_shutdown_function_handle_exception($error); Chris@0: } Chris@0: // In order to be compatible with PHP 5 we also catch regular Exceptions. Chris@0: catch (\Exception $exception) { Chris@0: _drupal_shutdown_function_handle_exception($exception); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Displays and logs any errors that may happen during shutdown. Chris@0: * Chris@0: * @param \Exception|\Throwable $exception Chris@0: * The exception object that was thrown. Chris@0: * Chris@0: * @see _drupal_shutdown_function() Chris@0: */ Chris@0: function _drupal_shutdown_function_handle_exception($exception) { Chris@0: // If using PHP-FPM then fastcgi_finish_request() will have been fired Chris@0: // preventing further output to the browser. Chris@0: if (!function_exists('fastcgi_finish_request')) { Chris@0: // If we are displaying errors, then do so with no possibility of a Chris@0: // further uncaught exception being thrown. Chris@0: require_once __DIR__ . '/errors.inc'; Chris@0: if (error_displayable()) { Chris@0: print '

Uncaught exception thrown in shutdown function.

'; Chris@0: print '

' . Error::renderExceptionSafe($exception) . '


'; Chris@0: } Chris@0: } Chris@0: error_log($exception); Chris@0: }