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 * Defines classes used for plugin info.
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 */
24namespace core\plugininfo;
25
26use moodle_url, part_of_admin_tree, admin_settingpage;
27
28defined('MOODLE_INTERNAL') || die();
29
30
31/**
32 * Class for Antiviruses
33 *
34 * @package    core_antivirus
35 * @copyright  2015 Ruslan Kabalin, Lancaster University.
36 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 */
38class antivirus extends base {
39    /**
40     * Finds all enabled plugins, the result may include missing plugins.
41     * @return array|null of enabled plugins $pluginname=>$pluginname, null means unknown
42     */
43    public static function get_enabled_plugins() {
44        global $CFG;
45
46        if (empty($CFG->antiviruses)) {
47            return array();
48        }
49
50        $enabled = array();
51        foreach (explode(',', $CFG->antiviruses) as $antivirus) {
52            $enabled[$antivirus] = $antivirus;
53        }
54
55        return $enabled;
56    }
57
58    /**
59     * Return the node name to use in admin settings menu for this plugin.
60     *
61     * @return string node name
62     */
63    public function get_settings_section_name() {
64        return 'antivirussettings' . $this->name;
65    }
66
67    /**
68     * Loads plugin settings to the settings tree
69     *
70     * This function usually includes settings.php file in plugins folder.
71     * Alternatively it can create a link to some settings page (instance of admin_externalpage)
72     *
73     * @param \part_of_admin_tree $adminroot
74     * @param string $parentnodename
75     * @param bool $hassiteconfig whether the current user has moodle/site:config capability
76     */
77    public function load_settings(part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig) {
78        global $CFG, $USER, $DB, $OUTPUT, $PAGE; // In case settings.php wants to refer to them.
79        $ADMIN = $adminroot; // May be used in settings.php.
80        $plugininfo = $this; // Also can be used inside settings.php.
81        $antivirus = $this;  // Also can be used inside settings.php.
82
83        if (!$this->is_installed_and_upgraded()) {
84            return;
85        }
86
87        if (!$hassiteconfig or !file_exists($this->full_path('settings.php'))) {
88            return;
89        }
90
91        $section = $this->get_settings_section_name();
92
93        $settings = new admin_settingpage($section, $this->displayname, 'moodle/site:config', $this->is_enabled() === false);
94        include($this->full_path('settings.php')); // This may also set $settings to null.
95
96        if ($settings) {
97            $ADMIN->add($parentnodename, $settings);
98        }
99    }
100
101    /**
102     * Clamav antivirus can not be uninstalled.
103     */
104    public function is_uninstall_allowed() {
105        if ($this->name === 'clamav') {
106            return false;
107        } else {
108            return true;
109        }
110    }
111
112    /**
113     * Return URL used for management of plugins of this type.
114     * @return moodle_url
115     */
116    public static function get_manage_url() {
117        return new moodle_url('/admin/settings.php', array('section' => 'manageantiviruses'));
118    }
119
120    /**
121     * Pre-uninstall hook.
122     */
123    public function uninstall_cleanup() {
124        global $CFG;
125
126        if (!empty($CFG->antiviruses)) {
127            $antiviruses = explode(',', $CFG->antiviruses);
128            $antiviruses = array_unique($antiviruses);
129        } else {
130            $antiviruses = array();
131        }
132        if (($key = array_search($this->name, $antiviruses)) !== false) {
133            unset($antiviruses[$key]);
134            set_config('antiviruses', implode(',', $antiviruses));
135        }
136        parent::uninstall_cleanup();
137    }
138}
139