3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * This plugin is used to access s3 files
22 * @package repository_s3
23 * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require_once($CFG->dirroot
. '/repository/lib.php');
27 require_once($CFG->dirroot
. '/repository/s3/S3.php');
30 * This is a repository class used to browse Amazon S3 content.
33 * @package repository_s3
34 * @copyright 2009 Dongsheng Cai {@link http://dongsheng.org}
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class repository_s3
extends repository
{
41 * @param int $repositoryid
42 * @param object $context
43 * @param array $options
45 public function __construct($repositoryid, $context = SYSCONTEXTID
, $options = array()) {
46 parent
::__construct($repositoryid, $context, $options);
47 $this->access_key
= get_config('s3', 'access_key');
48 $this->secret_key
= get_config('s3', 'secret_key');
49 $this->s
= new S3($this->access_key
, $this->secret_key
);
50 $this->s
->setExceptions(true);
54 * Extracts the Bucket and URI from the path
56 * @param string $path path in this format 'bucket/path/to/folder/and/file'
57 * @return array including bucket and uri
59 protected function explode_path($path) {
60 $parts = explode('/', $path, 2);
61 if (isset($parts[1]) && $parts[1] !== '') {
62 list($bucket, $uri) = $parts;
67 return array($bucket, $uri);
74 * @return array The file list and options
76 public function get_listing($path = '', $page = '') {
78 if (empty($this->access_key
)) {
79 throw new moodle_exception('needaccesskey', 'repository_s3');
83 $list['list'] = array();
84 $list['path'] = array(
85 array('name' => get_string('pluginname', 'repository_s3'), 'path' => '')
88 // the management interface url
89 $list['manage'] = false;
90 // dynamically loading
91 $list['dynload'] = true;
92 // the current path of this list.
93 // set to true, the login link will be removed
94 $list['nologin'] = true;
95 // set to true, the search button will be removed
96 $list['nosearch'] = true;
102 $buckets = $this->s
->listBuckets();
103 } catch (S3Exception
$e) {
104 throw new moodle_exception('errorwhilecommunicatingwith', 'repository', '', $this->get_name());
106 foreach ($buckets as $bucket) {
109 'children' => array(),
110 'thumbnail' => $OUTPUT->pix_url(file_folder_icon(90))->out(false),
118 list($bucket, $uri) = $this->explode_path($path);
121 $contents = $this->s
->getBucket($bucket, $uri, null, null, '/', true);
122 } catch (S3Exception
$e) {
123 throw new moodle_exception('errorwhilecommunicatingwith', 'repository', '', $this->get_name());
125 foreach ($contents as $object) {
127 // If object has a prefix, it is a 'CommonPrefix', which we consider a folder
128 if (isset($object['prefix'])) {
129 $title = rtrim($object['prefix'], '/');
131 $title = $object['name'];
134 // Removes the prefix (folder path) from the title
135 if (strlen($uri) > 0) {
136 $title = substr($title, strlen($uri));
137 // Check if title is empty and not zero
138 if (empty($title) && !is_numeric($title)) {
139 // Amazon returns the prefix itself, we skip it
144 // This is a so-called CommonPrefix, we consider it as a folder
145 if (isset($object['prefix'])) {
148 'children' => array(),
149 'thumbnail'=> $OUTPUT->pix_url(file_folder_icon(90))->out(false),
150 'path' => $bucket . '/' . $object['prefix']
155 'size' => $object['size'],
156 'datemodified' => $object['time'],
157 'source' => $bucket . '/' . $object['name'],
158 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($title, 90))->out(false)
162 $tree = array_merge($folders, $files);
167 $parts = explode('/', $path);
168 if (count($parts) > 1) {
169 foreach ($parts as $part) {
171 $trail .= $part . '/';
172 $list['path'][] = array('name' => $part, 'path' => $trail);
176 $list['path'][] = array('name' => $path, 'path' => $path);
180 $list['list'] = $tree;
186 * Download S3 files to moodle
188 * @param string $filepath
189 * @param string $file The file path in moodle
190 * @return array The local stored path
192 public function get_file($filepath, $file = '') {
193 list($bucket, $uri) = $this->explode_path($filepath);
194 $path = $this->prepare_file($file);
196 $this->s
->getObject($bucket, $uri, $path);
197 } catch (S3Exception
$e) {
198 throw new moodle_exception('errorwhilecommunicatingwith', 'repository', '', $this->get_name());
200 return array('path' => $path);
204 * Return the source information
206 * @param stdClass $filepath
209 public function get_file_source_info($filepath) {
210 return 'Amazon S3: ' . $filepath;
214 * S3 doesn't require login
218 public function check_login() {
223 * S3 doesn't provide search
227 public function global_search() {
231 public static function get_type_option_names() {
232 return array('access_key', 'secret_key', 'pluginname');
235 public static function type_config_form($mform, $classname = 'repository') {
236 parent
::type_config_form($mform);
237 $strrequired = get_string('required');
238 $mform->addElement('text', 'access_key', get_string('access_key', 'repository_s3'));
239 $mform->setType('access_key', PARAM_RAW_TRIMMED
);
240 $mform->addElement('text', 'secret_key', get_string('secret_key', 'repository_s3'));
241 $mform->setType('secret_key', PARAM_RAW_TRIMMED
);
242 $mform->addRule('access_key', $strrequired, 'required', null, 'client');
243 $mform->addRule('secret_key', $strrequired, 'required', null, 'client');
247 * S3 plugins doesn't support return links of files
251 public function supported_returntypes() {
252 return FILE_INTERNAL
;
256 * Is this repository accessing private data?
260 public function contains_private_data() {