| Conditions | 19 |
| Paths | 122 |
| Total Lines | 225 |
| Code Lines | 141 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
| 1 | <?php |
||
| 148 | public function addItem( |
||
| 149 | int $folderId, |
||
| 150 | string $label, |
||
| 151 | string $password, |
||
| 152 | string $description, |
||
| 153 | string $login, |
||
| 154 | string $email, |
||
| 155 | string $url, |
||
| 156 | string $tags, |
||
| 157 | string $anyone_can_modify, |
||
| 158 | string $icon, |
||
| 159 | int $userId, |
||
| 160 | string $username |
||
| 161 | ) : array |
||
| 162 | { |
||
| 163 | include_once API_ROOT_PATH . '/../sources/main.functions.php'; |
||
| 164 | $data = [ |
||
| 165 | 'folderId' => $folderId, |
||
| 166 | 'label' => $label, |
||
| 167 | 'password' => $password, |
||
| 168 | 'description' => $description, |
||
| 169 | 'login' => $login, |
||
| 170 | 'email' => $email, |
||
| 171 | 'tags' => $tags, |
||
| 172 | 'anyoneCanModify' => $anyone_can_modify, |
||
| 173 | 'url' => $url, |
||
| 174 | 'icon' => $icon, |
||
| 175 | ]; |
||
| 176 | |||
| 177 | $filters = [ |
||
| 178 | 'folderId' => 'cast:integer', |
||
| 179 | 'label' => 'trim|escape', |
||
| 180 | 'password' => 'trim|escape', |
||
| 181 | 'description' => 'trim|escape', |
||
| 182 | 'login' => 'trim|escape', |
||
| 183 | 'email' => 'trim|escape', |
||
| 184 | 'tags' => 'trim|escape', |
||
| 185 | 'anyoneCanModify' => 'trim|escape', |
||
| 186 | 'url' => 'trim|escape', |
||
| 187 | 'icon' => 'trim|escape', |
||
| 188 | ]; |
||
| 189 | |||
| 190 | $inputData = dataSanitizer( |
||
| 191 | $data, |
||
| 192 | $filters |
||
| 193 | ); |
||
| 194 | extract($inputData); |
||
|
|
|||
| 195 | |||
| 196 | $lang = new Language(); |
||
| 197 | include API_ROOT_PATH . '/../includes/config/tp.config.php'; |
||
| 198 | |||
| 199 | // is pwd empty? |
||
| 200 | if ($this->isPasswordEmptyAllowed($password, $SETTINGS['create_item_without_password'], $lang)) { |
||
| 201 | return [ |
||
| 202 | 'error' => true, |
||
| 203 | 'error_header' => 'HTTP/1.1 422 Unprocessable Entity', |
||
| 204 | 'error_message' => 'Empty password is not allowed' |
||
| 205 | ]; |
||
| 206 | } |
||
| 207 | |||
| 208 | // Check length |
||
| 209 | if (strlen($password) > $SETTINGS['pwd_maximum_length']) { |
||
| 210 | return [ |
||
| 211 | 'error' => true, |
||
| 212 | 'error_header' => 'HTTP/1.1 422 Unprocessable Entity', |
||
| 213 | 'error_message' => 'Password is too long (max allowed is ' . $SETTINGS['pwd_maximum_length'] . ' characters)' |
||
| 214 | ]; |
||
| 215 | } |
||
| 216 | |||
| 217 | // Need info in DB |
||
| 218 | // About special settings |
||
| 219 | $dataFolderSettings = DB::queryFirstRow( |
||
| 220 | 'SELECT bloquer_creation, bloquer_modification, personal_folder |
||
| 221 | FROM ' . prefixTable('nested_tree') . ' |
||
| 222 | WHERE id = %i', |
||
| 223 | $inputData['folderId'] |
||
| 224 | ); |
||
| 225 | $itemInfos = []; |
||
| 226 | $itemInfos['personal_folder'] = $dataFolderSettings['personal_folder']; |
||
| 227 | $itemInfos['no_complex_check_on_modification'] = (int) $itemInfos['personal_folder'] === 1 ? 1 : (int) $dataFolderSettings['bloquer_modification']; |
||
| 228 | $itemInfos['no_complex_check_on_creation'] = (int) $itemInfos['personal_folder'] === 1 ? 1 : (int) $dataFolderSettings['bloquer_creation']; |
||
| 229 | |||
| 230 | // Get folder complexity |
||
| 231 | $folderComplexity = DB::queryfirstrow( |
||
| 232 | 'SELECT valeur |
||
| 233 | FROM ' . prefixTable('misc') . ' |
||
| 234 | WHERE type = %s AND intitule = %i', |
||
| 235 | 'complex', |
||
| 236 | $inputData['folderId'] |
||
| 237 | ); |
||
| 238 | $itemInfos['requested_folder_complexity'] = $folderComplexity !== null ? (int) $folderComplexity['valeur'] : 0; |
||
| 239 | |||
| 240 | // Check COMPLEXITY |
||
| 241 | $zxcvbn = new Zxcvbn(); |
||
| 242 | $passwordStrength = $zxcvbn->passwordStrength($password); |
||
| 243 | $folderPasswordStrength = convertPasswordStrength($itemInfos['requested_folder_complexity']); |
||
| 244 | if ($passwordStrength['score'] < $folderPasswordStrength && (int) $itemInfos['no_complex_check_on_creation'] === 0) { |
||
| 245 | return [ |
||
| 246 | 'error' => true, |
||
| 247 | 'error_header' => 'HTTP/1.1 422 Unprocessable Entity', |
||
| 248 | 'error_message' => 'Password strength is too low' |
||
| 249 | ]; |
||
| 250 | } |
||
| 251 | |||
| 252 | // check if element doesn't already exist |
||
| 253 | DB::queryfirstrow( |
||
| 254 | 'SELECT * FROM ' . prefixTable('items') . ' |
||
| 255 | WHERE label = %s AND inactif = %i', |
||
| 256 | $label, |
||
| 257 | 0 |
||
| 258 | ); |
||
| 259 | if ( |
||
| 260 | DB::count() > 0 |
||
| 261 | && ((isset($SETTINGS['duplicate_item']) === true && (int) $SETTINGS['duplicate_item'] === 0) |
||
| 262 | || (int) $itemInfos['personal_folder'] === 0) |
||
| 263 | ) { |
||
| 264 | return [ |
||
| 265 | 'error' => true, |
||
| 266 | 'error_header' => 'HTTP/1.1 422 Unprocessable Entity', |
||
| 267 | 'error_message' => 'Similar item already exists. Duplicates are not allowed.' |
||
| 268 | ]; |
||
| 269 | } |
||
| 270 | |||
| 271 | // Handle case where pw is empty |
||
| 272 | // if not allowed then warn user |
||
| 273 | if ( |
||
| 274 | isset($SETTINGS['create_item_without_password']) === true && (int) $SETTINGS['create_item_without_password'] === 0 |
||
| 275 | && empty($password) === true |
||
| 276 | ) { |
||
| 277 | return [ |
||
| 278 | 'error' => true, |
||
| 279 | 'error_header' => 'HTTP/1.1 422 Unprocessable Entity', |
||
| 280 | 'error_message' => 'Empty password is not allowed.' |
||
| 281 | ]; |
||
| 282 | } |
||
| 283 | if (empty($password) === false) { |
||
| 284 | $cryptedStuff = doDataEncryption($password); |
||
| 285 | $password = $cryptedStuff['encrypted']; |
||
| 286 | $passwordKey = $cryptedStuff['objectKey']; |
||
| 287 | } else { |
||
| 288 | $passwordKey = ''; |
||
| 289 | } |
||
| 290 | |||
| 291 | // ADD item |
||
| 292 | DB::insert( |
||
| 293 | prefixTable('items'), |
||
| 294 | array( |
||
| 295 | 'label' => $label, |
||
| 296 | 'description' => $description, |
||
| 297 | 'pw' => $password, |
||
| 298 | 'pw_iv' => '', |
||
| 299 | 'email' => $email, |
||
| 300 | 'url' => $url, |
||
| 301 | 'id_tree' => $folderId, |
||
| 302 | 'login' => $login, |
||
| 303 | 'inactif' => 0, |
||
| 304 | 'restricted_to' => '', |
||
| 305 | 'perso' => $itemInfos['personal_folder'], |
||
| 306 | 'anyone_can_modify' => $anyoneCanModify, |
||
| 307 | 'complexity_level' => $passwordStrength['score'], |
||
| 308 | 'encryption_type' => 'teampass_aes', |
||
| 309 | 'fa_icon' => $icon, |
||
| 310 | 'item_key' => uniqidReal(50), |
||
| 311 | 'created_at' => time(), |
||
| 312 | ) |
||
| 313 | ); |
||
| 314 | $newID = DB::insertId(); |
||
| 315 | |||
| 316 | // Create sharekeys for the user itself |
||
| 317 | storeUsersShareKey( |
||
| 318 | prefixTable('sharekeys_items'), |
||
| 319 | (int) $itemInfos['personal_folder'], |
||
| 320 | (int) $folderId, |
||
| 321 | (int) $newID, |
||
| 322 | $passwordKey, |
||
| 323 | true, // only for the item creator |
||
| 324 | false, // no delete all |
||
| 325 | [], |
||
| 326 | -1, |
||
| 327 | $userId |
||
| 328 | ); |
||
| 329 | |||
| 330 | // log |
||
| 331 | logItems( |
||
| 332 | $SETTINGS, |
||
| 333 | (int) $newID, |
||
| 334 | $label, |
||
| 335 | $userId, |
||
| 336 | 'at_creation', |
||
| 337 | $username |
||
| 338 | ); |
||
| 339 | |||
| 340 | // Create new task for the new item |
||
| 341 | // If it is not a personnal one |
||
| 342 | if ((int) $itemInfos['personal_folder'] === 0) { |
||
| 343 | storeTask( |
||
| 344 | 'new_item', |
||
| 345 | $userId, |
||
| 346 | 0, |
||
| 347 | (int) $folderId, |
||
| 348 | (int) $newID, |
||
| 349 | $passwordKey, |
||
| 350 | [], |
||
| 351 | [], |
||
| 352 | ); |
||
| 353 | } |
||
| 354 | |||
| 355 | // Add tags |
||
| 356 | $tags = explode(',', $tags); |
||
| 357 | foreach ($tags as $tag) { |
||
| 358 | if (empty($tag) === false) { |
||
| 359 | DB::insert( |
||
| 360 | prefixTable('tags'), |
||
| 361 | array( |
||
| 362 | 'item_id' => $newID, |
||
| 363 | 'tag' => strtolower($tag), |
||
| 364 | ) |
||
| 365 | ); |
||
| 366 | } |
||
| 367 | } |
||
| 368 | |||
| 369 | return [ |
||
| 370 | 'error' => false, |
||
| 371 | 'message' => 'Item added successfully', |
||
| 372 | 'newId' => $newID, |
||
| 373 | ]; |
||
| 387 | } |