1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* FilePond Server plugin for Craft CMS 3.x |
4
|
|
|
* |
5
|
|
|
* Instant FilePond server that works with Craft Assets. |
6
|
|
|
* |
7
|
|
|
* @link https://workingconcept.com |
8
|
|
|
* @copyright Copyright (c) 2019 Working Concept |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace workingconcept\assetpond; |
12
|
|
|
|
13
|
|
|
use craft\services\Users; |
14
|
|
|
use workingconcept\assetpond\helpers\PostHelper; |
15
|
|
|
use workingconcept\assetpond\services\Server; |
16
|
|
|
use workingconcept\assetpond\variables\AssetPondVariable; |
17
|
|
|
use workingconcept\assetpond\models\Settings; |
18
|
|
|
|
19
|
|
|
use Craft; |
20
|
|
|
use craft\base\Plugin; |
21
|
|
|
use craft\web\UrlManager; |
22
|
|
|
use craft\web\twig\variables\CraftVariable; |
23
|
|
|
use craft\events\RegisterUrlRulesEvent; |
24
|
|
|
use craft\services\Elements; |
25
|
|
|
use craft\events\ElementEvent; |
26
|
|
|
use craft\services\Assets; |
27
|
|
|
use craft\events\AssetEvent; |
28
|
|
|
use craft\events\UserEvent; |
29
|
|
|
|
30
|
|
|
use yii\base\Event; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Class AssetPond |
34
|
|
|
* |
35
|
|
|
* @author Working Concept |
36
|
|
|
* @package AssetPond |
37
|
|
|
* @since 1.0.0 |
38
|
|
|
* |
39
|
|
|
* @property Server $filePondServerService |
40
|
|
|
*/ |
41
|
|
|
class AssetPond extends Plugin |
42
|
|
|
{ |
43
|
|
|
// Static Properties |
44
|
|
|
// ========================================================================= |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var AssetPond |
48
|
|
|
*/ |
49
|
|
|
public static $plugin; |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
// Public Properties |
53
|
|
|
// ========================================================================= |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var string |
57
|
|
|
*/ |
58
|
|
|
public $schemaVersion = '1.0.0'; |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
// Public Methods |
62
|
|
|
// ========================================================================= |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @inheritdoc |
66
|
|
|
*/ |
67
|
|
|
public function init() |
68
|
|
|
{ |
69
|
|
|
parent::init(); |
70
|
|
|
self::$plugin = $this; |
71
|
|
|
|
72
|
|
|
Event::on( |
73
|
|
|
UrlManager::class, |
74
|
|
|
UrlManager::EVENT_REGISTER_SITE_URL_RULES, |
75
|
|
|
static function (RegisterUrlRulesEvent $event) |
76
|
|
|
{ |
77
|
|
|
$event->rules['assetpond'] = 'asset-pond/default'; |
78
|
|
|
$event->rules['assetpond/<volumeId:\d+>'] = 'asset-pond/default'; |
79
|
|
|
} |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
$request = Craft::$app->getRequest(); |
83
|
|
|
|
84
|
|
|
if ($request->getIsPost()) |
85
|
|
|
{ |
86
|
|
|
$this->_handlePost(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
Event::on( |
90
|
|
|
CraftVariable::class, |
91
|
|
|
CraftVariable::EVENT_INIT, |
92
|
|
|
static function (Event $event) { |
93
|
|
|
/** @var CraftVariable $variable */ |
94
|
|
|
$variable = $event->sender; |
95
|
|
|
$variable->set('assetpond', AssetPondVariable::class); |
96
|
|
|
} |
97
|
|
|
); |
98
|
|
|
|
99
|
|
|
Craft::info( |
100
|
|
|
Craft::t( |
101
|
|
|
'asset-pond', |
102
|
|
|
'{name} plugin loaded', |
103
|
|
|
['name' => $this->name] |
104
|
|
|
), |
105
|
|
|
__METHOD__ |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
// Private Methods |
111
|
|
|
// ========================================================================= |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Writes base64-encoded FilePond uploads to files and populates $_FILES. |
115
|
|
|
* |
116
|
|
|
* Currently not working because Craft uses `is_uploaded_file()` to make |
117
|
|
|
* sure $_POST contents weren't manipulated—which is exactly what we're |
118
|
|
|
* trying to do here. Need another way to make this happen. |
119
|
|
|
*/ |
120
|
|
|
private function _handlePost() |
121
|
|
|
{ |
122
|
|
|
$request = Craft::$app->getRequest(); |
|
|
|
|
123
|
|
|
$filepondField = $this->getSettings()->formUploadField; |
124
|
|
|
|
125
|
|
|
if (empty($_POST[$filepondField])) |
126
|
|
|
{ |
127
|
|
|
return; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if ($filepondFiles = $_POST[$filepondField]) |
131
|
|
|
{ |
132
|
|
|
foreach ($filepondFiles as $targetField => $file) |
133
|
|
|
{ |
134
|
|
|
if ($tempFile = Server::base64FilePostToUploadedFile($file)) |
135
|
|
|
{ |
136
|
|
|
$_FILES[$targetField]['name'] = $tempFile->name; |
137
|
|
|
$_FILES[$targetField]['tmp_name'] = $tempFile->tempName; |
138
|
|
|
$_FILES[$targetField]['size'] = $tempFile->size; |
139
|
|
|
$_FILES[$targetField]['type'] = $tempFile->type; |
140
|
|
|
$_FILES[$targetField]['error'] = $tempFile->error; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
// we were never here |
146
|
|
|
unset($_POST[$filepondField]); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
|
150
|
|
|
// Protected Methods |
151
|
|
|
// ========================================================================= |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @inheritdoc |
155
|
|
|
*/ |
156
|
|
|
protected function createSettingsModel() |
157
|
|
|
{ |
158
|
|
|
return new Settings(); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @inheritdoc |
163
|
|
|
*/ |
164
|
|
|
protected function settingsHtml(): string |
165
|
|
|
{ |
166
|
|
|
return Craft::$app->view->renderTemplate( |
167
|
|
|
'asset-pond/settings', |
168
|
|
|
[ |
169
|
|
|
'settings' => $this->getSettings() |
170
|
|
|
] |
171
|
|
|
); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|