Unverified Commit 7521d545 authored by cilefen's avatar cilefen Committed by Chris McCafferty
Browse files

chore: CDN setup

parent a8f5371e
Loading
Loading
Loading
Loading
Loading
+1 −1
Changes for config/install/mathjax.settings.yml: 1 added line, 1 removed line.
Original line number Diff line number Diff line
use_cdn: 1
cdn_url: 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.9/MathJax.js?config=TeX-AMS-MML_HTMLorMML'
cdn_url: 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.9/MathJax.js?config=TeX-AMS-MML_HTMLorMML,Safe'
config_type: 0
default_config_string: '{"tex2jax":{"inlineMath":[["$","$"],["\\(","\\)"]],"processEscapes":"true"},"showProcessingMessages":"false","messageStyle":"none"}'
enable_for_admin: 0
+50 −2
Changes for mathjax.install: 50 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -13,7 +13,8 @@ function mathjax_requirements($phase) {

  if ($phase == 'runtime') {
    $config = Drupal::config('mathjax.settings');
    if ($config->get('use_cdn') && preg_match('#(?:/|@)(2\.7\.[0-8])/#', parse_url($config->get('cdn_url'), PHP_URL_PATH) ?? '', $matches)) {
    if ($config->get('use_cdn')) {
      if (preg_match('#(?:/|@)(2\.7\.[0-8])/#', parse_url($config->get('cdn_url'), PHP_URL_PATH) ?? '', $matches)) {
        $requirements['mathjax_cdn_version'] = [
          'title' => t('MathJax'),
          'value' => t('Insecure CDN version (@version)', ['@version' => $matches[1]]),
@@ -21,7 +22,24 @@ function mathjax_requirements($phase) {
          'severity' => REQUIREMENT_WARNING,
        ];
      }
    if (!$config->get('use_cdn')) {
      parse_str((string) parse_url($config->get('cdn_url'), PHP_URL_QUERY), $query);
      $configurations = $query['config'] ?? '';
      $safe = is_string($configurations) && in_array('Safe', explode(',', $configurations), TRUE);
      if ($config->get('config_type') == 1) {
        $custom_config = json_decode((string) $config->get('config_string'), TRUE);
        $extensions = $custom_config['extensions'] ?? [];
        $safe = $safe || (is_array($extensions) && in_array('Safe.js', $extensions, TRUE));
      }
      if (!$safe) {
        $requirements['mathjax_safe_mode'] = [
          'title' => t('MathJax'),
          'value' => t('Safe mode is not enabled'),
          'description' => t('Add Safe to the comma-separated config query parameter in the MathJax CDN URL, or add Safe.js to the extensions array in the custom configuration. Safe mode restricts potentially unsafe content in user-supplied mathematics. See the <a href=":url">MathJax Safe-mode documentation</a>.', [':url' => 'https://docs.mathjax.org/en/v2.7/safe-mode.html']),
          'severity' => REQUIREMENT_WARNING,
        ];
      }
    }
    else {
      if (!_mathjax_library_present()) {
        $requirements['mathjax_local_libraries'] = [
          'title' => t('MathJax'),
@@ -82,3 +100,33 @@ function mathjax_update_8203() {
    $config->set('cdn_url', $updated_url)->save(TRUE);
  }
}

/**
 * Enable Safe mode in existing MathJax CDN URLs.
 */
function mathjax_update_8204() {
  $config = \Drupal::configFactory()->getEditable('mathjax.settings');
  $url = (string) $config->get('cdn_url');
  if ($url === '') {
    return;
  }

  $parts = explode('#', $url, 2);
  $url_parts = explode('?', $parts[0], 2);
  $query = preg_replace_callback('/(^|&)config=([^&]*)/', function ($matches) {
    if (in_array('Safe', explode(',', urldecode($matches[2])), TRUE)) {
      return $matches[0];
    }
    return $matches[0] . ($matches[2] === '' ? 'Safe' : ',Safe');
  }, $url_parts[1] ?? '', -1, $count);
  if (!$count) {
    $query .= ($query === '' ? '' : '&') . 'config=Safe';
  }
  $updated_url = $url_parts[0] . '?' . $query;
  if (isset($parts[1])) {
    $updated_url .= '#' . $parts[1];
  }
  if ($updated_url !== $url) {
    $config->set('cdn_url', $updated_url)->save(TRUE);
  }
}
+1 −1
Changes for mathjax.module: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -62,7 +62,7 @@ function mathjax_library_info_build() {
  ];
  if ($config->get('use_cdn') == FALSE) {
    $libraries['source']['js'] = [
      \Drupal::request()->getBaseUrl() . '/libraries/MathJax/MathJax.js?config=TeX-AMS-MML_HTMLorMML' => [
      \Drupal::request()->getBaseUrl() . '/libraries/MathJax/MathJax.js?config=TeX-AMS-MML_HTMLorMML,Safe' => [
        'type' => 'external',
        'minified' => TRUE,
      ],