Completed
Branch dev (623cfe)
by Greg
03:59
created

classes/DomainMOD/DwBuild.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * /classes/DomainMOD/DwBuild.php
4
 *
5
 * This file is part of DomainMOD, an open source domain and internet asset manager.
6
 * Copyright (c) 2010-2017 Greg Chetcuti <[email protected]>
7
 *
8
 * Project: http://domainmod.org   Author: http://chetcuti.com
9
 *
10
 * DomainMOD is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
11
 * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
12
 * version.
13
 *
14
 * DomainMOD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
15
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License along with DomainMOD. If not, see
18
 * http://www.gnu.org/licenses/.
19
 *
20
 */
21
//@formatter:off
22
namespace DomainMOD;
23
24
class DwBuild
25
{
26
    public $log;
27
    public $system;
28
29
    public function __construct()
30
    {
31
        $this->log = new Log('class.dwbuild');
32
        $this->system = new System();
33
    }
34
35
    public function build()
36
    {
37
        $accounts = new DwAccounts();
38
        $zones = new DwZones();
39
        $records = new DwRecords();
40
        $servers = new DwServers();
41
        $stats = new DwStats();
42
        $time = new Time();
43
44
        $pdo = $this->system->db();
45
        $result = $servers->get();
46
47
        if (!$result) {
48
49
            $log_message = 'There are no web servers to process in the DW';
50
            $this->log->info($log_message);
51
            return $log_message;
52
53
        }
54
55
        $this->dropDwTables();
56
57
        $build_start_time_o = $time->stamp();
58
59
        $stmt = $pdo->prepare("
60
            UPDATE dw_servers
61
            SET build_status_overall = '0',
62
                build_start_time_overall = :build_start_time_o,
63
                build_end_time_overall = '1978-01-23 00:00:00',
64
                build_time = '0',
65
                build_status = '0',
66
                build_start_time = '1978-01-23 00:00:00',
67
                build_end_time = '1978-01-23 00:00:00',
68
                build_time_overall = '0',
69
                dw_accounts = '0',
70
                dw_dns_zones = '0',
71
                dw_dns_records = '0'");
72
        $stmt->bindValue('build_start_time_o', $build_start_time_o, \PDO::PARAM_STR);
73
        $stmt->execute();
74
75
        $accounts->createTable();
76
        $zones->createTable();
77
        $records->createTable();
78
        $servers->processEachServer($result);
79
80
        $clean = new DwClean();
81
        $clean->all();
82
83
        $result = $servers->get();
84
        $stats->updateServerStats($result);
85
        $stats->updateDwTotalsTable();
86
        $this->buildFinish($build_start_time_o);
87
        list($temp_dw_accounts, $temp_dw_dns_zones, $temp_dw_dns_records) = $stats->getServerTotals();
88
        $has_empty = $this->checkDwAssets($temp_dw_accounts, $temp_dw_dns_zones, $temp_dw_dns_records);
89
        $this->updateEmpty($has_empty);
90
91
        $result_message = 'The Data Warehouse has been rebuilt.';
92
        return $result_message;
93
    }
94
95
    public function dropDwTables()
96
    {
97
        $pdo = $this->system->db();
98
        $pdo->query("DROP TABLE IF EXISTS dw_accounts");
99
        $pdo->query("DROP TABLE IF EXISTS dw_dns_zones");
100
        $pdo->query("DROP TABLE IF EXISTS dw_dns_records");
101
    }
102
103
    public function buildFinish($build_start_time_o)
104
    {
105
        $pdo = $this->system->db();
106
107
        list($build_end_time_o, $total_build_time_o) = $this->getBuildTime($build_start_time_o);
108
109
        $stmt = $pdo->prepare("
110
            UPDATE dw_servers
111
            SET build_status_overall = '1',
112
                build_end_time_overall = :build_end_time_o,
113
                build_time_overall = :total_build_time_o,
114
                has_ever_been_built_overall = '1'");
115
        $stmt->bindValue('build_end_time_o', $build_end_time_o, \PDO::PARAM_STR);
116
        $stmt->bindValue('total_build_time_o', $total_build_time_o, \PDO::PARAM_INT);
117
        $stmt->execute();
118
119
    }
120
121
    public function getBuildTime($build_start_time)
122
    {
123
        $time = new Time();
124
125
        $build_end_time = $time->stamp();
126
127
        $total_build_time = (strtotime($build_end_time) - strtotime($build_start_time));
128
129
        return array($build_end_time, $total_build_time);
130
    }
131
132
    public function checkDwAssets($temp_dw_accounts, $temp_dw_dns_zones, $temp_dw_dns_records)
133
    {
134
        $empty_assets = '0';
135
136
        if ($temp_dw_accounts == "0" && $temp_dw_dns_zones == "0" && $temp_dw_dns_records == "0") {
137
138
            $empty_assets = '1';
139
140
        }
141
142
        return $empty_assets;
143
    }
144
145
    public function updateEmpty($empty_assets)
146
    {
147
        if ($empty_assets == '1') {
148
149
            $this->system->db()->query("
150
                UPDATE dw_servers
151
                SET build_status_overall = '0',
152
                    build_start_time_overall = '1978-01-23 00:00:00',
153
                    build_end_time_overall = '1978-01-23 00:00:00',
154
                    build_time = '0',
155
                    build_status = '0',
156
                    build_start_time = '1978-01-23 00:00:00',
157
                    build_end_time = '1978-01-23 00:00:00',
158
                    build_time_overall = '0',
159
                    build_status_overall = '0',
160
                    dw_accounts = '0',
161
                    dw_dns_zones = '0',
162
                    dw_dns_records = '0'");
163
164
        }
165
    }
166
167
    public function apiCall($api_call, $host, $protocol, $port, $username, $api_token, $hash)
168
    {
169
        return $this->apiGet($api_call, $host, $protocol, $port, $username, $api_token, $hash);
170
    }
171
172
    public function apiGet($api_call, $host, $protocol, $port, $username, $api_token, $hash)
173
    {
174
        $query = $protocol . "://" . $host . ":" . $port . $api_call;
175
        $header = '';
1 ignored issue
show
$header is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
176
        $curl = curl_init(); // Create Curl Object
177
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // Allow certs that do not match the domain
178
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // Allow self-signed certs
179
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // Return contents of transfer on curl_exec
180
        $header = array();
181
        if ($api_token != "") {
182
            $header[0] = "Authorization: WHM " . $username . ":" . $api_token;
183
        } else {
184
            $header[0] = "Authorization: WHM " . $username . ":" . preg_replace("'(\r|\n)'", "", $hash); // Remove newlines
185
        }
186
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header); // Set curl header
187
        curl_setopt($curl, CURLOPT_URL, $query); // Set your URL
188
        $api_results = curl_exec($curl); // Execute Query, assign to $api_results
189
        if ($api_results === false) {
190
            error_log("curl_exec error \"" . curl_error($curl) . "\" for " . $query . "");
191
        }
192
        curl_close($curl);
193
194
        return $api_results;
195
    }
196
197
    public function convertToArray($api_result)
198
    {
199
        return json_decode($api_result, true);
200
    }
201
202
} //@formatter:on
203