Commit 7f8719e3 authored by Ghislain Loas's avatar Ghislain Loas

Merge branch 'hotline#72344_lille_metro_pb_explorateur_de_fichier' into 'hotline'

hotline #72344 : add filename validator

See merge request afi/opacce!2530
parents 3b1b132c 37912665
- ticket #72344 : Explorateur de fichier : Ajout de la vérification du format du nom de fichier téléversé
\ No newline at end of file
......@@ -43,6 +43,10 @@ class ZendAfi_Form_Admin_FileManager_Import extends ZendAfi_Form {
$extensions = Class_AdminVar::get('ALLOWED_FILES_EXTENSIONS_FOR_IMPORT');
return $element
->addValidator((new ZendAfi_Validate_FileName(Class_FileManager::REGEX_NAME))
->setMessage($form->_('Le nom doit contenir uniquement des lettres, des chiffres et les caratères "_", "-", ".". Exemple : "mon_fichier.jpg"'),
ZendAfi_Validate_FileName::NOT_MATCH))
->addValidator((new Zend_Validate_File_Count(1))
->setMessage($form->_('Un seul fichier doit être transféré.')))
......
<?php
/**
* Copyright (c) 2012-2017, Agence Française Informatique (AFI). All rights reserved.
*
* BOKEH is free software; you can redistribute it and/or modify
* it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
* the Free Software Foundation.
*
* There are special exceptions to the terms and conditions of the AGPL as it
* is applied to this software (see README file).
*
* BOKEH is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
* along with BOKEH; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class ZendAfi_Validate_FileName extends Zend_Validate_Abstract{
protected $_pattern;
/**
* @const string Error constants
*/
const NOT_MATCH = 'fileNameDoesNotMatch';
/**
* @var array Error message templates
*/
protected $_messageTemplates =
[ self::NOT_MATCH => "The name '%value%' does not match '%pattern%' pattern" ];
/**
* @var array
*/
protected $_messageVariables = ['pattern' => '_pattern'];
public function __construct($pattern) {
$this->_pattern = $pattern;
}
public function isValid($temp_name, $file_infos = null) {
$status = @preg_match($this->_pattern, $file_infos['name']);
if (!$status) {
$this->_throw($file_infos, static::NOT_MATCH);
return false;
}
return true;
}
/**
* Throws an error of the given type
*
* @param string $file
* @param string $errorType
* @return false
*/
protected function _throw($file, $errorType)
{
if ($file !== null) {
$this->_value = $file['name'];
}
$this->_error($errorType);
return false;
}
}
......@@ -676,18 +676,14 @@ class FileManagerControllerWallDispatchTest extends FileManagerControllerTestCas
class FileManagerControllerImportCssDispatchTest extends FileManagerControllerTestCase {
public function setUp() {
parent::setUp();
$file = ['file' => ['name' => 'stylesheet.css',
'type' => 'image/jpeg',
'size' => 126976,
'tmp_name' => 'userfiles/stylesheet.css',
'destination' => 'userfiles',
'validators' => []]];
$files = ['file' => ['name' => 'stylesheet.css',
'type' => 'image/jpeg',
'size' => 126976,
'tmp_name' => 'xxxxx/php8iuxxajl']];
ZendAfi_Form_Admin_FileManager_Import::setTransferAdapter((new FileManagerController_Mock_TransferAdapter())
->setFiles($file));
ZendAfi_Form_Admin_FileManager_Import::setTransferAdapter((new FileManagerController_Mock_TransferAdapter())->setFiles($files));
ZendAfi_Form_Admin_FileManager_Import::setValidators(null);
$imported_file = (new Class_FileManager)
......
<?php
/**
* Copyright (c) 2012-2017, Agence Française Informatique (AFI). All rights reserved.
*
* BOKEH is free software; you can redistribute it and/or modify
* it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
* the Free Software Foundation.
*
* There are special exceptions to the terms and conditions of the AGPL as it
* is applied to this software (see README file).
*
* BOKEH is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
* along with BOKEH; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class ZendAfi_Validate_FileNameTest extends ModelTestCase {
public function setUp() {
parent::setUp();
$this->_validate = new ZendAfi_Validate_FileName('/[a-z]/i');
}
/** @test */
public function aShouldBeValid() {
$this->assertTrue($this->_validate->isValid('/tmp/phpzdvauie', ['name' => 'a']));
}
/** @test */
public function underscoreShouldNotBeValid() {
$this->assertFalse($this->_validate->isValid('/tmp/phpzdvauie', ['name' => '_']));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment