1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://flipboxfactory.com/software/force/license |
6
|
|
|
* @link https://www.flipboxfactory.com/software/force/ |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\craft\salesforce\controllers; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use craft\helpers\Json; |
13
|
|
|
use flipbox\craft\ember\controllers\AbstractController; |
14
|
|
|
use flipbox\craft\salesforce\criteria\ObjectCriteria; |
15
|
|
|
use flipbox\craft\salesforce\criteria\UrlCriteria; |
16
|
|
|
use flipbox\craft\salesforce\events\DownloadAttachmentEvent; |
17
|
|
|
use flipbox\craft\salesforce\Force; |
18
|
|
|
use yii\web\NotFoundHttpException; |
19
|
|
|
use yii\web\UnauthorizedHttpException; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author Flipbox Factory <[email protected]> |
23
|
|
|
* @since 1.2.0 |
24
|
|
|
*/ |
25
|
|
|
class AttachmentsController extends AbstractController |
26
|
|
|
{ |
27
|
|
|
const EVENT_DOWNLOAD_ATTACHMENT = 'downloadAttachment'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string|null $id |
31
|
|
|
* @return \craft\web\Response|\yii\console\Response |
32
|
|
|
* @throws NotFoundHttpException |
33
|
|
|
* @throws UnauthorizedHttpException |
34
|
|
|
* @throws \yii\web\BadRequestHttpException |
35
|
|
|
* @throws \yii\web\HttpException |
36
|
|
|
* @throws \yii\web\RangeNotSatisfiableHttpException |
37
|
|
|
*/ |
38
|
|
|
public function actionDownload(string $id = null, bool $canDownload = null, bool $inline = false) |
39
|
|
|
{ |
40
|
|
|
if ($canDownload !== true && !Force::getInstance()->getSettings()->enableDocumentDownloads) { |
41
|
|
|
throw new UnauthorizedHttpException("Unable to download attachment."); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$id = $id ?? Craft::$app->getRequest()->getRequiredBodyParam('id'); |
45
|
|
|
$inline = (bool) ($inline ?? Craft::$app->getRequest()->getParam('inline', $inline)); |
46
|
|
|
|
47
|
|
|
$result = (new ObjectCriteria()) |
48
|
|
|
->setObject('Attachment') |
49
|
|
|
->setId($id) |
50
|
|
|
->read(); |
51
|
|
|
|
52
|
|
|
if ($result->getStatusCode() !== 200) { |
53
|
|
|
throw new NotFoundHttpException( |
54
|
|
|
Craft::t( |
55
|
|
|
'salesforce', |
56
|
|
|
'The document could not be found.' |
57
|
|
|
) |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$object = Json::decodeIfJson( |
62
|
|
|
$result->getBody()->getContents() |
63
|
|
|
); |
64
|
|
|
|
65
|
|
|
$result = (new UrlCriteria()) |
66
|
|
|
->setUrl($object['Body']) |
67
|
|
|
->read(); |
68
|
|
|
|
69
|
|
|
if ($result->getStatusCode() !== 200) { |
70
|
|
|
throw new NotFoundHttpException( |
71
|
|
|
Craft::t( |
72
|
|
|
'salesforce', |
73
|
|
|
'The document contents could not be found.' |
74
|
|
|
) |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$event = new DownloadAttachmentEvent([ |
79
|
|
|
'object' => $object, |
80
|
|
|
'fileName' => $object['Name'], |
81
|
|
|
'fileContents' => $result->getBody()->getContents(), |
82
|
|
|
'canDownload' => $canDownload |
83
|
|
|
]); |
84
|
|
|
|
85
|
|
|
$this->trigger( |
86
|
|
|
self::EVENT_DOWNLOAD_ATTACHMENT, |
87
|
|
|
$event |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
if (!$event->canDownload) { |
91
|
|
|
throw new UnauthorizedHttpException( |
92
|
|
|
Craft::t( |
93
|
|
|
'salesforce', |
94
|
|
|
$event->unauthorizedMessage |
|
|
|
|
95
|
|
|
) |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return Craft::$app->getResponse()->sendContentAsFile( |
100
|
|
|
$event->fileContents, |
101
|
|
|
$event->fileName, |
102
|
|
|
[ |
103
|
|
|
'inline' => $inline |
104
|
|
|
] |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: