/usr/share/php/Nette/Http/FileUpload.php is in php-nette 2.4-20160731-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | <?php
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
namespace Nette\Http;
use Nette;
/**
* Provides access to individual files that have been uploaded by a client.
*
* @property-read string $name
* @property-read string $sanitizedName
* @property-read string|NULL $contentType
* @property-read int $size
* @property-read string $temporaryFile
* @property-read int $error
* @property-read bool $ok
* @property-read string|NULL $contents
*/
class FileUpload
{
use Nette\SmartObject;
/** @var string */
private $name;
/** @var string */
private $type;
/** @var string */
private $size;
/** @var string */
private $tmpName;
/** @var int */
private $error;
public function __construct($value)
{
foreach (['name', 'type', 'size', 'tmp_name', 'error'] as $key) {
if (!isset($value[$key]) || !is_scalar($value[$key])) {
$this->error = UPLOAD_ERR_NO_FILE;
return; // or throw exception?
}
}
$this->name = $value['name'];
$this->size = $value['size'];
$this->tmpName = $value['tmp_name'];
$this->error = $value['error'];
}
/**
* Returns the file name.
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Returns the sanitized file name.
* @return string
*/
public function getSanitizedName()
{
return trim(Nette\Utils\Strings::webalize($this->name, '.', FALSE), '.-');
}
/**
* Returns the MIME content type of an uploaded file.
* @return string|NULL
*/
public function getContentType()
{
if ($this->isOk() && $this->type === NULL) {
$this->type = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $this->tmpName);
}
return $this->type;
}
/**
* Returns the size of an uploaded file.
* @return int
*/
public function getSize()
{
return $this->size;
}
/**
* Returns the path to an uploaded file.
* @return string
*/
public function getTemporaryFile()
{
return $this->tmpName;
}
/**
* Returns the path to an uploaded file.
* @return string
*/
public function __toString()
{
return (string) $this->tmpName;
}
/**
* Returns the error code. {@link http://php.net/manual/en/features.file-upload.errors.php}
* @return int
*/
public function getError()
{
return $this->error;
}
/**
* Is there any error?
* @return bool
*/
public function isOk()
{
return $this->error === UPLOAD_ERR_OK;
}
/**
* Move uploaded file to new location.
* @param string
* @return self
*/
public function move($dest)
{
$dir = dirname($dest);
@mkdir($dir, 0777, TRUE); // @ - dir may already exist
if (!is_dir($dir)) {
throw new Nette\InvalidStateException("Directory '$dir' cannot be created. " . error_get_last()['message']);
}
@unlink($dest); // @ - file may not exists
Nette\Utils\Callback::invokeSafe(
is_uploaded_file($this->tmpName) ? 'move_uploaded_file' : 'rename',
[$this->tmpName, $dest],
function ($message) {
throw new Nette\InvalidStateException("Unable to move uploaded file '$this->tmpName' to '$dest'. $message");
}
);
@chmod($dest, 0666); // @ - possible low permission to chmod
$this->tmpName = $dest;
return $this;
}
/**
* Is uploaded file GIF, PNG or JPEG?
* @return bool
*/
public function isImage()
{
return in_array($this->getContentType(), ['image/gif', 'image/png', 'image/jpeg'], TRUE);
}
/**
* Returns the image.
* @return Nette\Utils\Image
* @throws Nette\Utils\ImageException
*/
public function toImage()
{
return Nette\Utils\Image::fromFile($this->tmpName);
}
/**
* Returns the dimensions of an uploaded image as array.
* @return array|NULL
*/
public function getImageSize()
{
return $this->isOk() ? @getimagesize($this->tmpName) : NULL; // @ - files smaller than 12 bytes causes read error
}
/**
* Get file contents.
* @return string|NULL
*/
public function getContents()
{
// future implementation can try to work around safe_mode and open_basedir limitations
return $this->isOk() ? file_get_contents($this->tmpName) : NULL;
}
}
|