PageRenderTime 24ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 1ms

/webservice/externallib.php

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