repository/googledocs New repository plugin MDL-16383
[moodle.git] / repository / googledocs / repository.class.php
CommitLineData
6667f9e4 1<?php
2/**
3 * Google Docs Plugin
4 *
5 * @author Dan Poltawski <talktodan@gmail.com>
6 * @version $Id$
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
8 */
9
10require_once($CFG->libdir.'/googleapi.php');
11
12class repository_googledocs extends repository {
13 private $subauthtoken = '';
14
15 public function __construct($repositoryid, $context = SITEID, $options = array()) {
16 global $USER;
17 parent::__construct($repositoryid, $context, $options);
18
19 // TODO: I wish there was somewhere we could explicitly put this outside of constructor..
20 $googletoken = optional_param('token', false, PARAM_RAW);
21 if($googletoken){
22 $gauth = new google_authsub(false, $googletoken); // will throw exception if fails
23 google_docs::set_sesskey($gauth->get_sessiontoken(), $USER->id);
24 }
25
26 # fixme - we are not checking login before all functions in the repo api.. eg search
27 # MDL-17474
28 $this->check_login();
29 }
30
31 public function check_login() {
32 global $USER;
33
34 $sesskey = google_docs::get_sesskey($USER->id);
35
36 if($sesskey){
37 try{
38 $gauth = new google_authsub($sesskey);
39 $this->subauthtoken = $sesskey;
40 return true;
41 }catch(Exception $e){
42 // sesskey is not valid, delete store and re-auth
43 google_docs::delete_sesskey($USER->id);
44 }
45 }
46
47 return false;
48 }
49
50 public function print_login($ajax = true){
51 global $CFG;
52 if($ajax){
53 $ret = array();
54 $popup_btn = new stdclass;
55 $popup_btn->type = 'popup';
56 $returnurl = $CFG->wwwroot.'/repository/ws.php?callback=yes&repo_id='.$this->id;
57 $popup_btn->url = google_authsub::login_url($returnurl, google_docs::REALM);
58 $ret['login'] = array($popup_btn);
59 return $ret;
60 }
61 }
62
63 public function get_listing($path='', $page = '') {
64 $gdocs = new google_docs(new google_authsub($this->subauthtoken));
65
66 $ret = array();
67 $ret['dynload'] = true;
68 $ret['list'] = $gdocs->get_file_list();
69 return $ret;
70 }
71
72 public function search($query){
73 $gdocs = new google_docs(new google_authsub($this->subauthtoken));
74
75 $ret = array();
76 $ret['dynload'] = true;
77 $ret['list'] = $gdocs->get_file_list($query);
78 return $ret;
79 }
80
81 public function logout(){
82 global $USER;
83
84 $token = google_docs::get_sesskey($USER->id);
85
86 $gauth = new google_authsub($token);
87 // revoke token from google
88 $gauth->revoke_session_token();
89
90 google_docs::delete_sesskey($USER->id);
91 $this->subauthtoken = '';
92
93 return parent::logout();
94 }
95
96 public function get_name(){
97 return get_string('repositoryname', 'repository_googledocs');
98 }
99
100 public function get_file($url, $file) {
101 global $CFG;
102
103
104 //FIXME: Why does every repo plugin.. do this mktemp file itself..
105
106 if (!file_exists($CFG->dataroot.'/repository/download')) {
107 mkdir($CFG->dataroot.'/repository/download/', 0777, true);
108 }
109
110 if(is_dir($CFG->dataroot.'/repository/download')) {
111 $dir = $CFG->dataroot.'/repository/download/';
112 }
113
114 if (empty($file)){
115 $file = time();
116 }
117
118 $fp = fopen($dir.$file, 'w');
119 $gdocs = new google_docs(new google_authsub($this->subauthtoken));
120 $gdocs->download_file($url, $fp);
121
122 return $dir.$file;
123 }
124
125
126}
127//Icon from: http://www.iconspedia.com/icon/google-2706.html