-
Notifications
You must be signed in to change notification settings - Fork 2
/
Service.php
109 lines (93 loc) · 3.29 KB
/
Service.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/**
* BoxBilling
*
* @copyright BoxBilling, Inc (http://www.boxbilling.com)
* @license Apache-2.0
*
* Copyright BoxBilling, Inc
* This source file is subject to the Apache-2.0 License that is bundled
* with this source code in the file LICENSE
*/
namespace Box\Mod\Dropbox;
require_once BB_PATH_MODS . '/Dropbox/dropbox-sdk/autoload.php';
class Service implements \Box\InjectionAwareInterface
{
protected $di;
public function setDi($di)
{
$this->di = $di;
}
public function getDi()
{
return $this->di;
}
public function install()
{
$sql = "
CREATE TABLE IF NOT EXISTS `dropbox` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`client_id` bigint(20) DEFAULT NULL,
`rel_id` bigint(20) DEFAULT NULL,
`extension` varchar(255) DEFAULT NULL,
`path` varchar(255) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
`created_at` varchar(35) DEFAULT NULL,
`updated_at` varchar(35) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
";
$this->di['db']->exec($sql);
}
public function uninstall()
{
$this->di['db']->exec('DROP TABLE dropbox;');
}
public function getDropboxClient()
{
$api = $this->di['api_admin'];
$config = $api->extension_config_get(array('ext' => 'mod_dropbox'));
if (!isset($config['access_token']) || empty($config['access_token'])) {
throw new \Box_Exception('Dropbox access token missing. Please configure it from admin area');
}
$accessToken = $config['access_token'];
return new \Dropbox\Client($accessToken, "PHP-BoxBilling/1.0");;
}
public function uploadFile($file, $client_id, $rel_id, $extension)
{
$f = fopen($file['tmp_name'], "rb");
$filename = $file['name'];
$dbxClient = $this->getDropboxClient();
$result = $dbxClient->uploadFile('/' . $filename, \Dropbox\WriteMode::add(), $f);
if (isset($result['path']) && !empty($result['path'])) {
$dropbox = $this->di['db']->dispense('dropbox');
$dropbox->client_id = $client_id;
$dropbox->rel_id = $rel_id;
$dropbox->extension = $extension;
$dropbox->path = $result['path'];
$dropbox->name = $filename;
$dropbox->updated_at = date('c');
$dropbox->created_at = date('c');
$this->di['db']->store($dropbox);
}
return true;
}
public function downloadFile(\RedBeanPHP\OODBBean $dropboxFile)
{
$path = $dropboxFile->name;
$f = fopen($path, "w+b");
$dbxClient = $this->getDropboxClient();
$fileMetadata = $dbxClient->getFile($dropboxFile->path, $f);
fclose($f);
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$path" . ";");
header("Content-Transfer-Encoding: binary");
readfile($path);
flush();
unlink($path);
return true;
}
}