| Total Complexity | 41 |
| Total Lines | 461 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ItemModel often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ItemModel, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class ItemModel extends Database |
||
| 37 | { |
||
| 38 | |||
| 39 | |||
| 40 | /** |
||
| 41 | * Get the list of items to return |
||
| 42 | * |
||
| 43 | * @param string $sqlExtra |
||
| 44 | * @param integer $limit |
||
| 45 | * @param string $userPrivateKey |
||
| 46 | * @param integer $userId |
||
| 47 | * |
||
| 48 | * @return array |
||
| 49 | */ |
||
| 50 | public function getItems(string $sqlExtra, int $limit, string $userPrivateKey, int $userId): array |
||
| 126 | } |
||
| 127 | //end getItems() |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Main function to add a new item to the database. |
||
| 131 | * It handles data preparation, validation, password checks, folder settings, |
||
| 132 | * item creation, and post-insertion tasks (like logging, sharing, and tagging). |
||
| 133 | */ |
||
| 134 | public function addItem( |
||
| 135 | int $folderId, |
||
| 136 | string $label, |
||
| 137 | string $password, |
||
| 138 | string $description, |
||
| 139 | string $login, |
||
| 140 | string $email, |
||
| 141 | string $url, |
||
| 142 | string $tags, |
||
| 143 | string $anyone_can_modify, |
||
| 144 | string $icon, |
||
| 145 | int $userId, |
||
| 146 | string $username |
||
| 147 | ) : array |
||
| 148 | { |
||
| 149 | try { |
||
| 150 | include_once API_ROOT_PATH . '/../sources/main.functions.php'; |
||
| 151 | include API_ROOT_PATH . '/../includes/config/tp.config.php'; |
||
| 152 | |||
| 153 | // Step 1: Prepare data and sanitize inputs |
||
| 154 | $data = $this->prepareData($folderId, $label, $password, $description, $login, $email, $url, $tags, $anyone_can_modify, $icon); |
||
| 155 | $this->validateData($data); // Step 2: Validate the data |
||
| 156 | |||
| 157 | // Step 3: Validate password rules (length, emptiness) |
||
| 158 | $this->validatePassword($password, $SETTINGS); |
||
|
|
|||
| 159 | |||
| 160 | // Step 4: Check folder settings for permission checks |
||
| 161 | $itemInfos = $this->getFolderSettings($folderId); |
||
| 162 | |||
| 163 | // Step 5: Ensure the password meets folder complexity requirements |
||
| 164 | $this->checkPasswordComplexity($password, $itemInfos); |
||
| 165 | |||
| 166 | // Step 6: Check for duplicates in the system |
||
| 167 | $this->checkForDuplicates($label, $SETTINGS, $itemInfos); |
||
| 168 | |||
| 169 | // Step 7: Encrypt password if provided |
||
| 170 | $cryptedData = $this->encryptPassword($password); |
||
| 171 | $passwordKey = $cryptedData['passwordKey']; |
||
| 172 | $password = $cryptedData['encrypted']; |
||
| 173 | |||
| 174 | // Step 8: Insert the new item into the database |
||
| 175 | $newID = $this->insertNewItem($data, $password, $passwordKey, $userId, $itemInfos, $SETTINGS); |
||
| 176 | |||
| 177 | // Step 9: Handle post-insert tasks (logging, sharing, tagging) |
||
| 178 | $this->handlePostInsertTasks($newID, $itemInfos, $folderId, $passwordKey, $userId, $username, $tags, $data, $SETTINGS); |
||
| 179 | |||
| 180 | // Success response |
||
| 181 | return [ |
||
| 182 | 'error' => false, |
||
| 183 | 'message' => 'Item added successfully', |
||
| 184 | 'newId' => $newID, |
||
| 185 | ]; |
||
| 186 | |||
| 187 | } catch (Exception $e) { |
||
| 188 | // Error response |
||
| 189 | return [ |
||
| 190 | 'error' => true, |
||
| 191 | 'error_header' => 'HTTP/1.1 422 Unprocessable Entity', |
||
| 192 | 'error_message' => $e->getMessage(), |
||
| 193 | ]; |
||
| 194 | } |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Prepares the data array for processing by combining all inputs. |
||
| 199 | * @param int $folderId - Folder ID where the item is stored |
||
| 200 | * @param string $label - Label or title of the item |
||
| 201 | * @param string $password - Password associated with the item |
||
| 202 | * @param string $description - Description of the item |
||
| 203 | * @param string $login - Login associated with the item |
||
| 204 | * @param string $email - Email linked to the item |
||
| 205 | * @param string $url - URL for the item |
||
| 206 | * @param string $tags - Tags for categorizing the item |
||
| 207 | * @param string $anyone_can_modify - Permission to allow modifications by others |
||
| 208 | * @param string $icon - Icon representing the item |
||
| 209 | * @return array - Returns the prepared data |
||
| 210 | */ |
||
| 211 | private function prepareData( |
||
| 226 | ]; |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Sanitizes and validates the input data according to specified filters. |
||
| 231 | * If validation fails, throws an exception. |
||
| 232 | * @param array $data - Data to be validated and sanitized |
||
| 233 | * @throws Exception - If the data is invalid |
||
| 234 | */ |
||
| 235 | private function validateData(array $data) : void |
||
| 236 | { |
||
| 237 | $filters = [ |
||
| 238 | 'folderId' => 'cast:integer', |
||
| 239 | 'label' => 'trim|escape', |
||
| 240 | 'password' => 'trim|escape', |
||
| 241 | 'description' => 'trim|escape', |
||
| 242 | 'login' => 'trim|escape', |
||
| 243 | 'email' => 'trim|escape', |
||
| 244 | 'tags' => 'trim|escape', |
||
| 245 | 'anyoneCanModify' => 'trim|escape', |
||
| 246 | 'url' => 'trim|escape', |
||
| 247 | 'icon' => 'trim|escape', |
||
| 248 | ]; |
||
| 249 | |||
| 250 | $inputData = dataSanitizer($data, $filters); |
||
| 251 | if (is_string($inputData)) { |
||
| 252 | throw new Exception('Data is not valid'); |
||
| 253 | } |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Validates the password against length and empty password rules. |
||
| 258 | * Throws an exception if validation fails. |
||
| 259 | * @param string $password - The password to validate |
||
| 260 | * @param array $SETTINGS - Global settings from configuration |
||
| 261 | * @throws Exception - If the password is invalid |
||
| 262 | */ |
||
| 263 | private function validatePassword(string $password, array $SETTINGS) : void |
||
| 264 | { |
||
| 265 | if ($this->isPasswordEmptyAllowed($password, $SETTINGS['create_item_without_password'])) { |
||
| 266 | throw new Exception('Empty password is not allowed'); |
||
| 267 | } |
||
| 268 | |||
| 269 | if (strlen($password) > $SETTINGS['pwd_maximum_length']) { |
||
| 270 | throw new Exception('Password is too long (max allowed is ' . $SETTINGS['pwd_maximum_length'] . ' characters)'); |
||
| 271 | } |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Retrieves folder-specific settings, including permission to modify and create items. |
||
| 276 | * @param int $folderId - The folder ID to fetch settings for |
||
| 277 | * @return array - Returns an array with folder-specific permissions |
||
| 278 | */ |
||
| 279 | private function getFolderSettings(int $folderId) : array |
||
| 280 | { |
||
| 281 | $dataFolderSettings = DB::queryFirstRow( |
||
| 282 | 'SELECT bloquer_creation, bloquer_modification, personal_folder |
||
| 283 | FROM ' . prefixTable('nested_tree') . ' |
||
| 284 | WHERE id = %i', |
||
| 285 | $folderId |
||
| 286 | ); |
||
| 287 | |||
| 288 | return [ |
||
| 289 | 'personal_folder' => $dataFolderSettings['personal_folder'], |
||
| 290 | 'no_complex_check_on_modification' => (int) $dataFolderSettings['personal_folder'] === 1 ? 1 : (int) $dataFolderSettings['bloquer_modification'], |
||
| 291 | 'no_complex_check_on_creation' => (int) $dataFolderSettings['personal_folder'] === 1 ? 1 : (int) $dataFolderSettings['bloquer_creation'], |
||
| 292 | ]; |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Validates that the password meets the complexity requirements of the folder. |
||
| 297 | * Throws an exception if the password is too weak. |
||
| 298 | * @param string $password - The password to check |
||
| 299 | * @param array $itemInfos - Folder settings including password complexity requirements |
||
| 300 | * @throws Exception - If the password complexity is insufficient |
||
| 301 | */ |
||
| 302 | private function checkPasswordComplexity(string $password, array $itemInfos) : void |
||
| 303 | { |
||
| 304 | $folderComplexity = DB::queryFirstRow( |
||
| 305 | 'SELECT valeur |
||
| 306 | FROM ' . prefixTable('misc') . ' |
||
| 307 | WHERE type = %s AND intitule = %i', |
||
| 308 | 'complex', |
||
| 309 | $itemInfos['folderId'] |
||
| 310 | ); |
||
| 311 | |||
| 312 | $requested_folder_complexity = $folderComplexity !== null ? (int) $folderComplexity['valeur'] : 0; |
||
| 313 | |||
| 314 | $zxcvbn = new Zxcvbn(); |
||
| 315 | $passwordStrength = $zxcvbn->passwordStrength($password); |
||
| 316 | $passwordStrengthScore = convertPasswordStrength($passwordStrength['score']); |
||
| 317 | |||
| 318 | if ($passwordStrengthScore < $requested_folder_complexity && (int) $itemInfos['no_complex_check_on_creation'] === 0) { |
||
| 319 | throw new Exception('Password strength is too low'); |
||
| 320 | } |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Checks if an item with the same label already exists in the folder. |
||
| 325 | * Throws an exception if duplicates are not allowed. |
||
| 326 | * @param string $label - The label of the item to check for duplicates |
||
| 327 | * @param array $SETTINGS - Global settings for duplicate items |
||
| 328 | * @param array $itemInfos - Folder-specific settings |
||
| 329 | * @throws Exception - If a duplicate item is found and not allowed |
||
| 330 | */ |
||
| 331 | private function checkForDuplicates(string $label, array $SETTINGS, array $itemInfos) : void |
||
| 332 | { |
||
| 333 | $existingItem = DB::queryFirstRow( |
||
| 334 | 'SELECT * FROM ' . prefixTable('items') . ' |
||
| 335 | WHERE label = %s AND inactif = %i', |
||
| 336 | $label, |
||
| 337 | 0 |
||
| 338 | ); |
||
| 339 | |||
| 340 | if (DB::count() > 0 && ( |
||
| 341 | (isset($SETTINGS['duplicate_item']) && (int) $SETTINGS['duplicate_item'] === 0) |
||
| 342 | || (int) $itemInfos['personal_folder'] === 0) |
||
| 343 | ) { |
||
| 344 | throw new Exception('Similar item already exists. Duplicates are not allowed.'); |
||
| 345 | } |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Encrypts the password using the system's encryption function. |
||
| 350 | * Returns an array containing both the encrypted password and the encryption key. |
||
| 351 | * @param string $password - The password to encrypt |
||
| 352 | * @return array - Returns the encrypted password and the encryption key |
||
| 353 | */ |
||
| 354 | private function encryptPassword(string $password) : array |
||
| 360 | ]; |
||
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Inserts the new item into the database with all its associated data. |
||
| 365 | * @param array $data - The item data to insert |
||
| 366 | * @param string $password - The encrypted password |
||
| 367 | * @param string $passwordKey - The encryption key for the password |
||
| 368 | * @param int $userId - The ID of the user creating the item |
||
| 369 | * @param array $itemInfos - Folder-specific settings |
||
| 370 | * @param array $SETTINGS - Global settings |
||
| 371 | * @return int - Returns the ID of the newly created item |
||
| 372 | */ |
||
| 373 | private function insertNewItem(array $data, string $password, string $passwordKey, int $userId, array $itemInfos, array $SETTINGS) : int |
||
| 374 | { |
||
| 375 | DB::insert( |
||
| 376 | prefixTable('items'), |
||
| 377 | [ |
||
| 378 | 'label' => $data['label'], |
||
| 379 | 'description' => $data['description'], |
||
| 380 | 'pw' => $password, |
||
| 381 | 'pw_iv' => '', |
||
| 382 | 'pw_len' => strlen($data['password']), |
||
| 383 | 'email' => $data['email'], |
||
| 384 | 'url' => $data['url'], |
||
| 385 | 'id_tree' => $data['folderId'], |
||
| 386 | 'login' => $data['login'], |
||
| 387 | 'inactif' => 0, |
||
| 388 | 'restricted_to' => '', |
||
| 389 | 'perso' => $itemInfos['personal_folder'], |
||
| 390 | 'anyone_can_modify' => $data['anyoneCanModify'], |
||
| 391 | 'complexity_level' => $this->getPasswordComplexityLevel($password), |
||
| 392 | 'encryption_type' => 'teampass_aes', |
||
| 393 | 'fa_icon' => $data['icon'], |
||
| 394 | 'item_key' => uniqidReal(50), |
||
| 395 | 'created_at' => time(), |
||
| 396 | ] |
||
| 397 | ); |
||
| 398 | |||
| 399 | return DB::insertId(); |
||
| 400 | } |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Determines the complexity level of a password based on its strength score. |
||
| 404 | * @param string $password - The encrypted password for which complexity is being evaluated |
||
| 405 | * @return int - Returns the complexity level (0 for weak, higher numbers for stronger passwords) |
||
| 406 | */ |
||
| 407 | private function getPasswordComplexityLevel(string $password) : int |
||
| 412 | } |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Handles tasks that need to be performed after the item is inserted: |
||
| 416 | * 1. Stores sharing keys |
||
| 417 | * 2. Logs the item creation |
||
| 418 | * 3. Adds a task if the folder is not personal |
||
| 419 | * 4. Adds tags to the item |
||
| 420 | * @param int $newID - The ID of the newly created item |
||
| 421 | * @param array $itemInfos - Folder-specific settings |
||
| 422 | * @param int $folderId - Folder ID of the item |
||
| 423 | * @param string $passwordKey - The encryption key for the item |
||
| 424 | * @param int $userId - ID of the user creating the item |
||
| 425 | * @param string $username - Username of the creator |
||
| 426 | * @param string $tags - Tags to be associated with the item |
||
| 427 | * @param array $data - The original data used to create the item (including the label) |
||
| 428 | * @param array $SETTINGS - System settings for logging and task creation |
||
| 429 | */ |
||
| 430 | private function handlePostInsertTasks( |
||
| 431 | int $newID, |
||
| 432 | array $itemInfos, |
||
| 433 | int $folderId, |
||
| 434 | string $passwordKey, |
||
| 435 | int $userId, |
||
| 436 | string $username, |
||
| 437 | string $tags, |
||
| 438 | array $data, |
||
| 439 | array $SETTINGS |
||
| 440 | ) : void { |
||
| 441 | // Create share keys for the creator |
||
| 442 | storeUsersShareKey( |
||
| 443 | prefixTable('sharekeys_items'), |
||
| 444 | (int) $itemInfos['personal_folder'], |
||
| 445 | (int) $folderId, |
||
| 446 | (int) $newID, |
||
| 447 | $passwordKey, |
||
| 448 | true, |
||
| 449 | false, |
||
| 450 | [], |
||
| 451 | -1, |
||
| 452 | $userId |
||
| 453 | ); |
||
| 454 | |||
| 455 | // Log the item creation |
||
| 456 | logItems($SETTINGS, $newID, $data['label'], $userId, 'at_creation', $username); |
||
| 457 | |||
| 458 | // Create a task if the folder is not personal |
||
| 459 | if ((int) $itemInfos['personal_folder'] === 0) { |
||
| 460 | storeTask('new_item', $userId, 0, $folderId, $newID, $passwordKey, [], []); |
||
| 461 | } |
||
| 462 | |||
| 463 | // Add tags to the item |
||
| 464 | $this->addTags($newID, $tags); |
||
| 465 | } |
||
| 466 | |||
| 467 | |||
| 468 | /** |
||
| 469 | * Splits the tags string into individual tags and inserts them into the database. |
||
| 470 | * @param int $newID - The ID of the item to associate tags with |
||
| 471 | * @param string $tags - A comma-separated string of tags |
||
| 472 | */ |
||
| 473 | private function addTags(int $newID, string $tags) : void |
||
| 474 | { |
||
| 475 | $tagsArray = explode(',', $tags); |
||
| 476 | foreach ($tagsArray as $tag) { |
||
| 477 | if (!empty($tag)) { |
||
| 478 | DB::insert( |
||
| 479 | prefixTable('tags'), |
||
| 480 | ['item_id' => $newID, 'tag' => strtolower($tag)] |
||
| 481 | ); |
||
| 482 | } |
||
| 483 | } |
||
| 484 | } |
||
| 485 | |||
| 486 | |||
| 487 | private function isPasswordEmptyAllowed($password, $create_item_without_password) |
||
| 497 | } |
||
| 498 | } |