diff --git a/Fabric/ServiceFabric.php b/Fabric/ServiceFabric.php index adc1c93..cdf68a0 100755 --- a/Fabric/ServiceFabric.php +++ b/Fabric/ServiceFabric.php @@ -1,7 +1,6 @@ $type, 'token_data' => $result); - return $data; - + return array('service' => $type, 'token_data' => $result); } - } @@ -50,19 +42,17 @@ public static function uploadFile($type, $access_token, $uploadFile, $fileName, } switch($type){ - case SELF::DROPBOX: + case self::DROPBOX: if(!isset($access_token)) { return array('status' => 'error', 'msg' => 'deniedByUser'); } - $result = array(); try { $result = \UploadModels\DropBoxModel::uploadFile($access_token, $uploadFile, $fileName, $config); - }catch (dbx\Exception $e){ + }catch (\Exception $e){ $result = array('status' => 'error', 'msg' => 'Cloud Error'); } break; - case SELF::GOOGLEDRIVE: - $result = array(); + case self::GOOGLEDRIVE: if(!isset($access_token)) { return array('status' => 'error', 'msg' => 'deniedByUser'); } @@ -73,22 +63,20 @@ public static function uploadFile($type, $access_token, $uploadFile, $fileName, $result = array('status' => 'error', 'msg' => 'Cloud Error'); } break; - case SELF::BOX: + case self::BOX: if(!isset($access_token)) { return array('status' => 'error', 'msg' => 'deniedByUser'); } - $result = array(); try { $result = \UploadModels\BoxModel::uploadFile($access_token, $uploadFile, $fileName, $config); } catch(\Exception $e){ $result = array('status' => 'error', 'msg' => 'Cloud Error'); } break; - case SELF::ONEDRIVE: + case self::ONEDRIVE: if(!isset($access_token)) { return array('status' => 'error', 'msg' => 'deniedByUser'); } - $result = array(); try { $result = \UploadModels\OneDriveModel::uploadFile($access_token, $uploadFile, $fileName, $config); } catch(\Exception $e){ @@ -103,20 +91,19 @@ public static function getToken($type, $config){ $result = ''; switch($type){ - case SELF::DROPBOX: + case self::DROPBOX: $result = \UploadModels\DropBoxModel::getToken($config); break; - case SELF::GOOGLEDRIVE: + case self::GOOGLEDRIVE: $result = \UploadModels\GoogleDriveModel::getToken($config); break; - case SELF::BOX: + case self::BOX: $result = \UploadModels\BoxModel::getToken($config); break; - case SELF::ONEDRIVE: + case self::ONEDRIVE: $result = \UploadModels\OneDriveModel::getToken($config); break; } return $result; } - -} \ No newline at end of file +} diff --git a/UploadModels/DropBoxModel.php b/UploadModels/DropBoxModel.php index 0d67aa3..27f8fbf 100755 --- a/UploadModels/DropBoxModel.php +++ b/UploadModels/DropBoxModel.php @@ -2,87 +2,206 @@ namespace UploadModels; -use \Dropbox as dbx; -use Dropbox\Exception; -use HttpReceiver\HttpRecieiver; - - -class DropBoxModel implements \Interfaces\UploadServiceInterface{ +use Exception; +use HttpReceiver\HttpReceiver; +use Interfaces\UploadServiceInterface; +use Kunnu\Dropbox\Dropbox; +use Kunnu\Dropbox\DropboxApp; +use Kunnu\Dropbox\Exceptions\DropboxClientException; + +class DropBoxModel implements UploadServiceInterface +{ + const CONFIG_CLIENT_ID = 'DROPBOX_KEY'; + const CONFIG_CLIENT_SECRET = 'DROPBOX_SECRET'; + const CONFIG_REDIRECT_URL = 'DROPBOX_REDIRECT_URI'; + + /** + * @var DropboxApp + */ + protected $app; + + /** + * @var Dropbox + */ + private $service; + /** + * @var string + */ + protected $redirectUrl; + /** + * @var string + */ + private $clientId; + /** + * @var string + */ + private $clientSecret; + /** + * @var null|string + */ + private $accessToken; + + /** + * DropBoxModel constructor. + * @param string $clientId + * @param string $clientSecret + * @param string $redirectUrl + * @param string|null $accessToken + */ + private function __construct($clientId, $clientSecret, $redirectUrl, $accessToken = null) + { + $this->clientId = $clientId; + $this->clientSecret = $clientSecret; + $this->redirectUrl = $redirectUrl; + $this->accessToken = $accessToken; + } - public static function auth($state, $config) { + private function init(array $serviceConfig = []) { + $this->app = new DropboxApp($this->clientId, $this->clientSecret, $this->accessToken); + $this->service = new Dropbox($this->app, $serviceConfig); + } - $authorizeUrl = self::getDropBoxAuth($config)->start(\HttpReceiver\HttpReceiver::get('userId','string')); - return $authorizeUrl; + /** + * @param int $userId + * @param array $config + * @return string + */ + public static function auth($userId, $config) + { + return self::create($config)->getAuthUrl($userId); } - public static function getToken($config) { - try { - list($accessToken, $userId, $urlState) = self::getDropBoxAuth($config)->finish($_GET); - if (isset($accessToken) && strlen($accessToken) > 0) { - return $accessToken; - } - }catch(dbx\Exception_BadRequest $e){ + /** + * @param array $config + * @return string + */ + public static function getToken($config) + { + if (!isset($_GET['code']) || !isset($_GET['state'])) { + return ''; + } + $code = $_GET['code']; + $state = $_GET['state']; + try { + return self::create($config)->getAccessToken($code, $state); + } catch (DropboxClientException $exception) { + return ''; } - return ''; } - - public static function uploadFile($access_token, $uploadFile, $fileName, $config) { - if(!isset($access_token)){ - return array('status' => 'error', 'msg' => 'refreshToken', 'url' => self::auth(\HttpReceiver\HttpReceiver::get('userId','int'), $config)); + /** + * @param string $access_token + * @param string $uploadFile + * @param string $fileName + * @param array $config + * @return array + */ + public static function uploadFile($access_token, $uploadFile, $fileName, $config) + { + if (empty($access_token)) { + return [ + 'status' => 'error', + 'msg' => 'refreshToken', + 'url' => self::auth(HttpReceiver::get('userId', 'int'), $config) + ]; } - if(!file_exists($uploadFile)) { - return array('status' => 'error', 'fileNotExist'); + if (!file_exists($uploadFile)) { + return ['status' => 'error', 'fileNotExist']; } - - - $dbxClient = new dbx\Client($access_token, "PHP-Example/1.0"); - - - $f = fopen($uploadFile, "rb"); - try { - if (!isset($fileName) || strlen($fileName) == 0 || $fileName == '0') { - $tmp = explode('/', $uploadFile); - $fileName = $tmp[sizeof($tmp) - 1]; - }else{ - $fileName .= '.'.self::getExtension($uploadFile); - } - - $result = $dbxClient->uploadFile("/".$config['SAVE_FOLDER']."/". $fileName, dbx\WriteMode::add(), $f); - }catch(Exception $e){ - return array('status' => 'error', 'msg' => 'refreshToken', 'url' => self::auth(\HttpReceiver\HttpReceiver::get('userId','int'), $config)); + if (empty($fileName) && $fileName !== '0') { + $remoteFilename = self::generateRemoteFilename($uploadFile); + } else { + $remoteFilename = $fileName .'.' . self::getExtension($uploadFile); } + $remotePath = "/" . $config['SAVE_FOLDER'] . "/" . $remoteFilename; - fclose($f); - if(!isset($result) || !isset($result['size'])){ - return array('status' => 'error', 'msg' => 'refreshToken'); - }else { - return array('status' => 'ok'); + try { + self::create($config, $access_token)->upload($uploadFile, $remotePath); + return ['status' => 'ok']; + } catch (Exception $e) { + return [ + 'status' => 'error', + 'msg' => 'refreshToken', + 'url' => self::create($config, $access_token)->getAuthUrl(HttpReceiver::get('userId', 'int')), + ]; } - } + /** + * @param int $userId + * @return string + */ + private function getAuthUrl($userId) + { + return $this->service->getAuthHelper()->getAuthUrl($this->redirectUrl, [], $userId); + } - private static function getDropBoxAuth($config) { - - $data = array('key' => $config['DROPBOX_KEY'], 'secret' => $config['DROPBOX_SECRET']); + /** + * @param string $code + * @param string $state + * @return string + */ + private function getAccessToken($code, $state) + { + return $this->service->getAuthHelper()->getAccessToken($code, $state, $this->redirectUrl)->getToken(); + } - $appInfo = dbx\AppInfo::loadFromJson($data); - $clientIdentifier = "my-app/1.0"; - $redirectUri = $config['DROPBOX_REDIRECT_URI']; - $csrfTokenStore = new dbx\ArrayEntryStore($_ENV, 'dropbox-auth-csrf-token'); + /** + * @param string $localFilename + * @param string $remoteFilename + */ - return new dbx\WebAuth($appInfo, $clientIdentifier, $redirectUri, $csrfTokenStore); + private function upload($localFilename, $remoteFilename) + { + $this->service->upload($localFilename, $remoteFilename, ['autorename' => true]); } - private static function getExtension($path){ - $ext = pathinfo($path,PATHINFO_EXTENSION); - if(strpos($path,'&') > -1){ - $ext = reset(explode('&',$ext)); + /** + * @param string $path + * @return string + */ + private static function getExtension($path) + { + $ext = pathinfo($path, PATHINFO_EXTENSION); + if (strpos($path, '&') > -1) { + $ext = reset(explode('&', $ext)); } return $ext; } -} \ No newline at end of file + /** + * @param string $localPath + * @return string + */ + private static function generateRemoteFilename($localPath) + { + $parts = explode('/', $localPath); + return end($parts); + } + + /** + * @param array $config + * @param string|null $accessToken + * @return self + */ + private static function create($config, $accessToken = null) + { + $self = new self( + $config[self::CONFIG_CLIENT_ID], + $config[self::CONFIG_CLIENT_SECRET], + $config[self::CONFIG_REDIRECT_URL], + $accessToken + ); + /** @see \Kunnu\Dropbox\Dropbox::__construct() */ + $serviceConfig = array_intersect_key($config, [ + 'http_client_handler' => null, + 'random_string_generator' => null, + 'persistent_data_store' => null + ]); + $self->init($serviceConfig); + + return $self; + } +} diff --git a/UploadModels/OneDriveModel.php b/UploadModels/OneDriveModel.php index 8ffbbb5..0f7c4ab 100755 --- a/UploadModels/OneDriveModel.php +++ b/UploadModels/OneDriveModel.php @@ -1,7 +1,6 @@ fetchObject($folderId); try { - $file = $parent->createFile($fileName, file_get_contents($uploadFile)); - }catch(Exception $e){ + $parent->createFile($fileName, file_get_contents($uploadFile)); + }catch(\Exception $e){ return array('status' => 'error', 'msg' => 'refreshToken', 'url' => self::auth($userId, $config)); } return array('status' => 'ok'); @@ -128,4 +127,4 @@ private static function getExtension($path){ return $ext; } -} \ No newline at end of file +} diff --git a/composer.json b/composer.json index 964da95..6b8559d 100755 --- a/composer.json +++ b/composer.json @@ -25,7 +25,7 @@ "require": { "krizalys/onedrive-php-sdk": "1.0.0", "pdffiller/boxphpapi": "^1.0", - "dropbox/dropbox-sdk": "1.1.*", - "google/apiclient": "2.0.*" + "google/apiclient": "2.0.*", + "kunalvarma05/dropbox-php-sdk": "^0.2.1" } } diff --git a/composer.lock b/composer.lock index 4803e7e..63fcc59 100644 --- a/composer.lock +++ b/composer.lock @@ -4,48 +4,8 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "bb6b21e51f204fa37cc9233c1836483d", - "content-hash": "4d0c7c929e8730abc06be2014f7a18bd", + "content-hash": "394a03dc79487d711cebdb5b126b4a3f", "packages": [ - { - "name": "dropbox/dropbox-sdk", - "version": "v1.1.7", - "source": { - "type": "git", - "url": "https://github.com/dropbox/dropbox-sdk-php.git", - "reference": "0f1a178ca9c0271bca6426dde8f5a2241578deae" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/dropbox/dropbox-sdk-php/zipball/0f1a178ca9c0271bca6426dde8f5a2241578deae", - "reference": "0f1a178ca9c0271bca6426dde8f5a2241578deae", - "shasum": "" - }, - "require": { - "ext-curl": "*", - "php": ">= 5.3" - }, - "require-dev": { - "apigen/apigen": "4.1.2", - "phpunit/phpunit": "~4.0", - "squizlabs/php_codesniffer": "2.0.0RC3" - }, - "type": "library", - "autoload": { - "psr-0": { - "Dropbox": "lib/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "description": "Dropbox SDK for PHP", - "keywords": [ - "dropbox" - ], - "time": "2016-08-08 23:48:49" - }, { "name": "firebase/php-jwt", "version": "v3.0.0", @@ -87,7 +47,7 @@ ], "description": "A simple library to encode and decode JSON Web Tokens (JWT) in PHP. Should conform to the current spec.", "homepage": "https://github.com/firebase/php-jwt", - "time": "2015-07-22 18:31:08" + "time": "2015-07-22T18:31:08+00:00" }, { "name": "google/apiclient", @@ -149,7 +109,7 @@ "source": "https://github.com/google/google-api-php-client/tree/v2.0.3", "issues": "https://github.com/google/google-api-php-client/issues" }, - "time": "2016-09-14 19:33:35" + "time": "2016-09-14T19:33:35+00:00" }, { "name": "google/apiclient-services", @@ -183,7 +143,7 @@ "keywords": [ "google" ], - "time": "2016-09-14 19:12:27" + "time": "2016-09-14T19:12:27+00:00" }, { "name": "google/auth", @@ -231,7 +191,7 @@ "google", "oauth2" ], - "time": "2016-08-02 22:00:48" + "time": "2016-08-02T22:00:48+00:00" }, { "name": "guzzlehttp/guzzle", @@ -293,7 +253,7 @@ "rest", "web service" ], - "time": "2017-02-28 22:50:30" + "time": "2017-02-28T22:50:30+00:00" }, { "name": "guzzlehttp/promises", @@ -344,7 +304,7 @@ "keywords": [ "promise" ], - "time": "2016-12-20 10:07:11" + "time": "2016-12-20T10:07:11+00:00" }, { "name": "guzzlehttp/psr7", @@ -409,7 +369,7 @@ "uri", "url" ], - "time": "2017-03-20 17:10:46" + "time": "2017-03-20T17:10:46+00:00" }, { "name": "krizalys/onedrive-php-sdk", @@ -450,7 +410,55 @@ "php", "sdk" ], - "time": "2015-11-22 09:58:28" + "time": "2015-11-22T09:58:28+00:00" + }, + { + "name": "kunalvarma05/dropbox-php-sdk", + "version": "v0.2.1", + "source": { + "type": "git", + "url": "https://github.com/kunalvarma05/dropbox-php-sdk.git", + "reference": "27036f20b7eeb59d5e559e03d4280851f6eb789b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/kunalvarma05/dropbox-php-sdk/zipball/27036f20b7eeb59d5e559e03d4280851f6eb789b", + "reference": "27036f20b7eeb59d5e559e03d4280851f6eb789b", + "shasum": "" + }, + "require": { + "guzzlehttp/guzzle": "~6.0", + "tightenco/collect": "^5.2" + }, + "require-dev": { + "phpunit/phpunit": "^6.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Kunnu\\Dropbox\\": "src/Dropbox" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Kunal Varma", + "email": "kunalvarma05@gmail.com" + } + ], + "description": "Dropbox PHP API V2 SDK (Unofficial)", + "keywords": [ + "api", + "client", + "dropbox", + "php", + "sdk", + "unofficial" + ], + "time": "2017-06-26T09:37:16+00:00" }, { "name": "monolog/monolog", @@ -528,7 +536,7 @@ "logging", "psr-3" ], - "time": "2017-03-13 07:08:03" + "time": "2017-03-13T07:08:03+00:00" }, { "name": "pdffiller/boxphpapi", @@ -568,7 +576,7 @@ "source": "https://github.com/pdffiller/Box-SDK/tree/master", "issues": "https://github.com/pdffiller/Box-SDK/issues" }, - "time": "2017-04-12 08:57:53" + "time": "2017-04-12T08:57:53+00:00" }, { "name": "phpseclib/phpseclib", @@ -660,7 +668,7 @@ "x.509", "x509" ], - "time": "2016-10-04 00:57:04" + "time": "2016-10-04T00:57:04+00:00" }, { "name": "psr/cache", @@ -706,7 +714,7 @@ "psr", "psr-6" ], - "time": "2016-08-06 20:24:11" + "time": "2016-08-06T20:24:11+00:00" }, { "name": "psr/http-message", @@ -756,7 +764,7 @@ "request", "response" ], - "time": "2016-08-06 14:39:51" + "time": "2016-08-06T14:39:51+00:00" }, { "name": "psr/log", @@ -803,7 +811,54 @@ "psr", "psr-3" ], - "time": "2016-10-10 12:19:37" + "time": "2016-10-10T12:19:37+00:00" + }, + { + "name": "tightenco/collect", + "version": "v5.5.13", + "source": { + "type": "git", + "url": "https://github.com/tightenco/collect.git", + "reference": "dae85b18c6a2315645bca3dac37e3e408542efa8" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/tightenco/collect/zipball/dae85b18c6a2315645bca3dac37e3e408542efa8", + "reference": "dae85b18c6a2315645bca3dac37e3e408542efa8", + "shasum": "" + }, + "require": { + "php": ">=7.0" + }, + "require-dev": { + "mockery/mockery": "~1.0", + "phpunit/phpunit": "~6.0" + }, + "type": "library", + "autoload": { + "files": [ + "src/Illuminate/Support/helpers.php" + ], + "psr-4": { + "Illuminate\\": "src/Illuminate" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Taylor Otwell", + "email": "taylorotwell@gmail.com" + } + ], + "description": "Collect - Illuminate Collections as a separate package.", + "keywords": [ + "collection", + "laravel" + ], + "time": "2017-09-25T17:13:51+00:00" } ], "packages-dev": [],