Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402] [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 = array("usecomments", "usetags", "enablenotes", "messaging", "enableblogs",
 160                                      "enablecompletion", "enablebadges", "messagingallusers", "enablecustomreports");
 161          foreach ($advancedfeatures as $feature) {
 162              if (isset($CFG->{$feature})) {
 163                  $siteinfo['advancedfeatures'][] = array(
 164                      'name' => $feature,
 165                      'value' => (int) $CFG->{$feature}
 166                  );
 167              }
 168          }
 169          // Special case mnet_dispatcher_mode.
 170          $siteinfo['advancedfeatures'][] = array(
 171              'name' => 'mnet_dispatcher_mode',
 172              'value' => ($CFG->mnet_dispatcher_mode == 'strict') ? 1 : 0
 173          );
 174  
 175          // User can manage own files.
 176          $siteinfo['usercanmanageownfiles'] = has_capability('moodle/user:manageownfiles', $context);
 177  
 178          // User quota. 0 means user can ignore the quota.
 179          $siteinfo['userquota'] = 0;
 180          if (!has_capability('moodle/user:ignoreuserquota', $context)) {
 181              $siteinfo['userquota'] = (int) $CFG->userquota; // Cast to int to ensure value is not higher than PHP_INT_MAX.
 182          }
 183  
 184          // User max upload file size. -1 means the user can ignore the upload file size.
 185          // Cast to int to ensure value is not higher than PHP_INT_MAX.
 186          $siteinfo['usermaxuploadfilesize'] = (int) get_user_max_upload_file_size($context, $CFG->maxbytes);
 187  
 188          // User home page.
 189          $siteinfo['userhomepage'] = get_home_page();
 190  
 191          // Calendar.
 192          $siteinfo['sitecalendartype'] = $CFG->calendartype;
 193          if (empty($USER->calendartype)) {
 194              $siteinfo['usercalendartype'] = $CFG->calendartype;
 195          } else {
 196              $siteinfo['usercalendartype'] = $USER->calendartype;
 197          }
 198          $siteinfo['userissiteadmin'] = is_siteadmin();
 199  
 200          // User key, to avoid using the WS token for fetching assets.
 201          $siteinfo['userprivateaccesskey'] = get_user_key('core_files', $USER->id);
 202  
 203          // Current theme.
 204          $siteinfo['theme'] = clean_param($PAGE->theme->name, PARAM_THEME);  // We always clean to avoid problem with old sites.
 205  
 206          $siteinfo['limitconcurrentlogins'] = (int) $CFG->limitconcurrentlogins;
 207          if (!empty($CFG->limitconcurrentlogins)) {
 208              // For performance, only when enabled.
 209              $siteinfo['usersessionscount'] = $DB->count_records('sessions', ['userid' => $USER->id]);
 210          }
 211  
 212          return $siteinfo;
 213      }
 214  
 215      /**
 216       * Returns description of method result value
 217       *
 218       * @return external_single_structure
 219       * @since Moodle 2.2
 220       */
 221      public static function get_site_info_returns() {
 222          return new external_single_structure(
 223              array(
 224                  'sitename'       => new external_value(PARAM_RAW, 'site name'),
 225                  'username'       => new external_value(PARAM_RAW, 'username'),
 226                  'firstname'      => new external_value(PARAM_TEXT, 'first name'),
 227                  'lastname'       => new external_value(PARAM_TEXT, 'last name'),
 228                  'fullname'       => new external_value(PARAM_TEXT, 'user full name'),
 229                  'lang'           => new external_value(PARAM_LANG, 'Current language.'),
 230                  'userid'         => new external_value(PARAM_INT, 'user id'),
 231                  'siteurl'        => new external_value(PARAM_RAW, 'site url'),
 232                  'userpictureurl' => new external_value(PARAM_URL, 'the user profile picture.
 233                      Warning: this url is the public URL that only works when forcelogin is set to NO and guestaccess is set to YES.
 234                      In order to retrieve user profile pictures independently of the Moodle config, replace "pluginfile.php" by
 235                      "webservice/pluginfile.php?token=WSTOKEN&file="
 236                      Of course the user can only see profile picture depending
 237                      on his/her permissions. Moreover it is recommended to use HTTPS too.'),
 238                  'functions'      => new external_multiple_structure(
 239                      new external_single_structure(
 240                          array(
 241                              'name' => new external_value(PARAM_RAW, 'function name'),
 242                              'version' => new external_value(PARAM_TEXT,
 243                                          'The version number of the component to which the function belongs')
 244                          ), 'functions that are available')
 245                      ),
 246                  'downloadfiles'  => new external_value(PARAM_INT, '1 if users are allowed to download files, 0 if not',
 247                                                         VALUE_OPTIONAL),
 248                  'uploadfiles'  => new external_value(PARAM_INT, '1 if users are allowed to upload files, 0 if not',
 249                                                         VALUE_OPTIONAL),
 250                  'release'  => new external_value(PARAM_TEXT, 'Moodle release number', VALUE_OPTIONAL),
 251                  'version'  => new external_value(PARAM_TEXT, 'Moodle version number', VALUE_OPTIONAL),
 252                  'mobilecssurl'  => new external_value(PARAM_URL, 'Mobile custom CSS theme', VALUE_OPTIONAL),
 253                  'advancedfeatures' => new external_multiple_structure(
 254                      new external_single_structure(
 255                          array(
 256                              'name'  => new external_value(PARAM_ALPHANUMEXT, 'feature name'),
 257                              'value' => new external_value(PARAM_INT, 'feature value. Usually 1 means enabled.')
 258                          ),
 259                          'Advanced features availability'
 260                      ),
 261                      'Advanced features availability',
 262                      VALUE_OPTIONAL
 263                  ),
 264                  'usercanmanageownfiles' => new external_value(PARAM_BOOL,
 265                                              'true if the user can manage his own files', VALUE_OPTIONAL),
 266                  'userquota' => new external_value(PARAM_INT,
 267                                      'user quota (bytes). 0 means user can ignore the quota', VALUE_OPTIONAL),
 268                  'usermaxuploadfilesize' => new external_value(PARAM_INT,
 269                                              'user max upload file size (bytes). -1 means the user can ignore the upload file size',
 270                                              VALUE_OPTIONAL),
 271                  'userhomepage' => new external_value(PARAM_INT,
 272                                                          'the default home page for the user: 0 for the site home, 1 for dashboard',
 273                                                          VALUE_OPTIONAL),
 274                  'userprivateaccesskey'  => new external_value(PARAM_ALPHANUM, 'Private user access key for fetching files.',
 275                      VALUE_OPTIONAL),
 276                  'siteid'  => new external_value(PARAM_INT, 'Site course ID', VALUE_OPTIONAL),
 277                  'sitecalendartype'  => new external_value(PARAM_PLUGIN, 'Calendar type set in the site.', VALUE_OPTIONAL),
 278                  'usercalendartype'  => new external_value(PARAM_PLUGIN, 'Calendar typed used by the user.', VALUE_OPTIONAL),
 279                  'userissiteadmin'  => new external_value(PARAM_BOOL, 'Whether the user is a site admin or not.', VALUE_OPTIONAL),
 280                  'theme'  => new external_value(PARAM_THEME, 'Current theme for the user.', VALUE_OPTIONAL),
 281                  'limitconcurrentlogins' => new external_value(PARAM_INT, 'Number of concurrent sessions allowed', VALUE_OPTIONAL),
 282                  'usersessionscount' => new external_value(PARAM_INT, 'Number of active sessions for current user.
 283                      Only returned when limitconcurrentlogins is used.', VALUE_OPTIONAL),
 284              )
 285          );
 286      }
 287  }