$filesize = filesize($thefile['path']); if (($maxbytes!=-1) && ($filesize>$maxbytes)) { print_error('maxbytes'); } if (!empty($thefile)) { $record = new stdClass(); $record->filepath = $savepath; $record->filename = $filename; $record->component = 'user'; $record->filearea = 'draft'; $record->itemid = $itemid; $record->license = ''; $record->author = ''; $record->source = $thefile['url']; try { $info = repository::move_to_filepool($thefile['path'], $record); redirect($home_url, get_string('downloadsucc', 'repository')); } catch (moodle_exception $e) { // inject target URL $e->link = $PAGE->url->out(); echo $OUTPUT->header(); // hack: we need the embedded header here, standard error printing would not use it throw $e; } } else { print_error('cannotdownload', 'repository'); } break; case 'confirm': echo $OUTPUT->header();
} else { // Download file to moodle. $downloadedfile = $repo->get_file($reference, $saveas_filename); if (empty($downloadedfile['path'])) { $err->error = get_string('cannotdownload', 'repository'); die(json_encode($err)); } // Check if exceed maxbytes. if ($maxbytes != -1 && filesize($downloadedfile['path']) > $maxbytes) { throw new file_exception('maxbytes'); } // Check if we exceed the max bytes of the area. if (file_is_draft_area_limit_reached($itemid, $areamaxbytes, filesize($downloadedfile['path']))) { throw new file_exception('maxareabytes'); } $info = repository::move_to_filepool($downloadedfile['path'], $record); if (empty($info)) { $info['e'] = get_string('error', 'moodle'); } } } echo json_encode($info); die; } break; case 'upload': $result = $repo->upload($saveas_filename, $maxbytes); echo json_encode($result); break; case 'overwrite': // existing file
/** * Process uploaded file. * * @return array Array of uploaded file information. */ public function upload($saveasfilename, $maxbytes) { global $CFG, $USER, $SESSION; $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, '/courses/') === 0) { $clienttype = 'sharepoint'; $filepath = substr($filepath, 8); } else { 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(); $result = $apiclient->create_file('', $filename, $content, 'application/octet-stream'); } 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 === 'sharepoint') { $pathtrimmed = trim($filepath, '/'); $pathparts = explode('/', $pathtrimmed); if (!is_numeric($pathparts[0])) { throw new \moodle_exception('errorbadpath', 'repository_office365'); } $courseid = (int) $pathparts[0]; unset($pathparts[0]); $relpath = !empty($pathparts) ? implode('/', $pathparts) : ''; $fullpath = !empty($relpath) ? '/' . $relpath : '/'; $courses = enrol_get_users_courses($USER->id); if (!isset($courses[$courseid])) { throw new \moodle_exception('erroraccessdenied', 'repository_office365'); } $curcourse = $courses[$courseid]; unset($courses); $sharepoint = $this->get_sharepoint_apiclient(); $parentsiteuri = $sharepoint->get_course_subsite_uri($curcourse->id); $sharepoint->set_site($parentsiteuri); $result = $sharepoint->create_file($fullpath, $filename, $content); $source = $this->pack_reference(['id' => $result['id'], 'source' => $clienttype, 'parentsiteuri' => $parentsiteuri]); } else { 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; }
// youtube plugin return a url instead a file path $url = $filepath; echo json_encode(array('type' => 'link', 'client_id' => $client_id, 'url' => $url, 'id' => $url, 'file' => $url)); } else { if (is_array($filepath)) { // file api don't have real file path, so we need more file api specific info for "local" plugin $fileinfo = $filepath; $info = array(); $info['client_id'] = $client_id; $info['file'] = $fileinfo['title']; $info['id'] = $itemid; $info['url'] = $CFG->httpswwwroot . '/draftfile.php/' . $fileinfo['contextid'] . '/user_draft/' . $itemid . '/' . $fileinfo['title']; echo json_encode($info); } else { // normal file path name $info = repository::move_to_filepool($filepath, $title, $itemid, $save_path); $info['client_id'] = $client_id; echo json_encode($info); } } } catch (repository_exception $e) { $err->e = $e->getMessage(); die(json_encode($err)); } catch (Exception $e) { $err->e = $e->getMessage(); die(json_encode($err)); } break; case 'upload': try { $upload = $repo->get_listing();
} break; case 'download': try { $path = $repo->get_file($file, $title); if ($path === false) { $err->e = get_string('cannotdownload', 'repository'); die(json_encode($err)); } if (empty($itemid)) { $itemid = (int) substr(hexdec(uniqid()), 0, 9) + rand(1, 100); } if (preg_match('#(https?://([-\\w\\.]+)+(:\\d+)?(/([\\w/_\\.]*(\\?\\S+)?)?)?)#', $path)) { echo json_encode(array('type' => 'link', 'client_id' => $client_id, 'url' => $path, 'id' => $path, 'file' => $path)); } else { $info = repository::move_to_filepool($path, $title, $itemid); $info['client_id'] = $client_id; if ($env == 'filepicker' || $env == 'filemanager') { echo json_encode($info); } else { if ($env == 'editor') { echo json_encode($info); } } } } catch (repository_exception $e) { $err->e = $e->getMessage(); die(json_encode($err)); } catch (Exception $e) { $err->e = $e->getMessage(); die(json_encode($err));