Skip to content

Commit efdec2c

Browse files
committed
removed linkFiles property, replaced file uploader
1 parent ab598aa commit efdec2c

8 files changed

Lines changed: 92 additions & 96 deletions

File tree

examples/form.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@
4343
'port' => 587,
4444
];
4545

46+
$fileManager = new FileManager([
47+
'uploadPath' => __DIR__ . '/attachments',
48+
'uploadUrl' => $_SERVER['HTTP_ORIGIN'] . '/attachments',
49+
]
50+
);
51+
4652
$message = [
4753
'from' => ['hello@justcoded.co.uk' => 'FROM NAME'],
4854
'to' => ['kostant21@yahoo.com' => 'TO NAME'],
@@ -51,7 +57,7 @@
5157
'subject' => 'Contact request from {name}',
5258
'bodyTemplate' => __DIR__ . '/template-html.php',
5359
'altBodyTemplate' => __DIR__ . '/template-plain.php',
54-
'attachments' => FileManager::prepareUpload([
60+
'attachments' => $fileManager->upload([
5561
'cv_file', 'image_file'
5662
])
5763
];

examples/template-html.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,9 @@
1313
<p>User IP address: <?php echo @$_SERVER['REMOTE_ADDR']; ?></p>
1414
<p>Browser: <?php echo @$_SERVER['HTTP_USER_AGENT']; ?></p>
1515

16-
<?php if (count($links) > 0): ?>
17-
<ul>
18-
<?php foreach ($links as $link): ?>
19-
<li>
20-
<a href="<?php echo $link->getPath() ?>"><?php echo $link->getName() ?> </a>
21-
</li>
22-
<?php endforeach; ?>
23-
</ul>
24-
<?php endif; ?>
16+
{cv_file}
17+
18+
{image_file}
2519

2620
</body>
2721
</html>

src/DataObjects/File.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ class File extends DataObject
3535
*/
3636
public $uniqName;
3737

38+
/**
39+
* @var string
40+
*/
41+
public $uploadUrl;
42+
43+
/**
44+
* @var string
45+
*/
46+
public $uploadPath;
47+
3848
/**
3949
* File constructor.
4050
* @param array $config
@@ -53,4 +63,9 @@ public function getExtension()
5363
{
5464
return pathinfo($this->name, PATHINFO_EXTENSION);
5565
}
66+
67+
public function __toString()
68+
{
69+
return $this->uploadUrl;
70+
}
5671
}

src/DataObjects/MailMessage.php

Lines changed: 4 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,6 @@ class MailMessage extends DataObject
6666
*/
6767
protected $files = [];
6868

69-
/**
70-
* @var array
71-
*/
72-
protected $fileLinks = [];
73-
7469
/**
7570
* Message constructor.
7671
*
@@ -166,7 +161,7 @@ public function getBody()
166161
if (!empty($this->body)) {
167162
return $this->body;
168163
} elseif (!empty($this->bodyTemplate)) {
169-
return render_template($this->bodyTemplate, $this->tokens, $this->fileLinks);
164+
return render_template($this->bodyTemplate, $this->tokens);
170165
} else {
171166
return null;
172167
}
@@ -180,32 +175,17 @@ public function getAltBody()
180175
if (!empty($this->altBody)) {
181176
return $this->altBody;
182177
} else {
183-
return render_template($this->altBodyTemplate, $this->tokens, $this->fileLinks);
178+
return render_template($this->altBodyTemplate, $this->tokens);
184179
}
185180
}
186181

187182
public function setFiles()
188183
{
189-
$uploadFolder = $this->getFullPathOfUploadFolder();
190-
191-
if (!file_exists($uploadFolder)) {
192-
mkdir($uploadFolder, 0777, true);
193-
}
194-
195184
foreach ($this->attachments as $file)
196185
{
197186
/** @var File $file */
198-
$path = $this->getFullPathOfUploadFolder() . DIRECTORY_SEPARATOR . $file->uniqName;
199-
@chmod($path, 0666 & ~umask());
200-
201-
if (move_uploaded_file($file->tmp_name, $path)) {
202-
203-
if ($file->size > self::ATTACHMENTS_SIZE_LIMIT) {
204-
$domainPath = $_SERVER['HTTP_ORIGIN'] . $this->getWebUploadFolder() .DIRECTORY_SEPARATOR . $file->uniqName;
205-
$this->addFileLink([$domainPath => $file->name]);
206-
} else {
207-
$this->addFile([$path => $file->name]);
208-
}
187+
if (!$file->size > self::ATTACHMENTS_SIZE_LIMIT) {
188+
$this->addFile([$file->uploadPath => $file->name]);
209189
}
210190
}
211191

@@ -228,43 +208,4 @@ protected function addFile($data)
228208
$this->files[] = new EmailAttachment($data);
229209
}
230210

231-
/**
232-
* @param $data
233-
*/
234-
protected function addFileLink($data)
235-
{
236-
$this->fileLinks[] = new EmailAttachment($data);
237-
}
238-
239-
/**
240-
* @return array
241-
*/
242-
public function getFileLinks()
243-
{
244-
return $this->fileLinks;
245-
}
246-
247-
/**
248-
* @return string
249-
*/
250-
protected function getFullPathOfUploadFolder()
251-
{
252-
return __DIR__ . $this->getUploadFolder();
253-
}
254-
255-
/**
256-
* @return string
257-
*/
258-
protected function getUploadFolder()
259-
{
260-
return '/../../examples' . $this->getWebUploadFolder();
261-
}
262-
263-
/**
264-
* @return string
265-
*/
266-
protected function getWebUploadFolder()
267-
{
268-
return '/attachments';
269-
}
270211
}

src/FileManager/FileManager.php

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,40 @@
77

88
class FileManager extends DataObject
99
{
10-
public static function prepareUpload(array $fields)
10+
/**
11+
* @var string
12+
*/
13+
protected $uploadPath;
14+
15+
/**
16+
* @var string
17+
*/
18+
protected $uploadUrl;
19+
20+
public function upload(array $fields)
1121
{
12-
$files = [];
22+
if (!is_dir($this->uploadPath)) {
23+
mkdir($this->uploadPath, 0777, true);
24+
}
1325

26+
$files = [];
1427
foreach ($fields as $field) {
15-
$file = $_FILES[$field];
28+
if (array_key_exists($field, $_FILES)) {
29+
$fileField = $_FILES[$field];
30+
$file = new File($fileField);
31+
32+
$name = preg_replace('/[^\00-\255]+/u', '', $file->name);
33+
$name = str_replace('"', '', trim($name));
34+
/** @var File $file */
35+
$path = realpath($this->uploadPath) . '/' . $name . $file->uniqName;
36+
37+
if ($file->error == 0 && move_uploaded_file($file->tmp_name, $path)) {
1638

17-
if ($file['error'] == 0) {
18-
$files[] = new File($file);
19-
$_POST[$field] = $files;
39+
$file->uploadUrl = $this->uploadUrl . '/' . $name . $file->uniqName;
40+
$file->uploadPath = $path;
41+
$_POST[$field] = $file;
42+
$files[] = $file;
43+
}
2044
}
2145
}
2246

src/FormHandler.php

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
namespace JustCoded\FormHandler;
44

55

6-
use JustCoded\FormHandler\DataObjects\File;
76
use JustCoded\FormHandler\Handlers\HandlerInterface;
87
use Valitron\Validator;
8+
use JustCoded\FormHandler\Validator\File as FileValidator;
99

1010
class FormHandler
1111
{
@@ -60,21 +60,7 @@ public function validate($data)
6060
$this->formFields = $data;
6161
$v = new Validator($data);
6262

63-
$v::addRule('file', function($field, $value, array $params, array $fields) {
64-
/**
65-
* @var File $file
66-
*/
67-
$file = $value[0];
68-
69-
if ($file->size >= $this->rules['rules']['file']['allowSize']) {
70-
return false;
71-
} elseif (! in_array($file->getExtension(), $this->rules['rules']['file']['allowType'])) {
72-
return false;
73-
}
74-
75-
return true;
76-
}, 'file error');
77-
63+
$v = FileValidator::validate($v, $this->rules);
7864
// create rules from input array.
7965
foreach ($this->rules['rules'] as $key => $params) {
8066
$rule = $v->rule($key, $params['fields']);

src/Validator/File.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace JustCoded\FormHandler\Validator;
4+
5+
use Valitron\Validator;
6+
use JustCoded\FormHandler\DataObjects\File as FileValidator;
7+
8+
class File
9+
{
10+
public static function validate(Validator $v, $rules)
11+
{
12+
$allowSize = $rules['rules']['file']['allowSize'];
13+
$allowType = $rules['rules']['file']['allowType'];
14+
15+
$v::addRule('file', function($field, $value, array $params, array $fields) use ($allowSize, $allowType) {
16+
/**
17+
* @var FileValidator $value
18+
*/
19+
if ($value->size >= $allowSize) {
20+
return false;
21+
} elseif (! in_array($value->getExtension(), $allowType)) {
22+
return false;
23+
}
24+
25+
return true;
26+
}, 'File error. ' . 'Max file size: ' . $allowSize . ' bytes. Allowable extensions: ' . implode(",", $allowType));
27+
28+
return $v;
29+
}
30+
}

src/functions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
if (! function_exists('render_template')) {
4-
function render_template($template, $data, $links)
4+
function render_template($template, $data)
55
{
66
if (! is_file($template)) {
77
throw new Exception('Unable to find template file: ' . $template);

0 commit comments

Comments
 (0)