XOOPS RMCommon Utilities  2.1.8.91RC
 All Classes Namespaces Files Functions Variables
uploader.php
Go to the documentation of this file.
1 <?php
2 // $Id: uploader.php 999 2012-07-02 03:53:17Z i.bitcero $
3 // --------------------------------------------------------------
4 // Red México Common Utilities
5 // A framework for Red México Modules
6 // Author: Eduardo Cortés <i.bitcero@gmail.com>
7 // Email: i.bitcero@gmail.com
8 // License: GPL 2.0
9 // --------------------------------------------------------------
10 
11 include_once XOOPS_ROOT_PATH.'/class/uploader.php';
12 
13 class RMFileUploader extends XoopsMediaUploader
14 {
23  public function __construct($uploadDir, $maxFileSize, $allowed_exts = array()){
24 
25  //$this->XoopsMediaUploader($dir, $allowedtypes, $maxsize);
26  $this->extensionToMime = include $GLOBALS['xoops']->path('include/mimetypes.inc.php');
27  $ev = RMEvents::get();
28  $this->extensionToMime =$ev->run_event('rmcommon.get.mime.types', $this->extensionToMime);
29  if (!is_array($this->extensionToMime)) {
30  $this->extensionToMime = array();
31  return false;
32  }
33  if (is_array($allowed_exts)) {
34  foreach($allowed_exts as $ext){
35 
36  $this->allowedMimeTypes[] = $this->extensionToMime[$ext];
37 
38  }
39  }
40 
41  $this->uploadDir = $uploadDir;
42  $this->maxFileSize = intval($maxFileSize);
43  if (isset($maxWidth)) {
44  $this->maxWidth = intval($maxWidth);
45  }
46  if (isset($maxHeight)) {
47  $this->maxHeight = intval($maxHeight);
48  }
49 
50  if (!@include_once $GLOBALS['xoops']->path('language/' . $GLOBALS['xoopsConfig']['language'] . '/uploader.php')) {
51  include_once $GLOBALS['xoops']->path('language/english/uploader.php');
52  }
53 
54  }
55 
56  function _copyFile($chmod){
57 
58  $matched = array();
59  if (!preg_match("/\.([a-zA-Z0-9]+)$/", $this->mediaName, $matched)) {
60  $this->setErrors(_ER_UP_INVALIDFILENAME);
61  return false;
62  }
63  if (isset($this->targetFileName)) {
64  $this->savedFileName = $this->targetFileName;
65  } else if (isset($this->prefix)) {
66  $this->savedFileName = uniqid($this->prefix) . '.' . strtolower($matched[1]);
67  } else {
68  $this->savedFileName = strtolower($this->mediaName);
69  }
70 
71  $fdata = pathinfo($this->savedFileName);
72  $this->savedFileName = TextCleaner::sweetstring($fdata['filename']).($fdata['extension']!='' ? '.'.$fdata['extension'] : '');
73  $fdata = pathinfo($this->savedFileName);
74 
75  if (file_exists($this->uploadDir . '/' . $this->savedFileName)){
76  $num = 1;
77  while(file_exists($this->uploadDir . '/' . $this->savedFileName)){
78  $this->savedFileName = $fdata['filename'].'-'.$num.($fdata['extension']!='' ? '.'.$fdata['extension'] : '');
79  $num++;
80  }
81  }
82 
83  $this->savedDestination = $this->uploadDir . '/' . $this->savedFileName;
84  if (!move_uploaded_file($this->mediaTmpName, $this->savedDestination)) {
85  $this->setErrors(sprintf(_ER_UP_FAILEDSAVEFILE, $this->savedDestination));
86  return false;
87  }
88  // Check IE XSS before returning success
89  $ext = strtolower(substr(strrchr($this->savedDestination, '.'), 1));
90  if (in_array($ext, $this->imageExtensions)) {
91  $info = @getimagesize($this->savedDestination);
92  if ($info === false || $this->imageExtensions[(int) $info[2]] != $ext) {
93  $this->setErrors(_ER_UP_SUSPICIOUSREFUSED);
94  @unlink($this->savedDestination);
95  return false;
96  }
97  }
98  @chmod($this->savedDestination, $chmod);
99  return true;
100 
101  }
102 
103  public function getMIME($extension){
104  print_r($this->extensionToMime); die();
105  }
106 
107 }