2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * ClamAV antivirus adminlib.
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
25 defined('MOODLE_INTERNAL') ||
die();
28 * Admin setting for running, adds verification.
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
34 class antivirus_clamav_runningmethod_setting
extends admin_setting_configselect
{
39 * @return string empty or error string
41 public function write_setting($data) {
42 $validated = $this->validate($data);
43 if ($validated !== true) {
46 return parent
::write_setting($data);
52 * This ensures that unix socket transport is supported by this system.
55 * @return mixed True on success, else error message.
57 public function validate($data) {
58 if ($data === 'unixsocket') {
59 $supportedtransports = stream_get_transports();
60 if (!array_search('unix', $supportedtransports)) {
61 return get_string('errornounixsocketssupported', 'antivirus_clamav');
68 * Admin setting for unix socket path, adds verification.
70 * @package antivirus_clamav
71 * @copyright 2015 Ruslan Kabalin, Lancaster University.
72 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
74 class antivirus_clamav_pathtounixsocket_setting
extends admin_setting_configtext
{
78 * This ensures that unix socket setting is correct and ClamAV is running.
81 * @return mixed True on success, else error message.
83 public function validate($data) {
84 $result = parent
::validate($data);
85 if ($result !== true) {
88 $runningmethod = get_config('antivirus_clamav', 'runningmethod');
89 if ($runningmethod === 'unixsocket') {
90 $socket = stream_socket_client('unix://' . $data, $errno, $errstr, ANTIVIRUS_CLAMAV_SOCKET_TIMEOUT
);
92 return get_string('errorcantopensocket', 'antivirus_clamav', "$errstr ($errno)");
94 // Send PING query to ClamAV socket to check its running state.
95 fwrite($socket, "nPING\n");
96 $response = stream_get_line($socket, 4);
98 if ($response !== 'PONG') {
99 return get_string('errorclamavnoresponse', 'antivirus_clamav');