in lib.php [290:414]
public function upload($saveasfilename, $maxbytes) {
global $CFG, $USER, $SESSION, $DB;
$caller = '\repository_office365::upload';
$types = optional_param_array('accepted_types', '*', PARAM_RAW);
$savepath = optional_param('savepath', '/', PARAM_PATH);
$itemid = optional_param('itemid', 0, PARAM_INT);
$license = optional_param('license', $CFG->sitedefaultlicense, PARAM_TEXT);
$author = optional_param('author', '', PARAM_TEXT);
$areamaxbytes = optional_param('areamaxbytes', FILE_AREA_MAX_BYTES_UNLIMITED, PARAM_INT);
$overwriteexisting = optional_param('overwrite', false, PARAM_BOOL);
$clientid = optional_param('client_id', '', PARAM_TEXT);
$filepath = '/';
if (!empty($SESSION->repository_office365)) {
if (isset($SESSION->repository_office365['curpath']) && isset($SESSION->repository_office365['curpath'][$clientid])) {
$filepath = $SESSION->repository_office365['curpath'][$clientid];
if (strpos($filepath, '/my/') === 0) {
$clienttype = 'onedrive';
$filepath = substr($filepath, 3);
} else if (strpos($filepath, '/groups/') === 0) {
$clienttype = 'onedrivegroup';
$filepath = substr($filepath, 7);
} else {
$errmsg = get_string('errorbadclienttype', 'repository_office365');
$debugdata = [
'filepath' => $filepath,
];
utils::debug($errmsg, $caller, $debugdata);
throw new \moodle_exception('errorbadclienttype', 'repository_office365');
}
}
}
if ($this->path_is_upload($filepath) === true) {
$filepath = substr($filepath, 0, -strlen('/upload/'));
}
$filename = (!empty($saveasfilename)) ? $saveasfilename : $_FILES['repo_upload_file']['name'];
$filename = clean_param($filename, PARAM_FILE);
$content = file_get_contents($_FILES['repo_upload_file']['tmp_name']);
if ($clienttype === 'onedrive') {
if ($this->unifiedconfigured === true) {
$apiclient = $this->get_unified_apiclient();
$parentid = (!empty($filepath)) ? substr($filepath, 1) : '';
$o365userid = utils::get_o365_userid($USER->id);
$result = $apiclient->create_file($parentid, $filename, $content, 'application/octet-stream', $o365userid);
} else {
$apiclient = $this->get_onedrive_apiclient();
$result = $apiclient->create_file($filepath, $filename, $content);
}
$source = $this->pack_reference(['id' => $result['id'], 'source' => 'onedrive']);
} else if ($clienttype === 'onedrivegroup') {
if ($this->unifiedconfigured === true) {
$apiclient = $this->get_unified_apiclient();
$parentid = (!empty($filepath)) ? substr($filepath, 1) : '';
$pathtrimmed = trim($parentid, '/');
$pathparts = explode('/', $pathtrimmed);
$coursesbyid = enrol_get_users_courses($USER->id, true);
if (!is_numeric($pathparts[0]) || !isset($coursesbyid[$pathparts[0]])
|| \local_o365\feature\usergroups\utils::course_is_group_enabled($pathparts[0]) !== true) {
utils::debug(get_string('errorbadpath', 'repository_office365'), $caller, ['path' => $filepath]);
throw new \moodle_exception('errorbadpath', 'repository_office365');
}
$courseid = (int)$pathparts[0];
if (!is_numeric($pathparts[1]) && $pathparts[1] !== 'coursegroup') {
utils::debug(get_string('errorbadpath', 'repository_office365'), $caller, ['path' => $filepath]);
throw new \moodle_exception('errorbadpath', 'repository_office365');
}
if ($pathparts[1] === 'coursegroup') {
$filters = ['type' => 'group', 'subtype' => 'course', 'moodleid' => $courseid];
$group = $DB->get_record('local_o365_objects', $filters);
} else {
$groupid = (int)$pathparts[1];
$group = $DB->get_record('groups', ['id' => $groupid]);
$filters = ['type' => 'group', 'subtype' => 'usergroup', 'moodleid' => $groupid];
$group = $DB->get_record('local_o365_objects', $filters);
}
try {
$result = $apiclient->create_group_file($group->objectid, '', $filename, $content);
$source = $this->pack_reference(['id' => $result['id'], 'source' => $clienttype
, 'groupid' => $group->objectid]);
} catch (\Exception $e) {
$errmsg = 'Exception when uploading share point files for group';
$debugdata = [
'fullpath' => $filepath,
'message' => $e->getMessage(),
'groupid' => $group->objectid,
];
utils::debug($errmsg, $caller, $debugdata);
$source = $this->pack_reference([]);
}
} else {
utils::debug('Tried to Upload a onedrive group file while the graph api is disabled.', $caller);
throw new \moodle_exception('errorwhileupload', 'repository_office365');
}
} else {
$errmsg = get_string('errorbadclienttype', 'repository_office365');
$debugdata = [
'clienttype' => $clienttype,
];
utils::debug($errmsg, $caller, $debugdata);
throw new \moodle_exception('errorbadclienttype', 'repository_office365');
}
$downloadedfile = $this->get_file($source, $filename);
$record = new \stdClass;
$record->filename = $filename;
$record->filepath = $savepath;
$record->component = 'user';
$record->filearea = 'draft';
$record->itemid = $itemid;
$record->license = $license;
$record->author = $author;
$usercontext = \context_user::instance($USER->id);
$now = time();
$record->contextid = $usercontext->id;
$record->timecreated = $now;
$record->timemodified = $now;
$record->userid = $USER->id;
$record->sortorder = 0;
$record->source = $this->build_source_field($source);
$info = \repository::move_to_filepool($downloadedfile['path'], $record);
return $info;
}