Merge branch 'install_22_STABLE' of git://github.com/amosbot/moodle into MOODLE_22_STABLE
[moodle.git] / repository / s3 / lib.php
blob0aa03fea8ad459dab54b7727aef61fc5bf231630
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
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.
9 //
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 /**
20 * This is a repository class used to browse Amazon S3 content.
22 * @since 2.0
23 * @package repository
24 * @subpackage s3
25 * @copyright 2009 Dongsheng Cai
26 * @author Dongsheng Cai <dongsheng@moodle.com>
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 require_once('S3.php');
32 class repository_s3 extends repository {
34 /**
35 * Constructor
36 * @param int $repositoryid
37 * @param object $context
38 * @param array $options
40 public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
41 parent::__construct($repositoryid, $context, $options);
42 $this->access_key = get_config('s3', 'access_key');
43 $this->secret_key = get_config('s3', 'secret_key');
44 $this->s = new S3($this->access_key, $this->secret_key);
47 /**
48 * Get S3 file list
50 * @param string $path
51 * @return array The file list and options
53 public function get_listing($path = '') {
54 global $CFG, $OUTPUT;
55 if (empty($this->access_key)) {
56 die(json_encode(array('e'=>get_string('needaccesskey', 'repository_s3'))));
58 $list = array();
59 $list['list'] = array();
60 // the management interface url
61 $list['manage'] = false;
62 // dynamically loading
63 $list['dynload'] = true;
64 // the current path of this list.
65 // set to true, the login link will be removed
66 $list['nologin'] = true;
67 // set to true, the search button will be removed
68 $list['nosearch'] = true;
69 $tree = array();
70 if (empty($path)) {
71 $buckets = $this->s->listBuckets();
72 foreach ($buckets as $bucket) {
73 $folder = array(
74 'title' => $bucket,
75 'children' => array(),
76 'thumbnail'=>$OUTPUT->pix_url('f/folder-32')->out(false),
77 'path'=>$bucket
79 $tree[] = $folder;
81 } else {
82 $contents = $this->s->getBucket($path);
83 foreach ($contents as $file) {
84 $info = $this->s->getObjectInfo($path, baseName($file['name']));
85 $tree[] = array(
86 'title'=>$file['name'],
87 'size'=>$file['size'],
88 'date'=>userdate($file['time']),
89 'source'=>$path.'/'.$file['name'],
90 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($file['name'], 32))->out(false)
95 $list['list'] = $tree;
97 return $list;
101 * Download S3 files to moodle
103 * @param string $filepath
104 * @param string $file The file path in moodle
105 * @return array The local stored path
107 public function get_file($filepath, $file) {
108 global $CFG;
109 $arr = explode('/', $filepath);
110 $bucket = $arr[0];
111 $filename = $arr[1];
112 $path = $this->prepare_file($file);
113 $this->s->getObject($bucket, $filename, $path);
114 return array('path'=>$path);
118 * S3 doesn't require login
120 * @return bool
122 public function check_login() {
123 return true;
127 * S3 doesn't provide search
129 * @return bool
131 public function global_search() {
132 return false;
135 public static function get_type_option_names() {
136 return array('access_key', 'secret_key', 'pluginname');
139 public function type_config_form($mform) {
140 parent::type_config_form($mform);
141 $strrequired = get_string('required');
142 $mform->addElement('text', 'access_key', get_string('access_key', 'repository_s3'));
143 $mform->addElement('text', 'secret_key', get_string('secret_key', 'repository_s3'));
144 $mform->addRule('access_key', $strrequired, 'required', null, 'client');
145 $mform->addRule('secret_key', $strrequired, 'required', null, 'client');
149 * S3 plugins doesn't support return links of files
151 * @return int
153 public function supported_returntypes() {
154 return FILE_INTERNAL;