MDL-22985, repository plugins use new callback url.
[moodle.git] / repository / dropbox / lib.php
index 963124c..480150b 100644 (file)
 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
 
 /**
- * dropbox class
- * A helper class to access dropbox resources
+ * repository_dropbox class
+ * This plugin is used to access user's dropbox files
+ *
+ * TODO:
+ * Dropbox has problems to process filepath with spaces, tried to use
+ * urlencode filepath, still doesn't work
+ * http://code.google.com/p/dropbox-php/ has the same problem
  *
  * @since 2.0
  * @package moodlecore
  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
 
+require_once(dirname(__FILE__).'/locallib.php');
+
+class repository_dropbox extends repository {
+    private $dropbox;
+    public $files;
+    public $logged=false;
+
+    /**
+     * Constructor of dropbox plugin
+     * @param int $repositoryid
+     * @param object $context
+     * @param array $options
+     */
+    public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
+        global $SESSION, $CFG;
+        $options['page']    = optional_param('p', 1, PARAM_INT);
+        parent::__construct($repositoryid, $context, $options);
+
+        $this->setting = 'dropbox_';
+
+        $this->dropbox_key = $this->get_option('dropbox_key');
+        $this->dropbox_secret  = $this->get_option('dropbox_secret');
+
+        $this->access_key    = get_user_preferences($this->setting.'_access_key', '');
+        $this->access_secret = get_user_preferences($this->setting.'_access_secret', '');
+
+        if (!empty($this->access_key) && !empty($this->access_secret)) {
+            $this->logged = true;
+        }
+
+        $this->callback = new moodle_url($CFG->wwwroot.'/repository/repository_callback.php', array(
+            'callback'=>'yes',
+            'repo_id'=>$repositoryid
+            ));
+
+        $args = array(
+            'oauth_consumer_key'=>$this->dropbox_key,
+            'oauth_consumer_secret'=>$this->dropbox_secret,
+            'oauth_callback' => $this->callback->out(false),
+            'api_root' => 'http://api.dropbox.com/0/oauth',
+        );
+
+        $this->dropbox = new dropbox($args);
+    }
+
+    /**
+     * Check if moodle has got access token and secret
+     * @return bool
+     */
+    public function check_login() {
+        return !empty($this->logged);
+    }
+
+    /**
+     * Generate dropbox login url
+     * @return array
+     */
+    public function print_login() {
+        $result = $this->dropbox->request_token();
+        set_user_preference($this->setting.'_request_secret', $result['oauth_token_secret']);
+        $url = $result['authorize_url'];
+        if ($this->options['ajax']) {
+            $ret = array();
+            $popup_btn = new stdclass;
+            $popup_btn->type = 'popup';
+            $popup_btn->url = $url;
+            $ret['login'] = array($popup_btn);
+            return $ret;
+        } else {
+            echo '<a target="_blank" href="'.$this->flickr->auth().'">'.get_string('login', 'repository').'</a>';
+        }
+    }
+
+    /**
+     * Request access token
+     * @return array
+     */
+    public function callback() {
+        $token  = optional_param('oauth_token', '', PARAM_TEXT);
+        $secret = get_user_preferences($this->setting.'_request_secret', '');
+        $access_token = $this->dropbox->get_access_token($token, $secret);
+        set_user_preference($this->setting.'_access_key', $access_token['oauth_token']);
+        set_user_preference($this->setting.'_access_secret', $access_token['oauth_token_secret']);
+    }
+
+    /**
+     * 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);
+        }
+        $result = $this->dropbox->get_listing($path, $this->access_key, $this->access_secret);
+        $current_path = file_correct_filepath($result->path);
+        if (empty($result->path)) {
+            $current_path = '/';
+        }
+
+        $list = array();
+        $list['list'] = array();
+        // process breacrumb trail
+        $list['path'] = array(
+            array('name'=>'Dropbox Sandbox', '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);
+            }
+        }
+        $list['manage'] = false;
+        $list['dynload'] = true;
+        $list['nosearch'] = true;
+
+        $files = $result->contents;
+        foreach ($files as $file) {
+            if ($file->is_dir) {
+                $list['list'][] = array(
+                    'title' => substr($file->path, strpos($file->path, $current_path)+strlen($current_path)),
+                    'path' => file_correct_filepath($file->path),
+                    'size' => $file->size,
+                    'date' => $file->modified,
+                    'thumbnail' => $OUTPUT->pix_url('f/folder-32').'',
+                    'children' => array(),
+                );
+            } else {
+                $list['list'][] = array(
+                    'title' => substr($file->path, strpos($file->path, $current_path)+strlen($current_path)),
+                    'source' => $file->path,
+                    'size' => $file->size,
+                    'date' => $file->modified,
+                    'thumbnail' => $OUTPUT->pix_url(file_extension_icon($file->path, 32)).''
+                );
+            }
+        }
+        return $list;
+    }
+    /**
+     * Logout from dropbox
+     * @return array
+     */
+    public function logout() {
+        set_user_preference($this->setting.'_access_key', '');
+        set_user_preference($this->setting.'_access_secret', '');
+        $this->access_key    = '';
+        $this->access_secret = '';
+        return $this->print_login();
+    }
+
+    /**
+     * Set dropbox option
+     * @param array $options
+     * @return mixed
+     */
+    public function set_option($options = array()) {
+        if (!empty($options['dropbox_key'])) {
+            set_config('dropbox_key', trim($options['dropbox_key']), 'dropbox');
+        }
+        if (!empty($options['dropbox_secret'])) {
+            set_config('dropbox_secret', trim($options['dropbox_secret']), 'dropbox');
+        }
+        unset($options['dropbox_key']);
+        unset($options['dropbox_secret']);
+        $ret = parent::set_option($options);
+        return $ret;
+    }
+
+    /**
+     * Get dropbox options
+     * @param string $config
+     * @return mixed
+     */
+    public function get_option($config = '') {
+        if ($config==='dropbox_key') {
+            return trim(get_config('dropbox', 'dropbox_key'));
+        } elseif ($config==='dropbox_secret') {
+            return trim(get_config('dropbox', 'dropbox_secret'));
+        } else {
+            $options['dropbox_key'] = trim(get_config('dropbox', 'dropbox_key'));
+            $options['dropbox_secret'] = trim(get_config('dropbox', 'dropbox_secret'));
+        }
+        $options = parent::get_option($config);
+        return $options;
+    }
+
+    /**
+     *
+     * @param string $photo_id
+     * @param string $file
+     * @return string
+     */
+    public function get_file($filepath, $saveas = '') {
+        $this->dropbox->set_access_token($this->access_key, $this->access_secret);
+        return $this->dropbox->get_file($filepath, $saveas);
+    }
+    /**
+     * Add Plugin settings input to Moodle form
+     * @param object $mform
+     */
+    public function type_config_form($mform) {
+        global $CFG;
+        $key    = get_config('dropbox', 'dropbox_key');
+        $secret = get_config('dropbox', 'dropbox_secret');
+
+        if (empty($key)) {
+            $key = '';
+        }
+        if (empty($secret)) {
+            $secret = '';
+        }
+
+        $strrequired = get_string('required');
+
+        $mform->addElement('text', 'dropbox_key', get_string('apikey', 'repository_dropbox'), array('value'=>$key,'size' => '40'));
+        $mform->addElement('text', 'dropbox_secret', get_string('secret', 'repository_dropbox'), array('value'=>$secret,'size' => '40'));
+
+        $mform->addRule('dropbox_key', $strrequired, 'required', null, 'client');
+        $mform->addRule('dropbox_secret', $strrequired, 'required', null, 'client');
+        $str_getkey = get_string('instruction', 'repository_dropbox');
+        $mform->addElement('static', null, '',  $str_getkey);
+    }
+
+    /**
+     * Option names of dropbox plugin
+     * @return array
+     */
+    public static function get_type_option_names() {
+        return array('dropbox_key', 'dropbox_secret');
+    }
+
+    /**
+     * Dropbox plugin supports all kinds of files
+     * @return array
+     */
+    public function supported_filetypes() {
+        return '*';
+    }
 
-require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
-require_once($CFG->libdir.'/oauthlib.php');
-
-class dropbox extends oauth_helper {
-    private $mode = 'sandbox';
-    private $dropbox_api = 'http://api.dropbox.com/0';
-    private $dropbox_content_api = 'http://api-content.dropbox.com/0';
-    function __construct($args) {
-        parent::__construct($args);
-    }
-    public function get_listing($path='/', $token='', $secret='') {
-        $url = $this->dropbox_api.'/metadata/'.$this->mode.$path;
-        $content = $this->get($url, array(), $token, $secret);
-        $data = json_decode($content);
-        return $data;
-    }
-    public function get_account_info($token, $secret) {
-        $url = $this->dropbox_api.'/account/info';
-        $content = $this->get($url, array(), $token, $secret);
-        return $content;
-    }
-    public function get_file($filepath, $saveas) {
-        $url = 'http://api-content.dropbox.com/0/files/sandbox'.$filepath;
-        $content = $this->get($url, array());
-        file_put_contents($saveas, $content);
-        return array('path'=>$saveas, 'url'=>$url);
+    /**
+     * User cannot use the external link to dropbox
+     * @return int
+     */
+    public function supported_returntypes() {
+        return FILE_INTERNAL;
     }
 }