Exemplo n.º 1
0
    /**
     * Internal function - creates htmls structure suitable for YUI tree.
     */
    protected function htmllize_tree($tree, $dir) {
        global $CFG;

        if (empty($dir['subdirs']) and empty($dir['files'])) {
            return '';
        }
        $browser = get_file_browser();
        $result = '<ul>';
        foreach ($dir['subdirs'] as $subdir) {
            $image = $this->output->pix_icon(file_folder_icon(24), $subdir['dirname'], 'moodle');
            $filename = html_writer::tag('span', $image, array('class' => 'fp-icon')). html_writer::tag('span', s($subdir['dirname']), array('class' => 'fp-filename'));
            $filename = html_writer::tag('div', $filename, array('class' => 'fp-filename-icon'));
            $result .= html_writer::tag('li', $filename. $this->htmllize_tree($tree, $subdir));
        }
        foreach ($dir['files'] as $file) {
            $fileinfo = $browser->get_file_info($tree->context, $file->get_component(),
                    $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename());
            $url = $fileinfo->get_url(true);
            $filename = $file->get_filename();
            if ($imageinfo = $fileinfo->get_imageinfo()) {
                $fileurl = new moodle_url($fileinfo->get_url());
                $image = $fileurl->out(false, array('preview' => 'tinyicon', 'oid' => $fileinfo->get_timemodified()));
                $image = html_writer::empty_tag('img', array('src' => $image));
            } else {
                $image = $this->output->pix_icon(file_file_icon($file, 24), $filename, 'moodle');
            }
            $filename = html_writer::tag('span', $image, array('class' => 'fp-icon')). html_writer::tag('span', $filename, array('class' => 'fp-filename'));
            $filename = html_writer::tag('span', html_writer::link($url, $filename), array('class' => 'fp-filename-icon'));
            $result .= html_writer::tag('li', $filename);
        }
        $result .= '</ul>';

        return $result;
    }
 public function test_upload()
 {
     global $USER;
     $this->resetAfterTest();
     $this->setAdminUser();
     $context = context_user::instance($USER->id);
     $contextid = $context->id;
     $component = "user";
     $filearea = "private";
     $itemid = 0;
     $filepath = "/";
     $filename = "Simple.txt";
     $filecontent = base64_encode("Let us create a nice simple file");
     $browser = get_file_browser();
     // Make sure no file exists.
     $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
     $this->assertEmpty($file);
     // Call the api to create a file.
     core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath, $filename, $filecontent);
     // Make sure the file was created.
     $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
     $this->assertNotEmpty($file);
     // Make sure no file exists.
     $itemid = 2;
     $filename = "Simple2.txt";
     $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
     $this->assertEmpty($file);
     // Call the api to create a file.
     $fileinfo = core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath, $filename, $filecontent);
     // Make sure itemid is always set to 0.
     $this->assertEquals(0, $fileinfo['itemid']);
     // Make sure the same file cannot be created again.
     $this->setExpectedException("moodle_exception");
     core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath, $filename, $filecontent);
 }
 /**
  * Retrieve a file for a user of the Moodle client calling this function
  * The file is encoded in base64
  * @global <type> $DB
  * @global <type> $USER
  * @global <type> $MNET_REMOTE_CLIENT
  * @param <type> $username
  * @param <type> $source
  * @return <type>
  */
 public function retrieveFile($username, $source)
 {
     global $DB, $USER, $MNET_REMOTE_CLIENT;
     ///check the the user is known
     ///he has to be previously connected to the server site in order to be in the database
     //TODO: this seems weird - is it executed from cron or what? Please review
     $USER = $DB->get_record('user', array('username' => $username, 'mnethostid' => $MNET_REMOTE_CLIENT->id));
     if (empty($USER)) {
         exit(mnet_server_fault(9016, get_string('usernotfound', 'repository_remotemoodle', $username)));
     }
     $file = unserialize(base64_decode($source));
     $contextid = $file[0];
     $filearea = $file[1];
     $itemid = $file[2];
     $filepath = $file[3];
     $filename = $file[4];
     ///check that the user has read permission on this file
     $browser = get_file_browser();
     $fileinfo = $browser->get_file_info(get_context_instance_by_id($contextid), $filearea, $itemid, $filepath, $filename);
     if (empty($fileinfo)) {
         exit(mnet_server_fault(9016, get_string('usercannotaccess', 'repository_remotemoodle', $file)));
     }
     ///retrieve the file with file API functions and return it encoded in base64
     $fs = get_file_storage();
     $sf = $fs->get_file($contextid, $filearea, $itemid, $filepath, $filename);
     $contents = base64_encode($sf->get_content());
     return array($contents, $sf->get_filename());
 }
Exemplo n.º 4
0
 /**
  * Get file listing
  *
  * @param string $encodedpath
  * @return mixed
  */
 public function get_listing($encodedpath = '', $page = '')
 {
     global $CFG, $USER, $OUTPUT;
     $ret = array();
     $ret['dynload'] = true;
     $ret['nosearch'] = true;
     $ret['nologin'] = true;
     $list = array();
     if (!empty($encodedpath)) {
         $params = unserialize(base64_decode($encodedpath));
         if (is_array($params)) {
             $component = is_null($params['component']) ? NULL : clean_param($params['component'], PARAM_COMPONENT);
             $filearea = is_null($params['filearea']) ? NULL : clean_param($params['filearea'], PARAM_AREA);
             $itemid = is_null($params['itemid']) ? NULL : clean_param($params['itemid'], PARAM_INT);
             $filepath = is_null($params['filepath']) ? NULL : clean_param($params['filepath'], PARAM_PATH);
             $filename = is_null($params['filename']) ? NULL : clean_param($params['filename'], PARAM_FILE);
             $context = get_context_instance_by_id(clean_param($params['contextid'], PARAM_INT));
         }
     } else {
         $itemid = null;
         $filename = null;
         $filearea = null;
         $filepath = null;
         $component = null;
         if (!empty($this->context)) {
             list($context, $course, $cm) = get_context_info_array($this->context->id);
             if (is_object($course)) {
                 $context = get_context_instance(CONTEXT_COURSE, $course->id);
             } else {
                 $context = get_system_context();
             }
         } else {
             $context = get_system_context();
         }
     }
     $browser = get_file_browser();
     $list = array();
     if ($fileinfo = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename)) {
         // build file tree
         $element = repository_local_file::retrieve_file_info($fileinfo, $this);
         $nonemptychildren = $element->get_non_empty_children();
         foreach ($nonemptychildren as $child) {
             $list[] = (array) $child->get_node();
         }
     } else {
         // if file doesn't exist, build path nodes root of current context
         $fileinfo = $browser->get_file_info($context, null, null, null, null, null);
     }
     // build path navigation
     $ret['path'] = array();
     $element = repository_local_file::retrieve_file_info($fileinfo, $this);
     for ($level = $element; $level; $level = $level->get_parent()) {
         if ($level == $element || !$level->can_skip()) {
             array_unshift($ret['path'], $level->get_node_path());
         }
     }
     $ret['list'] = array_filter($list, array($this, 'filter'));
     return $ret;
 }
 /**
  * Get file listing
  *
  * @param string $encodedpath
  * @return mixed
  */
 public function get_listing($encodedpath = '')
 {
     global $CFG, $USER, $OUTPUT;
     $ret = array();
     $ret['dynload'] = true;
     $ret['nosearch'] = true;
     $ret['nologin'] = true;
     $list = array();
     if (!empty($encodedpath)) {
         $params = unserialize(base64_decode($encodedpath));
         if (is_array($params)) {
             $filepath = clean_param($params['filepath'], PARAM_PATH);
             $filename = clean_param($params['filename'], PARAM_FILE);
         }
     } else {
         $itemid = 0;
         $filepath = '/';
         $filename = null;
     }
     $filearea = 'private';
     $component = 'user';
     $itemid = 0;
     $context = get_context_instance(CONTEXT_USER, $USER->id);
     try {
         $browser = get_file_browser();
         if ($fileinfo = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename)) {
             $pathnodes = array();
             $level = $fileinfo;
             $params = $fileinfo->get_params();
             while ($level && $params['component'] == 'user' && $params['filearea'] == 'private') {
                 $encodedpath = base64_encode(serialize($level->get_params()));
                 $pathnodes[] = array('name' => $level->get_visible_name(), 'path' => $encodedpath);
                 $level = $level->get_parent();
                 $params = $level->get_params();
             }
             $ret['path'] = array_reverse($pathnodes);
             // build file tree
             $children = $fileinfo->get_children();
             foreach ($children as $child) {
                 if ($child->is_directory()) {
                     $encodedpath = base64_encode(serialize($child->get_params()));
                     $node = array('title' => $child->get_visible_name(), 'size' => 0, 'date' => '', 'path' => $encodedpath, 'children' => array(), 'thumbnail' => $OUTPUT->pix_url('f/folder-32')->out(false));
                     $list[] = $node;
                 } else {
                     $encodedpath = base64_encode(serialize($child->get_params()));
                     $node = array('title' => $child->get_visible_name(), 'size' => 0, 'date' => '', 'source' => $encodedpath, 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($child->get_visible_name(), 32))->out(false));
                     $list[] = $node;
                 }
             }
         }
     } catch (Exception $e) {
         throw new repository_exception('emptyfilelist', 'repository_user');
     }
     $ret['list'] = $list;
     $ret['list'] = array_filter($list, array($this, 'filter'));
     return $ret;
 }
Exemplo n.º 6
0
 /**
  * Return draft files information
  *
  * @global object $USER
  * @param string $fid file id
  * @param string $title
  * @param string $itemid
  * @return string the location of the file
  */
 public function get_file($fid, $title = '', $itemid = '')
 {
     global $USER;
     $ret = array();
     $browser = get_file_browser();
     $user_context = get_context_instance(CONTEXT_USER, $USER->id);
     $ret['itemid'] = $itemid;
     $ret['title'] = $title;
     $ret['contextid'] = $user_context->id;
     return $ret;
 }
Exemplo n.º 7
0
 function print_student_answer($userid, $return = false)
 {
     global $CFG, $USER, $OUTPUT;
     $fs = get_file_storage();
     $browser = get_file_browser();
     $output = '';
     if ($files = $fs->get_area_files($this->context->id, 'assignment_submission', $userid, "timemodified", false)) {
         foreach ($files as $file) {
             $filename = $file->get_filename();
             $found = true;
             $mimetype = $file->get_mimetype();
             $path = file_encode_url($CFG->wwwroot . '/pluginfile.php', '/' . $this->context->id . '/assignment_submission/' . $userid . '/' . $filename);
             $output .= '<a href="' . $path . '" ><img class="icon" src="' . $OUTPUT->old_icon_url(file_mimetype_icon($mimetype)) . '" alt="' . $mimetype . '" />' . s($filename) . '</a><br />';
         }
     }
     $output = '<div class="files">' . $output . '</div>';
     return $output;
 }
Exemplo n.º 8
0
 function print_student_answer($userid, $return = false)
 {
     global $CFG, $USER, $OUTPUT;
     $fs = get_file_storage();
     $browser = get_file_browser();
     $output = '';
     if ($submission = $this->get_submission($userid)) {
         if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) {
             foreach ($files as $file) {
                 $filename = $file->get_filename();
                 $found = true;
                 $mimetype = $file->get_mimetype();
                 $path = file_encode_url($CFG->wwwroot . '/pluginfile.php', '/' . $this->context->id . '/mod_assignment/submission/' . $submission->id . '/' . $filename);
                 $output .= '<a href="' . $path . '" >' . $OUTPUT->pix_icon(file_file_icon($file), get_mimetype_description($file), 'moodle', array('class' => 'icon')) . s($filename) . '</a><br />';
                 $output .= plagiarism_get_links(array('userid' => $userid, 'file' => $file, 'cmid' => $this->cm->id, 'course' => $this->course, 'assignment' => $this->assignment));
                 $output .= '<br/>';
             }
         }
     }
     $output = '<div class="files">' . $output . '</div>';
     return $output;
 }
Exemplo n.º 9
0
 /**
  * Uploading a file to moodle
  *
  * @param int    $contextid    context id
  * @param string $component    component
  * @param string $filearea     file area
  * @param int    $itemid       item id
  * @param string $filepath     file path
  * @param string $filename     file name
  * @param string $filecontent  file content
  * @param string $contextlevel Context level (block, course, coursecat, system, user or module)
  * @param int    $instanceid   Instance id of the item associated with the context level
  * @return array
  * @since Moodle 2.2
  */
 public static function upload($contextid, $component, $filearea, $itemid, $filepath, $filename, $filecontent, $contextlevel, $instanceid)
 {
     global $USER, $CFG;
     $fileinfo = self::validate_parameters(self::upload_parameters(), array('contextid' => $contextid, 'component' => $component, 'filearea' => $filearea, 'itemid' => $itemid, 'filepath' => $filepath, 'filename' => $filename, 'filecontent' => $filecontent, 'contextlevel' => $contextlevel, 'instanceid' => $instanceid));
     if (!isset($fileinfo['filecontent'])) {
         throw new moodle_exception('nofile');
     }
     // Saving file.
     $dir = make_temp_directory('wsupload');
     if (empty($fileinfo['filename'])) {
         $filename = uniqid('wsupload', true) . '_' . time() . '.tmp';
     } else {
         $filename = $fileinfo['filename'];
     }
     if (file_exists($dir . $filename)) {
         $savedfilepath = $dir . uniqid('m') . $filename;
     } else {
         $savedfilepath = $dir . $filename;
     }
     file_put_contents($savedfilepath, base64_decode($fileinfo['filecontent']));
     @chmod($savedfilepath, $CFG->filepermissions);
     unset($fileinfo['filecontent']);
     if (!empty($fileinfo['filepath'])) {
         $filepath = $fileinfo['filepath'];
     } else {
         $filepath = '/';
     }
     // Only allow uploads to draft area
     if (!($fileinfo['component'] == 'user' and $fileinfo['filearea'] == 'draft')) {
         throw new coding_exception('File can be uploaded to user draft area only');
     } else {
         $component = 'user';
         $filearea = $fileinfo['filearea'];
     }
     $itemid = 0;
     if (isset($fileinfo['itemid'])) {
         $itemid = $fileinfo['itemid'];
     }
     if ($filearea == 'draft' && $itemid <= 0) {
         // Generate a draft area for the files.
         $itemid = file_get_unused_draft_itemid();
     } else {
         if ($filearea == 'private') {
             // TODO MDL-31116 in user private area, itemid is always 0.
             $itemid = 0;
         }
     }
     // We need to preserve backword compatibility. Context id is no more a required.
     if (empty($fileinfo['contextid'])) {
         unset($fileinfo['contextid']);
     }
     // Get and validate context.
     $context = self::get_context_from_params($fileinfo);
     self::validate_context($context);
     if ($fileinfo['component'] == 'user' and $fileinfo['filearea'] == 'private') {
         throw new moodle_exception('privatefilesupload');
     }
     $browser = get_file_browser();
     // Check existing file.
     if ($file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename)) {
         throw new moodle_exception('fileexist');
     }
     // Move file to filepool.
     if ($dir = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, '.')) {
         $info = $dir->create_file_from_pathname($filename, $savedfilepath);
         $params = $info->get_params();
         unlink($savedfilepath);
         return array('contextid' => $params['contextid'], 'component' => $params['component'], 'filearea' => $params['filearea'], 'itemid' => $params['itemid'], 'filepath' => $params['filepath'], 'filename' => $params['filename'], 'url' => $info->get_url());
     } else {
         throw new moodle_exception('nofile');
     }
 }
Exemplo n.º 10
0
 /**
  * Download a file, this function can be overridden by
  * subclass.
  *
  * @global object $CFG
  * @param string $url the url of file
  * @param string $file save location
  * @return string the location of the file
  * @see curl package
  */
 public function get_file($encoded, $title = '', $itemid = '')
 {
     global $USER, $DB;
     $ret = array();
     $browser = get_file_browser();
     $params = unserialize(base64_decode($encoded));
     $user_context = get_context_instance(CONTEXT_USER, $USER->id);
     // the final file
     $contextid = $params['contextid'];
     $filearea = $params['filearea'];
     $filepath = $params['filepath'];
     $filename = $params['filename'];
     $fileitemid = $params['itemid'];
     $context = get_context_instance_by_id($contextid);
     $file_info = $browser->get_file_info($context, $filearea, $fileitemid, $filepath, $filename);
     $file_info->copy_to_storage($user_context->id, 'user_draft', $itemid, '/', $title);
     $ret['itemid'] = $itemid;
     $ret['title'] = $title;
     $ret['contextid'] = $user_context->id;
     return $ret;
 }
Exemplo n.º 11
0
    /**
     * Get file listing
     *
     * @param string $encodedpath
     * @return mixed
     */
    public function get_listing($encodedpath = '') {
        global $CFG, $USER, $OUTPUT;
        $ret = array();
        $ret['dynload'] = true;
        $ret['nosearch'] = true;
        $ret['nologin'] = true;
        $list = array();

        if (!empty($encodedpath)) {
            $params = unserialize(base64_decode($encodedpath));
            if (is_array($params)) {
                $component = is_null($params['component']) ? NULL : clean_param($params['component'], PARAM_COMPONENT);
                $filearea  = is_null($params['filearea']) ? NULL : clean_param($params['filearea'], PARAM_AREA);
                $itemid    = is_null($params['itemid']) ? NULL : clean_param($params['itemid'], PARAM_INT);
                $filepath  = is_null($params['filepath']) ? NULL : clean_param($params['filepath'], PARAM_PATH);;
                $filename  = is_null($params['filename']) ? NULL : clean_param($params['filename'], PARAM_FILE);
                $context = get_context_instance_by_id(clean_param($params['contextid'], PARAM_INT));
            }
        } else {
            $itemid   = null;
            $filename = null;
            $filearea = null;
            $filepath = null;
            $component = null;
            if (!empty($this->context)) {
                list($context, $course, $cm) = get_context_info_array($this->context->id);
                $courseid = is_object($course) ? $course->id : SITEID;
                $context = get_context_instance(CONTEXT_COURSE, $courseid);
            } else {
                $context = get_system_context();
            }
        }

        $browser = get_file_browser();

        if ($fileinfo = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename)) {
            // build path navigation
            $pathnodes = array();
            $encodedpath = base64_encode(serialize($fileinfo->get_params()));
            $pathnodes[] = array('name'=>$fileinfo->get_visible_name(), 'path'=>$encodedpath);
            $level = $fileinfo->get_parent();
            while ($level) {
                $encodedpath = base64_encode(serialize($level->get_params()));
                $pathnodes[] = array('name'=>$level->get_visible_name(), 'path'=>$encodedpath);
                $level = $level->get_parent();
            }
            if (!empty($pathnodes) && is_array($pathnodes)) {
                $pathnodes = array_reverse($pathnodes);
                $ret['path'] = $pathnodes;
            }
            // build file tree
            $children = $fileinfo->get_children();
            foreach ($children as $child) {
                if ($child->is_directory()) {
                    if ($child->is_empty_area()) {
                        continue;
                    }
                    $params = $child->get_params();
                    $encodedpath = base64_encode(serialize($params));
                    // hide user_private area from local plugin, user should
                    // use private file plugin to access private files
                    //if ($params['filearea'] == 'user_private') {
                        //continue;
                    //}
                    $node = array(
                        'title' => $child->get_visible_name(),
                        'size' => 0,
                        'date' => '',
                        'path' => $encodedpath,
                        'children'=>array(),
                        'thumbnail' => $OUTPUT->pix_url('f/folder-32')->out(false)
                    );
                    $list[] = $node;
                } else {
                    $encodedpath = base64_encode(serialize($child->get_params()));
                    $node = array(
                        'title' => $child->get_visible_name(),
                        'size' => 0,
                        'date' => '',
                        'source'=> $encodedpath,
                        'thumbnail' => $OUTPUT->pix_url(file_extension_icon($child->get_visible_name(), 32))->out(false)
                    );
                    $list[] = $node;
                }
            }
        } else {
            // if file doesn't exist, build path nodes root of current context
            $pathnodes = array();
            $fileinfo = $browser->get_file_info($context, null, null, null, null, null);
            $encodedpath = base64_encode(serialize($fileinfo->get_params()));
            $pathnodes[] = array('name'=>$fileinfo->get_visible_name(), 'path'=>$encodedpath);
            $level = $fileinfo->get_parent();
            while ($level) {
                $encodedpath = base64_encode(serialize($level->get_params()));
                $pathnodes[] = array('name'=>$level->get_visible_name(), 'path'=>$encodedpath);
                $level = $level->get_parent();
            }
            if (!empty($pathnodes) && is_array($pathnodes)) {
                $pathnodes = array_reverse($pathnodes);
                $ret['path'] = $pathnodes;
            }
            $list = array();
        }
        $ret['list'] = array_filter($list, array($this, 'filter'));
        return $ret;
    }
Exemplo n.º 12
0
function instance_remotedownload($contextid, $filename, $component, $filearea, $itemid, $requestid, $filepath = '/')
{
    global $CFG, $USER;
    //set up return object
    //set up return object
    $return = fetchReturnArray(true);
    //set up auto transcoding (mp3 or mp4) or not
    //The jsp to call is different.
    $jsp = "download.jsp";
    $convertlocally = false;
    $downloadfilename = $filename;
    $ext = substr($filename, -4);
    $filenamebase = substr($filename, 0, -4);
    switch ($ext) {
        case ".mp4":
            if ($CFG->filter_poodll_ffmpeg) {
                $convertlocally = true;
                $downloadfilename = $filenamebase . ".flv";
            } else {
                $jsp = "convert.jsp";
            }
            break;
        case ".mp3":
            if ($CFG->filter_poodll_ffmpeg) {
                $convertlocally = true;
                $downloadfilename = $filenamebase . ".flv";
            } else {
                $jsp = "convert.jsp";
            }
            break;
        case ".png":
            $jsp = "snapshot.jsp";
            break;
        default:
            $jsp = "download.jsp";
            break;
    }
    //setup our file manipulators
    $fs = get_file_storage();
    $browser = get_file_browser();
    //create the file record for our new file
    $file_record = new stdClass();
    $file_record->userid = $USER->id;
    $file_record->contextid = $contextid;
    $file_record->component = $component;
    $file_record->filearea = $filearea;
    $file_record->itemid = $itemid;
    $file_record->filepath = $filepath;
    $file_record->filename = $filename;
    $file_record->license = $CFG->sitedefaultlicense;
    $file_record->author = 'Moodle User';
    $file_record->source = '';
    $file_record->timecreated = time();
    $file_record->timemodified = time();
    //one condition of using this function is that only one file can be here,
    //attachment limits
    /*
    if($filearea=='draft'){
        $fs->delete_area_files($contextid,$component,$filearea,$itemid);
    }
    */
    //if file already exists, delete it
    //we could use fileinfo, but it don&'t work
    if ($fs->file_exists($contextid, $component, $filearea, $itemid, $filepath, $filename)) {
        //delete here ---
    }
    //setup download information
    $red5_fileurl = "http://" . $CFG->filter_poodll_servername . ":" . $CFG->filter_poodll_serverhttpport . "/poodll/" . $jsp . "?poodllserverid=" . $CFG->filter_poodll_serverid . "&filename=" . $downloadfilename . "&caller=" . urlencode($CFG->wwwroot);
    //download options
    $options = array();
    $options['headers'] = null;
    $options['postdata'] = null;
    $options['fullresponse'] = false;
    $options['timeout'] = 300;
    $options['connecttimeout'] = 20;
    $options['skipcertverify'] = false;
    $options['calctimeout'] = false;
    //clear the output buffer, otherwise strange characters can get in to our file
    //seems to have no effect though ...
    while (ob_get_level()) {
        ob_end_clean();
    }
    //branch logic depending on whether (converting locally) or (not conv||convert on server)
    if ($convertlocally) {
        //determine the temp directory
        if (isset($CFG->tempdir)) {
            $tempdir = $CFG->tempdir . "/";
        } else {
            //moodle 2.1 users have no $CFG->tempdir
            $tempdir = $CFG->dataroot . "/temp/";
        }
        //actually make the file on disk so FFMPEG can get it
        $mediastring = file_get_contents($red5_fileurl);
        $ret = file_put_contents($tempdir . $downloadfilename, $mediastring);
        //if successfully saved to disk, convert
        if ($ret) {
            $do_bg_encoding = $CFG->filter_poodll_bgtranscode_audio && $ext == ".mp3" || $CFG->filter_poodll_bgtranscode_video && $ext == ".mp4";
            if ($do_bg_encoding && $CFG->version >= 2014051200) {
                $stored_file = convert_with_ffmpeg_bg($file_record, $tempdir, $downloadfilename, $filenamebase, $ext);
            } else {
                $stored_file = convert_with_ffmpeg($file_record, $tempdir, $downloadfilename, $filenamebase, $ext);
            }
            if ($stored_file) {
                $filename = $stored_file->get_filename();
                //setup our return object
                $returnfilepath = $filename;
                array_push($return['messages'], $returnfilepath);
                //if failed, default to using the original uploaded data
                //and delete the temp file we made
            } else {
                $return['success'] = false;
                array_push($return['messages'], "Unable to convert file locally.");
                if (is_readable(realpath($tempdir . $filename))) {
                    unlink(realpath($tempdir . $filename));
                }
            }
        } else {
            $return['success'] = false;
            array_push($return['messages'], "Unable to create local temp file.");
        }
        //we process the result for return to browser
        $xml_output = prepareXMLReturn($return, $requestid);
        //we return to browser the result of our file operation
        return $xml_output;
    }
    //If get here we are downloading from JSP only, ie not converting locally
    //actually copy over the file from remote server
    if (!$fs->create_file_from_url($file_record, $red5_fileurl, $options, false)) {
        //	echo "boo:" . $red5_fileurl;
        $return['success'] = false;
        array_push($return['messages'], "Unable to create file from url.");
    } else {
        // echo "yay:" . $red5_fileurl;
        //get a file object if successful
        $thecontext = context::instance_by_id($contextid);
        //get_context_instance_by_id($contextid);
        $fileinfo = $browser->get_file_info($thecontext, $component, $filearea, $itemid, $filepath, $filename);
        //if we could get a fileinfo object, return the url of the object
        if ($fileinfo) {
            //$returnfilepath  = $fileinfo->get_url();
            //echo "boo:" . $red5_fileurl;
            $returnfilepath = $filename;
            array_push($return['messages'], $returnfilepath);
        } else {
            //if we couldn't get an url and it is a draft file, guess the URL
            //<p><a href="http://m2.poodll.com/draftfile.php/5/user/draft/875191859/IMG_0594.MOV">IMG_0594.MOV</a></p>
            if ($filearea == 'draft') {
                $returnfilepath = $filename;
                array_push($return['messages'], $returnfilepath);
            } else {
                $return['success'] = false;
                array_push($return['messages'], "Unable to get URL for file.");
            }
        }
        //end of if fileinfo
    }
    //end of if could create_file_from_url
    //we process the result for return to browser
    $xml_output = prepareXMLReturn($return, $requestid);
    //we return to browser the result of our file operation
    return $xml_output;
}
Exemplo n.º 13
0
function pathIsWritable($moduleid, $courseid, $itemid, $filearea, $filepath = DIRECTORY_SEPARATOR, $filename = "")
{
    global $DB;
    //get a handle on the module context
    $thiscontext = get_context_instance(CONTEXT_MODULE, $moduleid);
    //fetch info and ids about the module calling this data
    $course = $DB->get_record('course', array('id' => $courseid));
    $modinfo = get_fast_modinfo($course);
    $cm = $modinfo->get_cm($moduleid);
    $component = "mod_" . $cm->modname;
    //FIlter could submit submission/draft/content/intro as options here
    if ($filearea == "") {
        $filearea = "content";
    }
    //get our file object
    $filepath = DIRECTORY_SEPARATOR;
    $filename = "";
    $browser = get_file_browser();
    $fileinfo = $browser->get_file_info($thiscontext, $component, $filearea, $itemid, $filepath, $filename);
    //return writeable or not
    if ($fileinfo && $fileinfo->is_writable()) {
        return true;
    } else {
        return false;
    }
}
Exemplo n.º 14
0
 /**
  * Copy a file to file area
  *
  * @global object $USER
  * @global object $DB
  * @param string $encoded The metainfo of file, it is base64 encoded php serialized data
  * @param string $draftitemid itemid
  * @param string $new_filename The intended name of file
  * @param string $new_filepath the new path in draft area
  * @return array The information of file
  */
 public function copy_to_area($encoded, $draftitemid, $new_filepath, $new_filename)
 {
     global $USER, $DB;
     $info = array();
     $browser = get_file_browser();
     $fs = get_file_storage();
     $user_context = get_context_instance(CONTEXT_USER, $USER->id);
     // the final file
     $params = unserialize(base64_decode($encoded));
     $contextid = clean_param($params['contextid'], PARAM_INT);
     $fileitemid = clean_param($params['itemid'], PARAM_INT);
     $filename = clean_param($params['filename'], PARAM_FILE);
     $filepath = clean_param($params['filepath'], PARAM_PATH);
     $filearea = clean_param($params['filearea'], PARAM_ALPHAEXT);
     $component = clean_param($params['component'], PARAM_ALPHAEXT);
     $context = get_context_instance_by_id($contextid);
     if ($existingfile = $fs->get_file($user_context->id, 'user', 'draft', $draftitemid, $new_filepath, $new_filename)) {
         throw new moodle_exception('fileexists');
     }
     $file_info = $browser->get_file_info($context, $component, $filearea, $fileitemid, $filepath, $filename);
     $file_info->copy_to_storage($user_context->id, 'user', 'draft', $draftitemid, $new_filepath, $new_filename);
     $info['itemid'] = $draftitemid;
     $info['title'] = $new_filename;
     $info['contextid'] = $user_context->id;
     $info['filesize'] = $file_info->get_filesize();
     return $info;
 }
Exemplo n.º 15
0
 /**
  * Get file listing
  *
  * @param string $encodedpath
  * @return mixed
  */
 public function get_listing($encodedpath = '', $page = '')
 {
     global $CFG, $USER, $OUTPUT;
     $ret = array();
     $ret['dynload'] = true;
     $ret['nosearch'] = true;
     $ret['nologin'] = true;
     $manageurl = new moodle_url('/user/files.php');
     $ret['manage'] = $manageurl->out();
     $list = array();
     if (!empty($encodedpath)) {
         $params = json_decode(base64_decode($encodedpath), true);
         if (is_array($params)) {
             $filepath = clean_param($params['filepath'], PARAM_PATH);
             $filename = clean_param($params['filename'], PARAM_FILE);
         }
     } else {
         $itemid = 0;
         $filepath = '/';
         $filename = null;
     }
     $filearea = 'private';
     $component = 'user';
     $itemid = 0;
     $context = context_user::instance($USER->id);
     try {
         $browser = get_file_browser();
         if ($fileinfo = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename)) {
             $pathnodes = array();
             $level = $fileinfo;
             $params = $fileinfo->get_params();
             while ($level && $params['component'] == 'user' && $params['filearea'] == 'private') {
                 $encodedpath = base64_encode(json_encode($level->get_params()));
                 $pathnodes[] = array('name' => $level->get_visible_name(), 'path' => $encodedpath);
                 $level = $level->get_parent();
                 $params = $level->get_params();
             }
             $ret['path'] = array_reverse($pathnodes);
             // build file tree
             $children = $fileinfo->get_children();
             foreach ($children as $child) {
                 if ($child->is_directory()) {
                     $encodedpath = base64_encode(json_encode($child->get_params()));
                     $node = array('title' => $child->get_visible_name(), 'datemodified' => $child->get_timemodified(), 'datecreated' => $child->get_timecreated(), 'path' => $encodedpath, 'children' => array(), 'thumbnail' => $OUTPUT->pix_url(file_folder_icon(90))->out(false));
                     $list[] = $node;
                 } else {
                     $encodedpath = base64_encode(json_encode($child->get_params()));
                     $node = array('title' => $child->get_visible_name(), 'size' => $child->get_filesize(), 'datemodified' => $child->get_timemodified(), 'datecreated' => $child->get_timecreated(), 'author' => $child->get_author(), 'license' => $child->get_license(), 'isref' => $child->is_external_file(), 'source' => $encodedpath, 'icon' => $OUTPUT->pix_url(file_file_icon($child, 24))->out(false), 'thumbnail' => $OUTPUT->pix_url(file_file_icon($child, 90))->out(false));
                     if ($child->get_status() == 666) {
                         $node['originalmissing'] = true;
                     }
                     if ($imageinfo = $child->get_imageinfo()) {
                         $fileurl = new moodle_url($child->get_url());
                         $node['realthumbnail'] = $fileurl->out(false, array('preview' => 'thumb', 'oid' => $child->get_timemodified()));
                         $node['realicon'] = $fileurl->out(false, array('preview' => 'tinyicon', 'oid' => $child->get_timemodified()));
                         $node['image_width'] = $imageinfo['width'];
                         $node['image_height'] = $imageinfo['height'];
                     }
                     $list[] = $node;
                 }
             }
         }
     } catch (Exception $e) {
         throw new repository_exception('emptyfilelist', 'repository_user');
     }
     $ret['list'] = $list;
     $ret['list'] = array_filter($list, array($this, 'filter'));
     return $ret;
 }
Exemplo n.º 16
0
 /**
  * Print complete information about the user's interaction with the assignment
  *
  * @param $user object
  */
 function user_complete($user)
 {
     global $OUTPUT;
     if ($submission = $this->get_submission($user->id)) {
         $fs = get_file_storage();
         $browser = get_file_browser();
         if ($files = $fs->get_area_files($this->context->id, 'assignment_submission', $user->id, "timemodified", false)) {
             $countfiles = count($files) . " " . get_string("uploadedfiles", "assignment");
             foreach ($files as $file) {
                 $countfiles .= "; " . $file->get_filename();
             }
         }
         echo $OUTPUT->box_start();
         echo get_string("lastmodified") . ": ";
         echo userdate($submission->timemodified);
         echo $this->display_lateness($submission->timemodified);
         $this->print_user_files($user->id);
         echo '<br />';
         if (empty($submission->timemarked)) {
             print_string("notgradedyet", "assignment");
         } else {
             $this->view_feedback($submission);
         }
         echo $OUTPUT->box_end();
     } else {
         print_string("notsubmittedyet", "assignment");
     }
 }
Exemplo n.º 17
0
 /**
  * Return size of a file in bytes.
  *
  * @param string $source encoded and serialized data of file
  * @return int file size in bytes
  */
 public function get_file_size($source)
 {
     // TODO MDL-33297 remove this function completely?
     $browser = get_file_browser();
     $params = unserialize(base64_decode($source));
     $contextid = clean_param($params['contextid'], PARAM_INT);
     $fileitemid = clean_param($params['itemid'], PARAM_INT);
     $filename = clean_param($params['filename'], PARAM_FILE);
     $filepath = clean_param($params['filepath'], PARAM_PATH);
     $filearea = clean_param($params['filearea'], PARAM_AREA);
     $component = clean_param($params['component'], PARAM_COMPONENT);
     $context = context::instance_by_id($contextid);
     $file_info = $browser->get_file_info($context, $component, $filearea, $fileitemid, $filepath, $filename);
     if (!empty($file_info)) {
         $filesize = $file_info->get_filesize();
     } else {
         $filesize = null;
     }
     return $filesize;
 }
Exemplo n.º 18
0
 /**
  * Get file listing
  *
  * @param string $encodedpath
  * @param string $page no paging is used in repository_local
  * @return mixed
  */
 public function get_listing($encodedpath = '', $page = '')
 {
     global $CFG, $USER, $OUTPUT;
     $ret = array();
     $ret['dynload'] = true;
     $ret['nosearch'] = true;
     $ret['nologin'] = true;
     $ret['list'] = array();
     $itemid = null;
     $filename = null;
     $filearea = null;
     $filepath = null;
     $component = null;
     if (!empty($encodedpath)) {
         $params = json_decode(base64_decode($encodedpath), true);
         if (is_array($params) && isset($params['contextid'])) {
             $component = is_null($params['component']) ? NULL : clean_param($params['component'], PARAM_COMPONENT);
             $filearea = is_null($params['filearea']) ? NULL : clean_param($params['filearea'], PARAM_AREA);
             $itemid = is_null($params['itemid']) ? NULL : clean_param($params['itemid'], PARAM_INT);
             $filepath = is_null($params['filepath']) ? NULL : clean_param($params['filepath'], PARAM_PATH);
             $filename = is_null($params['filename']) ? NULL : clean_param($params['filename'], PARAM_FILE);
             $context = context::instance_by_id(clean_param($params['contextid'], PARAM_INT));
         }
     }
     if (empty($context) && !empty($this->context)) {
         $context = $this->context->get_course_context(false);
     }
     if (empty($context)) {
         $context = context_system::instance();
     }
     // prepare list of allowed extensions: $extensions is either string '*'
     // or array of lowercase extensions, i.e. array('.gif','.jpg')
     $extensions = optional_param_array('accepted_types', '', PARAM_RAW);
     if (empty($extensions) || $extensions === '*' || is_array($extensions) && in_array('*', $extensions)) {
         $extensions = '*';
     } else {
         if (!is_array($extensions)) {
             $extensions = array($extensions);
         }
         $extensions = array_map('core_text::strtolower', $extensions);
     }
     // build file tree
     $browser = get_file_browser();
     if (!($fileinfo = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename))) {
         // if file doesn't exist, build path nodes root of current context
         $fileinfo = $browser->get_file_info($context, null, null, null, null, null);
     }
     $ret['list'] = $this->get_non_empty_children($fileinfo, $extensions);
     // build path navigation
     $path = array();
     for ($level = $fileinfo; $level; $level = $level->get_parent()) {
         array_unshift($path, $level);
     }
     array_unshift($path, null);
     $ret['path'] = array();
     for ($i = 1; $i < count($path); $i++) {
         if ($path[$i] == $fileinfo || !$this->can_skip($path[$i], $extensions, $path[$i - 1])) {
             $ret['path'][] = $this->get_node_path($path[$i]);
         }
     }
     return $ret;
 }
Exemplo n.º 19
0
    function print_responsefiles($userid, $return=false) {
        global $CFG, $USER, $OUTPUT, $PAGE;

        $mode    = optional_param('mode', '', PARAM_ALPHA);
        $offset  = optional_param('offset', 0, PARAM_INT);

        $output = $OUTPUT->box_start('responsefiles');

        $candelete = $this->can_manage_responsefiles();
        $strdelete   = get_string('delete');

        $fs = get_file_storage();
        $browser = get_file_browser();

        if ($submission = $this->get_submission($userid)) {
            $renderer = $PAGE->get_renderer('mod_assignment');
            $output .= $renderer->assignment_files($this->context, $submission->id, 'response');
        }
        $output .= $OUTPUT->box_end();

        if ($return) {
            return $output;
        }
        echo $output;
    }
Exemplo n.º 20
0
 /**
  * Return size of a file in bytes.
  *
  * @param string $source encoded and serialized data of file
  * @return integer file size in bytes
  */
 public function get_file_size($source)
 {
     $browser = get_file_browser();
     $params = unserialize(base64_decode($source));
     $contextid = clean_param($params['contextid'], PARAM_INT);
     $fileitemid = clean_param($params['itemid'], PARAM_INT);
     $filename = clean_param($params['filename'], PARAM_FILE);
     $filepath = clean_param($params['filepath'], PARAM_PATH);
     $filearea = clean_param($params['filearea'], PARAM_SAFEDIR);
     $component = clean_param($params['component'], PARAM_ALPHAEXT);
     $context = get_context_instance_by_id($contextid);
     $file_info = $browser->get_file_info($context, $component, $filearea, $fileitemid, $filepath, $filename);
     if (!empty($file_info)) {
         $filesize = $file_info->get_filesize();
     } else {
         $filesize = null;
     }
     return $filesize;
 }
Exemplo n.º 21
0
 /**
  * render the files in file area
  * @param string $component
  * @param string $area
  * @param int $submissionid
  * @return string
  */
 public function render_area_files($component, $area, $submissionid)
 {
     global $USER;
     if (!$submissionid) {
         $submission = $this->get_user_submission($USER->id, false);
         $submissionid = $submission->id;
     }
     $fs = get_file_storage();
     $browser = get_file_browser();
     $files = $fs->get_area_files($this->get_context()->id, $component, $area, $submissionid, "timemodified", false);
     return $this->output->assign_files($this->context, $submissionid, $area, $component);
 }
Exemplo n.º 22
0
 function fetch_poodllaudiolist($moduleid, $courseid, $path = "/", $playertype, $filearea, $listtype = "xml")
 {
     global $CFG, $DB, $COURSE;
     //if a single file was passed in, just play that alone.
     //for PoodlL 2 this is all we can do in a question right now
     if (strlen($path) > 4 && substr($path, -4) == ".flv") {
         switch ($listtype) {
             case "xml":
                 $ret_output = "<audios>\n";
                 $ret_output .= "\t<audio audioname='" . basename($path) . "' playertype='" . $playertype . "' url='" . trim($path) . "'/>\n";
                 $ret_output .= "</audios>\n";
                 break;
             case "rss":
                 $ret_output = "<channel><title></title>";
                 break;
             case "alinks":
                 $ret_output = "<div class=\"poodllplaylist\">";
                 $ret_output .= "<a href=\"" . trim($path) . "\"><span>" . basename($path) . "</span></a>";
                 $ret_output .= "</div>";
                 break;
         }
         return $ret_output;
     }
     //FIlter could submit submission/draft/content/intro as options here
     if ($filearea == "") {
         $filearea = "content";
     }
     //make sure we have a trailing slash
     if (strlen($path) > 0) {
         if (substr($path, -1) != '/') {
             $path .= "/";
         }
         if (substr($path, 0, 1) != '/') {
             $path = "/" . $path;
         }
     } else {
         $path = "/";
     }
     //set up xml/div to return
     switch ($listtype) {
         case "xml":
             $ret_output = "<audios>\n";
             break;
         case "rss":
             $ret_output = "<channel><title></title>";
             break;
         case "alist":
             $ret_output = "<div class=\"poodllplaylist\">";
             break;
     }
     if ($filearea == "poodlldata") {
         //if(strlen($path)>6 && true){
         //If we are using PoodLL Data Dir file handling, we build a list of files here:
         //=============================================
         //filter file types
         $filterstring = "/*.{flv,mp3,mp4}";
         //set up the search dir
         $baseDir = $CFG->{'dirroot'} . "/" . $CFG->{'filter_poodll_datadir'} . $path;
         $baseURL = $CFG->{'wwwroot'} . "/" . $CFG->{'filter_poodll_datadir'} . $path;
         //for debugging
         //$ret_output .= $baseDir . " " . $baseURL;
         foreach (glob($baseDir . $filterstring, GLOB_BRACE) as $filename) {
             $urltofile = $baseURL . basename($filename);
             switch ($listtype) {
                 case "xml":
                     $ret_output .= "\t<audio audioname='" . basename($filename) . "' playertype='" . $playertype . "' url='" . $urltofile . "'/>\n";
                     break;
                     //"type" was necessary for flowplayer ??? but it breaks JWPlayer
                     // justin 20141220
                 //"type" was necessary for flowplayer ??? but it breaks JWPlayer
                 // justin 20141220
                 case "xrss":
                     $ext = substr($filename, -4);
                     switch ($ext) {
                         case ".mp3":
                             $mimetype = "audio/mpeg3";
                             break;
                         case ".flv":
                             $mimetype = "audio/mp4";
                             break;
                         case ".mp4":
                             $mimetype = "video/x-flv";
                             break;
                     }
                     $ret_output .= "\t<item><title>" . basename($filename) . "</title><media:content url=\"" . trim($urltofile) . "\" type=\"" . $mimetype . "\"/></item>";
                     break;
                 case "rss":
                     $ret_output .= "\t<item><title>" . basename($filename) . "</title><media:content url=\"" . trim($urltofile) . "\" /></item>";
                     break;
                 case "alist":
                     $ret_output .= "<a href=\"" . trim($urltofile) . "\"><span>" . basename($filename) . "</span></a>";
                     break;
             }
             //$xml_output .=  "\t<audio audioname='" . basename($filename) ."' playertype='" . $playertype . "' url='" . $baseURL . basename($filename). "'/>\n";
         }
         //=============================================
         //end of PoodLL Data Dir
     } else {
         //fetch info and ids about the module calling this data
         $course = $DB->get_record('course', array('id' => $courseid));
         $modinfo = get_fast_modinfo($course);
         $cm = $modinfo->get_cm($moduleid);
         //If we are using Moodle 2 file handling, we build a list of files here:
         //=============================================
         //get filehandling objects
         $browser = get_file_browser();
         $fs = get_file_storage();
         //get a handle on the module context
         $thiscontext = \context_module::instance($moduleid);
         $contextid = $thiscontext->id;
         //fetch a list of files in this area, and sort them alphabetically
         $files = $fs->get_area_files($contextid, "mod_" . $cm->modname, $filearea);
         usort($files, '\\filter_poodll\\poodlltools\\srtFilenames');
         //loop through all the media files and load'em up
         foreach ($files as $f) {
             $filename = trim($f->get_filename());
             //if we are not a directory and filename is long enough and extension is mp3 or flv or mp4, we proceed
             if ($filename != ".") {
                 if (strlen($filename) > 4) {
                     $ext = substr($filename, -4);
                     if ($ext == ".mp3" || $ext == ".mp4" || $ext == ".flv") {
                         switch ($ext) {
                             case ".mp3":
                                 $mimetype = "audio/mpeg3";
                                 break;
                             case ".flv":
                                 $mimetype = "audio/mp4";
                                 break;
                             case ".mp4":
                                 $mimetype = "video/x-flv";
                                 break;
                         }
                         //fetch our info object
                         $fileinfo = $browser->get_file_info($thiscontext, $f->get_component(), $f->get_filearea(), $f->get_itemid(), $f->get_filepath(), $f->get_filename());
                         //if we are at the dir level
                         if ($f->get_filepath() == $path) {
                             //get the url to the file and add it to the XML
                             $urltofile = $fileinfo->get_url();
                             switch ($listtype) {
                                 case "xml":
                                     $ret_output .= "\t<audio audioname='" . basename($filename) . "' playertype='" . $playertype . "' url='" . trim($urltofile) . "'/>\n";
                                     break;
                                 case "rss":
                                     $ret_output .= "\t<item><title>" . basename($filename) . "</title><media:content url=\"" . trim($urltofile) . "\" type=\"" . $mimetype . "\"/></item>";
                                     break;
                                 case "alist":
                                     $ret_output .= "<a href=\"" . trim($urltofile) . "\"><span>" . basename($filename) . "</span></a>";
                                     break;
                             }
                         }
                     }
                 }
             }
         }
         //=============================================
         //end of Moodle 2 file
     }
     //for debugging
     //$ret_output .=  "\t<audio audioname='" . $cm->modname  . " " . $filearea . " " . $urltofile ."' playertype='" . $playertype . "' url='" . $mediapath . basename($contextid). "'/>\n";
     //close xml/alist tags to return
     switch ($listtype) {
         case "xml":
             $ret_output .= "</audios>";
             break;
         case "rss":
             $ret_output .= "</channel>";
             break;
         case "alist":
             $ret_output .= "</div>";
             break;
     }
     //Return the data
     return $ret_output;
 }
Exemplo n.º 23
0
if (preg_match($regex, $imgdataurl, $matches)) {
    $mimetype = $matches[1];
    $base64data = $matches[2];
    $imgdataurl = null;
} else {
    paintweb_send_result($imgurl, $imgurlnew, false, get_string('moodleServer:malformedDataUrl', 'paintweb'));
}
if (empty($base64data) || !isset($imgallowedtypes[$mimetype])) {
    paintweb_send_result($imgurl, $imgurlnew, false, get_string('moodleServer:malformedDataUrl', 'paintweb'));
}
$imgdata = base64_decode($base64data);
$base64data = null;
$filename = 'paintweb_' . sha1($imgdata) . '.' . $imgallowedtypes[$mimetype];
// Save the file using the new File API.
$fs = get_file_storage();
$fbrowser = get_file_browser();
$file_record = new object();
$file_record->contextid = $context->id;
$file_record->filearea = $filearea;
$file_record->itemid = $draftitemid;
$file_record->filepath = $filepath;
$file_record->filename = $filename;
$file_record->userid = $USER->id;
try {
    $file = $fs->create_file_from_string($file_record, $imgdata);
} catch (Exception $err) {
    paintweb_send_result($imgurl, $imgurlnew, false, $err->getMessage());
}
$imgdata = null;
$binfo = $fbrowser->get_file_info($context, $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename());
if (empty($binfo)) {
 function fetch_poodllmedialist($moduleid, $courseid, $path = "/", $playertype, $filearea)
 {
     global $CFG, $DB;
     //FIlter could submit submission/draft/content/intro as options here
     if ($filearea == "") {
         $filearea = "content";
     }
     //fetch info and ids about the module calling this data
     $course = $DB->get_record('course', array('id' => $courseid));
     $modinfo = get_fast_modinfo($course);
     $cm = $modinfo->get_cm($moduleid);
     //make sure we have a trailing slash and leading slash
     if (strlen($path) > 0) {
         if (substr($path, -1) != '/') {
             $path .= "/";
         }
         if (substr($path, 0, 1) != '/') {
             $path = "/" . $path;
         }
     } else {
         $path = "/";
     }
     //set up xml to return
     $xml_output = "<videos>\n";
     //get filehandling objects
     $browser = get_file_browser();
     $fs = get_file_storage();
     //get a handle on the module context
     $thiscontext = get_context_instance(CONTEXT_MODULE, $moduleid);
     $contextid = $thiscontext->id;
     //fetch a list of files in this area, and sort them alphabetically
     $files = $fs->get_area_files($contextid, "mod_" . $cm->modname, $filearea);
     usort($files, "self::cmpFilenames");
     //loop through all the media files and load'em up
     foreach ($files as $f) {
         $filename = trim($f->get_filename());
         //if we are not a directory and filename is long enough and extension is mp3 or flv or mp4, we proceed
         if ($filename != ".") {
             if (strlen($filename) > 4) {
                 $ext = substr($filename, -4);
                 if ($ext == ".mp3" || $ext == ".mp4" || $ext == ".flv") {
                     //fetch our info object
                     $fileinfo = $browser->get_file_info($thiscontext, $f->get_component(), $f->get_filearea(), $f->get_itemid(), $f->get_filepath(), $f->get_filename());
                     //if we are at the dir level
                     if ($f->get_filepath() == $path) {
                         //get the url to the file and add it to the XML
                         $urltofile = $fileinfo->get_url();
                         $xml_output .= "\t<video videoname='" . basename($filename) . "' playertype='" . $playertype . "' url='" . trim($urltofile) . "'/>\n";
                     }
                 }
             }
         }
     }
     //close xml to return
     $xml_output .= "</videos>";
     //Return the data
     return $xml_output;
 }
Exemplo n.º 25
0
    public function get_link($encoded) {
        $info = array();

        $browser = get_file_browser();

        // the final file
        $params = unserialize(base64_decode($encoded));
        $contextid  = clean_param($params['contextid'], PARAM_INT);
        $fileitemid = clean_param($params['itemid'], PARAM_INT);
        $filename = clean_param($params['filename'], PARAM_FILE);
        $filepath = clean_param($params['filepath'], PARAM_PATH);;
        $filearea = clean_param($params['filearea'], PARAM_ALPHAEXT);
        $component = clean_param($params['component'], PARAM_ALPHAEXT);
        $context = get_context_instance_by_id($contextid);

        $file_info = $browser->get_file_info($context, $component, $filearea, $fileitemid, $filepath, $filename);
        return $file_info->get_url();
    }
Exemplo n.º 26
0
 /**
  * Test add_user_private_files
  */
 public function test_add_user_private_files()
 {
     global $USER, $CFG, $DB;
     $this->resetAfterTest(true);
     $context = context_system::instance();
     $roleid = $this->assignUserCapability('moodle/user:manageownfiles', $context->id);
     $context = context_user::instance($USER->id);
     $contextid = $context->id;
     $component = "user";
     $filearea = "draft";
     $itemid = 0;
     $filepath = "/";
     $filename = "Simple.txt";
     $filecontent = base64_encode("Let us create a nice simple file");
     $contextlevel = null;
     $instanceid = null;
     $browser = get_file_browser();
     // Call the files api to create a file.
     $draftfile = core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath, $filename, $filecontent, $contextlevel, $instanceid);
     $draftid = $draftfile['itemid'];
     // Make sure the file was created.
     $file = $browser->get_file_info($context, $component, $filearea, $draftid, $filepath, $filename);
     $this->assertNotEmpty($file);
     // Make sure the file does not exist in the user private files.
     $file = $browser->get_file_info($context, $component, 'private', 0, $filepath, $filename);
     $this->assertEmpty($file);
     // Call the external function.
     core_user_external::add_user_private_files($draftid);
     // Make sure the file was added to the user private files.
     $file = $browser->get_file_info($context, $component, 'private', 0, $filepath, $filename);
     $this->assertNotEmpty($file);
 }
Exemplo n.º 27
0
/**
 * Produces a list of links to the issued certificates.  Used for report.
 *
 * @param stdClass $certificate
 * @param int $userid
 * @param stdClass $context
 * @return boolean, true if success printing
 */
function certificate_print_user_files($certificate, $userid, $context)
{
    global $CFG, $DB, $OUTPUT;
    $output = '';
    $certrecord = certificate_get_latest_issue($certificate->id, $userid);
    $fs = get_file_storage();
    $browser = get_file_browser();
    $component = 'mod_certificate';
    $filearea = 'issue';
    $files = $fs->get_area_files($context, $component, $filearea, $certrecord->id);
    foreach ($files as $file) {
        $filename = $file->get_filename();
        $mimetype = $file->get_mimetype();
        $link = file_encode_url($CFG->wwwroot . '/pluginfile.php', '/' . $context . '/mod_certificate/issue/' . $certrecord->id . '/' . $filename);
        $output = '<img src="' . $OUTPUT->pix_url(file_mimetype_icon($file->get_mimetype())) . '" height="16" width="16" alt="' . $file->get_mimetype() . '" />&nbsp;' . '<a href="' . $link . '" >' . s($filename) . '</a>';
    }
    $output .= '<br />';
    $output = '<div class="files">' . $output . '</div>';
    return $output;
}
Exemplo n.º 28
0
 function display_browse_field($recordid, $template)
 {
     global $CFG, $DB;
     if (!($content = $DB->get_record('data_content', array('fieldid' => $this->field->id, 'recordid' => $recordid)))) {
         return '';
     }
     if (empty($content->content)) {
         return '';
     }
     $browser = get_file_browser();
     if (!($file = $this->get_file($recordid, $content))) {
         return '';
     }
     $name = empty($content->content1) ? $file->get_filename() : $content->content1;
     $src = file_encode_url($CFG->wwwroot . '/pluginfile.php', '/' . $this->context->id . '/data_content/' . $content->id . '/' . $file->get_filename());
     $width = $this->field->param1 ? ' width  = "' . s($this->field->param1) . '" ' : ' ';
     $height = $this->field->param2 ? ' height = "' . s($this->field->param2) . '" ' : ' ';
     $str = '<img src="' . $OUTPUT->old_icon_url(file_mimetype_icon($file->get_mimetype())) . '" height="16" width="16" alt="' . $file->get_mimetype() . '" />&nbsp;' . '<a href="' . $src . '" >' . s($name) . '</a>';
     return $str;
 }
Exemplo n.º 29
0
 /**
  * Render the files in file area.
  *
  * @param string $component
  * @param string $area
  * @param int $submissionid
  * @return string
  */
 public function render_area_files($component, $area, $submissionid)
 {
     global $USER;
     $fs = get_file_storage();
     $browser = get_file_browser();
     $files = $fs->get_area_files($this->get_context()->id, $component, $area, $submissionid, 'timemodified', false);
     return $this->get_renderer()->assign_files($this->context, $submissionid, $area, $component);
 }
Exemplo n.º 30
0
function referentiel_get_manage_block_files1($contextid, $itemid, $filename, $titre, $appli)
{
    $browser = get_file_browser();
    $filename = null;
    if ($fileinfo = $browser->get_file_info($contextid, 'block_referentiel', 'referentiel', $itemid, '/', $filename)) {
        // build a Breadcrumb trail
        $level = $fileinfo->get_parent();
        while ($level) {
            $path[] = array('name' => $level->get_visible_name());
            $level = $level->get_parent();
        }
        $path = array_reverse($path);
        $children = $fileinfo->get_children();
        foreach ($children as $child) {
            if ($child->is_directory()) {
                echo $child->get_visible_name();
                // display contextid, itemid, component, filepath and filename
                var_dump($child->get_params());
            }
        }
    }
}