Skip to content

Commit

Permalink
Fixed #497: Allow non-latin and additional characters in filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
luke- committed Jan 25, 2016
1 parent 3ed8fad commit 40d2d7c
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 42 deletions.
16 changes: 8 additions & 8 deletions protected/humhub/modules/file/controllers/FileController.php
Expand Up @@ -147,35 +147,35 @@ public function actionDownload()
throw new HttpException(401, Yii::t('FileModule.controllers_FileController', 'Insufficient permissions!'));
}

$filePath = $file->getPath();
$fileName = $file->getFilename($suffix);

if (!file_exists($filePath . DIRECTORY_SEPARATOR . $fileName)) {
if (!file_exists($file->getStoredFilePath($suffix))) {
throw new HttpException(404, Yii::t('FileModule.controllers_FileController', 'Could not find requested file!'));
}

$options = [
'inline' => false,
'mimeType' => FileHelper::getMimeTypeByExtension($fileName)
'mimeType' => FileHelper::getMimeTypeByExtension($file->getFilename($suffix))
];

if ($download != 1 && in_array($options['mimeType'], Yii::$app->getModule('file')->inlineMimeTypes)) {
$options['inline'] = true;
}

if (!Setting::Get('useXSendfile', 'file')) {
Yii::$app->response->sendFile($filePath . DIRECTORY_SEPARATOR . $fileName, $fileName, $options);
Yii::$app->response->sendFile($file->getStoredFilePath($suffix), $file->getFilename($suffix), $options);
} else {
$filePath = $file->getStoredFilePath($suffix);

if (strpos($_SERVER['SERVER_SOFTWARE'], 'nginx') === 0) {
// set nginx specific X-Sendfile header name
$options['xHeader'] = 'X-Accel-Redirect';
// make path relative to docroot
$docroot = rtrim($_SERVER['DOCUMENT_ROOT'], DIRECTORY_SEPARATOR);
if (substr($filePath, 0, strlen($docroot)) == $docroot) {
$filePath = substr($filePath, strlen($docroot));
$filePath = substr($file, strlen($docroot));
}
}
Yii::$app->response->xSendFile($filePath . DIRECTORY_SEPARATOR . $fileName, null, $options);

Yii::$app->response->xSendFile($filePath, $file->getFilename($suffix), $options);
}
}

Expand Down
@@ -0,0 +1,38 @@
<?php

use yii\db\Schema;
use yii\db\Migration;

class m160125_053702_stored_filename extends Migration
{
public function up()
{
foreach (\humhub\modules\file\models\File::find()->all() as $file) {

$oldFileName = $file->getPath() . DIRECTORY_SEPARATOR . $file->getFileName();
$newFileName = $file->getPath() . DIRECTORY_SEPARATOR . 'file';

if (!file_exists($newFileName) && file_exists($oldFileName) && is_writable($file->getPath())) {
rename($oldFileName, $newFileName);
}
}
}

public function down()
{
echo "m160125_053702_stored_filename cannot be reverted.\n";

return false;
}

/*
// Use safeUp/safeDown to run migration code within a transaction
public function safeUp()
{
}
public function safeDown()
{
}
*/
}
63 changes: 31 additions & 32 deletions protected/humhub/modules/file/models/File.php
Expand Up @@ -140,8 +140,7 @@ public function afterSave($insert, $changedAttributes)
{
// Set new uploaded file
if ($this->uploadedFile !== null && $this->uploadedFile instanceof UploadedFile) {
$newFilename = $this->getPath() . DIRECTORY_SEPARATOR . $this->getFilename();

$newFilename = $this->getStoredFilePath();
if (is_uploaded_file($this->uploadedFile->tempName)) {
move_uploaded_file($this->uploadedFile->tempName, $newFilename);
@chmod($newFilename, 0744);
Expand All @@ -158,33 +157,14 @@ public function afterSave($insert, $changedAttributes)

// Set file by given contents
if ($this->newFileContent != null) {
$newFilename = $this->getPath() . DIRECTORY_SEPARATOR . $this->getFilename();
$newFilename = $this->getStoredFilePath();
file_put_contents($newFilename, $this->newFileContent);
@chmod($newFilename, 0744);
}

return parent::afterSave($insert, $changedAttributes);
}

/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => Yii::t('FileModule.models_File', 'ID'),
'guid' => Yii::t('FileModule.models_File', 'Guid'),
'file_name' => Yii::t('FileModule.models_File', 'File name'),
'title' => Yii::t('FileModule.models_File', 'Title'),
'mime_type' => Yii::t('FileModule.models_File', 'Mime Type'),
'size' => Yii::t('FileModule.models_File', 'Size'),
'created_at' => Yii::t('FileModule.models_File', 'Created at'),
'created_by' => Yii::t('FileModule.models_File', 'Created By'),
'updated_at' => Yii::t('FileModule.models_File', 'Updated at'),
'updated_by' => Yii::t('FileModule.models_File', 'Updated by'),
);
}

/**
* Returns the Path of the File
*/
Expand Down Expand Up @@ -225,19 +205,39 @@ public function getUrl($suffix = "", $absolute = true)
/**
* Returns the filename
*
* @param string $prefix
* @param string $suffix
* @return string
*/
public function getFilename($prefix = "")
public function getFilename($suffix = "")
{
// without prefix
if ($prefix == "") {
if ($suffix == "") {
return $this->file_name;
}

$fileParts = pathinfo($this->file_name);

return $fileParts['filename'] . "_" . $prefix . "." . $fileParts['extension'];
return $fileParts['filename'] . "_" . $suffix . "." . $fileParts['extension'];
}

/**
* Returns the file and path to the stored file
*/
public function getStoredFilePath($suffix = '')
{
/*
// Fallback for older versions
$oldFile = $this->getPath() . DIRECTORY_SEPARATOR . $this->getFilename($suffix);
if (file_exists($oldFile)) {
return $oldFile;
}
*
*/

$suffix = preg_replace("/[^a-z0-9_]/i", "", $suffix);

$file = ($suffix == '') ? 'file' : $suffix;
return $this->getPath() . DIRECTORY_SEPARATOR . $file;
}

public function getMimeBaseType()
Expand All @@ -262,14 +262,14 @@ public function getMimeSubType()

public function getPreviewImageUrl($maxWidth = 1000, $maxHeight = 1000)
{
$prefix = 'pi_' . $maxWidth . "x" . $maxHeight;
$suffix = 'pi_' . $maxWidth . "x" . $maxHeight;

$originalFilename = $this->getPath() . DIRECTORY_SEPARATOR . $this->getFilename();
$previewFilename = $this->getPath() . DIRECTORY_SEPARATOR . $this->getFilename($prefix);
$originalFilename = $this->getStoredFilePath();
$previewFilename = $this->getStoredFilePath($suffix);

// already generated
if (is_file($previewFilename)) {
return $this->getUrl($prefix);
return $this->getUrl($suffix);
}

// Check file exists & has valid mime type
Expand All @@ -290,7 +290,7 @@ public function getPreviewImageUrl($maxWidth = 1000, $maxHeight = 1000)
}

ImageConverter::Resize($originalFilename, $previewFilename, array('mode' => 'max', 'width' => $maxWidth, 'height' => $maxHeight));
return $this->getUrl($prefix);
return $this->getUrl($suffix);
}

public function getExtension()
Expand Down Expand Up @@ -350,7 +350,6 @@ public function setUploadedFile(UploadedFile $uploadedFile)
public function sanitizeFilename()
{
$this->file_name = trim($this->file_name);
$this->file_name = preg_replace("/[^a-z0-9_\-s\. ]/i", "", $this->file_name);

// Ensure max length
$pathInfo = pathinfo($this->file_name);
Expand Down
Expand Up @@ -19,6 +19,6 @@

<script>
<?php foreach ($files as $file): ?>
addToUploadList("<?php echo $uploaderId; ?>", "<?php echo $file->guid; ?>", "<?php echo $file->file_name; ?>", "<?php echo MimeHelper::getMimeIconClassByExtension($file->getExtension()); ?>");
addToUploadList("<?php echo $uploaderId; ?>", "<?php echo $file->guid; ?>", "<?php echo Html::encode($file->file_name); ?>", "<?php echo MimeHelper::getMimeIconClassByExtension($file->getExtension()); ?>");
<?php endforeach; ?>
</script>
3 changes: 2 additions & 1 deletion protected/humhub/modules/file/widgets/views/showFiles.php
@@ -1,5 +1,6 @@
<?php

use yii\helpers\Html;
use humhub\libs\Helpers;

$object = $this->context->object;
Expand Down Expand Up @@ -36,7 +37,7 @@
?>
<li class="mime <?php echo \humhub\libs\MimeHelper::getMimeIconClassByExtension($file->getExtension()); ?>"><a
href="<?php echo $file->getUrl(); ?>" target="_blank"><span
class="filename"><?php echo Helpers::trimText($file->file_name, 40); ?></span></a>
class="filename"><?php echo Html::encode(Helpers::trimText($file->file_name, 40)); ?></span></a>
<span class="time" style="padding-right: 20px;"> - <?php echo Yii::$app->formatter->asSize($file->size); ?></span>

<?php if ($file->getExtension() == "mp3") : ?>
Expand Down

0 comments on commit 40d2d7c

Please sign in to comment.