Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 * Accept uploading files by web service token to the user draft file area.
20
 *
21
 * POST params:
22
 *  token => the web service user token (needed for authentication)
23
 *  filepath => file path (where files will be stored)
24
 *  [_FILES] => for example you can send the files with <input type=file>,
25
 *              or with curl magic: 'file_1' => '@/path/to/file', or ...
26
 *  itemid   => The draftid - this can be used to add a list of files
27
 *              to a draft area in separate requests. If it is 0, a new draftid will be generated.
28
 *
29
 * @package    core_webservice
30
 * @copyright  2011 Dongsheng Cai <dongsheng@moodle.com>
31
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 */
33
 
34
/**
35
 * AJAX_SCRIPT - exception will be converted into JSON
36
 */
37
define('AJAX_SCRIPT', true);
38
 
39
/**
40
 * NO_MOODLE_COOKIES - we don't want any cookie
41
 */
42
define('NO_MOODLE_COOKIES', true);
43
 
44
require_once(__DIR__ . '/../config.php');
45
require_once($CFG->dirroot . '/webservice/lib.php');
46
 
47
// Allow CORS requests.
48
header('Access-Control-Allow-Origin: *');
49
 
50
$filepath = optional_param('filepath', '/', PARAM_PATH);
51
$itemid = optional_param('itemid', 0, PARAM_INT);
52
 
53
echo $OUTPUT->header();
54
 
55
// Authenticate the user.
56
$token = required_param('token', PARAM_ALPHANUM);
57
$webservicelib = new webservice();
58
$authenticationinfo = $webservicelib->authenticate_user($token);
59
$fileuploaddisabled = empty($authenticationinfo['service']->uploadfiles);
60
if ($fileuploaddisabled) {
61
    throw new webservice_access_exception('Web service file upload must be enabled in external service settings');
62
}
63
 
64
$context = context_user::instance($USER->id);
65
 
66
$fs = get_file_storage();
67
 
68
$totalsize = 0;
69
$files = array();
70
foreach ($_FILES as $fieldname => $uploadedfile) {
71
    // Check upload errors.
72
    if (!empty($_FILES[$fieldname]['error'])) {
73
        switch ($_FILES[$fieldname]['error']) {
74
            case UPLOAD_ERR_INI_SIZE:
75
                throw new moodle_exception('upload_error_ini_size', 'repository_upload');
76
                break;
77
            case UPLOAD_ERR_FORM_SIZE:
78
                throw new moodle_exception('upload_error_form_size', 'repository_upload');
79
                break;
80
            case UPLOAD_ERR_PARTIAL:
81
                throw new moodle_exception('upload_error_partial', 'repository_upload');
82
                break;
83
            case UPLOAD_ERR_NO_FILE:
84
                throw new moodle_exception('upload_error_no_file', 'repository_upload');
85
                break;
86
            case UPLOAD_ERR_NO_TMP_DIR:
87
                throw new moodle_exception('upload_error_no_tmp_dir', 'repository_upload');
88
                break;
89
            case UPLOAD_ERR_CANT_WRITE:
90
                throw new moodle_exception('upload_error_cant_write', 'repository_upload');
91
                break;
92
            case UPLOAD_ERR_EXTENSION:
93
                throw new moodle_exception('upload_error_extension', 'repository_upload');
94
                break;
95
            default:
96
                throw new moodle_exception('nofile');
97
        }
98
    }
99
 
100
    // Scan for viruses.
1441 ariadna 101
    $avscanstarttime = microtime(true);
1 efrain 102
    \core\antivirus\manager::scan_file($_FILES[$fieldname]['tmp_name'], $_FILES[$fieldname]['name'], true);
103
 
104
    $file = new stdClass();
1441 ariadna 105
    $file->avscantime = microtime(true) - $avscanstarttime;
1 efrain 106
    $file->filename = clean_param($_FILES[$fieldname]['name'], PARAM_FILE);
107
    // Check system maxbytes setting.
108
    if (($_FILES[$fieldname]['size'] > get_max_upload_file_size($CFG->maxbytes))) {
109
        // Oversize file will be ignored, error added to array to notify
110
        // web service client.
111
        $file->errortype = 'fileoversized';
112
        $file->error = get_string('maxbytes', 'error');
113
    } else {
114
        $file->filepath = $_FILES[$fieldname]['tmp_name'];
115
        // Calculate total size of upload.
116
        $totalsize += $_FILES[$fieldname]['size'];
117
        // Size of individual file.
118
        $file->size = $_FILES[$fieldname]['size'];
119
    }
120
    $files[] = $file;
121
}
122
 
123
$fs = get_file_storage();
124
 
125
if ($itemid <= 0) {
126
    $itemid = file_get_unused_draft_itemid();
127
}
128
 
129
// Get any existing file size limits.
130
$maxupload = get_user_max_upload_file_size($context, $CFG->maxbytes);
131
 
132
// Check the size of this upload.
133
if ($maxupload !== USER_CAN_IGNORE_FILE_SIZE_LIMITS && $totalsize > $maxupload) {
134
    throw new file_exception('userquotalimit');
135
}
136
 
137
$results = array();
138
foreach ($files as $file) {
139
    if (!empty($file->error)) {
140
        // Including error and filename.
141
        $results[] = $file;
142
        continue;
143
    }
144
    $filerecord = new stdClass;
145
    $filerecord->component = 'user';
146
    $filerecord->contextid = $context->id;
147
    $filerecord->userid = $USER->id;
148
    $filerecord->filearea = 'draft';
149
    $filerecord->filename = $file->filename;
150
    $filerecord->filepath = $filepath;
151
    $filerecord->itemid = $itemid;
152
    $filerecord->license = $CFG->sitedefaultlicense;
153
    $filerecord->author = fullname($authenticationinfo['user']);
154
    $filerecord->source = serialize((object)array('source' => $file->filename));
155
    $filerecord->filesize = $file->size;
156
 
157
    // Check if the file already exist.
158
    $existingfile = $fs->file_exists($filerecord->contextid, $filerecord->component, $filerecord->filearea,
159
                $filerecord->itemid, $filerecord->filepath, $filerecord->filename);
160
    if ($existingfile) {
161
        $file->errortype = 'filenameexist';
162
        $file->error = get_string('filenameexist', 'webservice', $file->filename);
163
        $results[] = $file;
164
    } else {
165
        $storedfile = $fs->create_file_from_pathname($filerecord, $file->filepath);
166
        $results[] = $filerecord;
167
 
168
        // Log the event when a file is uploaded to the draft area.
169
        $logevent = \core\event\draft_file_added::create([
170
                'objectid' => $storedfile->get_id(),
171
                'context' => $context,
172
                'other' => [
173
                        'itemid' => $filerecord->itemid,
174
                        'filename' => $filerecord->filename,
175
                        'filesize' => $filerecord->filesize,
176
                        'filepath' => $filerecord->filepath,
177
                        'contenthash' => $storedfile->get_contenthash(),
1441 ariadna 178
                        'avscantime' => $file->avscantime,
1 efrain 179
                ],
180
        ]);
181
        $logevent->trigger();
182
    }
183
}
184
echo json_encode($results);