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   * ClamAV antivirus adminlib.
  19   *
  20   * @package    antivirus_clamav
  21   * @copyright  2015 Ruslan Kabalin, Lancaster University.
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * Admin setting for running, adds verification.
  29   *
  30   * @package    antivirus_clamav
  31   * @copyright  2015 Ruslan Kabalin, Lancaster University.
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class antivirus_clamav_runningmethod_setting extends admin_setting_configselect {
  35      /**
  36       * Save a setting
  37       *
  38       * @param string $data
  39       * @return string empty or error string
  40       */
  41      public function write_setting($data) {
  42          $validated = $this->validate($data);
  43          if ($validated !== true) {
  44              return $validated;
  45          }
  46          return parent::write_setting($data);
  47      }
  48  
  49      /**
  50       * Validate data.
  51       *
  52       * This ensures that the selected socket transport is supported by this system.
  53       *
  54       * @param string $data
  55       * @return mixed True on success, else error message.
  56       */
  57      public function validate($data) {
  58          $supportedtransports = stream_get_transports();
  59          if ($data === 'unixsocket') {
  60              if (array_search('unix', $supportedtransports) === false) {
  61                  return get_string('errornounixsocketssupported', 'antivirus_clamav');
  62              }
  63          } else if ($data === 'tcpsocket') {
  64              if (array_search('tcp', $supportedtransports) === false) {
  65                  return get_string('errornotcpsocketssupported', 'antivirus_clamav');
  66              }
  67          }
  68          return true;
  69      }
  70  }
  71  
  72  
  73  /**
  74   * Abstract socket checking class
  75   *
  76   * @package    antivirus_clamav
  77   * @copyright  2015 Ruslan Kabalin, Lancaster University.
  78   * @copyright  2019 Didier Raboud, Liip AG.
  79   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  80   */
  81  class antivirus_clamav_socket_setting extends admin_setting_configtext {
  82      /**
  83       * Ping ClamAV socket.
  84       *
  85       * This ensures that a socket setting is correct and that ClamAV is running.
  86       *
  87       * @param string $socketaddress Address to the socket to connect to (for stream_socket_client)
  88       * @return mixed True on success, else error message.
  89       */
  90      protected function validate_clamav_socket($socketaddress) {
  91          $socket = stream_socket_client($socketaddress, $errno, $errstr, ANTIVIRUS_CLAMAV_SOCKET_TIMEOUT);
  92          if (!$socket) {
  93              return get_string('errorcantopensocket', 'antivirus_clamav', "$errstr ($errno)");
  94          } else {
  95              // Send PING query to ClamAV socket to check its running state.
  96              fwrite($socket, "nPING\n");
  97              $response = stream_get_line($socket, 4);
  98              fclose($socket);
  99              if ($response !== 'PONG') {
 100                  return get_string('errorclamavnoresponse', 'antivirus_clamav');
 101              }
 102          }
 103          return true;
 104      }
 105  }
 106  /**
 107   * Admin setting for unix socket path, adds verification.
 108   *
 109   * @package    antivirus_clamav
 110   * @copyright  2015 Ruslan Kabalin, Lancaster University.
 111   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 112   */
 113  class antivirus_clamav_pathtounixsocket_setting extends antivirus_clamav_socket_setting {
 114      /**
 115       * Validate data.
 116       *
 117       * This ensures that unix socket setting is correct and ClamAV is running.
 118       *
 119       * @param string $data
 120       * @return mixed True on success, else error message.
 121       */
 122      public function validate($data) {
 123          $result = parent::validate($data);
 124          if ($result !== true) {
 125              return $result;
 126          }
 127          $runningmethod = get_config('antivirus_clamav', 'runningmethod');
 128          if ($runningmethod === 'unixsocket') {
 129              return $this->validate_clamav_socket('unix://' . $data);
 130          }
 131          return true;
 132      }
 133  }
 134  
 135  /**
 136   * Admin setting for Internet domain socket host, adds verification.
 137   *
 138   * @package    antivirus_clamav
 139   * @copyright  2019 Didier Raboud, Liip AG.
 140   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 141   */
 142  class antivirus_clamav_tcpsockethost_setting extends antivirus_clamav_socket_setting {
 143      /**
 144       * Validate data.
 145       *
 146       * This ensures that Internet domain socket setting is correct and ClamAV is running.
 147       *
 148       * @param string $data
 149       * @return mixed True on success, else error message.
 150       */
 151      public function validate($data) {
 152          $result = parent::validate($data);
 153          if ($result !== true) {
 154              return $result;
 155          }
 156          $runningmethod = get_config('antivirus_clamav', 'runningmethod');
 157          $tcpport = get_config('antivirus_clamav', 'tcpsocketport');
 158          if ($tcpport === false) {
 159              $tcpport = 3310;
 160          }
 161          if ($runningmethod === 'tcpsocket') {
 162              return $this->validate_clamav_socket('tcp://' . $data . ':' . $tcpport);
 163          }
 164          return true;
 165      }
 166  }