Passed
Push — master ( fd5210...56a3aa )
by Nils
04:41
created

FolderController::createNewFolderAction()   A

Complexity

Conditions 5
Paths 12

Size

Total Lines 37
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 25
nc 12
nop 1
dl 0
loc 37
rs 9.2088
c 0
b 0
f 0
1
<?php
2
/**
3
 * Teampass - a collaborative passwords manager.
4
 * ---
5
 * This library is distributed in the hope that it will be useful,
6
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8
 * ---
9
 *
10
 * @project   Teampass
11
 * @version    API
12
 *
13
 * @file      FolderControler.php
14
 * ---
15
 *
16
 * @author    Nils Laumaillé ([email protected])
17
 *
18
 * @copyright 2009-2024 Teampass.net
19
 *
20
 * @license   https://spdx.org/licenses/GPL-3.0-only.html#licenseText GPL-3.0
21
 * ---
22
 *
23
 * @see       https://www.teampass.net
24
 */
25
26
use Symfony\Component\HttpFoundation\Request;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Request. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
27
28
class FolderController extends BaseController
29
{
30
31
    /**
32
     * Get list of Folders
33
     *
34
     * @return void
35
     */
36
    public function listFoldersAction(array $userData)
37
    {
38
        $request = Request::createFromGlobals();
39
        $requestMethod = $request->getMethod();
40
        $strErrorDesc = $responseData = $strErrorHeader = '';
41
42
        if (strtoupper($requestMethod) === 'GET') {
43
            if (empty($userData['folders_list'])) {
44
                $this->sendOutput("", ['HTTP/1.1 204 No Content']);
45
            } else {
46
                try {
47
                    $folderModel = new FolderModel();
48
                    $arrFolders = $folderModel->getFoldersInfo(explode(",", $userData['folders_list']));
49
                    $responseData = json_encode($arrFolders);
50
                } catch (Error $e) {
51
                    $strErrorDesc = $e->getMessage() . ' Something went wrong! Please contact support.3';
52
                    $strErrorHeader = 'HTTP/1.1 500 Internal Server Error';
53
                }
54
            }
55
        } else {
56
            $strErrorDesc = 'Method not supported';
57
            $strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity';
58
        }
59
60
        // send output
61
        if (empty($strErrorDesc) === true) {
62
            $this->sendOutput(
63
                $responseData,
64
                ['Content-Type: application/json', 'HTTP/1.1 200 OK']
65
            );
66
        } else {
67
            $this->sendOutput(
68
                json_encode(['error' => $strErrorDesc]),
69
                ['Content-Type: application/json', $strErrorHeader]
70
            );
71
        }
72
    }
73
    //end listInFoldersAction()
74
75
    /**
76
     * create new folder
77
     *
78
     * @return void
79
     */
80
    public function createAction(array $userData)
81
    {
82
        $request = Request::createFromGlobals();
83
        $requestMethod = $request->getMethod();
84
        //$superGlobal = new SuperGlobal();
85
        $strErrorDesc = $responseData = $strErrorHeader = '';
86
        //$requestMethod = $superGlobal->get('REQUEST_METHOD', 'SERVER');
87
88
        if (strtoupper($requestMethod) === 'POST') {
89
            if (empty($userData['folders_list'])) {
90
                $this->sendOutput("", ['HTTP/1.1 204 No Content']);
91
            } else {
92
                // get parameters
93
                $arrQueryStringParams = $this->getQueryStringParams();
94
95
                try {
96
                    $folderModel = new FolderModel();
97
                    $arrFolder = $folderModel->createFolder(
98
                        (string) $arrQueryStringParams['title'],
99
                        (int) $arrQueryStringParams['parent_id'],
100
                        (int) $arrQueryStringParams['complexity'],
101
                        (int) $arrQueryStringParams['duration'],
102
                        (int) $arrQueryStringParams['create_auth_without'],
103
                        (int) $arrQueryStringParams['edit_auth_without'],
104
                        (string) $arrQueryStringParams['icon'],
105
                        (string) $arrQueryStringParams['icon_selected'],
106
                        (string) $arrQueryStringParams['access_rights'],
107
                        (int) $userData['is_admin'],
108
                        (array) explode(',', $userData['folders_list']),
109
                        (int) $userData['is_manager'],
110
                        (int) $userData['user_can_create_root_folder'],
111
                        (int) $userData['user_can_manage_all_users'],
112
                        (int) $userData['id'],
113
                        (string) $userData['roles'],
114
                    );
115
                    
116
                    $responseData = json_encode($arrFolder);
117
                } catch (Error $e) {
118
                    $strErrorDesc = $e->getMessage() . ' Something went wrong! Please contact support.1';
119
                    $strErrorHeader = 'HTTP/1.1 500 Internal Server Error';
120
                }
121
            }
122
        } else {
123
            $strErrorDesc = 'Method not supported';
124
            $strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity';
125
        }
126
127
        // send output
128
        if (empty($strErrorDesc) === true) {
129
            $this->sendOutput(
130
                $responseData,
131
                ['Content-Type: application/json', 'HTTP/1.1 200 OK']
132
            );
133
        } else {
134
            $this->sendOutput(
135
                json_encode(['error' => $strErrorDesc]),
136
                ['Content-Type: application/json', $strErrorHeader]
137
            );
138
        }
139
    }
140
    //end createFolderAction() 
141
}
142