|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00018 00030 require_once('S3.php'); 00031 00032 class repository_s3 extends repository { 00033 00040 public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) { 00041 parent::__construct($repositoryid, $context, $options); 00042 $this->access_key = get_config('s3', 'access_key'); 00043 $this->secret_key = get_config('s3', 'secret_key'); 00044 $this->s = new S3($this->access_key, $this->secret_key); 00045 } 00046 00053 public function get_listing($path = '') { 00054 global $CFG, $OUTPUT; 00055 if (empty($this->access_key)) { 00056 die(json_encode(array('e'=>get_string('needaccesskey', 'repository_s3')))); 00057 } 00058 $list = array(); 00059 $list['list'] = array(); 00060 // the management interface url 00061 $list['manage'] = false; 00062 // dynamically loading 00063 $list['dynload'] = true; 00064 // the current path of this list. 00065 // set to true, the login link will be removed 00066 $list['nologin'] = true; 00067 // set to true, the search button will be removed 00068 $list['nosearch'] = true; 00069 $tree = array(); 00070 if (empty($path)) { 00071 $buckets = $this->s->listBuckets(); 00072 foreach ($buckets as $bucket) { 00073 $folder = array( 00074 'title' => $bucket, 00075 'children' => array(), 00076 'thumbnail'=>$OUTPUT->pix_url('f/folder-32')->out(false), 00077 'path'=>$bucket 00078 ); 00079 $tree[] = $folder; 00080 } 00081 } else { 00082 $contents = $this->s->getBucket($path); 00083 foreach ($contents as $file) { 00084 $info = $this->s->getObjectInfo($path, baseName($file['name'])); 00085 $tree[] = array( 00086 'title'=>$file['name'], 00087 'size'=>$file['size'], 00088 'date'=>userdate($file['time']), 00089 'source'=>$path.'/'.$file['name'], 00090 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($file['name'], 32))->out(false) 00091 ); 00092 } 00093 } 00094 00095 $list['list'] = $tree; 00096 00097 return $list; 00098 } 00099 00107 public function get_file($filepath, $file) { 00108 global $CFG; 00109 $arr = explode('/', $filepath); 00110 $bucket = $arr[0]; 00111 $filename = $arr[1]; 00112 $path = $this->prepare_file($file); 00113 $this->s->getObject($bucket, $filename, $path); 00114 return array('path'=>$path); 00115 } 00116 00122 public function check_login() { 00123 return true; 00124 } 00125 00131 public function global_search() { 00132 return false; 00133 } 00134 00135 public static function get_type_option_names() { 00136 return array('access_key', 'secret_key', 'pluginname'); 00137 } 00138 00139 public function type_config_form($mform) { 00140 parent::type_config_form($mform); 00141 $strrequired = get_string('required'); 00142 $mform->addElement('text', 'access_key', get_string('access_key', 'repository_s3')); 00143 $mform->addElement('text', 'secret_key', get_string('secret_key', 'repository_s3')); 00144 $mform->addRule('access_key', $strrequired, 'required', null, 'client'); 00145 $mform->addRule('secret_key', $strrequired, 'required', null, 'client'); 00146 } 00147 00153 public function supported_returntypes() { 00154 return FILE_INTERNAL; 00155 } 00156 }