"Fossies" - the Fresh Open Source Software Archive

Member "moodle/admin/tool/mobile/launch.php" (6 Sep 2019, 5687 Bytes) of package /linux/www/moodle-3.6.6.tgz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "launch.php" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 3.6.5_vs_3.6.6.

    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 require_once($CFG->libdir . '/externallib.php');
   30 
   31 $serviceshortname  = required_param('service',  PARAM_ALPHANUMEXT);
   32 $passport          = required_param('passport',  PARAM_RAW);    // Passport send from the app to validate the response URL.
   33 $urlscheme         = optional_param('urlscheme', 'moodlemobile', PARAM_ALPHANUM); // The URL scheme the app supports.
   34 $confirmed         = optional_param('confirmed', false, PARAM_BOOL);  // If we are being redirected after user confirmation.
   35 $oauthsso          = optional_param('oauthsso', 0, PARAM_INT); // Id of the OpenID issuer (for OAuth direct SSO).
   36 
   37 // Check web services enabled.
   38 if (!$CFG->enablewebservices) {
   39     throw new moodle_exception('enablewsdescription', 'webservice');
   40 }
   41 
   42 // We have been requested to start a SSO process via OpenID.
   43 if (!empty($oauthsso) && is_enabled_auth('oauth2')) {
   44     $wantsurl = new moodle_url('/admin/tool/mobile/launch.php',
   45         array('service' => $serviceshortname, 'passport' => $passport, 'urlscheme' => $urlscheme, 'confirmed' => $confirmed));
   46     $oauthurl = new moodle_url('/auth/oauth2/login.php',
   47         array('id' => $oauthsso, 'sesskey' => sesskey(), 'wantsurl' => $wantsurl));
   48     header('Location: ' . $oauthurl->out(false));
   49     die;
   50 }
   51 
   52 // Check if the plugin is properly configured.
   53 $typeoflogin = get_config('tool_mobile', 'typeoflogin');
   54 if (empty($SESSION->justloggedin) and
   55         $typeoflogin != tool_mobile\api::LOGIN_VIA_BROWSER and
   56         $typeoflogin != tool_mobile\api::LOGIN_VIA_EMBEDDED_BROWSER) {
   57     throw new moodle_exception('pluginnotenabledorconfigured', 'tool_mobile');
   58 }
   59 
   60 // Check if the service exists and is enabled.
   61 $service = $DB->get_record('external_services', array('shortname' => $serviceshortname, 'enabled' => 1));
   62 if (empty($service)) {
   63     throw new moodle_exception('servicenotavailable', 'webservice');
   64 }
   65 
   66 require_login(0, false);
   67 
   68 // Require an active user: not guest, not suspended.
   69 core_user::require_active_user($USER);
   70 
   71 // Get an existing token or create a new one.
   72 $timenow = time();
   73 $token = external_generate_token_for_current_user($service);
   74 $privatetoken = $token->privatetoken;
   75 external_log_token_request($token);
   76 
   77 // Don't return the private token if the user didn't just log in and a new token wasn't created.
   78 if (empty($SESSION->justloggedin) and $token->timecreated < $timenow) {
   79     $privatetoken = null;
   80 }
   81 
   82 $siteadmin = has_capability('moodle/site:config', context_system::instance(), $USER->id);
   83 
   84 // Passport is generated in the mobile app, so the app opening can be validated using that variable.
   85 // Passports are valid only one time, it's deleted in the app once used.
   86 $siteid = md5($CFG->wwwroot . $passport);
   87 $apptoken = $siteid . ':::' . $token->token;
   88 if ($privatetoken and is_https() and !$siteadmin) {
   89     $apptoken .= ':::' . $privatetoken;
   90 }
   91 
   92 $apptoken = base64_encode($apptoken);
   93 
   94 // Redirect using the custom URL scheme checking first if a URL scheme is forced in the site settings.
   95 $forcedurlscheme = get_config('tool_mobile', 'forcedurlscheme');
   96 if (!empty($forcedurlscheme)) {
   97     $urlscheme = $forcedurlscheme;
   98 }
   99 
  100 $location = "$urlscheme://token=$apptoken";
  101 
  102 // For iOS 10 onwards, we have to simulate a user click.
  103 // If we come from the confirmation page, we should display a nicer page.
  104 $isios = core_useragent::is_ios();
  105 if ($confirmed or $isios) {
  106     $PAGE->set_context(context_system::instance());
  107     $PAGE->set_heading($COURSE->fullname);
  108     $params = array('service' => $serviceshortname, 'passport' => $passport, 'urlscheme' => $urlscheme, 'confirmed' => $confirmed);
  109     $PAGE->set_url("/$CFG->admin/tool/mobile/launch.php", $params);
  110 
  111     echo $OUTPUT->header();
  112     if ($confirmed) {
  113         $confirmedstr = get_string('confirmed');
  114         $PAGE->navbar->add($confirmedstr);
  115         $PAGE->set_title($confirmedstr);
  116         echo $OUTPUT->notification($confirmedstr, \core\output\notification::NOTIFY_SUCCESS);
  117         echo $OUTPUT->box_start('generalbox centerpara boxwidthnormal boxaligncenter');
  118         echo $OUTPUT->single_button(new moodle_url('/course/'), get_string('courses'));
  119         echo $OUTPUT->box_end();
  120     }
  121 
  122     $notice = get_string('clickheretolaunchtheapp', 'tool_mobile');
  123     echo html_writer::link($location, $notice, array('id' => 'launchapp'));
  124     echo html_writer::script(
  125         "window.onload = function() {
  126             document.getElementById('launchapp').click();
  127         };"
  128     );
  129     echo $OUTPUT->footer();
  130 } else {
  131     // For Android a http redirect will do fine.
  132     header('Location: ' . $location);
  133     die;
  134 }