|
| 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 | +defined('MOODLE_INTERNAL') || die(); |
| 20 | + |
| 21 | +/** |
| 22 | + * Self-update mechanism: check GitHub for new releases and install in-place. |
| 23 | + * |
| 24 | + * @package local_ai_course_assistant |
| 25 | + * @copyright 2025 AI Course Assistant |
| 26 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 27 | + */ |
| 28 | +class plugin_updater { |
| 29 | + |
| 30 | + /** GitHub API endpoint for latest release. */ |
| 31 | + const GITHUB_API = 'https://api.github.com/repos/saylordotorg/sola-moodle-plugin/releases/latest'; |
| 32 | + |
| 33 | + /** |
| 34 | + * Get the current installed version info. |
| 35 | + * |
| 36 | + * @return object {version, release, component} |
| 37 | + */ |
| 38 | + public static function get_current_version(): object { |
| 39 | + global $CFG; |
| 40 | + $plugin = new \stdClass(); |
| 41 | + include($CFG->dirroot . '/local/ai_course_assistant/version.php'); |
| 42 | + return $plugin; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Check GitHub for the latest release. |
| 47 | + * |
| 48 | + * @return object|null {tag, version, changelog, zip_url, current_version, update_available} |
| 49 | + * or null on failure. |
| 50 | + */ |
| 51 | + public static function check_for_update(): ?object { |
| 52 | + $token = get_config('local_ai_course_assistant', 'github_token'); |
| 53 | + |
| 54 | + $headers = [ |
| 55 | + 'Accept: application/vnd.github+json', |
| 56 | + 'User-Agent: SOLA-Moodle-Plugin/1.0', |
| 57 | + ]; |
| 58 | + if (!empty($token)) { |
| 59 | + $headers[] = 'Authorization: Bearer ' . $token; |
| 60 | + } |
| 61 | + |
| 62 | + $ch = curl_init(self::GITHUB_API); |
| 63 | + curl_setopt_array($ch, [ |
| 64 | + CURLOPT_RETURNTRANSFER => true, |
| 65 | + CURLOPT_FOLLOWLOCATION => true, |
| 66 | + CURLOPT_TIMEOUT => 15, |
| 67 | + CURLOPT_HTTPHEADER => $headers, |
| 68 | + ]); |
| 69 | + $body = curl_exec($ch); |
| 70 | + $code = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE); |
| 71 | + curl_close($ch); |
| 72 | + |
| 73 | + if ($code !== 200 || !$body) { |
| 74 | + return null; |
| 75 | + } |
| 76 | + |
| 77 | + $data = json_decode($body); |
| 78 | + if (!$data || empty($data->tag_name)) { |
| 79 | + return null; |
| 80 | + } |
| 81 | + |
| 82 | + // Find the zipball URL (source code zip from GitHub). |
| 83 | + $zipurl = $data->zipball_url ?? ''; |
| 84 | + |
| 85 | + // Also check for a release asset named ai_course_assistant.zip. |
| 86 | + if (!empty($data->assets) && is_array($data->assets)) { |
| 87 | + foreach ($data->assets as $asset) { |
| 88 | + if (strpos($asset->name, 'ai_course_assistant') !== false |
| 89 | + || strpos($asset->name, '.zip') !== false) { |
| 90 | + $zipurl = $asset->browser_download_url; |
| 91 | + break; |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + $current = self::get_current_version(); |
| 97 | + $latestversion = ltrim($data->tag_name, 'v'); |
| 98 | + |
| 99 | + $result = new \stdClass(); |
| 100 | + $result->tag = $data->tag_name; |
| 101 | + $result->version = $latestversion; |
| 102 | + $result->changelog = $data->body ?? ''; |
| 103 | + $result->zip_url = $zipurl; |
| 104 | + $result->current_version = $current->release; |
| 105 | + $result->update_available = version_compare($latestversion, $current->release, '>'); |
| 106 | + $result->published_at = $data->published_at ?? ''; |
| 107 | + |
| 108 | + return $result; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Download a release zip to Moodle's temp directory. |
| 113 | + * |
| 114 | + * @param string $zipurl URL to download. |
| 115 | + * @return string Path to downloaded zip, or empty string on failure. |
| 116 | + */ |
| 117 | + public static function download_release(string $zipurl): string { |
| 118 | + global $CFG; |
| 119 | + |
| 120 | + // Security: only download from github.com. |
| 121 | + if (strpos($zipurl, 'https://github.com/') !== 0 |
| 122 | + && strpos($zipurl, 'https://api.github.com/') !== 0 |
| 123 | + && strpos($zipurl, 'https://codeload.github.com/') !== 0) { |
| 124 | + return ''; |
| 125 | + } |
| 126 | + |
| 127 | + $tempdir = $CFG->tempdir . '/sola_update'; |
| 128 | + if (!is_dir($tempdir)) { |
| 129 | + mkdir($tempdir, 0755, true); |
| 130 | + } |
| 131 | + $zippath = $tempdir . '/sola_release.zip'; |
| 132 | + |
| 133 | + $token = get_config('local_ai_course_assistant', 'github_token'); |
| 134 | + $headers = ['User-Agent: SOLA-Moodle-Plugin/1.0']; |
| 135 | + if (!empty($token)) { |
| 136 | + $headers[] = 'Authorization: Bearer ' . $token; |
| 137 | + } |
| 138 | + |
| 139 | + $ch = curl_init($zipurl); |
| 140 | + $fp = fopen($zippath, 'w'); |
| 141 | + curl_setopt_array($ch, [ |
| 142 | + CURLOPT_FILE => $fp, |
| 143 | + CURLOPT_FOLLOWLOCATION => true, |
| 144 | + CURLOPT_MAXREDIRS => 5, |
| 145 | + CURLOPT_TIMEOUT => 120, |
| 146 | + CURLOPT_HTTPHEADER => $headers, |
| 147 | + ]); |
| 148 | + curl_exec($ch); |
| 149 | + $code = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE); |
| 150 | + curl_close($ch); |
| 151 | + fclose($fp); |
| 152 | + |
| 153 | + if ($code !== 200 || !file_exists($zippath) || filesize($zippath) < 1000) { |
| 154 | + @unlink($zippath); |
| 155 | + return ''; |
| 156 | + } |
| 157 | + |
| 158 | + return $zippath; |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Install an update from a downloaded zip file. |
| 163 | + * |
| 164 | + * @param string $zippath Path to the release zip. |
| 165 | + * @return array ['success' => bool, 'message' => string] |
| 166 | + */ |
| 167 | + public static function install_update(string $zippath): array { |
| 168 | + global $CFG; |
| 169 | + |
| 170 | + $plugindir = $CFG->dirroot . '/local/ai_course_assistant'; |
| 171 | + $tempdir = $CFG->tempdir . '/sola_update'; |
| 172 | + |
| 173 | + // Pre-flight: check write permissions. |
| 174 | + if (!is_writable($plugindir)) { |
| 175 | + return ['success' => false, 'message' => 'Plugin directory is not writable by the web server.']; |
| 176 | + } |
| 177 | + if (!is_writable(dirname($plugindir))) { |
| 178 | + return ['success' => false, 'message' => 'Parent directory (local/) is not writable.']; |
| 179 | + } |
| 180 | + |
| 181 | + // Extract the zip. |
| 182 | + $extractdir = $tempdir . '/extracted'; |
| 183 | + if (is_dir($extractdir)) { |
| 184 | + self::rmdir_recursive($extractdir); |
| 185 | + } |
| 186 | + mkdir($extractdir, 0755, true); |
| 187 | + |
| 188 | + $zip = new \ZipArchive(); |
| 189 | + if ($zip->open($zippath) !== true) { |
| 190 | + return ['success' => false, 'message' => 'Failed to open the downloaded zip file.']; |
| 191 | + } |
| 192 | + $zip->extractTo($extractdir); |
| 193 | + $zip->close(); |
| 194 | + |
| 195 | + // Find the plugin directory inside the extract. |
| 196 | + // GitHub zipball nests files under a random directory name. |
| 197 | + $sourcedir = self::find_plugin_root($extractdir); |
| 198 | + if (!$sourcedir) { |
| 199 | + self::rmdir_recursive($extractdir); |
| 200 | + return ['success' => false, 'message' => 'Could not find ai_course_assistant directory in the zip.']; |
| 201 | + } |
| 202 | + |
| 203 | + // Validate: the extracted dir must contain version.php. |
| 204 | + if (!file_exists($sourcedir . '/version.php')) { |
| 205 | + self::rmdir_recursive($extractdir); |
| 206 | + return ['success' => false, 'message' => 'Invalid plugin: version.php not found in the extracted files.']; |
| 207 | + } |
| 208 | + |
| 209 | + // Create backup of current plugin. |
| 210 | + $backupdir = $plugindir . '.bak_' . date('Ymd_His'); |
| 211 | + if (!@rename($plugindir, $backupdir)) { |
| 212 | + self::rmdir_recursive($extractdir); |
| 213 | + return ['success' => false, 'message' => 'Failed to create backup of the current plugin.']; |
| 214 | + } |
| 215 | + |
| 216 | + // Move the new version into place. |
| 217 | + if (!@rename($sourcedir, $plugindir)) { |
| 218 | + // Rollback: restore from backup. |
| 219 | + @rename($backupdir, $plugindir); |
| 220 | + self::rmdir_recursive($extractdir); |
| 221 | + return ['success' => false, 'message' => 'Failed to install new version. Rolled back to previous version.']; |
| 222 | + } |
| 223 | + |
| 224 | + // Cleanup. |
| 225 | + self::rmdir_recursive($extractdir); |
| 226 | + @unlink($zippath); |
| 227 | + |
| 228 | + // Keep only the most recent backup, remove older ones. |
| 229 | + $localdir = dirname($plugindir); |
| 230 | + foreach (scandir($localdir) as $entry) { |
| 231 | + if (strpos($entry, 'ai_course_assistant.bak_') === 0) { |
| 232 | + $oldbackup = $localdir . '/' . $entry; |
| 233 | + if ($oldbackup !== $backupdir) { |
| 234 | + self::rmdir_recursive($oldbackup); |
| 235 | + } |
| 236 | + } |
| 237 | + } |
| 238 | + |
| 239 | + return ['success' => true, 'message' => 'Update installed successfully. Redirecting to complete the upgrade.']; |
| 240 | + } |
| 241 | + |
| 242 | + /** |
| 243 | + * Find the plugin root directory inside an extracted zip. |
| 244 | + * Handles both direct extraction and GitHub's nested directory structure. |
| 245 | + */ |
| 246 | + private static function find_plugin_root(string $dir): ?string { |
| 247 | + // Direct: dir contains version.php. |
| 248 | + if (file_exists($dir . '/version.php')) { |
| 249 | + return $dir; |
| 250 | + } |
| 251 | + |
| 252 | + // Look for ai_course_assistant/ subdirectory. |
| 253 | + foreach (scandir($dir) as $entry) { |
| 254 | + if ($entry === '.' || $entry === '..') { |
| 255 | + continue; |
| 256 | + } |
| 257 | + $subdir = $dir . '/' . $entry; |
| 258 | + if (!is_dir($subdir)) { |
| 259 | + continue; |
| 260 | + } |
| 261 | + |
| 262 | + // Direct plugin directory. |
| 263 | + if ($entry === 'ai_course_assistant' && file_exists($subdir . '/version.php')) { |
| 264 | + return $subdir; |
| 265 | + } |
| 266 | + |
| 267 | + // GitHub zipball: random-named root dir containing ai_course_assistant/. |
| 268 | + $nested = $subdir . '/ai_course_assistant'; |
| 269 | + if (is_dir($nested) && file_exists($nested . '/version.php')) { |
| 270 | + return $nested; |
| 271 | + } |
| 272 | + |
| 273 | + // GitHub zipball: random-named root dir IS the plugin (contains version.php directly). |
| 274 | + if (file_exists($subdir . '/version.php')) { |
| 275 | + return $subdir; |
| 276 | + } |
| 277 | + } |
| 278 | + |
| 279 | + return null; |
| 280 | + } |
| 281 | + |
| 282 | + /** |
| 283 | + * Recursively remove a directory. |
| 284 | + */ |
| 285 | + private static function rmdir_recursive(string $dir): void { |
| 286 | + if (!is_dir($dir)) { |
| 287 | + return; |
| 288 | + } |
| 289 | + foreach (scandir($dir) as $entry) { |
| 290 | + if ($entry === '.' || $entry === '..') { |
| 291 | + continue; |
| 292 | + } |
| 293 | + $path = $dir . '/' . $entry; |
| 294 | + if (is_dir($path)) { |
| 295 | + self::rmdir_recursive($path); |
| 296 | + } else { |
| 297 | + @unlink($path); |
| 298 | + } |
| 299 | + } |
| 300 | + @rmdir($dir); |
| 301 | + } |
| 302 | +} |
0 commit comments