MDL-30740 skydrive: show albums as essentially they are "folders"
[moodle.git] / repository / skydrive / microsoftliveapi.php
CommitLineData
74462841
DP
1<?php
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
17/**
18 * Functions for operating with the skydrive API
19 *
20 * @package repository_skydrive
21 * @copyright 2012 Lancaster University Network Services Ltd
22 * @author Dan Poltawski <dan.poltawski@luns.net.uk>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26
27defined('MOODLE_INTERNAL') || die();
28
29require_once($CFG->libdir.'/oauthlib.php');
30
31/**
32 * A helper class to access microsoft live resources using the api.
33 *
34 * This uses the microsfot API defined in
35 * http://msdn.microsoft.com/en-us/library/hh243648.aspx
36 *
37 * @package repository_skydrive
38 * @copyright 2012 Lancaster University Network Services Ltd
39 * @author Dan Poltawski <dan.poltawski@luns.net.uk>
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 */
42class microsoft_skydrive extends oauth2_client {
43 /** @var string OAuth 2.0 scope */
44 const SCOPE = 'wl.skydrive';
45 /** @var string Base url to access API */
46 const API = 'https://apis.live.net/v5.0';
47
48 /**
49 * Construct a skydrive request object
50 *
51 * @param string $clientid client id for OAuth 2.0 provided by microsoft
52 * @param string $clientsecret secret for OAuth 2.0 provided by microsoft
53 * @param moodle_url $returnurl url to return to after succseful auth
54 */
55 public function __construct($clientid, $clientsecret, $returnurl) {
56 parent::__construct($clientid, $clientsecret, $returnurl, self::SCOPE);
57 }
58
59 /**
60 * Should HTTP GET be used instead of POST?
61 *
62 * The Microsoft API does not support POST, so we should use
63 * GET instead (with the auth_token passed as a GET param).
64 *
65 * @return bool true if GET should be used
66 */
67 protected function use_http_get() {
68 return true;
69 }
70
71 /**
72 * Returns the auth url for OAuth 2.0 request
73 * @return string the auth url
74 */
75 protected function auth_url() {
76 return 'https://oauth.live.com/authorize';
77 }
78
79 /**
80 * Returns the token url for OAuth 2.0 request
81 * @return string the auth url
82 */
83 protected function token_url() {
84 return 'https://oauth.live.com/token';
85 }
86
87 /**
88 * Downloads a file to a file from skydrive using authenticated request
89 *
90 * @param string $id id of file
91 * @param string $path path to save file to
92 * @return array stucture for repository download_file
93 */
94 public function download_file($id, $path) {
95 $url = self::API."/${id}/content";
96 // Microsoft live redirects to the real download location..
97 $this->setopt(array('CURLOPT_FOLLOWLOCATION' => true, 'CURLOPT_MAXREDIRS' => 3));
98 $content = $this->get($url);
99 file_put_contents($path, $content);
100 return array('path'=>$path, 'url'=>$url);
101 }
102
103 /**
104 * Returns a list of files the user has formated for files api
105 *
106 * @param string $path the path which we are in
107 * @return mixed Array of files formated for fileapoi
108 */
109 public function get_file_list($path = '') {
110 global $OUTPUT;
111
112 if (empty($path)) {
113 $url = self::API."/me/skydrive/files/";
114 } else {
115 $url = self::API."/{$path}/files/";
116 }
117
118 $ret = json_decode($this->get($url));
119
120 if (isset($ret->error)) {
121 $this->log_out();
122 return false;
123 }
124
125 $files = array();
126
127 foreach ($ret->data as $file) {
128 switch($file->type) {
129 case 'folder':
1bb2f421 130 case 'album':
74462841
DP
131 $files[] = array(
132 'title' => $file->name,
133 'path' => $file->id,
134 'size' => 0,
135 'date' => strtotime($file->updated_time),
136 'thumbnail' => $OUTPUT->pix_url(file_folder_icon(90))->out(false),
137 'children' => array(),
138 );
139 break;
140 case 'photo':
141 $files[] = array(
142 'title' => $file->name,
143 'size' => $file->size,
144 'date' => strtotime($file->updated_time),
145 'thumbnail' => $file->picture,
146 'source' => $file->id,
147 'url' => $file->link,
148 );
149 break;
150 case 'video':
151 $files[] = array(
152 'title' => $file->name,
153 'size' => $file->size,
154 'date' => strtotime($file->updated_time),
155 'thumbnail' => $file->picture,
156 'source' => $file->id,
157 'url' => $file->link,
158 );
159 break;
160 case 'audio':
161 $files[] = array(
162 'title' => $file->name,
163 'size' => $file->size,
164 'date' => strtotime($file->updated_time),
165 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($file->name, 90))->out(false),
166 'source' => $file->id,
167 'url' => $file->link,
168 );
169 break;
170 case 'file':
171 $files[] = array(
172 'title' => $file->name,
173 'size' => $file->size,
174 'date' => strtotime($file->updated_time),
175 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($file->name, 90))->out(false),
176 'source' => $file->id,
177 'url' => $file->link,
178 );
179 break;
180 }
181 }
182 return $files;
183 }
184}