PageRenderTime 70ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/webservice/externallib.php

https://gitlab.com/JrLucena/moodle
PHP | 326 lines | 181 code | 30 blank | 115 comment | 17 complexity | 6b3c6b03090a0d2d0985567fc02c5ee1 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' => current_language(),
  83. 'userid' => $USER->id,
  84. 'userpictureurl' => $profileimageurl->out(false)
  85. );
  86. // Retrieve the service and functions from the web service linked to the token
  87. // If you call this function directly from external (not a web service call),
  88. // then it will still return site info without information about a service
  89. // Note: wsusername/wspassword ws authentication is not supported.
  90. $functions = array();
  91. if ($CFG->enablewebservices) { // No need to check token if web service are disabled and not a ws call.
  92. $token = optional_param('wstoken', '', PARAM_ALPHANUM);
  93. if (!empty($token)) { // No need to run if not a ws call.
  94. // Retrieve service shortname.
  95. $servicesql = 'SELECT s.*
  96. FROM {external_services} s, {external_tokens} t
  97. WHERE t.externalserviceid = s.id AND token = ? AND t.userid = ? AND s.enabled = 1';
  98. $service = $DB->get_record_sql($servicesql, array($token, $USER->id));
  99. $siteinfo['downloadfiles'] = $service->downloadfiles;
  100. $siteinfo['uploadfiles'] = $service->uploadfiles;
  101. if (!empty($service)) {
  102. // Return the release and version number for web service users only.
  103. $siteinfo['release'] = $CFG->release;
  104. $siteinfo['version'] = $CFG->version;
  105. // Retrieve the functions.
  106. $functionssql = "SELECT f.*
  107. FROM {external_functions} f, {external_services_functions} sf
  108. WHERE f.name = sf.functionname AND sf.externalserviceid = ?";
  109. $functions = $DB->get_records_sql($functionssql, array($service->id));
  110. } else {
  111. throw new coding_exception('No service found in get_site_info: something is buggy, \
  112. it should have fail at the ws server authentication layer.');
  113. }
  114. }
  115. }
  116. // Build up the returned values of the list of functions.
  117. $componentversions = array();
  118. $availablefunctions = array();
  119. foreach ($functions as $function) {
  120. $functioninfo = array();
  121. $functioninfo['name'] = $function->name;
  122. if ($function->component == 'moodle' || $function->component == 'core') {
  123. $version = $CFG->version; // Moodle version.
  124. } else {
  125. $versionpath = core_component::get_component_directory($function->component).'/version.php';
  126. if (is_readable($versionpath)) {
  127. // We store the component version once retrieved (so we don't load twice the version.php).
  128. if (!isset($componentversions[$function->component])) {
  129. $plugin = new stdClass();
  130. include($versionpath);
  131. $componentversions[$function->component] = $plugin->version;
  132. $version = $plugin->version;
  133. } else {
  134. $version = $componentversions[$function->component];
  135. }
  136. } else {
  137. // Function component should always have a version.php,
  138. // otherwise the function should have been described with component => 'moodle'.
  139. throw new moodle_exception('missingversionfile', 'webservice', '', $function->component);
  140. }
  141. }
  142. $functioninfo['version'] = $version;
  143. $availablefunctions[] = $functioninfo;
  144. }
  145. $siteinfo['functions'] = $availablefunctions;
  146. // Mobile CSS theme and alternative login url.
  147. $siteinfo['mobilecssurl'] = $CFG->mobilecssurl;
  148. // Retrieve some advanced features. Only enable/disable ones (bool).
  149. $advancedfeatures = array("usecomments", "usetags", "enablenotes", "messaging", "enableblogs",
  150. "enablecompletion", "enablebadges");
  151. foreach ($advancedfeatures as $feature) {
  152. if (isset($CFG->{$feature})) {
  153. $siteinfo['advancedfeatures'][] = array(
  154. 'name' => $feature,
  155. 'value' => (int) $CFG->{$feature}
  156. );
  157. }
  158. }
  159. // Special case mnet_dispatcher_mode.
  160. $siteinfo['advancedfeatures'][] = array(
  161. 'name' => 'mnet_dispatcher_mode',
  162. 'value' => ($CFG->mnet_dispatcher_mode == 'strict') ? 1 : 0
  163. );
  164. // User can manage own files.
  165. $siteinfo['usercanmanageownfiles'] = has_capability('moodle/user:manageownfiles', $context);
  166. // User quota. 0 means user can ignore the quota.
  167. $siteinfo['userquota'] = 0;
  168. if (!has_capability('moodle/user:ignoreuserquota', $context)) {
  169. $siteinfo['userquota'] = $CFG->userquota;
  170. }
  171. // User max upload file size. -1 means the user can ignore the upload file size.
  172. $siteinfo['usermaxuploadfilesize'] = get_user_max_upload_file_size($context, $CFG->maxbytes);
  173. // User home page.
  174. $siteinfo['userhomepage'] = get_home_page();
  175. return $siteinfo;
  176. }
  177. /**
  178. * Returns description of method result value
  179. *
  180. * @return external_single_structure
  181. * @since Moodle 2.2
  182. */
  183. public static function get_site_info_returns() {
  184. return new external_single_structure(
  185. array(
  186. 'sitename' => new external_value(PARAM_RAW, 'site name'),
  187. 'username' => new external_value(PARAM_RAW, 'username'),
  188. 'firstname' => new external_value(PARAM_TEXT, 'first name'),
  189. 'lastname' => new external_value(PARAM_TEXT, 'last name'),
  190. 'fullname' => new external_value(PARAM_TEXT, 'user full name'),
  191. 'lang' => new external_value(PARAM_LANG, 'user language'),
  192. 'userid' => new external_value(PARAM_INT, 'user id'),
  193. 'siteurl' => new external_value(PARAM_RAW, 'site url'),
  194. 'userpictureurl' => new external_value(PARAM_URL, 'the user profile picture.
  195. Warning: this url is the public URL that only works when forcelogin is set to NO and guestaccess is set to YES.
  196. In order to retrieve user profile pictures independently of the Moodle config, replace "pluginfile.php" by
  197. "webservice/pluginfile.php?token=WSTOKEN&file="
  198. Of course the user can only see profile picture depending
  199. on his/her permissions. Moreover it is recommended to use HTTPS too.'),
  200. 'functions' => new external_multiple_structure(
  201. new external_single_structure(
  202. array(
  203. 'name' => new external_value(PARAM_RAW, 'function name'),
  204. 'version' => new external_value(PARAM_TEXT,
  205. 'The version number of the component to which the function belongs')
  206. ), 'functions that are available')
  207. ),
  208. 'downloadfiles' => new external_value(PARAM_INT, '1 if users are allowed to download files, 0 if not',
  209. VALUE_OPTIONAL),
  210. 'uploadfiles' => new external_value(PARAM_INT, '1 if users are allowed to upload files, 0 if not',
  211. VALUE_OPTIONAL),
  212. 'release' => new external_value(PARAM_TEXT, 'Moodle release number', VALUE_OPTIONAL),
  213. 'version' => new external_value(PARAM_TEXT, 'Moodle version number', VALUE_OPTIONAL),
  214. 'mobilecssurl' => new external_value(PARAM_URL, 'Mobile custom CSS theme', VALUE_OPTIONAL),
  215. 'advancedfeatures' => new external_multiple_structure(
  216. new external_single_structure(
  217. array(
  218. 'name' => new external_value(PARAM_ALPHANUMEXT, 'feature name'),
  219. 'value' => new external_value(PARAM_INT, 'feature value. Usually 1 means enabled.')
  220. ),
  221. 'Advanced features availability'
  222. ),
  223. 'Advanced features availability',
  224. VALUE_OPTIONAL
  225. ),
  226. 'usercanmanageownfiles' => new external_value(PARAM_BOOL,
  227. 'true if the user can manage his own files', VALUE_OPTIONAL),
  228. 'userquota' => new external_value(PARAM_INT,
  229. 'user quota (bytes). 0 means user can ignore the quota', VALUE_OPTIONAL),
  230. 'usermaxuploadfilesize' => new external_value(PARAM_INT,
  231. 'user max upload file size (bytes). -1 means the user can ignore the upload file size',
  232. VALUE_OPTIONAL),
  233. 'userhomepage' => new external_value(PARAM_INT,
  234. 'the default home page for the user: 0 for the site home, 1 for dashboard',
  235. VALUE_OPTIONAL)
  236. )
  237. );
  238. }
  239. }
  240. /**
  241. * Deprecated web service related functions
  242. *
  243. * @package core_webservice
  244. * @category external
  245. * @copyright 2011 Jerome Mouneyrac <jerome@moodle.com>
  246. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  247. * @deprecated Moodle 2.2 MDL-29106 - please do not use this class any more.
  248. * @see core_webservice_external
  249. * @since Moodle 2.1
  250. */
  251. class moodle_webservice_external extends external_api {
  252. /**
  253. * Returns description of method parameters
  254. *
  255. * @return external_function_parameters
  256. * @deprecated Moodle 2.2 - please do not use this function any more.
  257. * @see core_webservice_external::get_site_info_parameters
  258. * @since Moodle 2.1
  259. */
  260. public static function get_siteinfo_parameters() {
  261. return core_webservice_external::get_site_info_parameters();
  262. }
  263. /**
  264. * Return user information including profile picture + basic site information
  265. * Note:
  266. * - no capability checking because we return just known information by logged user
  267. *
  268. * @param array $serviceshortnames of service shortnames - the functions of these services will be returned
  269. * @return array
  270. * @deprecated Moodle 2.2 - please do not use this function any more.
  271. * @see core_webservice_external::get_site_info
  272. * @since Moodle 2.1
  273. */
  274. public function get_siteinfo($serviceshortnames = array()) {
  275. return core_webservice_external::get_site_info($serviceshortnames);
  276. }
  277. /**
  278. * Returns description of method result value
  279. *
  280. * @return external_single_structure
  281. * @deprecated Moodle 2.2 - please do not use this function any more.
  282. * @see core_webservice_external::get_site_info_returns
  283. * @since Moodle 2.1
  284. */
  285. public static function get_siteinfo_returns() {
  286. return core_webservice_external::get_site_info_returns();
  287. }
  288. /**
  289. * Marking the method as deprecated.
  290. *
  291. * @return bool
  292. */
  293. public static function get_siteinfo_is_deprecated() {
  294. return true;
  295. }
  296. }