Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are 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   * Class with static back-end methods used by the file type tool.
  19   *
  20   * @package tool_filetypes
  21   * @copyright 2014 The Open University
  22   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace tool_filetypes;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Class with static back-end methods used by the file type tool.
  31   *
  32   * @package tool_filetypes
  33   * @copyright 2014 The Open University
  34   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class utils {
  37      /**
  38       * Checks if the given file type extension is invalid.
  39       * The added file type extension must be unique and must not begin with a dot.
  40       *
  41       * @param string $extension Extension of the file type to add
  42       * @param string $oldextension Extension prior to update (empty string if adding new type)
  43       * @return bool True if it the file type trying to add already exists
  44       */
  45      public static function is_extension_invalid($extension, $oldextension = '') {
  46          $extension = trim($extension);
  47          if ($extension === '' || $extension[0] === '.') {
  48              return true;
  49          }
  50  
  51          $mimeinfo = get_mimetypes_array();
  52          if ($oldextension !== '') {
  53              unset($mimeinfo[$oldextension]);
  54          }
  55  
  56          return array_key_exists($extension, $mimeinfo);
  57      }
  58  
  59      /**
  60       * Checks if we are allowed to turn on the 'default icon' option. You can
  61       * only have one of these for a given MIME type.
  62       *
  63       * @param string $mimetype MIME type
  64       * @param string $oldextension File extension name (before any change)
  65       */
  66      public static function is_defaulticon_allowed($mimetype, $oldextension = '') {
  67          $mimeinfo = get_mimetypes_array();
  68          if ($oldextension !== '') {
  69              unset($mimeinfo[$oldextension]);
  70          }
  71          foreach ($mimeinfo as $extension => $values) {
  72              if ($values['type'] !== $mimetype) {
  73                  continue;
  74              }
  75              if (!empty($values['defaulticon'])) {
  76                  return false;
  77              }
  78          }
  79          return true;
  80      }
  81  
  82      /**
  83       * Gets all unique file type icons from a specific path, not including
  84       * sub-directories.
  85       *
  86       * Icon files such as pdf.png, pdf-24.png and pdf-36.png etc. are counted as
  87       * the same icon type.
  88       *
  89       * The resultant array has both key and value set to the icon name prefix,
  90       * such as 'pdf' => 'pdf'.
  91       *
  92       * @param string $path The path of the icon path
  93       * @return array An array of unique file icons within the given path
  94       */
  95      public static function get_icons_from_path($path) {
  96          $icons = array();
  97          if ($handle = @opendir($path)) {
  98              while (($file = readdir($handle)) !== false) {
  99                  $matches = array();
 100                  if (preg_match('~(.+?)(?:-24|-32|-48|-64|-72|-80|-96|-128|-256)?\.(?:svg|gif|png)$~',
 101                          $file, $matches)) {
 102                      $key = $matches[1];
 103                      $icons[$key] = $key;
 104                  }
 105              }
 106              closedir($handle);
 107          }
 108          ksort($icons);
 109          return $icons;
 110      }
 111  
 112      /**
 113       * Gets unique file type icons from pix/f folder.
 114       *
 115       * @return array An array of unique file type icons e.g. 'pdf' => 'pdf'
 116       */
 117      public static function get_file_icons() {
 118          global $CFG;
 119          $path = $CFG->dirroot . '/pix/f';
 120          return self::get_icons_from_path($path);
 121      }
 122  }