Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
83.33% covered (warning)
83.33%
35 / 42
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
installer
83.33% covered (warning)
83.33%
35 / 42
50.00% covered (danger)
50.00%
2 / 4
20.67
0.00% covered (danger)
0.00%
0 / 1
 can_install
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 is_present
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 install
81.25% covered (warning)
81.25%
26 / 32
0.00% covered (danger)
0.00%
0 / 1
16.48
 is_component
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
2.02
1<?php
2// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
16
17namespace message_notifyhub\local;
18
19/**
20 * Installs a channel plugin from its signed GitHub release, with the same checks as the DevLMS payment hub.
21 *
22 * Nothing is unpacked unless the zip is exactly the file GitHub published for the newest release (SHA-256), it is
23 * signed with the DevLMS release key (Ed25519), and its version.php declares the expected component. Moodle's
24 * upgrade screen then finishes the install.
25 *
26 * @package    message_notifyhub
27 * @copyright  2026 DevLMS
28 * @license    https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 */
30final class installer {
31    /**
32     * Whether this site can install a component from the web: its plugin type folder must be writable.
33     *
34     * @param string $component
35     * @return bool
36     */
37    public static function can_install(string $component): bool {
38        [$type] = \core_component::normalize_component($component);
39        return \core_plugin_manager::instance()->is_plugintype_writable($type);
40    }
41
42    /**
43     * Whether a component is installed (its code is on the site).
44     *
45     * @param string $component
46     * @return bool
47     */
48    public static function is_present(string $component): bool {
49        [$type, $name] = \core_component::normalize_component($component);
50        return array_key_exists($name, \core_component::get_plugin_list($type));
51    }
52
53    /**
54     * Download, check and unpack one channel.
55     *
56     * @param string $component Catalogue key.
57     * @param callable|null $fetch For tests: url => file content.
58     */
59    public static function install(string $component, ?callable $fetch = null): void {
60        global $CFG;
61        require_once($CFG->libdir . '/filelib.php');
62        require_capability('moodle/site:config', \context_system::instance());
63        $entry = catalogue::get($component);
64        if (!$entry) {
65            throw new \moodle_exception('error:unknownchannel', 'message_notifyhub', '', $component);
66        }
67        if (self::is_present($component)) {
68            return;
69        }
70        if (!self::can_install($component)) {
71            throw new \moodle_exception('error:notwritable', 'message_notifyhub');
72        }
73        $release = releases::latest($component, $entry['repo'], true);
74        if (!$release) {
75            throw new \moodle_exception('error:norelease', 'message_notifyhub', '', $entry['repo']);
76        }
77        $zip = $fetch ? $fetch($release['url']) : download_file_content($release['url'], null, null, false, 120, 20);
78        if (!is_string($zip) || $zip === '') {
79            throw new \moodle_exception('error:download', 'message_notifyhub', '', $release['url']);
80        }
81        if (!hash_equals($release['sha256'], hash('sha256', $zip))) {
82            throw new \moodle_exception('error:checksum', 'message_notifyhub', '', $component);
83        }
84        $sig = $fetch ? $fetch($release['sigurl']) : download_file_content($release['sigurl'], null, null, false, 60, 20);
85        if (!is_string($sig) || !signature::valid($zip, $sig)) {
86            throw new \moodle_exception('error:signature', 'message_notifyhub', '', $component);
87        }
88        [$type, $name] = \core_component::normalize_component($component);
89        $path = make_request_directory() . '/' . $name . '.zip';
90        file_put_contents($path, $zip);
91        $manager = new \core\update\code_manager();
92        $root = $manager->get_plugin_zip_root_dir($path);
93        if ($root === false || !self::is_component($path, $root, $component)) {
94            throw new \moodle_exception('error:wrongplugin', 'message_notifyhub', '', $component);
95        }
96        $target = \core_plugin_manager::instance()->get_plugintype_root($type);
97        if (!$manager->unzip_plugin_file($path, $target, $name)) {
98            throw new \moodle_exception('error:download', 'message_notifyhub', '', $release['url']);
99        }
100        \core_component::reset();
101        \core_plugin_manager::reset_caches();
102    }
103
104    /**
105     * Whether the zip's version.php declares the expected component.
106     *
107     * @param string $path
108     * @param string $root Root folder inside the zip.
109     * @param string $component
110     * @return bool
111     */
112    private static function is_component(string $path, string $root, string $component): bool {
113        $zip = new \ZipArchive();
114        if ($zip->open($path) !== true) {
115            return false;
116        }
117        $version = (string) $zip->getFromName($root . '/version.php');
118        $zip->close();
119        return (bool) preg_match('/\\$plugin->component\s*=\s*[\'"]' . preg_quote($component, '/') . '[\'"]\s*;/', $version);
120    }
121}