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  use core_external\external_function_parameters;
  18  use core_external\external_multiple_structure;
  19  use core_external\external_single_structure;
  20  use core_external\external_value;
  21  /**
  22   * Web service related functions
  23   *
  24   * @package    core_webservice
  25   * @category   external
  26   * @copyright  2011 Jerome Mouneyrac <jerome@moodle.com>
  27   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  28   * @since Moodle 2.2
  29   */
  30  class core_webservice_external extends \core_external\external_api {
  31  
  32      /**
  33       * Returns description of method parameters
  34       *
  35       * @return external_function_parameters
  36       * @since Moodle 2.2
  37       */
  38      public static function get_site_info_parameters() {
  39          return new external_function_parameters(
  40              array('serviceshortnames' => new external_multiple_structure (
  41                  new external_value(
  42                      PARAM_ALPHANUMEXT,
  43                      'service shortname'),
  44                      'DEPRECATED PARAMETER - it was a design error in the original implementation. \
  45                      It is ignored now. (parameter kept for backward compatibility)',
  46                      VALUE_DEFAULT,
  47                      array()
  48                  ),
  49              )
  50          );
  51      }
  52  
  53      /**
  54       * Return user information including profile picture + basic site information
  55       * Note:
  56       * - no capability checking because we return only known information about logged user
  57       *
  58       * @param array $serviceshortnames - DEPRECATED PARAMETER - values will be ignored -
  59       * it was an original design error, we keep for backward compatibility.
  60       * @return array site info
  61       * @since Moodle 2.2
  62       */
  63      public static function get_site_info($serviceshortnames = array()) {
  64          global $USER, $SITE, $CFG, $DB, $PAGE;
  65  
  66          $params = self::validate_parameters(self::get_site_info_parameters(),
  67                        array('serviceshortnames'=>$serviceshortnames));
  68  
  69          $context = context_user::instance($USER->id);
  70          $systemcontext = context_system::instance();
  71  
  72          $userpicture = new user_picture($USER);
  73          $userpicture->size = 1; // Size f1.
  74          $profileimageurl = $userpicture->get_url($PAGE);
  75  
  76          // Site information.
  77          $siteinfo =  array(
  78              'sitename' => \core_external\util::format_string($SITE->fullname, $systemcontext),
  79              'siteurl' => $CFG->wwwroot,
  80              'username' => $USER->username,
  81              'firstname' => $USER->firstname,
  82              'lastname' => $USER->lastname,
  83              'fullname' => fullname($USER),
  84              'lang' => clean_param(current_language(), PARAM_LANG),
  85              'userid' => $USER->id,
  86              'userpictureurl' => $profileimageurl->out(false),
  87              'siteid' => SITEID
  88          );
  89  
  90          // Retrieve the service and functions from the web service linked to the token
  91          // If you call this function directly from external (not a web service call),
  92          // then it will still return site info without information about a service
  93          // Note: wsusername/wspassword ws authentication is not supported.
  94          $functions = array();
  95          if ($CFG->enablewebservices) { // No need to check token if web service are disabled and not a ws call.
  96              $token = optional_param('wstoken', '', PARAM_ALPHANUM);
  97  
  98              if (!empty($token)) { // No need to run if not a ws call.
  99                  // Retrieve service shortname.
 100                  $servicesql = 'SELECT s.*
 101                                 FROM {external_services} s, {external_tokens} t
 102                                 WHERE t.externalserviceid = s.id AND token = ? AND t.userid = ? AND s.enabled = 1';
 103                  $service = $DB->get_record_sql($servicesql, array($token, $USER->id));
 104  
 105                  $siteinfo['downloadfiles'] = $service->downloadfiles;
 106                  $siteinfo['uploadfiles'] = $service->uploadfiles;
 107  
 108                  if (!empty($service)) {
 109                      // Return the release and version number for web service users only.
 110                      $siteinfo['release'] = $CFG->release;
 111                      $siteinfo['version'] = $CFG->version;
 112                      // Retrieve the functions.
 113                      $functionssql = "SELECT f.*
 114                              FROM {external_functions} f, {external_services_functions} sf
 115                              WHERE f.name = sf.functionname AND sf.externalserviceid = ?";
 116                      $functions = $DB->get_records_sql($functionssql, array($service->id));
 117                  } else {
 118                      throw new coding_exception('No service found in get_site_info: something is buggy, \
 119                                                  it should have fail at the ws server authentication layer.');
 120                  }
 121              }
 122          }
 123  
 124          // Build up the returned values of the list of functions.
 125          $componentversions = array();
 126          $availablefunctions = array();
 127          foreach ($functions as $function) {
 128              $functioninfo = array();
 129              $functioninfo['name'] = $function->name;
 130              if ($function->component == 'moodle' || $function->component == 'core') {
 131                  $version = $CFG->version; // Moodle version.
 132              } else {
 133                  $versionpath = core_component::get_component_directory($function->component).'/version.php';
 134                  if (is_readable($versionpath)) {
 135                      // We store the component version once retrieved (so we don't load twice the version.php).
 136                      if (!isset($componentversions[$function->component])) {
 137                          $plugin = new stdClass();
 138                          include($versionpath);
 139                          $componentversions[$function->component] = $plugin->version;
 140                          $version = $plugin->version;
 141                      } else {
 142                          $version = $componentversions[$function->component];
 143                      }
 144                  } else {
 145                      // Ignore this component or plugin, it was probably incorrectly uninstalled.
 146                      continue;
 147                  }
 148              }
 149              $functioninfo['version'] = $version;
 150              $availablefunctions[] = $functioninfo;
 151          }
 152  
 153          $siteinfo['functions'] = $availablefunctions;
 154  
 155          // Mobile CSS theme and alternative login url.
 156          $siteinfo['mobilecssurl'] = !empty($CFG->mobilecssurl) ? $CFG->mobilecssurl : '';
 157  
 158          // Retrieve some advanced features. Only enable/disable ones (bool).
 159          $advancedfeatures = ["usecomments", "usetags", "enablenotes", "messaging", "enableblogs",
 160              "enablecompletion", "enablebadges", "messagingallusers", "enablecustomreports", "enableglobalsearch"];
 161  
 162          foreach ($advancedfeatures as $feature) {
 163              if (isset($CFG->{$feature})) {
 164                  $siteinfo['advancedfeatures'][] = array(
 165                      'name' => $feature,
 166                      'value' => (int) $CFG->{$feature}
 167                  );
 168              }
 169          }
 170          // Special case mnet_dispatcher_mode.
 171          $siteinfo['advancedfeatures'][] = array(
 172              'name' => 'mnet_dispatcher_mode',
 173              'value' => ($CFG->mnet_dispatcher_mode == 'strict') ? 1 : 0
 174          );
 175  
 176          // User can manage own files.
 177          $siteinfo['usercanmanageownfiles'] = has_capability('moodle/user:manageownfiles', $context);
 178  
 179          // User quota. 0 means user can ignore the quota.
 180          $siteinfo['userquota'] = 0;
 181          if (!has_capability('moodle/user:ignoreuserquota', $context)) {
 182              $siteinfo['userquota'] = (int) $CFG->userquota; // Cast to int to ensure value is not higher than PHP_INT_MAX.
 183          }
 184  
 185          // User max upload file size. -1 means the user can ignore the upload file size.
 186          // Cast to int to ensure value is not higher than PHP_INT_MAX.
 187          $siteinfo['usermaxuploadfilesize'] = (int) get_user_max_upload_file_size($context, $CFG->maxbytes);
 188  
 189          // User home page.
 190          $siteinfo['userhomepage'] = get_home_page();
 191  
 192          // Calendar.
 193          $siteinfo['sitecalendartype'] = $CFG->calendartype;
 194          if (empty($USER->calendartype)) {
 195              $siteinfo['usercalendartype'] = $CFG->calendartype;
 196          } else {
 197              $siteinfo['usercalendartype'] = $USER->calendartype;
 198          }
 199          $siteinfo['userissiteadmin'] = is_siteadmin();
 200  
 201          // User key, to avoid using the WS token for fetching assets.
 202          $siteinfo['userprivateaccesskey'] = get_user_key('core_files', $USER->id);
 203  
 204          // Current theme.
 205          $siteinfo['theme'] = clean_param($PAGE->theme->name, PARAM_THEME);  // We always clean to avoid problem with old sites.
 206  
 207          $siteinfo['limitconcurrentlogins'] = (int) $CFG->limitconcurrentlogins;
 208          if (!empty($CFG->limitconcurrentlogins)) {
 209              // For performance, only when enabled.
 210              $siteinfo['usersessionscount'] = $DB->count_records('sessions', ['userid' => $USER->id]);
 211          }
 212  
 213          return $siteinfo;
 214      }
 215  
 216      /**
 217       * Returns description of method result value
 218       *
 219       * @return external_single_structure
 220       * @since Moodle 2.2
 221       */
 222      public static function get_site_info_returns() {
 223          return new external_single_structure(
 224              array(
 225                  'sitename'       => new external_value(PARAM_RAW, 'site name'),
 226                  'username'       => new external_value(PARAM_RAW, 'username'),
 227                  'firstname'      => new external_value(PARAM_TEXT, 'first name'),
 228                  'lastname'       => new external_value(PARAM_TEXT, 'last name'),
 229                  'fullname'       => new external_value(PARAM_TEXT, 'user full name'),
 230                  'lang'           => new external_value(PARAM_LANG, 'Current language.'),
 231                  'userid'         => new external_value(PARAM_INT, 'user id'),
 232                  'siteurl'        => new external_value(PARAM_RAW, 'site url'),
 233                  'userpictureurl' => new external_value(PARAM_URL, 'the user profile picture.
 234                      Warning: this url is the public URL that only works when forcelogin is set to NO and guestaccess is set to YES.
 235                      In order to retrieve user profile pictures independently of the Moodle config, replace "pluginfile.php" by
 236                      "webservice/pluginfile.php?token=WSTOKEN&file="
 237                      Of course the user can only see profile picture depending
 238                      on his/her permissions. Moreover it is recommended to use HTTPS too.'),
 239                  'functions'      => new external_multiple_structure(
 240                      new external_single_structure(
 241                          array(
 242                              'name' => new external_value(PARAM_RAW, 'function name'),
 243                              'version' => new external_value(PARAM_TEXT,
 244                                          'The version number of the component to which the function belongs')
 245                          ), 'functions that are available')
 246                      ),
 247                  'downloadfiles'  => new external_value(PARAM_INT, '1 if users are allowed to download files, 0 if not',
 248                                                         VALUE_OPTIONAL),
 249                  'uploadfiles'  => new external_value(PARAM_INT, '1 if users are allowed to upload files, 0 if not',
 250                                                         VALUE_OPTIONAL),
 251                  'release'  => new external_value(PARAM_TEXT, 'Moodle release number', VALUE_OPTIONAL),
 252                  'version'  => new external_value(PARAM_TEXT, 'Moodle version number', VALUE_OPTIONAL),
 253                  'mobilecssurl'  => new external_value(PARAM_URL, 'Mobile custom CSS theme', VALUE_OPTIONAL),
 254                  'advancedfeatures' => new external_multiple_structure(
 255                      new external_single_structure(
 256                          array(
 257                              'name'  => new external_value(PARAM_ALPHANUMEXT, 'feature name'),
 258                              'value' => new external_value(PARAM_INT, 'feature value. Usually 1 means enabled.')
 259                          ),
 260                          'Advanced features availability'
 261                      ),
 262                      'Advanced features availability',
 263                      VALUE_OPTIONAL
 264                  ),
 265                  'usercanmanageownfiles' => new external_value(PARAM_BOOL,
 266                                              'true if the user can manage his own files', VALUE_OPTIONAL),
 267                  'userquota' => new external_value(PARAM_INT,
 268                                      'user quota (bytes). 0 means user can ignore the quota', VALUE_OPTIONAL),
 269                  'usermaxuploadfilesize' => new external_value(PARAM_INT,
 270                                              'user max upload file size (bytes). -1 means the user can ignore the upload file size',
 271                                              VALUE_OPTIONAL),
 272                  'userhomepage' => new external_value(PARAM_INT,
 273                                                          'the default home page for the user: 0 for the site home, 1 for dashboard',
 274                                                          VALUE_OPTIONAL),
 275                  'userprivateaccesskey'  => new external_value(PARAM_ALPHANUM, 'Private user access key for fetching files.',
 276                      VALUE_OPTIONAL),
 277                  'siteid'  => new external_value(PARAM_INT, 'Site course ID', VALUE_OPTIONAL),
 278                  'sitecalendartype'  => new external_value(PARAM_PLUGIN, 'Calendar type set in the site.', VALUE_OPTIONAL),
 279                  'usercalendartype'  => new external_value(PARAM_PLUGIN, 'Calendar typed used by the user.', VALUE_OPTIONAL),
 280                  'userissiteadmin'  => new external_value(PARAM_BOOL, 'Whether the user is a site admin or not.', VALUE_OPTIONAL),
 281                  'theme'  => new external_value(PARAM_THEME, 'Current theme for the user.', VALUE_OPTIONAL),
 282                  'limitconcurrentlogins' => new external_value(PARAM_INT, 'Number of concurrent sessions allowed', VALUE_OPTIONAL),
 283                  'usersessionscount' => new external_value(PARAM_INT, 'Number of active sessions for current user.
 284                      Only returned when limitconcurrentlogins is used.', VALUE_OPTIONAL),
 285              )
 286          );
 287      }
 288  }