public function get_listing($path = '', $page = '')
 {
     global $CFG, $OUTPUT;
     $list = array();
     $list['list'] = array();
     // the management interface url
     $list['manage'] = false;
     // dynamically loading
     $list['dynload'] = true;
     // the current path of this list.
     // set to true, the login link will be removed
     $list['nologin'] = true;
     // set to true, the search button will be removed
     $list['nosearch'] = true;
     $tree = array();
     if (empty($path)) {
         $buckets = $this->s->listBuckets();
         foreach ($buckets as $bucket) {
             $folder = array('title' => $bucket, 'children' => array(), 'thumbnail' => $OUTPUT->old_icon_url('f/folder-32'), 'path' => $bucket);
             $tree[] = $folder;
         }
     } else {
         $contents = $this->s->getBucket($path);
         foreach ($contents as $file) {
             $info = $this->s->getObjectInfo($path, baseName($file['name']));
             $tree[] = array('title' => $file['name'], 'size' => $file['size'], 'date' => userdate($file['time']), 'source' => $path . '/' . $file['name'], 'thumbnail' => $OUTPUT->old_icon_url(file_extension_icon($file['name'], 32)));
         }
     }
     $list['list'] = $tree;
     return $list;
 }
 function definition()
 {
     global $CFG;
     $mform =& $this->_form;
     //--------------------------------------------------------------------------------
     $urls = $this->_customdata['urls'];
     $fromareaname = $this->_customdata['fromareaname'];
     $toareaname = $this->_customdata['toareaname'];
     $fileoptions = array(QUESTION_FILEDONOTHING => get_string('donothing', 'question'), QUESTION_FILECOPY => get_string('copy', 'question', $fromareaname), QUESTION_FILEMOVE => get_string('move', 'question', $fromareaname), QUESTION_FILEMOVELINKSONLY => get_string('movelinksonly', 'question', $fromareaname));
     $brokenfileoptions = array(QUESTION_FILEDONOTHING => get_string('donothing', 'question'), QUESTION_FILEMOVELINKSONLY => get_string('movelinksonly', 'question', $fromareaname));
     $brokenurls = $this->_customdata['brokenurls'];
     if (count($urls)) {
         $mform->addElement('header', 'general', get_string('filestomove', 'question', $toareaname));
         $i = 0;
         foreach (array_keys($urls) as $url) {
             $icontype = mimeinfo('type', $url);
             $img = "<img src=\"" . $OUTPUT->old_icon_url(file_extension_icon($url)) . "\"  class=\"icon\" alt=\"{$icontype}\" />";
             if (in_array($url, $brokenurls)) {
                 $mform->addElement('select', "urls[{$i}]", $img . $url, $brokenfileoptions);
             } else {
                 $mform->addElement('select', "urls[{$i}]", $img . $url, $fileoptions);
             }
             $i++;
         }
     }
     if (count($brokenurls)) {
         $mform->addElement('advcheckbox', 'ignorebroken', get_string('ignorebroken', 'question'));
     }
     //--------------------------------------------------------------------------------
     $this->add_action_buttons(true, get_string('moveq', 'question'));
 }
 public function get_listing($path = '', $page = '')
 {
     global $OUTPUT;
     $client = new wikimedia();
     $result = $client->search_images($this->keyword);
     $list = array();
     $list['list'] = array();
     foreach ($result as $title => $url) {
         $list['list'][] = array('title' => substr($title, 5), 'thumbnail' => $OUTPUT->old_icon_url(file_extension_icon('xx.jpg', 32)), 'source' => $url, 'url' => $url);
     }
     return $list;
 }
 function get_content()
 {
     global $CFG, $DB, $OUTPUT;
     if ($this->content !== NULL) {
         return $this->content;
     }
     $this->content = new stdClass();
     $this->content->items = array();
     $this->content->icons = array();
     $this->content->footer = '';
     $course = $this->page->course;
     require_once $CFG->dirroot . '/course/lib.php';
     $modinfo = get_fast_modinfo($course);
     $modfullnames = array();
     $archetypes = array();
     foreach ($modinfo->cms as $cm) {
         // Exclude activities which are not visible or have no link (=label)
         if (!$cm->uservisible or !$cm->has_view()) {
             continue;
         }
         if (array_key_exists($cm->modname, $modfullnames)) {
             continue;
         }
         if (!array_key_exists($cm->modname, $archetypes)) {
             $archetypes[$cm->modname] = plugin_supports('mod', $cm->modname, FEATURE_MOD_ARCHETYPE, MOD_ARCHETYPE_OTHER);
         }
         if ($archetypes[$cm->modname] == MOD_ARCHETYPE_RESOURCE) {
             if (!array_key_exists('resources', $modfullnames)) {
                 $modfullnames['resources'] = get_string('resources');
             }
         } else {
             $modfullnames[$cm->modname] = $cm->modplural;
         }
     }
     collatorlib::asort($modfullnames);
     foreach ($modfullnames as $modname => $modfullname) {
         if ($modname === 'resources') {
             $icon = $OUTPUT->pix_icon(file_extension_icon('.htm'), '', 'moodle', array('class' => 'icon')) . '&nbsp;';
             $this->content->items[] = '<a href="' . $CFG->wwwroot . '/course/resources.php?id=' . $course->id . '">' . $icon . $modfullname . '</a>';
         } else {
             $icon = '<img src="' . $OUTPUT->pix_url('icon', $modname) . '" class="icon" alt="" />&nbsp;';
             $this->content->items[] = '<a href="' . $CFG->wwwroot . '/mod/' . $modname . '/index.php?id=' . $course->id . '">' . $icon . $modfullname . '</a>';
         }
     }
     return $this->content;
 }
Beispiel #5
0
 public function get_listing($path = '', $page = '')
 {
     global $CFG, $OUTPUT;
     $list = array();
     $list['list'] = array();
     // process breacrumb trail
     $list['path'] = array(array('name' => 'Root', '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);
         }
         $this->root_path .= $path . '/';
     }
     $list['manage'] = false;
     $list['dynload'] = true;
     $list['nologin'] = true;
     $list['nosearch'] = true;
     if ($dh = opendir($this->root_path)) {
         while (($file = readdir($dh)) != false) {
             if ($file != '.' and $file != '..') {
                 if (filetype($this->root_path . $file) == 'file') {
                     $list['list'][] = array('title' => $file, 'source' => $path . '/' . $file, 'size' => filesize($this->root_path . $file), 'date' => time(), 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($this->root_path . $file, 32))->out(false));
                 } else {
                     if (!empty($path)) {
                         $current_path = $path . '/' . $file;
                     } else {
                         $current_path = $file;
                     }
                     $list['list'][] = array('title' => $file, 'children' => array(), 'thumbnail' => $OUTPUT->pix_url('f/folder-32')->out(false), 'path' => $current_path);
                 }
             }
         }
     }
     $list['list'] = array_filter($list['list'], array($this, 'filter'));
     return $list;
 }
Beispiel #6
0
 function get_listing($path = '', $page = '')
 {
     global $OUTPUT;
     if (!empty($path)) {
         $pathArray = json_decode($path);
     } else {
         $pathArray = array(array('name' => '', 'type' => 'top-communities'));
     }
     $list = array();
     $list['nologin'] = true;
     $list['dynload'] = true;
     $list['nosearch'] = true;
     $list['list'] = array();
     $lastPath = (array) end($pathArray);
     $results = $this->getChildrenByType($lastPath);
     foreach ($results as $result) {
         $itemPath = $pathArray;
         $itemPath[] = array('name' => $result->name, 'id' => $result->id, 'type' => $result->type);
         if ($result->countItems === 0) {
             continue;
         }
         $baseElement = array('title' => $result->name);
         switch ($result->type) {
             case 'community':
             case 'collection':
             case 'item':
                 $typeOptions = array('children' => array(), 'dynload' => true, 'thumbnail' => $OUTPUT->pix_url(file_folder_icon(64))->out(false), 'path' => json_encode($itemPath));
                 break;
             case 'bitstream':
                 $typeOptions = array('thumbnail' => $OUTPUT->pix_url(file_extension_icon($result->name, 64))->out(false), 'url' => $this->rest_url . 'bitstreams/' . $result->id . '/retrieve', 'source' => $this->rest_url . 'bitstreams/' . $result->id . '/retrieve');
                 break;
         }
         $list['list'][] = array_merge($baseElement, $typeOptions);
     }
     $list['path'] = array();
     while (count($pathArray) > 0) {
         $lastItem = (array) end($pathArray);
         array_unshift($list['path'], array('path' => json_encode($pathArray), 'name' => strlen($lastItem['name']) <= 20 ? $lastItem['name'] : substr($lastItem['name'], 0, 17) . '...'));
         array_pop($pathArray);
     }
     return $list;
 }
Beispiel #7
0
 /**
  * Get S3 file list
  *
  * @param string $path this parameter can a folder name, or a identification of folder
  * @param string $page the page number of file list
  * @return array the list of files, including some meta infomation
  */
 public function get_listing($path = '', $page = '')
 {
     global $OUTPUT;
     $s = $this->create_s3();
     $bucket = $this->get_option('bucket_name');
     $list = array();
     $list['list'] = array();
     $list['path'] = array(array('name' => $bucket, 'path' => ''));
     $list['manage'] = false;
     $list['dynload'] = true;
     $list['nologin'] = true;
     $list['nosearch'] = true;
     $files = array();
     $folders = array();
     try {
         $contents = $s->getBucket($bucket, $path, null, null, '/', true);
     } catch (S3Exception $e) {
         throw new moodle_exception('errorwhilecommunicatingwith', 'repository', '', $this->get_name(), $e->getMessage());
     }
     foreach ($contents as $object) {
         if (isset($object['prefix'])) {
             $title = rtrim($object['prefix'], '/');
         } else {
             $title = $object['name'];
         }
         if (strlen($path) > 0) {
             $title = substr($title, strlen($path));
             if (empty($title) && !is_numeric($title)) {
                 continue;
             }
         }
         if (isset($object['prefix'])) {
             $folders[] = array('title' => $title, 'children' => array(), 'thumbnail' => $OUTPUT->pix_url(file_folder_icon(90))->out(false), 'path' => $object['prefix']);
         } else {
             $files[] = array('title' => $title, 'size' => $object['size'], 'datemodified' => $object['time'], 'source' => $object['name'], 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($title, 90))->out(false));
         }
     }
     $list['list'] = array_merge($folders, $files);
     return $list;
 }
Beispiel #8
0
    /**
     * Get S3 file list
     *
     * @param string $path
     * @return array The file list and options
     */
    public function get_listing($path = '', $page = '') {
        global $CFG, $OUTPUT;
        if (empty($this->access_key)) {
            throw new moodle_exception('needaccesskey', 'repository_s3');
        }

        $list = array();
        $list['list'] = array();
        $list['path'] = array(
            array('name' => get_string('pluginname', 'repository_s3'), 'path' => '')
        );

        // the management interface url
        $list['manage'] = false;
        // dynamically loading
        $list['dynload'] = true;
        // the current path of this list.
        // set to true, the login link will be removed
        $list['nologin'] = true;
        // set to true, the search button will be removed
        $list['nosearch'] = true;

        $tree = array();

        if (empty($path)) {
            try {
                $buckets = $this->s->listBuckets();
            } catch (S3Exception $e) {
                throw new moodle_exception('errorwhilecommunicatingwith', 'repository', '', $this->get_name());
            }
            foreach ($buckets as $bucket) {
                $folder = array(
                    'title' => $bucket,
                    'children' => array(),
                    'thumbnail' => $OUTPUT->pix_url(file_folder_icon(90))->out(false),
                    'path' => $bucket
                    );
                $tree[] = $folder;
            }
        } else {
            $files = array();
            $folders = array();
            list($bucket, $uri) = $this->explode_path($path);

            try {
                $contents = $this->s->getBucket($bucket, $uri, null, null, '/', true);
            } catch (S3Exception $e) {
                throw new moodle_exception('errorwhilecommunicatingwith', 'repository', '', $this->get_name());
            }
            foreach ($contents as $object) {

                // If object has a prefix, it is a 'CommonPrefix', which we consider a folder
                if (isset($object['prefix'])) {
                    $title = rtrim($object['prefix'], '/');
                } else {
                    $title = $object['name'];
                }

                // Removes the prefix (folder path) from the title
                if (strlen($uri) > 0) {
                    $title = substr($title, strlen($uri));
                    // Check if title is empty and not zero
                    if (empty($title) && !is_numeric($title)) {
                        // Amazon returns the prefix itself, we skip it
                        continue;
                    }
                }

                // This is a so-called CommonPrefix, we consider it as a folder
                if (isset($object['prefix'])) {
                    $folders[] = array(
                        'title' => $title,
                        'children' => array(),
                        'thumbnail'=> $OUTPUT->pix_url(file_folder_icon(90))->out(false),
                        'path' => $bucket . '/' . $object['prefix']
                    );
                } else {
                    $files[] = array(
                        'title' => $title,
                        'size' => $object['size'],
                        'datemodified' => $object['time'],
                        'source' => $bucket . '/' . $object['name'],
                        'thumbnail' => $OUTPUT->pix_url(file_extension_icon($title, 90))->out(false)
                    );
                }
            }
            $tree = array_merge($folders, $files);
        }

        $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);
            }
        }

        $list['list'] = $tree;

        return $list;
    }
Beispiel #9
0
    function print_user_files($userid=0, $return=false) {
        global $OUTPUT, $CFG, $USER;

        if (!$userid) {
            if (!isloggedin()) {
                return '';
            }
            $userid = $USER->id;
        }

        if (!$submission = $this->get_submission($userid)) {
            return '';
        }

        $link = new moodle_url("/mod/assignment/type/online/file.php?id={$this->cm->id}&userid={$submission->userid}");
        $action = new popup_action('click', $link, 'file'.$userid, array('height' => 450, 'width' => 580));
        $popup = $OUTPUT->action_link($link, get_string('popupinnewwindow','assignment'), $action, array('title'=>get_string('submission', 'assignment')));

        $output = '<div class="files">'.
                  $OUTPUT->pix_icon(file_extension_icon('.htm'), 'html', 'moodle', array('height' => 16, 'width' => 16)).
                  $popup .
                  '</div>';

        $wordcount = '<p id="wordcount">'. $popup . '&nbsp;';
    /// Decide what to count
        if ($CFG->assignment_itemstocount == ASSIGNMENT_COUNT_WORDS) {
            $wordcount .= '('.get_string('numwords', '', count_words(format_text($submission->data1, $submission->data2))).')';
        } else if ($CFG->assignment_itemstocount == ASSIGNMENT_COUNT_LETTERS) {
            $wordcount .= '('.get_string('numletters', '', count_letters(format_text($submission->data1, $submission->data2))).')';
        }
        $wordcount .= '</p>';

        $text = file_rewrite_pluginfile_urls($submission->data1, 'pluginfile.php', $this->context->id, 'mod_assignment', $this->filearea, $submission->id);
        return $wordcount . format_text($text, $submission->data2, array('overflowdiv'=>true));


        }
Beispiel #10
0
    /**
     * Get file listing
     *
     * @param string $encodedpath
     * @param string $path not used by this plugin
     * @return mixed
     */
    public function get_listing($encodedpath = '', $page = '') {
        global $OUTPUT;
        $ret = array();
        $ret['dynload'] = true;
        $ret['nosearch'] = true;
        $ret['nologin'] = true;
        $list = array();
        $files = $this->get_recent_files(0, $this->number);

        try {
            foreach ($files as $file) {
                $params = base64_encode(serialize($file));
                // Check that file exists and accessible
                $filesize = $this->get_file_size($params);
                if (!empty($filesize)) {
                    $node = array(
                        'title' => $file['filename'],
                        'size' => $filesize,
                        'date' => '',
                        'source'=> $params,
                        'thumbnail' => $OUTPUT->pix_url(file_extension_icon($file['filename'], 32))->out(false),
                    );
                    $list[] = $node;
                }
            }
        } catch (Exception $e) {
            throw new repository_exception('emptyfilelist', 'repository_recent');
        }
        $ret['list'] = array_filter($list, array($this, 'filter'));
        return $ret;
    }
Beispiel #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;
    }
Beispiel #12
0
/**
 * Return the relative icon path for a given file
 *
 * Usage:
 * <code>
 * // $file - instance of stored_file or file_info
 * $icon = $OUTPUT->pix_url(file_file_icon($file))->out();
 * echo html_writer::empty_tag('img', array('src' => $icon, 'alt' => get_mimetype_description($file)));
 * </code>
 * or
 * <code>
 * echo $OUTPUT->pix_icon(file_file_icon($file), get_mimetype_description($file));
 * </code>
 *
 * @param stored_file|file_info|stdClass|array $file (in case of object attributes $file->filename
 *     and $file->mimetype are expected)
 * @param int $size The size of the icon. Defaults to 16 can also be 24, 32, 64, 128, 256
 * @return string
 */
function file_file_icon($file, $size = null)
{
    if (!is_object($file)) {
        $file = (object) $file;
    }
    if (isset($file->filename)) {
        $filename = $file->filename;
    } else {
        if (method_exists($file, 'get_filename')) {
            $filename = $file->get_filename();
        } else {
            if (method_exists($file, 'get_visible_name')) {
                $filename = $file->get_visible_name();
            } else {
                $filename = '';
            }
        }
    }
    if (isset($file->mimetype)) {
        $mimetype = $file->mimetype;
    } else {
        if (method_exists($file, 'get_mimetype')) {
            $mimetype = $file->get_mimetype();
        } else {
            $mimetype = '';
        }
    }
    $mimetypes =& get_mimetypes_array();
    if ($filename) {
        $extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
        if ($extension && !empty($mimetypes[$extension])) {
            // if file name has known extension, return icon for this extension
            return file_extension_icon($filename, $size);
        }
    }
    return file_mimetype_icon($mimetype, $size);
}
Beispiel #13
0
 /**
  * Get a file list from alfresco
  *
  * @param string $uuid a unique id of directory in alfresco
  * @param string $path path to a directory
  * @return array
  */
 public function get_listing($uuid = '', $path = '')
 {
     global $CFG, $SESSION, $OUTPUT;
     $ret = array();
     $ret['dynload'] = true;
     $ret['list'] = array();
     $server_url = $this->options['alfresco_url'];
     $pattern = '#^(.*)api#';
     if ($return = preg_match($pattern, $server_url, $matches)) {
         $ret['manage'] = $matches[1] . 'faces/jsp/dashboards/container.jsp';
     }
     $ret['path'] = array(array('name' => get_string('pluginname', 'repository_alfresco'), 'path' => ''));
     try {
         if (empty($uuid)) {
             $this->current_node = $this->store->companyHome;
         } else {
             $this->current_node = $this->user_session->getNode($this->store, $uuid);
         }
         $folder_filter = "{http://www.alfresco.org/model/content/1.0}folder";
         $file_filter = "{http://www.alfresco.org/model/content/1.0}content";
         // top level sites folder
         $sites_filter = "{http://www.alfresco.org/model/site/1.0}sites";
         // individual site
         $site_filter = "{http://www.alfresco.org/model/site/1.0}site";
         foreach ($this->current_node->children as $child) {
             if ($child->child->type == $folder_filter or $child->child->type == $sites_filter or $child->child->type == $site_filter) {
                 $ret['list'][] = array('title' => $child->child->cm_name, 'path' => $child->child->id, 'thumbnail' => $OUTPUT->pix_url(file_folder_icon(90))->out(false), 'children' => array());
             } elseif ($child->child->type == $file_filter) {
                 $ret['list'][] = array('title' => $child->child->cm_name, 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($child->child->cm_name, 90))->out(false), 'source' => $child->child->id);
             }
         }
     } catch (Exception $e) {
         unset($SESSION->{$this->sessname});
         $ret = $this->print_login();
     }
     return $ret;
 }
Beispiel #14
0
/**
 * Optimised mimetype detection from general URL
 * @param $fullurl
 * @param int $size of the icon.
 * @return string|null mimetype or null when the filetype is not relevant.
 */
function url_guess_icon($fullurl, $size = null)
{
    global $CFG;
    require_once "{$CFG->libdir}/filelib.php";
    if (substr_count($fullurl, '/') < 3 or substr($fullurl, -1) === '/') {
        // Most probably default directory - index.php, index.html, etc. Return null because
        // we want to use the default module icon instead of the HTML file icon.
        return null;
    }
    $icon = file_extension_icon($fullurl, $size);
    $htmlicon = file_extension_icon('.htm', $size);
    $unknownicon = file_extension_icon('', $size);
    // We do not want to return those icon types, the module icon is more appropriate.
    if ($icon === $unknownicon || $icon === $htmlicon) {
        return null;
    }
    return $icon;
}
 public function get_listing($path = '', $page = '')
 {
     global $CFG, $OUTPUT;
     $list = array();
     $ret = array();
     $ret['dynload'] = true;
     $ret['nosearch'] = true;
     $ret['nologin'] = true;
     $ret['path'] = array(array('name' => 'Root', 'path' => 0));
     $ret['list'] = array();
     if (!$this->wd->open()) {
         return $ret;
     }
     if (empty($path)) {
         $path = '/';
         $dir = $this->wd->ls($path);
     } else {
         if (empty($this->options['webdav_type'])) {
             $partern = '#http://' . $this->webdav_host . '/#';
         } else {
             $partern = '#http://' . $this->webdav_type . $this->webdav_host . '/#';
         }
         $path = '/' . preg_replace($partern, '', $path);
         $dir = $this->wd->ls($path);
     }
     if (!is_array($dir)) {
         return $ret;
     }
     foreach ($dir as $v) {
         if (!empty($v['creationdate'])) {
             $ts = $this->wd->iso8601totime($v['creationdate']);
             $filedate = userdate($ts);
         } else {
             $filedate = '';
         }
         if (!empty($v['resourcetype']) && $v['resourcetype'] == 'collection') {
             // a folder
             if ($path != $v['href']) {
                 $matches = array();
                 preg_match('#(\\w+)$#i', $v['href'], $matches);
                 if (!empty($matches[1])) {
                     $title = urldecode($matches[1]);
                 } else {
                     $title = urldecode($v['href']);
                 }
                 $ret['list'][] = array('title' => $title, 'thumbnail' => $OUTPUT->old_icon_url('f/folder-32'), 'children' => array(), 'date' => $filedate, 'size' => 0, 'path' => $v['href']);
             }
         } else {
             // a file
             $title = urldecode(substr($v['href'], strpos($v['href'], $path) + strlen($path)));
             $size = !empty($v['getcontentlength']) ? $v['getcontentlength'] : '';
             $ret['list'][] = array('title' => $title, 'thumbnail' => $OUTPUT->old_icon_url(file_extension_icon($title, 32)), 'size' => $size, 'date' => $filedate, 'source' => $v['href']);
         }
     }
     return $ret;
 }
Beispiel #16
0
 /**
  * Search for images and return photos array.
  *
  * @param string $keyword
  * @param int $page
  * @param array $params additional query params
  * @return array
  */
 public function search_images($keyword, $page = 0, $params = array())
 {
     global $OUTPUT;
     $files_array = array();
     $this->_param['action'] = 'query';
     $this->_param['generator'] = 'search';
     $this->_param['gsrsearch'] = $keyword;
     $this->_param['gsrnamespace'] = WIKIMEDIA_FILE_NS;
     $this->_param['gsrlimit'] = WIKIMEDIA_THUMBS_PER_PAGE;
     $this->_param['gsroffset'] = $page * WIKIMEDIA_THUMBS_PER_PAGE;
     $this->_param['prop'] = 'imageinfo';
     $this->_param['iiprop'] = 'url|dimensions|mime|timestamp|size|user';
     $this->_param += $params;
     $this->_param += array('iiurlwidth' => WIKIMEDIA_IMAGE_SIDE_LENGTH, 'iiurlheight' => WIKIMEDIA_IMAGE_SIDE_LENGTH);
     //didn't work with POST
     $content = $this->_conn->get($this->api, $this->_param);
     $result = unserialize($content);
     if (!empty($result['query']['pages'])) {
         foreach ($result['query']['pages'] as $page) {
             $title = $page['title'];
             $file_type = $page['imageinfo'][0]['mime'];
             $image_types = array('image/jpeg', 'image/png', 'image/gif', 'image/svg+xml');
             if (in_array($file_type, $image_types)) {
                 //is image
                 $extension = pathinfo($title, PATHINFO_EXTENSION);
                 $issvg = strcmp($extension, 'svg') == 0;
                 // Get PNG equivalent to SVG files.
                 if ($issvg) {
                     $title .= '.png';
                 }
                 // The thumbnail (max size requested) is smaller than the original size, we will use the thumbnail.
                 if ($page['imageinfo'][0]['thumbwidth'] < $page['imageinfo'][0]['width']) {
                     $attrs = array('source' => $page['imageinfo'][0]['thumburl'], 'image_width' => $page['imageinfo'][0]['thumbwidth'], 'image_height' => $page['imageinfo'][0]['thumbheight']);
                     if ($attrs['image_width'] <= WIKIMEDIA_THUMB_SIZE && $attrs['image_height'] <= WIKIMEDIA_THUMB_SIZE) {
                         $attrs['realthumbnail'] = $attrs['source'];
                     }
                     if ($attrs['image_width'] <= 24 && $attrs['image_height'] <= 24) {
                         $attrs['realicon'] = $attrs['source'];
                     }
                     // We use the original file.
                 } else {
                     $attrs = array('image_width' => $page['imageinfo'][0]['width'], 'image_height' => $page['imageinfo'][0]['height'], 'size' => $page['imageinfo'][0]['size']);
                     // We cannot use the source when the file is SVG.
                     if ($issvg) {
                         // So we generate a PNG thumbnail of the file at its original size.
                         $attrs['source'] = $this->get_thumb_url($page['imageinfo'][0]['url'], $page['imageinfo'][0]['width'], $page['imageinfo'][0]['height'], $page['imageinfo'][0]['width'], true);
                     } else {
                         $attrs['source'] = $page['imageinfo'][0]['url'];
                     }
                 }
                 $attrs += array('realthumbnail' => $this->get_thumb_url($page['imageinfo'][0]['url'], $page['imageinfo'][0]['width'], $page['imageinfo'][0]['height'], WIKIMEDIA_THUMB_SIZE), 'realicon' => $this->get_thumb_url($page['imageinfo'][0]['url'], $page['imageinfo'][0]['width'], $page['imageinfo'][0]['height'], 24), 'author' => $page['imageinfo'][0]['user'], 'datemodified' => strtotime($page['imageinfo'][0]['timestamp']));
             } else {
                 // other file types
                 $attrs = array('source' => $page['imageinfo'][0]['url']);
             }
             $files_array[] = array('title' => substr($title, 5), 'thumbnail' => $OUTPUT->pix_url(file_extension_icon(substr($title, 5), WIKIMEDIA_THUMB_SIZE))->out(false), 'thumbnail_width' => WIKIMEDIA_THUMB_SIZE, 'thumbnail_height' => WIKIMEDIA_THUMB_SIZE, 'license' => 'cc-sa', 'url' => $page['imageinfo'][0]['descriptionurl']) + $attrs;
         }
     }
     return $files_array;
 }
 public function search($search_text, $page = 1)
 {
     global $OUTPUT;
     $ret = array();
     $list = array();
     $ret['nologin'] = true;
     if (empty($page)) {
         $page = 1;
     }
     // Pagination not working
     //$searchlist = WebService::standardSearch($this->repositoryClient,'2','2',$search_text,1+($page-1)*repository_hive::$resultsPerPage,repository_hive::$resultsPerPage);
     $searchlist = WebService::standardSearch($this->id, $search_text, -1, -1);
     $searchArray = $searchlist['searchResults']['list'];
     foreach ($searchArray as $value) {
         $list[] = array('shorttitle' => $value['title'], 'title' => $value['filename'], 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($value['filename'], 32))->out(false), 'source' => $value['bureauId'] . '|' . $value['aliasId'] . '|' . urlencode($value['filename']));
     }
     // Pagination
     // NOT WORKING
     //$ret['pages'] = ceil($searchlist['totalNumberOfResults']/repository_hive::$resultsPerPage);
     $ret['list'] = $list;
     return $ret;
 }
Beispiel #18
0
 /**
  * Search files from box.net
  *
  * @param string $search_text
  * @return mixed
  */
 public function search($search_text, $page = 0) {
     global $OUTPUT;
     $list = array();
     $ret  = array();
     $tree = $this->boxclient->getAccountTree();
     if (!empty($tree)) {
         $filenames = $tree['file_name'];
         $fileids   = $tree['file_id'];
         $filesizes = $tree['file_size'];
         $filedates = $tree['file_date'];
         $fileicon  = $tree['thumbnail'];
         foreach ($filenames as $n=>$v){
             if(strstr(strtolower($v), strtolower($search_text)) !== false) {
                 $list[] = array('title'=>$v,
                         'size'=>$filesizes[$n],
                         'date'=>$filedates[$n],
                         'source'=>'https://www.box.com/api/1.0/download/'
                             .$this->auth_token.'/'.$fileids[$n],
                         'thumbnail' => $OUTPUT->pix_url(file_extension_icon($v, 90))->out(false));
             }
         }
     }
     $ret['list'] = array_filter($list, array($this, 'filter'));
     return $ret;
 }
Beispiel #19
0
 public function get_listing($path = '', $page = '') {
     global $CFG, $OUTPUT;
     $list = array();
     $list['list'] = array();
     // process breacrumb trail
     $list['path'] = array(
         array('name'=>get_string('root', 'repository_filesystem'), '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);
         }
         $this->root_path .= ($path.'/');
     }
     $list['manage'] = false;
     $list['dynload'] = true;
     $list['nologin'] = true;
     $list['nosearch'] = true;
     // retrieve list of files and directories and sort them
     $fileslist = array();
     $dirslist = array();
     if ($dh = opendir($this->root_path)) {
         while (($file = readdir($dh)) != false) {
             if ( $file != '.' and $file !='..') {
                 if (is_file($this->root_path.$file)) {
                     $fileslist[] = $file;
                 } else {
                     $dirslist[] = $file;
                 }
             }
         }
     }
     collatorlib::asort($fileslist, collatorlib::SORT_STRING);
     collatorlib::asort($dirslist, collatorlib::SORT_STRING);
     // fill the $list['list']
     foreach ($dirslist as $file) {
         if (!empty($path)) {
             $current_path = $path . '/'. $file;
         } else {
             $current_path = $file;
         }
         $list['list'][] = array(
             'title' => $file,
             'children' => array(),
             'datecreated' => filectime($this->root_path.$file),
             'datemodified' => filemtime($this->root_path.$file),
             'thumbnail' => $OUTPUT->pix_url(file_folder_icon(90))->out(false),
             'path' => $current_path
             );
     }
     foreach ($fileslist as $file) {
         $list['list'][] = array(
             'title' => $file,
             'source' => $path.'/'.$file,
             'size' => filesize($this->root_path.$file),
             'datecreated' => filectime($this->root_path.$file),
             'datemodified' => filemtime($this->root_path.$file),
             'thumbnail' => $OUTPUT->pix_url(file_extension_icon($file, 90))->out(false),
             'icon' => $OUTPUT->pix_url(file_extension_icon($file, 24))->out(false)
         );
     }
     $list['list'] = array_filter($list['list'], array($this, 'filter'));
     return $list;
 }
Beispiel #20
0
 /**
  * Get file listing
  *
  * @param string $path
  * @param string $page
  * @return mixed
  */
 public function get_listing($fullpath = '', $page = '')
 {
     global $OUTPUT;
     $ret = array();
     $ret['list'] = array();
     $ret['manage'] = self::MANAGE_URL;
     $ret['dynload'] = true;
     $crumbs = explode('/', $fullpath);
     $path = array_pop($crumbs);
     if (empty($path)) {
         $type = 'folder';
         $pathid = 0;
         $pathname = get_string('pluginname', 'repository_boxnet');
     } else {
         list($type, $pathid, $pathname) = $this->split_part($path);
     }
     $ret['path'] = $this->build_breadcrumb($fullpath);
     $folders = array();
     $files = array();
     if ($type == 'search') {
         $result = $this->boxnetclient->search($pathname);
     } else {
         $result = $this->boxnetclient->get_folder_items($pathid);
     }
     foreach ($result->entries as $item) {
         if ($item->type == 'folder') {
             $folders[$item->name . ':' . $item->id] = array('title' => $item->name, 'path' => $fullpath . '/' . $this->build_part('folder', $item->id, $item->name), 'date' => strtotime($item->modified_at), 'thumbnail' => $OUTPUT->pix_url(file_folder_icon(64))->out(false), 'thumbnail_height' => 64, 'thumbnail_width' => 64, 'children' => array(), 'size' => $item->size);
         } else {
             $files[$item->name . ':' . $item->id] = array('title' => $item->name, 'source' => $this->build_part('file', $item->id, $item->name), 'size' => $item->size, 'date' => strtotime($item->modified_at), 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($item->name, 64))->out(false), 'thumbnail_height' => 64, 'thumbnail_width' => 64, 'author' => $item->owned_by->name);
         }
     }
     collatorlib::ksort($folders, core_collator::SORT_NATURAL);
     collatorlib::ksort($files, core_collator::SORT_NATURAL);
     $ret['list'] = array_merge($folders, $files);
     $ret['list'] = array_filter($ret['list'], array($this, 'filter'));
     return $ret;
 }
Beispiel #21
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();
        $component = 'course';
        $filearea  = 'legacy';
        $itemid = 0;

        $browser = get_file_browser();

        if (!empty($encodedpath)) {
            $params = unserialize(base64_decode($encodedpath));
            if (is_array($params)) {
                $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 {
            $filename = null;
            $filepath = null;
            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);
        }

        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) {
                $params = $level->get_params();
                $encodedpath = base64_encode(serialize($params));
                if ($params['contextid'] != $context->id) {
                    break;
                }
                $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()) {
                    $params = $child->get_params();
                    $subdir_children = $child->get_children();
                    $encodedpath = base64_encode(serialize($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;
                }
            }
        } else {
            $list = array();
        }
        $ret['list'] = array_filter($list, array($this, 'filter'));
        return $ret;
    }
Beispiel #22
0
/**
 * Optimised mimetype detection from general URL
 * @param $fullurl
 * @return string mimetype
 */
function url_guess_icon($fullurl) {
    global $CFG;
    require_once("$CFG->libdir/filelib.php");

    if (substr_count($fullurl, '/') < 3 or substr($fullurl, -1) === '/') {
        // most probably default directory - index.php, index.html, etc.
        return file_extension_icon('.htm');
    }

    $icon = file_extension_icon($fullurl);

    if ($icon === file_extension_icon('')) {
        return file_extension_icon('.htm');
    }

    return $icon;
}
Beispiel #23
0
/**
 * Given a course_module object, this function returns any
 * "extra" information that may be needed when printing
 * this activity in a course listing.
 *
 * See {@link get_array_of_activities()} in course/lib.php
 *
 * @param object $coursemodule
 * @return object info
 */
function resource_get_coursemodule_info($coursemodule) {
    global $CFG, $DB;
    require_once("$CFG->libdir/filelib.php");
    require_once("$CFG->dirroot/mod/resource/locallib.php");
    require_once($CFG->libdir.'/completionlib.php');

    $context = get_context_instance(CONTEXT_MODULE, $coursemodule->id);

    if (!$resource = $DB->get_record('resource', array('id'=>$coursemodule->instance), 'id, name, display, displayoptions, tobemigrated, revision')) {
        return NULL;
    }

    $info = new stdClass();
    $info->name = $resource->name;

    if ($resource->tobemigrated) {
        $info->icon ='i/cross_red_big';
        return $info;
    }
    $fs = get_file_storage();
    $files = $fs->get_area_files($context->id, 'mod_resource', 'content', 0, 'sortorder DESC, id ASC', false); // TODO: this is not very efficient!!
    if (count($files) >= 1) {
        $mainfile = array_pop($files);
        $info->icon = file_extension_icon($mainfile->get_filename());
        $resource->mainfile = $mainfile->get_filename();
    }

    $display = resource_get_final_display_type($resource);

    if ($display == RESOURCELIB_DISPLAY_POPUP) {
        $fullurl = "$CFG->wwwroot/mod/resource/view.php?id=$coursemodule->id&amp;redirect=1";
        $options = empty($resource->displayoptions) ? array() : unserialize($resource->displayoptions);
        $width  = empty($options['popupwidth'])  ? 620 : $options['popupwidth'];
        $height = empty($options['popupheight']) ? 450 : $options['popupheight'];
        $wh = "width=$width,height=$height,toolbar=no,location=no,menubar=no,copyhistory=no,status=no,directories=no,scrollbars=yes,resizable=yes";
        $info->extra = "onclick=\"window.open('$fullurl', '', '$wh'); return false;\"";

    } else if ($display == RESOURCELIB_DISPLAY_NEW) {
        $fullurl = "$CFG->wwwroot/mod/resource/view.php?id=$coursemodule->id&amp;redirect=1";
        $info->extra = "onclick=\"window.open('$fullurl'); return false;\"";

    } else if ($display == RESOURCELIB_DISPLAY_OPEN) {
        $fullurl = "$CFG->wwwroot/mod/resource/view.php?id=$coursemodule->id&amp;redirect=1";
        $info->extra = "onclick=\"window.location.href ='$fullurl';return false;\"";

    } else if ($display == RESOURCELIB_DISPLAY_DOWNLOAD) {
        if (empty($mainfile)) {
            return NULL;
        }
        // do not open any window because it would be left there after download
        $path = '/'.$context->id.'/mod_resource/content/'.$resource->revision.$mainfile->get_filepath().$mainfile->get_filename();
        $fullurl = addslashes_js(file_encode_url($CFG->wwwroot.'/pluginfile.php', $path, true));

        // When completion information is enabled for download files, make
        // the JavaScript version go to the view page with redirect set,
        // instead of directly to the file, otherwise we can't make it tick
        // the box for them
        if (!$course = $DB->get_record('course', array('id'=>$coursemodule->course), 'id, enablecompletion')) {
            return NULL;
        }
        $completion = new completion_info($course);
        if ($completion->is_enabled($coursemodule) == COMPLETION_TRACKING_AUTOMATIC) {
            $fullurl = "$CFG->wwwroot/mod/resource/view.php?id=$coursemodule->id&amp;redirect=1";
        }
        $info->extra = "onclick=\"window.open('$fullurl'); return false;\"";
    }

    return $info;
}
 /**
  * Generate thumbnail URL from image URL.
  *
  * @param string $image_url
  * @param int $orig_width
  * @param int $orig_height
  * @param int $thumb_width
  * @global object OUTPUT
  * @return string
  */
 public function get_thumb_url($image_url, $orig_width, $orig_height, $thumb_width = 75)
 {
     global $OUTPUT;
     if ($orig_width <= $thumb_width and $orig_height <= $thumb_width) {
         return $image_url;
     } else {
         $thumb_url = '';
         $commons_main_dir = 'http://wikieducator.org/';
         if ($image_url) {
             $short_path = str_replace($commons_main_dir, '', $image_url);
             $extension = pathinfo($short_path, PATHINFO_EXTENSION);
             if (strcmp($extension, 'gif') == 0) {
                 //no thumb for gifs
                 return $OUTPUT->pix_url(file_extension_icon('xx.jpg', 32));
             }
             $dir_parts = explode('/', $short_path);
             $file_name = end($dir_parts);
             if ($orig_height > $orig_width) {
                 $thumb_width = round($thumb_width * $orig_width / $orig_height);
             }
             //$dir_parts = array_shift($dir_parts);
             //$thumb_url = $commons_main_dir . 'images/thumb/' . implode('/', $dir_parts) . '/'. $thumb_width .'px-' . $file_name;
             $thumb_url = str_replace('/images', '/images/thumb', implode('/', $dir_parts)) . '/' . $thumb_width . 'px-' . $file_name;
             //$thumb_url =  implode('/', $dir_parts); // full image
             if (strcmp($extension, 'svg') == 0) {
                 //png thumb for svg-s
                 $thumb_url .= '.png';
             }
         }
         return $thumb_url;
     }
 }
 /**
  * Returns a list of files the user has formated for files api
  *
  * @param string $path the path which we are in
  * @return mixed Array of files formated for fileapoi
  */
 public function get_file_list($path = '')
 {
     global $OUTPUT;
     if (empty($path)) {
         $url = self::API . "/me/skydrive/files/";
     } else {
         $url = self::API . "/{$path}/files/";
     }
     $ret = json_decode($this->get($url));
     if (isset($ret->error)) {
         $this->log_out();
         return false;
     }
     $files = array();
     foreach ($ret->data as $file) {
         switch ($file->type) {
             case 'folder':
                 $files[] = array('title' => $file->name, 'path' => $file->id, 'size' => 0, 'date' => strtotime($file->updated_time), 'thumbnail' => $OUTPUT->pix_url(file_folder_icon(90))->out(false), 'children' => array());
                 break;
             case 'photo':
                 $files[] = array('title' => $file->name, 'size' => $file->size, 'date' => strtotime($file->updated_time), 'thumbnail' => $file->picture, 'source' => $file->id, 'url' => $file->link);
                 break;
             case 'video':
                 $files[] = array('title' => $file->name, 'size' => $file->size, 'date' => strtotime($file->updated_time), 'thumbnail' => $file->picture, 'source' => $file->id, 'url' => $file->link);
                 break;
             case 'audio':
                 $files[] = array('title' => $file->name, 'size' => $file->size, 'date' => strtotime($file->updated_time), 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($file->name, 90))->out(false), 'source' => $file->id, 'url' => $file->link);
                 break;
             case 'file':
                 $files[] = array('title' => $file->name, 'size' => $file->size, 'date' => strtotime($file->updated_time), 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($file->name, 90))->out(false), 'source' => $file->id, 'url' => $file->link);
                 break;
         }
     }
     return $files;
 }
Beispiel #26
0
    /**
     * Builds a tree of files This function is
     * then called recursively.
     *
     * @param $fileinfo an object returned by file_browser::get_file_info()
     * @param $search searched string
     * @param $dynamicmode bool no recursive call is done when in dynamic mode
     * @param $list - the array containing the files under the passed $fileinfo
     * @returns int the number of files found
     *
     * todo: take $search into account, and respect a threshold for dynamic loading
     */
    public static function build_tree($fileinfo, $search, $dynamicmode, &$list) {
        global $CFG, $OUTPUT;

        $filecount = 0;
        $children = $fileinfo->get_children();

        foreach ($children as $child) {
            $filename = $child->get_visible_name();
            $filesize = $child->get_filesize();
            $filesize = $filesize ? display_size($filesize) : '';
            $filedate = $child->get_timemodified();
            $filedate = $filedate ? userdate($filedate) : '';
            $filetype = $child->get_mimetype();

            if ($child->is_directory()) {
                $path = array();
                $level = $child->get_parent();
                while ($level) {
                    $params = $level->get_params();
                    $path[] = array($params['filepath'], $level->get_visible_name());
                    $level = $level->get_parent();
                }

                $tmp = array(
                    'title' => $child->get_visible_name(),
                    'size' => 0,
                    'date' => $filedate,
                    'path' => array_reverse($path),
                    'thumbnail' => $OUTPUT->pix_url('f/folder-32')
                );

                //if ($dynamicmode && $child->is_writable()) {
                //    $tmp['children'] = array();
                //} else {
                    // if folder name matches search, we send back all files contained.
                $_search = $search;
                if ($search && stristr($tmp['title'], $search) !== false) {
                    $_search = false;
                }
                $tmp['children'] = array();
                $_filecount = repository::build_tree($child, $_search, $dynamicmode, $tmp['children']);
                if ($search && $_filecount) {
                    $tmp['expanded'] = 1;
                }

                //}

                if (!$search || $_filecount || (stristr($tmp['title'], $search) !== false)) {
                    $filecount += $_filecount;
                    $list[] = $tmp;
                }

            } else { // not a directory
                // skip the file, if we're in search mode and it's not a match
                if ($search && (stristr($filename, $search) === false)) {
                    continue;
                }
                $params = $child->get_params();
                $source = serialize(array($params['contextid'], $params['component'], $params['filearea'], $params['itemid'], $params['filepath'], $params['filename']));
                $list[] = array(
                    'title' => $filename,
                    'size' => $filesize,
                    'date' => $filedate,
                    //'source' => $child->get_url(),
                    'source' => base64_encode($source),
                    'thumbnail'=>$OUTPUT->pix_url(file_extension_icon($filename, 32)),
                );
                $filecount++;
            }
        }

        return $filecount;
    }
Beispiel #27
0
 /**
  * Prepare the file/folder listing.
  *
  * @param array $list of files and folders.
  * @return array of files and folders.
  * @since Moodle 2.3.3
  */
 protected static function prepare_list($list)
 {
     global $OUTPUT;
     $foldericon = $OUTPUT->pix_url(file_folder_icon(24))->out(false);
     // Reset the array keys because non-numeric keys will create an object when converted to JSON.
     $list = array_values($list);
     $len = count($list);
     for ($i = 0; $i < $len; $i++) {
         if (is_object($list[$i])) {
             $file = (array) $list[$i];
             $converttoobject = true;
         } else {
             $file =& $list[$i];
             $converttoobject = false;
         }
         if (isset($file['size'])) {
             $file['size'] = (int) $file['size'];
             $file['size_f'] = display_size($file['size']);
         }
         if (isset($file['license']) && get_string_manager()->string_exists($file['license'], 'license')) {
             $file['license_f'] = get_string($file['license'], 'license');
         }
         if (isset($file['image_width']) && isset($file['image_height'])) {
             $a = array('width' => $file['image_width'], 'height' => $file['image_height']);
             $file['dimensions'] = get_string('imagesize', 'repository', (object) $a);
         }
         foreach (array('date', 'datemodified', 'datecreated') as $key) {
             if (!isset($file[$key]) && isset($file['date'])) {
                 $file[$key] = $file['date'];
             }
             if (isset($file[$key])) {
                 // must be UNIX timestamp
                 $file[$key] = (int) $file[$key];
                 if (!$file[$key]) {
                     unset($file[$key]);
                 } else {
                     $file[$key . '_f'] = userdate($file[$key], get_string('strftimedatetime', 'langconfig'));
                     $file[$key . '_f_s'] = userdate($file[$key], get_string('strftimedatetimeshort', 'langconfig'));
                 }
             }
         }
         $isfolder = array_key_exists('children', $file) || isset($file['type']) && $file['type'] == 'folder';
         $filename = null;
         if (isset($file['title'])) {
             $filename = $file['title'];
         } else {
             if (isset($file['fullname'])) {
                 $filename = $file['fullname'];
             }
         }
         if (!isset($file['mimetype']) && !$isfolder && $filename) {
             $file['mimetype'] = get_mimetype_description(array('filename' => $filename));
         }
         if (!isset($file['icon'])) {
             if ($isfolder) {
                 $file['icon'] = $foldericon;
             } else {
                 if ($filename) {
                     $file['icon'] = $OUTPUT->pix_url(file_extension_icon($filename, 24))->out(false);
                 }
             }
         }
         // Recursively loop over children.
         if (isset($file['children'])) {
             $file['children'] = self::prepare_list($file['children']);
         }
         // Convert the array back to an object.
         if ($converttoobject) {
             $list[$i] = (object) $file;
         }
     }
     return $list;
 }
Beispiel #28
0
    public function get_listing($path='', $page = '') {
        global $CFG, $OUTPUT;
        $list = array();
        $ret  = array();
        $ret['dynload'] = true;
        $ret['nosearch'] = true;
        $ret['nologin'] = true;
        $ret['path'] = array(array('name'=>get_string('webdav', 'repository_webdav'), 'path'=>''));
        $ret['list'] = array();
        if (!$this->dav->open()) {
            return $ret;
        }
        $webdavpath = rtrim('/'.ltrim($this->options['webdav_path'], '/ '), '/ '); // without slash in the end
        if (empty($path) || $path =='/') {
            $path = '/';
        } else {
            $chunks = preg_split('|/|', trim($path, '/'));
            for ($i = 0; $i < count($chunks); $i++) {
                $ret['path'][] = array(
                    'name' => urldecode($chunks[$i]),
                    'path' => '/'. join('/', array_slice($chunks, 0, $i+1)). '/'
                );
            }
        }
        $dir = $this->dav->ls($webdavpath. urldecode($path));
        if (!is_array($dir)) {
            return $ret;
        }
        $folders = array();
        $files = array();
        foreach ($dir as $v) {
            if (!empty($v['lastmodified'])) {
                $v['lastmodified'] = strtotime($v['lastmodified']);
            } else {
                $v['lastmodified'] = null;
            }

            // Extracting object title from absolute path
            $v['href'] = substr(urldecode($v['href']), strlen($webdavpath));
            $title = substr($v['href'], strlen($path));

            if (!empty($v['resourcetype']) && $v['resourcetype'] == 'collection') {
                // a folder
                if ($path != $v['href']) {
                    $folders[strtoupper($title)] = array(
                        'title'=>rtrim($title, '/'),
                        'thumbnail'=>$OUTPUT->pix_url(file_folder_icon(90))->out(false),
                        'children'=>array(),
                        'datemodified'=>$v['lastmodified'],
                        'path'=>$v['href']
                    );
                }
            }else{
                // a file
                $size = !empty($v['getcontentlength'])? $v['getcontentlength']:'';
                $files[strtoupper($title)] = array(
                    'title'=>$title,
                    'thumbnail' => $OUTPUT->pix_url(file_extension_icon($title, 90))->out(false),
                    'size'=>$size,
                    'datemodified'=>$v['lastmodified'],
                    'source'=>$v['href']
                );
            }
        }
        ksort($files);
        ksort($folders);
        $ret['list'] = array_merge($folders, $files);
        return $ret;
    }
Beispiel #29
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;
 }
Beispiel #30
0
 /**
  * Returns a list of files the user has formated for files api
  *
  * @param string $search A search string to do full text search on the documents
  * @return mixed Array of files formated for fileapoi
  */
 public function get_file_list($search = '')
 {
     global $CFG, $OUTPUT;
     $url = self::DOCUMENTFEED_URL;
     if ($search) {
         $url .= '?q=' . urlencode($search);
     }
     $files = array();
     $content = $this->googleoauth->get($url);
     try {
         if (strpos($content, '<?xml') !== 0) {
             throw new moodle_exception('invalidxmlresponse');
         }
         $xml = new SimpleXMLElement($content);
     } catch (Exception $e) {
         // An error occured while trying to parse the XML, let's just return nothing. SimpleXML does not
         // return a more specific Exception, that's why the global Exception class is caught here.
         return $files;
     }
     date_default_timezone_set(core_date::get_user_timezone());
     foreach ($xml->entry as $gdoc) {
         $docid = (string) $gdoc->children('http://schemas.google.com/g/2005')->resourceId;
         list($type, $docid) = explode(':', $docid);
         $title = '';
         $source = '';
         // FIXME: We're making hard-coded choices about format here.
         // If the repo api can support it, we could let the user
         // chose.
         switch ($type) {
             case 'document':
                 $title = $gdoc->title . '.rtf';
                 $source = 'https://docs.google.com/feeds/download/documents/Export?id=' . $docid . '&exportFormat=rtf';
                 break;
             case 'presentation':
                 $title = $gdoc->title . '.ppt';
                 $source = 'https://docs.google.com/feeds/download/presentations/Export?id=' . $docid . '&exportFormat=ppt';
                 break;
             case 'spreadsheet':
                 $title = $gdoc->title . '.xls';
                 $source = 'https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=' . $docid . '&exportFormat=xls';
                 break;
             case 'pdf':
             case 'file':
                 $title = (string) $gdoc->title;
                 // Some files don't have a content probably because the download has been restricted.
                 if (isset($gdoc->content)) {
                     $source = (string) $gdoc->content[0]->attributes()->src;
                 }
                 break;
         }
         $files[] = array('title' => $title, 'url' => "{$gdoc->link[0]->attributes()->href}", 'source' => $source, 'date' => strtotime($gdoc->updated), 'thumbnail' => (string) $OUTPUT->pix_url(file_extension_icon($title, 32)));
     }
     core_date::set_default_server_timezone();
     return $files;
 }