[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/webservice/ -> externallib.php (source)

   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  /**
  19   * external API for mobile web services
  20   *
  21   * @package    core_webservice
  22   * @category   external
  23   * @copyright  2011 Jerome Mouneyrac <[email protected]>
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  defined('MOODLE_INTERNAL') || die;
  28  
  29  require_once("$CFG->libdir/externallib.php");
  30  
  31  /**
  32   * Web service related functions
  33   *
  34   * @package    core_webservice
  35   * @category   external
  36   * @copyright  2011 Jerome Mouneyrac <[email protected]>
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   * @since Moodle 2.2
  39   */
  40  class core_webservice_external extends external_api {
  41  
  42      /**
  43       * Returns description of method parameters
  44       *
  45       * @return external_function_parameters
  46       * @since Moodle 2.2
  47       */
  48      public static function get_site_info_parameters() {
  49          return new external_function_parameters(
  50              array('serviceshortnames' => new external_multiple_structure (
  51                  new external_value(
  52                      PARAM_ALPHANUMEXT,
  53                      'service shortname'),
  54                      'DEPRECATED PARAMETER - it was a design error in the original implementation. \
  55                      It is ignored now. (parameter kept for backward compatibility)',
  56                      VALUE_DEFAULT,
  57                      array()
  58                  ),
  59              )
  60          );
  61      }
  62  
  63      /**
  64       * Return user information including profile picture + basic site information
  65       * Note:
  66       * - no capability checking because we return only known information about logged user
  67       *
  68       * @param array $serviceshortnames - DEPRECATED PARAMETER - values will be ignored -
  69       * it was an original design error, we keep for backward compatibility.
  70       * @return array site info
  71       * @since Moodle 2.2
  72       */
  73      public static function get_site_info($serviceshortnames = array()) {
  74          global $USER, $SITE, $CFG, $DB;
  75  
  76          $params = self::validate_parameters(self::get_site_info_parameters(),
  77                        array('serviceshortnames'=>$serviceshortnames));
  78  
  79          $profileimageurl = moodle_url::make_pluginfile_url(
  80                  context_user::instance($USER->id)->id, 'user', 'icon', null, '/', 'f1');
  81  
  82          // Site information.
  83          $siteinfo =  array(
  84              'sitename' => $SITE->fullname,
  85              'siteurl' => $CFG->wwwroot,
  86              'username' => $USER->username,
  87              'firstname' => $USER->firstname,
  88              'lastname' => $USER->lastname,
  89              'fullname' => fullname($USER),
  90              'lang' => current_language(),
  91              'userid' => $USER->id,
  92              'userpictureurl' => $profileimageurl->out(false)
  93          );
  94  
  95          // Retrieve the service and functions from the web service linked to the token
  96          // If you call this function directly from external (not a web service call),
  97          // then it will still return site info without information about a service
  98          // Note: wsusername/wspassword ws authentication is not supported.
  99          $functions = array();
 100          if ($CFG->enablewebservices) { // No need to check token if web service are disabled and not a ws call.
 101              $token = optional_param('wstoken', '', PARAM_ALPHANUM);
 102  
 103              if (!empty($token)) { // No need to run if not a ws call.
 104                  // Retrieve service shortname.
 105                  $servicesql = 'SELECT s.*
 106                                 FROM {external_services} s, {external_tokens} t
 107                                 WHERE t.externalserviceid = s.id AND token = ? AND t.userid = ? AND s.enabled = 1';
 108                  $service = $DB->get_record_sql($servicesql, array($token, $USER->id));
 109  
 110                  $siteinfo['downloadfiles'] = $service->downloadfiles;
 111                  $siteinfo['uploadfiles'] = $service->uploadfiles;
 112  
 113                  if (!empty($service)) {
 114                      // Return the release and version number for web service users only.
 115                      $siteinfo['release'] = $CFG->release;
 116                      $siteinfo['version'] = $CFG->version;
 117                      // Retrieve the functions.
 118                      $functionssql = "SELECT f.*
 119                              FROM {external_functions} f, {external_services_functions} sf
 120                              WHERE f.name = sf.functionname AND sf.externalserviceid = ?";
 121                      $functions = $DB->get_records_sql($functionssql, array($service->id));
 122                  } else {
 123                      throw new coding_exception('No service found in get_site_info: something is buggy, \
 124                                                  it should have fail at the ws server authentication layer.');
 125                  }
 126              }
 127          }
 128  
 129          // Build up the returned values of the list of functions.
 130          $componentversions = array();
 131          $availablefunctions = array();
 132          foreach ($functions as $function) {
 133              $functioninfo = array();
 134              $functioninfo['name'] = $function->name;
 135              if ($function->component == 'moodle' || $function->component == 'core') {
 136                  $version = $CFG->version; // Moodle version.
 137              } else {
 138                  $versionpath = core_component::get_component_directory($function->component).'/version.php';
 139                  if (is_readable($versionpath)) {
 140                      // We store the component version once retrieved (so we don't load twice the version.php).
 141                      if (!isset($componentversions[$function->component])) {
 142                          include($versionpath);
 143                          $componentversions[$function->component] = $plugin->version;
 144                          $version = $plugin->version;
 145                      } else {
 146                          $version = $componentversions[$function->component];
 147                      }
 148                  } else {
 149                      // Function component should always have a version.php,
 150                      // otherwise the function should have been described with component => 'moodle'.
 151                      throw new moodle_exception('missingversionfile', 'webservice', '', $function->component);
 152                  }
 153              }
 154              $functioninfo['version'] = $version;
 155              $availablefunctions[] = $functioninfo;
 156          }
 157  
 158          $siteinfo['functions'] = $availablefunctions;
 159  
 160          // Mobile CSS theme and alternative login url.
 161          $siteinfo['mobilecssurl'] = $CFG->mobilecssurl;
 162  
 163          return $siteinfo;
 164      }
 165  
 166      /**
 167       * Returns description of method result value
 168       *
 169       * @return external_single_structure
 170       * @since Moodle 2.2
 171       */
 172      public static function get_site_info_returns() {
 173          return new external_single_structure(
 174              array(
 175                  'sitename'       => new external_value(PARAM_RAW, 'site name'),
 176                  'username'       => new external_value(PARAM_RAW, 'username'),
 177                  'firstname'      => new external_value(PARAM_TEXT, 'first name'),
 178                  'lastname'       => new external_value(PARAM_TEXT, 'last name'),
 179                  'fullname'       => new external_value(PARAM_TEXT, 'user full name'),
 180                  'lang'           => new external_value(PARAM_LANG, 'user language'),
 181                  'userid'         => new external_value(PARAM_INT, 'user id'),
 182                  'siteurl'        => new external_value(PARAM_RAW, 'site url'),
 183                  'userpictureurl' => new external_value(PARAM_URL, 'the user profile picture.
 184                      Warning: this url is the public URL that only works when forcelogin is set to NO and guestaccess is set to YES.
 185                      In order to retrieve user profile pictures independently of the Moodle config, replace "pluginfile.php" by
 186                      "webservice/pluginfile.php?token=WSTOKEN&file="
 187                      Of course the user can only see profile picture depending
 188                      on his/her permissions. Moreover it is recommended to use HTTPS too.'),
 189                  'functions'      => new external_multiple_structure(
 190                      new external_single_structure(
 191                          array(
 192                              'name' => new external_value(PARAM_RAW, 'function name'),
 193                              'version' => new external_value(PARAM_TEXT,
 194                                          'The version number of the component to which the function belongs')
 195                          ), 'functions that are available')
 196                      ),
 197                  'downloadfiles'  => new external_value(PARAM_INT, '1 if users are allowed to download files, 0 if not',
 198                                                         VALUE_OPTIONAL),
 199                  'uploadfiles'  => new external_value(PARAM_INT, '1 if users are allowed to upload files, 0 if not',
 200                                                         VALUE_OPTIONAL),
 201                  'release'  => new external_value(PARAM_TEXT, 'Moodle release number', VALUE_OPTIONAL),
 202                  'version'  => new external_value(PARAM_TEXT, 'Moodle version number', VALUE_OPTIONAL),
 203                  'mobilecssurl'  => new external_value(PARAM_URL, 'Mobile custom CSS theme', VALUE_OPTIONAL)
 204              )
 205          );
 206      }
 207  }
 208  
 209  /**
 210   * Deprecated web service related functions
 211   *
 212   * @package    core_webservice
 213   * @category   external
 214   * @copyright  2011 Jerome Mouneyrac <[email protected]>
 215   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 216   * @deprecated Moodle 2.2 MDL-29106 - please do not use this class any more.
 217   * @see core_webservice_external
 218   * @since Moodle 2.1
 219   */
 220  class moodle_webservice_external extends external_api {
 221  
 222      /**
 223       * Returns description of method parameters
 224       *
 225       * @return external_function_parameters
 226       * @deprecated Moodle 2.2 - please do not use this function any more.
 227       * @see core_webservice_external::get_site_info_parameters
 228       * @since Moodle 2.1
 229       */
 230      public static function get_siteinfo_parameters() {
 231          return core_webservice_external::get_site_info_parameters();
 232      }
 233  
 234      /**
 235       * Return user information including profile picture + basic site information
 236       * Note:
 237       * - no capability checking because we return just known information by logged user
 238       *
 239       * @param array $serviceshortnames of service shortnames - the functions of these services will be returned
 240       * @return array
 241       * @deprecated Moodle 2.2 - please do not use this function any more.
 242       * @see core_webservice_external::get_site_info
 243       * @since Moodle 2.1
 244       */
 245      public function get_siteinfo($serviceshortnames = array()) {
 246          return core_webservice_external::get_site_info($serviceshortnames);
 247      }
 248  
 249      /**
 250       * Returns description of method result value
 251       *
 252       * @return external_single_structure
 253       * @deprecated Moodle 2.2 - please do not use this function any more.
 254       * @see core_webservice_external::get_site_info_returns
 255       * @since Moodle 2.1
 256       */
 257      public static function get_siteinfo_returns() {
 258          return core_webservice_external::get_site_info_returns();
 259      }
 260  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1