KumbiaPHP  beta2
Framework PHP
 Todo Estructuras de Datos Namespaces Archivos Funciones Variables Páginas
upload.php
Ir a la documentación de este archivo.
1 <?php
27 abstract class Upload
28 {
29 
35  protected $_name;
41  protected $_allowScripts = FALSE;
47  protected $_minSize = NULL;
53  protected $_maxSize = NULL;
59  protected $_types = NULL;
65  protected $_extensions = NULL;
71  protected $_overwrite = FALSE;
72 
78  public function __construct($name)
79  {
80  $this->_name = $name;
81  }
82 
88  public function setAllowScripts($value)
89  {
90  $this->_allowScripts = $value;
91  }
92 
98  public function setMinSize($size)
99  {
100  $this->_minSize = trim($size);
101  }
102 
108  public function setMaxSize($size)
109  {
110  $this->_maxSize = trim($size);
111  }
112 
118  public function setTypes($value)
119  {
120  if (!is_array($value))
121  $value = explode('|', $value);
122  $this->_types = $value;
123  }
124 
130  public function setExtensions($value)
131  {
132  if (!is_array($value))
133  $value = explode('|', $value);
134  $this->_extensions = $value;
135  }
136 
142  public function overwrite($value)
143  {
144  $this->_overwrite = (bool) $value;
145  }
146 
153  protected function _beforeSave($name)
154  {
155 
156  }
157 
163  protected function _afterSave($name)
164  {
165 
166  }
167 
174  public function save($name = NULL)
175  {
176  if (!$this->isUploaded()) {
177  return FALSE;
178  }
179  if (!$name) {
180  $name = $_FILES[$this->_name]['name'];
181  } else {
182  $name = $name . $this->_getExtension();
183  }
184  // Guarda el archivo
185  if ($this->_beforeSave($name) !== FALSE && $this->_overwrite($name) && $this->_validates() && $this->_saveFile($name)) {
186  $this->_afterSave($name);
187  return $name;
188  }
189  return FALSE;
190  }
191 
197  public function saveRandom()
198  {
199  // Genera el nombre de archivo
200  $name = md5(time());
201 
202  // Guarda el archivo
203  if ($this->save($name)) {
204  return $name . $this->_getExtension();
205  }
206 
207  return FALSE;
208  }
209 
215  public function isUploaded()
216  {
217  // Verifica si ha ocurrido un error al subir
218  if ($_FILES[$this->_name]['error'] > 0) {
219  $error = array(
220  UPLOAD_ERR_INI_SIZE => 'el archivo excede el tamaño máximo (' . ini_get('upload_max_filesize') . 'b) permitido por el servidor',
221  UPLOAD_ERR_FORM_SIZE => 'el archivo excede el tamaño máximo permitido',
222  UPLOAD_ERR_PARTIAL => 'se ha subido el archivo parcialmente',
223  UPLOAD_ERR_NO_FILE => 'no se ha subido ningún archivo',
224  UPLOAD_ERR_NO_TMP_DIR => 'no se encuentra el directorio de archivos temporales',
225  UPLOAD_ERR_CANT_WRITE => 'falló al escribir el archivo en disco',
226  UPLOAD_ERR_EXTENSION => 'una extensión de php ha detenido la subida del archivo'
227  );
228 
229  Flash::error('Error: ' . $error[$_FILES[$this->_name]['error']]);
230  return FALSE;
231  }
232  return TRUE;
233  }
234 
240  protected function _validates()
241  {
242  // Denegar subir archivos de scripts ejecutables
243  if (!$this->_allowScripts && preg_match('/\.(php|phtml|php3|php4|js|shtml|pl|py|rb|rhtml)$/i', $_FILES[$this->_name]['name'])) {
244  Flash::error('Error: no esta permitido subir scripts ejecutables');
245  return FALSE;
246  }
247 
248  // Valida el tipo de archivo
249  if ($this->_types !== NULL && !$this->_validatesTypes()) {
250  Flash::error('Error: el tipo de archivo no es válido');
251  return FALSE;
252  }
253 
254  // Valida extensión del archivo
255  if ($this->_extensions !== NULL && !preg_match('/\.(' . implode('|', $this->_extensions) . ')$/i', $_FILES[$this->_name]['name'])) {
256  Flash::error('Error: la extensión del archivo no es válida');
257  return FALSE;
258  }
259 
260  // Verifica si es superior al tamaño indicado
261  if ($this->_maxSize !== NULL && $_FILES[$this->_name]['size'] > $this->_toBytes($this->_maxSize)) {
262  Flash::error("Error: no se admiten archivos superiores a $this->_maxSize" . 'b');
263  return FALSE;
264  }
265 
266  // Verifica si es inferior al tamaño indicado
267  if ($this->_minSize !== NULL && $_FILES[$this->_name]['size'] < $this->_toBytes($this->_minSize)) {
268  Flash::error("Error: no se admiten archivos inferiores a $this->_minSize" . 'b');
269  return FALSE;
270  }
271 
272  return TRUE;
273  }
274 
280  protected function _validatesTypes()
281  {
282  return in_array($_FILES[$this->_name]['type'], $this->_types);
283  }
284 
290  protected function _getExtension()
291  {
292  if($ext = pathinfo($_FILES[$this->_name]['name'], PATHINFO_EXTENSION)){
293  return '.'. $ext;
294  }
295  return NULL;
296  }
297 
303  protected function _overwrite($name)
304  {
305  if ($this->_overwrite) {
306  return TRUE;
307  }
308  if (file_exists("$this->_path/$name")) {
309  Flash::error('Error: ya existe este fichero. Y no se permite reescribirlo');
310  return FALSE;
311  }
312  return TRUE;
313  }
314 
321  protected function _toBytes($size)
322  {
323  if (is_int($size) || ctype_digit($size)) {
324  return (int) $size;
325  }
326 
327  $tipo = strtolower(substr($size, -1));
328  $size = (int) $size;
329 
330  switch ($tipo) {
331  case 'g': //Gigabytes
332  $size *= 1073741824;
333  break;
334  case 'm': //Megabytes
335  $size *= 1048576;
336  break;
337  case 'k': //Kilobytes
338  $size *= 1024;
339  break;
340  default :
341  $size = -1;
342  Flash::error('Error: el tamaño debe ser un int para bytes, o un string terminado con K, M o G. Ej: 30k , 2M, 2G');
343  }
344 
345  return $size;
346  }
347 
354  protected abstract function _saveFile($name);
355 
363  public static function factory($name, $adapter = 'file')
364  {
365  require_once dirname(__FILE__) . "/adapters/{$adapter}_upload.php";
366  $class = $adapter . 'upload';
367 
368  return new $class($name);
369  }
370 
371 }