MDL-29857 - lib: Add an OAuth 2.0 client
[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 {
37 private $subauthtoken = '';
38
447c7a19 39 public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
6667f9e4 40 global $USER;
41 parent::__construct($repositoryid, $context, $options);
42
43 // TODO: I wish there was somewhere we could explicitly put this outside of constructor..
44 $googletoken = optional_param('token', false, PARAM_RAW);
45 if($googletoken){
46 $gauth = new google_authsub(false, $googletoken); // will throw exception if fails
47 google_docs::set_sesskey($gauth->get_sessiontoken(), $USER->id);
48 }
94742bca 49 $this->check_login();
6667f9e4 50 }
51
52 public function check_login() {
53 global $USER;
54
55 $sesskey = google_docs::get_sesskey($USER->id);
56
57 if($sesskey){
58 try{
59 $gauth = new google_authsub($sesskey);
60 $this->subauthtoken = $sesskey;
61 return true;
62 }catch(Exception $e){
63 // sesskey is not valid, delete store and re-auth
64 google_docs::delete_sesskey($USER->id);
65 }
66 }
67
68 return false;
69 }
70
4317f92f 71 public function print_login($ajax = true){
6667f9e4 72 global $CFG;
73 if($ajax){
4317f92f 74 $ret = array();
6bdfef5d 75 $popup_btn = new stdClass();
4317f92f 76 $popup_btn->type = 'popup';
0b75ea78 77 $returnurl = $CFG->wwwroot.'/repository/repository_callback.php?callback=yes&repo_id='.$this->id;
6667f9e4 78 $popup_btn->url = google_authsub::login_url($returnurl, google_docs::REALM);
4317f92f
PS
79 $ret['login'] = array($popup_btn);
80 return $ret;
6667f9e4 81 }
82 }
83
84 public function get_listing($path='', $page = '') {
85 $gdocs = new google_docs(new google_authsub($this->subauthtoken));
86
87 $ret = array();
88 $ret['dynload'] = true;
89 $ret['list'] = $gdocs->get_file_list();
90 return $ret;
91 }
92
68a7c9a6 93 public function search($search_text, $page = 0) {
6667f9e4 94 $gdocs = new google_docs(new google_authsub($this->subauthtoken));
95
96 $ret = array();
97 $ret['dynload'] = true;
68a7c9a6 98 $ret['list'] = $gdocs->get_file_list($search_text);
6667f9e4 99 return $ret;
100 }
101
102 public function logout(){
103 global $USER;
104
105 $token = google_docs::get_sesskey($USER->id);
106
107 $gauth = new google_authsub($token);
108 // revoke token from google
109 $gauth->revoke_session_token();
110
111 google_docs::delete_sesskey($USER->id);
112 $this->subauthtoken = '';
113
114 return parent::logout();
115 }
116
68a7c9a6 117 public function get_file($url, $file = '') {
6667f9e4 118 global $CFG;
a53d4f45 119 $path = $this->prepare_file($file);
6667f9e4 120
a53d4f45 121 $fp = fopen($path, 'w');
6667f9e4 122 $gdocs = new google_docs(new google_authsub($this->subauthtoken));
123 $gdocs->download_file($url, $fp);
124
1dce6261 125 return array('path'=>$path, 'url'=>$url);
41076c58 126 }
6667f9e4 127
41076c58
DC
128 public function supported_filetypes() {
129 return array('document');
130 }
131 public function supported_returntypes() {
132 return FILE_INTERNAL;
133 }
6667f9e4 134}
135//Icon from: http://www.iconspedia.com/icon/google-2706.html