From d2c98296e6d1b82441a14624afd849587b18cd86 Mon Sep 17 00:00:00 2001
From: Dongsheng Cai <unoter@gmail.com>
Date: Wed, 18 Nov 2009 07:22:04 +0000
Subject: [PATCH] "MDL-16910, fixed access key check, repository s3 plugin"

---
 repository/s3/repository.class.php | 79 +++++++++++++++++++++++++++---
 1 file changed, 73 insertions(+), 6 deletions(-)

diff --git a/repository/s3/repository.class.php b/repository/s3/repository.class.php
index a9a5182d93a..10d7b852865 100644
--- a/repository/s3/repository.class.php
+++ b/repository/s3/repository.class.php
@@ -1,18 +1,59 @@
 <?php
+
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+
+/**
+ * This is a repository class used to browse Amazon S3 content.
+ *
+ * @since 2.0
+ * @package moodlecore
+ * @subpackage repository
+ * @copyright 2009 Dongsheng Cai <dongsheng@moodle.com>
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
 require_once('S3.php');
 
 class repository_s3 extends repository {
+
+    /**
+     * Constructor
+     * @param int $repositoryid
+     * @param object $context
+     * @param array $options
+     */
     public function __construct($repositoryid, $context = SITEID, $options = array()) {
         parent::__construct($repositoryid, $context, $options);
         $this->access_key = get_config('s3', 'access_key');
         $this->secret_key = get_config('s3', 'secret_key');
-        if (empty($this->access_key)) {
-            die(json_encode(array('e'=>get_string('repository_s3', 'needaccesskey'))));
-        }
         $this->s = new S3($this->access_key, $this->secret_key);
     }
-    public function get_listing($path = '', $page = '') {
+
+    /**
+     * Get S3 file list
+     *
+     * @param string $path
+     * @return array The file list and options
+     */
+    public function get_listing($path = '') {
         global $CFG, $OUTPUT;
+        if (empty($this->access_key)) {
+            die(json_encode(array('e'=>get_string('needaccesskey', 'repository_s3'))));
+        }
         $list = array();
         $list['list'] = array();
         // the management interface url
@@ -54,6 +95,14 @@ class repository_s3 extends repository {
 
         return $list;
     }
+
+    /**
+     * Download S3 files to moodle
+     *
+     * @param string $filepath
+     * @param string $file The file path in moodle
+     * @return array The local stored path
+     */
     public function get_file($filepath, $file) {
         global $CFG;
         $arr = explode('/', $filepath);
@@ -63,24 +112,42 @@ class repository_s3 extends repository {
         $this->s->getObject($bucket, $filename, $path);
         return $path;
     }
-    // login
+
+    /**
+     * S3 doesn't require login
+     *
+     * @return bool
+     */
     public function check_login() {
         return true;
     }
+                    
+    /**
+     * S3 doesn't provide search
+     *
+     * @return bool
+     */             
     public function global_search() {
         return false;
     }
+
     public static function get_type_option_names() {
         return array('access_key', 'secret_key');
     }
+
     public function type_config_form(&$mform) {
         $strrequired = get_string('required');
         $mform->addElement('text', 'access_key', get_string('access_key', 'repository_s3'));
         $mform->addElement('text', 'secret_key', get_string('secret_key', 'repository_s3'));
         $mform->addRule('access_key', $strrequired, 'required', null, 'client');
         $mform->addRule('secret_key', $strrequired, 'required', null, 'client');
-        return true;
     }
+
+    /**
+     * S3 plugins doesn't support return links of files
+     *
+     * @return int
+     */             
     public function supported_returntypes() {
         return FILE_INTERNAL;
     }