[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/repository/skydrive/ -> microsoftliveapi.php (source)

   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  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  require_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   */
  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';
  47      /** @var cache_session cache of foldernames */
  48      var $foldernamecache = null;
  49  
  50      /**
  51       * Construct a skydrive request object
  52       *
  53       * @param string $clientid client id for OAuth 2.0 provided by microsoft
  54       * @param string $clientsecret secret for OAuth 2.0 provided by microsoft
  55       * @param moodle_url $returnurl url to return to after succseful auth
  56       */
  57      public function __construct($clientid, $clientsecret, $returnurl) {
  58          parent::__construct($clientid, $clientsecret, $returnurl, self::SCOPE);
  59          // Make a session cache
  60          $this->foldernamecache = cache::make('repository_skydrive', 'foldername');
  61      }
  62  
  63      /**
  64       * Should HTTP GET be used instead of POST?
  65       *
  66       * The Microsoft API does not support POST, so we should use
  67       * GET instead (with the auth_token passed as a GET param).
  68       *
  69       * @return bool true if GET should be used
  70       */
  71      protected function use_http_get() {
  72          return true;
  73      }
  74  
  75      /**
  76       * Returns the auth url for OAuth 2.0 request
  77       * @return string the auth url
  78       */
  79      protected function auth_url() {
  80          return 'https://login.live.com/oauth20_authorize.srf';
  81      }
  82  
  83      /**
  84       * Returns the token url for OAuth 2.0 request
  85       * @return string the auth url
  86       */
  87      protected function token_url() {
  88          return 'https://login.live.com/oauth20_token.srf';
  89      }
  90  
  91      /**
  92       * Downloads a file to a  file from skydrive using authenticated request
  93       *
  94       * @param string $id id of file
  95       * @param string $path path to save file to
  96       * @return array stucture for repository download_file
  97       */
  98      public function download_file($id, $path) {
  99          $url = self::API."/$id}/content";
 100          // Microsoft live redirects to the real download location..
 101          $this->setopt(array('CURLOPT_FOLLOWLOCATION' => true, 'CURLOPT_MAXREDIRS' => 3));
 102          $content = $this->get($url);
 103          file_put_contents($path, $content);
 104          return array('path'=>$path, 'url'=>$url);
 105      }
 106  
 107      /**
 108       * Returns a folder name property for a given folderid.
 109       *
 110       * @param string $folderid the folder id which is passed
 111       * @return mixed folder name or false in case of error
 112       */
 113      public function get_folder_name($folderid) {
 114          if (empty($folderid)) {
 115              throw new coding_exception('Empty folderid passed to get_folder_name');
 116          }
 117  
 118          // Cache based on oauthtoken and folderid.
 119          $cachekey = $this->folder_cache_key($folderid);
 120  
 121          if ($foldername = $this->foldernamecache->get($cachekey)) {
 122              return $foldername;
 123          }
 124  
 125          $url = self::API."/{$folderid}";
 126          $ret = json_decode($this->get($url));
 127          if (isset($ret->error)) {
 128              $this->log_out();
 129              return false;
 130          }
 131  
 132          $this->foldernamecache->set($cachekey, $ret->name);
 133          return $ret->name;
 134      }
 135  
 136      /**
 137       * Returns a list of files the user has formated for files api
 138       *
 139       * @param string $path the path which we are in
 140       * @return mixed Array of files formated for fileapoi
 141       */
 142      public function get_file_list($path = '') {
 143          global $OUTPUT;
 144  
 145          $precedingpath = '';
 146          if (empty($path)) {
 147              $url = self::API."/me/skydrive/files/";
 148          } else {
 149              $parts = explode('/', $path);
 150              $currentfolder = array_pop($parts);
 151              $url = self::API."/{$currentfolder}/files/";
 152          }
 153  
 154          $ret = json_decode($this->get($url));
 155  
 156          if (isset($ret->error)) {
 157              $this->log_out();
 158              return false;
 159          }
 160  
 161          $files = array();
 162  
 163          foreach ($ret->data as $file) {
 164              switch($file->type) {
 165                  case 'folder':
 166                  case 'album':
 167                      // Cache the foldername for future requests.
 168                      $cachekey = $this->folder_cache_key($file->id);
 169                      $this->foldernamecache->set($cachekey, $file->name);
 170  
 171                      $files[] = array(
 172                          'title' => $file->name,
 173                          'path' => $path.'/'.$file->id,
 174                          'size' => 0,
 175                          'date' => strtotime($file->updated_time),
 176                          'thumbnail' => $OUTPUT->pix_url(file_folder_icon(90))->out(false),
 177                          'children' => array(),
 178                      );
 179                      break;
 180                  case 'photo':
 181                      $files[] = array(
 182                          'title' => $file->name,
 183                          'size' => $file->size,
 184                          'date' => strtotime($file->updated_time),
 185                          'thumbnail' => $OUTPUT->pix_url(file_extension_icon($file->name, 90))->out(false),
 186                          'realthumbnail' => $file->picture,
 187                          'source' => $file->id,
 188                          'url' => $file->link,
 189                          'image_height' => $file->height,
 190                          'image_width' => $file->width,
 191                          'author' => $file->from->name,
 192                      );
 193                      break;
 194                  case 'video':
 195                      $files[] = array(
 196                          'title' => $file->name,
 197                          'size' => $file->size,
 198                          'date' => strtotime($file->updated_time),
 199                          'thumbnail' => $OUTPUT->pix_url(file_extension_icon($file->name, 90))->out(false),
 200                          'realthumbnail' => $file->picture,
 201                          'source' => $file->id,
 202                          'url' => $file->link,
 203                          'author' => $file->from->name,
 204                      );
 205                      break;
 206                  case 'audio':
 207                      $files[] = array(
 208                          'title' => $file->name,
 209                          'size' => $file->size,
 210                          'date' => strtotime($file->updated_time),
 211                          'thumbnail' => $OUTPUT->pix_url(file_extension_icon($file->name, 90))->out(false),
 212                          'source' => $file->id,
 213                          'url' => $file->link,
 214                          'author' => $file->from->name,
 215                      );
 216                      break;
 217                  case 'file':
 218                      $files[] = array(
 219                          'title' => $file->name,
 220                          'size' => $file->size,
 221                          'date' => strtotime($file->updated_time),
 222                          'thumbnail' => $OUTPUT->pix_url(file_extension_icon($file->name, 90))->out(false),
 223                          'source' => $file->id,
 224                          'url' => $file->link,
 225                          'author' => $file->from->name,
 226                      );
 227                      break;
 228              }
 229          }
 230          return $files;
 231      }
 232  
 233      /**
 234       * Returns a key for foldernane cache
 235       *
 236       * @param string $folderid the folder id which is to be cached
 237       * @return string the cache key to use
 238       */
 239      private function folder_cache_key($folderid) {
 240          // Cache based on oauthtoken and folderid.
 241          return $this->get_tokenname().'_'.$folderid;
 242      }
 243  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1