| ... | ... |
@@ -20,13 +20,21 @@ use Contao\CoreBundle\Monolog\ContaoContext; |
| 20 | 20 |
use Contao\Dbafs; |
| 21 | 21 |
use Contao\File; |
| 22 | 22 |
use Contao\FilesModel; |
| 23 |
+use Contao\FileUpload; |
|
| 23 | 24 |
use Contao\Frontend; |
| 25 |
+use Contao\FrontendUser; |
|
| 24 | 26 |
use Contao\MemberModel; |
| 25 | 27 |
use Contao\StringUtil; |
| 26 | 28 |
use Contao\System; |
| 27 | 29 |
use Contao\Validator; |
| 28 | 30 |
use Psr\Log\LogLevel; |
| 29 | 31 |
|
| 32 |
+ |
|
| 33 |
+/** |
|
| 34 |
+ * Class Member |
|
| 35 |
+ * |
|
| 36 |
+ * @property int $avatar UUID of the avatar |
|
| 37 |
+ */ |
|
| 30 | 38 |
class Member extends Frontend |
| 31 | 39 |
{
|
| 32 | 40 |
/** |
| ... | ... |
@@ -39,24 +47,42 @@ class Member extends Frontend |
| 39 | 47 |
/** |
| 40 | 48 |
* Create avatar for a member | Registration |
| 41 | 49 |
* |
| 42 |
- * @param int $userId |
|
| 43 |
- * @param array $arrData |
|
| 50 |
+ * @param int $userId |
|
| 51 |
+ * @param array $arrData |
|
| 52 |
+ * |
|
| 53 |
+ * @return void |
|
| 44 | 54 |
*/ |
| 45 |
- public function createAvatar($userId, $arrData) |
|
| 55 |
+ public function createAvatar(int $userId, array $arrData): void |
|
| 46 | 56 |
{
|
| 47 | 57 |
$objMember = MemberModel::findById($userId); |
| 48 |
- $this->updateAvatar($objMember, $arrData); |
|
| 58 |
+ $this->processAvatar($objMember, $arrData); |
|
| 59 |
+ } |
|
| 60 |
+ |
|
| 61 |
+ /** |
|
| 62 |
+ * Update avatar of a member | Login |
|
| 63 |
+ * |
|
| 64 |
+ * @param FrontendUser $objUser |
|
| 65 |
+ * @param array $arrData |
|
| 66 |
+ * |
|
| 67 |
+ * @return void |
|
| 68 |
+ */ |
|
| 69 |
+ public function updateAvatar(FrontendUser $objUser, $arrData): void |
|
| 70 |
+ {
|
|
| 71 |
+ $objMember = MemberModel::findById($objUser->id); |
|
| 72 |
+ $this->processAvatar($objMember, $arrData); |
|
| 49 | 73 |
} |
| 50 | 74 |
|
| 51 | 75 |
/** |
| 52 |
- * Update avatar of member |
|
| 76 |
+ * Process avatar upload for a member |
|
| 77 |
+ * |
|
| 78 |
+ * @param MemberModel $objMember |
|
| 79 |
+ * @param array $arrData |
|
| 53 | 80 |
* |
| 54 |
- * @param MemberModel $objMember |
|
| 55 |
- * @param array $arrData |
|
| 81 |
+ * @return void |
|
| 56 | 82 |
*/ |
| 57 |
- public function updateAvatar($objUser, $arrData) |
|
| 83 |
+ protected function processAvatar(MemberModel $objMember, array $arrData): void |
|
| 58 | 84 |
{
|
| 59 |
- $objMember = MemberModel::findByPk($objUser->id); |
|
| 85 |
+ $objMember = MemberModel::findByPk($objMember->id); |
|
| 60 | 86 |
|
| 61 | 87 |
if ($objMember === null) |
| 62 | 88 |
{
|
| ... | ... |
@@ -74,38 +100,26 @@ class Member extends Frontend |
| 74 | 100 |
} |
| 75 | 101 |
catch (\InvalidArgumentException $e) |
| 76 | 102 |
{
|
| 77 |
- // ToDo: Fehler: Dateiname beinhaltet unzulässige Zeichen |
|
| 78 |
- $this->addError($GLOBALS['TL_LANG']['ERR']['filename']); |
|
| 79 |
- |
|
| 103 |
+ // ToDo: add error message for invalid characters |
|
| 80 | 104 |
return; |
| 81 | 105 |
} |
| 82 | 106 |
|
| 83 | 107 |
// Invalid file name |
| 84 | 108 |
if (!Validator::isValidFileName($file['name'])) |
| 85 | 109 |
{
|
| 86 |
- // ToDo: Fehler: Dateiname beinhaltet unzulässige Zeichen |
|
| 87 |
- $this->addError($GLOBALS['TL_LANG']['ERR']['filename']); |
|
| 110 |
+ // ToDo: add error message for invalid characters |
|
| 88 | 111 |
return; |
| 89 | 112 |
} |
| 90 | 113 |
|
| 91 | 114 |
// File was not uploaded |
| 92 |
- // ToDo: File was not uploaded |
|
| 93 | 115 |
if (!is_uploaded_file($file['tmp_name'])) |
| 94 | 116 |
{
|
| 95 |
- if ($file['error'] == 1 || $file['error'] == 2) |
|
| 96 |
- {
|
|
| 97 |
- $this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['filesize'], $maxlength_kb_readable)); |
|
| 98 |
- } |
|
| 99 |
- elseif ($file['error'] == 3) |
|
| 100 |
- {
|
|
| 101 |
- $this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['filepartial'], $file['name'])); |
|
| 102 |
- } |
|
| 103 |
- elseif ($file['error'] > 0) |
|
| 104 |
- {
|
|
| 105 |
- $this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['fileerror'], $file['error'], $file['name'])); |
|
| 106 |
- } |
|
| 117 |
+ // ToDo: Add error messages |
|
| 118 |
+ /*if ($file['error'] == 1 || $file['error'] == 2) { // Add error message for maximum file size }
|
|
| 119 |
+ elseif ($file['error'] == 3) { // Add error message for partial upload }
|
|
| 120 |
+ elseif ($file['error'] > 0) { // Add error message for failed upload }*/
|
|
| 107 | 121 |
|
| 108 |
- unset($_FILES[$this->strName]); |
|
| 122 |
+ unset($_SESSION['FILES']['avatar']); |
|
| 109 | 123 |
|
| 110 | 124 |
return; |
| 111 | 125 |
} |
| ... | ... |
@@ -113,8 +127,7 @@ class Member extends Frontend |
| 113 | 127 |
// File is too big |
| 114 | 128 |
if ($file['size'] > $maxlength_kb) |
| 115 | 129 |
{
|
| 116 |
- // ToDo: Fehler: Datei zu groß |
|
| 117 |
- $this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['filesize'], $maxlength_kb_readable)); |
|
| 130 |
+ // ToDo: add error message for maximum file size |
|
| 118 | 131 |
unset($_SESSION['FILES']['avatar']); |
| 119 | 132 |
|
| 120 | 133 |
return; |
| ... | ... |
@@ -126,8 +139,7 @@ class Member extends Frontend |
| 126 | 139 |
// File type is not allowed |
| 127 | 140 |
if (!\in_array($objFile->extension, $uploadTypes)) |
| 128 | 141 |
{
|
| 129 |
- // ToDo: Fehler: Dateityp nicht erlaubt |
|
| 130 |
- $this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['filetype'], $objFile->extension)); |
|
| 142 |
+ // ToDo: add error message for not allowed file type |
|
| 131 | 143 |
unset($_SESSION['FILES']['avatar']); |
| 132 | 144 |
|
| 133 | 145 |
return; |
| ... | ... |
@@ -139,8 +151,8 @@ class Member extends Frontend |
| 139 | 151 |
|
| 140 | 152 |
// Image exceeds maximum image width |
| 141 | 153 |
if ($intImageWidth > 0 && $arrImageSize[0] > $intImageWidth) {
|
| 142 |
- $this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['filewidth'], $file['name'], $intImageWidth)); |
|
| 143 |
- unset($_FILES[$this->strName]); |
|
| 154 |
+ // ToDo: add error message for exceeding width |
|
| 155 |
+ unset($_SESSION['FILES']['avatar']); |
|
| 144 | 156 |
|
| 145 | 157 |
return; |
| 146 | 158 |
} |
| ... | ... |
@@ -149,8 +161,8 @@ class Member extends Frontend |
| 149 | 161 |
|
| 150 | 162 |
// Image exceeds maximum image height |
| 151 | 163 |
if ($intImageHeight > 0 && $arrImageSize[1] > $intImageHeight) {
|
| 152 |
- $this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['fileheight'], $file['name'], $intImageHeight)); |
|
| 153 |
- unset($_FILES[$this->strName]); |
|
| 164 |
+ // ToDo: add error message for exceeding height |
|
| 165 |
+ unset($_SESSION['FILES']['avatar']); |
|
| 154 | 166 |
|
| 155 | 167 |
return; |
| 156 | 168 |
} |
| ... | ... |
@@ -159,9 +171,9 @@ class Member extends Frontend |
| 159 | 171 |
// Upload valid file type with no width and height -> svg |
| 160 | 172 |
|
| 161 | 173 |
// Don't upload if no homedir is assigned |
| 162 |
- // ToDo: Add error |
|
| 163 | 174 |
if (!$objMember->assignDir || !$objMember->homeDir) |
| 164 | 175 |
{
|
| 176 |
+ // ToDo: add error message for no homedir |
|
| 165 | 177 |
return; |
| 166 | 178 |
} |
| 167 | 179 |
|
| ... | ... |
@@ -245,45 +257,28 @@ class Member extends Frontend |
| 245 | 257 |
*/ |
| 246 | 258 |
protected function getMaximumUploadSize() |
| 247 | 259 |
{
|
| 248 |
- // Get the upload_max_filesize from the php.ini |
|
| 249 |
- $upload_max_filesize = ini_get('upload_max_filesize');
|
|
| 250 |
- |
|
| 251 |
- // Convert the value to bytes |
|
| 252 |
- if (stripos($upload_max_filesize, 'K') !== false) |
|
| 260 |
+ if ($this->maxlength > 0) |
|
| 253 | 261 |
{
|
| 254 |
- $upload_max_filesize = round($upload_max_filesize * 1024); |
|
| 255 |
- } |
|
| 256 |
- elseif (stripos($upload_max_filesize, 'M') !== false) |
|
| 257 |
- {
|
|
| 258 |
- $upload_max_filesize = round($upload_max_filesize * 1024 * 1024); |
|
| 259 |
- } |
|
| 260 |
- elseif (stripos($upload_max_filesize, 'G') !== false) |
|
| 261 |
- {
|
|
| 262 |
- $upload_max_filesize = round($upload_max_filesize * 1024 * 1024 * 1024); |
|
| 262 |
+ return $this->maxlength; |
|
| 263 | 263 |
} |
| 264 | 264 |
|
| 265 |
- return min($upload_max_filesize, Config::get('maxFileSize'));
|
|
| 265 |
+ return FileUpload::getMaxUploadSize(); |
|
| 266 | 266 |
} |
| 267 | 267 |
|
| 268 | 268 |
/** |
| 269 |
- * Add an error message |
|
| 269 |
+ * @param MemberModel $objMember |
|
| 270 | 270 |
* |
| 271 |
- * @param string $strError The error message |
|
| 271 |
+ * @return void |
|
| 272 | 272 |
*/ |
| 273 |
- public function addError($strError) |
|
| 274 |
- {
|
|
| 275 |
- $this->class = 'error'; |
|
| 276 |
- $this->arrErrors[] = $strError; |
|
| 277 |
- } |
|
| 278 |
- |
|
| 279 |
- public function deleteAvatar($objMember) |
|
| 273 |
+ public function deleteAvatar(MemberModel $objMember): void |
|
| 280 | 274 |
{
|
| 281 | 275 |
if(!!$objMember->avatar) |
| 282 | 276 |
{
|
| 283 |
- $objFile = FilesModel::findByUuid($objMember->avatar) ?? ''; |
|
| 277 |
+ $objFile = FilesModel::findByUuid($objMember->avatar) ?: ''; |
|
| 278 |
+ $projectDir = System::getContainer()->getParameter('kernel.project_dir');
|
|
| 284 | 279 |
|
| 285 |
- // Only delete existing file |
|
| 286 |
- if (!!$objFile && file_exists($objFile->path)) |
|
| 280 |
+ // Only delete if file exists |
|
| 281 |
+ if (!!$objFile && file_exists($projectDir . '/' . $objFile->path)) |
|
| 287 | 282 |
{
|
| 288 | 283 |
$file = new File($objFile->path); |
| 289 | 284 |
$file->delete(); |