-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtension.php
More file actions
executable file
·278 lines (221 loc) · 10.6 KB
/
Extension.php
File metadata and controls
executable file
·278 lines (221 loc) · 10.6 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?php
namespace Bolt\Extension\blockmurder\ImageUpload;
use Bolt\Application;
/**
* Class Extension
* @package ImageUpload
* @author blockmurder (info@blockmurder.ch)
*/
class Extension extends \Bolt\BaseExtension{
private $authorized = false;
public $config;
public function getName()
{
return "ImageUpload";
}
/**
* Set up the routing and menu-item
*/
public function initialize()
{
$this->config = $this->getConfig();
/**
* ensure proper config
*/
if (!isset($this->config['permissions']) || !is_array($this->config['permissions'])) {
$this->config['permissions'] = array('root', 'admin', 'developer');
} else {
$this->config['permissions'][] = 'root';
}
if (!isset($this->config['gallery_path'])) { $this->config['gallery_path'] = "gallerys"; }
if (!isset($this->config['navigation'])) { $this->config['navigation'] = "by_year"; }
if (!isset($this->config['watermark']) || !is_bool($this->config['watermark'])){ $this->config['watermark'] = "false"; }
if (!isset($this->config['imageMaxWidth'])) { $this->config['imageMaxWidth'] = "1000"; }
if (!isset($this->config['imageMaxHeight'])) { $this->config['imageMaxHeight'] = "1000"; }
if (!isset($this->config['imageCrop']) || !is_bool($this->config['imageCrop'])){ $this->config['imageCrop'] = "false"; }
if (!isset($this->config['imageQuality'])) { $this->config['imageQuality'] = ".85"; }
if (!isset($this->config['imageOrientation']) || !is_bool($this->config['imageOrientation'])){ $this->config['imageOrientation'] = "false"; }
if (!isset($this->config['loadImageMaxFileSize'])) { $this->config['loadImageMaxFileSize'] = "20000000"; }
// check if user has allowed role(s)
$currentUser = $this->app['users']->getCurrentUser();
$currentUserId = $currentUser['id'];
foreach ($this->config['permissions'] as $role) {
if ($this->app['users']->hasRole($currentUserId, $role)) {
$this->authorized = true;
break;
}
}
if ($this->authorized)
{
$this->path = $this->app['config']->get('general/branding/path') . '/extensions/image-upload';
$this->app->match($this->path, array($this, 'ImageUpload'));
$this->addMenuOption('Image Upload', $this->app['paths']['bolt'] . 'extensions/image-upload', "fa:rocket");
}
}
/**
* Callback function, where the actual work is done.
*
* @return mixed
*/
public function ImageUpload()
{
$title = "ImageUpload";
$urlbase = $this->app['paths']['extensions'] . 'vendor/blockmurder/imageupload';
// add MenuEditor template namespace to twig
$this->app['twig.loader.filesystem']->addPath(__DIR__.'/views/', 'ImageUpload');
// render the template.
$html = $this->app['render']->render('@ImageUpload/_imageupload.twig', array(
'title' => $title,
'content' => $content,
'urlbase' => $urlbase,
'navigation_by' => $this->config['navigation'],
));
return $this->injectAssets($html);
}
private function str_replace_first($needle, $replacement, $haystack)
{
$needle_start = strpos($haystack, $needle);
$needle_end = $needle_start + strlen($needle);
if($needle_start!==false) {
$to_replace = substr($haystack, 0, $needle_end);
return str_replace($needle, $replacement, $to_replace) . substr($haystack, $needle_end);
}
else
return $haystack;
}
/**
* @param $html
* @return mixed
*/
private function injectAssets($html)
{
$urlbase = $this->app['paths']['extensions'] . 'vendor/blockmurder/imageupload';
$url = $this->app['paths']['files'].$this->config['gallery_path'].'/';
$assets = "
<link rel='stylesheet' href='{urlbase}/assets/css/blueimp-gallery.min.css'>
<link rel='stylesheet' href='{urlbase}/assets/css/jquery.fileupload.css'>
<link rel='stylesheet' href='{urlbase}/assets/css/jquery.fileupload-ui2.css'>
<script src='{urlbase}/assets/js/vendor/navgoco-master/src/jquery.cookie.js'></script>
<script type='text/javascript' src='{urlbase}/assets/js/vendor/navgoco-master/src/jquery.navgoco.js'></script>
<link rel='stylesheet' type='text/css' href='{urlbase}/assets/js/vendor/navgoco-master/src/jquery.navgoco.css' media='screen' />
<noscript><link rel='stylesheet' href='{urlbase}/assets/css/jquery.fileupload-noscript.css'></noscript>
<noscript><link rel='stylesheet' href='{urlbase}/assets/css/jquery.fileupload-ui-noscript.css'></noscript>
";
$assets_down = "
<script src='{urlbase}/assets/js/vendor/jquery.ui.widget.js'></script>
<script src='{urlbase}/assets/js/tmpl.min.js'></script>
<script src='{urlbase}/assets/js/load-image.all.min.js'></script>
<script src='{urlbase}/assets/js/canvas-to-blob.min.js'></script>
<script src='{urlbase}/assets/js/jquery.blueimp-gallery.min.js'></script>
<script src='{urlbase}/assets/js/jquery.iframe-transport.js'></script>
<script src='{urlbase}/assets/js/jquery.fileupload.js'></script>
<script src='{urlbase}/assets/js/jquery.fileupload-process.js'></script>
<script src='{urlbase}/assets/js/jquery.fileupload-image.js'></script>
<script src='{urlbase}/assets/js/jquery.fileupload-audio.js'></script>
<script src='{urlbase}/assets/js/jquery.fileupload-video.js'></script>
<script src='{urlbase}/assets/js/jquery.fileupload-validate.js'></script>
<script src='{urlbase}/assets/js/jquery.fileupload-ui.js'></script>
<script>
$(function (){
'use strict';
//var g_input = $('#gallery_path');
var g_path = '{url}'+'temp/';
// Initialize the jQuery File Upload widget:
$('#fileupload').fileupload({
// Uncomment the following to send cross-domain cookies:
//xhrFields: {withCredentials: true},
url: '{urlbase}/server/',
formData: { gallery_path: g_path, watermark: {watermark} },
// Enable image resizing, except for Android and Opera,
// which actually support image resizing, but fail to
// send Blob objects via XHR requests:
disableImageResize: /Android(?!.*Chrome)|Opera/
.test(window.navigator && navigator.userAgent),
imageMaxWidth: {imageMaxWidth},
imageMaxHeight: {imageMaxHeight},
imageCrop: {imageCrop} ,// Force cropped images,
imageQuality: {imageQuality},
imageOrientation: {imageOrientation},
sequentialUploads: true,
loadImageMaxFileSize: {loadImageMaxFileSize}
});
// Enable iframe cross-domain access via redirect option:
$('#fileupload').fileupload(
'option',
'redirect',
window.location.href.replace(
/\/[^\/]*$/,
'/cors/result.html?%s'
)
);
// Load existing files:
$('#fileupload').addClass('fileupload-processing');
$.ajax({
// Uncomment the following to send cross-domain cookies:
//xhrFields: {withCredentials: true},
url: $('#fileupload').fileupload('option', 'url')+'?gallery_path='+g_path+'&watermark={watermark}',
dataType: 'json',
context: $('#fileupload')[0]
}).always(function () {
$(this).removeClass('fileupload-processing');
}).done(function (result) {
$(this).fileupload('option', 'done')
.call(this, $.Event('done'), {result: result});
});
$('#selected_gallery li a').click(function() {
var g_path = '{url}'+$(this).attr('id')+'/';
$('.template-download').remove();
$('#fileupload').fileupload({
url: '{urlbase}/server/',
formData: { gallery_path: g_path, watermark: {watermark} },
disableImageResize: /Android(?!.*Chrome)|Opera/
.test(window.navigator && navigator.userAgent),
imageMaxWidth: {imageMaxWidth},
imageMaxHeight: {imageMaxHeight},
imageCrop: {imageCrop} ,// Force cropped images,
imageQuality: {imageQuality},
imageOrientation: {imageOrientation},
sequentialUploads: true,
loadImageMaxFileSize: {loadImageMaxFileSize}
});
// Load existing files:
$('#fileupload').addClass('fileupload-processing');
$.ajax({
url: $('#fileupload').fileupload('option', 'url')+'?gallery_path='+g_path+'&watermark={watermark}',
dataType: 'json',
context: $('#fileupload')[0]
}).always(function () {
$(this).removeClass('fileupload-processing');
}).done(function (result) {
$(this).fileupload('option', 'done')
.call(this, $.Event('done'), {result: result});
});
});
});
</script>
<!--[if (gte IE 8)&(lt IE 10)]>
<script src='{urlbase}extensions/ImageUpload/js/cors/jquery.xdr-transport.js'></script>
<![endif]-->
";
setlocale (LC_ALL, 'en_GB', 'en_GB.UTF-8', 'en_GB.utf8');
$assets = preg_replace('~\{urlbase\}~', $urlbase, $assets);
$assets_down = preg_replace('~\{urlbase\}~', $urlbase, $assets_down);
$assets_down = preg_replace('~\{url\}~', $url, $assets_down);
$assets_down = preg_replace('~\{watermark\}~', ($this->config['watermark']) ? 'true' : 'false', $assets_down);
$assets_down = preg_replace('~\{imageMaxWidth\}~', $this->config['imageMaxWidth'], $assets_down);
$assets_down = preg_replace('~\{imageMaxHeight\}~', $this->config['imageMaxHeight'], $assets_down);
$assets_down = preg_replace('~\{imageCrop\}~', ($this->config['imageCrop']) ? 'true' : 'false', $assets_down);
$assets_down = preg_replace('~\{imageQuality\}~', $this->config['imageQuality'], $assets_down);
$assets_down = preg_replace('~\{imageOrientation\}~', ($this->config['imageOrientation']) ? 'true' : 'false', $assets_down);
$assets_down = preg_replace('~\{loadImageMaxFileSize\}~', $this->config['loadImageMaxFileSize'], $assets_down);
// Insert just before </head>
preg_match("~^([ \t]*)</head~mi", $html, $matches);
preg_match("~^([ \t]*)</body~mi", $html, $matches_down);
$replacement = sprintf("%s\t%s\n%s", $matches[1], $assets, $matches[0]);
$replacement_down = sprintf("%s\t%s\n%s", $matches_down[1], $assets_down, $matches_down[0]);
//$html = str_replace('<link rel="stylesheet" href="'.$urlbase.'view/css/bootstrap.min.css">', "", $html);
$html = str_replace($this->app['paths']['root'].'app/view/lib/upload/jquery.fileupload-ui.css', $urlbase."/assets/css/jquery.fileupload-ui2.css", $html);
$html_new = $this->str_replace_first($matches_down[0], $replacement_down, $html);
return $this->str_replace_first($matches[0], $replacement, $html_new);
}
}