Skip to content

Commit 451681c

Browse files
Merge branch 'main' into 6.0
2 parents 8ea4cf1 + ccda53c commit 451681c

File tree

11 files changed

+881
-702
lines changed

11 files changed

+881
-702
lines changed

build.xml

+762-663
Large diffs are not rendered by default.

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
"sebastianfeldmann/ftp": "^0.9.2",
5858
"guzzlehttp/guzzle": "^5.3.4|^6.5.8|^7.5.0",
5959
"aws/aws-sdk-php": "^3.10",
60-
"kunalvarma05/dropbox-php-sdk": "^0.4",
60+
"kunalvarma05/dropbox-php-sdk": "^0.5",
6161
"phpseclib/phpseclib": "^2.0",
6262
"softlayer/objectstorage": "dev-master",
6363
"vlucas/phpdotenv": "^4.0",

doc/config/log/webhook.json

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
"type": "webhook",
33
"options": {
44
"uri": "https://example.com/webhook",
5+
"sendOnlyOnError": false,
6+
"sendOnSimulation": false,
57
"username": "misterX",
68
"password": "secret",
79
"method": "POST",

doc/config/log/webhook.xml

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
<!-- mandatory -->
44
<option name="uri" value="https://some.host.com/webhook" />
55

6+
<!-- optional default=false -->
7+
<option name="sendOnlyOnError" value="true" />
8+
9+
<!-- optional default=true -->
10+
<option name="sendOnSimulation" value="false" />
11+
612
<!-- optional default=false -->
713
<option name="username" value="misterX" />
814

doc/config/sync/dropbox.json

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
"type": "dropbox",
33
"options": {
44
"token": "myCrazyLongApiTokenThatIGotFromDropbox",
5+
"appKey": "myAppKey",
6+
"appSecret": "myAppSecret",
57
"path": "/some/dir"
68
}
79
}

doc/config/sync/dropbox.xml

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
<!-- mandatory -->
44
<option name="token" value="mycrazylongapitokenthatigotfromdropbox" />
55

6+
<!-- mandatory -->
7+
<option name="appKey" value="myAppKey" />
8+
9+
<!-- mandatory -->
10+
<option name="appSecret" value="myAppSecret" />
11+
612
<!-- mandatory -->
713
<option name="path" value="/some/dir" />
814
</sync>

src/Backup/Collector/Dropbox.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<?php
22
namespace phpbu\App\Backup\Collector;
33

4-
use Kunnu\Dropbox\Dropbox as DropboxApi;
5-
use Kunnu\Dropbox\Models\FolderMetadata;
4+
use Kunnu\Dropbox as DropboxApi;
65
use phpbu\App\Backup\Collector;
76
use phpbu\App\Backup\File;
87
use phpbu\App\Backup\Path;
@@ -35,7 +34,7 @@ class Dropbox extends Remote implements Collector
3534
* @param \phpbu\App\Backup\Path $path
3635
* @param \Kunnu\Dropbox\Dropbox $client
3736
*/
38-
public function __construct(Target $target, Path $path, DropboxApi $client)
37+
public function __construct(Target $target, Path $path, DropboxApi\Dropbox $client)
3938
{
4039
$this->setUp($target, $path);
4140
$this->client = $client;
@@ -55,7 +54,7 @@ protected function collectBackups()
5554
/** @var \Kunnu\Dropbox\Models\FileMetadata $item */
5655
foreach ($items->getItems() as $item) {
5756
// skip directories
58-
if ($item instanceof FolderMetadata) {
57+
if ($item instanceof DropboxApi\Models\FolderMetadata) {
5958
continue;
6059
}
6160
if ($this->isFileMatch($item->getPathDisplay())) {

src/Backup/File/Dropbox.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22
namespace phpbu\App\Backup\File;
33

4-
use Kunnu\Dropbox\Dropbox as DropboxApi;
5-
use Kunnu\Dropbox\Models\FileMetadata;
4+
use Kunnu\Dropbox as DropboxApi;
5+
use phpbu\App\Exception;
66

77
/**
88
* Dropbox class.
@@ -31,7 +31,7 @@ class Dropbox extends Remote
3131
* @param \Kunnu\Dropbox\Dropbox $client
3232
* @param \Kunnu\Dropbox\Models\FileMetadata $dropboxFile
3333
*/
34-
public function __construct(DropboxApi $client, FileMetadata $dropboxFile)
34+
public function __construct(DropboxApi\Dropbox $client, DropboxApi\Models\FileMetadata $dropboxFile)
3535
{
3636
$this->client = $client;
3737
$this->filename = $dropboxFile->getName();
@@ -50,7 +50,7 @@ public function unlink()
5050
try {
5151
$this->client->delete($this->pathname);
5252
} catch (\Exception $e) {
53-
throw new \phpbu\App\Exception($e->getMessage());
53+
throw new Exception($e->getMessage());
5454
}
5555
}
5656
}

src/Backup/Sync/Dropbox.php

+22-10
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
<?php
22
namespace phpbu\App\Backup\Sync;
33

4-
use Kunnu\Dropbox\DropboxApp as DropboxConfig;
5-
use Kunnu\Dropbox\Dropbox as DropboxApi;
6-
use Kunnu\Dropbox\DropboxFile;
4+
use Kunnu\Dropbox as DropboxApi;
5+
use Kunnu\Dropbox\Exceptions\DropboxClientException;
76
use phpbu\App\Backup\Collector;
87
use phpbu\App\Backup\Path;
98
use phpbu\App\Result;
@@ -61,6 +60,16 @@ class Dropbox implements Simulator
6160
*/
6261
protected $time;
6362

63+
/**
64+
* @var string
65+
*/
66+
private mixed $appKey;
67+
68+
/**
69+
* @var string
70+
*/
71+
private mixed $appSecret;
72+
6473
/**
6574
* (non-PHPDoc)
6675
*
@@ -76,10 +85,12 @@ public function setup(array $config)
7685
}
7786

7887
// check for mandatory options
79-
$this->validateConfig($config, ['token', 'path']);
88+
$this->validateConfig($config, ['token', 'path', 'appKey', 'appSecret']);
8089

81-
$this->time = time();
82-
$this->token = $config['token'];
90+
$this->time = time();
91+
$this->token = $config['token'];
92+
$this->appKey = $config['appKey'];
93+
$this->appSecret = $config['appSecret'];
8394
// make sure the path contains a leading slash
8495
$this->path = new Path(Util\Path::withLeadingSlash($config['path']), $this->time);
8596

@@ -117,7 +128,7 @@ public function sync(Target $target, Result $result)
117128
$client = $this->createClient();
118129

119130
try {
120-
$file = new DropboxFile($sourcePath);
131+
$file = new DropboxApi\DropboxFile($sourcePath);
121132
$meta = $client->upload($file, $dropboxPath, ['autorename' => true]);
122133
$result->debug('upload: done (' . $meta->getSize() . ')');
123134

@@ -163,12 +174,13 @@ protected function createCollector(Target $target) : Collector
163174
* Create a dropbox api client.
164175
*
165176
* @return \Kunnu\Dropbox\Dropbox
177+
* @throws DropboxClientException
166178
*/
167-
protected function createClient() : DropboxApi
179+
protected function createClient() : DropboxApi\Dropbox
168180
{
169181
if (!$this->client) {
170-
$config = new DropboxConfig("id", "secret", $this->token);
171-
$this->client = new DropboxApi($config);
182+
$app = new DropboxApi\DropboxApp($this->appKey, $this->appSecret, $this->token);
183+
$this->client = new DropboxApi\Dropbox($app);
172184
}
173185
return $this->client;
174186
}

src/Log/Webhook.php

+32-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use phpbu\App\Result;
99
use phpbu\App\Util\Arr;
1010
use phpbu\App\Util\Path;
11+
use phpbu\App\Util\Str;
1112
use Throwable;
1213

1314
/**
@@ -91,6 +92,20 @@ class Webhook implements Listener, Logger
9192
'application/xml' => '\\phpbu\\App\\Log\\ResultFormatter\\Xml'
9293
];
9394

95+
/**
96+
* Call webhook only if errors occur
97+
*
98+
* @var bool
99+
*/
100+
private bool $sendOnlyOnError = false;
101+
102+
/**
103+
* Call the webhook while simulating
104+
*
105+
* @var bool
106+
*/
107+
private bool $sendSimulating = false;
108+
94109
/**
95110
* Constructor will only set the start time to be able to log duration
96111
*/
@@ -140,6 +155,8 @@ public function setup(array $options)
140155
throw new Exception('webhook URI is invalid');
141156
}
142157

158+
$this->sendOnlyOnError = Str::toBoolean(Arr::getValue($options, 'sendOnlyOnError'), false);
159+
$this->sendSimulating = Str::toBoolean(Arr::getValue($options, 'sendOnSimulation'), true);
143160
$this->uri = $options['uri'];
144161
$this->method = Arr::getValue($options, 'method', 'GET');
145162
$this->username = Arr::getValue($options, 'username', '');
@@ -156,7 +173,10 @@ public function setup(array $options)
156173
*/
157174
public function onPhpbuEnd(Event\App\End $event)
158175
{
159-
$result = $event->getResult();
176+
$result = $event->getResult();
177+
if ($this->shouldWebhookBeCalled($result) === false) {
178+
return;
179+
}
160180
$data = $this->getQueryStringData($result);
161181
$uri = $this->method === 'GET' ? $this->buildGetUri($data) : $this->uri;
162182
$formatter = $this->getBodyFormatter();
@@ -259,4 +279,15 @@ protected function fireRequest(string $uri, string $body = '')
259279
throw new Exception('could not reach webhook: ' . $this->uri);
260280
}
261281
}
282+
283+
/**
284+
* Should a webhook be called
285+
*
286+
* @param \phpbu\App\Result $result
287+
* @return bool
288+
*/
289+
protected function shouldWebhookBeCalled(Result $result) : bool
290+
{
291+
return (!$this->sendOnlyOnError || !$result->allOk()) && ($this->sendSimulating || !$this->isSimulation);
292+
}
262293
}

0 commit comments

Comments
 (0)