| Total Complexity | 73 |
| Total Lines | 577 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ItemController 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 ItemController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class ItemController extends BaseController |
||
| 29 | { |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Get user's private key from database |
||
| 33 | * |
||
| 34 | * Retrieves the user's private key by decrypting it from the database. |
||
| 35 | * The private key is stored ENCRYPTED in the database and is decrypted |
||
| 36 | * using the session_key from the JWT token. |
||
| 37 | * |
||
| 38 | * @param array $userData User data from JWT token (must include id, key_tempo, and session_key) |
||
| 39 | * @return string|null Decrypted private key or null if not found/invalid session |
||
| 40 | */ |
||
| 41 | private function getUserPrivateKey(array $userData): ?string |
||
| 42 | { |
||
| 43 | include_once API_ROOT_PATH . '/inc/jwt_utils.php'; |
||
| 44 | |||
| 45 | // Verify session_key exists in JWT payload |
||
| 46 | if (!isset($userData['session_key']) || empty($userData['session_key'])) { |
||
| 47 | error_log('getUserPrivateKey: Missing session_key in JWT token for user ID ' . $userData['id']); |
||
| 48 | return null; |
||
| 49 | } |
||
| 50 | |||
| 51 | $userKeys = get_user_keys( |
||
| 52 | (int) $userData['id'], |
||
| 53 | (string) $userData['key_tempo'], |
||
| 54 | (string) $userData['session_key'] // Session key from JWT for decryption |
||
| 55 | ); |
||
| 56 | |||
| 57 | if ($userKeys === null) { |
||
| 58 | return null; |
||
| 59 | } |
||
| 60 | |||
| 61 | return $userKeys['private_key']; |
||
| 62 | } |
||
| 63 | |||
| 64 | |||
| 65 | /** |
||
| 66 | * Manage case inFolder - get items inside an array of folders |
||
| 67 | * |
||
| 68 | * @param array $userData |
||
| 69 | */ |
||
| 70 | public function inFoldersAction(array $userData): void |
||
| 158 | ); |
||
| 159 | } |
||
| 160 | } |
||
| 161 | //end InFoldersAction() |
||
| 162 | |||
| 163 | private function checkNewItemData(array $arrQueryStringParams, array $userData): array |
||
| 164 | { |
||
| 165 | if (isset($arrQueryStringParams['label']) === true |
||
| 166 | && isset($arrQueryStringParams['folder_id']) === true |
||
| 167 | && isset($arrQueryStringParams['password']) === true |
||
| 168 | && isset($arrQueryStringParams['login']) === true |
||
| 169 | && isset($arrQueryStringParams['email']) === true |
||
| 170 | && isset($arrQueryStringParams['url']) === true |
||
| 171 | && isset($arrQueryStringParams['tags']) === true |
||
| 172 | && isset($arrQueryStringParams['anyone_can_modify']) === true |
||
| 173 | ) { |
||
| 174 | // |
||
| 175 | if (in_array($arrQueryStringParams['folder_id'], $userData['folders_list']) === false && $userData['user_can_create_root_folder'] === 0) { |
||
| 176 | return [ |
||
| 177 | 'error' => true, |
||
| 178 | 'strErrorDesc' => 'User is not allowed in this folder', |
||
| 179 | 'strErrorHeader' => 'HTTP/1.1 401 Unauthorized', |
||
| 180 | ]; |
||
| 181 | } else if (empty($arrQueryStringParams['label']) === true) { |
||
| 182 | return [ |
||
| 183 | 'error' => true, |
||
| 184 | 'strErrorDesc' => 'Label is mandatory', |
||
| 185 | 'strErrorHeader' => 'HTTP/1.1 401 Expected parameters not provided', |
||
| 186 | ]; |
||
| 187 | } else { |
||
| 188 | return [ |
||
| 189 | 'error' => false, |
||
| 190 | ]; |
||
| 191 | } |
||
| 192 | } |
||
| 193 | |||
| 194 | return [ |
||
| 195 | 'error' => true, |
||
| 196 | 'strErrorDesc' => 'All fields have to be provided even if empty (refer to documentation).', |
||
| 197 | 'strErrorHeader' => 'HTTP/1.1 401 Expected parameters not provided', |
||
| 198 | ]; |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Manage case Add |
||
| 203 | * |
||
| 204 | * @param array $userData |
||
| 205 | */ |
||
| 206 | public function createAction(array $userData) |
||
| 207 | { |
||
| 208 | $request = symfonyRequest::createFromGlobals(); |
||
| 209 | $requestMethod = $request->getMethod(); |
||
| 210 | $strErrorDesc = $strErrorHeader = $responseData = ''; |
||
| 211 | |||
| 212 | if (strtoupper($requestMethod) === 'POST') { |
||
| 213 | // Is user allowed to create a folder |
||
| 214 | // We check if allowed_to_create |
||
| 215 | if ((int) $userData['allowed_to_create'] !== 1) { |
||
| 216 | $strErrorDesc = 'User is not allowed to create an item'; |
||
| 217 | $strErrorHeader = 'HTTP/1.1 401 Unauthorized'; |
||
| 218 | } else { |
||
| 219 | if (empty($userData['folders_list']) === false) { |
||
| 220 | $userData['folders_list'] = explode(',', $userData['folders_list']); |
||
| 221 | } else { |
||
| 222 | $userData['folders_list'] = []; |
||
| 223 | } |
||
| 224 | |||
| 225 | // get parameters |
||
| 226 | $arrQueryStringParams = $this->getQueryStringParams(); |
||
| 227 | |||
| 228 | // Check that the parameters are indeed an array before using them |
||
| 229 | if (is_array($arrQueryStringParams)) { |
||
| 230 | // check parameters |
||
| 231 | $arrCheck = $this->checkNewItemData($arrQueryStringParams, $userData); |
||
| 232 | |||
| 233 | if ($arrCheck['error'] === true) { |
||
| 234 | $strErrorDesc = $arrCheck['strErrorDesc']; |
||
| 235 | $strErrorHeader = $arrCheck['strErrorHeader']; |
||
| 236 | } else { |
||
| 237 | // launch |
||
| 238 | $itemModel = new ItemModel(); |
||
| 239 | $ret = $itemModel->addItem( |
||
| 240 | (int) $arrQueryStringParams['folder_id'], |
||
| 241 | (string) $arrQueryStringParams['label'], |
||
| 242 | (string) $arrQueryStringParams['password'], |
||
| 243 | (string) $arrQueryStringParams['description'], |
||
| 244 | (string) $arrQueryStringParams['login'], |
||
| 245 | (string) $arrQueryStringParams['email'], |
||
| 246 | (string) $arrQueryStringParams['url'], |
||
| 247 | (string) $arrQueryStringParams['tags'], |
||
| 248 | (string) $arrQueryStringParams['anyone_can_modify'], |
||
| 249 | (string) $arrQueryStringParams['icon'], |
||
| 250 | (int) $userData['id'], |
||
| 251 | (string) $userData['username'], |
||
| 252 | ); |
||
| 253 | $responseData = json_encode($ret); |
||
| 254 | } |
||
| 255 | |||
| 256 | } else { |
||
| 257 | // Gérer le cas où les paramètres ne sont pas un tableau |
||
| 258 | $strErrorDesc = 'Data not consistent'; |
||
| 259 | $strErrorHeader = 'Expected array, received ' . gettype($arrQueryStringParams); |
||
| 260 | } |
||
| 261 | } |
||
| 262 | } else { |
||
| 263 | $strErrorDesc = 'Method not supported'; |
||
| 264 | $strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity'; |
||
| 265 | } |
||
| 266 | |||
| 267 | // send output |
||
| 268 | if (empty($strErrorDesc) === true) { |
||
| 269 | $this->sendOutput( |
||
| 270 | $responseData, |
||
| 271 | ['Content-Type: application/json', 'HTTP/1.1 200 OK'] |
||
| 272 | ); |
||
| 273 | } else { |
||
| 274 | $this->sendOutput( |
||
| 275 | json_encode(['error' => $strErrorDesc]), |
||
| 276 | ['Content-Type: application/json', $strErrorHeader] |
||
| 277 | ); |
||
| 278 | } |
||
| 279 | } |
||
| 280 | //end addAction() |
||
| 281 | |||
| 282 | |||
| 283 | /** |
||
| 284 | * Manage case get - get an item |
||
| 285 | * |
||
| 286 | * @param array $userData |
||
| 287 | */ |
||
| 288 | public function getAction(array $userData): void |
||
| 357 | ); |
||
| 358 | } |
||
| 359 | } |
||
| 360 | //end getAction() |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Find items by URL |
||
| 364 | * Searches for items matching a specific URL |
||
| 365 | * |
||
| 366 | * @param array $userData User data from JWT token |
||
| 367 | * @return void |
||
| 368 | */ |
||
| 369 | public function findByUrlAction(array $userData): void |
||
| 370 | { |
||
| 371 | $request = symfonyRequest::createFromGlobals(); |
||
| 372 | $requestMethod = $request->getMethod(); |
||
| 373 | $strErrorDesc = $responseData = $strErrorHeader = ''; |
||
| 374 | |||
| 375 | // Get parameters |
||
| 376 | $arrQueryStringParams = $this->getQueryStringParams(); |
||
| 377 | |||
| 378 | if (strtoupper($requestMethod) === 'GET') { |
||
| 379 | // Check if URL parameter is provided |
||
| 380 | if (isset($arrQueryStringParams['url']) === false || empty($arrQueryStringParams['url']) === true) { |
||
| 381 | $this->sendOutput( |
||
| 382 | json_encode(['error' => 'URL parameter is mandatory']), |
||
| 383 | ['Content-Type: application/json', 'HTTP/1.1 400 Bad Request'] |
||
| 384 | ); |
||
| 385 | return; |
||
| 386 | } |
||
| 387 | |||
| 388 | // Prepare user's accessible folders |
||
| 389 | /*if (empty($userData['folders_list']) === false) { |
||
| 390 | $userData['folders_list'] = explode(',', $userData['folders_list']); |
||
| 391 | } else { |
||
| 392 | $userData['folders_list'] = []; |
||
| 393 | }*/ |
||
| 394 | |||
| 395 | // Build SQL constraint for accessible folders |
||
| 396 | $sql_constraint = ' AND (i.id_tree IN (' . $userData['folders_list'] . ')'; |
||
| 397 | if (!empty($userData['restricted_items_list'])) { |
||
| 398 | $sql_constraint .= ' OR i.id IN (' . $userData['restricted_items_list'] . ')'; |
||
| 399 | } |
||
| 400 | $sql_constraint .= ')'; |
||
| 401 | |||
| 402 | // Decode URL if needed |
||
| 403 | $searchUrl = urldecode($arrQueryStringParams['url']); |
||
| 404 | |||
| 405 | try { |
||
| 406 | // Get user's private key from database |
||
| 407 | $userPrivateKey = $this->getUserPrivateKey($userData); |
||
| 408 | if ($userPrivateKey === null) { |
||
| 409 | $strErrorDesc = 'Invalid session or user keys not found'; |
||
| 410 | $strErrorHeader = 'HTTP/1.1 401 Unauthorized'; |
||
| 411 | } else { |
||
| 412 | // Query items with the specific URL |
||
| 413 | $rows = DB::query( |
||
| 414 | "SELECT i.id, i.label, i.login, i.url, i.id_tree, |
||
| 415 | CASE WHEN o.enabled = 1 THEN 1 ELSE 0 END AS has_otp |
||
| 416 | FROM " . prefixTable('items') . " AS i |
||
| 417 | LEFT JOIN " . prefixTable('items_otp') . " AS o ON (o.item_id = i.id) |
||
| 418 | WHERE i.url LIKE %s" . $sql_constraint . " |
||
| 419 | ORDER BY i.label ASC", |
||
| 420 | "%".$searchUrl."%" |
||
| 421 | ); |
||
| 422 | |||
| 423 | $ret = []; |
||
| 424 | foreach ($rows as $row) { |
||
| 425 | // Get user's sharekey for this item |
||
| 426 | $shareKey = DB::queryfirstrow( |
||
|
|
|||
| 427 | 'SELECT share_key |
||
| 428 | FROM ' . prefixTable('sharekeys_items') . ' |
||
| 429 | WHERE user_id = %i AND object_id = %i', |
||
| 430 | $userData['id'], |
||
| 431 | $row['id'] |
||
| 432 | ); |
||
| 433 | |||
| 434 | // Skip if no sharekey found (user doesn't have access) |
||
| 435 | if (DB::count() === 0) { |
||
| 436 | continue; |
||
| 437 | } |
||
| 438 | |||
| 439 | // Build response |
||
| 440 | array_push( |
||
| 441 | $ret, |
||
| 442 | [ |
||
| 443 | 'id' => (int) $row['id'], |
||
| 444 | 'label' => $row['label'], |
||
| 445 | 'login' => $row['login'], |
||
| 446 | 'url' => $row['url'], |
||
| 447 | 'folder_id' => (int) $row['id_tree'], |
||
| 448 | 'has_otp' => (int) $row['has_otp'], |
||
| 449 | ] |
||
| 450 | ); |
||
| 451 | } |
||
| 452 | |||
| 453 | if (!empty($ret)) { |
||
| 454 | $responseData = json_encode($ret); |
||
| 455 | } else { |
||
| 456 | $strErrorDesc = 'No items found with this URL'; |
||
| 457 | $strErrorHeader = 'HTTP/1.1 204 No Content'; |
||
| 458 | } |
||
| 459 | } |
||
| 460 | } catch (Error $e) { |
||
| 461 | $strErrorDesc = $e->getMessage() . '. Something went wrong! Please contact support.'; |
||
| 462 | $strErrorHeader = 'HTTP/1.1 500 Internal Server Error'; |
||
| 463 | } |
||
| 464 | } else { |
||
| 465 | $strErrorDesc = 'Method not supported'; |
||
| 466 | $strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity'; |
||
| 467 | } |
||
| 468 | |||
| 469 | // Send output |
||
| 470 | if (empty($strErrorDesc) === true) { |
||
| 471 | $this->sendOutput( |
||
| 472 | $responseData, |
||
| 473 | ['Content-Type: application/json', 'HTTP/1.1 200 OK'] |
||
| 474 | ); |
||
| 475 | } else { |
||
| 476 | $this->sendOutput( |
||
| 477 | json_encode(['error' => $strErrorDesc]), |
||
| 478 | ['Content-Type: application/json', $strErrorHeader] |
||
| 479 | ); |
||
| 480 | } |
||
| 481 | } |
||
| 482 | //end findByUrlAction() |
||
| 483 | |||
| 484 | |||
| 485 | /** |
||
| 486 | * Get OTP/TOTP code for an item |
||
| 487 | * Retrieves the current 6-digit TOTP code for an item with OTP enabled |
||
| 488 | * |
||
| 489 | * @param array $userData User data from JWT token |
||
| 490 | * @return void |
||
| 491 | */ |
||
| 492 | public function getOtpAction(array $userData): void |
||
| 605 | ); |
||
| 606 | } |
||
| 612 |