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 '
' . Error::renderExceptionSafe($exception) . '
'; Chris@0: print '' . Error::renderExceptionSafe($exception2) . '
' . Error::renderExceptionSafe($exception) . '