| 1 | efrain | 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 |  * Launch page, launch the app using custom URL schemes.
 | 
        
           |  |  | 19 |  *
 | 
        
           |  |  | 20 |  * If the user is not logged when visiting this page, he will be redirected to the login page.
 | 
        
           |  |  | 21 |  * Once he is logged, he will be redirected again to this page and the app launched via custom URL schemes.
 | 
        
           |  |  | 22 |  *
 | 
        
           |  |  | 23 |  * @package    tool_mobile
 | 
        
           |  |  | 24 |  * @copyright  2016 Juan Leyva
 | 
        
           |  |  | 25 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 26 |  */
 | 
        
           |  |  | 27 |   | 
        
           |  |  | 28 | require_once(__DIR__ . '/../../../config.php');
 | 
        
           |  |  | 29 |   | 
        
           |  |  | 30 | $serviceshortname  = required_param('service',  PARAM_ALPHANUMEXT);
 | 
        
           |  |  | 31 | $passport          = required_param('passport',  PARAM_RAW);    // Passport send from the app to validate the response URL.
 | 
        
           |  |  | 32 | $urlscheme         = optional_param('urlscheme', 'moodlemobile', PARAM_NOTAGS); // The URL scheme the app supports.
 | 
        
           |  |  | 33 | $confirmed         = optional_param('confirmed', false, PARAM_BOOL);  // If we are being redirected after user confirmation.
 | 
        
           |  |  | 34 | $oauthsso          = optional_param('oauthsso', 0, PARAM_INT); // Id of the OpenID issuer (for OAuth direct SSO).
 | 
        
           |  |  | 35 |   | 
        
           |  |  | 36 | // Validate that the urlscheme is valid.
 | 
        
           |  |  | 37 | if (!preg_match('/^[a-zA-Z][a-zA-Z0-9-\+\.]*$/', $urlscheme)) {
 | 
        
           |  |  | 38 |     throw new moodle_exception('Invalid parameter: the value of urlscheme isn\'t valid. ' .
 | 
        
           |  |  | 39 |             'It should start with a letter and can only contain letters, numbers and the characters "." "+" "-".');
 | 
        
           |  |  | 40 | }
 | 
        
           |  |  | 41 |   | 
        
           |  |  | 42 | // Check web services enabled.
 | 
        
           |  |  | 43 | if (!$CFG->enablewebservices) {
 | 
        
           |  |  | 44 |     throw new moodle_exception('enablewsdescription', 'webservice');
 | 
        
           |  |  | 45 | }
 | 
        
           |  |  | 46 |   | 
        
           |  |  | 47 | // Check if the service exists and is enabled.
 | 
        
           |  |  | 48 | $service = $DB->get_record('external_services', ['shortname' => $serviceshortname, 'enabled' => 1]);
 | 
        
           |  |  | 49 | if (empty($service)) {
 | 
        
           |  |  | 50 |     throw new moodle_exception('servicenotavailable', 'webservice');
 | 
        
           |  |  | 51 | }
 | 
        
           |  |  | 52 |   | 
        
           |  |  | 53 | // Set a cookie indicating that there was a launch to authenticate via the site from the app.
 | 
        
           |  |  | 54 | $ldata = json_encode(['service' => $serviceshortname, 'passport' => $passport,
 | 
        
           |  |  | 55 |     'urlscheme' => $urlscheme, 'confirmed' => (int) $confirmed, 'oauthsso' => $oauthsso]);
 | 
        
           |  |  | 56 | $expires = time() + (15 * MINSECS); // 15 minutes for authentication should be enough.
 | 
        
           |  |  | 57 | setcookie('tool_mobile_launch', $ldata, $expires, $CFG->sessioncookiepath, $CFG->sessioncookiedomain);
 | 
        
           |  |  | 58 |   | 
        
           |  |  | 59 | // We have been requested to start a SSO process via OpenID.
 | 
        
           |  |  | 60 | if (!empty($oauthsso) && is_enabled_auth('oauth2')) {
 | 
        
           |  |  | 61 |     $wantsurl = new moodle_url('/admin/tool/mobile/launch.php',
 | 
        
           |  |  | 62 |         array('service' => $serviceshortname, 'passport' => $passport, 'urlscheme' => $urlscheme, 'confirmed' => $confirmed));
 | 
        
           |  |  | 63 |     $oauthurl = new moodle_url('/auth/oauth2/login.php',
 | 
        
           |  |  | 64 |         array('id' => $oauthsso, 'sesskey' => sesskey(), 'wantsurl' => $wantsurl));
 | 
        
           |  |  | 65 |     header('Location: ' . $oauthurl->out(false));
 | 
        
           |  |  | 66 |     die;
 | 
        
           |  |  | 67 | }
 | 
        
           |  |  | 68 |   | 
        
           |  |  | 69 | // Check if the plugin is properly configured.
 | 
        
           |  |  | 70 | $typeoflogin = get_config('tool_mobile', 'typeoflogin');
 | 
        
           |  |  | 71 | if (empty($SESSION->justloggedin) &&
 | 
        
           |  |  | 72 |         !is_enabled_auth('oauth2') &&
 | 
        
           |  |  | 73 |         $typeoflogin != tool_mobile\api::LOGIN_VIA_BROWSER &&
 | 
        
           |  |  | 74 |         $typeoflogin != tool_mobile\api::LOGIN_VIA_EMBEDDED_BROWSER) {
 | 
        
           |  |  | 75 |     throw new moodle_exception('pluginnotenabledorconfigured', 'tool_mobile');
 | 
        
           |  |  | 76 | }
 | 
        
           |  |  | 77 |   | 
        
           |  |  | 78 | require_login(0, false);
 | 
        
           |  |  | 79 |   | 
        
           |  |  | 80 | // Require an active user: not guest, not suspended.
 | 
        
           |  |  | 81 | core_user::require_active_user($USER);
 | 
        
           |  |  | 82 |   | 
        
           |  |  | 83 | // Remove cookie.
 | 
        
           |  |  | 84 | unset($_COOKIE['tool_mobile_launch']);
 | 
        
           |  |  | 85 | setcookie('tool_mobile_launch', '', -1, $CFG->sessioncookiepath);
 | 
        
           |  |  | 86 |   | 
        
           |  |  | 87 | // Get an existing token or create a new one.
 | 
        
           |  |  | 88 | $timenow = time();
 | 
        
           |  |  | 89 | $token = \core_external\util::generate_token_for_current_user($service);
 | 
        
           |  |  | 90 | $privatetoken = $token->privatetoken;
 | 
        
           |  |  | 91 | \core_external\util::log_token_request($token);
 | 
        
           |  |  | 92 |   | 
        
           |  |  | 93 | // Don't return the private token if the user didn't just log in and a new token wasn't created.
 | 
        
           |  |  | 94 | if (empty($SESSION->justloggedin) and $token->timecreated < $timenow) {
 | 
        
           |  |  | 95 |     $privatetoken = null;
 | 
        
           |  |  | 96 | }
 | 
        
           |  |  | 97 |   | 
        
           |  |  | 98 | $siteadmin = has_capability('moodle/site:config', context_system::instance(), $USER->id);
 | 
        
           |  |  | 99 |   | 
        
           |  |  | 100 | // Passport is generated in the mobile app, so the app opening can be validated using that variable.
 | 
        
           |  |  | 101 | // Passports are valid only one time, it's deleted in the app once used.
 | 
        
           |  |  | 102 | $siteid = md5($CFG->wwwroot . $passport);
 | 
        
           |  |  | 103 | $apptoken = $siteid . ':::' . $token->token;
 | 
        
           |  |  | 104 | if ($privatetoken and is_https() and !$siteadmin) {
 | 
        
           |  |  | 105 |     $apptoken .= ':::' . $privatetoken;
 | 
        
           |  |  | 106 | }
 | 
        
           |  |  | 107 |   | 
        
           |  |  | 108 | $apptoken = base64_encode($apptoken);
 | 
        
           |  |  | 109 |   | 
        
           |  |  | 110 | // Redirect using the custom URL scheme checking first if a URL scheme is forced in the site settings.
 | 
        
           |  |  | 111 | $forcedurlscheme = get_config('tool_mobile', 'forcedurlscheme');
 | 
        
           |  |  | 112 | if (!empty($forcedurlscheme)) {
 | 
        
           |  |  | 113 |     $urlscheme = $forcedurlscheme;
 | 
        
           |  |  | 114 | }
 | 
        
           |  |  | 115 |   | 
        
           |  |  | 116 | $location = "$urlscheme://token=$apptoken";
 | 
        
           |  |  | 117 |   | 
        
           |  |  | 118 | // For iOS 10 onwards, we have to simulate a user click.
 | 
        
           |  |  | 119 | // If we come from the confirmation page, we should display a nicer page.
 | 
        
           |  |  | 120 | $isios = core_useragent::is_ios();
 | 
        
           |  |  | 121 | if ($confirmed or $isios) {
 | 
        
           |  |  | 122 |     $PAGE->set_context(context_system::instance());
 | 
        
           |  |  | 123 |     $PAGE->set_heading($COURSE->fullname);
 | 
        
           |  |  | 124 |     $params = array('service' => $serviceshortname, 'passport' => $passport, 'urlscheme' => $urlscheme, 'confirmed' => $confirmed);
 | 
        
           |  |  | 125 |     $PAGE->set_url("/$CFG->admin/tool/mobile/launch.php", $params);
 | 
        
           |  |  | 126 |   | 
        
           |  |  | 127 |     echo $OUTPUT->header();
 | 
        
           |  |  | 128 |     if ($confirmed) {
 | 
        
           |  |  | 129 |         $confirmedstr = get_string('confirmed');
 | 
        
           |  |  | 130 |         $PAGE->navbar->add($confirmedstr);
 | 
        
           |  |  | 131 |         $PAGE->set_title($confirmedstr);
 | 
        
           |  |  | 132 |         echo $OUTPUT->notification($confirmedstr, \core\output\notification::NOTIFY_SUCCESS);
 | 
        
           |  |  | 133 |         echo $OUTPUT->box_start('generalbox centerpara boxwidthnormal boxaligncenter');
 | 
        
           |  |  | 134 |         echo $OUTPUT->single_button(new moodle_url('/course/'), get_string('courses'));
 | 
        
           |  |  | 135 |         echo $OUTPUT->box_end();
 | 
        
           |  |  | 136 |     }
 | 
        
           |  |  | 137 |   | 
        
           |  |  | 138 |     $notice = get_string('clickheretolaunchtheapp', 'tool_mobile');
 | 
        
           |  |  | 139 |     echo $OUTPUT->box(html_writer::link($location, $notice, ['id' => 'launchapp']), 'generalbox warning centerpara');
 | 
        
           |  |  | 140 |     echo html_writer::script(
 | 
        
           |  |  | 141 |         "window.onload = function() {
 | 
        
           |  |  | 142 |             document.getElementById('launchapp').click();
 | 
        
           |  |  | 143 |         };"
 | 
        
           |  |  | 144 |     );
 | 
        
           |  |  | 145 |     echo $OUTPUT->footer();
 | 
        
           |  |  | 146 | } else {
 | 
        
           |  |  | 147 |     // For Android a http redirect will do fine.
 | 
        
           |  |  | 148 |     header('Location: ' . $location);
 | 
        
           |  |  | 149 |     die;
 | 
        
           |  |  | 150 | }
 |