[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/repository/dropbox/ -> locallib.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   * A helper class to access dropbox resources
  19   *
  20   * @since Moodle 2.0
  21   * @package    repository_dropbox
  22   * @copyright  2012 Marina Glancy
  23   * @copyright  2010 Dongsheng Cai
  24   * @author     Dongsheng Cai <dongsheng@moodle.com>
  25   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  require_once($CFG->libdir.'/oauthlib.php');
  30  
  31  /**
  32   * Authentication class to access Dropbox API
  33   *
  34   * @package    repository_dropbox
  35   * @copyright  2010 Dongsheng Cai
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class dropbox extends oauth_helper {
  39      /** @var string dropbox access type, can be dropbox or sandbox */
  40      private $mode = 'dropbox';
  41      /** @var string dropbox api url*/
  42      private $dropbox_api = 'https://api.dropbox.com/1';
  43      /** @var string dropbox content api url*/
  44      private $dropbox_content_api = 'https://api-content.dropbox.com/1';
  45  
  46      /**
  47       * Constructor for dropbox class
  48       *
  49       * @param array $args
  50       */
  51      function __construct($args) {
  52          parent::__construct($args);
  53      }
  54  
  55      /**
  56       * Get file listing from dropbox
  57       *
  58       * @param string $path
  59       * @param string $token
  60       * @param string $secret
  61       * @return array
  62       */
  63      public function get_listing($path='/', $token='', $secret='') {
  64          $url = $this->dropbox_api.'/metadata/'.$this->mode.$path;
  65          $content = $this->get($url, array(), $token, $secret);
  66          $data = json_decode($content);
  67          return $data;
  68      }
  69  
  70      /**
  71       * Prepares the filename to pass to Dropbox API as part of URL
  72       *
  73       * @param string $filepath
  74       * @return string
  75       */
  76      protected function prepare_filepath($filepath) {
  77          $info = pathinfo($filepath);
  78          $dirname = $info['dirname'];
  79          $basename = $info['basename'];
  80          $filepath = $dirname . rawurlencode($basename);
  81          if ($dirname != '/') {
  82              $filepath = $dirname . '/' . $basename;
  83              $filepath = str_replace("%2F", "/", rawurlencode($filepath));
  84          }
  85          return $filepath;
  86      }
  87  
  88      /**
  89       * Retrieves the default (64x64) thumbnail for dropbox file
  90       *
  91       * @throws moodle_exception when file could not be downloaded
  92       *
  93       * @param string $filepath local path in Dropbox
  94       * @param string $saveas path to file to save the result
  95       * @param int $timeout request timeout in seconds, 0 means no timeout
  96       * @return array with attributes 'path' and 'url'
  97       */
  98      public function get_thumbnail($filepath, $saveas, $timeout = 0) {
  99          $url = $this->dropbox_content_api.'/thumbnails/'.$this->mode.$this->prepare_filepath($filepath);
 100          if (!($fp = fopen($saveas, 'w'))) {
 101              throw new moodle_exception('cannotwritefile', 'error', '', $saveas);
 102          }
 103          $this->setup_oauth_http_options(array('timeout' => $timeout, 'file' => $fp, 'BINARYTRANSFER' => true));
 104          $result = $this->get($url);
 105          fclose($fp);
 106          if ($result === true) {
 107              return array('path'=>$saveas, 'url'=>$url);
 108          } else {
 109              unlink($saveas);
 110              throw new moodle_exception('errorwhiledownload', 'repository', '', $result);
 111          }
 112      }
 113  
 114      /**
 115       * Downloads a file from Dropbox and saves it locally
 116       *
 117       * @throws moodle_exception when file could not be downloaded
 118       *
 119       * @param string $filepath local path in Dropbox
 120       * @param string $saveas path to file to save the result
 121       * @param int $timeout request timeout in seconds, 0 means no timeout
 122       * @return array with attributes 'path' and 'url'
 123       */
 124      public function get_file($filepath, $saveas, $timeout = 0) {
 125          $url = $this->dropbox_content_api.'/files/'.$this->mode.$this->prepare_filepath($filepath);
 126          if (!($fp = fopen($saveas, 'w'))) {
 127              throw new moodle_exception('cannotwritefile', 'error', '', $saveas);
 128          }
 129          $this->setup_oauth_http_options(array('timeout' => $timeout, 'file' => $fp, 'BINARYTRANSFER' => true));
 130          $result = $this->get($url);
 131          fclose($fp);
 132          if ($result === true) {
 133              return array('path'=>$saveas, 'url'=>$url);
 134          } else {
 135              unlink($saveas);
 136              throw new moodle_exception('errorwhiledownload', 'repository', '', $result);
 137          }
 138      }
 139  
 140      /**
 141       * Returns direct link to Dropbox file
 142       *
 143       * @param string $filepath local path in Dropbox
 144       * @param int $timeout request timeout in seconds, 0 means no timeout
 145       * @return string|null information object or null if request failed with an error
 146       */
 147      public function get_file_share_link($filepath, $timeout = 0) {
 148          $url = $this->dropbox_api.'/shares/'.$this->mode.$this->prepare_filepath($filepath);
 149          $this->setup_oauth_http_options(array('timeout' => $timeout));
 150          $result = $this->post($url, array('short_url'=>0));
 151          if (!$this->http->get_errno()) {
 152              $data = json_decode($result);
 153              if (isset($data->url)) {
 154                  return $data->url;
 155              }
 156          }
 157          return null;
 158      }
 159  
 160      /**
 161       * Sets Dropbox API mode (dropbox or sandbox, default dropbox)
 162       *
 163       * @param string $mode
 164       */
 165      public function set_mode($mode) {
 166          $this->mode = $mode;
 167      }
 168  }


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