MDL-33501 - oauth2lib: improve redirect url handling
[moodle.git] / repository / googledocs / lib.php
CommitLineData
6667f9e4 1<?php
10d53fd3
DC
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
6667f9e4 17/**
67233725 18 * This plugin is used to access google docs
6667f9e4 19 *
10d53fd3 20 * @since 2.0
67233725 21 * @package repository_googledocs
61506a0a 22 * @copyright 2009 Dan Poltawski <talktodan@gmail.com>
d078f6d3 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
6667f9e4 24 */
67233725 25require_once($CFG->dirroot . '/repository/lib.php');
6667f9e4 26require_once($CFG->libdir.'/googleapi.php');
27
67233725
DC
28/**
29 * Google Docs Plugin
30 *
31 * @since 2.0
32 * @package repository_googledocs
33 * @copyright 2009 Dan Poltawski <talktodan@gmail.com>
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 */
6667f9e4 36class repository_googledocs extends repository {
4560fd1b 37 private $googleoauth = null;
6667f9e4 38
447c7a19 39 public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
6667f9e4 40 parent::__construct($repositoryid, $context, $options);
41
4560fd1b
DP
42 $returnurl = new moodle_url('/repository/repository_callback.php',
43 array('callback' => 'yes', 'repo_id' =>$this->id));
44
45 $clientid = get_config('googledocs', 'clientid');
46 $secret = get_config('googledocs', 'secret');
db7602af 47 $this->googleoauth = new google_oauth($clientid, $secret, $returnurl, google_docs::REALM);
4560fd1b 48
94742bca 49 $this->check_login();
6667f9e4 50 }
51
52 public function check_login() {
4560fd1b 53 return $this->googleoauth->is_logged_in();
6667f9e4 54 }
55
4560fd1b
DP
56 public function print_login() {
57 $url = $this->googleoauth->get_login_url();
58
59 if ($this->options['ajax']) {
60 $popup = new stdClass();
61 $popup->type = 'popup';
62 $popup->url = $url->out(false);
63 return array('login' => array($popup));
64 } else {
65 echo '<a target="_blank" href="'.$url->out(false).'">'.get_string('login', 'repository').'</a>';
6667f9e4 66 }
67 }
68
69 public function get_listing($path='', $page = '') {
4560fd1b 70 $gdocs = new google_docs($this->googleoauth);
6667f9e4 71
72 $ret = array();
73 $ret['dynload'] = true;
74 $ret['list'] = $gdocs->get_file_list();
75 return $ret;
76 }
77
68a7c9a6 78 public function search($search_text, $page = 0) {
4560fd1b 79 $gdocs = new google_docs($this->googleoauth);
6667f9e4 80
81 $ret = array();
82 $ret['dynload'] = true;
68a7c9a6 83 $ret['list'] = $gdocs->get_file_list($search_text);
6667f9e4 84 return $ret;
85 }
86
4560fd1b
DP
87 public function logout() {
88 $this->googleoauth->log_out();
6667f9e4 89 return parent::logout();
90 }
91
68a7c9a6 92 public function get_file($url, $file = '') {
4560fd1b 93 $gdocs = new google_docs($this->googleoauth);
6667f9e4 94
4560fd1b
DP
95 $path = $this->prepare_file($file);
96 return $gdocs->download_file($url, $path);
41076c58 97 }
6667f9e4 98
41076c58 99 public function supported_filetypes() {
4560fd1b 100 return '*';
41076c58
DC
101 }
102 public function supported_returntypes() {
103 return FILE_INTERNAL;
104 }
4560fd1b
DP
105
106 public static function get_type_option_names() {
107 return array('clientid', 'secret', 'pluginname');
108 }
109
110 public static function type_config_form($mform, $classname = 'repository') {
111
112 $a = new stdClass;
113 $a->docsurl = get_docs_url('Google_OAuth2_Setup');
114 $a->callbackurl = google_oauth::callback_url()->out(false);
115
116 $mform->addElement('static', null, '', get_string('oauthinfo', 'repository_googledocs', $a));
117
118 parent::type_config_form($mform);
119 $mform->addElement('text', 'clientid', get_string('clientid', 'repository_googledocs'));
120 $mform->addElement('text', 'secret', get_string('secret', 'repository_googledocs'));
121
122 $strrequired = get_string('required');
123 $mform->addRule('clientid', $strrequired, 'required', null, 'client');
124 $mform->addRule('secret', $strrequired, 'required', null, 'client');
125 }
6667f9e4 126}
4560fd1b 127// Icon from: http://www.iconspedia.com/icon/google-2706.html.