MDL-22985, repository plugins use new callback url.
[moodle.git] / repository / googledocs / lib.php
CommitLineData
6667f9e4 1<?php
10d53fd3
DC
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
6667f9e4 18/**
19 * Google Docs Plugin
20 *
10d53fd3
DC
21 * @since 2.0
22 * @package moodlecore
23 * @subpackage repository
24 * @copyright 2009 Dan Poltawski <talktodan@gmail.com>
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
6667f9e4 26 */
27
28require_once($CFG->libdir.'/googleapi.php');
29
30class repository_googledocs extends repository {
31 private $subauthtoken = '';
32
447c7a19 33 public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
6667f9e4 34 global $USER;
35 parent::__construct($repositoryid, $context, $options);
36
37 // TODO: I wish there was somewhere we could explicitly put this outside of constructor..
38 $googletoken = optional_param('token', false, PARAM_RAW);
39 if($googletoken){
40 $gauth = new google_authsub(false, $googletoken); // will throw exception if fails
41 google_docs::set_sesskey($gauth->get_sessiontoken(), $USER->id);
42 }
94742bca 43 $this->check_login();
6667f9e4 44 }
45
46 public function check_login() {
47 global $USER;
48
49 $sesskey = google_docs::get_sesskey($USER->id);
50
51 if($sesskey){
52 try{
53 $gauth = new google_authsub($sesskey);
54 $this->subauthtoken = $sesskey;
55 return true;
56 }catch(Exception $e){
57 // sesskey is not valid, delete store and re-auth
58 google_docs::delete_sesskey($USER->id);
59 }
60 }
61
62 return false;
63 }
64
4317f92f 65 public function print_login($ajax = true){
6667f9e4 66 global $CFG;
67 if($ajax){
4317f92f
PS
68 $ret = array();
69 $popup_btn = new stdclass;
70 $popup_btn->type = 'popup';
0b75ea78 71 $returnurl = $CFG->wwwroot.'/repository/repository_callback.php?callback=yes&repo_id='.$this->id;
6667f9e4 72 $popup_btn->url = google_authsub::login_url($returnurl, google_docs::REALM);
4317f92f
PS
73 $ret['login'] = array($popup_btn);
74 return $ret;
6667f9e4 75 }
76 }
77
78 public function get_listing($path='', $page = '') {
79 $gdocs = new google_docs(new google_authsub($this->subauthtoken));
80
81 $ret = array();
82 $ret['dynload'] = true;
83 $ret['list'] = $gdocs->get_file_list();
84 return $ret;
85 }
86
87 public function search($query){
88 $gdocs = new google_docs(new google_authsub($this->subauthtoken));
89
90 $ret = array();
91 $ret['dynload'] = true;
92 $ret['list'] = $gdocs->get_file_list($query);
93 return $ret;
94 }
95
96 public function logout(){
97 global $USER;
98
99 $token = google_docs::get_sesskey($USER->id);
100
101 $gauth = new google_authsub($token);
102 // revoke token from google
103 $gauth->revoke_session_token();
104
105 google_docs::delete_sesskey($USER->id);
106 $this->subauthtoken = '';
107
108 return parent::logout();
109 }
110
0b75ea78 111 public function get_name() {
614d18d2 112 return get_string('pluginname', 'repository_googledocs');
6667f9e4 113 }
114
115 public function get_file($url, $file) {
116 global $CFG;
a53d4f45 117 $path = $this->prepare_file($file);
6667f9e4 118
a53d4f45 119 $fp = fopen($path, 'w');
6667f9e4 120 $gdocs = new google_docs(new google_authsub($this->subauthtoken));
121 $gdocs->download_file($url, $fp);
122
1dce6261 123 return array('path'=>$path, 'url'=>$url);
41076c58 124 }
6667f9e4 125
41076c58
DC
126 public function supported_filetypes() {
127 return array('document');
128 }
129 public function supported_returntypes() {
130 return FILE_INTERNAL;
131 }
6667f9e4 132}
133//Icon from: http://www.iconspedia.com/icon/google-2706.html