|
From: <be...@us...> - 2014-04-23 04:58:56
|
Revision: 12466
http://sourceforge.net/p/xoops/svn/12466
Author: beckmi
Date: 2014-04-23 04:58:51 +0000 (Wed, 23 Apr 2014)
Log Message:
-----------
updated XoopsMediaUploader to use system memory values from php.ini, and to offer random file names (mamba/luciorota)
Modified Paths:
--------------
XoopsCore/branches/2.5.x/2.5.7/docs/changelog.250.txt
XoopsCore/branches/2.5.x/2.5.7/htdocs/class/uploader.php
Modified: XoopsCore/branches/2.5.x/2.5.7/docs/changelog.250.txt
===================================================================
--- XoopsCore/branches/2.5.x/2.5.7/docs/changelog.250.txt 2014-04-21 13:26:32 UTC (rev 12465)
+++ XoopsCore/branches/2.5.x/2.5.7/docs/changelog.250.txt 2014-04-23 04:58:51 UTC (rev 12466)
@@ -3,10 +3,10 @@
===============================
2014/xx/xx: Version 2.5.7 Final
===============================
+- updated XoopsMediaUploader to use system memory values from php.ini, and to offer random file names (mamba/luciorota)
-
===============================
-2014/xx/xx: Version 2.5.7 RC 1
+2014/04/06: Version 2.5.7 RC 1
===============================
Bugfixes:
Modified: XoopsCore/branches/2.5.x/2.5.7/htdocs/class/uploader.php
===================================================================
--- XoopsCore/branches/2.5.x/2.5.7/htdocs/class/uploader.php 2014-04-21 13:26:32 UTC (rev 12465)
+++ XoopsCore/branches/2.5.x/2.5.7/htdocs/class/uploader.php 2014-04-23 04:58:51 UTC (rev 12466)
@@ -101,6 +101,7 @@
14 => 'iff' ,
15 => 'wbmp' ,
16 => 'xbm');
+ var $randomFilename = false;
/**
* Constructor
@@ -111,10 +112,9 @@
* @param int $maxWidth
* @param int $maxHeight
*
- * @return bool
- * @internal param int $cmodvalue
*/
- function XoopsMediaUploader($uploadDir, $allowedMimeTypes, $maxFileSize = 0, $maxWidth = null, $maxHeight = null)
+
+ public function __construct($uploadDir, $allowedMimeTypes, $maxFileSize = 0, $maxWidth = null, $maxHeight = null, $randomFilename = false)
{
$this->extensionToMime = include $GLOBALS['xoops']->path('include/mimetypes.inc.php');
if (!is_array($this->extensionToMime)) {
@@ -126,20 +126,74 @@
$this->allowedMimeTypes =& $allowedMimeTypes;
}
$this->uploadDir = $uploadDir;
- $this->maxFileSize = intval($maxFileSize);
+
+ if (isset($maxFileSize) && $maxFileSize > 0) {
+ $maxFileSizeInBytes = $this->return_bytes($maxFileSize);
+ $maxUploadInBytes = $this->return_bytes(ini_get('upload_max_filesize'));
+ $maxPostInBytes = $this->return_bytes(ini_get('post_max_size'));
+ $memoryLimitInBytes = $this->return_bytes(ini_get('memory_limit'));
+ $newMaxFileSize = min($maxFileSizeInBytes, $maxUploadInBytes, $maxPostInBytes, $memoryLimitInBytes);
+ $this->maxFileSize = $newMaxFileSize;
+ } else {
+ $this->maxFileSize = $maxFileSize;
+ }
+
+
+
if (isset($maxWidth)) {
$this->maxWidth = intval($maxWidth);
}
if (isset($maxHeight)) {
$this->maxHeight = intval($maxHeight);
}
-
+ if (isset($randomFilename)) {
+ $this->randomFilename = $randomFilename;
+ }
if (!include_once $GLOBALS['xoops']->path('language/' . $GLOBALS['xoopsConfig']['language'] . '/uploader.php')) {
include_once $GLOBALS['xoops']->path('language/english/uploader.php');
}
}
+
/**
+ * Constructor
+ *
+ * @param string $uploadDir
+ * @param array $allowedMimeTypes
+ * @param int $maxFileSize
+ * @param int $maxWidth
+ * @param int $maxHeight
+ *
+ */
+
+
+ function XoopsMediaUploader($uploadDir, $allowedMimeTypes, $maxFileSize = 0, $maxWidth = 0, $maxHeight = null, $randomFilename = false) {
+
+ $this->__construct($uploadDir, $allowedMimeTypes, $maxFileSize, $maxWidth, $maxHeight, $randomFilename );
+
+ }
+
+
+ /**
+ * converts memory/file sizes as defined in php.ini to bytes
+ *
+ * @param $size_str
+ *
+ * @return int
+ */
+ function return_bytes($size_str)
+ {
+ switch (substr ($size_str, -1))
+ {
+ case 'K': case 'k': return (int)$size_str * 1024;
+ case 'M': case 'm': return (int)$size_str * 1048576;
+ case 'G': case 'g': return (int)$size_str * 1073741824;
+ default: return $size_str;
+ }
+ }
+
+
+ /**
* Fetch the uploaded file
*
* @param string $media_name Name of the file field
@@ -160,6 +214,10 @@
} else if (is_array($_FILES[$media_name]['name']) && isset($index)) {
$index = intval($index);
$this->mediaName = (get_magic_quotes_gpc()) ? stripslashes($_FILES[$media_name]['name'][$index]) : $_FILES[$media_name]['name'][$index];
+ if ($this->randomFilename) {
+ $unique = uniqid(time());
+ $this->mediaName = '' . $unique . '--' . $this->mediaName;
+ }
$this->mediaType = $_FILES[$media_name]['type'][$index];
$this->mediaSize = $_FILES[$media_name]['size'][$index];
$this->mediaTmpName = $_FILES[$media_name]['tmp_name'][$index];
@@ -167,6 +225,10 @@
} else {
$media_name =& $_FILES[$media_name];
$this->mediaName = (get_magic_quotes_gpc()) ? stripslashes($media_name['name']) : $media_name['name'];
+ if ($this->randomFilename) {
+ $unique = uniqid(time());
+ $this->mediaName = '' . $unique . '--' . $this->mediaName;
+ }
$this->mediaType = $media_name['type'];
$this->mediaSize = $media_name['size'];
$this->mediaTmpName = $media_name['tmp_name'];
@@ -353,6 +415,10 @@
$this->savedFileName = strtolower($this->mediaName);
}
+ $this->savedFileName = iconv("UTF-8", "ASCII//TRANSLIT", $this->savedFileName);
+ $this->savedFileName = preg_replace('!\s+!', '_', $this->savedFileName);
+ $this->savedFileName = preg_replace("/[^a-zA-Z0-9\._-]/", "", $this->savedFileName);
+
$this->savedDestination = $this->uploadDir . '/' . $this->savedFileName;
if (!move_uploaded_file($this->mediaTmpName, $this->savedDestination)) {
$this->setErrors(sprintf(_ER_UP_FAILEDSAVEFILE, $this->savedDestination));
|