Esempio n. 1
0
 /**
  * Get dropbox files
  *
  * @param string $path
  * @param int $page
  * @return array
  */
 public function get_listing($path = '', $page = '1')
 {
     global $OUTPUT;
     if (empty($path) || $path == '/') {
         $path = '/';
     } else {
         $path = file_correct_filepath($path);
     }
     $encoded_path = str_replace("%2F", "/", rawurlencode($path));
     $list = array();
     $list['list'] = array();
     $list['manage'] = 'https://www.dropbox.com/home';
     $list['dynload'] = true;
     $list['nosearch'] = true;
     $list['logouturl'] = 'https://www.dropbox.com/logout';
     $list['message'] = get_string('logoutdesc', 'repository_dropbox');
     // process breadcrumb trail
     $list['path'] = array(array('name' => get_string('dropbox', 'repository_dropbox'), 'path' => '/'));
     $result = $this->dropbox->get_listing($encoded_path, $this->access_key, $this->access_secret);
     if (!is_object($result) || empty($result)) {
         return $list;
     }
     if (empty($result->path)) {
         $current_path = '/';
     } else {
         $current_path = file_correct_filepath($result->path);
     }
     $trail = '';
     if (!empty($path)) {
         $parts = explode('/', $path);
         if (count($parts) > 1) {
             foreach ($parts as $part) {
                 if (!empty($part)) {
                     $trail .= '/' . $part;
                     $list['path'][] = array('name' => $part, 'path' => $trail);
                 }
             }
         } else {
             $list['path'][] = array('name' => $path, 'path' => $path);
         }
     }
     if (!empty($result->error)) {
         // reset access key
         set_user_preference($this->setting . '_access_key', '');
         set_user_preference($this->setting . '_access_secret', '');
         throw new repository_exception('repositoryerror', 'repository', '', $result->error);
     }
     if (empty($result->contents) or !is_array($result->contents)) {
         return $list;
     }
     $files = $result->contents;
     $dirslist = array();
     $fileslist = array();
     foreach ($files as $file) {
         if ($file->is_dir) {
             $dirslist[] = array('title' => substr($file->path, strpos($file->path, $current_path) + strlen($current_path)), 'path' => file_correct_filepath($file->path), 'date' => strtotime($file->modified), 'thumbnail' => $OUTPUT->pix_url(file_folder_icon(64))->out(false), 'thumbnail_height' => 64, 'thumbnail_width' => 64, 'children' => array());
         } else {
             $thumbnail = null;
             if ($file->thumb_exists) {
                 $thumburl = new moodle_url('/repository/dropbox/thumbnail.php', array('repo_id' => $this->id, 'ctx_id' => $this->context->id, 'source' => $file->path, 'rev' => $file->rev));
                 $thumbnail = $thumburl->out(false);
             }
             $fileslist[] = array('title' => substr($file->path, strpos($file->path, $current_path) + strlen($current_path)), 'source' => $file->path, 'size' => $file->bytes, 'date' => strtotime($file->modified), 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($file->path, 64))->out(false), 'realthumbnail' => $thumbnail, 'thumbnail_height' => 64, 'thumbnail_width' => 64);
         }
     }
     $fileslist = array_filter($fileslist, array($this, 'filter'));
     $list['list'] = array_merge($dirslist, array_values($fileslist));
     return $list;
 }
Esempio n. 2
0
 /**
  * Do the actual processing of the uploaded file
  * @param string $saveas_filename name to give to the file
  * @param int $maxbytes maximum file size
  * @param mixed $types optional array of file extensions that are allowed or '*' for all
  * @param string $savepath optional path to save the file to
  * @param int $itemid optional the ID for this item within the file area
  * @param string $license optional the license to use for this file
  * @param string $author optional the name of the author of this file
  * @param bool $overwriteexisting optional user has asked to overwrite the existing file
  * @param int $areamaxbytes maximum size of the file area.
  * @return object containing details of the file uploaded
  */
 public function process_upload($saveas_filename, $maxbytes, $types = '*', $savepath = '/', $itemid = 0, $license = null, $author = '', $overwriteexisting = false, $areamaxbytes = FILE_AREA_MAX_BYTES_UNLIMITED)
 {
     global $USER, $CFG;
     if (is_array($types) and in_array('*', $types) or $types == '*') {
         $this->mimetypes = '*';
     } else {
         foreach ($types as $type) {
             $this->mimetypes[] = mimeinfo('type', $type);
         }
     }
     if ($license == null) {
         $license = $CFG->sitedefaultlicense;
     }
     $record = new stdClass();
     $record->filearea = 'draft';
     $record->component = 'user';
     $record->filepath = $savepath;
     $record->itemid = $itemid;
     $record->license = $license;
     $record->author = $author;
     $context = context_user::instance($USER->id);
     $elname = 'repo_upload_file';
     $fs = get_file_storage();
     $sm = get_string_manager();
     if ($record->filepath !== '/') {
         $record->filepath = file_correct_filepath($record->filepath);
     }
     if (!isset($_FILES[$elname])) {
         throw new moodle_exception('nofile');
     }
     if (!empty($_FILES[$elname]['error'])) {
         switch ($_FILES[$elname]['error']) {
             case UPLOAD_ERR_INI_SIZE:
                 throw new moodle_exception('upload_error_ini_size', 'repository_upload');
                 break;
             case UPLOAD_ERR_FORM_SIZE:
                 throw new moodle_exception('upload_error_form_size', 'repository_upload');
                 break;
             case UPLOAD_ERR_PARTIAL:
                 throw new moodle_exception('upload_error_partial', 'repository_upload');
                 break;
             case UPLOAD_ERR_NO_FILE:
                 throw new moodle_exception('upload_error_no_file', 'repository_upload');
                 break;
             case UPLOAD_ERR_NO_TMP_DIR:
                 throw new moodle_exception('upload_error_no_tmp_dir', 'repository_upload');
                 break;
             case UPLOAD_ERR_CANT_WRITE:
                 throw new moodle_exception('upload_error_cant_write', 'repository_upload');
                 break;
             case UPLOAD_ERR_EXTENSION:
                 throw new moodle_exception('upload_error_extension', 'repository_upload');
                 break;
             default:
                 throw new moodle_exception('nofile');
         }
     }
     \core\antivirus\manager::scan_file($_FILES[$elname]['tmp_name'], $_FILES[$elname]['name'], true);
     // {@link repository::build_source_field()}
     $sourcefield = $this->get_file_source_info($_FILES[$elname]['name']);
     $record->source = self::build_source_field($sourcefield);
     if (empty($saveas_filename)) {
         $record->filename = clean_param($_FILES[$elname]['name'], PARAM_FILE);
     } else {
         $ext = '';
         $match = array();
         $filename = clean_param($_FILES[$elname]['name'], PARAM_FILE);
         if (strpos($filename, '.') === false) {
             // File has no extension at all - do not add a dot.
             $record->filename = $saveas_filename;
         } else {
             if (preg_match('/\\.([a-z0-9]+)$/i', $filename, $match)) {
                 if (isset($match[1])) {
                     $ext = $match[1];
                 }
             }
             $ext = !empty($ext) ? $ext : '';
             if (preg_match('#\\.(' . $ext . ')$#i', $saveas_filename)) {
                 // saveas filename contains file extension already
                 $record->filename = $saveas_filename;
             } else {
                 $record->filename = $saveas_filename . '.' . $ext;
             }
         }
     }
     // Check the file has some non-null contents - usually an indication that a user has
     // tried to upload a folder by mistake
     if (!$this->check_valid_contents($_FILES[$elname]['tmp_name'])) {
         throw new moodle_exception('upload_error_invalid_file', 'repository_upload', '', $record->filename);
     }
     if ($this->mimetypes != '*') {
         // check filetype
         $filemimetype = file_storage::mimetype($_FILES[$elname]['tmp_name'], $record->filename);
         if (!in_array($filemimetype, $this->mimetypes)) {
             throw new moodle_exception('invalidfiletype', 'repository', '', get_mimetype_description(array('filename' => $_FILES[$elname]['name'])));
         }
     }
     if (empty($record->itemid)) {
         $record->itemid = 0;
     }
     if ($maxbytes !== -1 && filesize($_FILES[$elname]['tmp_name']) > $maxbytes) {
         $maxbytesdisplay = display_size($maxbytes);
         throw new file_exception('maxbytesfile', (object) array('file' => $record->filename, 'size' => $maxbytesdisplay));
     }
     if (file_is_draft_area_limit_reached($record->itemid, $areamaxbytes, filesize($_FILES[$elname]['tmp_name']))) {
         throw new file_exception('maxareabytes');
     }
     $record->contextid = $context->id;
     $record->userid = $USER->id;
     if (repository::draftfile_exists($record->itemid, $record->filepath, $record->filename)) {
         $existingfilename = $record->filename;
         $unused_filename = repository::get_unused_filename($record->itemid, $record->filepath, $record->filename);
         $record->filename = $unused_filename;
         $stored_file = $fs->create_file_from_pathname($record, $_FILES[$elname]['tmp_name']);
         if ($overwriteexisting) {
             repository::overwrite_existing_draftfile($record->itemid, $record->filepath, $existingfilename, $record->filepath, $record->filename);
             $record->filename = $existingfilename;
         } else {
             $event = array();
             $event['event'] = 'fileexists';
             $event['newfile'] = new stdClass();
             $event['newfile']->filepath = $record->filepath;
             $event['newfile']->filename = $unused_filename;
             $event['newfile']->url = moodle_url::make_draftfile_url($record->itemid, $record->filepath, $unused_filename)->out(false);
             $event['existingfile'] = new stdClass();
             $event['existingfile']->filepath = $record->filepath;
             $event['existingfile']->filename = $existingfilename;
             $event['existingfile']->url = moodle_url::make_draftfile_url($record->itemid, $record->filepath, $existingfilename)->out(false);
             return $event;
         }
     } else {
         $stored_file = $fs->create_file_from_pathname($record, $_FILES[$elname]['tmp_name']);
     }
     return array('url' => moodle_url::make_draftfile_url($record->itemid, $record->filepath, $record->filename)->out(false), 'id' => $record->itemid, 'file' => $record->filename);
 }
Esempio n. 3
0
    /**
     * Process uploaded file
     * @return array|bool
     */
    public function upload($saveas_filename, $maxbytes) {
        global $USER, $CFG;

        $types = optional_param('accepted_types', '*', PARAM_RAW);
        if ((is_array($types) and in_array('*', $types)) or $types == '*') {
            $this->mimetypes = '*';
        } else {
            foreach ($types as $type) {
                $this->mimetypes[] = mimeinfo('type', $type);
            }
        }

        $record = new stdClass();
        $record->filearea = 'draft';
        $record->component = 'user';
        $record->filepath = optional_param('savepath', '/', PARAM_PATH);
        $record->itemid   = optional_param('itemid', 0, PARAM_INT);
        $record->license  = optional_param('license', $CFG->sitedefaultlicense, PARAM_TEXT);
        $record->author   = optional_param('author', '', PARAM_TEXT);

        $context = get_context_instance(CONTEXT_USER, $USER->id);
        $elname = 'repo_upload_file';

        $fs = get_file_storage();
        $sm = get_string_manager();

        if ($record->filepath !== '/') {
            $record->filepath = file_correct_filepath($record->filepath);
        }

        if (!isset($_FILES[$elname])) {
            throw new moodle_exception('nofile');
        }
        if (!empty($_FILES[$elname]['error'])) {
            switch ($_FILES[$elname]['error']) {
            case UPLOAD_ERR_INI_SIZE:
                throw new moodle_exception('upload_error_ini_size', 'repository_upload');
                break;
            case UPLOAD_ERR_FORM_SIZE:
                throw new moodle_exception('upload_error_form_size', 'repository_upload');
                break;
            case UPLOAD_ERR_PARTIAL:
                throw new moodle_exception('upload_error_partial', 'repository_upload');
                break;
            case UPLOAD_ERR_NO_FILE:
                throw new moodle_exception('upload_error_no_file', 'repository_upload');
                break;
            case UPLOAD_ERR_NO_TMP_DIR:
                throw new moodle_exception('upload_error_no_tmp_dir', 'repository_upload');
                break;
            case UPLOAD_ERR_CANT_WRITE:
                throw new moodle_exception('upload_error_cant_write', 'repository_upload');
                break;
            case UPLOAD_ERR_EXTENSION:
                throw new moodle_exception('upload_error_extension', 'repository_upload');
                break;
            default:
                throw new moodle_exception('nofile');
            }
        }

        if (empty($saveas_filename)) {
            $record->filename = clean_param($_FILES[$elname]['name'], PARAM_FILE);
        } else {
            $ext = '';
            $match = array();
            $filename = clean_param($_FILES[$elname]['name'], PARAM_FILE);
            if (preg_match('/\.([a-z0-9]+)$/i', $filename, $match)) {
                if (isset($match[1])) {
                    $ext = $match[1];
                }
            }
            $ext = !empty($ext) ? $ext : '';
            if (preg_match('#\.(' . $ext . ')$#i', $saveas_filename)) {
                // saveas filename contains file extension already
                $record->filename = $saveas_filename;
            } else {
                $record->filename = $saveas_filename . '.' . $ext;
            }
        }

        if ($this->mimetypes != '*') {
            // check filetype
            $filemimetype = mimeinfo('type', $_FILES[$elname]['name']);
            if (!in_array($filemimetype, $this->mimetypes)) {
                if ($sm->string_exists($filemimetype, 'mimetypes')) {
                    $filemimetype = get_string($filemimetype, 'mimetypes');
                }
                throw new moodle_exception('invalidfiletype', 'repository', '', $filemimetype);
            }
        }

        if (empty($record->itemid)) {
            $record->itemid = 0;
        }

        if (($maxbytes!==-1) && (filesize($_FILES[$elname]['tmp_name']) > $maxbytes)) {
            throw new file_exception('maxbytes');
        }

        if ($file = $fs->get_file($context->id, $record->component, $record->filearea, $record->itemid, $record->filepath, $record->filename)) {
            throw new moodle_exception('fileexists', 'repository');
        }

        $record->contextid = $context->id;
        $record->userid    = $USER->id;
        $record->source    = '';

        $stored_file = $fs->create_file_from_pathname($record, $_FILES[$elname]['tmp_name']);

        return array(
            'url'=>moodle_url::make_draftfile_url($record->itemid, $record->filepath, $record->filename)->out(),
            'id'=>$record->itemid,
            'file'=>$record->filename);
    }
Esempio n. 4
0
/**
 * Listing all files (including folders) in current path (draft area)
 * used by file manager
 * @param int $draftitemid
 * @param string $filepath
 * @return mixed
 */
function file_get_drafarea_files($draftitemid, $filepath = '/')
{
    global $USER, $OUTPUT, $CFG;
    $context = get_context_instance(CONTEXT_USER, $USER->id);
    $fs = get_file_storage();
    $data = new stdClass();
    $data->path = array();
    $data->path[] = array('name' => get_string('files'), 'path' => '/');
    // will be used to build breadcrumb
    $trail = '';
    if ($filepath !== '/') {
        $filepath = file_correct_filepath($filepath);
        $parts = explode('/', $filepath);
        foreach ($parts as $part) {
            if ($part != '' && $part != null) {
                $trail .= '/' . $part . '/';
                $data->path[] = array('name' => $part, 'path' => $trail);
            }
        }
    }
    $list = array();
    $maxlength = 12;
    if ($files = $fs->get_directory_files($context->id, 'user', 'draft', $draftitemid, $filepath, false)) {
        foreach ($files as $file) {
            $item = new stdClass();
            $item->filename = $file->get_filename();
            $item->filepath = $file->get_filepath();
            $item->fullname = trim($item->filename, '/');
            $filesize = $file->get_filesize();
            $item->filesize = $filesize ? display_size($filesize) : '';
            $icon = mimeinfo_from_type('icon', $file->get_mimetype());
            $item->icon = $OUTPUT->pix_url('f/' . $icon)->out();
            $item->sortorder = $file->get_sortorder();
            if ($icon == 'zip') {
                $item->type = 'zip';
            } else {
                $item->type = 'file';
            }
            if ($file->is_directory()) {
                $item->filesize = 0;
                $item->icon = $OUTPUT->pix_url('f/folder')->out();
                $item->type = 'folder';
                $foldername = explode('/', trim($item->filepath, '/'));
                $item->fullname = trim(array_pop($foldername), '/');
            } else {
                // do NOT use file browser here!
                $item->url = moodle_url::make_draftfile_url($draftitemid, $item->filepath, $item->filename)->out();
            }
            $list[] = $item;
        }
    }
    $data->itemid = $draftitemid;
    $data->list = $list;
    return $data;
}
Esempio n. 5
0
/**
 * Listing all files (including folders) in current path (draft area)
 * used by file manager
 * @param int $draftitemid
 * @param string $filepath
 * @return stdClass
 */
function file_get_drafarea_files($draftitemid, $filepath = '/')
{
    global $USER, $OUTPUT, $CFG;
    $context = context_user::instance($USER->id);
    $fs = get_file_storage();
    $data = new stdClass();
    $data->path = array();
    $data->path[] = array('name' => get_string('files'), 'path' => '/');
    // will be used to build breadcrumb
    $trail = '/';
    if ($filepath !== '/') {
        $filepath = file_correct_filepath($filepath);
        $parts = explode('/', $filepath);
        foreach ($parts as $part) {
            if ($part != '' && $part != null) {
                $trail .= $part . '/';
                $data->path[] = array('name' => $part, 'path' => $trail);
            }
        }
    }
    $list = array();
    $maxlength = 12;
    if ($files = $fs->get_directory_files($context->id, 'user', 'draft', $draftitemid, $filepath, false)) {
        foreach ($files as $file) {
            $item = new stdClass();
            $item->filename = $file->get_filename();
            $item->filepath = $file->get_filepath();
            $item->fullname = trim($item->filename, '/');
            $filesize = $file->get_filesize();
            $item->size = $filesize ? $filesize : null;
            $item->filesize = $filesize ? display_size($filesize) : '';
            $item->sortorder = $file->get_sortorder();
            $item->author = $file->get_author();
            $item->license = $file->get_license();
            $item->datemodified = $file->get_timemodified();
            $item->datecreated = $file->get_timecreated();
            $item->isref = $file->is_external_file();
            if ($item->isref && $file->get_status() == 666) {
                $item->originalmissing = true;
            }
            // find the file this draft file was created from and count all references in local
            // system pointing to that file
            $source = @unserialize($file->get_source());
            if (isset($source->original)) {
                $item->refcount = $fs->search_references_count($source->original);
            }
            if ($file->is_directory()) {
                $item->filesize = 0;
                $item->icon = $OUTPUT->pix_url(file_folder_icon(24))->out(false);
                $item->type = 'folder';
                $foldername = explode('/', trim($item->filepath, '/'));
                $item->fullname = trim(array_pop($foldername), '/');
                $item->thumbnail = $OUTPUT->pix_url(file_folder_icon(90))->out(false);
            } else {
                // do NOT use file browser here!
                $item->mimetype = get_mimetype_description($file);
                if (file_extension_in_typegroup($file->get_filename(), 'archive')) {
                    $item->type = 'zip';
                } else {
                    $item->type = 'file';
                }
                $itemurl = moodle_url::make_draftfile_url($draftitemid, $item->filepath, $item->filename);
                $item->url = $itemurl->out();
                $item->icon = $OUTPUT->pix_url(file_file_icon($file, 24))->out(false);
                $item->thumbnail = $OUTPUT->pix_url(file_file_icon($file, 90))->out(false);
                if ($imageinfo = $file->get_imageinfo()) {
                    $item->realthumbnail = $itemurl->out(false, array('preview' => 'thumb', 'oid' => $file->get_timemodified()));
                    $item->realicon = $itemurl->out(false, array('preview' => 'tinyicon', 'oid' => $file->get_timemodified()));
                    $item->image_width = $imageinfo['width'];
                    $item->image_height = $imageinfo['height'];
                }
            }
            $list[] = $item;
        }
    }
    $data->itemid = $draftitemid;
    $data->list = $list;
    return $data;
}
Esempio n. 6
0
 /**
  * Do the actual processing of the uploaded file
  * @param string $saveas_filename name to give to the file
  * @param int $maxbytes maximum file size
  * @param mixed $types optional array of file extensions that are allowed or '*' for all
  * @param string $savepath optional path to save the file to
  * @param int $itemid optional the ID for this item within the file area
  * @param string $license optional the license to use for this file
  * @param string $author optional the name of the author of this file
  * @return object containing details of the file uploaded
  */
 public function process_upload($saveas_filename, $maxbytes, $types = '*', $savepath = '/', $itemid = 0, $license = null, $author = '')
 {
     global $USER, $CFG;
     if (is_array($types) and in_array('*', $types) or $types == '*') {
         $this->mimetypes = '*';
     } else {
         foreach ($types as $type) {
             $this->mimetypes[] = mimeinfo('type', $type);
         }
     }
     if ($license == null) {
         $license = $CFG->sitedefaultlicense;
     }
     $record = new stdClass();
     $record->filearea = 'draft';
     $record->component = 'user';
     $record->filepath = $savepath;
     $record->itemid = $itemid;
     $record->license = $license;
     $record->author = $author;
     $context = get_context_instance(CONTEXT_USER, $USER->id);
     $elname = 'repo_upload_file';
     $fs = get_file_storage();
     $sm = get_string_manager();
     if ($record->filepath !== '/') {
         $record->filepath = file_correct_filepath($record->filepath);
     }
     if (!isset($_FILES[$elname])) {
         throw new moodle_exception('nofile');
     }
     if (!empty($_FILES[$elname]['error'])) {
         switch ($_FILES[$elname]['error']) {
             case UPLOAD_ERR_INI_SIZE:
                 throw new moodle_exception('upload_error_ini_size', 'repository_upload');
                 break;
             case UPLOAD_ERR_FORM_SIZE:
                 throw new moodle_exception('upload_error_form_size', 'repository_upload');
                 break;
             case UPLOAD_ERR_PARTIAL:
                 throw new moodle_exception('upload_error_partial', 'repository_upload');
                 break;
             case UPLOAD_ERR_NO_FILE:
                 throw new moodle_exception('upload_error_no_file', 'repository_upload');
                 break;
             case UPLOAD_ERR_NO_TMP_DIR:
                 throw new moodle_exception('upload_error_no_tmp_dir', 'repository_upload');
                 break;
             case UPLOAD_ERR_CANT_WRITE:
                 throw new moodle_exception('upload_error_cant_write', 'repository_upload');
                 break;
             case UPLOAD_ERR_EXTENSION:
                 throw new moodle_exception('upload_error_extension', 'repository_upload');
                 break;
             default:
                 throw new moodle_exception('nofile');
         }
     }
     // scan the files, throws exception and deletes if virus found
     // this is tricky because clamdscan daemon might not be able to access the files
     $permissions = fileperms($_FILES[$elname]['tmp_name']);
     @chmod($_FILES[$elname]['tmp_name'], $CFG->filepermissions);
     self::antivir_scan_file($_FILES[$elname]['tmp_name'], $_FILES[$elname]['name'], true);
     @chmod($_FILES[$elname]['tmp_name'], $permissions);
     if (empty($saveas_filename)) {
         $record->filename = clean_param($_FILES[$elname]['name'], PARAM_FILE);
     } else {
         $ext = '';
         $match = array();
         $filename = clean_param($_FILES[$elname]['name'], PARAM_FILE);
         if (preg_match('/\\.([a-z0-9]+)$/i', $filename, $match)) {
             if (isset($match[1])) {
                 $ext = $match[1];
             }
         }
         $ext = !empty($ext) ? $ext : '';
         if (preg_match('#\\.(' . $ext . ')$#i', $saveas_filename)) {
             // saveas filename contains file extension already
             $record->filename = $saveas_filename;
         } else {
             $record->filename = $saveas_filename . '.' . $ext;
         }
     }
     // Check the file has some non-null contents - usually an indication that a user has
     // tried to upload a folder by mistake
     if (!$this->check_valid_contents($_FILES[$elname]['tmp_name'])) {
         throw new moodle_exception('upload_error_invalid_file', 'repository_upload', '', $record->filename);
     }
     if ($this->mimetypes != '*') {
         // check filetype
         $filemimetype = mimeinfo('type', $_FILES[$elname]['name']);
         if (!in_array($filemimetype, $this->mimetypes)) {
             throw new moodle_exception('invalidfiletype', 'repository', '', get_mimetype_description(array('filename' => $_FILES[$elname]['name'])));
         }
     }
     if (empty($record->itemid)) {
         $record->itemid = 0;
     }
     if ($maxbytes !== -1 && filesize($_FILES[$elname]['tmp_name']) > $maxbytes) {
         throw new file_exception('maxbytes');
     }
     $record->contextid = $context->id;
     $record->userid = $USER->id;
     $record->source = '';
     if (repository::draftfile_exists($record->itemid, $record->filepath, $record->filename)) {
         $existingfilename = $record->filename;
         $unused_filename = repository::get_unused_filename($record->itemid, $record->filepath, $record->filename);
         $record->filename = $unused_filename;
         $stored_file = $fs->create_file_from_pathname($record, $_FILES[$elname]['tmp_name']);
         $event = array();
         $event['event'] = 'fileexists';
         $event['newfile'] = new stdClass();
         $event['newfile']->filepath = $record->filepath;
         $event['newfile']->filename = $unused_filename;
         $event['newfile']->url = moodle_url::make_draftfile_url($record->itemid, $record->filepath, $unused_filename)->out(false);
         $event['existingfile'] = new stdClass();
         $event['existingfile']->filepath = $record->filepath;
         $event['existingfile']->filename = $existingfilename;
         $event['existingfile']->url = moodle_url::make_draftfile_url($record->itemid, $record->filepath, $existingfilename)->out(false);
         return $event;
     } else {
         $stored_file = $fs->create_file_from_pathname($record, $_FILES[$elname]['tmp_name']);
         return array('url' => moodle_url::make_draftfile_url($record->itemid, $record->filepath, $record->filename)->out(false), 'id' => $record->itemid, 'file' => $record->filename);
     }
 }
Esempio n. 7
0
         } else {
             if ($result = $stored_file->delete()) {
                 $return->filepath = $parent_path;
                 echo json_encode($return);
             } else {
                 echo json_encode(false);
             }
         }
     } else {
         echo json_encode(false);
     }
     die;
 case 'setmainfile':
     $filename = required_param('filename', PARAM_FILE);
     $filepath = required_param('filepath', PARAM_PATH);
     $filepath = file_correct_filepath($filepath);
     // reset sort order
     file_reset_sortorder($user_context->id, 'user', 'draft', $draftid);
     // set main file
     $return = file_set_sortorder($user_context->id, 'user', 'draft', $draftid, $filepath, $filename, 1);
     echo json_encode($return);
     die;
 case 'updatefile':
     // Allows to Rename file, move it to another directory, change it's license and author information in one request
     $filename = required_param('filename', PARAM_FILE);
     $filepath = required_param('filepath', PARAM_PATH);
     $fs = get_file_storage();
     if (!($file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, $filename))) {
         die(json_encode((object) array('error' => get_string('filenotfound', 'error'))));
     }
     $updatedata = array();
Esempio n. 8
0
 /**
  * Get dropbox files
  * @param string $path
  * @param int $page
  * @return array
  */
 public function get_listing($path = '', $page = '1')
 {
     global $OUTPUT;
     if (empty($path) || $path == '/') {
         $path = '/';
     } else {
         $path = file_correct_filepath($path);
     }
     $encoded_path = str_replace("%2F", "/", rawurlencode($path));
     $list = array();
     $list['list'] = array();
     $list['manage'] = false;
     $list['dynload'] = true;
     $list['nosearch'] = true;
     // process breadcrumb trail
     $list['path'] = array(array('name' => get_string('dropbox', 'repository_dropbox'), 'path' => '/'));
     $result = $this->dropbox->get_listing($encoded_path, $this->access_key, $this->access_secret);
     if (!is_object($result) || empty($result)) {
         return $list;
     }
     if (empty($result->path)) {
         $current_path = '/';
     } else {
         $current_path = file_correct_filepath($result->path);
     }
     $trail = '';
     if (!empty($path)) {
         $parts = explode('/', $path);
         if (count($parts) > 1) {
             foreach ($parts as $part) {
                 if (!empty($part)) {
                     $trail .= '/' . $part;
                     $list['path'][] = array('name' => $part, 'path' => $trail);
                 }
             }
         } else {
             $list['path'][] = array('name' => $path, 'path' => $path);
         }
     }
     if (!empty($result->error)) {
         // reset access key
         set_user_preference($this->setting . '_access_key', '');
         set_user_preference($this->setting . '_access_secret', '');
         throw new repository_exception('repositoryerror', 'repository', '', $result->error);
     }
     if (empty($result->contents) or !is_array($result->contents)) {
         return $list;
     }
     $files = $result->contents;
     foreach ($files as $file) {
         if ($file->is_dir) {
             $list['list'][] = array('title' => substr($file->path, strpos($file->path, $current_path) + strlen($current_path)), 'path' => file_correct_filepath($file->path), 'size' => $file->size, 'date' => $file->modified, 'thumbnail' => $OUTPUT->pix_url('f/folder-32')->out(false), 'children' => array());
         } else {
             $list['list'][] = array('title' => substr($file->path, strpos($file->path, $current_path) + strlen($current_path)), 'source' => $file->path, 'size' => $file->size, 'date' => $file->modified, 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($file->path, 32))->out(false));
         }
     }
     return $list;
 }
Esempio n. 9
0
function xmldb_resource_upgrade($oldversion)
{
    global $CFG, $DB;
    require_once "{$CFG->dirroot}/mod/resource/db/upgradelib.php";
    $dbman = $DB->get_manager();
    //===== 1.9.0 upgrade line ======//
    if ($oldversion < 2009041900) {
        resource_20_prepare_migration();
        // resource savepoint reached
        upgrade_mod_savepoint(true, 2009041900, 'resource');
    }
    if ($oldversion < 2009042000) {
        // Rename field summary on table resource to intro
        $table = new xmldb_table('resource');
        $field = new xmldb_field('summary', XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'reference');
        // Launch rename field summary
        $dbman->rename_field($table, $field, 'intro');
        // resource savepoint reached
        upgrade_mod_savepoint(true, 2009042000, 'resource');
    }
    if ($oldversion < 2009042001) {
        // Define field introformat to be added to resource
        $table = new xmldb_table('resource');
        $field = new xmldb_field('introformat', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'intro');
        // Launch add field introformat
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // conditionally migrate to html format in intro
        if ($CFG->texteditors !== 'textarea') {
            $rs = $DB->get_recordset('resource', array('introformat' => FORMAT_MOODLE), '', 'id,intro,introformat');
            foreach ($rs as $r) {
                $r->intro = text_to_html($r->intro, false, false, true);
                $r->introformat = FORMAT_HTML;
                $DB->update_record('resource', $r);
                upgrade_set_timeout();
            }
            $rs->close();
        }
        // resource savepoint reached
        upgrade_mod_savepoint(true, 2009042001, 'resource');
    }
    if ($oldversion < 2009062600) {
        $res_count = $DB->count_records('resource');
        $old_count = $DB->count_records('resource_old', array('migrated' => 0));
        if ($res_count != $old_count) {
            //we can not continue, something is very wrong!!
            upgrade_log(UPGRADE_LOG_ERROR, null, 'Resource migration failed.');
            upgrade_mod_savepoint(false, 2009062600, 'resource');
        }
        // Drop obsoleted fields from resource table
        $table = new xmldb_table('resource');
        $fields = array('type', 'reference', 'alltext', 'popup', 'options');
        foreach ($fields as $fname) {
            $field = new xmldb_field($fname);
            $dbman->drop_field($table, $field);
        }
        // resource savepoint reached
        upgrade_mod_savepoint(true, 2009062600, 'resource');
    }
    if ($oldversion < 2009062601) {
        $table = new xmldb_table('resource');
        // Define field tobemigrated to be added to resource
        $field = new xmldb_field('tobemigrated', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'introformat');
        // Conditionally launch add field tobemigrated
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Define field mainfile to be added to resource
        $field = new xmldb_field('mainfile', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'tobemigrated');
        // Conditionally launch add field mainfile
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Define field legacyfiles to be added to resource
        $field = new xmldb_field('legacyfiles', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'mainfile');
        // Conditionally launch add field legacyfiles
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Define field legacyfileslast to be added to resource
        $field = new xmldb_field('legacyfileslast', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'legacyfiles');
        // Conditionally launch add field legacyfileslast
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Define field display to be added to resource
        $field = new xmldb_field('display', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'legacyfileslast');
        // Conditionally launch add field display
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Define field displayoptions to be added to resource
        $field = new xmldb_field('displayoptions', XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'display');
        // Conditionally launch add field displayoptions
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Define field filterfiles to be added to resource
        $field = new xmldb_field('filterfiles', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'displayoptions');
        // Conditionally launch add field filterfiles
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Define field revision to be added to resource
        $field = new xmldb_field('revision', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'filterfiles');
        // Conditionally launch add field revision
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        //mark all records as awaiting migration
        $DB->set_field('resource', 'tobemigrated', 1, array());
        // resource savepoint reached
        upgrade_mod_savepoint(true, 2009062601, 'resource');
    }
    if ($oldversion < 2009062603) {
        resource_20_migrate();
        upgrade_mod_savepoint(true, 2009062603, 'resource');
    }
    if ($oldversion < 2009063000) {
        //migrate and prune old settings - admins need to review and set up all module settings anyway
        if (!empty($CFG->resource_framesize)) {
            set_config('framesize', $CFG->resource_framesize, 'resource');
        }
        if (!empty($CFG->resource_popupheight)) {
            set_config('popupheight', $CFG->resource_popupheight, 'resource');
        }
        if (!empty($CFG->resource_popupwidth)) {
            set_config('popupwidth', $CFG->resource_popupwidth, 'resource');
        }
        $cleanupsettings = array('resource_framesize', 'resource_popupheight', 'resource_popupwidth', 'resource_popupmenubar', 'resource_websearch', 'resource_defaulturl', 'resource_allowlocalfiles', 'resource_popup', 'resource_popupresizable', 'resource_popupscrollbars', 'resource_popupdirectories', 'resource_popuplocation', 'resource_popuptoolbar', 'resource_popupstatus');
        foreach ($cleanupsettings as $setting) {
            unset_config($setting);
        }
        upgrade_mod_savepoint(true, 2009063000, 'resource');
    }
    if ($oldversion < 2009080501) {
        require_once "{$CFG->libdir}/filelib.php";
        $sql = "SELECT r.id,\n                       r.mainfile,\n                       cm.id AS cmid\n                  FROM {resource} r\n                  JOIN {modules} m ON m.name='resource'\n                  JOIN {course_modules} cm ON (cm.module = m.id AND cm.instance = r.id)";
        if ($instances = $DB->get_recordset_sql($sql)) {
            foreach ($instances as $instance) {
                $context = get_context_instance(CONTEXT_MODULE, $instance->cmid);
                $component = 'mod_resource';
                $filearea = 'content';
                $itemid = 0;
                $filepath = file_correct_filepath(dirname($instance->mainfile));
                $filename = basename($instance->mainfile);
                file_set_sortorder($context->id, $component, $filearea, $itemid, $filepath, $filename, 1);
            }
        }
        /// Define field mainfile to be dropped from resource
        $table = new xmldb_table('resource');
        $field = new xmldb_field('mainfile');
        /// Conditionally launch drop field mainfile
        if ($dbman->field_exists($table, $field)) {
            $dbman->drop_field($table, $field);
        }
        /// resource savepoint reached
        upgrade_mod_savepoint(true, 2009080501, 'resource');
    }
    // MDL-10906. Removing resource_allowlocalfiles setting.
    if ($oldversion < 2010083000) {
        unset_config('resource_allowlocalfiles');
        upgrade_mod_savepoint(true, 2010083000, 'resource');
    }
    return true;
}
Esempio n. 10
0
 /**
  * Process uploaded file
  * @return array|bool
  */
 public function upload($search_text)
 {
     global $USER, $CFG;
     $record = new stdClass();
     $record->filearea = 'draft';
     $record->component = 'user';
     $record->filepath = optional_param('savepath', '/', PARAM_PATH);
     $record->itemid = optional_param('itemid', 0, PARAM_INT);
     $record->license = optional_param('license', $CFG->sitedefaultlicense, PARAM_TEXT);
     $record->author = optional_param('author', '', PARAM_TEXT);
     $context = get_context_instance(CONTEXT_USER, $USER->id);
     $filename = required_param('recordaudio_filename', PARAM_FILE);
     $filedata = required_param('recordaudio_filedata', PARAM_RAW);
     $filedata = base64_decode($filedata);
     $fs = get_file_storage();
     $sm = get_string_manager();
     if ($record->filepath !== '/') {
         $record->filepath = file_correct_filepath($record->filepath);
     }
     $record->filename = $filename;
     if (empty($record->itemid)) {
         $record->itemid = 0;
     }
     $record->contextid = $context->id;
     $record->userid = $USER->id;
     $record->source = '';
     if (repository::draftfile_exists($record->itemid, $record->filepath, $record->filename)) {
         $existingfilename = $record->filename;
         $unused_filename = repository::get_unused_filename($record->itemid, $record->filepath, $record->filename);
         $record->filename = $unused_filename;
         $stored_file = $fs->create_file_from_string($record, $filedata);
         $event = array();
         $event['event'] = 'fileexists';
         $event['newfile'] = new stdClass();
         $event['newfile']->filepath = $record->filepath;
         $event['newfile']->filename = $unused_filename;
         $event['newfile']->url = moodle_url::make_draftfile_url($record->itemid, $record->filepath, $unused_filename)->out();
         $event['existingfile'] = new stdClass();
         $event['existingfile']->filepath = $record->filepath;
         $event['existingfile']->filename = $existingfilename;
         $event['existingfile']->url = moodle_url::make_draftfile_url($record->itemid, $record->filepath, $existingfilename)->out();
         return $event;
     } else {
         $stored_file = $fs->create_file_from_string($record, $filedata);
         return array('url' => moodle_url::make_draftfile_url($record->itemid, $record->filepath, $record->filename)->out(), 'id' => $record->itemid, 'file' => $record->filename);
     }
 }
Esempio n. 11
0
 /**
  * Process uploaded file
  * @return array|bool
  */
 public function upload($saveas_filename, $maxbytes)
 {
     global $USER, $CFG;
     $types = optional_param('accepted_types', '*', PARAM_RAW);
     if (is_array($types) and in_array('*', $types) or $types == '*') {
         $this->mimetypes = '*';
     } else {
         foreach ($types as $type) {
             $this->mimetypes[] = mimeinfo('type', $type);
         }
     }
     $record = new stdClass();
     $record->filearea = 'draft';
     $record->component = 'user';
     $record->filepath = optional_param('savepath', '/', PARAM_PATH);
     $record->itemid = optional_param('itemid', 0, PARAM_INT);
     $record->license = optional_param('license', $CFG->sitedefaultlicense, PARAM_TEXT);
     $record->author = optional_param('author', '', PARAM_TEXT);
     $context = get_context_instance(CONTEXT_USER, $USER->id);
     $elname = 'repo_upload_file';
     $fs = get_file_storage();
     $sm = get_string_manager();
     if ($record->filepath !== '/') {
         $record->filepath = file_correct_filepath($record->filepath);
     }
     if (!isset($_FILES[$elname])) {
         throw new moodle_exception('nofile');
     }
     if (!empty($_FILES[$elname]['error'])) {
         throw new moodle_exception('maxbytes');
     }
     if (empty($saveas_filename)) {
         $record->filename = clean_param($_FILES[$elname]['name'], PARAM_FILE);
     } else {
         $record->filename = $saveas_filename;
     }
     if ($this->mimetypes != '*') {
         // check filetype
         $filemimetype = mimeinfo('type', $_FILES[$elname]['name']);
         if (!in_array($filemimetype, $this->mimetypes)) {
             if ($sm->string_exists($filemimetype, 'mimetypes')) {
                 $filemimetype = get_string($filemimetype, 'mimetypes');
             }
             throw new moodle_exception('invalidfiletype', 'repository', '', $filemimetype);
         }
     }
     if (empty($record->itemid)) {
         $record->itemid = 0;
     }
     if ($maxbytes !== -1 && filesize($_FILES[$elname]['tmp_name']) > $maxbytes) {
         throw new file_exception('maxbytes');
     }
     if ($file = $fs->get_file($context->id, $record->component, $record->filearea, $record->itemid, $record->filepath, $record->filename)) {
         throw new moodle_exception('fileexists', 'repository');
     }
     $record->contextid = $context->id;
     $record->userid = $USER->id;
     $record->source = '';
     $stored_file = $fs->create_file_from_pathname($record, $_FILES[$elname]['tmp_name']);
     return array('url' => moodle_url::make_draftfile_url($record->itemid, $record->filepath, $record->filename)->out(), 'id' => $record->itemid, 'file' => $record->filename);
 }
Esempio n. 12
0
File: lib.php Progetto: dg711/moodle
 /**
  * Process a standard entries list.
  *
  * @param   array       $entries    The list of entries returned from the API
  * @return  array                   The manipulated entries for display in the file picker
  */
 protected function process_entries(array $entries)
 {
     global $OUTPUT;
     $dirslist = [];
     $fileslist = [];
     foreach ($entries as $entry) {
         $entrydata = $entry;
         if (isset($entrydata->metadata)) {
             // If this is metadata, fetch the metadata content.
             // We only use the consistent parts of the file, folder, and metadata.
             $entrydata = $entrydata->metadata;
         }
         if ($entrydata->{".tag"} === "folder") {
             $dirslist[] = ['title' => $entrydata->name, 'path' => file_correct_filepath($entrydata->path_display), 'thumbnail' => $OUTPUT->pix_url(file_folder_icon(64))->out(false), 'thumbnail_height' => 64, 'thumbnail_width' => 64, 'children' => array()];
         } else {
             if ($entrydata->{".tag"} === "file") {
                 $fileslist[] = ['title' => $entrydata->name, 'source' => $entrydata->path_lower, 'size' => $entrydata->size, 'date' => strtotime($entrydata->client_modified), 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($entrydata->path_lower, 64))->out(false), 'realthumbnail' => $this->get_thumbnail_url($entrydata), 'thumbnail_height' => 64, 'thumbnail_width' => 64];
             }
         }
     }
     $fileslist = array_filter($fileslist, array($this, 'filter'));
     return array_merge($dirslist, array_values($fileslist));
 }