| 1 | 1 |
deleted file mode 100644 |
| ... | ... |
@@ -1,332 +0,0 @@ |
| 1 |
-<?php |
|
| 2 |
- |
|
| 3 |
-declare(strict_types=1); |
|
| 4 |
- |
|
| 5 |
-/* |
|
| 6 |
- * This file is part of Oveleon ContaoMemberExtension Bundle. |
|
| 7 |
- * |
|
| 8 |
- * @package contao-member-extension-bundle |
|
| 9 |
- * @license MIT |
|
| 10 |
- * @author Sebastian Zoglowek <https://github.com/zoglo> |
|
| 11 |
- * @author Daniele Sciannimanica <https://github.com/doishub> |
|
| 12 |
- * @author Fabian Ekert <https://github.com/eki89> |
|
| 13 |
- * @copyright Oveleon <https://www.oveleon.de/> |
|
| 14 |
- */ |
|
| 15 |
- |
|
| 16 |
-namespace Oveleon\ContaoMemberExtensionBundle; |
|
| 17 |
- |
|
| 18 |
-use Contao\Config; |
|
| 19 |
-use Contao\CoreBundle\Monolog\ContaoContext; |
|
| 20 |
-use Contao\Dbafs; |
|
| 21 |
-use Contao\File; |
|
| 22 |
-use Contao\FilesModel; |
|
| 23 |
-use Contao\FileUpload; |
|
| 24 |
-use Contao\Frontend; |
|
| 25 |
-use Contao\FrontendUser; |
|
| 26 |
-use Contao\MemberModel; |
|
| 27 |
-use Contao\StringUtil; |
|
| 28 |
-use Contao\System; |
|
| 29 |
-use Contao\Validator; |
|
| 30 |
-use Psr\Log\LogLevel; |
|
| 31 |
- |
|
| 32 |
-/** |
|
| 33 |
- * Class Member |
|
| 34 |
- * |
|
| 35 |
- * @property int $avatar UUID of the avatar |
|
| 36 |
- */ |
|
| 37 |
-class Member extends Frontend |
|
| 38 |
-{
|
|
| 39 |
- const DEFAULT_PICTURE = 'bundles/contaomemberextension/avatar.png'; |
|
| 40 |
- |
|
| 41 |
- /** |
|
| 42 |
- * MemberAvatar file name |
|
| 43 |
- */ |
|
| 44 |
- protected string $avatarName = 'memberAvatar'; |
|
| 45 |
- |
|
| 46 |
- /** |
|
| 47 |
- * Create avatar for a member | Registration |
|
| 48 |
- */ |
|
| 49 |
- public function createAvatar(int $userId, array $arrData): void |
|
| 50 |
- {
|
|
| 51 |
- $objMember = MemberModel::findById($userId); |
|
| 52 |
- $this->processAvatar($objMember, $arrData); |
|
| 53 |
- } |
|
| 54 |
- |
|
| 55 |
- /** |
|
| 56 |
- * Update avatar of a member | Login |
|
| 57 |
- */ |
|
| 58 |
- public function updateAvatar(FrontendUser $objUser, array $arrData): void |
|
| 59 |
- {
|
|
| 60 |
- $objMember = MemberModel::findById($objUser->id); |
|
| 61 |
- $this->processAvatar($objMember, $arrData); |
|
| 62 |
- } |
|
| 63 |
- |
|
| 64 |
- /** |
|
| 65 |
- * Process avatar upload for a member |
|
| 66 |
- */ |
|
| 67 |
- protected function processAvatar(MemberModel $objMember, ?array $arrData): void |
|
| 68 |
- {
|
|
| 69 |
- $objMember = MemberModel::findByPk($objMember->id); |
|
| 70 |
- |
|
| 71 |
- if ($objMember === null) |
|
| 72 |
- {
|
|
| 73 |
- return; |
|
| 74 |
- } |
|
| 75 |
- |
|
| 76 |
- // ToDo: remove $_SESSION when contao 4.13 support ends (Contao ^5.* is not possible with Contao 4.* support) |
|
| 77 |
- $file = $_SESSION['FILES']['avatar']; |
|
| 78 |
- $maxlength_kb = $this->getMaximumUploadSize(); |
|
| 79 |
- $maxlength_kb_readable = $this->getReadableSize($maxlength_kb); |
|
| 80 |
- |
|
| 81 |
- // Sanitize the filename |
|
| 82 |
- try |
|
| 83 |
- {
|
|
| 84 |
- $file['name'] = StringUtil::sanitizeFileName($file['name']); |
|
| 85 |
- } |
|
| 86 |
- catch (\InvalidArgumentException $e) |
|
| 87 |
- {
|
|
| 88 |
- // ToDo: add error message for invalid characters |
|
| 89 |
- return; |
|
| 90 |
- } |
|
| 91 |
- |
|
| 92 |
- // Invalid file name |
|
| 93 |
- if (!Validator::isValidFileName($file['name'])) |
|
| 94 |
- {
|
|
| 95 |
- // ToDo: add error message for invalid characters |
|
| 96 |
- return; |
|
| 97 |
- } |
|
| 98 |
- |
|
| 99 |
- // File was not uploaded |
|
| 100 |
- if (!is_uploaded_file($file['tmp_name'])) |
|
| 101 |
- {
|
|
| 102 |
- // ToDo: Add error messages |
|
| 103 |
- /*if ($file['error'] == 1 || $file['error'] == 2) { // Add error message for maximum file size }
|
|
| 104 |
- elseif ($file['error'] == 3) { // Add error message for partial upload }
|
|
| 105 |
- elseif ($file['error'] > 0) { // Add error message for failed upload }*/
|
|
| 106 |
- |
|
| 107 |
- unset($_SESSION['FILES']['avatar']); |
|
| 108 |
- |
|
| 109 |
- return; |
|
| 110 |
- } |
|
| 111 |
- |
|
| 112 |
- // File is too big |
|
| 113 |
- if ($file['size'] > $maxlength_kb) |
|
| 114 |
- {
|
|
| 115 |
- // ToDo: add error message for maximum file size |
|
| 116 |
- unset($_SESSION['FILES']['avatar']); |
|
| 117 |
- |
|
| 118 |
- return; |
|
| 119 |
- } |
|
| 120 |
- |
|
| 121 |
- $objFile = new File($file['name']); |
|
| 122 |
- $uploadTypes = StringUtil::trimsplit(',', Config::get('validImageTypes'));
|
|
| 123 |
- |
|
| 124 |
- // File type is not allowed |
|
| 125 |
- if (!\in_array($objFile->extension, $uploadTypes)) |
|
| 126 |
- {
|
|
| 127 |
- // ToDo: add error message for not allowed file type |
|
| 128 |
- unset($_SESSION['FILES']['avatar']); |
|
| 129 |
- |
|
| 130 |
- return; |
|
| 131 |
- } |
|
| 132 |
- |
|
| 133 |
- if ($arrImageSize = @getimagesize($file['tmp_name'])) |
|
| 134 |
- {
|
|
| 135 |
- $intImageWidth = Config::get('imageWidth');
|
|
| 136 |
- |
|
| 137 |
- // Image exceeds maximum image width |
|
| 138 |
- if ($intImageWidth > 0 && $arrImageSize[0] > $intImageWidth) {
|
|
| 139 |
- // ToDo: add error message for exceeding width |
|
| 140 |
- unset($_SESSION['FILES']['avatar']); |
|
| 141 |
- |
|
| 142 |
- return; |
|
| 143 |
- } |
|
| 144 |
- |
|
| 145 |
- $intImageHeight = Config::get('imageHeight');
|
|
| 146 |
- |
|
| 147 |
- // Image exceeds maximum image height |
|
| 148 |
- if ($intImageHeight > 0 && $arrImageSize[1] > $intImageHeight) {
|
|
| 149 |
- // ToDo: add error message for exceeding height |
|
| 150 |
- unset($_SESSION['FILES']['avatar']); |
|
| 151 |
- |
|
| 152 |
- return; |
|
| 153 |
- } |
|
| 154 |
- } |
|
| 155 |
- |
|
| 156 |
- // Upload valid file type with no width and height -> svg |
|
| 157 |
- |
|
| 158 |
- // Don't upload if no homedir is assigned |
|
| 159 |
- // ToDo: Create homedir? |
|
| 160 |
- if (!$objMember->assignDir || !$objMember->homeDir) |
|
| 161 |
- {
|
|
| 162 |
- // ToDo: add error message for no homedir |
|
| 163 |
- return; |
|
| 164 |
- } |
|
| 165 |
- |
|
| 166 |
- $intUploadFolder = $objMember->homeDir; |
|
| 167 |
- |
|
| 168 |
- $objUploadFolder = FilesModel::findByUuid($intUploadFolder); |
|
| 169 |
- |
|
| 170 |
- // The upload folder could not be found |
|
| 171 |
- if ($objUploadFolder === null) |
|
| 172 |
- {
|
|
| 173 |
- throw new Exception("Invalid upload folder ID $intUploadFolder");
|
|
| 174 |
- } |
|
| 175 |
- |
|
| 176 |
- $strUploadFolder = $objUploadFolder->path; |
|
| 177 |
- |
|
| 178 |
- // Store the file if the upload folder exists |
|
| 179 |
- $projectDir = System::getContainer()->getParameter('kernel.project_dir');
|
|
| 180 |
- |
|
| 181 |
- if (!!$strUploadFolder & is_dir($projectDir . '/' . $strUploadFolder)) |
|
| 182 |
- {
|
|
| 183 |
- // Delete existing avatar if it exists |
|
| 184 |
- $this->deleteAvatar($objMember); |
|
| 185 |
- |
|
| 186 |
- $this->import('Files');
|
|
| 187 |
- |
|
| 188 |
- // Rename file |
|
| 189 |
- $file['name'] = $this->avatarName . '.' . $objFile->extension; |
|
| 190 |
- |
|
| 191 |
- // Move the file to its destination |
|
| 192 |
- $this->Files->move_uploaded_file($file['tmp_name'], $strUploadFolder . '/' . $file['name']); |
|
| 193 |
- $this->Files->chmod($strUploadFolder . '/' . $file['name'], 0666 & ~umask()); |
|
| 194 |
- |
|
| 195 |
- $strUuid = null; |
|
| 196 |
- $strFile = $strUploadFolder . '/' . $file['name']; |
|
| 197 |
- |
|
| 198 |
- |
|
| 199 |
- // Generate the DB entries |
|
| 200 |
- if (Dbafs::shouldBeSynchronized($strFile)) |
|
| 201 |
- {
|
|
| 202 |
- $objModel = FilesModel::findByPath($strFile); |
|
| 203 |
- |
|
| 204 |
- if ($objModel === null) |
|
| 205 |
- {
|
|
| 206 |
- $objModel = Dbafs::addResource($strFile); |
|
| 207 |
- } |
|
| 208 |
- |
|
| 209 |
- $strUuid = StringUtil::binToUuid($objModel->uuid); |
|
| 210 |
- |
|
| 211 |
- // Update the hash of the target folder |
|
| 212 |
- Dbafs::updateFolderHashes($strUploadFolder); |
|
| 213 |
- |
|
| 214 |
- // Update member avatar |
|
| 215 |
- $objMember->avatar = $objModel->uuid; |
|
| 216 |
- $objMember->save(); |
|
| 217 |
- } |
|
| 218 |
- |
|
| 219 |
- // Add the session entry |
|
| 220 |
- $_SESSION['FILES']['avatar'] = array |
|
| 221 |
- ( |
|
| 222 |
- 'name' => $file['name'], |
|
| 223 |
- 'type' => $file['type'], |
|
| 224 |
- 'tmp_name' => $projectDir . '/' . $strFile, |
|
| 225 |
- 'error' => $file['error'], |
|
| 226 |
- 'size' => $file['size'], |
|
| 227 |
- 'uploaded' => true, |
|
| 228 |
- 'uuid' => $strUuid |
|
| 229 |
- ); |
|
| 230 |
- |
|
| 231 |
- // Add a log entry |
|
| 232 |
- $logger = System::getContainer()->get('monolog.logger.contao');
|
|
| 233 |
- $logger->log(LogLevel::INFO, 'File "' . $strUploadFolder . '/' . $file['name'] . '" has been uploaded', ['contao' => new ContaoContext(__METHOD__, TL_FILES)]); |
|
| 234 |
- } |
|
| 235 |
- |
|
| 236 |
- unset($_SESSION['FILES']['avatar']); |
|
| 237 |
- } |
|
| 238 |
- |
|
| 239 |
- /** |
|
| 240 |
- * Return the maximum upload file size in bytes |
|
| 241 |
- */ |
|
| 242 |
- protected function getMaximumUploadSize() |
|
| 243 |
- {
|
|
| 244 |
- if ($this->maxlength > 0) |
|
| 245 |
- {
|
|
| 246 |
- return $this->maxlength; |
|
| 247 |
- } |
|
| 248 |
- |
|
| 249 |
- return FileUpload::getMaxUploadSize(); |
|
| 250 |
- } |
|
| 251 |
- |
|
| 252 |
- /** |
|
| 253 |
- * Parses an avatar to the template |
|
| 254 |
- */ |
|
| 255 |
- public static function parseMemberAvatar(?MemberModel $objMember, &$objTemplate, ?string $imgSize): void |
|
| 256 |
- {
|
|
| 257 |
- $objTemplate->addImage= true; |
|
| 258 |
- |
|
| 259 |
- $objTemplate->singleSRC = self::DEFAULT_PICTURE; |
|
| 260 |
- $objTemplate->addFallbackImage = true; |
|
| 261 |
- |
|
| 262 |
- $projectDir = System::getContainer()->getParameter('kernel.project_dir');
|
|
| 263 |
- |
|
| 264 |
- // Check if member avatar exists |
|
| 265 |
- if (null === $objMember || null === $objMember->avatar || null === ($objFile = FilesModel::findByUuid($objMember->avatar)) || !\is_file($projectDir.'/'. $objFile->path)) |
|
| 266 |
- {
|
|
| 267 |
- $objFile = !!($uuidDefault = Config::get('defaultAvatar')) ? FilesModel::findByUuid($uuidDefault) : null;
|
|
| 268 |
- } |
|
| 269 |
- |
|
| 270 |
- // Check if config avatar exists |
|
| 271 |
- if (null === $objFile || !\is_file($projectDir . '/' . $objFile->path)) |
|
| 272 |
- {
|
|
| 273 |
- return; |
|
| 274 |
- } |
|
| 275 |
- |
|
| 276 |
- $objTemplate->addFallbackImage = false; |
|
| 277 |
- $imgSize = $imgSize ?? null; |
|
| 278 |
- |
|
| 279 |
- $figureBuilder = System::getContainer() |
|
| 280 |
- ->get('contao.image.studio')
|
|
| 281 |
- ->createFigureBuilder() |
|
| 282 |
- ->from($objFile->path) |
|
| 283 |
- ->setSize($imgSize) |
|
| 284 |
- ; |
|
| 285 |
- |
|
| 286 |
- if (null !== ($figure = $figureBuilder->buildIfResourceExists())) |
|
| 287 |
- {
|
|
| 288 |
- $figure->applyLegacyTemplateData($objTemplate); |
|
| 289 |
- } |
|
| 290 |
- } |
|
| 291 |
- |
|
| 292 |
- /** |
|
| 293 |
- * Gets the url for a member avatar |
|
| 294 |
- */ |
|
| 295 |
- public static function getMemberAvatarURL(?MemberModel $objMember): string |
|
| 296 |
- {
|
|
| 297 |
- // ToDo: Merge logic with parseMemberAvatar |
|
| 298 |
- $projectDir = System::getContainer()->getParameter('kernel.project_dir');
|
|
| 299 |
- |
|
| 300 |
- if (null === $objMember || null === $objMember->avatar || null === ($objFile = FilesModel::findByUuid($objMember->avatar)) || !\is_file($projectDir.'/'. $objFile->path)) |
|
| 301 |
- {
|
|
| 302 |
- $objFile = !!($uuidDefault = Config::get('defaultAvatar')) ? FilesModel::findByUuid($uuidDefault) : null;
|
|
| 303 |
- } |
|
| 304 |
- |
|
| 305 |
- // Check if config avatar exists |
|
| 306 |
- if (null === $objFile || !\is_file($projectDir . '/' . $objFile->path)) |
|
| 307 |
- {
|
|
| 308 |
- return self::DEFAULT_PICTURE; |
|
| 309 |
- } |
|
| 310 |
- |
|
| 311 |
- return $objFile->path; |
|
| 312 |
- } |
|
| 313 |
- |
|
| 314 |
- /** |
|
| 315 |
- * Deletes an avatar |
|
| 316 |
- */ |
|
| 317 |
- public static function deleteAvatar(MemberModel $objMember): void |
|
| 318 |
- {
|
|
| 319 |
- if (!!$objMember->avatar) |
|
| 320 |
- {
|
|
| 321 |
- $objFile = FilesModel::findByUuid($objMember->avatar) ?: ''; |
|
| 322 |
- $projectDir = System::getContainer()->getParameter('kernel.project_dir');
|
|
| 323 |
- |
|
| 324 |
- // Only delete if file exists |
|
| 325 |
- if (!!$objFile && file_exists($projectDir . '/' . $objFile->path)) |
|
| 326 |
- {
|
|
| 327 |
- $file = new File($objFile->path); |
|
| 328 |
- $file->delete(); |
|
| 329 |
- } |
|
| 330 |
- } |
|
| 331 |
- } |
|
| 332 |
-} |
| ... | ... |
@@ -73,6 +73,7 @@ class Member extends Frontend |
| 73 | 73 |
return; |
| 74 | 74 |
} |
| 75 | 75 |
|
| 76 |
+ // ToDo: remove $_SESSION when contao 4.13 support ends (Contao ^5.* is not possible with Contao 4.* support) |
|
| 76 | 77 |
$file = $_SESSION['FILES']['avatar']; |
| 77 | 78 |
$maxlength_kb = $this->getMaximumUploadSize(); |
| 78 | 79 |
$maxlength_kb_readable = $this->getReadableSize($maxlength_kb); |
| ... | ... |
@@ -118,7 +119,7 @@ class Member extends Frontend |
| 118 | 119 |
} |
| 119 | 120 |
|
| 120 | 121 |
$objFile = new File($file['name']); |
| 121 |
- $uploadTypes = StringUtil::trimsplit(',', \Config::get('validImageTypes'));
|
|
| 122 |
+ $uploadTypes = StringUtil::trimsplit(',', Config::get('validImageTypes'));
|
|
| 122 | 123 |
|
| 123 | 124 |
// File type is not allowed |
| 124 | 125 |
if (!\in_array($objFile->extension, $uploadTypes)) |
| ... | ... |
@@ -238,7 +239,7 @@ class Member extends Frontend |
| 238 | 239 |
/** |
| 239 | 240 |
* Return the maximum upload file size in bytes |
| 240 | 241 |
*/ |
| 241 |
- protected function getMaximumUploadSize(): string |
|
| 242 |
+ protected function getMaximumUploadSize() |
|
| 242 | 243 |
{
|
| 243 | 244 |
if ($this->maxlength > 0) |
| 244 | 245 |
{
|
| ... | ... |
@@ -251,7 +252,7 @@ class Member extends Frontend |
| 251 | 252 |
/** |
| 252 | 253 |
* Parses an avatar to the template |
| 253 | 254 |
*/ |
| 254 |
- public static function parseMemberAvatar(?MemberModel $objMember, &$objTemplate, ?string $strImgSize): void |
|
| 255 |
+ public static function parseMemberAvatar(?MemberModel $objMember, &$objTemplate, ?string $imgSize): void |
|
| 255 | 256 |
{
|
| 256 | 257 |
$objTemplate->addImage= true; |
| 257 | 258 |
|
| ... | ... |
@@ -261,7 +262,7 @@ class Member extends Frontend |
| 261 | 262 |
$projectDir = System::getContainer()->getParameter('kernel.project_dir');
|
| 262 | 263 |
|
| 263 | 264 |
// Check if member avatar exists |
| 264 |
- if (null === $objMember || null === $objMember->avatar || null === ($objFile = FilesModel::findByUuid($objMember->avatar)) || !\is_file($projectDir.'/'.$objFile->path)) |
|
| 265 |
+ if (null === $objMember || null === $objMember->avatar || null === ($objFile = FilesModel::findByUuid($objMember->avatar)) || !\is_file($projectDir.'/'. $objFile->path)) |
|
| 265 | 266 |
{
|
| 266 | 267 |
$objFile = !!($uuidDefault = Config::get('defaultAvatar')) ? FilesModel::findByUuid($uuidDefault) : null;
|
| 267 | 268 |
} |
| ... | ... |
@@ -273,16 +274,19 @@ class Member extends Frontend |
| 273 | 274 |
} |
| 274 | 275 |
|
| 275 | 276 |
$objTemplate->addFallbackImage = false; |
| 277 |
+ $imgSize = $imgSize ?? null; |
|
| 276 | 278 |
|
| 277 |
- $arrData = ['singleSRC'=>$objFile->path]; |
|
| 279 |
+ $figureBuilder = System::getContainer() |
|
| 280 |
+ ->get('contao.image.studio')
|
|
| 281 |
+ ->createFigureBuilder() |
|
| 282 |
+ ->from($objFile->path) |
|
| 283 |
+ ->setSize($imgSize) |
|
| 284 |
+ ; |
|
| 278 | 285 |
|
| 279 |
- if (null !== $strImgSize) |
|
| 286 |
+ if (null !== ($figure = $figureBuilder->buildIfResourceExists())) |
|
| 280 | 287 |
{
|
| 281 |
- $arrData['size'] = $strImgSize; |
|
| 288 |
+ $figure->applyLegacyTemplateData($objTemplate); |
|
| 282 | 289 |
} |
| 283 |
- |
|
| 284 |
- //ToDo: Change to FigureBuilder in the future |
|
| 285 |
- $objTemplate->addImageToTemplate($objTemplate, $arrData, null, null, $objFile); |
|
| 286 | 290 |
} |
| 287 | 291 |
|
| 288 | 292 |
/** |
| ... | ... |
@@ -29,7 +29,6 @@ use Contao\System; |
| 29 | 29 |
use Contao\Validator; |
| 30 | 30 |
use Psr\Log\LogLevel; |
| 31 | 31 |
|
| 32 |
- |
|
| 33 | 32 |
/** |
| 34 | 33 |
* Class Member |
| 35 | 34 |
* |
| ... | ... |
@@ -262,7 +261,7 @@ class Member extends Frontend |
| 262 | 261 |
$projectDir = System::getContainer()->getParameter('kernel.project_dir');
|
| 263 | 262 |
|
| 264 | 263 |
// Check if member avatar exists |
| 265 |
- if(null === $objMember || null === $objMember->avatar || null === ($objFile = FilesModel::findByUuid($objMember->avatar)) || !\is_file($projectDir.'/'.$objFile->path)) |
|
| 264 |
+ if (null === $objMember || null === $objMember->avatar || null === ($objFile = FilesModel::findByUuid($objMember->avatar)) || !\is_file($projectDir.'/'.$objFile->path)) |
|
| 266 | 265 |
{
|
| 267 | 266 |
$objFile = !!($uuidDefault = Config::get('defaultAvatar')) ? FilesModel::findByUuid($uuidDefault) : null;
|
| 268 | 267 |
} |
| ... | ... |
@@ -294,7 +293,7 @@ class Member extends Frontend |
| 294 | 293 |
// ToDo: Merge logic with parseMemberAvatar |
| 295 | 294 |
$projectDir = System::getContainer()->getParameter('kernel.project_dir');
|
| 296 | 295 |
|
| 297 |
- if(null === $objMember || null === $objMember->avatar || null === ($objFile = FilesModel::findByUuid($objMember->avatar)) || !\is_file($projectDir.'/'. $objFile->path)) |
|
| 296 |
+ if (null === $objMember || null === $objMember->avatar || null === ($objFile = FilesModel::findByUuid($objMember->avatar)) || !\is_file($projectDir.'/'. $objFile->path)) |
|
| 298 | 297 |
{
|
| 299 | 298 |
$objFile = !!($uuidDefault = Config::get('defaultAvatar')) ? FilesModel::findByUuid($uuidDefault) : null;
|
| 300 | 299 |
} |
| ... | ... |
@@ -313,7 +312,7 @@ class Member extends Frontend |
| 313 | 312 |
*/ |
| 314 | 313 |
public static function deleteAvatar(MemberModel $objMember): void |
| 315 | 314 |
{
|
| 316 |
- if(!!$objMember->avatar) |
|
| 315 |
+ if (!!$objMember->avatar) |
|
| 317 | 316 |
{
|
| 318 | 317 |
$objFile = FilesModel::findByUuid($objMember->avatar) ?: ''; |
| 319 | 318 |
$projectDir = System::getContainer()->getParameter('kernel.project_dir');
|
| ... | ... |
@@ -252,7 +252,7 @@ class Member extends Frontend |
| 252 | 252 |
/** |
| 253 | 253 |
* Parses an avatar to the template |
| 254 | 254 |
*/ |
| 255 |
- public static function parseMemberAvatar(?MemberModel $objMember, &$objTemplate, $strImgSize): void |
|
| 255 |
+ public static function parseMemberAvatar(?MemberModel $objMember, &$objTemplate, ?string $strImgSize): void |
|
| 256 | 256 |
{
|
| 257 | 257 |
$objTemplate->addImage= true; |
| 258 | 258 |
|
| ... | ... |
@@ -274,7 +274,13 @@ class Member extends Frontend |
| 274 | 274 |
} |
| 275 | 275 |
|
| 276 | 276 |
$objTemplate->addFallbackImage = false; |
| 277 |
- $arrData = ['singleSRC'=>$objFile->path, 'size'=>$strImgSize]; |
|
| 277 |
+ |
|
| 278 |
+ $arrData = ['singleSRC'=>$objFile->path]; |
|
| 279 |
+ |
|
| 280 |
+ if (null !== $strImgSize) |
|
| 281 |
+ {
|
|
| 282 |
+ $arrData['size'] = $strImgSize; |
|
| 283 |
+ } |
|
| 278 | 284 |
|
| 279 | 285 |
//ToDo: Change to FigureBuilder in the future |
| 280 | 286 |
$objTemplate->addImageToTemplate($objTemplate, $arrData, null, null, $objFile); |
| ... | ... |
@@ -7,10 +7,10 @@ declare(strict_types=1); |
| 7 | 7 |
* |
| 8 | 8 |
* @package contao-member-extension-bundle |
| 9 | 9 |
* @license MIT |
| 10 |
- * @author Daniele Sciannimanica <https://github.com/doishub> |
|
| 11 |
- * @author Fabian Ekert <https://github.com/eki89> |
|
| 12 |
- * @author Sebastian Zoglowek <https://github.com/zoglo> |
|
| 13 |
- * @copyright Oveleon <https://www.oveleon.de/> |
|
| 10 |
+ * @author Sebastian Zoglowek <https://github.com/zoglo> |
|
| 11 |
+ * @author Daniele Sciannimanica <https://github.com/doishub> |
|
| 12 |
+ * @author Fabian Ekert <https://github.com/eki89> |
|
| 13 |
+ * @copyright Oveleon <https://www.oveleon.de/> |
|
| 14 | 14 |
*/ |
| 15 | 15 |
|
| 16 | 16 |
namespace Oveleon\ContaoMemberExtensionBundle; |
| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,323 @@ |
| 1 |
+<?php |
|
| 2 |
+ |
|
| 3 |
+declare(strict_types=1); |
|
| 4 |
+ |
|
| 5 |
+/* |
|
| 6 |
+ * This file is part of Oveleon ContaoMemberExtension Bundle. |
|
| 7 |
+ * |
|
| 8 |
+ * @package contao-member-extension-bundle |
|
| 9 |
+ * @license MIT |
|
| 10 |
+ * @author Daniele Sciannimanica <https://github.com/doishub> |
|
| 11 |
+ * @author Fabian Ekert <https://github.com/eki89> |
|
| 12 |
+ * @author Sebastian Zoglowek <https://github.com/zoglo> |
|
| 13 |
+ * @copyright Oveleon <https://www.oveleon.de/> |
|
| 14 |
+ */ |
|
| 15 |
+ |
|
| 16 |
+namespace Oveleon\ContaoMemberExtensionBundle; |
|
| 17 |
+ |
|
| 18 |
+use Contao\Config; |
|
| 19 |
+use Contao\CoreBundle\Monolog\ContaoContext; |
|
| 20 |
+use Contao\Dbafs; |
|
| 21 |
+use Contao\File; |
|
| 22 |
+use Contao\FilesModel; |
|
| 23 |
+use Contao\FileUpload; |
|
| 24 |
+use Contao\Frontend; |
|
| 25 |
+use Contao\FrontendUser; |
|
| 26 |
+use Contao\MemberModel; |
|
| 27 |
+use Contao\StringUtil; |
|
| 28 |
+use Contao\System; |
|
| 29 |
+use Contao\Validator; |
|
| 30 |
+use Psr\Log\LogLevel; |
|
| 31 |
+ |
|
| 32 |
+ |
|
| 33 |
+/** |
|
| 34 |
+ * Class Member |
|
| 35 |
+ * |
|
| 36 |
+ * @property int $avatar UUID of the avatar |
|
| 37 |
+ */ |
|
| 38 |
+class Member extends Frontend |
|
| 39 |
+{
|
|
| 40 |
+ const DEFAULT_PICTURE = 'bundles/contaomemberextension/avatar.png'; |
|
| 41 |
+ |
|
| 42 |
+ /** |
|
| 43 |
+ * MemberAvatar file name |
|
| 44 |
+ */ |
|
| 45 |
+ protected string $avatarName = 'memberAvatar'; |
|
| 46 |
+ |
|
| 47 |
+ /** |
|
| 48 |
+ * Create avatar for a member | Registration |
|
| 49 |
+ */ |
|
| 50 |
+ public function createAvatar(int $userId, array $arrData): void |
|
| 51 |
+ {
|
|
| 52 |
+ $objMember = MemberModel::findById($userId); |
|
| 53 |
+ $this->processAvatar($objMember, $arrData); |
|
| 54 |
+ } |
|
| 55 |
+ |
|
| 56 |
+ /** |
|
| 57 |
+ * Update avatar of a member | Login |
|
| 58 |
+ */ |
|
| 59 |
+ public function updateAvatar(FrontendUser $objUser, array $arrData): void |
|
| 60 |
+ {
|
|
| 61 |
+ $objMember = MemberModel::findById($objUser->id); |
|
| 62 |
+ $this->processAvatar($objMember, $arrData); |
|
| 63 |
+ } |
|
| 64 |
+ |
|
| 65 |
+ /** |
|
| 66 |
+ * Process avatar upload for a member |
|
| 67 |
+ */ |
|
| 68 |
+ protected function processAvatar(MemberModel $objMember, ?array $arrData): void |
|
| 69 |
+ {
|
|
| 70 |
+ $objMember = MemberModel::findByPk($objMember->id); |
|
| 71 |
+ |
|
| 72 |
+ if ($objMember === null) |
|
| 73 |
+ {
|
|
| 74 |
+ return; |
|
| 75 |
+ } |
|
| 76 |
+ |
|
| 77 |
+ $file = $_SESSION['FILES']['avatar']; |
|
| 78 |
+ $maxlength_kb = $this->getMaximumUploadSize(); |
|
| 79 |
+ $maxlength_kb_readable = $this->getReadableSize($maxlength_kb); |
|
| 80 |
+ |
|
| 81 |
+ // Sanitize the filename |
|
| 82 |
+ try |
|
| 83 |
+ {
|
|
| 84 |
+ $file['name'] = StringUtil::sanitizeFileName($file['name']); |
|
| 85 |
+ } |
|
| 86 |
+ catch (\InvalidArgumentException $e) |
|
| 87 |
+ {
|
|
| 88 |
+ // ToDo: add error message for invalid characters |
|
| 89 |
+ return; |
|
| 90 |
+ } |
|
| 91 |
+ |
|
| 92 |
+ // Invalid file name |
|
| 93 |
+ if (!Validator::isValidFileName($file['name'])) |
|
| 94 |
+ {
|
|
| 95 |
+ // ToDo: add error message for invalid characters |
|
| 96 |
+ return; |
|
| 97 |
+ } |
|
| 98 |
+ |
|
| 99 |
+ // File was not uploaded |
|
| 100 |
+ if (!is_uploaded_file($file['tmp_name'])) |
|
| 101 |
+ {
|
|
| 102 |
+ // ToDo: Add error messages |
|
| 103 |
+ /*if ($file['error'] == 1 || $file['error'] == 2) { // Add error message for maximum file size }
|
|
| 104 |
+ elseif ($file['error'] == 3) { // Add error message for partial upload }
|
|
| 105 |
+ elseif ($file['error'] > 0) { // Add error message for failed upload }*/
|
|
| 106 |
+ |
|
| 107 |
+ unset($_SESSION['FILES']['avatar']); |
|
| 108 |
+ |
|
| 109 |
+ return; |
|
| 110 |
+ } |
|
| 111 |
+ |
|
| 112 |
+ // File is too big |
|
| 113 |
+ if ($file['size'] > $maxlength_kb) |
|
| 114 |
+ {
|
|
| 115 |
+ // ToDo: add error message for maximum file size |
|
| 116 |
+ unset($_SESSION['FILES']['avatar']); |
|
| 117 |
+ |
|
| 118 |
+ return; |
|
| 119 |
+ } |
|
| 120 |
+ |
|
| 121 |
+ $objFile = new File($file['name']); |
|
| 122 |
+ $uploadTypes = StringUtil::trimsplit(',', \Config::get('validImageTypes'));
|
|
| 123 |
+ |
|
| 124 |
+ // File type is not allowed |
|
| 125 |
+ if (!\in_array($objFile->extension, $uploadTypes)) |
|
| 126 |
+ {
|
|
| 127 |
+ // ToDo: add error message for not allowed file type |
|
| 128 |
+ unset($_SESSION['FILES']['avatar']); |
|
| 129 |
+ |
|
| 130 |
+ return; |
|
| 131 |
+ } |
|
| 132 |
+ |
|
| 133 |
+ if ($arrImageSize = @getimagesize($file['tmp_name'])) |
|
| 134 |
+ {
|
|
| 135 |
+ $intImageWidth = Config::get('imageWidth');
|
|
| 136 |
+ |
|
| 137 |
+ // Image exceeds maximum image width |
|
| 138 |
+ if ($intImageWidth > 0 && $arrImageSize[0] > $intImageWidth) {
|
|
| 139 |
+ // ToDo: add error message for exceeding width |
|
| 140 |
+ unset($_SESSION['FILES']['avatar']); |
|
| 141 |
+ |
|
| 142 |
+ return; |
|
| 143 |
+ } |
|
| 144 |
+ |
|
| 145 |
+ $intImageHeight = Config::get('imageHeight');
|
|
| 146 |
+ |
|
| 147 |
+ // Image exceeds maximum image height |
|
| 148 |
+ if ($intImageHeight > 0 && $arrImageSize[1] > $intImageHeight) {
|
|
| 149 |
+ // ToDo: add error message for exceeding height |
|
| 150 |
+ unset($_SESSION['FILES']['avatar']); |
|
| 151 |
+ |
|
| 152 |
+ return; |
|
| 153 |
+ } |
|
| 154 |
+ } |
|
| 155 |
+ |
|
| 156 |
+ // Upload valid file type with no width and height -> svg |
|
| 157 |
+ |
|
| 158 |
+ // Don't upload if no homedir is assigned |
|
| 159 |
+ // ToDo: Create homedir? |
|
| 160 |
+ if (!$objMember->assignDir || !$objMember->homeDir) |
|
| 161 |
+ {
|
|
| 162 |
+ // ToDo: add error message for no homedir |
|
| 163 |
+ return; |
|
| 164 |
+ } |
|
| 165 |
+ |
|
| 166 |
+ $intUploadFolder = $objMember->homeDir; |
|
| 167 |
+ |
|
| 168 |
+ $objUploadFolder = FilesModel::findByUuid($intUploadFolder); |
|
| 169 |
+ |
|
| 170 |
+ // The upload folder could not be found |
|
| 171 |
+ if ($objUploadFolder === null) |
|
| 172 |
+ {
|
|
| 173 |
+ throw new Exception("Invalid upload folder ID $intUploadFolder");
|
|
| 174 |
+ } |
|
| 175 |
+ |
|
| 176 |
+ $strUploadFolder = $objUploadFolder->path; |
|
| 177 |
+ |
|
| 178 |
+ // Store the file if the upload folder exists |
|
| 179 |
+ $projectDir = System::getContainer()->getParameter('kernel.project_dir');
|
|
| 180 |
+ |
|
| 181 |
+ if (!!$strUploadFolder & is_dir($projectDir . '/' . $strUploadFolder)) |
|
| 182 |
+ {
|
|
| 183 |
+ // Delete existing avatar if it exists |
|
| 184 |
+ $this->deleteAvatar($objMember); |
|
| 185 |
+ |
|
| 186 |
+ $this->import('Files');
|
|
| 187 |
+ |
|
| 188 |
+ // Rename file |
|
| 189 |
+ $file['name'] = $this->avatarName . '.' . $objFile->extension; |
|
| 190 |
+ |
|
| 191 |
+ // Move the file to its destination |
|
| 192 |
+ $this->Files->move_uploaded_file($file['tmp_name'], $strUploadFolder . '/' . $file['name']); |
|
| 193 |
+ $this->Files->chmod($strUploadFolder . '/' . $file['name'], 0666 & ~umask()); |
|
| 194 |
+ |
|
| 195 |
+ $strUuid = null; |
|
| 196 |
+ $strFile = $strUploadFolder . '/' . $file['name']; |
|
| 197 |
+ |
|
| 198 |
+ |
|
| 199 |
+ // Generate the DB entries |
|
| 200 |
+ if (Dbafs::shouldBeSynchronized($strFile)) |
|
| 201 |
+ {
|
|
| 202 |
+ $objModel = FilesModel::findByPath($strFile); |
|
| 203 |
+ |
|
| 204 |
+ if ($objModel === null) |
|
| 205 |
+ {
|
|
| 206 |
+ $objModel = Dbafs::addResource($strFile); |
|
| 207 |
+ } |
|
| 208 |
+ |
|
| 209 |
+ $strUuid = StringUtil::binToUuid($objModel->uuid); |
|
| 210 |
+ |
|
| 211 |
+ // Update the hash of the target folder |
|
| 212 |
+ Dbafs::updateFolderHashes($strUploadFolder); |
|
| 213 |
+ |
|
| 214 |
+ // Update member avatar |
|
| 215 |
+ $objMember->avatar = $objModel->uuid; |
|
| 216 |
+ $objMember->save(); |
|
| 217 |
+ } |
|
| 218 |
+ |
|
| 219 |
+ // Add the session entry |
|
| 220 |
+ $_SESSION['FILES']['avatar'] = array |
|
| 221 |
+ ( |
|
| 222 |
+ 'name' => $file['name'], |
|
| 223 |
+ 'type' => $file['type'], |
|
| 224 |
+ 'tmp_name' => $projectDir . '/' . $strFile, |
|
| 225 |
+ 'error' => $file['error'], |
|
| 226 |
+ 'size' => $file['size'], |
|
| 227 |
+ 'uploaded' => true, |
|
| 228 |
+ 'uuid' => $strUuid |
|
| 229 |
+ ); |
|
| 230 |
+ |
|
| 231 |
+ // Add a log entry |
|
| 232 |
+ $logger = System::getContainer()->get('monolog.logger.contao');
|
|
| 233 |
+ $logger->log(LogLevel::INFO, 'File "' . $strUploadFolder . '/' . $file['name'] . '" has been uploaded', ['contao' => new ContaoContext(__METHOD__, TL_FILES)]); |
|
| 234 |
+ } |
|
| 235 |
+ |
|
| 236 |
+ unset($_SESSION['FILES']['avatar']); |
|
| 237 |
+ } |
|
| 238 |
+ |
|
| 239 |
+ /** |
|
| 240 |
+ * Return the maximum upload file size in bytes |
|
| 241 |
+ */ |
|
| 242 |
+ protected function getMaximumUploadSize(): string |
|
| 243 |
+ {
|
|
| 244 |
+ if ($this->maxlength > 0) |
|
| 245 |
+ {
|
|
| 246 |
+ return $this->maxlength; |
|
| 247 |
+ } |
|
| 248 |
+ |
|
| 249 |
+ return FileUpload::getMaxUploadSize(); |
|
| 250 |
+ } |
|
| 251 |
+ |
|
| 252 |
+ /** |
|
| 253 |
+ * Parses an avatar to the template |
|
| 254 |
+ */ |
|
| 255 |
+ public static function parseMemberAvatar(?MemberModel $objMember, &$objTemplate, $strImgSize): void |
|
| 256 |
+ {
|
|
| 257 |
+ $objTemplate->addImage= true; |
|
| 258 |
+ |
|
| 259 |
+ $objTemplate->singleSRC = self::DEFAULT_PICTURE; |
|
| 260 |
+ $objTemplate->addFallbackImage = true; |
|
| 261 |
+ |
|
| 262 |
+ $projectDir = System::getContainer()->getParameter('kernel.project_dir');
|
|
| 263 |
+ |
|
| 264 |
+ // Check if member avatar exists |
|
| 265 |
+ if(null === $objMember || null === $objMember->avatar || null === ($objFile = FilesModel::findByUuid($objMember->avatar)) || !\is_file($projectDir.'/'.$objFile->path)) |
|
| 266 |
+ {
|
|
| 267 |
+ $objFile = !!($uuidDefault = Config::get('defaultAvatar')) ? FilesModel::findByUuid($uuidDefault) : null;
|
|
| 268 |
+ } |
|
| 269 |
+ |
|
| 270 |
+ // Check if config avatar exists |
|
| 271 |
+ if (null === $objFile || !\is_file($projectDir . '/' . $objFile->path)) |
|
| 272 |
+ {
|
|
| 273 |
+ return; |
|
| 274 |
+ } |
|
| 275 |
+ |
|
| 276 |
+ $objTemplate->addFallbackImage = false; |
|
| 277 |
+ $arrData = ['singleSRC'=>$objFile->path, 'size'=>$strImgSize]; |
|
| 278 |
+ |
|
| 279 |
+ //ToDo: Change to FigureBuilder in the future |
|
| 280 |
+ $objTemplate->addImageToTemplate($objTemplate, $arrData, null, null, $objFile); |
|
| 281 |
+ } |
|
| 282 |
+ |
|
| 283 |
+ /** |
|
| 284 |
+ * Gets the url for a member avatar |
|
| 285 |
+ */ |
|
| 286 |
+ public static function getMemberAvatarURL(?MemberModel $objMember): string |
|
| 287 |
+ {
|
|
| 288 |
+ // ToDo: Merge logic with parseMemberAvatar |
|
| 289 |
+ $projectDir = System::getContainer()->getParameter('kernel.project_dir');
|
|
| 290 |
+ |
|
| 291 |
+ if(null === $objMember || null === $objMember->avatar || null === ($objFile = FilesModel::findByUuid($objMember->avatar)) || !\is_file($projectDir.'/'. $objFile->path)) |
|
| 292 |
+ {
|
|
| 293 |
+ $objFile = !!($uuidDefault = Config::get('defaultAvatar')) ? FilesModel::findByUuid($uuidDefault) : null;
|
|
| 294 |
+ } |
|
| 295 |
+ |
|
| 296 |
+ // Check if config avatar exists |
|
| 297 |
+ if (null === $objFile || !\is_file($projectDir . '/' . $objFile->path)) |
|
| 298 |
+ {
|
|
| 299 |
+ return self::DEFAULT_PICTURE; |
|
| 300 |
+ } |
|
| 301 |
+ |
|
| 302 |
+ return $objFile->path; |
|
| 303 |
+ } |
|
| 304 |
+ |
|
| 305 |
+ /** |
|
| 306 |
+ * Deletes an avatar |
|
| 307 |
+ */ |
|
| 308 |
+ public static function deleteAvatar(MemberModel $objMember): void |
|
| 309 |
+ {
|
|
| 310 |
+ if(!!$objMember->avatar) |
|
| 311 |
+ {
|
|
| 312 |
+ $objFile = FilesModel::findByUuid($objMember->avatar) ?: ''; |
|
| 313 |
+ $projectDir = System::getContainer()->getParameter('kernel.project_dir');
|
|
| 314 |
+ |
|
| 315 |
+ // Only delete if file exists |
|
| 316 |
+ if (!!$objFile && file_exists($projectDir . '/' . $objFile->path)) |
|
| 317 |
+ {
|
|
| 318 |
+ $file = new File($objFile->path); |
|
| 319 |
+ $file->delete(); |
|
| 320 |
+ } |
|
| 321 |
+ } |
|
| 322 |
+ } |
|
| 323 |
+} |