Skip to content

Commit

Permalink
MDL-42668 portfolio_boxnet: Migration to APIv2
Browse files Browse the repository at this point in the history
Conflicts:

	lib/boxlib.php
	portfolio/boxnet/lib.php
  • Loading branch information
Frederic Massart committed Nov 7, 2013
1 parent b1455d0 commit d311f9c
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 133 deletions.
87 changes: 83 additions & 4 deletions lib/boxlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class boxnet_client extends oauth2_client {
/** @const API URL */
const API = 'https://api.box.com/2.0';

/** @const UPLOAD_API URL */
const UPLOAD_API = 'https://upload.box.com/api/2.0';

/**
* Return authorize URL.
*
Expand All @@ -50,6 +53,21 @@ protected function auth_url() {
return 'https://www.box.com/api/oauth2/authorize';
}

/**
* Create a folder.
*
* @param string $foldername The folder name.
* @param int $parentid The ID of the parent folder.
* @return array Information about the new folder.
*/
public function create_folder($foldername, $parentid = 0) {
$params = array('name' => $foldername, 'parent' => array('id' => (string) $parentid));
$this->reset_state();
$result = $this->post($this->make_url("/folders"), json_encode($params));
$result = json_decode($result);
return $result;
}

/**
* Download the file.
*
Expand All @@ -58,6 +76,7 @@ protected function auth_url() {
* @return bool Success or not.
*/
public function download_file($fileid, $path) {
$this->reset_state();
$result = $this->download_one($this->make_url("/files/$fileid/content"), array(),
array('filepath' => $path, 'CURLOPT_FOLLOWLOCATION' => true));
return ($result === true && $this->info['http_code'] === 200);
Expand All @@ -70,6 +89,7 @@ public function download_file($fileid, $path) {
* @return object
*/
public function get_file_info($fileid) {
$this->reset_state();
$result = $this->request($this->make_url("/files/$fileid"));
return json_decode($result);
}
Expand All @@ -81,6 +101,7 @@ public function get_file_info($fileid) {
* @return object
*/
public function get_folder_items($folderid = 0) {
$this->reset_state();
$result = $this->request($this->make_url("/folders/$folderid/items",
array('fields' => 'id,name,type,modified_at,size,owned_by')));
return json_decode($result);
Expand All @@ -98,6 +119,7 @@ public function log_out() {
'client_secret' => $this->get_clientsecret(),
'token' => $accesstoken->token
);
$this->reset_state();
$this->post($this->revoke_url(), $params);
}
parent::log_out();
Expand All @@ -108,13 +130,47 @@ public function log_out() {
*
* @param string $uri The URI to request.
* @param array $params Query string parameters.
* @param bool $uploadapi Whether this works with the upload API or not.
* @return string
*/
protected function make_url($uri, $params = array()) {
$url = new moodle_url(self::API . '/' . ltrim($uri, '/'), $params);
protected function make_url($uri, $params = array(), $uploadapi = false) {
$api = $uploadapi ? self::UPLOAD_API : self::API;
$url = new moodle_url($api . '/' . ltrim($uri, '/'), $params);
return $url->out(false);
}

/**
* Rename a file.
*
* @param int $fileid The file ID.
* @param string $newname The new file name.
* @return object Box.net file object.
*/
public function rename_file($fileid, $newname) {
// This requires a PUT request with data within it. We cannot use
// the standard PUT request 'CURLOPT_PUT' because it expects a file.
$data = array('name' => $newname);
$options = array(
'CURLOPT_CUSTOMREQUEST' => 'PUT',
'CURLOPT_POSTFIELDS' => json_encode($data)
);
$url = $this->make_url("/files/$fileid");
$this->reset_state();
$result = $this->request($url, $options);
$result = json_decode($result);
return $result;
}

/**
* Resets curl for multiple requests.
*
* @return void
*/
public function reset_state() {
$this->cleanopt();
$this->resetHeader();
}

/**
* Return the revoke URL.
*
Expand All @@ -140,13 +196,13 @@ public function share_file($fileid, $businesscheck = true) {
'CURLOPT_CUSTOMREQUEST' => 'PUT',
'CURLOPT_POSTFIELDS' => json_encode($data)
);
$this->reset_state();
$result = $this->request($this->make_url("/files/$fileid"), $options);
$result = json_decode($result);

if ($businesscheck) {
// Checks that the user has the right to share the file. If not, throw an exception.
$this->resetopt();
$this->resetHeader();
$this->reset_state();
$this->head($result->shared_link->download_url);
$info = $this->get_info();
if ($info['http_code'] == 403) {
Expand All @@ -163,6 +219,7 @@ public function share_file($fileid, $businesscheck = true) {
* @return object
*/
public function search($query) {
$this->reset_state();
$result = $this->request($this->make_url('/search', array('query' => $query, 'limit' => 50, 'offset' => 0)));
return json_decode($result);
}
Expand All @@ -176,6 +233,28 @@ protected function token_url() {
return 'https://www.box.com/api/oauth2/token';
}

/**
* Upload a file.
*
* Please note that the file is named on Box.net using the path we are providing, and so
* the file has the name of the stored_file hash.
*
* @param stored_file $storedfile A stored_file.
* @param integer $parentid The ID of the parent folder.
* @return object Box.net file object.
*/
public function upload_file(stored_file $storedfile, $parentid = 0) {
$url = $this->make_url('/files/content', array(), true);
$options = array(
'filename' => $storedfile,
'parent_id' => $parentid
);
$this->reset_state();
$result = $this->post($url, $options);
$result = json_decode($result);
return $result;
}

}

/**
Expand Down
10 changes: 6 additions & 4 deletions portfolio/boxnet/lang/en/portfolio_boxnet.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

$string['apikey'] = 'API key';
$string['err_noapikey'] = 'No API key';
$string['err_noapikey_help'] = 'There is no API key configured for this plugin. You can get one of these from OpenBox development page.';
$string['clientid'] = 'Client ID';
$string['clientsecret'] = 'Client secret';
$string['existingfolder'] = 'Existing folder to put file(s) into';
$string['folderclash'] = 'The folder you asked to create already exists!';
$string['foldercreatefailed'] = 'Failed to create your target folder on box.net';
$string['folderlistfailed'] = 'Failed to retrieve a folder listing from box.net';
$string['missingoauthkeys'] = 'Missing client ID and secret';
$string['missingoauthkeys_help'] = 'There is no client ID or secret configured for this plugin. You can get one of these from Box.net development page.';
$string['newfolder'] = 'New folder to put file(s) into';
$string['noauthtoken'] = 'Could not retrieve an authentication token for use in this session';
$string['notarget'] = 'You must specify either an existing folder or a new folder to upload into';
Expand All @@ -38,10 +39,11 @@
$string['pluginname'] = 'Box.net';
$string['sendfailed'] = 'Failed to send content to box.net: {$a}';
$string['setupinfo'] = 'Setup instructions';
$string['setupinfodetails'] = 'To obtain API key, log in to Box.net and visit their <a href="{$a->servicesurl}">OpenBox development page</a>. In \'Developer Tools\', follow \'Create new application\' and create new application for your Moodle site. API key is displayed in \'Backend parameters\' section of the application edit form. In that form, fill \'Redirect URL\' field to:<br /><code>{$a->callbackurl}</code><br />Optionally, you can also provide other information about your Moodle site. These values can be edited later at \'View my applications\' page.';
$string['setupinfodetails'] = 'To obtain a client ID and secret, log in to Box.net and visit their <a href="{$a->servicesurl}">developers page</a>. Follow \'Create new application\' and create new application for your Moodle site. The client ID ans secret are displayed in \'OAuth2 parameters\' section of the application edit form. Optionally, you can also provide other information about your Moodle site.';
$string['sharedfolder'] = 'Shared';
$string['sharefile'] = 'Share this file?';
$string['sharefolder'] = 'Share this new folder?';
$string['targetfolder'] = 'Target folder';
$string['tobecreated'] = 'To be created';
$string['username'] = 'Your box.net username (will not be stored)';
$string['warninghttps'] = 'Box.net requires your website to be using HTTPS in order for the portfolio to work.';
Loading

0 comments on commit d311f9c

Please sign in to comment.