| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <[email protected]> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Symfony\Component\DomCrawler\Field; |
| 13 |
|
| 14 |
/** |
| 15 |
* FileFormField represents a file form field (an HTML file input tag). |
| 16 |
* |
| 17 |
* @author Fabien Potencier <[email protected]> |
| 18 |
*/ |
| 19 |
class FileFormField extends FormField |
| 20 |
{ |
| 21 |
/** |
| 22 |
* Sets the PHP error code associated with the field. |
| 23 |
* |
| 24 |
* @param int $error The error code (one of UPLOAD_ERR_INI_SIZE, UPLOAD_ERR_FORM_SIZE, UPLOAD_ERR_PARTIAL, UPLOAD_ERR_NO_FILE, UPLOAD_ERR_NO_TMP_DIR, UPLOAD_ERR_CANT_WRITE, or UPLOAD_ERR_EXTENSION) |
| 25 |
* |
| 26 |
* @throws \InvalidArgumentException When error code doesn't exist |
| 27 |
*/ |
| 28 |
public function setErrorCode(int $error) |
| 29 |
{ |
| 30 |
$codes = [\UPLOAD_ERR_INI_SIZE, \UPLOAD_ERR_FORM_SIZE, \UPLOAD_ERR_PARTIAL, \UPLOAD_ERR_NO_FILE, \UPLOAD_ERR_NO_TMP_DIR, \UPLOAD_ERR_CANT_WRITE, \UPLOAD_ERR_EXTENSION]; |
| 31 |
if (!\in_array($error, $codes)) { |
| 32 |
throw new \InvalidArgumentException(sprintf('The error code "%s" is not valid.', $error)); |
| 33 |
} |
| 34 |
|
| 35 |
$this->value = ['name' => '', 'type' => '', 'tmp_name' => '', 'error' => $error, 'size' => 0]; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Sets the value of the field. |
| 40 |
*/ |
| 41 |
public function upload(?string $value) |
| 42 |
{ |
| 43 |
$this->setValue($value); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Sets the value of the field. |
| 48 |
*/ |
| 49 |
public function setValue(?string $value) |
| 50 |
{ |
| 51 |
if (null !== $value && is_readable($value)) { |
| 52 |
$error = \UPLOAD_ERR_OK; |
| 53 |
$size = filesize($value); |
| 54 |
$info = pathinfo($value); |
| 55 |
$name = $info['basename']; |
| 56 |
|
| 57 |
// copy to a tmp location |
| 58 |
$tmp = sys_get_temp_dir().'/'.strtr(substr(base64_encode(hash('sha256', uniqid(mt_rand(), true), true)), 0, 7), '/', '_'); |
| 59 |
if (\array_key_exists('extension', $info)) { |
| 60 |
$tmp .= '.'.$info['extension']; |
| 61 |
} |
| 62 |
if (is_file($tmp)) { |
| 63 |
unlink($tmp); |
| 64 |
} |
| 65 |
copy($value, $tmp); |
| 66 |
$value = $tmp; |
| 67 |
} else { |
| 68 |
$error = \UPLOAD_ERR_NO_FILE; |
| 69 |
$size = 0; |
| 70 |
$name = ''; |
| 71 |
$value = ''; |
| 72 |
} |
| 73 |
|
| 74 |
$this->value = ['name' => $name, 'type' => '', 'tmp_name' => $value, 'error' => $error, 'size' => $size]; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Sets path to the file as string for simulating HTTP request. |
| 79 |
*/ |
| 80 |
public function setFilePath(string $path) |
| 81 |
{ |
| 82 |
parent::setValue($path); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Initializes the form field. |
| 87 |
* |
| 88 |
* @throws \LogicException When node type is incorrect |
| 89 |
*/ |
| 90 |
protected function initialize() |
| 91 |
{ |
| 92 |
if ('input' !== $this->node->nodeName) { |
| 93 |
throw new \LogicException(sprintf('A FileFormField can only be created from an input tag (%s given).', $this->node->nodeName)); |
| 94 |
} |
| 95 |
|
| 96 |
if ('file' !== strtolower($this->node->getAttribute('type'))) { |
| 97 |
throw new \LogicException(sprintf('A FileFormField can only be created from an input tag with a type of file (given type is "%s").', $this->node->getAttribute('type'))); |
| 98 |
} |
| 99 |
|
| 100 |
$this->setValue(null); |
| 101 |
} |
| 102 |
} |
| 103 |
|