weekly release 3.10.1+
[moodle.git] / webservice / pluginfile.php
CommitLineData
ec0d6ea2 1<?php
ec0d6ea2
DC
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
a0a07014 17
ec0d6ea2
DC
18/**
19 * A script to serve files from web service client
20 *
a0a07014 21 * @package core_webservice
ec0d6ea2
DC
22 * @copyright 2011 Dongsheng Cai <dongsheng@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
a0a07014
JM
26/**
27 * AJAX_SCRIPT - exception will be converted into JSON
28 */
07cc3d11 29define('AJAX_SCRIPT', true);
a0a07014
JM
30
31/**
32 * NO_MOODLE_COOKIES - we don't want any cookie
33 */
ec0d6ea2 34define('NO_MOODLE_COOKIES', true);
a0a07014
JM
35
36
1fcf0ca8 37require_once(__DIR__ . '/../config.php');
ec0d6ea2 38require_once($CFG->libdir . '/filelib.php');
07cc3d11 39require_once($CFG->dirroot . '/webservice/lib.php');
ec0d6ea2 40
3665f803
FM
41// Allow CORS requests.
42header('Access-Control-Allow-Origin: *');
43
a4727b81 44// Authenticate the user.
ec0d6ea2 45$token = required_param('token', PARAM_ALPHANUM);
a4727b81
JL
46// Use preview in order to display the preview of the file (e.g. "thumb" for a thumbnail).
47$preview = optional_param('preview', null, PARAM_ALPHANUM);
48// Offline means download the file from the repository and serve it, even if it was an external link.
49// The repository may have to export the file to an offline format.
50$offline = optional_param('offline', 0, PARAM_BOOL);
51
07cc3d11
JM
52$webservicelib = new webservice();
53$authenticationinfo = $webservicelib->authenticate_user($token);
ec0d6ea2 54
a4727b81 55// Check the service allows file download.
07cc3d11 56$enabledfiledownload = (int) ($authenticationinfo['service']->downloadfiles);
ec0d6ea2 57if (empty($enabledfiledownload)) {
96d3b93b 58 throw new webservice_access_exception('Web service file downloading must be enabled in external service settings');
ec0d6ea2
DC
59}
60
a4727b81 61// Finally we can serve the file :).
07cc3d11 62$relativepath = get_file_argument();
a4727b81 63file_pluginfile($relativepath, 0, $preview, $offline);