Completed
Push — development ( 73b519...b85158 )
by Nils
09:02
created

install.js ➔ ... ➔ $.ajax.complete   C

Complexity

Conditions 11
Paths 13

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
nc 13
nop 1
dl 0
loc 33
rs 5.2653
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like install.js ➔ ... ➔ $.ajax.complete 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
 * @file          install.js
3
 * @author        Nils Laumaillé
4
 * @version       2.1.27
5
 * @copyright     (c) 2009-2011 Nils Laumaillé
6
 * @licensing     GNU AFFERO GPL 3.0
7
 * @link          http://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").hide();
17
18
    // no paste
19
    $("#admin_pwd").bind("paste",function(e) {
20
        alert("Paste option is disabled !!");
21
        e.preventDefault();
22
    });
23
});
24
25
function aesEncrypt(text)
26
{
27
    return Aes.Ctr.encrypt(text, "cpm", 128);
28
}
29
30
function checkPage()
31
{
32
    var step = $("#page_id").val();
33
    var data = "";
34
    var error = "";
35
    var index = "";
36
    var tasks = [];
37
    var multiple = "";
38
    var tsk = "";
39
    $("#step_error").hide().html("");
40
    $("#res_"+step).html("");
41
42
    if (step === "2") {
43
    // STEP 2
44
        if ($("#url_path").val() === "" || $("#root_path").val() === "") {
45
            error = "Fields need to be filled in!";
46
        } else {
47
            const jsonValues = {"root_path":$("#root_path").val(), "url_path":$("#url_path").val()};
48
            data = JSON.stringify(jsonValues);
49
            tasks = ["folder*install", "folder*includes", "folder*files", "folder*upload", "extension*mcrypt", "extension*mbstring", "extension*openssl", "extension*bcmath", "extension*iconv", "extension*gd", "function*mysqli_fetch_all", "version*php", "ini*max_execution_time", "folder*includes/avatars", "extension*xml", "folder*includes/libraries/csrfp/libs", "folder*includes/libraries/csrfp/js", "folder*includes/libraries/csrfp/log", "folder*includes/config", "extension*curl"];
50
            multiple = true;
51
            $("#hid_abspath").val($("#root_path").val());
52
            $("#hid_url_path").val($("#url_path").val());
53
        }
54
    } else if (step === "3") {
55
    // STEP 3
56
        if ($("#db_host").val() === "" || $("#db_db").val() === "" || $("#db_login").val() === "" || $("#db_port").val() === "") {
57
            error = "Paths need to be filled in!";
58
        } else if ($("#db_pw").val().indexOf('"') > -1) {
59
            error = "Double quotes in password not allowed!";
60
        } else {
61
            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()};
62
            data = JSON.stringify(jsonValues);
63
            tasks = ["connection*test"];
64
            multiple = "";
65
            $("#hid_db_host").val($("#db_host").val());
66
            $("#hid_db_bdd").val($("#db_bdd").val());
67
            $("#hid_db_login").val($("#db_login").val());
68
            $("#hid_db_pwd").val($("#db_pw").val());
69
            $("#hid_db_port").val($("#db_port").val());
70
        }
71
    } else if (step === "4") {
72
    // STEP 4
73
        if ($("#admin_pwd").val() === "") {
74
            error = "You must define a password for Admin account!";
75
        } else{
76
            $("#hid_db_pre").val($("#tbl_prefix").val());
77
            const jsonValues = {"tbl_prefix":sanitizeString($("#tbl_prefix").val()), "sk_path":sanitizeString($("#sk_path").val()), "admin_pwd":sanitizeString($("#admin_pwd").val()), "send_stats":""};
78
            data = JSON.stringify(jsonValues);
79
            tasks = ["misc*preparation"];
80
            multiple = "";
81
        }
82
    } else if (step === "5") {
83
    // STEP 5
84
        data = "";
85
        tasks = ["table*items", "table*log_items", "table*misc", "table*nested_tree", "table*rights", "table*users", "populate*admin", "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"];
86
        multiple = true;
87
    } else if (step === "6") {
88
    // STEP 6
89
        const jsonValues = {"url_path":sanitizeString($("#hid_url_path").val())};
90
        data = JSON.stringify(jsonValues);
91
        tasks = ["file*teampass-seckey", "file*sk.php", "file*security", "install*cleanup", "file*settings.php", "file*csrfp-token"];
92
        multiple = true;
93
    }
94
95
    // launch query
96
    if (error === "" && multiple === true) {
97
        var ajaxReqs = [];
98
99
        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()};
100
101
        $("#step_result").html("Please wait <img src=\"images/ajax-loader.gif\">");
102
        $("#step_res").val("true");
103
        $("#pop_db").html("");
104
105
        for (index = 0; index < tasks.length; ++index) {
106
            tsk = tasks[index].split("*");
107
            ajaxReqs.push($.ajax({
108
                url: "install.queries.php",
109
                type : "POST",
110
                dataType : "json",
111
                data : {
112
                    type:       "step_"+step,
113
                    data:       aesEncrypt(data), //
114
                    activity:   aesEncrypt(tsk[0]),
115
                    task:       aesEncrypt(tsk[1]),
116
                    db:         aesEncrypt(JSON.stringify(dbInfo)),
117
                    index:      index,
118
                    multiple:   multiple
119
                },
120
                complete : function(data){
121
                    if (data.responseText === "") {
122
                        // stop error occured, PHP5.5 installed?
123
                        $("#step_result").html("[ERROR] Answer from server is empty.");
124
                    } else {
125
                        data = $.parseJSON(data.responseText);
126
                        if (data[0].error === "") {
127
                            if (step === "5") {
128
                                if (data[0].activity === "table") {
129
                                    $("#pop_db").append("<li>Table <b>"+data[0].task+"</b> created</li>");
130
                                } else if (data[0].activity === "entry") {
131
                                    $("#pop_db").append("<li>Entries <b>"+data[0].task+"</b> were added</li>");
132
                                }
133
                            } else {
134
                                $("#res"+step+"_check"+data[0].index).html("<img src=\"images/tick.png\">");
135
                            }
136
137
                            if (data[0].result !== undefined && data[0].result !== "" ) {
138
                                $("#step_result").html(data[0].result);
139
                            }
140
                        } else {
141
                            // ignore setting error if regarding setting permissions (step 6, index 2)
142
                            if (step+data[0].index !== "62") {
143
                                //$("#step_res").val("false");
144
                            }
145
                            $("#res"+step+"_check"+data[0].index).html("<img src=\"images/exclamation-red.png\">&nbsp;<i>"+data[0].error+"</i>");
146
                            $("#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>");
147
                            if (data[0].result !== undefined && data[0].result !== "" ) {
148
                                $("#step_result").html(data[0].result);
149
                            }
150
                        }
151
                    }
152
                }
153
            }));
154
        }
155
        $.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...
156
            setTimeout(function(){
157
                // all requests are complete
158
                if ($("#step_res").val() === "false") {
159
                    $("#step_error").show().html("At least one task has failed! Please correct and relaunch. ");
160
                    $("#res_"+step).html("<img src=\"images/exclamation-red.png\">");
161
                } else {
162
                    $("#but_launch").prop("disabled", true);
163
                    $("#but_launch").hide();
164
                    $("#but_next").prop("disabled", false);
165
                    $("#but_next").show();
166
                    // Hide restart button at end of step 6 if successful
167
                    if (step === "6") {
168
                        $("#but_restart").prop("disabled", true);
169
                        $("#but_restart").hide();
170
                    }
171
                }
172
                $("#step_result").html("");
173
            }, 1000);
174
        });
175
    } else if (error === "" && multiple === "") {
176
        $("#step_result").html("Please wait <img src=\"images/ajax-loader.gif\">");
177
        tsk = tasks[0].split("*");
178
179
        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()};
180
181
        $.ajax({
182
            url: "install.queries.php",
183
            type : 'POST',
184
            dataType : "json",
185
            data : {
186
                type:       "step_"+step,
187
                data:       aesEncrypt(data),
188
                activity:   aesEncrypt(tsk[0]),
189
                task:       aesEncrypt(tsk[1]),
190
                db:         aesEncrypt(JSON.stringify(dbInfo)),
191
                index:      index,
192
                multiple:   multiple
193
            },
194
            complete : function(data){
195
                data = $.parseJSON(data.responseText);
196
                $("#step_result").html("");
197
                if (data[0].error !== "" ) {
198
                    $("#step_error").show().html("The next ERROR occurred: <i>'"+data[0].error+"'</i><br />Please correct and relaunch.");
199
                    $("#res_"+step).html("<img src=\"images/exclamation-red.png\">");
200
                } else {
201
                    if (data[0].result !== undefined && data[0].result !== "" ) {
202
                        $("#step_result").html("<span style=\"font-weight:bold; margin-right:20px;\">"+data[0].result+"</span>");
203
                    }
204
                    $("#but_launch").prop("disabled", true);
205
                    $("#but_launch").hide();
206
                    $("#but_next").prop("disabled", false);
207
                    $("#but_next").show();
208
                }
209
            }
210
        });
211
    } else {
212
        $("#step_error").show().html(error);
213
    }
214
}
215
216
217
function GotoNextStep()
218
{
219
    var step = $("#page_id").val();
220
    var nextStep = parseInt(step)+1;
221
222
    if (nextStep === 7) {
223
        $("#but_restart, #but_next, #but_launch").hide();
224
        $("#but_start").hide();
225
        $("#step_result").html("").hide();
226
        $("#step_name").html($("#menu_step"+nextStep).html());
227
        $("#step_content").html($("#text_step"+nextStep).html());
228
        $("#menu_step"+step).switchClass("li_inprogress", "li_done");
229
        $("#menu_step"+nextStep).switchClass("", "li_inprogress");
230
        $("#res_"+step).html("<img src=\"images/tick.png\">");
231
    } else {
232
        $("#page_id").val(nextStep);
233
        $("#but_launch").show().prop("disabled", false);
234
        $("#but_launch").show();
235
        $("#but_restart").show();
236
        $("#but_next").prop("disabled", true);
237
        $("#but_next").hide();
238
        $("#menu_step"+step).switchClass("li_inprogress", "li_done");
239
        $("#menu_step"+nextStep).switchClass("", "li_inprogress");
240
        $("#res_"+step).html("<img src=\"images/tick.png\">");
241
        $("#step_result").html("");
242
        $("#step_name").html($("#menu_step"+nextStep).html());
243
        $("#step_content").html($("#text_step"+nextStep).html());
244
        $('#admin_pwd').live("paste",function(e) {
245
            alert("Paste option is disabled !!");
246
            e.preventDefault();
247
        });
248
        $("#admin_pwd").live("keypress", function(e){
249
            var key = e.charCode || e.keyCode || 0;
250
            // allow backspace, tab, delete, arrows, letters, numbers and keypad numbers ONLY
251
            return (
252
                key !== 39
253
            );
254
        });
255
        // Auto start as required
256
        if (nextStep === "5" || nextStep === "6" ) {
257
            checkPage();
258
        }
259
    }
260
}
261