Passed
Push — master ( 380c0f...083792 )
by Nils
05:05
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 TeampassClasses\SuperGlobal\SuperGlobal;
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
        $superGlobal = new SuperGlobal();
39
        $strErrorDesc = $responseData = $strErrorHeader = '';
40
        $requestMethod = $superGlobal->get('REQUEST_METHOD', 'SERVER');
41
42
        // get parameters
43
        //$arrQueryStringParams = $this->getQueryStringParams();
44
45
        if (strtoupper($requestMethod) === 'GET') {
46
            if (empty($userData['folders_list'])) {
47
                $this->sendOutput("", ['HTTP/1.1 204 No Content']);
48
            } else {
49
                try {
50
                    $folderModel = new FolderModel();
51
                    $arrFolders = $folderModel->getFoldersInfo(explode(",", $userData['folders_list']));
52
                    $responseData = json_encode($arrFolders);
53
                } catch (Error $e) {
54
                    $strErrorDesc = $e->getMessage() . ' Something went wrong! Please contact support.';
55
                    $strErrorHeader = 'HTTP/1.1 500 Internal Server Error';
56
                }
57
            }
58
        } else {
59
            $strErrorDesc = 'Method not supported';
60
            $strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity';
61
        }
62
63
        // send output
64
        if (empty($strErrorDesc) === true) {
65
            $this->sendOutput(
66
                $responseData,
67
                ['Content-Type: application/json', 'HTTP/1.1 200 OK']
68
            );
69
        } else {
70
            $this->sendOutput(
71
                json_encode(['error' => $strErrorDesc]),
72
                ['Content-Type: application/json', $strErrorHeader]
73
            );
74
        }
75
    }
76
    //end listInFoldersAction()
77
78
    /**
79
     * create new folder
80
     *
81
     * @return void
82
     */
83
    public function createNewFolderAction(array $userData)
84
    {
85
        $superGlobal = new SuperGlobal();
86
        $strErrorDesc = $responseData = $strErrorHeader = '';
87
        $requestMethod = $superGlobal->get('REQUEST_METHOD', 'SERVER');
88
89
        // get parameters
90
        //$arrQueryStringParams = $this->getQueryStringParams();
91
92
        if (strtoupper($requestMethod) === 'POST') {
93
            if (empty($userData['folders_list'])) {
94
                $this->sendOutput("", ['HTTP/1.1 204 No Content']);
95
            } else {
96
                try {
97
                    $folderModel = new FolderModel();
98
                    $arrFolders = $folderModel->getFoldersInfo(explode(",", $userData['folders_list']));
99
                    $responseData = json_encode($arrFolders);
100
                } catch (Error $e) {
101
                    $strErrorDesc = $e->getMessage() . ' Something went wrong! Please contact support.';
102
                    $strErrorHeader = 'HTTP/1.1 500 Internal Server Error';
103
                }
104
            }
105
        } else {
106
            $strErrorDesc = 'Method not supported';
107
            $strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity';
108
        }
109
110
        // send output
111
        if (empty($strErrorDesc) === true) {
112
            $this->sendOutput(
113
                $responseData,
114
                ['Content-Type: application/json', 'HTTP/1.1 200 OK']
115
            );
116
        } else {
117
            $this->sendOutput(
118
                json_encode(['error' => $strErrorDesc]),
119
                ['Content-Type: application/json', $strErrorHeader]
120
            );
121
        }
122
    }
123
    //end createNewFolderAction() 
124
}
125