MDL-57409 tool_mobile: New forcelogout setting
[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;
b2478ed0
JL
32
33/**
e2fe3bc0 34 * API exposed by tool_mobile, to be used mostly by external functions.
b2478ed0
JL
35 *
36 * @copyright 2016 Juan Leyva
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 * @since Moodle 3.1
39 */
40class api {
41
c951f1fe
JL
42 /** @var int to identify the login via app. */
43 const LOGIN_VIA_APP = 1;
44 /** @var int to identify the login via browser. */
45 const LOGIN_VIA_BROWSER = 2;
46 /** @var int to identify the login via an embedded browser. */
47 const LOGIN_VIA_EMBEDDED_BROWSER = 3;
961c9549
JL
48 /** @var int seconds an auto-login key will expire. */
49 const LOGIN_KEY_TTL = 60;
c951f1fe 50
b2478ed0
JL
51 /**
52 * Returns a list of Moodle plugins supporting the mobile app.
53 *
54 * @return array an array of objects containing the plugin information
55 */
56 public static function get_plugins_supporting_mobile() {
57 global $CFG;
58 require_once($CFG->libdir . '/adminlib.php');
59
60 $pluginsinfo = [];
61 $plugintypes = core_component::get_plugin_types();
62
63 foreach ($plugintypes as $plugintype => $unused) {
64 // We need to include files here.
65 $pluginswithfile = core_component::get_plugin_list_with_file($plugintype, 'db' . DIRECTORY_SEPARATOR . 'mobile.php');
66 foreach ($pluginswithfile as $plugin => $notused) {
67 $path = core_component::get_plugin_directory($plugintype, $plugin);
68 $component = $plugintype . '_' . $plugin;
69 $version = get_component_version($component);
70
71 require_once("$path/db/mobile.php");
72 foreach ($addons as $addonname => $addoninfo) {
73 $plugininfo = array(
74 'component' => $component,
75 'version' => $version,
76 'addon' => $addonname,
b4baee42 77 'dependencies' => !empty($addoninfo['dependencies']) ? $addoninfo['dependencies'] : array(),
b2478ed0
JL
78 'fileurl' => '',
79 'filehash' => '',
80 'filesize' => 0
81 );
82
83 // All the mobile packages must be under the plugin mobile directory.
84 $package = $path . DIRECTORY_SEPARATOR . 'mobile' . DIRECTORY_SEPARATOR . $addonname . '.zip';
85 if (file_exists($package)) {
86 $plugininfo['fileurl'] = $CFG->wwwroot . '' . str_replace($CFG->dirroot, '', $package);
87 $plugininfo['filehash'] = sha1_file($package);
88 $plugininfo['filesize'] = filesize($package);
89 }
90 $pluginsinfo[] = $plugininfo;
91 }
92 }
93 }
94 return $pluginsinfo;
95 }
96
0002056f
JL
97 /**
98 * Returns a list of the site public settings, those not requiring authentication.
99 *
100 * @return array with the settings and warnings
101 */
7c1cb3bf 102 public static function get_public_config() {
5d5e30c6 103 global $CFG, $SITE, $PAGE, $OUTPUT;
851b2919 104 require_once($CFG->libdir . '/authlib.php');
0002056f
JL
105
106 $context = context_system::instance();
107 // We need this to make work the format text functions.
108 $PAGE->set_context($context);
109
e2fe3bc0
JL
110 list($authinstructions, $notusedformat) = external_format_text($CFG->auth_instructions, FORMAT_MOODLE, $context->id);
111 list($maintenancemessage, $notusedformat) = external_format_text($CFG->maintenance_message, FORMAT_MOODLE, $context->id);
0002056f
JL
112 $settings = array(
113 'wwwroot' => $CFG->wwwroot,
114 'httpswwwroot' => $CFG->httpswwwroot,
115 'sitename' => external_format_string($SITE->fullname, $context->id, true),
116 'guestlogin' => $CFG->guestloginbutton,
117 'rememberusername' => $CFG->rememberusername,
118 'authloginviaemail' => $CFG->authloginviaemail,
119 'registerauth' => $CFG->registerauth,
120 'forgottenpasswordurl' => $CFG->forgottenpasswordurl,
e2fe3bc0 121 'authinstructions' => $authinstructions,
0002056f
JL
122 'authnoneenabled' => (int) is_enabled_auth('none'),
123 'enablewebservices' => $CFG->enablewebservices,
124 'enablemobilewebservice' => $CFG->enablemobilewebservice,
125 'maintenanceenabled' => $CFG->maintenance_enabled,
e2fe3bc0 126 'maintenancemessage' => $maintenancemessage,
91fff391 127 'mobilecssurl' => !empty($CFG->mobilecssurl) ? $CFG->mobilecssurl : '',
0002056f 128 );
c951f1fe
JL
129
130 $typeoflogin = get_config('tool_mobile', 'typeoflogin');
131 // Not found, edge case.
132 if ($typeoflogin === false) {
133 $typeoflogin = self::LOGIN_VIA_APP; // Defaults to via app.
134 }
135 $settings['typeoflogin'] = $typeoflogin;
136
851b2919
JL
137 // Check if the user can sign-up to return the launch URL in that case.
138 $cansignup = signup_is_enabled();
139
c951f1fe 140 if ($typeoflogin == self::LOGIN_VIA_BROWSER or
851b2919
JL
141 $typeoflogin == self::LOGIN_VIA_EMBEDDED_BROWSER or
142 $cansignup) {
c951f1fe
JL
143 $url = new moodle_url("/$CFG->admin/tool/mobile/launch.php");
144 $settings['launchurl'] = $url->out(false);
145 }
5d5e30c6
JL
146
147 if ($logourl = $OUTPUT->get_logo_url()) {
148 $settings['logourl'] = $logourl->out(false);
149 }
150 if ($compactlogourl = $OUTPUT->get_compact_logo_url()) {
151 $settings['compactlogourl'] = $compactlogourl->out(false);
152 }
153
0002056f
JL
154 return $settings;
155 }
156
6b492628
JL
157 /**
158 * Returns a list of site configurations, filtering by section.
159 *
160 * @param string $section section name
161 * @return stdClass object containing the settings
162 */
163 public static function get_config($section) {
164 global $CFG, $SITE;
165
166 $settings = new \stdClass;
167 $context = context_system::instance();
168 $isadmin = has_capability('moodle/site:config', $context);
169
170 if (empty($section) or $section == 'frontpagesettings') {
171 require_once($CFG->dirroot . '/course/format/lib.php');
172 // First settings that anyone can deduce.
e2fe3bc0
JL
173 $settings->fullname = external_format_string($SITE->fullname, $context->id);
174 $settings->shortname = external_format_string($SITE->shortname, $context->id);
b14a04e0
DM
175
176 // Return to a var instead of directly to $settings object because of differences between
177 // list() in php5 and php7. {@link http://php.net/manual/en/function.list.php}
178 $formattedsummary = external_format_text($SITE->summary, $SITE->summaryformat,
e2fe3bc0 179 $context->id);
b14a04e0
DM
180 $settings->summary = $formattedsummary[0];
181 $settings->summaryformat = $formattedsummary[1];
6b492628
JL
182 $settings->frontpage = $CFG->frontpage;
183 $settings->frontpageloggedin = $CFG->frontpageloggedin;
184 $settings->maxcategorydepth = $CFG->maxcategorydepth;
185 $settings->frontpagecourselimit = $CFG->frontpagecourselimit;
186 $settings->numsections = course_get_format($SITE)->get_course()->numsections;
187 $settings->newsitems = $SITE->newsitems;
188 $settings->commentsperpage = $CFG->commentsperpage;
189
190 // Now, admin settings.
191 if ($isadmin) {
192 $settings->defaultfrontpageroleid = $CFG->defaultfrontpageroleid;
193 }
194 }
195
196 if (empty($section) or $section == 'sitepolicies') {
197 $settings->disableuserimages = $CFG->disableuserimages;
198 }
199
200 if (empty($section) or $section == 'gradessettings') {
201 require_once($CFG->dirroot . '/user/lib.php');
202 $settings->mygradesurl = user_mygrades_url()->out(false);
203 }
204
205 return $settings;
206 }
207
961c9549
JL
208 /*
209 * Check if all the required conditions are met to allow the auto-login process continue.
210 *
211 * @param int $userid current user id
212 * @since Moodle 3.2
213 * @throws moodle_exception
214 */
215 public static function check_autologin_prerequisites($userid) {
216 global $CFG;
217
218 if (!$CFG->enablewebservices or !$CFG->enablemobilewebservice) {
219 throw new moodle_exception('enablewsdescription', 'webservice');
220 }
221
222 if (!is_https()) {
223 throw new moodle_exception('httpsrequired', 'tool_mobile');
224 }
225
226 if (has_capability('moodle/site:config', context_system::instance(), $userid) or is_siteadmin($userid)) {
227 throw new moodle_exception('autologinnotallowedtoadmins', 'tool_mobile');
228 }
229 }
230
231 /**
232 * Creates an auto-login key for the current user, this key is restricted by time and ip address.
233 *
234 * @return string the key
235 * @since Moodle 3.2
236 */
237 public static function get_autologin_key() {
238 global $USER;
239 // Delete previous keys.
240 delete_user_key('tool_mobile', $USER->id);
241
242 // Create a new key.
243 $iprestriction = getremoteaddr();
244 $validuntil = time() + self::LOGIN_KEY_TTL;
245 return create_user_key('tool_mobile', $USER->id, null, $iprestriction, $validuntil);
246 }
b2478ed0 247}