2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Functions for operating with the skydrive API
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
27 defined('MOODLE_INTERNAL') || die();
29 require_once($CFG->libdir.'/oauthlib.php');
32 * A helper class to access microsoft live resources using the api.
34 * This uses the microsfot API defined in
35 * http://msdn.microsoft.com/en-us/library/hh243648.aspx
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
42 class 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';
49 * Construct a skydrive request object
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
55 public function __construct($clientid, $clientsecret, $returnurl) {
56 parent::__construct($clientid, $clientsecret, $returnurl, self::SCOPE);
60 * Should HTTP GET be used instead of POST?
62 * The Microsoft API does not support POST, so we should use
63 * GET instead (with the auth_token passed as a GET param).
65 * @return bool true if GET should be used
67 protected function use_http_get() {
72 * Returns the auth url for OAuth 2.0 request
73 * @return string the auth url
75 protected function auth_url() {
76 return 'https://oauth.live.com/authorize';
80 * Returns the token url for OAuth 2.0 request
81 * @return string the auth url
83 protected function token_url() {
84 return 'https://oauth.live.com/token';
88 * Downloads a file to a file from skydrive using authenticated request
90 * @param string $id id of file
91 * @param string $path path to save file to
92 * @return array stucture for repository download_file
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);
104 * Returns a list of files the user has formated for files api
106 * @param string $path the path which we are in
107 * @return mixed Array of files formated for fileapoi
109 public function get_file_list($path = '') {
113 $url = self::API."/me/skydrive/files/";
115 $url = self::API."/{$path}/files/";
118 $ret = json_decode($this->get($url));
120 if (isset($ret->error)) {
127 foreach ($ret->data as $file) {
128 switch($file->type) {
131 'title' => $file->name,
134 'date' => strtotime($file->updated_time),
135 'thumbnail' => $OUTPUT->pix_url(file_folder_icon(90))->out(false),
136 'children' => array(),
141 'title' => $file->name,
142 'size' => $file->size,
143 'date' => strtotime($file->updated_time),
144 'thumbnail' => $file->picture,
145 'source' => $file->id,
146 'url' => $file->link,
151 'title' => $file->name,
152 'size' => $file->size,
153 'date' => strtotime($file->updated_time),
154 'thumbnail' => $file->picture,
155 'source' => $file->id,
156 'url' => $file->link,
161 'title' => $file->name,
162 'size' => $file->size,
163 'date' => strtotime($file->updated_time),
164 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($file->name, 90))->out(false),
165 'source' => $file->id,
166 'url' => $file->link,
171 'title' => $file->name,
172 'size' => $file->size,
173 'date' => strtotime($file->updated_time),
174 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($file->name, 90))->out(false),
175 'source' => $file->id,
176 'url' => $file->link,