MDL-66582 auth: Fix empty auth_logo in CAS/Shibboleth auth plugins
[moodle.git] / admin / tool / mobile / classes / api.php
CommitLineData
b2478ed0
JL
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 * Class for Moodle Mobile tools.
19 *
20 * @package tool_mobile
21 * @copyright 2016 Juan Leyva
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 * @since Moodle 3.1
24 */
25namespace tool_mobile;
26
27use core_component;
28use core_plugin_manager;
0002056f 29use context_system;
c951f1fe 30use moodle_url;
961c9549 31use moodle_exception;
b2551b4c 32use lang_string;
66a159f8 33use curl;
b2478ed0
JL
34
35/**
b2551b4c 36 * API exposed by tool_mobile, to be used mostly by external functions and the plugin settings.
b2478ed0
JL
37 *
38 * @copyright 2016 Juan Leyva
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 * @since Moodle 3.1
41 */
42class api {
43
c951f1fe
JL
44 /** @var int to identify the login via app. */
45 const LOGIN_VIA_APP = 1;
46 /** @var int to identify the login via browser. */
47 const LOGIN_VIA_BROWSER = 2;
48 /** @var int to identify the login via an embedded browser. */
49 const LOGIN_VIA_EMBEDDED_BROWSER = 3;
961c9549
JL
50 /** @var int seconds an auto-login key will expire. */
51 const LOGIN_KEY_TTL = 60;
c951f1fe 52
b2478ed0
JL
53 /**
54 * Returns a list of Moodle plugins supporting the mobile app.
55 *
56 * @return array an array of objects containing the plugin information
57 */
58 public static function get_plugins_supporting_mobile() {
59 global $CFG;
60 require_once($CFG->libdir . '/adminlib.php');
61
7b1c55ea
JL
62 $cachekey = 'mobileplugins';
63 if (!isloggedin()) {
64 $cachekey = 'authmobileplugins'; // Use a different cache for not logged users.
65 }
66
30fccfcd
SK
67 // Check if we can return this from cache.
68 $cache = \cache::make('tool_mobile', 'plugininfo');
7b1c55ea 69 $pluginsinfo = $cache->get($cachekey);
30fccfcd
SK
70 if ($pluginsinfo !== false) {
71 return (array)$pluginsinfo;
72 }
73
b2478ed0 74 $pluginsinfo = [];
7b1c55ea
JL
75 // For not logged users return only auth plugins.
76 // This is to avoid anyone (not being a registered user) to obtain and download all the site remote add-ons.
77 if (!isloggedin()) {
78 $plugintypes = array('auth' => $CFG->dirroot.'/auth');
79 } else {
80 $plugintypes = core_component::get_plugin_types();
81 }
b2478ed0
JL
82
83 foreach ($plugintypes as $plugintype => $unused) {
84 // We need to include files here.
85 $pluginswithfile = core_component::get_plugin_list_with_file($plugintype, 'db' . DIRECTORY_SEPARATOR . 'mobile.php');
86 foreach ($pluginswithfile as $plugin => $notused) {
87 $path = core_component::get_plugin_directory($plugintype, $plugin);
88 $component = $plugintype . '_' . $plugin;
89 $version = get_component_version($component);
90
30fccfcd 91 require("$path/db/mobile.php");
b2478ed0 92 foreach ($addons as $addonname => $addoninfo) {
73d85fee
JL
93
94 // Add handlers (for site add-ons).
95 $handlers = !empty($addoninfo['handlers']) ? $addoninfo['handlers'] : array();
96 $handlers = json_encode($handlers); // JSON formatted, since it is a complex structure that may vary over time.
97
98 // Now language strings used by the app.
99 $lang = array();
100 if (!empty($addoninfo['lang'])) {
101 $stringmanager = get_string_manager();
8c5850a5 102 $langs = $stringmanager->get_list_of_translations(true);
73d85fee
JL
103 foreach ($langs as $langid => $langname) {
104 foreach ($addoninfo['lang'] as $stringinfo) {
105 $lang[$langid][$stringinfo[0]] =
106 $stringmanager->get_string($stringinfo[0], $stringinfo[1], null, $langid);
107 }
108 }
109 }
110 $lang = json_encode($lang);
111
b2478ed0
JL
112 $plugininfo = array(
113 'component' => $component,
114 'version' => $version,
115 'addon' => $addonname,
b4baee42 116 'dependencies' => !empty($addoninfo['dependencies']) ? $addoninfo['dependencies'] : array(),
b2478ed0
JL
117 'fileurl' => '',
118 'filehash' => '',
73d85fee
JL
119 'filesize' => 0,
120 'handlers' => $handlers,
121 'lang' => $lang,
b2478ed0
JL
122 );
123
124 // All the mobile packages must be under the plugin mobile directory.
1c668c3c 125 $package = $path . '/mobile/' . $addonname . '.zip';
b2478ed0
JL
126 if (file_exists($package)) {
127 $plugininfo['fileurl'] = $CFG->wwwroot . '' . str_replace($CFG->dirroot, '', $package);
128 $plugininfo['filehash'] = sha1_file($package);
129 $plugininfo['filesize'] = filesize($package);
130 }
131 $pluginsinfo[] = $plugininfo;
132 }
133 }
134 }
30fccfcd 135
7b1c55ea 136 $cache->set($cachekey, $pluginsinfo);
30fccfcd 137
b2478ed0
JL
138 return $pluginsinfo;
139 }
140
0002056f
JL
141 /**
142 * Returns a list of the site public settings, those not requiring authentication.
143 *
144 * @return array with the settings and warnings
145 */
7c1cb3bf 146 public static function get_public_config() {
5d5e30c6 147 global $CFG, $SITE, $PAGE, $OUTPUT;
851b2919 148 require_once($CFG->libdir . '/authlib.php');
0002056f
JL
149
150 $context = context_system::instance();
151 // We need this to make work the format text functions.
152 $PAGE->set_context($context);
153
e2fe3bc0
JL
154 list($authinstructions, $notusedformat) = external_format_text($CFG->auth_instructions, FORMAT_MOODLE, $context->id);
155 list($maintenancemessage, $notusedformat) = external_format_text($CFG->maintenance_message, FORMAT_MOODLE, $context->id);
0002056f
JL
156 $settings = array(
157 'wwwroot' => $CFG->wwwroot,
672f4836 158 'httpswwwroot' => $CFG->wwwroot,
0002056f
JL
159 'sitename' => external_format_string($SITE->fullname, $context->id, true),
160 'guestlogin' => $CFG->guestloginbutton,
161 'rememberusername' => $CFG->rememberusername,
162 'authloginviaemail' => $CFG->authloginviaemail,
163 'registerauth' => $CFG->registerauth,
2479a7c4 164 'forgottenpasswordurl' => clean_param($CFG->forgottenpasswordurl, PARAM_URL), // We may expect a mailto: here.
e2fe3bc0 165 'authinstructions' => $authinstructions,
0002056f
JL
166 'authnoneenabled' => (int) is_enabled_auth('none'),
167 'enablewebservices' => $CFG->enablewebservices,
168 'enablemobilewebservice' => $CFG->enablemobilewebservice,
169 'maintenanceenabled' => $CFG->maintenance_enabled,
e2fe3bc0 170 'maintenancemessage' => $maintenancemessage,
91fff391 171 'mobilecssurl' => !empty($CFG->mobilecssurl) ? $CFG->mobilecssurl : '',
b2551b4c 172 'tool_mobile_disabledfeatures' => get_config('tool_mobile', 'disabledfeatures'),
8d9dc60b
JL
173 'country' => clean_param($CFG->country, PARAM_NOTAGS),
174 'agedigitalconsentverification' => \core_auth\digital_consent::is_age_digital_consent_verification_enabled(),
e236259d
JL
175 'autolang' => $CFG->autolang,
176 'lang' => clean_param($CFG->lang, PARAM_LANG), // Avoid breaking WS because of incorrect package langs.
177 'langmenu' => $CFG->langmenu,
178 'langlist' => $CFG->langlist,
179 'locale' => $CFG->locale,
4bf08f5b 180 'tool_mobile_minimumversion' => get_config('tool_mobile', 'minimumversion'),
12a289c7
JL
181 'tool_mobile_iosappid' => get_config('tool_mobile', 'iosappid'),
182 'tool_mobile_androidappid' => get_config('tool_mobile', 'androidappid'),
183 'tool_mobile_setuplink' => clean_param(get_config('tool_mobile', 'setuplink'), PARAM_URL),
0002056f 184 );
c951f1fe
JL
185
186 $typeoflogin = get_config('tool_mobile', 'typeoflogin');
187 // Not found, edge case.
188 if ($typeoflogin === false) {
189 $typeoflogin = self::LOGIN_VIA_APP; // Defaults to via app.
190 }
191 $settings['typeoflogin'] = $typeoflogin;
192
851b2919
JL
193 // Check if the user can sign-up to return the launch URL in that case.
194 $cansignup = signup_is_enabled();
195
b1037978
JL
196 $url = new moodle_url("/$CFG->admin/tool/mobile/launch.php");
197 $settings['launchurl'] = $url->out(false);
5d5e30c6 198
6703e031
JL
199 // Check that we are receiving a moodle_url object, themes can override get_logo_url and may return incorrect values.
200 if (($logourl = $OUTPUT->get_logo_url()) && $logourl instanceof moodle_url) {
818d40a9 201 $settings['logourl'] = clean_param($logourl->out(false), PARAM_URL);
5d5e30c6 202 }
6703e031 203 if (($compactlogourl = $OUTPUT->get_compact_logo_url()) && $compactlogourl instanceof moodle_url) {
818d40a9 204 $settings['compactlogourl'] = clean_param($compactlogourl->out(false), PARAM_URL);
5d5e30c6
JL
205 }
206
a67e3bda
JL
207 // Identity providers.
208 $authsequence = get_enabled_auth_plugins(true);
209 $identityproviders = \auth_plugin_base::get_identity_providers($authsequence);
210 $identityprovidersdata = \auth_plugin_base::prepare_identity_providers_for_output($identityproviders, $OUTPUT);
211 if (!empty($identityprovidersdata)) {
212 $settings['identityproviders'] = $identityprovidersdata;
213 }
214
8d9dc60b
JL
215 // If age is verified, return also the admin contact details.
216 if ($settings['agedigitalconsentverification']) {
217 $settings['supportname'] = clean_param($CFG->supportname, PARAM_NOTAGS);
218 $settings['supportemail'] = clean_param($CFG->supportemail, PARAM_EMAIL);
219 }
220
0002056f
JL
221 return $settings;
222 }
223
6b492628
JL
224 /**
225 * Returns a list of site configurations, filtering by section.
226 *
227 * @param string $section section name
228 * @return stdClass object containing the settings
229 */
230 public static function get_config($section) {
231 global $CFG, $SITE;
232
233 $settings = new \stdClass;
234 $context = context_system::instance();
235 $isadmin = has_capability('moodle/site:config', $context);
236
237 if (empty($section) or $section == 'frontpagesettings') {
238 require_once($CFG->dirroot . '/course/format/lib.php');
239 // First settings that anyone can deduce.
e2fe3bc0
JL
240 $settings->fullname = external_format_string($SITE->fullname, $context->id);
241 $settings->shortname = external_format_string($SITE->shortname, $context->id);
b14a04e0
DM
242
243 // Return to a var instead of directly to $settings object because of differences between
244 // list() in php5 and php7. {@link http://php.net/manual/en/function.list.php}
245 $formattedsummary = external_format_text($SITE->summary, $SITE->summaryformat,
e2fe3bc0 246 $context->id);
b14a04e0
DM
247 $settings->summary = $formattedsummary[0];
248 $settings->summaryformat = $formattedsummary[1];
6b492628
JL
249 $settings->frontpage = $CFG->frontpage;
250 $settings->frontpageloggedin = $CFG->frontpageloggedin;
251 $settings->maxcategorydepth = $CFG->maxcategorydepth;
252 $settings->frontpagecourselimit = $CFG->frontpagecourselimit;
89b909f6 253 $settings->numsections = course_get_format($SITE)->get_last_section_number();
6b492628
JL
254 $settings->newsitems = $SITE->newsitems;
255 $settings->commentsperpage = $CFG->commentsperpage;
256
257 // Now, admin settings.
258 if ($isadmin) {
259 $settings->defaultfrontpageroleid = $CFG->defaultfrontpageroleid;
260 }
261 }
262
263 if (empty($section) or $section == 'sitepolicies') {
1727c939
MG
264 $manager = new \core_privacy\local\sitepolicy\manager();
265 $settings->sitepolicy = ($sitepolicy = $manager->get_embed_url()) ? $sitepolicy->out(false) : '';
266 $settings->sitepolicyhandler = $CFG->sitepolicyhandler;
6b492628
JL
267 $settings->disableuserimages = $CFG->disableuserimages;
268 }
269
270 if (empty($section) or $section == 'gradessettings') {
271 require_once($CFG->dirroot . '/user/lib.php');
6703e031
JL
272 $settings->mygradesurl = user_mygrades_url();
273 // The previous function may return moodle_url instances or plain string URLs.
274 if ($settings->mygradesurl instanceof moodle_url) {
275 $settings->mygradesurl = $settings->mygradesurl->out(false);
276 }
6b492628
JL
277 }
278
7bdcf970
JL
279 if (empty($section) or $section == 'mobileapp') {
280 $settings->tool_mobile_forcelogout = get_config('tool_mobile', 'forcelogout');
af1b6043 281 $settings->tool_mobile_customlangstrings = get_config('tool_mobile', 'customlangstrings');
b2551b4c 282 $settings->tool_mobile_disabledfeatures = get_config('tool_mobile', 'disabledfeatures');
63d142e2 283 $settings->tool_mobile_custommenuitems = get_config('tool_mobile', 'custommenuitems');
04df75ce 284 $settings->tool_mobile_apppolicy = get_config('tool_mobile', 'apppolicy');
7bdcf970
JL
285 }
286
ab1b8238
JL
287 if (empty($section) or $section == 'calendar') {
288 $settings->calendartype = $CFG->calendartype;
289 $settings->calendar_site_timeformat = $CFG->calendar_site_timeformat;
290 $settings->calendar_startwday = $CFG->calendar_startwday;
291 $settings->calendar_adminseesall = $CFG->calendar_adminseesall;
292 $settings->calendar_lookahead = $CFG->calendar_lookahead;
293 $settings->calendar_maxevents = $CFG->calendar_maxevents;
294 }
295
6b492628
JL
296 return $settings;
297 }
298
961c9549
JL
299 /*
300 * Check if all the required conditions are met to allow the auto-login process continue.
301 *
302 * @param int $userid current user id
303 * @since Moodle 3.2
304 * @throws moodle_exception
305 */
306 public static function check_autologin_prerequisites($userid) {
307 global $CFG;
308
309 if (!$CFG->enablewebservices or !$CFG->enablemobilewebservice) {
310 throw new moodle_exception('enablewsdescription', 'webservice');
311 }
312
313 if (!is_https()) {
314 throw new moodle_exception('httpsrequired', 'tool_mobile');
315 }
316
317 if (has_capability('moodle/site:config', context_system::instance(), $userid) or is_siteadmin($userid)) {
318 throw new moodle_exception('autologinnotallowedtoadmins', 'tool_mobile');
319 }
320 }
321
322 /**
323 * Creates an auto-login key for the current user, this key is restricted by time and ip address.
324 *
325 * @return string the key
326 * @since Moodle 3.2
327 */
328 public static function get_autologin_key() {
329 global $USER;
330 // Delete previous keys.
331 delete_user_key('tool_mobile', $USER->id);
332
333 // Create a new key.
334 $iprestriction = getremoteaddr();
335 $validuntil = time() + self::LOGIN_KEY_TTL;
336 return create_user_key('tool_mobile', $USER->id, null, $iprestriction, $validuntil);
337 }
b2551b4c
JL
338
339 /**
340 * Get a list of the Mobile app features.
341 *
342 * @return array array with the features grouped by theirs ubication in the app.
343 * @since Moodle 3.3
344 */
345 public static function get_features_list() {
346 global $CFG;
347
348 $general = new lang_string('general');
349 $mainmenu = new lang_string('mainmenu', 'tool_mobile');
350 $course = new lang_string('course');
351 $modules = new lang_string('managemodules');
c1720141 352 $blocks = new lang_string('blocks');
b2551b4c
JL
353 $user = new lang_string('user');
354 $files = new lang_string('files');
355 $remoteaddons = new lang_string('remoteaddons', 'tool_mobile');
356
357 $availablemods = core_plugin_manager::instance()->get_plugins_of_type('mod');
358 $coursemodules = array();
c3e745b7 359 $appsupportedmodules = array('assign', 'book', 'chat', 'choice', 'data', 'feedback', 'folder', 'forum', 'glossary', 'imscp',
d808c220
JL
360 'label', 'lesson', 'lti', 'page', 'quiz', 'resource', 'scorm', 'survey', 'url', 'wiki', 'workshop');
361
b2551b4c
JL
362 foreach ($availablemods as $mod) {
363 if (in_array($mod->name, $appsupportedmodules)) {
364 $coursemodules['$mmCourseDelegate_mmaMod' . ucfirst($mod->name)] = $mod->displayname;
365 }
366 }
bf258846 367 asort($coursemodules);
b2551b4c
JL
368
369 $remoteaddonslist = array();
370 $mobileplugins = self::get_plugins_supporting_mobile();
371 foreach ($mobileplugins as $plugin) {
372 $displayname = core_plugin_manager::instance()->plugin_name($plugin['component']) . " - " . $plugin['addon'];
d6c06935 373 $remoteaddonslist['sitePlugin_' . $plugin['component'] . '_' . $plugin['addon']] = $displayname;
b2551b4c
JL
374
375 }
376
c1720141
JL
377 // Display blocks.
378 $availableblocks = core_plugin_manager::instance()->get_plugins_of_type('block');
379 $courseblocks = array();
380 $appsupportedblocks = array(
381 'activity_modules' => 'CoreBlockDelegate_AddonBlockActivityModules',
382 'site_main_menu' => 'CoreBlockDelegate_AddonBlockSiteMainMenu',
383 'myoverview' => 'CoreBlockDelegate_AddonBlockMyOverview',
384 'timeline' => 'CoreBlockDelegate_AddonBlockTimeline',
385 'recentlyaccessedcourses' => 'CoreBlockDelegate_AddonBlockRecentlyAccessedCourses',
386 'starredcourses' => 'CoreBlockDelegate_AddonBlockStarredCourses',
387 'recentlyaccesseditems' => 'CoreBlockDelegate_AddonBlockRecentlyAccessedItems',
bf258846
JL
388 'badges' => 'CoreBlockDelegate_AddonBlockBadges',
389 'blog_menu' => 'CoreBlockDelegate_AddonBlockBlogMenu',
390 'blog_recent' => 'CoreBlockDelegate_AddonBlockBlogRecent',
391 'blog_tags' => 'CoreBlockDelegate_AddonBlockBlogTags',
392 'calendar_month' => 'CoreBlockDelegate_AddonBlockCalendarMonth',
393 'calendar_upcoming' => 'CoreBlockDelegate_AddonBlockCalendarUpcoming',
394 'comments' => 'CoreBlockDelegate_AddonBlockComments',
395 'completionstatus' => 'CoreBlockDelegate_AddonBlockCompletionStatus',
396 'feedback' => 'CoreBlockDelegate_AddonBlockFeedback',
397 'glossary_random' => 'CoreBlockDelegate_AddonBlockGlossaryRandom',
398 'html' => 'CoreBlockDelegate_AddonBlockHtml',
399 'lp' => 'CoreBlockDelegate_AddonBlockLp',
400 'news_items' => 'CoreBlockDelegate_AddonBlockNewsItems',
401 'online_users' => 'CoreBlockDelegate_AddonBlockOnlineUsers',
402 'selfcompletion' => 'CoreBlockDelegate_AddonBlockSelfCompletion',
403 'tags' => 'CoreBlockDelegate_AddonBlockTags',
c1720141
JL
404 );
405
406 foreach ($availableblocks as $block) {
407 if (isset($appsupportedblocks[$block->name])) {
408 $courseblocks[$appsupportedblocks[$block->name]] = $block->displayname;
409 }
410 }
bf258846 411 asort($courseblocks);
c1720141 412
b2551b4c 413 $features = array(
bf258846
JL
414 "$general" => array(
415 'NoDelegate_CoreOffline' => new lang_string('offlineuse', 'tool_mobile'),
416 'NoDelegate_SiteBlocks' => new lang_string('blocks'),
417 'NoDelegate_CoreComments' => new lang_string('comments'),
418 'NoDelegate_CoreRating' => new lang_string('ratings', 'rating'),
419 'NoDelegate_CoreTag' => new lang_string('tags'),
420 '$mmLoginEmailSignup' => new lang_string('startsignup'),
88932e49 421 'NoDelegate_ForgottenPassword' => new lang_string('forgotten'),
bf258846
JL
422 'NoDelegate_ResponsiveMainMenuItems' => new lang_string('responsivemainmenuitems', 'tool_mobile'),
423 ),
b2551b4c 424 "$mainmenu" => array(
b2551b4c 425 '$mmSideMenuDelegate_mmaFrontpage' => new lang_string('sitehome'),
bf258846
JL
426 '$mmSideMenuDelegate_mmCourses' => new lang_string('mycourses'),
427 'CoreMainMenuDelegate_CoreCoursesDashboard' => new lang_string('myhome'),
428 '$mmSideMenuDelegate_mmaCalendar' => new lang_string('calendar', 'calendar'),
b2551b4c
JL
429 '$mmSideMenuDelegate_mmaNotifications' => new lang_string('notifications', 'message'),
430 '$mmSideMenuDelegate_mmaMessages' => new lang_string('messages', 'message'),
bf258846
JL
431 '$mmSideMenuDelegate_mmaGrades' => new lang_string('grades', 'grades'),
432 '$mmSideMenuDelegate_mmaCompetency' => new lang_string('myplans', 'tool_lp'),
433 'CoreMainMenuDelegate_AddonBlog' => new lang_string('blog', 'blog'),
b2551b4c
JL
434 '$mmSideMenuDelegate_mmaFiles' => new lang_string('files'),
435 '$mmSideMenuDelegate_website' => new lang_string('webpage'),
436 '$mmSideMenuDelegate_help' => new lang_string('help'),
437 ),
438 "$course" => array(
bf258846
JL
439 'NoDelegate_CourseBlocks' => new lang_string('blocks'),
440 'CoreCourseOptionsDelegate_AddonBlog' => new lang_string('blog', 'blog'),
b2551b4c
JL
441 '$mmCoursesDelegate_search' => new lang_string('search'),
442 '$mmCoursesDelegate_mmaCompetency' => new lang_string('competencies', 'competency'),
443 '$mmCoursesDelegate_mmaParticipants' => new lang_string('participants'),
444 '$mmCoursesDelegate_mmaGrades' => new lang_string('grades', 'grades'),
445 '$mmCoursesDelegate_mmaCourseCompletion' => new lang_string('coursecompletion', 'completion'),
446 '$mmCoursesDelegate_mmaNotes' => new lang_string('notes', 'notes'),
3c8de97d
JL
447 'NoDelegate_CoreCourseDownload' => new lang_string('downloadcourse', 'tool_mobile'),
448 'NoDelegate_CoreCoursesDownload' => new lang_string('downloadcourses', 'tool_mobile'),
b2551b4c
JL
449 ),
450 "$user" => array(
f72a4828 451 'CoreUserDelegate_AddonBlog:blogs' => new lang_string('blog', 'blog'),
b2551b4c
JL
452 '$mmUserDelegate_mmaBadges' => new lang_string('badges', 'badges'),
453 '$mmUserDelegate_mmaCompetency:learningPlan' => new lang_string('competencies', 'competency'),
454 '$mmUserDelegate_mmaCourseCompletion:viewCompletion' => new lang_string('coursecompletion', 'completion'),
455 '$mmUserDelegate_mmaGrades:viewGrades' => new lang_string('grades', 'grades'),
456 '$mmUserDelegate_mmaMessages:sendMessage' => new lang_string('sendmessage', 'message'),
457 '$mmUserDelegate_mmaMessages:addContact' => new lang_string('addcontact', 'message'),
458 '$mmUserDelegate_mmaMessages:blockContact' => new lang_string('blockcontact', 'message'),
459 '$mmUserDelegate_mmaNotes:addNote' => new lang_string('addnewnote', 'notes'),
460 '$mmUserDelegate_picture' => new lang_string('userpic'),
461 ),
462 "$files" => array(
463 'files_privatefiles' => new lang_string('privatefiles'),
464 'files_sitefiles' => new lang_string('sitefiles'),
465 'files_upload' => new lang_string('upload'),
466 ),
467 "$modules" => $coursemodules,
c1720141 468 "$blocks" => $courseblocks,
b2551b4c
JL
469 );
470
471 if (!empty($remoteaddonslist)) {
472 $features["$remoteaddons"] = $remoteaddonslist;
473 }
474
475 return $features;
476 }
66a159f8
JL
477
478 /**
479 * This function check the current site for potential configuration issues that may prevent the mobile app to work.
480 *
481 * @return array list of potential issues
482 * @since Moodle 3.4
483 */
484 public static function get_potential_config_issues() {
485 global $CFG;
486 require_once($CFG->dirroot . "/lib/filelib.php");
487 require_once($CFG->dirroot . '/message/lib.php');
488
489 $warnings = array();
490
491 $curl = new curl();
492 // Return certificate information and verify the certificate.
493 $curl->setopt(array('CURLOPT_CERTINFO' => 1, 'CURLOPT_SSL_VERIFYPEER' => true));
494 $httpswwwroot = str_replace('http:', 'https:', $CFG->wwwroot); // Force https url.
098b16fb
JL
495 // Check https using a page not redirecting or returning exceptions.
496 $curl->head($httpswwwroot . "/$CFG->admin/tool/mobile/mobile.webmanifest.php");
66a159f8
JL
497 $info = $curl->get_info();
498
499 // First of all, check the server certificate (if any).
500 if (empty($info['http_code']) or ($info['http_code'] >= 400)) {
501 $warnings[] = ['nohttpsformobilewarning', 'admin'];
502 } else {
503 // Check the certificate is not self-signed or has an untrusted-root.
504 // This may be weak in some scenarios (when the curl SSL verifier is outdated).
505 if (empty($info['certinfo'])) {
506 $warnings[] = ['selfsignedoruntrustedcertificatewarning', 'tool_mobile'];
507 } else {
508 $timenow = time();
509 $expectedissuer = null;
510 foreach ($info['certinfo'] as $cert) {
511 // Check if the signature algorithm is weak (Android won't work with SHA-1).
512 if ($cert['Signature Algorithm'] == 'sha1WithRSAEncryption' || $cert['Signature Algorithm'] == 'sha1WithRSA') {
513 $warnings[] = ['insecurealgorithmwarning', 'tool_mobile'];
514 }
515 // Check certificate start date.
516 if (strtotime($cert['Start date']) > $timenow) {
517 $warnings[] = ['invalidcertificatestartdatewarning', 'tool_mobile'];
518 }
519 // Check certificate end date.
520 if (strtotime($cert['Expire date']) < $timenow) {
521 $warnings[] = ['invalidcertificateexpiredatewarning', 'tool_mobile'];
522 }
523 // Check the chain.
524 if ($expectedissuer !== null) {
525 if ($expectedissuer !== $cert['Subject'] || $cert['Subject'] === $cert['Issuer']) {
526 $warnings[] = ['invalidcertificatechainwarning', 'tool_mobile'];
527 }
528 }
529 $expectedissuer = $cert['Issuer'];
530 }
531 }
532 }
533 // Now check typical configuration problems.
534 if ((int) $CFG->userquota === PHP_INT_MAX) {
535 // In old Moodle version was a text so was possible to have numeric values > PHP_INT_MAX.
536 $warnings[] = ['invaliduserquotawarning', 'tool_mobile'];
537 }
538 // Check ADOdb debug enabled.
539 if (get_config('auth_db', 'debugauthdb') || get_config('enrol_database', 'debugdb')) {
540 $warnings[] = ['adodbdebugwarning', 'tool_mobile'];
541 }
542 // Check display errors on.
543 if (!empty($CFG->debugdisplay)) {
544 $warnings[] = ['displayerrorswarning', 'tool_mobile'];
545 }
546 // Check mobile notifications.
547 $processors = get_message_processors();
548 $enabled = false;
549 foreach ($processors as $processor => $status) {
550 if ($processor == 'airnotifier' && $status->enabled) {
551 $enabled = true;
552 }
553 }
554 if (!$enabled) {
555 $warnings[] = ['mobilenotificationsdisabledwarning', 'tool_mobile'];
556 }
557
558 return $warnings;
559 }
b2478ed0 560}