MDL-60425 blog: Remove empty line
[moodle.git] / lib / antivirus / clamav / adminlib.php
blobf473cea3198e2e8224e46c55b16323f613411a94
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.
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/>.
17 /**
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();
27 /**
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 {
35 /**
36 * Save a setting
38 * @param string $data
39 * @return string empty or error string
41 public function write_setting($data) {
42 $validated = $this->validate($data);
43 if ($validated !== true) {
44 return $validated;
46 return parent::write_setting($data);
49 /**
50 * Validate data.
52 * This ensures that unix socket transport is supported by this system.
54 * @param string $data
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');
64 return true;
67 /**
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 {
75 /**
76 * Validate data.
78 * This ensures that unix socket setting is correct and ClamAV is running.
80 * @param string $data
81 * @return mixed True on success, else error message.
83 public function validate($data) {
84 $result = parent::validate($data);
85 if ($result !== true) {
86 return $result;
88 $runningmethod = get_config('antivirus_clamav', 'runningmethod');
89 if ($runningmethod === 'unixsocket') {
90 $socket = stream_socket_client('unix://' . $data, $errno, $errstr, ANTIVIRUS_CLAMAV_SOCKET_TIMEOUT);
91 if (!$socket) {
92 return get_string('errorcantopensocket', 'antivirus_clamav', "$errstr ($errno)");
93 } else {
94 // Send PING query to ClamAV socket to check its running state.
95 fwrite($socket, "nPING\n");
96 $response = stream_get_line($socket, 4);
97 fclose($socket);
98 if ($response !== 'PONG') {
99 return get_string('errorclamavnoresponse', 'antivirus_clamav');
103 return true;