|
| 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 auth_saml2\local; |
| 18 | + |
| 19 | +use core\url as moodle_url; |
| 20 | +use core\context\system; |
| 21 | +use file_exception; |
| 22 | + |
| 23 | +/** |
| 24 | + * Class idp_logo_cache |
| 25 | + * |
| 26 | + * @package auth_saml2 |
| 27 | + * @copyright 2026 University of Graz |
| 28 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 29 | + */ |
| 30 | +class idp_logo_cache { |
| 31 | + const COMPONENT = 'auth_saml2'; |
| 32 | + const FILEAREA = 'idplogo'; |
| 33 | + const FILEPATH = '/'; |
| 34 | + |
| 35 | + /** |
| 36 | + * Cache remote idp icon to local plugin filearea. |
| 37 | + * |
| 38 | + * @param string $remoteurl The remote origin URL of the icon/logo to cache. |
| 39 | + * @param int $itemid The id of the idp in the auth_saml2_idps table. |
| 40 | + * @return \stored_file|false |
| 41 | + */ |
| 42 | + public static function cache_logo($remoteurl, $itemid) { |
| 43 | + $fs = get_file_storage(); |
| 44 | + |
| 45 | + $fileinfo = [ |
| 46 | + 'contextid' => system::instance()->id, |
| 47 | + 'component' => self::COMPONENT, |
| 48 | + 'filearea' => self::FILEAREA, |
| 49 | + 'itemid' => $itemid, |
| 50 | + 'filepath' => self::FILEPATH, |
| 51 | + 'filename' => self::get_filename($remoteurl), |
| 52 | + ]; |
| 53 | + |
| 54 | + try { |
| 55 | + $file = $fs->create_file_from_url($fileinfo, url: $remoteurl); |
| 56 | + return $file; |
| 57 | + } catch (file_exception $e) { |
| 58 | + return false; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Delete a cached logo. |
| 64 | + * |
| 65 | + * @param int $itemid The id of the idp. `itemid` of the file is the `auth_saml2_idps.id`. |
| 66 | + * @return void |
| 67 | + */ |
| 68 | + public static function delete_cached_logo($itemid) { |
| 69 | + $fs = get_file_storage(); |
| 70 | + $fs->delete_area_files(system::instance()->id, self::COMPONENT, self::FILEAREA, $itemid); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Get the URL of a cached logo, only if the file exists. |
| 75 | + * |
| 76 | + * @param object $idp |
| 77 | + * @return null|moodle_url |
| 78 | + */ |
| 79 | + public static function get_cached_logo($idp) { |
| 80 | + if (empty($idp->logo)) { |
| 81 | + return null; |
| 82 | + } |
| 83 | + |
| 84 | + $context = system::instance(); |
| 85 | + $component = self::COMPONENT; |
| 86 | + $filearea = self::FILEAREA; |
| 87 | + $itemid = $idp->id; |
| 88 | + $filepath = self::FILEPATH; |
| 89 | + $filename = self::get_filename($idp->logo); |
| 90 | + |
| 91 | + if ($filename === '' || $filename === null) { |
| 92 | + return null; |
| 93 | + } |
| 94 | + |
| 95 | + $fs = get_file_storage(); |
| 96 | + $file = $fs->get_file( |
| 97 | + $context->id, |
| 98 | + $component, |
| 99 | + $filearea, |
| 100 | + $itemid, |
| 101 | + $filepath, |
| 102 | + $filename |
| 103 | + ); |
| 104 | + |
| 105 | + if (!$file) { |
| 106 | + return null; |
| 107 | + } |
| 108 | + |
| 109 | + return \moodle_url::make_pluginfile_url( |
| 110 | + $file->get_contextid(), |
| 111 | + $file->get_component(), |
| 112 | + $file->get_filearea(), |
| 113 | + $file->get_itemid(), |
| 114 | + $file->get_filepath(), |
| 115 | + $file->get_filename(), |
| 116 | + false |
| 117 | + ); |
| 118 | + } |
| 119 | + |
| 120 | + |
| 121 | + /** |
| 122 | + * Generate a unique filename via hashing the remote source URL and taking the original file extension. |
| 123 | + * |
| 124 | + * @param mixed $remoteurl |
| 125 | + * @return string |
| 126 | + */ |
| 127 | + private static function get_filename($remoteurl): string { |
| 128 | + $hash = md5($remoteurl); |
| 129 | + // Try to preserve extension from URL, default to png. |
| 130 | + $basename = basename(parse_url($remoteurl, PHP_URL_PATH) ?? ''); |
| 131 | + $ext = pathinfo($basename, PATHINFO_EXTENSION) ?: 'png'; |
| 132 | + return "{$hash}.{$ext}"; |
| 133 | + } |
| 134 | +} |
0 commit comments