Skip to content

Commit ea4f758

Browse files
committed
Fixed file upload bugs
1 parent d9d3a71 commit ea4f758

6 files changed

Lines changed: 95 additions & 67 deletions

File tree

class/Controller/Admin/Slide.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ protected function listHtml(Request $request)
4646
$carouselId = $request->pullGetInteger('carouselId');
4747
$carouselFactory = new \carousel\Factory\CarouselFactory;
4848
$carousel = $carouselFactory->load($carouselId);
49-
return $this->view->scriptView('Slide', true,
49+
return $this->view->scriptView('Slide',
5050
['carouselId' => $carouselId, 'carouselTitle' => $carousel->title]);
5151
}
5252

@@ -56,18 +56,21 @@ protected function listJson(Request $request)
5656
$search = $request->pullGetString('search', true);
5757
$sort = $request->pullGetString('sortBy', true);
5858
$sortDir = $request->pullGetString('sortByDir', true);
59-
$listing = $this->factory->listing(['carouselId'=>$carouselId, 'search'=>$search, 'sort'=>$sort, 'sortDir'=>$sortDir]);
59+
$listing = $this->factory->listing(['carouselId' => $carouselId, 'search' => $search, 'sort' => $sort, 'sortDir' => $sortDir]);
6060
return ['listing' => $listing];
6161
}
6262

6363
protected function mediaPost(Request $request)
6464
{
6565
try {
66-
$slide = $this->factory->postMedia($request);
66+
$slide = $this->factory->load($request->pullPostInteger('slideId'));
67+
$this->factory->postMedia($slide, $request);
6768
$this->factory->save($slide);
6869
return ['success' => true];
6970
} catch (\Exception $e) {
70-
$this->factory->delete($slide);
71+
if (isset($slide) && empty($slide->filepath)) {
72+
$this->factory->delete($slide);
73+
}
7174
throw $e;
7275
}
7376
}
@@ -99,11 +102,11 @@ protected function delete(Request $request)
99102
$slide = $this->factory->load($this->id);
100103
$this->factory->delete($slide);
101104
}
102-
105+
103106
protected function sortPatch(Request $request)
104107
{
105108
$this->factory->resort($this->id, $request->pullPatchInteger('position'));
106-
return ['success'=>true];
109+
return ['success' => true];
107110
}
108111

109112
}

class/Factory/SlideFactory.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use carousel\Resource\SlideResource;
1818
use carousel\UploadHandler;
1919

20-
define('CAROUSEL_MEDIA_DIRECTORY', './images/carousel/');
20+
define('CAROUSEL_MEDIA_DIRECTORY', 'images/carousel/');
2121

2222
class SlideFactory extends BaseFactory
2323
{
@@ -135,9 +135,8 @@ public function lastSlide(int $carouselId)
135135
return $db->selectColumn();
136136
}
137137

138-
public function postMedia(Request $request)
138+
public function postMedia(SlideResource $slide, Request $request)
139139
{
140-
$slide = $this->load($request->pullPostInteger('slideId'));
141140
$result = $this->upload($slide->carouselId);
142141

143142
$slide->filepath = $result['filepath'];
@@ -151,7 +150,6 @@ public function postMedia(Request $request)
151150
$slide->width = $dim[0];
152151
$slide->height = $dim[1];
153152
}
154-
return $slide;
155153
}
156154

157155
public function put($slideId, Request $request)
@@ -188,7 +186,7 @@ public function upload(int $carouselId)
188186
throw new \Exception('Upload missing image/media file.');
189187
}
190188
$file = $_FILES['file'];
191-
189+
$file['name'] = preg_replace('/[|;,!@#$()<>\"\'`~{}\[\]=+&\^\s\t]/', '_', $file['name']);
192190
if (in_array($file['type'], $imageTypes)) {
193191
$result = $this->saveImage($file, $carouselId);
194192
$result['type'] = 0;
@@ -253,6 +251,7 @@ private function getImageOptions($pic, $imageDirectory, int $carouselId)
253251
$imageDirectory = CAROUSEL_MEDIA_DIRECTORY . $carouselId . '/';
254252
$imagePath = PHPWS_HOME_DIR . $imageDirectory;
255253
$options = array(
254+
'name' => $pic['name'],
256255
'max_width' => CAROUSEL_SYSTEM_SETTINGS['maxWidth'],
257256
'max_height' => CAROUSEL_SYSTEM_SETTINGS['maxHeight'],
258257
'param_name' => 'file',

class/UploadHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,7 +1367,7 @@ public function post($print_response = true)
13671367
foreach ($upload['tmp_name'] as $index => $value) {
13681368
$files[] = $this->handle_file_upload(
13691369
$upload['tmp_name'][$index],
1370-
$file_name ? $file_name : $upload['name'][$index],
1370+
$file_name ? $file_name : !empty($this->options['name']) ? $this->options['name'] : $upload['name'][$index],
13711371
$size ? $size : $upload['size'][$index],
13721372
$upload['type'][$index], $upload['error'][$index],
13731373
$index, $content_range
@@ -1378,7 +1378,7 @@ public function post($print_response = true)
13781378
// $_FILES is a one-dimensional array:
13791379
$files[] = $this->handle_file_upload(
13801380
isset($upload['tmp_name']) ? $upload['tmp_name'] : null,
1381-
$file_name ? $file_name : (isset($upload['name']) ?
1381+
$file_name ? $file_name : !empty($this->options['name']) ? $this->options['name'] : (isset($upload['name']) ?
13821382
$upload['name'] : null),
13831383
$size ? $size : (isset($upload['size']) ?
13841384
$upload['size'] : $this->get_server_var('CONTENT_LENGTH')),

class/View/AbstractView.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,22 @@
1717

1818
abstract class AbstractView
1919
{
20+
2021
const directory = PHPWS_SOURCE_DIR . 'mod/carousel/';
2122
const http = PHPWS_SOURCE_HTTP . 'mod/carousel/';
22-
23+
2324
protected $factory;
2425

25-
26-
protected function getDirectory() {
26+
protected function getDirectory()
27+
{
2728
return self::directory;
2829
}
29-
30-
protected function getHttp() {
30+
31+
protected function getHttp()
32+
{
3133
return self::http;
3234
}
33-
35+
3436
private function addScriptVars($vars)
3537
{
3638
if (empty($vars)) {
@@ -79,24 +81,23 @@ protected function getAssetPath($scriptName)
7981
* @param array $vars
8082
* @return string
8183
*/
82-
public function scriptView($view_name, $add_anchor = true, $vars = null, $skip_vendor = false)
84+
public function scriptView($view_name, $vars = null, $skip_vendor = false)
8385
{
8486
static $vendor_included = false;
8587
if (!$vendor_included && !$skip_vendor) {
8688
$script[] = $this->getScript('vendor');
8789
$vendor_included = true;
8890
}
89-
if (!empty($vars)) {
91+
if (!empty($vars) && is_array($vars)) {
9092
$script[] = $this->addScriptVars($vars);
9193
}
9294
$script[] = $this->getScript($view_name);
9395
$react = implode("\n", $script);
9496
\Layout::addJSHeader($react);
95-
if ($add_anchor) {
96-
$content = <<<EOF
97+
$content = <<<EOF
9798
<div id="$view_name"></div>
9899
EOF;
99-
return $content;
100-
}
100+
return $content;
101101
}
102+
102103
}

class/View/CarouselView.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function view(CarouselResource $carousel)
3636
}
3737
$options['intervalTime'] = $carousel->intervalTime * 1000;
3838
$options['iterations'] = $carousel->iterations * count($slides);
39-
$this->scriptView('View', false, $options, true);
39+
$this->scriptView('View', $options, true);
4040
\Layout::addStyle('carousel');
4141
return $this->viewSlides($carousel, $slides);
4242
}
@@ -111,7 +111,7 @@ private function viewSlides(CarouselResource $carousel, array $slides)
111111

112112
public function miniAdmin(int $keyId)
113113
{
114-
$this->scriptView('MiniAdmin', false, ['keyId' => $keyId], true);
114+
$this->scriptView('MiniAdmin', ['keyId' => $keyId], true);
115115

116116
$options['titleOnly'] = true;
117117
$pinnedCarousel = $this->factory->getPinned($keyId);

0 commit comments

Comments
 (0)