2016-10-23 22:48:26 +00:00
|
|
|
<?php
|
|
|
|
/*----------------------------------------------------------------------------------| www.vdm.io |----/
|
|
|
|
Vast Development Method
|
|
|
|
/-------------------------------------------------------------------------------------------------------/
|
|
|
|
|
2018-01-29 21:18:09 +00:00
|
|
|
@version 2.0.3 - 27th January, 2018
|
2016-11-22 05:48:55 +00:00
|
|
|
@package Dropbox API 2
|
|
|
|
@subpackage dropbox.php
|
2016-10-23 22:48:26 +00:00
|
|
|
@author Llewellyn van der Merwe <http://www.vdm.io>
|
|
|
|
@copyright Copyright (C) 2015. All Rights Reserved
|
|
|
|
@license GNU/GPL Version 2 or later - http://www.gnu.org/licenses/gpl-2.0.html
|
|
|
|
|
|
|
|
/------------------------------------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
// No direct access.
|
|
|
|
defined('_JEXEC') or die;
|
|
|
|
|
|
|
|
/**
|
2018-01-29 21:18:09 +00:00
|
|
|
* Dropbox class to get shared links
|
2016-10-23 22:48:26 +00:00
|
|
|
*/
|
|
|
|
class Dropbox
|
|
|
|
{
|
2016-11-22 05:48:55 +00:00
|
|
|
/**
|
|
|
|
* final url
|
|
|
|
*/
|
|
|
|
protected $url;
|
2018-01-23 22:05:57 +00:00
|
|
|
|
2016-11-22 05:48:55 +00:00
|
|
|
/**
|
|
|
|
* The array for the post url
|
|
|
|
*/
|
2018-01-23 22:05:57 +00:00
|
|
|
protected $postUrl = array(
|
2016-11-22 05:48:55 +00:00
|
|
|
"protocol" => "https://",
|
|
|
|
"suddomain" => "api.",
|
|
|
|
"domain" => "dropboxapi.com",
|
|
|
|
"path" => "/2/"
|
|
|
|
);
|
2016-10-23 22:48:26 +00:00
|
|
|
|
2016-11-22 05:48:55 +00:00
|
|
|
/**
|
|
|
|
* the verious pathes we need
|
2018-01-23 22:05:57 +00:00
|
|
|
*/
|
|
|
|
protected $domainpath = array(
|
|
|
|
"list_shared_links" => "sharing/list_shared_links",
|
2016-11-22 05:48:55 +00:00
|
|
|
"list_folder" => "files/list_folder",
|
|
|
|
"list_folder_continue" => "files/list_folder/continue",
|
2018-01-23 22:05:57 +00:00
|
|
|
"create_shared_link_with_settings" => "sharing/create_shared_link_with_settings",
|
2016-11-22 05:48:55 +00:00
|
|
|
"get_shared_link_metadata" => "sharing/get_shared_link_metadata",
|
|
|
|
"revoke" => "auth/token/revoke"
|
|
|
|
);
|
2016-10-23 22:48:26 +00:00
|
|
|
|
2016-11-22 05:48:55 +00:00
|
|
|
/**
|
2018-01-29 21:18:09 +00:00
|
|
|
* the target path to get
|
2018-01-23 22:05:57 +00:00
|
|
|
*/
|
2016-11-22 05:48:55 +00:00
|
|
|
protected $targetPath = false;
|
|
|
|
protected $targetPathOriginal = false;
|
2016-10-23 22:48:26 +00:00
|
|
|
|
2016-11-22 05:48:55 +00:00
|
|
|
/**
|
|
|
|
* oauth token
|
2018-01-23 22:05:57 +00:00
|
|
|
*/
|
2018-01-29 21:18:09 +00:00
|
|
|
protected $oauthToken = array();
|
|
|
|
protected $token = 0;
|
2016-10-23 22:48:26 +00:00
|
|
|
|
2016-11-22 05:48:55 +00:00
|
|
|
/**
|
|
|
|
* the verious pathes we need
|
2018-01-23 22:05:57 +00:00
|
|
|
*/
|
2016-11-22 05:48:55 +00:00
|
|
|
protected $permissionType;
|
2016-10-23 22:48:26 +00:00
|
|
|
|
2016-11-22 05:48:55 +00:00
|
|
|
/**
|
2018-01-23 22:05:57 +00:00
|
|
|
* The array of queries for creating shared links
|
|
|
|
*/
|
|
|
|
protected $createSharedLinks = array();
|
2018-01-29 21:18:09 +00:00
|
|
|
protected $createSharedLinks_ = array(); // retry array
|
2018-01-23 22:05:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The array of queries for getting shared links
|
|
|
|
*/
|
|
|
|
protected $getSharedLinks = array();
|
2018-01-29 21:18:09 +00:00
|
|
|
protected $getSharedLinks_ = array(); // retry array
|
2016-10-23 22:48:26 +00:00
|
|
|
|
2016-11-22 05:48:55 +00:00
|
|
|
/**
|
|
|
|
* the success switch
|
2018-01-23 22:05:57 +00:00
|
|
|
*/
|
2016-11-22 05:48:55 +00:00
|
|
|
protected $succes;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the type call
|
2018-01-23 22:05:57 +00:00
|
|
|
*/
|
2016-11-22 05:48:55 +00:00
|
|
|
protected $type;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the query for the call
|
2018-01-23 22:05:57 +00:00
|
|
|
*/
|
2016-11-22 05:48:55 +00:00
|
|
|
protected $query;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the query for the call
|
2018-01-23 22:05:57 +00:00
|
|
|
*/
|
2016-11-22 05:48:55 +00:00
|
|
|
protected $model;
|
2016-10-23 22:48:26 +00:00
|
|
|
|
2018-01-29 21:18:09 +00:00
|
|
|
/**
|
|
|
|
* the slow down timer
|
|
|
|
*/
|
|
|
|
protected $slowdown = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the speed up switch
|
|
|
|
*/
|
|
|
|
protected $speedup = false;
|
|
|
|
|
2016-11-22 05:48:55 +00:00
|
|
|
/**
|
|
|
|
* the mediaData bucket
|
2018-01-23 22:05:57 +00:00
|
|
|
*/
|
2016-11-22 05:48:55 +00:00
|
|
|
public $mediaData = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the error messages
|
2018-01-23 22:05:57 +00:00
|
|
|
*/
|
2016-11-22 05:48:55 +00:00
|
|
|
public $error_summary = array();
|
2018-01-23 22:05:57 +00:00
|
|
|
|
2016-12-22 21:32:13 +00:00
|
|
|
/**
|
2018-01-23 22:05:57 +00:00
|
|
|
* force the update to reset
|
|
|
|
**/
|
2016-12-22 21:32:13 +00:00
|
|
|
public $forceReset = false;
|
2016-10-23 22:48:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*/
|
2016-11-22 05:48:55 +00:00
|
|
|
public function __construct(JModelLegacy $model, $buildType)
|
|
|
|
{
|
2018-01-23 22:05:57 +00:00
|
|
|
// set the first call
|
|
|
|
$this->firstCall = 'get'; // TODO (we may what to make this dynamic)
|
|
|
|
// set the second call
|
|
|
|
$this->secondCall = 'create'; // TODO (we may what to make this dynamic)
|
2016-11-22 05:48:55 +00:00
|
|
|
// set the url at this point for now
|
2018-01-23 22:05:57 +00:00
|
|
|
$this->url = $this->postUrl["protocol"].$this->postUrl["suddomain"].$this->postUrl["domain"].$this->postUrl["path"];
|
2016-11-22 05:48:55 +00:00
|
|
|
// set the local model
|
|
|
|
$this->model = $model;
|
|
|
|
// set the build type
|
|
|
|
$this->build = (int) $buildType;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* getFiles
|
|
|
|
*
|
|
|
|
* =============
|
|
|
|
* $details
|
|
|
|
* =============
|
2016-12-22 21:32:13 +00:00
|
|
|
* sourceID
|
2016-11-22 05:48:55 +00:00
|
|
|
* dropboxOption
|
|
|
|
* dropboxTarget
|
|
|
|
* addTypes
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function getFiles($token, $permissiontype, $details = array())
|
2016-10-23 22:48:26 +00:00
|
|
|
{
|
2016-11-22 05:48:55 +00:00
|
|
|
// we need more then the normal time to run this script 5 minutes at least.
|
|
|
|
ini_set('max_execution_time', 500);
|
2018-01-29 21:18:09 +00:00
|
|
|
// little tweak for big folders (use multy tokens)
|
|
|
|
if (strpos($token, '____') !== false)
|
|
|
|
{
|
|
|
|
$this->oauthToken = array_filter((array) explode('____', $token),
|
|
|
|
function ($string) {
|
|
|
|
return $this->checkString($string);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// set the oauth toke
|
|
|
|
$this->oauthToken = array($token);
|
|
|
|
}
|
2018-01-23 22:05:57 +00:00
|
|
|
|
2016-11-22 05:48:55 +00:00
|
|
|
// set the permission type
|
|
|
|
$this->permissionType = $permissiontype;
|
2018-01-23 22:05:57 +00:00
|
|
|
|
2016-11-22 05:48:55 +00:00
|
|
|
// set the details
|
|
|
|
if ($this->checkArray($details))
|
2016-10-23 22:48:26 +00:00
|
|
|
{
|
2016-11-22 05:48:55 +00:00
|
|
|
foreach ($details as $detail => $value)
|
2016-10-23 22:48:26 +00:00
|
|
|
{
|
2016-11-22 05:48:55 +00:00
|
|
|
$this->$detail = $value;
|
2016-10-23 22:48:26 +00:00
|
|
|
}
|
2016-11-22 05:48:55 +00:00
|
|
|
}
|
2018-01-29 21:18:09 +00:00
|
|
|
|
2016-11-22 05:48:55 +00:00
|
|
|
// set the curent folder path
|
|
|
|
if (!$this->setFolderPath())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// start the main factory that calles for the folder data
|
|
|
|
$this->query = array("path" => $this->targetPath, "recursive" => true, "include_media_info" => true);
|
|
|
|
$this->type = 'list_folder';
|
|
|
|
if ($this->makeCall())
|
|
|
|
{
|
2018-01-23 22:05:57 +00:00
|
|
|
if ($this->_isCurl())
|
|
|
|
{
|
|
|
|
// run the share link builder
|
|
|
|
return $this->makeMultiExecCalls();
|
|
|
|
}
|
2016-11-22 05:48:55 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-01-23 22:05:57 +00:00
|
|
|
return false;
|
2016-11-22 05:48:55 +00:00
|
|
|
}
|
2018-01-23 22:05:57 +00:00
|
|
|
|
2018-01-29 21:18:09 +00:00
|
|
|
protected function getToken()
|
|
|
|
{
|
|
|
|
$key = $this->token;
|
|
|
|
if (isset($this->oauthToken[$key]))
|
|
|
|
{
|
|
|
|
// update key
|
|
|
|
$this->token++;
|
|
|
|
return $this->oauthToken[$key];
|
|
|
|
}
|
|
|
|
// reset key
|
|
|
|
$this->token = 1;
|
|
|
|
// return first token
|
|
|
|
return $this->oauthToken[0];
|
|
|
|
}
|
|
|
|
|
2018-01-23 22:05:57 +00:00
|
|
|
protected function makeMultiExecCalls()
|
|
|
|
{
|
2018-01-29 21:18:09 +00:00
|
|
|
$this->succesCall = true;
|
2018-01-23 22:05:57 +00:00
|
|
|
// make the fist call
|
|
|
|
$this->multiSharedLinks($this->firstCall);
|
|
|
|
// make the second call
|
|
|
|
$this->multiSharedLinks($this->secondCall);
|
2018-01-29 21:18:09 +00:00
|
|
|
// slow down if needed
|
|
|
|
$this->speedup = false;
|
|
|
|
// for safety
|
|
|
|
if (($this->checkArray($this->createSharedLinks) || $this->checkArray($this->createSharedLinks_) ||
|
|
|
|
$this->checkArray($this->getSharedLinks) || $this->checkArray($this->getSharedLinks_)) &&
|
|
|
|
$this->succesCall)
|
|
|
|
{
|
|
|
|
return $this->makeMultiExecCalls();
|
|
|
|
}
|
|
|
|
// all done
|
|
|
|
return $this->succesCall;
|
2018-01-23 22:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function multiSharedLinks($type)
|
|
|
|
{
|
|
|
|
switch ($type)
|
|
|
|
{
|
|
|
|
case "create":
|
|
|
|
// great links if not made already
|
2018-01-29 21:18:09 +00:00
|
|
|
if ($this->checkArray($this->createSharedLinks) || $this->checkArray($this->createSharedLinks_))
|
2018-01-23 22:05:57 +00:00
|
|
|
{
|
|
|
|
$url = $this->url.$this->domainpath['create_shared_link_with_settings'];
|
|
|
|
$this->type = 'create_shared_link_with_settings';
|
|
|
|
// build return function
|
|
|
|
$storeSharedLink = function ($data, $target) {
|
2018-01-29 21:18:09 +00:00
|
|
|
// check if we are going to fast
|
|
|
|
if (isset($data->error_summary) && strpos($data->error_summary, 'too_many_requests') !== false)
|
|
|
|
{
|
|
|
|
$this->slowDown($data);
|
|
|
|
// this call was droped, so we must try again
|
|
|
|
$this->createSharedLinks_[] = json_encode(array("path" => $target, "settings" => array("requested_visibility" => "public")));
|
|
|
|
}
|
2018-01-23 22:05:57 +00:00
|
|
|
// check if link already made
|
2018-01-29 21:18:09 +00:00
|
|
|
elseif (isset($data->error_summary) && strpos($data->error_summary, 'shared_link_already_exists') !== false)
|
2018-01-23 22:05:57 +00:00
|
|
|
{
|
|
|
|
$this->getSharedLinks[] = json_encode(array("path" => $target));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$this->curlCallResult($data);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// run call
|
2018-01-29 21:18:09 +00:00
|
|
|
if ($this->checkArray($this->createSharedLinks))
|
|
|
|
{
|
|
|
|
$this->multiCall($url, 'createSharedLinks', $storeSharedLink);
|
|
|
|
}
|
|
|
|
// also run the retries
|
|
|
|
if ($this->checkArray($this->createSharedLinks_))
|
|
|
|
{
|
|
|
|
$this->multiCall($url, 'createSharedLinks_', $storeSharedLink);
|
|
|
|
}
|
2018-01-23 22:05:57 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "get":
|
|
|
|
// now get the already made links
|
2018-01-29 21:18:09 +00:00
|
|
|
if ($this->checkArray($this->getSharedLinks) || $this->checkArray($this->getSharedLinks_))
|
2018-01-23 22:05:57 +00:00
|
|
|
{
|
|
|
|
$url = $this->url.$this->domainpath['list_shared_links'];
|
|
|
|
$this->type = 'list_shared_links';
|
|
|
|
// build return function
|
|
|
|
$storeSharedLink = function ($data, $target) {
|
|
|
|
// check if link not made
|
2018-01-29 21:18:09 +00:00
|
|
|
if (isset($data->error_summary) && strpos($data->error_summary, 'too_many_requests') !== false)
|
|
|
|
{
|
|
|
|
$this->slowDown($data);
|
|
|
|
// this call was droped, so we must try again
|
|
|
|
$this->getSharedLinks_[] = json_encode(array("path" => $target));
|
|
|
|
}
|
|
|
|
elseif (isset($data->error_summary))
|
2018-01-23 22:05:57 +00:00
|
|
|
{
|
|
|
|
$this->createSharedLinks[] = json_encode(array("path" => $target, "settings" => array("requested_visibility" => "public")));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$this->curlCallResult($data);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// run call
|
2018-01-29 21:18:09 +00:00
|
|
|
if ($this->checkArray($this->getSharedLinks))
|
|
|
|
{
|
|
|
|
$this->multiCall($url, 'getSharedLinks', $storeSharedLink);
|
|
|
|
}
|
|
|
|
// also run the retries
|
|
|
|
if ($this->checkArray($this->getSharedLinks_))
|
|
|
|
{
|
|
|
|
$this->multiCall($url, 'getSharedLinks_', $storeSharedLink);
|
|
|
|
}
|
2018-01-23 22:05:57 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function multiCall(&$url, $type, &$storeSharedLink)
|
|
|
|
{
|
|
|
|
$timer = 1;
|
|
|
|
$options = array();
|
2018-01-29 21:18:09 +00:00
|
|
|
// set the options array and make the calls every 550
|
|
|
|
foreach ($this->{$type} as $query)
|
2018-01-23 22:05:57 +00:00
|
|
|
{
|
2018-01-29 21:18:09 +00:00
|
|
|
$options[] = array(CURLOPT_HTTPHEADER => array('Authorization: Bearer ' . $this->getToken(), 'Content-Type: application/json'), CURLOPT_POST => 1, CURLOPT_POSTFIELDS => $query); $timer++;
|
2018-01-23 22:05:57 +00:00
|
|
|
// check timer
|
|
|
|
if ($timer >= 550)
|
|
|
|
{
|
|
|
|
// run multi curl
|
|
|
|
$this->curlMultiExec($url, $options, $storeSharedLink);
|
|
|
|
// reset
|
|
|
|
$timer = 1;
|
|
|
|
$options = array();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// make sure all was done
|
2018-01-29 21:18:09 +00:00
|
|
|
if ($timer > 1 && $this->checkArray($options))
|
2018-01-23 22:05:57 +00:00
|
|
|
{
|
|
|
|
// run multi curl
|
|
|
|
$this->curlMultiExec($url, $options, $storeSharedLink);
|
|
|
|
}
|
|
|
|
// reset the values
|
2018-01-29 21:18:09 +00:00
|
|
|
$this->{$type} = array();
|
|
|
|
// check if we have errors
|
|
|
|
if ($this->checkArray($this->error_summary))
|
2018-01-23 22:05:57 +00:00
|
|
|
{
|
2018-01-29 21:18:09 +00:00
|
|
|
$this->succesCall = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function slowDown($data)
|
|
|
|
{
|
|
|
|
if (!$this->speedup)
|
|
|
|
{
|
|
|
|
// set default
|
|
|
|
$this->slowdown = 300000000;
|
|
|
|
// can we set it dynamicly
|
|
|
|
if (isset($data->error) && isset($data->error->retry_after))
|
|
|
|
{
|
|
|
|
// convert retry_after minutes to microseconds
|
|
|
|
$this->slowdown = (int) bcmul($data->error->retry_after, 1000000);
|
|
|
|
}
|
2018-01-23 22:05:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-22 05:48:55 +00:00
|
|
|
public function revokeToken($token = null)
|
|
|
|
{
|
|
|
|
if ($token)
|
|
|
|
{
|
|
|
|
// set the oauth toke
|
2018-01-29 21:18:09 +00:00
|
|
|
$this->oauthToken[] = $token;
|
2016-11-22 05:48:55 +00:00
|
|
|
}
|
|
|
|
// set the call to revoke the token
|
|
|
|
$this->query = 'null';
|
|
|
|
$this->type = 'revoke';
|
2018-01-29 21:18:09 +00:00
|
|
|
// remove all tokens
|
|
|
|
$removed = true;
|
|
|
|
if ($this->checkArray($this->oauthToken))
|
2016-11-22 05:48:55 +00:00
|
|
|
{
|
2018-01-29 21:18:09 +00:00
|
|
|
foreach($this->oauthToken as $token)
|
|
|
|
{
|
|
|
|
if (!$this->makeCall())
|
|
|
|
{
|
|
|
|
$removed = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $removed;
|
2016-10-23 22:48:26 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2018-01-23 22:05:57 +00:00
|
|
|
|
2016-11-22 05:48:55 +00:00
|
|
|
protected function setFolderPath()
|
2016-10-23 22:48:26 +00:00
|
|
|
{
|
2016-11-22 05:48:55 +00:00
|
|
|
if ('full' == $this->permissionType && isset($this->dropboxOption) && isset($this->dropboxTarget) && $this->checkString($this->dropboxTarget))
|
2016-10-23 22:48:26 +00:00
|
|
|
{
|
2016-11-22 05:48:55 +00:00
|
|
|
if (2 == $this->dropboxOption)
|
2016-10-23 22:48:26 +00:00
|
|
|
{
|
2016-11-22 05:48:55 +00:00
|
|
|
// simply set the path
|
2018-01-23 22:05:57 +00:00
|
|
|
$this->targetPath = '/'.trim(strtolower($this->dropboxTarget), '/');
|
|
|
|
|
2016-11-22 05:48:55 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
elseif (1 == $this->dropboxOption)
|
|
|
|
{
|
|
|
|
// make a call to get the path
|
|
|
|
$this->query = array("url" => $this->dropboxTarget);
|
|
|
|
$this->type = 'get_shared_link_metadata';
|
|
|
|
if ($this->makeCall())
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2016-10-23 22:48:26 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-22 05:48:55 +00:00
|
|
|
elseif ('app' == $this->permissionType)
|
|
|
|
{
|
|
|
|
$this->targetPath = "";
|
2018-01-23 22:05:57 +00:00
|
|
|
|
2016-11-22 05:48:55 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2016-10-23 22:48:26 +00:00
|
|
|
}
|
2018-01-23 22:05:57 +00:00
|
|
|
|
2016-11-22 05:48:55 +00:00
|
|
|
protected function makeCall()
|
2016-10-23 22:48:26 +00:00
|
|
|
{
|
2016-11-22 05:48:55 +00:00
|
|
|
if ($this->_isCurl())
|
2016-10-23 22:48:26 +00:00
|
|
|
{
|
2016-11-22 05:48:55 +00:00
|
|
|
return $this->makeCurlCall();
|
2016-10-23 22:48:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-11-22 05:48:55 +00:00
|
|
|
return $this->makeGetCall();
|
2016-10-23 22:48:26 +00:00
|
|
|
}
|
2016-11-22 05:48:55 +00:00
|
|
|
}
|
2018-01-23 22:05:57 +00:00
|
|
|
|
2016-11-22 05:48:55 +00:00
|
|
|
protected function makeGetCall()
|
2018-01-23 22:05:57 +00:00
|
|
|
{
|
2016-11-22 05:48:55 +00:00
|
|
|
$options = array(
|
|
|
|
'http' => array(
|
2018-01-23 22:05:57 +00:00
|
|
|
'header' => "Content-Type: application/json\r\n".
|
2018-01-29 21:18:09 +00:00
|
|
|
"Authorization: Bearer ".$this->getToken(),
|
2018-01-23 22:05:57 +00:00
|
|
|
'method' => "POST"
|
2016-12-22 21:32:13 +00:00
|
|
|
),
|
|
|
|
);
|
2018-01-23 22:05:57 +00:00
|
|
|
|
2016-12-22 21:32:13 +00:00
|
|
|
if ($this->checkArray($this->query))
|
|
|
|
{
|
|
|
|
$this->query = json_encode($this->query);
|
|
|
|
}
|
2018-01-23 22:05:57 +00:00
|
|
|
// add the query
|
|
|
|
if ($this->checkString($this->query))
|
|
|
|
{
|
|
|
|
$options['http']['content'] = $this->query;
|
|
|
|
}
|
2016-11-22 05:48:55 +00:00
|
|
|
$context = stream_context_create($options);
|
2018-01-23 22:05:57 +00:00
|
|
|
$response = file_get_contents($this->url.$this->domainpath[$this->type], false, $context);
|
2016-11-22 05:48:55 +00:00
|
|
|
|
2017-12-25 12:46:35 +00:00
|
|
|
// store the result
|
|
|
|
return $this->getCallResult($response);
|
|
|
|
}
|
2018-01-23 22:05:57 +00:00
|
|
|
|
2017-12-25 12:46:35 +00:00
|
|
|
protected function getCallResult($response)
|
|
|
|
{
|
2016-11-22 05:48:55 +00:00
|
|
|
if ($response === FALSE)
|
2016-10-23 22:48:26 +00:00
|
|
|
{
|
2018-01-23 22:05:57 +00:00
|
|
|
$this->error_summary[] = $this->type.'_error';
|
2016-11-22 05:48:55 +00:00
|
|
|
return false;
|
2016-10-23 22:48:26 +00:00
|
|
|
}
|
2017-12-25 12:46:35 +00:00
|
|
|
// store the result
|
|
|
|
return $this->setTheResult(json_decode($response));
|
2016-11-22 05:48:55 +00:00
|
|
|
}
|
2018-01-23 22:05:57 +00:00
|
|
|
|
2016-11-22 05:48:55 +00:00
|
|
|
protected function makeCurlCall()
|
|
|
|
{
|
2018-01-23 22:05:57 +00:00
|
|
|
// do not run creat shared link this way
|
2018-01-29 21:18:09 +00:00
|
|
|
$headers = array('Authorization: Bearer '. $this->getToken(),
|
2018-01-23 22:05:57 +00:00
|
|
|
'Content-Type: application/json'
|
|
|
|
);
|
2017-12-25 12:46:35 +00:00
|
|
|
|
2018-01-23 22:05:57 +00:00
|
|
|
$ch = curl_init($this->url.$this->domainpath[$this->type]);
|
2017-12-25 12:46:35 +00:00
|
|
|
|
2016-11-22 05:48:55 +00:00
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
2016-12-22 21:32:13 +00:00
|
|
|
// check if query is set
|
|
|
|
if ($this->checkArray($this->query))
|
|
|
|
{
|
|
|
|
$this->query = json_encode($this->query);
|
|
|
|
}
|
2018-01-23 22:05:57 +00:00
|
|
|
// add the query
|
|
|
|
if ($this->checkString($this->query))
|
|
|
|
{
|
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->query);
|
|
|
|
}
|
2016-11-22 05:48:55 +00:00
|
|
|
curl_setopt($ch, CURLOPT_POST, true);
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
// curl_setopt($ch, CURLOPT_VERBOSE, 1); // debug
|
2017-12-25 12:46:35 +00:00
|
|
|
|
2016-11-22 05:48:55 +00:00
|
|
|
$response = curl_exec($ch);
|
2017-12-25 12:46:35 +00:00
|
|
|
|
2016-11-22 05:48:55 +00:00
|
|
|
curl_close($ch);
|
2017-12-25 12:46:35 +00:00
|
|
|
|
|
|
|
// store the result
|
|
|
|
return $this->curlCallResult($response);
|
|
|
|
}
|
2018-01-23 22:05:57 +00:00
|
|
|
|
2017-12-25 12:46:35 +00:00
|
|
|
public function curlCallResult($response)
|
2018-01-23 22:05:57 +00:00
|
|
|
{
|
2016-11-22 05:48:55 +00:00
|
|
|
if ($this->checkJson($response))
|
2016-10-23 22:48:26 +00:00
|
|
|
{
|
2016-11-22 05:48:55 +00:00
|
|
|
$response = json_decode($response);
|
2016-10-23 22:48:26 +00:00
|
|
|
}
|
2016-11-22 05:48:55 +00:00
|
|
|
// store the result
|
|
|
|
return $this->setTheResult($response);
|
2016-10-23 22:48:26 +00:00
|
|
|
}
|
2018-01-23 22:05:57 +00:00
|
|
|
|
2016-11-22 05:48:55 +00:00
|
|
|
protected function setTheResult($data)
|
2016-10-23 22:48:26 +00:00
|
|
|
{
|
2016-11-22 05:48:55 +00:00
|
|
|
// if there was an error stop!!!
|
|
|
|
if (isset($data->error_summary))
|
2016-10-23 22:48:26 +00:00
|
|
|
{
|
2016-11-22 05:48:55 +00:00
|
|
|
$this->error_summary[] = $data->error_summary;
|
2016-12-22 21:32:13 +00:00
|
|
|
$this->forceReset = true;
|
2016-11-22 05:48:55 +00:00
|
|
|
return false;
|
2016-10-23 22:48:26 +00:00
|
|
|
}
|
2018-01-23 22:05:57 +00:00
|
|
|
|
2016-11-22 05:48:55 +00:00
|
|
|
// deal with each type
|
|
|
|
switch ($this->type)
|
2016-10-23 22:48:26 +00:00
|
|
|
{
|
2018-01-23 22:05:57 +00:00
|
|
|
case "list_folder":
|
2016-11-22 05:48:55 +00:00
|
|
|
case "list_folder_continue":
|
|
|
|
if (isset($data->entries) && $this->checkArray($data->entries))
|
|
|
|
{
|
|
|
|
if ($this->storeFiles($data->entries))
|
|
|
|
{
|
|
|
|
// run the continue if needed
|
|
|
|
if (isset($data->has_more) && $data->has_more && isset($data->cursor))
|
|
|
|
{
|
|
|
|
// start the main factory that calles for the folder data
|
|
|
|
$this->query = array("cursor" => $data->cursor);
|
|
|
|
$this->type = 'list_folder_continue';
|
|
|
|
if ($this->makeCall())
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2018-01-23 22:05:57 +00:00
|
|
|
$this->error_summary[] = $this->type.'_error';
|
|
|
|
break;
|
|
|
|
case "list_shared_links":
|
|
|
|
if (isset($data->links) && $this->checkArray($data->links))
|
|
|
|
{
|
|
|
|
foreach ($data->links as $link)
|
|
|
|
{
|
|
|
|
if (!$this->storeSharedLink($link))
|
|
|
|
{
|
|
|
|
// we could not stored the link
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
$this->error_summary[] = $this->type.'_error';
|
2016-11-22 05:48:55 +00:00
|
|
|
break;
|
2018-01-23 22:05:57 +00:00
|
|
|
case "create_shared_link_with_settings":
|
|
|
|
if ($this->storeSharedLink($data))
|
2016-11-22 05:48:55 +00:00
|
|
|
{
|
|
|
|
// we stored the link
|
|
|
|
return true;
|
|
|
|
}
|
2018-01-23 22:05:57 +00:00
|
|
|
$this->error_summary[] = $this->type.'_error';
|
2016-11-22 05:48:55 +00:00
|
|
|
break;
|
|
|
|
case "get_shared_link_metadata":
|
|
|
|
if (isset($data->path_lower))
|
|
|
|
{
|
|
|
|
$this->targetPath = $data->path_lower;
|
|
|
|
return true;
|
|
|
|
}
|
2018-01-23 22:05:57 +00:00
|
|
|
$this->error_summary[] = $this->type.'_error';
|
2016-11-22 05:48:55 +00:00
|
|
|
break;
|
|
|
|
case "revoke":
|
|
|
|
if (is_null($data))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2018-01-23 22:05:57 +00:00
|
|
|
$this->error_summary[] = $this->type.'_error';
|
2016-11-22 05:48:55 +00:00
|
|
|
break;
|
2016-10-23 22:48:26 +00:00
|
|
|
}
|
2016-12-22 21:32:13 +00:00
|
|
|
$this->forceReset = true;
|
2016-10-23 22:48:26 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-01-23 22:05:57 +00:00
|
|
|
|
|
|
|
protected function storeSharedLink($data)
|
2016-10-23 22:48:26 +00:00
|
|
|
{
|
2016-11-22 05:48:55 +00:00
|
|
|
// we need to store the url to DB
|
2018-01-23 22:05:57 +00:00
|
|
|
if (isset($data->url) && isset($data->name) && isset($data->size) && (isset($data->path) || isset($data->path_lower)))
|
2016-10-23 22:48:26 +00:00
|
|
|
{
|
2018-01-23 22:05:57 +00:00
|
|
|
$path = (isset($data->path)) ? $data->path : $data->path_lower;
|
|
|
|
$localListing = array();
|
|
|
|
$localListing['id'] = 0;
|
|
|
|
$localListing['name'] = $data->name;
|
|
|
|
$localListing['size'] = $data->size;
|
|
|
|
$localListing['key'] = $this->fixPath($path);
|
|
|
|
$localListing['url'] = str_replace('?dl=0','?dl=1',$data->url);
|
|
|
|
$localListing['build'] = $this->build;
|
|
|
|
$localListing['external_source'] = (int) $this->sourceID;
|
2016-11-22 05:48:55 +00:00
|
|
|
// check if item already set
|
2018-01-23 22:05:57 +00:00
|
|
|
if ($id = $this->model->searchForId($localListing['key']))
|
2016-11-22 05:48:55 +00:00
|
|
|
{
|
|
|
|
// update item
|
|
|
|
$localListing['id'] = $id;
|
|
|
|
}
|
|
|
|
return $this->model->save($localListing);
|
2016-10-23 22:48:26 +00:00
|
|
|
}
|
2016-11-22 05:48:55 +00:00
|
|
|
return false;
|
2016-10-23 22:48:26 +00:00
|
|
|
}
|
|
|
|
|
2016-11-22 05:48:55 +00:00
|
|
|
protected function storeFiles($entries)
|
2018-01-23 22:05:57 +00:00
|
|
|
{
|
2016-11-22 05:48:55 +00:00
|
|
|
foreach ($entries as $item)
|
2018-01-23 22:05:57 +00:00
|
|
|
{
|
2016-11-22 05:48:55 +00:00
|
|
|
if (isset($item->{'.tag'}) && 'file' == $item->{'.tag'} && isset($item->name))
|
2016-10-23 22:48:26 +00:00
|
|
|
{
|
2016-11-22 05:48:55 +00:00
|
|
|
$addLink = false;
|
|
|
|
// remove if not related to type
|
|
|
|
if (isset($this->addTypes) && $this->checkArray($this->addTypes))
|
2016-10-23 22:48:26 +00:00
|
|
|
{
|
2016-11-22 05:48:55 +00:00
|
|
|
foreach ($this->addTypes as $add)
|
2016-10-23 22:48:26 +00:00
|
|
|
{
|
2018-01-23 22:05:57 +00:00
|
|
|
if (strpos($item->name,$add) !== false)
|
2016-10-23 22:48:26 +00:00
|
|
|
{
|
2016-11-22 05:48:55 +00:00
|
|
|
$addLink = true;
|
2016-10-23 22:48:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-22 05:48:55 +00:00
|
|
|
if ($addLink && isset($item->path_lower))
|
|
|
|
{
|
2018-01-23 22:05:57 +00:00
|
|
|
// set based on first call
|
|
|
|
if ('get' === $this->firstCall)
|
|
|
|
{
|
|
|
|
// first check if shared link exist
|
|
|
|
$this->query = array("path" => $item->path_lower);
|
|
|
|
// set the type of call
|
|
|
|
$this->type = 'list_shared_links';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// first check if shared link exist
|
|
|
|
$this->query = array("path" => $item->path_lower, "settings" => array("requested_visibility" => "public"));
|
|
|
|
// set the type of call
|
|
|
|
$this->type = 'create_shared_link_with_settings';
|
|
|
|
}
|
|
|
|
// if we have curl the use multi curl execution
|
|
|
|
if ($this->_isCurl())
|
|
|
|
{
|
|
|
|
// set query to worker
|
|
|
|
$this->{$this->firstCall."SharedLinks"}[] = json_encode($this->query);
|
|
|
|
}
|
|
|
|
elseif (!$this->makeCall())
|
2016-11-22 05:48:55 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-10-23 22:48:26 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-22 05:48:55 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-01-23 22:05:57 +00:00
|
|
|
|
2016-11-22 05:48:55 +00:00
|
|
|
protected function fixPath($path)
|
|
|
|
{
|
|
|
|
if ($this->checkString($this->targetPath))
|
|
|
|
{
|
|
|
|
$path = str_replace($this->targetPath, 'VDM_pLeK_h0uEr', $path);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-01-23 22:05:57 +00:00
|
|
|
$path = 'VDM_pLeK_h0uEr'.$path;
|
2016-11-22 05:48:55 +00:00
|
|
|
}
|
|
|
|
return $path;
|
2016-10-23 22:48:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function checkObject($object)
|
|
|
|
{
|
|
|
|
if (isset($object) && is_object($object) && count($object) > 0)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function checkArray($array)
|
|
|
|
{
|
|
|
|
if (isset($array) && is_array($array) && count($array) > 0)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2018-01-23 22:05:57 +00:00
|
|
|
|
2016-11-22 05:48:55 +00:00
|
|
|
protected function checkJson($string)
|
|
|
|
{
|
|
|
|
if ($this->checkString($string))
|
|
|
|
{
|
|
|
|
json_decode($string);
|
|
|
|
return (json_last_error() === JSON_ERROR_NONE);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2016-10-23 22:48:26 +00:00
|
|
|
|
|
|
|
protected function checkString($string)
|
|
|
|
{
|
|
|
|
if (isset($string) && is_string($string) && strlen($string) > 0)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2018-01-23 22:05:57 +00:00
|
|
|
|
2016-11-22 05:48:55 +00:00
|
|
|
protected function _isCurl()
|
|
|
|
{
|
|
|
|
return function_exists('curl_version');
|
|
|
|
}
|
2018-01-23 22:05:57 +00:00
|
|
|
|
|
|
|
protected function curlMultiExec(&$url, &$_options, $callback = null)
|
|
|
|
{
|
|
|
|
if ($this->checkString($url))
|
|
|
|
{
|
2018-01-29 21:18:09 +00:00
|
|
|
// check if we should slow down
|
|
|
|
if ($this->slowdown > 0)
|
|
|
|
{
|
|
|
|
usleep($this->slowdown);
|
|
|
|
// reset the trafic speed
|
|
|
|
$this->slowdown = 0;
|
|
|
|
$this->speedup = true; // we should only sleep once per/bunch
|
|
|
|
}
|
2018-01-23 22:05:57 +00:00
|
|
|
// make sure the thread size isn't greater than the # of _options
|
2018-01-29 21:18:09 +00:00
|
|
|
$threadSize = 50;
|
2018-01-23 22:05:57 +00:00
|
|
|
$threadSize = (sizeof($_options) < $threadSize) ? sizeof($_options) : $threadSize;
|
|
|
|
// set the options
|
|
|
|
$options = array();
|
|
|
|
$options[CURLOPT_URL] = $url;
|
|
|
|
$options[CURLOPT_USERAGENT] = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12';
|
|
|
|
$options[CURLOPT_RETURNTRANSFER] = TRUE;
|
|
|
|
$options[CURLOPT_SSL_VERIFYPEER] = FALSE;
|
|
|
|
// start multi threading :)
|
|
|
|
$handle = curl_multi_init();
|
|
|
|
// start the first batch of requests
|
|
|
|
for ($i = 0; $i < $threadSize; $i++)
|
|
|
|
{
|
|
|
|
if (isset($_options[$i]))
|
|
|
|
{
|
|
|
|
$ch = curl_init();
|
|
|
|
foreach ($_options[$i] as $curlopt => $string)
|
|
|
|
{
|
|
|
|
$options[$curlopt] = $string;
|
|
|
|
}
|
|
|
|
curl_setopt_array($ch, $options);
|
|
|
|
curl_multi_add_handle($handle, $ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// we wait for all the calls to finish (should not take long)
|
|
|
|
do {
|
|
|
|
while(($execrun = curl_multi_exec($handle, $working)) == CURLM_CALL_MULTI_PERFORM);
|
|
|
|
if($execrun != CURLM_OK)
|
|
|
|
break;
|
|
|
|
// a request was just completed -- find out which one
|
|
|
|
while($done = curl_multi_info_read($handle))
|
|
|
|
{
|
|
|
|
if (is_callable($callback))
|
|
|
|
{
|
|
|
|
// $info = curl_getinfo($done['handle']);
|
|
|
|
// request successful. process output using the callback function.
|
|
|
|
$output = curl_multi_getcontent($done['handle']);
|
2018-01-29 21:18:09 +00:00
|
|
|
if ($this->checkJson($output) && isset($_options[$i]))
|
|
|
|
{
|
|
|
|
$callback(json_decode($output), json_decode(end($_options[$i]))->path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// check if we should slow down
|
|
|
|
if ($this->slowdown > 0)
|
|
|
|
{
|
|
|
|
usleep($this->slowdown);
|
|
|
|
// reset the trafic speed
|
|
|
|
$this->slowdown = 0;
|
|
|
|
$this->speedup = true; // we should only sleep once per/bunch
|
2018-01-23 22:05:57 +00:00
|
|
|
}
|
2018-01-29 21:18:09 +00:00
|
|
|
// next key
|
2018-01-23 22:05:57 +00:00
|
|
|
$key = $i + 1;
|
|
|
|
if(isset($_options[$key]))
|
|
|
|
{
|
|
|
|
// start a new request (it's important to do this before removing the old one)
|
|
|
|
$ch = curl_init(); $i++;
|
|
|
|
// add options
|
|
|
|
foreach ($_options[$key] as $curlopt => $string)
|
|
|
|
{
|
|
|
|
$options[$curlopt] = $string;
|
|
|
|
}
|
|
|
|
curl_setopt_array($ch, $options);
|
|
|
|
curl_multi_add_handle($handle, $ch);
|
|
|
|
// remove options again
|
|
|
|
foreach ($_options[$key] as $curlopt => $string)
|
|
|
|
{
|
|
|
|
unset($options[$curlopt]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// remove the curl handle that just completed
|
|
|
|
curl_multi_remove_handle($handle, $done['handle']);
|
|
|
|
}
|
|
|
|
// stop wasting CPU cycles and rest for a couple ms
|
|
|
|
usleep(10000);
|
|
|
|
} while ($working);
|
|
|
|
// close the curl multi thread
|
|
|
|
curl_multi_close($handle);
|
|
|
|
// okay done
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2016-10-23 22:48:26 +00:00
|
|
|
}
|