Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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.

Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   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   * Base class for antivirus integration.
  19   *
  20   * @package    core_antivirus
  21   * @copyright  2015 Ruslan Kabalin, Lancaster University.
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core\antivirus;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Base abstract antivirus scanner class.
  31   *
  32   * @package    core
  33   * @subpackage antivirus
  34   * @copyright  2015 Ruslan Kabalin, Lancaster University.
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  abstract class scanner {
  38      /** Scanning result indicating no virus found. */
  39      const SCAN_RESULT_OK = 0;
  40      /** Scanning result indicating that virus is found. */
  41      const SCAN_RESULT_FOUND = 1;
  42      /** Scanning result indicating the error. */
  43      const SCAN_RESULT_ERROR = 2;
  44  
  45      /** @var stdClass the config for antivirus */
  46      protected $config;
  47      /** @var string scanning notice */
  48      protected $scanningnotice = '';
  49  
  50      /**
  51       * Class constructor.
  52       *
  53       * @return void.
  54       */
  55      public function __construct() {
  56          // Populate config variable, inheriting class namespace is matching
  57          // full plugin name, so we can use it directly to retrieve plugin
  58          // configuration.
  59          $ref = new \ReflectionClass(get_class($this));
  60          $this->config = get_config($ref->getNamespaceName());
  61      }
  62  
  63      /**
  64       * Config get method.
  65       *
  66       * @param string $property config property to get.
  67       * @return mixed
  68       * @throws \coding_exception
  69       */
  70      public function get_config($property) {
  71          if (property_exists($this->config, $property)) {
  72              return $this->config->$property;
  73          }
  74          throw new \coding_exception('Config property "' . $property . '" doesn\'t exist');
  75      }
  76  
  77      /**
  78       * Get scanning notice.
  79       *
  80       * @return string
  81       */
  82      public function get_scanning_notice() {
  83          return $this->scanningnotice;
  84      }
  85  
  86      /**
  87       * Set scanning notice.
  88       *
  89       * @param string $notice notice to set.
  90       * @return void
  91       */
  92      protected function set_scanning_notice($notice) {
  93          $this->scanningnotice = $notice;
  94      }
  95  
  96      /**
  97       * Are the antivirus settings configured?
  98       *
  99       * @return bool True if plugin has been configured.
 100       */
 101      public abstract function is_configured();
 102  
 103      /**
 104       * Scan file.
 105       *
 106       * @param string $file Full path to the file.
 107       * @param string $filename Name of the file (could be different from physical file if temp file is used).
 108       * @return int Scanning result constants.
 109       */
 110      public abstract function scan_file($file, $filename);
 111  
 112      /**
 113       * Scan data.
 114       *
 115       * By default it saves data variable content to file and then scans it using
 116       * scan_file method, however if antivirus plugin permits scanning data directly,
 117       * the method can be overridden.
 118       *
 119       * @param string $data The variable containing the data to scan.
 120       * @return int Scanning result constants.
 121       */
 122      public function scan_data($data) {
 123          // Prepare temp file.
 124          $tempdir = make_request_directory();
 125          $tempfile = $tempdir . DIRECTORY_SEPARATOR . rand();
 126          file_put_contents($tempfile, $data);
 127  
 128          // Perform a virus scan now.
 129          return $this->scan_file($tempfile, get_string('datastream', 'antivirus'));
 130      }
 131  
 132      /**
 133       * Email admins about antivirus scan outcomes.
 134       *
 135       * @param string $notice The body of the email to be sent.
 136       * @return void
 137       */
 138      public function message_admins($notice) {
 139  
 140          $site = get_site();
 141  
 142          $subject = get_string('emailsubject', 'antivirus', format_string($site->fullname));
 143          $admins = get_admins();
 144          foreach ($admins as $admin) {
 145              $eventdata = new \core\message\message();
 146              $eventdata->courseid          = SITEID;
 147              $eventdata->component         = 'moodle';
 148              $eventdata->name              = 'errors';
 149              $eventdata->userfrom          = get_admin();
 150              $eventdata->userto            = $admin;
 151              $eventdata->subject           = $subject;
 152              $eventdata->fullmessage       = $notice;
 153              $eventdata->fullmessageformat = FORMAT_PLAIN;
 154              $eventdata->fullmessagehtml   = '';
 155              $eventdata->smallmessage      = '';
 156              message_send($eventdata);
 157          }
 158      }
 159  }