Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * On-the-fly conversion of Moodle lang strings to TinyMCE expected JS format.
  19   *
  20   * @package    editor_tinymce
  21   * @copyright  2009 Petr Skoda (http://skodak.org)
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  define('NO_MOODLE_COOKIES', true);
  26  define('NO_UPGRADE_CHECK', true);
  27  
  28  require('../../../config.php');
  29  require_once("$CFG->dirroot/lib/jslib.php");
  30  require_once("$CFG->dirroot/lib/configonlylib.php");
  31  
  32  $lang  = optional_param('elanguage', 'en', PARAM_SAFEDIR);
  33  $rev   = optional_param('rev', -1, PARAM_INT);
  34  
  35  $PAGE->set_context(context_system::instance());
  36  $PAGE->set_url('/lib/editor/tinymce/extra/strings.php');
  37  
  38  if (!get_string_manager()->translation_exists($lang, false)) {
  39      $lang = 'en';
  40      $rev = -1; // Do not cache missing langs.
  41  }
  42  
  43  $candidate = "$CFG->cachedir/editor_tinymce/$rev/$lang.js";
  44  $etag = sha1("$lang/$rev");
  45  
  46  if ($rev > -1 and file_exists($candidate)) {
  47      if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
  48          // we do not actually need to verify the etag value because our files
  49          // never change in cache because we increment the rev parameter
  50          js_send_unmodified(filemtime($candidate), $etag);
  51      }
  52      js_send_cached($candidate, $etag, 'all_strings.php');
  53  }
  54  
  55  $string = get_string_manager()->load_component_strings('editor_tinymce', $lang);
  56  
  57  // Process the $strings to match expected tinymce lang array structure.
  58  $result = array();
  59  
  60  foreach ($string as $key=>$value) {
  61      $parts = explode(':', $key);
  62      if (count($parts) != 2) {
  63          // Ignore non-TinyMCE strings.
  64          continue;
  65      }
  66  
  67      $result[$parts[0]][$parts[1]] = $value;
  68  }
  69  
  70  // Add subplugin strings, accept only those with proper pluginname prefix with colon.
  71  foreach (core_component::get_plugin_list('tinymce') as $component => $ignored) {
  72      $componentstrings = get_string_manager()->load_component_strings(
  73              'tinymce_' . $component, $lang);
  74      foreach ($componentstrings as $key => $value) {
  75          if (strpos($key, "$component:") !== 0 and strpos($key, $component.'_dlg:') !== 0) {
  76              // Ignore normal lang strings.
  77              continue;
  78          }
  79          $parts = explode(':', $key);
  80          if (count($parts) != 2) {
  81              // Ignore malformed strings with more colons.
  82              continue;
  83          }
  84          $component = $parts[0];
  85          $string = $parts[1];
  86          $result[$component][$string] = $value;
  87      }
  88  }
  89  
  90  $output = 'tinyMCE.addI18n({'.$lang.':'.json_encode($result).'});';
  91  
  92  if ($rev > -1) {
  93      js_write_cache_file_content($candidate, $output);
  94      // verify nothing failed in cache file creation
  95      clearstatcache();
  96      if (file_exists($candidate)) {
  97          js_send_cached($candidate, $etag, 'all_strings.php');
  98      }
  99  }
 100  
 101  js_send_uncached($output, 'all_strings.php');