|
| 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 | +namespace local_ai_course_assistant; |
| 18 | + |
| 19 | +/** |
| 20 | + * Fetches and caches remote SOLA configuration from a GitHub-hosted JSON file. |
| 21 | + * |
| 22 | + * Allows Saylor to push prompt/config updates without a plugin release. |
| 23 | + * Local admin settings always take priority over remote values. |
| 24 | + * Falls back to empty array (hardcoded defaults) on any failure. |
| 25 | + * |
| 26 | + * @package local_ai_course_assistant |
| 27 | + * @copyright 2025 AI Course Assistant |
| 28 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 29 | + */ |
| 30 | +class remote_config_manager { |
| 31 | + |
| 32 | + /** Default remote config URL (overridable in plugin settings). */ |
| 33 | + const DEFAULT_URL = 'https://raw.githubusercontent.com/saylordotorg/sola-moodle-plugin/main/sola-config.json'; |
| 34 | + |
| 35 | + /** Cache TTL in seconds (1 hour). */ |
| 36 | + const CACHE_TTL = 3600; |
| 37 | + |
| 38 | + /** |
| 39 | + * Return remote config as decoded array. Returns [] on any failure. |
| 40 | + * |
| 41 | + * @return array |
| 42 | + */ |
| 43 | + public static function get(): array { |
| 44 | + $cache = \cache::make('local_ai_course_assistant', 'remoteconfig'); |
| 45 | + $cached = $cache->get('config'); |
| 46 | + if ($cached !== false) { |
| 47 | + return $cached; |
| 48 | + } |
| 49 | + |
| 50 | + $url = get_config('local_ai_course_assistant', 'remoteconfigurl') ?: self::DEFAULT_URL; |
| 51 | + $url = trim($url); |
| 52 | + |
| 53 | + // Security: only allow HTTPS URLs. |
| 54 | + if (!$url || strpos($url, 'https://') !== 0) { |
| 55 | + $cache->set('config', []); |
| 56 | + return []; |
| 57 | + } |
| 58 | + |
| 59 | + $ch = curl_init($url); |
| 60 | + curl_setopt_array($ch, [ |
| 61 | + CURLOPT_RETURNTRANSFER => true, |
| 62 | + CURLOPT_FOLLOWLOCATION => true, |
| 63 | + CURLOPT_MAXREDIRS => 3, |
| 64 | + CURLOPT_TIMEOUT => 10, |
| 65 | + CURLOPT_USERAGENT => 'SOLA-Moodle-Plugin/1.0', |
| 66 | + ]); |
| 67 | + $body = curl_exec($ch); |
| 68 | + $code = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE); |
| 69 | + curl_close($ch); |
| 70 | + |
| 71 | + if ($code !== 200 || !$body) { |
| 72 | + $cache->set('config', []); |
| 73 | + return []; |
| 74 | + } |
| 75 | + |
| 76 | + $data = json_decode($body, true); |
| 77 | + if (!is_array($data)) { |
| 78 | + $cache->set('config', []); |
| 79 | + return []; |
| 80 | + } |
| 81 | + |
| 82 | + $cache->set('config', $data); |
| 83 | + return $data; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Get a single key from remote config with a fallback value. |
| 88 | + * |
| 89 | + * @param string $key Top-level key in the remote config JSON. |
| 90 | + * @param mixed $fallback Value to return if key is absent or fetch failed. |
| 91 | + * @return mixed |
| 92 | + */ |
| 93 | + public static function get_value(string $key, $fallback = null) { |
| 94 | + $config = self::get(); |
| 95 | + return $config[$key] ?? $fallback; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Invalidate the remote config cache (e.g. after saving settings). |
| 100 | + * |
| 101 | + * @return void |
| 102 | + */ |
| 103 | + public static function invalidate(): void { |
| 104 | + \cache::make('local_ai_course_assistant', 'remoteconfig')->delete('config'); |
| 105 | + } |
| 106 | +} |
0 commit comments