Passed
Push — development ( 22fbae...7d1ffd )
by Nils
09:59
created

install/install.js   B

Complexity

Total Complexity 47
Complexity/F 4.27

Size

Lines of Code 250
Function Count 11

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 22
Bugs 0 Features 0
Metric Value
cc 0
eloc 185
nc 1
dl 0
loc 250
rs 8.64
c 22
b 0
f 0
wmc 47
mnd 7
bc 47
fnc 11
bpm 4.2727
cpm 4.2727
noi 4

3 Functions

Rating   Name   Duplication   Size   Complexity  
A install.js ➔ GotoNextStep 0 44 4
A install.js ➔ aesEncrypt 0 4 1
F install.js ➔ checkPage 0 187 19

How to fix   Complexity   

Complexity

Complex classes like install/install.js 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.

1
/**
2
 * @package       install.js
3
 * @author        Nils Laumaillé <[email protected]>
4
 * @version       2.1.27
5
 * @copyright     (c) 2009-2011 Nils Laumaillé
6
 * @license       GNU GPL-3.0
7
 * @link          https://www.teampass.net
8
 *
9
 * This library is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
 */
13
14
$(function() {
15
    $(".button").button();
16
    $("#but_launch, #step_error, #but_restart").addClass("hidden");
17
    $("#but_launch").prop("disabled", true);
18
19
    // no paste
20
    $("#admin_pwd").bind("paste",function(e) {
21
        alert("Paste option is disabled !!");
0 ignored issues
show
Debugging Code Best Practice introduced by
The alert UI element is often considered obtrusive and is generally only used as a temporary measure. Consider replacing it with another UI element.
Loading history...
22
        e.preventDefault();
23
    });
24
});
25
26
function aesEncrypt(text)
27
{
28
    return Aes.Ctr.encrypt(text, "cpm", 128);
29
}
30
31
function checkPage()
32
{
33
    var step = $("#page_id").val();
34
    var data = "";
35
    var error = "";
36
    var index = "";
37
    var tasks = [];
38
    var multiple = "";
39
    var tsk = "";
40
    $("#step_error").addClass("hidden").html("");
41
    $("#res_"+step).html("");
42
43
    if (step === "2") {
44
    // STEP 2
45
        if ($("#url_path").val() === "" || $("#root_path").val() === "") {
46
            error = "Fields need to be filled in!";
47
        } else {
48
            const jsonValues = {"root_path":$("#root_path").val(), "url_path":$("#url_path").val()};
49
            data = JSON.stringify(jsonValues);
50
            tasks = ["folder*install", "folder*includes", "folder*includes/config", "folder*includes/avatars", "folder*includes/libraries/csrfp/libs", "folder*includes/libraries/csrfp/js", "folder*includes/libraries/csrfp/log", "folder*files", "folder*upload", "extension*mbstring", "extension*openssl", "extension*bcmath", "extension*iconv", "extension*gd", "extension*xml", "extension*curl", "version*php", "ini*max_execution_time"];
51
            multiple = true;
52
            $("#hid_abspath").val($("#root_path").val());
53
            $("#hid_url_path").val($("#url_path").val());
54
        }
55
    } else if (step === "3") {
56
    // STEP 3
57
        if ($("#db_host").val() === "" || $("#db_db").val() === "" || $("#db_login").val() === "" || $("#db_port").val() === "") {
58
            error = "Paths need to be filled in!";
59
        } else if ($("#db_pw").val().indexOf('"') > -1) {
60
            error = "Double quotes in password not allowed!";
61
        } else {
62
            const jsonValues = {"db_host":$("#db_host").val(), "db_bdd":$("#db_bdd").val(), "db_login":$("#db_login").val(), "db_pw":$("#db_pw").val(), "db_port":$("#db_port").val(), "abspath":$("#hid_abspath").val(), "url_path":$("#hid_url_path").val()};
63
            data = JSON.stringify(jsonValues);
64
            tasks = ["connection*test"];
65
            multiple = "";
66
            $("#hid_db_host").val($("#db_host").val());
67
            $("#hid_db_bdd").val($("#db_bdd").val());
68
            $("#hid_db_login").val($("#db_login").val());
69
            $("#hid_db_pwd").val($("#db_pw").val());
70
            $("#hid_db_port").val($("#db_port").val());
71
        }
72
    } else if (step === "4") {
73
    // STEP 4
74
        if ($("#admin_pwd").val() === "") {
75
            error = "You must define a password for Admin account!";
76
        } else{
77
            $("#hid_db_pre").val($("#tbl_prefix").val());
78
            const jsonValues = {"tbl_prefix":sanitizeString($("#tbl_prefix").val()), "sk_path":sanitizeString($("#sk_path").val()), "admin_pwd":sanitizeString($("#admin_pwd").val()), "send_stats":""};
79
            data = JSON.stringify(jsonValues);
80
            tasks = ["misc*preparation"];
81
            multiple = "";
82
        }
83
    } else if (step === "5") {
84
    // STEP 5
85
        data = "";
86
        tasks = ["table*utf8", "table*items", "table*log_items", "table*misc", "table*nested_tree", "table*rights", "table*users", "table*tags", "table*log_system", "table*files", "table*cache", "table*roles_title", "table*roles_values", "table*kb", "table*kb_categories", "table*kb_items", "table*restriction_to_roles", "table*languages", "table*emails", "table*automatic_del", "table*items_edition", "table*categories", "table*categories_items", "table*categories_folders", "table*api", "table*otv", "table*suggestion", "table*tokens", "table*items_change", "table*templates"];
87
        multiple = true;
88
    } else if (step === "6") {
89
    // STEP 6
90
        const jsonValues = {"url_path":sanitizeString($("#hid_url_path").val())};
91
        data = JSON.stringify(jsonValues);
92
        tasks = ["file*sk.php", "file*security", "install*cleanup", "file*settings.php", "file*csrfp-token"];
93
        multiple = true;
94
    }
95
96
    // launch query
97
    if (error === "" && multiple === true) {
98
        var ajaxReqs = [];
99
100
        const dbInfo = {"db_host" : $("#hid_db_host").val(), "db_bdd" : $("#hid_db_bdd").val(), "db_login" : $("#hid_db_login").val(), "db_pw" : $("#hid_db_pwd").val(), "db_port" : $("#hid_db_port").val(), "db_pre" : $("#hid_db_pre").val()};
101
102
        $("#step_result").html("Please wait <img src=\"images/ajax-loader.gif\">");
103
        $("#step_res").val("true");
104
        $("#pop_db").html("");
105
106
        for (index = 0; index < tasks.length; ++index) {
107
            tsk = tasks[index].split("*");
108
            ajaxReqs.push($.ajax({
109
                url: "install.queries.php",
110
                type : "POST",
111
                dataType : "json",
112
                data : {
113
                    type:       "step_"+step,
114
                    data:       aesEncrypt(data), //
115
                    activity:   aesEncrypt(tsk[0]),
116
                    task:       aesEncrypt(tsk[1]),
117
                    db:         aesEncrypt(JSON.stringify(dbInfo)),
118
                    index:      index,
119
                    multiple:   multiple,
120
                    info:       tsk[0]+"-"+tsk[1]
121
                },
122
                complete : function(data){
123
                    if (data.responseText === "") {
124
                        // stop error occured, PHP5.5 installed?
125
                        $("#step_result").html("[ERROR] Answer from server is empty.");
126
                    } else {
127
                        data = $.parseJSON(data.responseText);
128
                        if (data[0].error === "") {
129
                            if (step === "5") {
130
                                if (data[0].activity === "table") {
131
                                    $("#pop_db").append("<li>Table <b>"+data[0].task+"</b> created</li>");
132
                                } else if (data[0].activity === "entry") {
133
                                    $("#pop_db").append("<li>Entries <b>"+data[0].task+"</b> were added</li>");
134
                                }
135
                            } else {
136
                                $("#res"+step+"_check"+data[0].index).html("<img src=\"images/tick.png\">");
137
                            }
138
139
                            if (data[0].result !== undefined && data[0].result !== "" ) {
140
                                $("#step_result").html(data[0].result);
141
                            }
142
                        } else {
143
                            // ignore setting error if regarding setting permissions (step 6, index 2)
144
                            if (step+data[0].index !== "62") {
0 ignored issues
show
Comprehensibility Documentation Best Practice introduced by
This code block is empty. Consider removing it or adding a comment to explain.
Loading history...
145
                                //$("#step_res").val("false");
146
                            }
147
                            $("#res"+step+"_check"+data[0].index).html("<img src=\"images/exclamation-red.png\">&nbsp;<i>"+data[0].error+"</i>");
148
                            $("#pop_db").append("<li><img src=\"images/exclamation-red.png\">&nbsp;Error on task `<b>"+data[0].activity+" > "+data[0].task+"`</b>. <i>"+data[0].error+"</i></li>");
149
                            if (data[0].result !== undefined && data[0].result !== "" ) {
150
                                $("#step_result").html(data[0].result);
151
                            }
152
                        }
153
                    }
154
                }
155
            }));
156
        }
157
        $.when.apply($, ajaxReqs).done(function(data) {
0 ignored issues
show
Unused Code introduced by
The parameter data is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
158
            setTimeout(function(){
159
                // all requests are complete
160
                if ($("#step_res").val() === "false") {
161
                    $("#step_error").removeClass("hidden").html("At least one task has failed! Please correct and relaunch. ");
162
                    $("#res_"+step).html("<img src=\"images/exclamation-red.png\">");
163
                } else {
164
                    $("#but_launch").prop("disabled", true);
165
                    $("#but_launch").addClass("hidden");
166
                    $("#but_next").prop("disabled", false);
167
                    $("#but_next").removeClass("hidden");
168
                    // Hide restart button at end of step 6 if successful
169
                    if (step === "6") {
170
                        $("#but_restart").prop("disabled", true);
171
                        $("#but_restart").addClass("hidden");
172
                    }
173
                }
174
                $("#step_result").html("");
175
            }, 1000);
176
        });
177
    } else if (error === "" && multiple === "") {
178
        $("#step_result").html("Please wait <img src=\"images/ajax-loader.gif\">");
179
        tsk = tasks[0].split("*");
180
181
        const dbInfo = {"db_host" : $("#hid_db_host").val(), "db_bdd" : $("#hid_db_bdd").val(), "db_login" : $("#hid_db_login").val(), "db_pw" : $("#hid_db_pwd").val(), "db_port" : $("#hid_db_port").val()};
182
183
        $.ajax({
184
            url: "install.queries.php",
185
            type : 'POST',
186
            dataType : "json",
187
            data : {
188
                type:       "step_"+step,
189
                data:       aesEncrypt(data),
190
                activity:   aesEncrypt(tsk[0]),
191
                task:       aesEncrypt(tsk[1]),
192
                db:         aesEncrypt(JSON.stringify(dbInfo)),
193
                index:      index,
194
                multiple:   multiple,
195
                info:       tsk[0]+"-"+tsk[1]
196
            },
197
            complete : function(data){
198
                data = $.parseJSON(data.responseText);
199
                $("#step_result").html("");
200
                if (data[0].error !== "" ) {
201
                    $("#step_error").removeClass("hidden").html("The next ERROR occurred: <i>'"+data[0].error+"'</i><br />Please correct and relaunch.");
202
                    $("#res_"+step).html("<img src=\"images/exclamation-red.png\">");
203
                } else {
204
                    if (data[0].result !== undefined && data[0].result !== "" ) {
205
                        $("#step_result").html("<span style=\"font-weight:bold; margin-right:20px;\">"+data[0].result+"</span>");
206
                    }
207
                    $("#but_launch").prop("disabled", true);
208
                    $("#but_launch").addClass("hidden");
209
                    $("#but_next").prop("disabled", false);
210
                    $("#but_next").removeClass("hidden");
211
                }
212
            }
213
        });
214
    } else {
215
        $("#step_error").removeClass("hidden").html(error);
216
    }
217
}
218
219
220
function GotoNextStep()
221
{
222
    var step = $("#page_id").val();
223
    var nextStep = parseInt(step)+1;
224
225
    if (nextStep === 7) {
226
        $("#but_restart, #but_next, #but_launch").addClass("hidden");
227
        $("#but_start").addClass("hidden");
228
        $("#step_result").html("").addClass("hidden");
229
        $("#step_name").html($("#menu_step"+nextStep).html());
230
        $("#step_content").html($("#text_step"+nextStep).html());
231
        $("#menu_step"+step).switchClass("li_inprogress", "li_done");
232
        $("#menu_step"+nextStep).switchClass("", "li_inprogress");
233
        $("#res_"+step).html("<img src=\"images/tick.png\">");
234
    } else {
235
        $("#page_id").val(nextStep);
236
        $("#but_launch").removeClass("hidden").prop("disabled", false);
237
        $("#but_launch").removeClass("hidden");
238
        $("#but_restart").removeClass("hidden");
239
        $("#but_next").prop("disabled", true);
240
        $("#but_next").addClass("hidden");
241
        $("#menu_step"+step).switchClass("li_inprogress", "li_done");
242
        $("#menu_step"+nextStep).switchClass("", "li_inprogress");
243
        $("#res_"+step).html("<img src=\"images/tick.png\">");
244
        $("#step_result").html("");
245
        $("#step_name").html($("#menu_step"+nextStep).html());
246
        $("#step_content").html($("#text_step"+nextStep).html());
247
        $('#admin_pwd').live("paste",function(e) {
248
            alert("Paste option is disabled !!");
0 ignored issues
show
Debugging Code Best Practice introduced by
The alert UI element is often considered obtrusive and is generally only used as a temporary measure. Consider replacing it with another UI element.
Loading history...
249
            e.preventDefault();
250
        });
251
        $("#admin_pwd").live("keypress", function(e){
252
            var key = e.charCode || e.keyCode || 0;
253
            // allow backspace, tab, delete, arrows, letters, numbers and keypad numbers ONLY
254
            return (
255
                key !== 39
256
            );
257
        });
258
        // Auto start as required
259
        if (nextStep === "5" || nextStep === "6" ) {
260
            checkPage();
261
        }
262
    }
263
}
264