Rajout du dossier core + MAJ .gitignore

This commit is contained in:
2019-11-21 12:48:42 +01:00
parent f4aabcb9b1
commit 459f8966b0
10448 changed files with 1835600 additions and 1 deletions

View File

@@ -0,0 +1,22 @@
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
namespace Thelia\Files\Exception;
/**
* Class ProcessFileException
* @package Thelia\Files\Exception
* @author manuel raynaud <manu@raynaud.io>
*/
class ProcessFileException extends \RuntimeException
{
}

View File

@@ -0,0 +1,51 @@
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
namespace Thelia\Files;
/**
* Class FileConfiguration
* @package Thelia\Files
* @author manuel raynaud <manu@raynaud.io>
*/
class FileConfiguration
{
public static function getImageConfig()
{
return [
'objectType' => 'image',
'validMimeTypes' => [
'image/jpeg' => ["jpg", "jpeg"],
'image/png' => ["png"],
'image/gif' => ["gif"],
],
'extBlackList' => []
];
}
public static function getDocumentConfig()
{
return [
'objectType' => 'document',
'validMimeTypes' => [],
'extBlackList' => [
"php",
"php3",
"php4",
"php5",
"php6",
"asp",
"aspx",
]
];
}
}

View File

@@ -0,0 +1,269 @@
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
namespace Thelia\Files;
use Propel\Runtime\Connection\ConnectionInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Thelia\Core\Event\File\FileCreateOrUpdateEvent;
use Thelia\Exception\FileException;
use Thelia\Exception\ImageException;
/**
* File Manager
*
* @package File
* @author Guillaume MOREL <gmorel@openstudio.fr>, Franck Allimant <franck@cqfdev.fr>
*
*/
class FileManager
{
protected $supportedFileModels = array();
/**
* Create a new FileManager instance.
*
* @param array $supportedFileModels The key should have form type.parent, where type is the file type (document or image) and parent is the parent object of the file, form example product, brand, folder, etc.
*/
public function __construct($supportedFileModels)
{
$this->supportedFileModels = $supportedFileModels;
}
/**
* Create the file type identifier, to access the related class in the supportedFileModels table.
*
* @param string $fileType the file type, e.g. document or image.
* @param string $parentType the parent object type, e.g. product, folder, brand, etc.
* @return string
*/
protected function getFileTypeIdentifier($fileType, $parentType)
{
return strtolower("$fileType.$parentType");
}
/**
* Create a new FileModelInterface instance, from the supportedFileModels table
*
* @param string $fileType the file type, such as document, image, etc.
* @param string $parentType the parent type, such as product, category, etc.
*
* @return FileModelInterface a file model interface instance
*
* @throws FileException if the file type is not supported, or if the class does not implements FileModelInterface
*/
public function getModelInstance($fileType, $parentType)
{
if (! isset($this->supportedFileModels[$this->getFileTypeIdentifier($fileType, $parentType)])) {
throw new FileException(
sprintf("Unsupported file type '%s' for parent type '%s'", $fileType, $parentType)
);
}
$className = $this->supportedFileModels[$this->getFileTypeIdentifier($fileType, $parentType)];
$instance = new $className;
if (! $instance instanceof FileModelInterface) {
throw new FileException(
sprintf(
"Wrong class type for file type '%s', parent type '%s'. Class '%s' should implements FileModelInterface",
$fileType,
$parentType,
$className
)
);
}
return $instance;
}
/**
* A a new FileModelInterface class name to the supported class list.
*
* @param string $fileType the file type, such as document, image, etc.
* @param string $parentType the parent type, such as Product, Category, etc.
* @param string $fullyQualifiedClassName the fully qualified class name
*/
public function addFileModel($fileType, $parentType, $fullyQualifiedClassName)
{
$this->supportedFileModels[$this->getFileTypeIdentifier($fileType, $parentType)] = $fullyQualifiedClassName;
}
/**
* Copy UploadedFile into the server storage directory
*
* @param FileModelInterface $model Model saved
* @param UploadedFile $uploadedFile Ready to be uploaded file
* @param ConnectionInterface $con current transaction with database
*
* @throws \Thelia\Exception\ImageException
* @return UploadedFile|null
*/
public function copyUploadedFile(FileModelInterface $model, UploadedFile $uploadedFile, ConnectionInterface $con = null)
{
$newUploadedFile = null;
if ($uploadedFile !== null) {
$directory = $model->getUploadDir();
$fileName = $this->renameFile($model->getId(), $uploadedFile);
$newUploadedFile = $uploadedFile->move($directory, $fileName);
$model->setFile($fileName);
if (!$model->save($con)) {
throw new ImageException(
sprintf(
'Failed to update model after copy of uploaded file %s to %s',
$uploadedFile,
$model->getFile()
)
);
}
}
return $newUploadedFile;
}
/**
* Save file into the database
*
* @param int $parentId the parent object ID
* @param FileModelInterface $fileModel the file model object (image or document) to save.
*
* @return int number of modified rows in database
*
* @throws \Thelia\Exception\ImageException
*/
protected function saveFile($parentId, FileModelInterface $fileModel)
{
$nbModifiedLines = 0;
if ($fileModel->getFile() !== null) {
$fileModel->setParentId($parentId);
$nbModifiedLines = $fileModel->save();
if (!$nbModifiedLines) {
throw new ImageException(
sprintf(
'Failed to update %s file model',
$fileModel->getFile()
)
);
}
}
return $nbModifiedLines;
}
/**
* Save file into the database
*
* @param FileCreateOrUpdateEvent $event the event
* @param FileModelInterface $imageModel the file model object (image or document) to save.
*
* @return int number of modified rows in database
*/
public function saveImage(FileCreateOrUpdateEvent $event, FileModelInterface $imageModel)
{
return $this->saveFile($event->getParentId(), $imageModel);
}
/**
* Save file into the database
*
* @param FileCreateOrUpdateEvent $event the event
* @param FileModelInterface $documentModel the file model object (image or document) to save.
*
* @return int number of modified rows in database
*/
public function saveDocument(FileCreateOrUpdateEvent $event, FileModelInterface $documentModel)
{
return $this->saveFile($event->getParentId(), $documentModel);
}
/**
* Sanitizes a filename replacing whitespace with dashes
*
* Removes special characters that are illegal in filenames on certain
* operating systems and special characters requiring special escaping
* to manipulate at the command line.
*
* @param string $string The filename to be sanitized
*
* @return string The sanitized filename
*/
public function sanitizeFileName($string)
{
return strtolower(preg_replace('/[^a-zA-Z0-9-_\.]/', '', $string));
}
/**
* Delete image from file storage and database
*
* @param FileModelInterface $model File being deleted
*/
public function deleteFile(FileModelInterface $model)
{
$url = $model->getUploadDir() . DS . $model->getFile();
@unlink(str_replace('..', '', $url));
$model->delete();
}
/**
* Rename file with image model id
*
* @param int $modelId Model id
* @param UploadedFile $uploadedFile File being saved
*
* @return string
*/
public function renameFile($modelId, UploadedFile $uploadedFile)
{
$extension = $uploadedFile->getClientOriginalExtension();
if (!empty($extension)) {
$extension = '.' . strtolower($extension);
}
$fileName = $this->sanitizeFileName(
str_replace(
$extension,
'',
$uploadedFile->getClientOriginalName()
) . '-' . $modelId . $extension
);
return $fileName;
}
/**
* Check if a file is an image
* Check based on mime type
*
* @param string $mimeType File mime type
*
* @return bool
*/
public function isImage($mimeType)
{
$isValid = false;
$allowedType = array('image/jpeg' , 'image/png' ,'image/gif');
if (in_array($mimeType, $allowedType)) {
$isValid = true;
}
return $isValid;
}
}

View File

@@ -0,0 +1,153 @@
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
namespace Thelia\Files;
use Propel\Runtime\ActiveQuery\ModelCriteria;
use Thelia\Core\HttpFoundation\Request;
use Thelia\Form\BaseForm;
interface FileModelInterface
{
/**
* Set file parent id
*
* @param int $parentId parent id
*
* @return $this
*/
public function setParentId($parentId);
/**
* Get file parent id
*
* @return int parent id
*/
public function getParentId();
/**
* @return string the file name
*/
public function getFile();
/**
* @param string $file the file name
*/
public function setFile($file);
/**
* @return FileModelParentInterface the parent file model
*/
public function getParentFileModel();
/**
* Get the ID of the form used to change this object information
*
* @return BaseForm the form
*/
public function getUpdateFormId();
/**
* @return string the path to the upload directory where files are stored, without final slash
*/
public function getUploadDir();
/**
* @param int $objectId the object ID
*
* @return string the URL to redirect to after update from the back-office
*/
public function getRedirectionUrl();
/**
* Get the Query instance for this object
*
* @return ModelCriteria
*/
public function getQueryInstance();
/**
* Save the model object.
*
* @return mixed
*/
public function save();
/**
* Delete the model object.
*
* @return mixed
*/
public function delete();
/**
* Get the model object ID
*
* @return int
*/
public function getId();
/**
* Set the current title
*
* @param string $title the title in the current locale
*/
public function setTitle($title);
/**
* Get the current title
*
* @param string $title the title in the current locale
* @return FileModelInterface
*/
public function getTitle();
/**
* Set the chapo
*
* @param string $chapo the chapo in the current locale
* @return FileModelInterface
*/
public function setChapo($chapo);
/**
* Set the description
*
* @param string $description the description in the current locale
* @return FileModelInterface
*/
public function setDescription($description);
/**
* Set the postscriptum
*
* @param string $postscriptum the postscriptum in the current locale
* @return FileModelInterface
*/
public function setPostscriptum($postscriptum);
/**
* Set the current locale
*
* @param string $locale the locale string
* @return FileModelInterface
*/
public function setLocale($locale);
/**
* Set the current locale
*
* @param bool $visible true if the file is visible, false otherwise
* @return FileModelInterface
*/
public function setVisible($visible);
}

View File

@@ -0,0 +1,18 @@
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
namespace Thelia\Files;
interface FileModelParentInterface
{
public function getTitle();
}