Completed
Push — development ( 8bea34...53781d )
by Nils
07:10
created

install/install.js   B

Complexity

Total Complexity 50
Complexity/F 4.17

Size

Lines of Code 246
Function Count 12

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 0
nc 1
dl 0
loc 246
rs 8.6206
c 0
b 0
f 0
wmc 50
mnd 7
bc 48
fnc 12
bpm 4
cpm 4.1666
noi 9

3 Functions

Rating   Name   Duplication   Size   Complexity  
B install.js ➔ GotoNextStep 0 42 5
D install.js ➔ CheckPage 0 186 20
A install.js ➔ aes_encrypt 0 4 1

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
 * @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 aes_encrypt(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 error_msg = "";
0 ignored issues
show
Unused Code introduced by
The variable error_msg seems to be never used. Consider removing it.
Loading history...
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
            data = '{"root_path":"'+$("#root_path").val()+'", "url_path":"'+$("#url_path").val()+'"}';
48
            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"];
49
            multiple = true;
50
            $("#hid_abspath").val($("#root_path").val());
51
            $("#hid_url_path").val($("#url_path").val());
52
        }
53
    } else if (step === "3") {
54
    // STEP 3
55
        if ($("#db_host").val() === "" || $("#db_db").val() === "" || $("#db_login").val() === "" || $("#db_port").val() == "") {
56
            error = "Paths need to be filled in!";
57
        } else if ($("#db_pw").val().indexOf('"') > -1) {
58
            error = "Double quotes in password not allowed!";
59
        } else {
60
            data = '{"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()+'"}';
61
            tasks = ["connection*test"];
62
            multiple = "";
63
            $("#hid_db_host").val($("#db_host").val());
64
            $("#hid_db_bdd").val($("#db_bdd").val());
65
            $("#hid_db_login").val($("#db_login").val());
66
            $("#hid_db_pwd").val($("#db_pw").val());
67
            $("#hid_db_port").val($("#db_port").val());
68
        }
69
    } else if (step === "4") {
70
    // STEP 4
71
        if ($("#admin_pwd").val() === "") {
72
            error = "You must define a password for Admin account!";
73
        } else{
74
            data = '{"tbl_prefix":"'+sanitizeString($("#tbl_prefix").val())+'", "sk_path":"'+sanitizeString($("#sk_path").val())+'", "admin_pwd":"'+sanitizeString($("#admin_pwd").val())+'", "send_stats":""}';
75
            tasks = ["misc*preparation"];
76
            multiple = "";
77
        }
78
    } else if (step === "5") {
79
    // STEP 5
80
        data = '';
81
        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"];
82
        multiple = true;
83
    } else if (step === "6") {
84
    // STEP 6
85
        data = '{"url_path":"'+sanitizeString($("#hid_url_path").val())+'"}';
86
        tasks = ["file*settings.php", "file*sk.php", "file*security", "file*teampass-seckey", "file*csrfp-token"];
87
        multiple = true;
88
    } else if (step === "7") {
89
    // STEP 7
90
        data = '';
91
        tasks = ["file*deleteInstall"];
92
        multiple = true;
93
    }
94
95
    // launch query
96
    if (error === "" && multiple === true) {
97
        var globalResult = true;
0 ignored issues
show
Unused Code introduced by
The variable globalResult seems to be never used. Consider removing it.
Loading history...
98
        var ajaxReqs = [];
99
        var tsk;
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:       aes_encrypt(data), //
114
                    activity:   aes_encrypt(tsk[0]),
115
                    task:       aes_encrypt(tsk[1]),
116
                    db:         aes_encrypt('{"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()+'"}'),
117
                    index:      index,
118
                    multiple:   multiple
119
                },
120
                complete : function(data, statut){
0 ignored issues
show
Unused Code introduced by
The parameter statut 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...
121
                    if (data.responseText == "") {
122
                        // stop error occured, PHP5.5 installed?
123
                        $("#step_result").html("[ERROR] Answer from server is empty. This may occur if PHP version is not at least 5.5. Please check this this fit your server configuration!");
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, statut) {
0 ignored issues
show
Unused Code introduced by
The parameter statut 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
                    data = $.parseJSON(data.responseText);
160
                    $("#step_error").show().html("At least one task has failed! Please correct and relaunch. ");
161
                    $("#res_"+step).html("<img src=\"images/exclamation-red.png\">");
162
                } else {
163
                    $("#but_launch").prop("disabled", true);
164
                    $("#but_launch").hide();
165
                    $("#but_next").prop("disabled", false);
166
                    $("#but_next").show();
167
                    // Hide restart button at end of step 6 if successful
168
                    if (step === "7") {
169
                        $("#but_restart").prop("disabled", true);
170
                        $("#but_restart").hide();
171
                    }
172
                }
173
                $("#step_result").html("");
174
            }, 1000);
175
        });
176
    } else if (error === "" && multiple === "") {
177
        $("#step_result").html("Please wait <img src=\"images/ajax-loader.gif\">");
178
        var tsk = tasks[0].split("*");
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable tsk already seems to be declared on line 99. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
179
        $.ajax({
180
            url: "install.queries.php",
181
            type : 'POST',
182
            dataType : "json",
183
            data : {
184
                type:       "step_"+step,
185
                data:       aes_encrypt(data),
186
                activity:   aes_encrypt(tsk[0]),
187
                task:       aes_encrypt(tsk[1]),
188
                db:         aes_encrypt('{"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()+'"}'),
189
                index:      index,
190
                multiple:   multiple
191
            },
192
            complete : function(data, statut){
0 ignored issues
show
Unused Code introduced by
The parameter statut 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...
193
                data = $.parseJSON(data.responseText);
194
                $("#step_result").html("");
195
                if (data[0].error !== "" ) {
196
                    $("#step_error").show().html("The next ERROR occurred: <i>'"+data[0].error+"'</i><br />Please correct and relaunch.");
197
                    $("#res_"+step).html("<img src=\"images/exclamation-red.png\">");
198
                } else {
199
                    if (data[0].result !== undefined && data[0].result !== "" ) {
200
                        $("#step_result").html("<span style=\"font-weight:bold; margin-right:20px;\">"+data[0].result+"</span>");
201
                    }
202
                    $("#but_launch").prop("disabled", true);
203
                    $("#but_launch").hide();
204
                    $("#but_next").prop("disabled", false);
205
                    $("#but_next").show();
206
                }
207
            },
208
            error : function(resultat, statut, erreur){
0 ignored issues
show
Unused Code introduced by
The parameter statut 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...
Unused Code introduced by
The parameter erreur 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...
Unused Code introduced by
The parameter resultat 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...
209
210
            }
211
        });
212
    } else {
213
        $("#step_error").show().html(error);
214
    }
215
}
216
217
218
function GotoNextStep()
219
{
220
    var step = $("#page_id").val();
221
    var nextStep = parseInt(step)+1;
222
223
    if (nextStep === "8") {
224
        $("#but_restart, #but_next, #but_launch").hide();
225
        $("#but_start").show();
226
        $("#step_result").html("Installation finished.");
227
        $("#step_name").html($("#menu_step"+nextStep).html());
228
        $("#step_content").html($("#text_step"+nextStep).html());
229
        $("#menu_step"+step).switchClass("li_inprogress", "li_done");
230
        $("#menu_step"+nextStep).switchClass("", "li_inprogress");
231
        $("#res_"+step).html("<img src=\"images/tick.png\">");
232
    } else {
233
        $("#page_id").val(nextStep);
234
        $("#but_launch").show().prop("disabled", false);
235
        $("#but_launch").show();
236
        $("#but_restart").show();
237
        $("#but_next").prop("disabled", true);
238
        $("#but_next").hide();
239
        $("#menu_step"+step).switchClass("li_inprogress", "li_done");
240
        $("#menu_step"+nextStep).switchClass("", "li_inprogress");
241
        $("#res_"+step).html("<img src=\"images/tick.png\">");
242
        $("#step_result").html("");
243
        $("#step_name").html($("#menu_step"+nextStep).html());
244
        $("#step_content").html($("#text_step"+nextStep).html());
245
        $('#admin_pwd').live("paste",function(e) {
246
            alert('Paste option is disabled !!');
247
            e.preventDefault();
248
        });
249
        $("#admin_pwd").live('keypress', function(e){
250
            var key = e.charCode || e.keyCode || 0;
251
            // allow backspace, tab, delete, arrows, letters, numbers and keypad numbers ONLY
252
            return (
253
                key != 39
254
            );
255
        });
256
        // Auto start as required
257
        if (nextStep === "5" || nextStep === "6" || nextStep === "7" ) CheckPage();
258
    }
259
}
260