Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 and 403]

   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   * Web service test client.
  19   *
  20   * @package   core_webservice
  21   * @copyright 2009 Moodle Pty Ltd (http://moodle.com)
  22   * @author    Jerome Mouneyrac
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  require('../config.php');
  27  
  28  require_login();
  29  
  30  $usercontext = context_user::instance($USER->id);
  31  
  32  $PAGE->set_context($usercontext);
  33  $PAGE->set_url('/user/managetoken.php');
  34  $PAGE->set_title(get_string('securitykeys', 'webservice'));
  35  $PAGE->set_pagelayout('admin');
  36  
  37  $rsstokenboxhtml = $webservicetokenboxhtml = '';
  38  // Manage user web service tokens.
  39  if ( !is_siteadmin($USER->id)
  40      && !empty($CFG->enablewebservices)
  41      && has_capability('moodle/webservice:createtoken', $usercontext )) {
  42      require_once($CFG->dirroot.'/webservice/lib.php');
  43  
  44      $action  = optional_param('action', '', PARAM_ALPHANUMEXT);
  45      $tokenid = optional_param('tokenid', '', PARAM_SAFEDIR);
  46      $confirm = optional_param('confirm', 0, PARAM_BOOL);
  47  
  48      $webservice = new webservice(); // Load the webservice library.
  49      $wsrenderer = $PAGE->get_renderer('core', 'webservice');
  50  
  51      if ($action == 'resetwstoken') {
  52          $token = $webservice->get_created_by_user_ws_token($USER->id, $tokenid);
  53          // Display confirmation page to Reset the token.
  54          if (!$confirm) {
  55              $resetconfirmation = $wsrenderer->user_reset_token_confirmation($token);
  56          } else {
  57              // Delete the token that need to be regenerated.
  58              require_sesskey();
  59              $webservice->delete_user_ws_token($tokenid);
  60  
  61              // Now re-create one against the same service.
  62              \core_external\util::generate_token(
  63                  EXTERNAL_TOKEN_PERMANENT,
  64                  \core_external\util::get_service_by_id($token->externalserviceid),
  65                  $USER->id,
  66                  context_system::instance()
  67              );
  68  
  69              redirect($PAGE->url, get_string('resettokencomplete', 'core_webservice'));
  70          }
  71      }
  72  
  73      // No point creating the table is we're just displaying a confirmation screen.
  74      if (empty($resetconfirmation)) {
  75          $webservice->generate_user_ws_tokens($USER->id); // Generate all token that need to be generated.
  76          $tokens = $webservice->get_user_ws_tokens($USER->id);
  77          foreach ($tokens as $token) {
  78              if ($token->restrictedusers) {
  79                  $authlist = $webservice->get_ws_authorised_user($token->wsid, $USER->id);
  80                  if (empty($authlist)) {
  81                      $token->enabled = false;
  82                  }
  83              }
  84          }
  85          $webservicetokenboxhtml = $wsrenderer->user_webservice_tokens_box($tokens, $USER->id,
  86                  $CFG->enablewsdocumentation); // Display the box for web service token.
  87      }
  88  }
  89  
  90  // RSS keys.
  91  if (!empty($CFG->enablerssfeeds)) {
  92      require_once($CFG->dirroot.'/lib/rsslib.php');
  93  
  94      $action  = optional_param('action', '', PARAM_ALPHANUMEXT);
  95      $confirm = optional_param('confirm', 0, PARAM_BOOL);
  96  
  97      $rssrenderer = $PAGE->get_renderer('core', 'rss');
  98  
  99      if ($action == 'resetrsstoken') {
 100          // Display confirmation page to Reset the token.
 101          if (!$confirm) {
 102              $resetconfirmation = $rssrenderer->user_reset_rss_token_confirmation();
 103          } else {
 104              require_sesskey();
 105              rss_delete_token($USER->id);
 106              redirect($PAGE->url, get_string('resettokencomplete', 'core_webservice'));
 107          }
 108      }
 109      if (empty($resetconfirmation)) {
 110          $token = rss_get_token($USER->id);
 111          $rsstokenboxhtml = $rssrenderer->user_rss_token_box($token); // Display the box for the user's RSS token.
 112      }
 113  }
 114  
 115  // PAGE OUTPUT.
 116  echo $OUTPUT->header();
 117  if (!empty($resetconfirmation)) {
 118      echo $resetconfirmation;
 119  } else {
 120  
 121      if (!empty($SESSION->webservicenewlycreatedtoken)) {
 122          $webservicemanager = new webservice();
 123          $newtoken = $webservicemanager->get_created_by_user_ws_token(
 124              $USER->id,
 125              $SESSION->webservicenewlycreatedtoken
 126          );
 127          if ($newtoken) {
 128              // Unset the session variable.
 129              unset($SESSION->webservicenewlycreatedtoken);
 130              // Display the newly created token.
 131              echo $OUTPUT->render_from_template(
 132                  'core_admin/webservice_token_new', ['token' => $newtoken->token, 'tokenname' => $newtoken->tokenname]
 133              );
 134          }
 135      }
 136  
 137      echo $webservicetokenboxhtml;
 138      echo $rsstokenboxhtml;
 139  }
 140  echo $OUTPUT->footer();
 141  
 142