Completed
Push — development ( eb9524...db4517 )
by Andrij
28:49 queued 02:09
created

Sys_update::get_license()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 3
eloc 9
nc 3
nop 0
1
<?php
2
3
if (!defined('BASEPATH')) {
4
    exit('No direct script access allowed');
5
}
6
7
class Sys_update extends BaseAdminController
8
{
0 ignored issues
show
introduced by
Opening brace of a class must be on the same line as the definition
Loading history...
9
10
    /**
11
     * instance of Update library
12
     * @var Update
13
     */
14
    private $update;
15
16
    /**
17
     * Sys_update constructor.
18
     */
19
    public function __construct() {
20
21
        if (!extension_loaded('soap')) {
22
            showMessage(lang('PHP SOAP extension is not installed'), '', 'r');
23
            pjax('/admin');
24
        }
25
        parent::__construct();
26
        $this->update = new Update();
27
28
        $this->load->library('lib_admin');
29
        $this->lib_admin->init_settings();
30
        ini_set('soap.wsdl_cache_enabled', '0');
31
    }
32
33
    /**
0 ignored issues
show
introduced by
Doc comment is empty
Loading history...
34
     *
35
     */
36
    public function index() {
37
38
        if (!file_exists('md5.txt')) {
39
            write_file('md5.txt', json_encode($this->update->parse_md5()));
40
        }
41
        try {
42
            $array = $this->update->getStatus();
43
44
        } catch (Exception $e) {
45
46
            $error = $e->getMessage();
47
            $this->lib_admin->log($error);
48
        }
49
50
        $error = $error ?: null;
51
52
        if ($array) {
53
            $data = [
54
                'build' => $array['build_id'],
55
                'date' => date('Y-m-d', $array['time']),
56
                'size' => number_format($array['size'] / 1024 / 1024, 3),
57
                'newRelise' => TRUE,
58
            ];
59
        } else {
60
            $data = [
61
                'newRelise' => FALSE,
62
                'error' => $error,
63
            ];
64
        }
65
66
        $this->template->show('sys_update_info', FALSE, $data);
67
    }
68
69
    /**
70
     * initiate update process
71
     */
72
    public function do_update() {
73
74
        set_time_limit(99999999999999);
75
76
        try {
77
            $this->update->createBackUp();
78
            $this->update->getUpdate();
79
            $this->cache->delete_all();
80
            $this->update->restoreFromZIP(BACKUPFOLDER . 'updates.zip');
81
82
            chmod($this->input->server('DOCUMENT_ROOT'), 0777);
83
            unlink(BACKUPFOLDER . 'updates.zip');
84
85
        } catch (Exception $e) {
86
87
            $this->lib_admin->log($e->getMessage());
88
            echo $e->getMessage();
89
        }
90
91
    }
92
93
    /**
94
     * @param string $sort_by
95
     * @param string $order
96
     */
97
    public function update($sort_by = 'create_date', $order = 'asc') {
98
        $this->load->library('pagination');
99
100
        // Show upgrade window;
101
        try {
102
103
            $status = $this->update->getStatus();
104
            $result = $this->update->getHashSum();
105
106
        } catch (Exception $e) {
107
108
            $result['error'] = $e->getMessage();
109
            $this->lib_admin->log($result['error']);
110
        }
111
        // Create pagination
112
        $filesCount = count($result);
113
        $config['base_url'] = '/admin/sys_update/update?';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$config was never initialized. Although not strictly required by PHP, it is generally a good practice to add $config = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
114
        $config['page_query_string'] = true;
115
        $config['uri_segment'] = 3;
116
        $config['total_rows'] = $filesCount;
117
        $config['per_page'] = 25;
118
        $config['full_tag_open'] = '<div class="pagination pull-left"><ul>';
119
        $config['full_tag_close'] = '</ul></div>';
120
        $config['controls_tag_open'] = '<div class="pagination pull-right"><ul>';
121
        $config['controls_tag_close'] = '</ul></div>';
122
        $config['next_link'] = lang('Next', 'admin') . '&nbsp;&gt;';
123
        $config['prev_link'] = '&lt;&nbsp;' . lang('Prev', 'admin');
124
        $config['cur_tag_open'] = '<li class="btn-primary active"><span>';
125
        $config['cur_tag_close'] = '</span></li>';
126
        $config['prev_tag_open'] = '<li>';
127
        $config['prev_tag_close'] = '</li>';
128
        $config['next_tag_open'] = '<li>';
129
        $config['next_tag_close'] = '</li>';
130
        $config['num_tag_close'] = '</li>';
131
        $config['num_tag_open'] = '<li>';
132
        $config['num_tag_close'] = '</li>';
133
        $config['last_tag_open'] = '<li>';
134
        $config['last_tag_close'] = '</li>';
135
        $config['first_tag_open'] = '<li>';
136
        $config['first_tag_close'] = '</li>';
137
138
        $result = array_chunk($result , 25 , true);
0 ignored issues
show
Coding Style introduced by
Space found before comma in function call
Loading history...
139
        $this->pagination->initialize($config);
140
141
        $page_num = $this->input->get('per_page') >= 25  ? $this->input->get('per_page') / 25 : 0;
0 ignored issues
show
introduced by
Expected 1 space before "?"; 2 found
Loading history...
142
        if (!$result[0]['error']) {
143
            $data = [
144
                'paginator' => $this->pagination->create_links_ajax(),
145
                'filesCount' => $filesCount,
146
                'sort_by' => $sort_by,
147
                'order' => $order,
148
                'diff_files_dates' => $this->update->get_files_dates(),
149
                'diff_files' => $result[$page_num] ,
150
                'restore_files' => $this->sort($this->update->restore_files_list(), $sort_by, $order),
151
                'new_version' => $status ? TRUE : FALSE
152
            ];
153
        } else {
154
            $data = [
155
                'restore_files' => $this->sort($this->update->restore_files_list(), $sort_by, $order),
156
                'error' => $result[0]['error']
157
            ];
158
        }
159
        $this->template->show('sys_update', FALSE, $data);
160
    }
161
162
    /**
0 ignored issues
show
introduced by
Doc comment is empty
Loading history...
163
     *
164
     */
165
    public function restore() {
166
167
        try {
168
            echo $this->update->restoreFromZIP($this->input->post('file_name'));
169
        } catch (Exception $e) {
0 ignored issues
show
Unused Code introduced by
catch (\Exception $e) { ...b_admin->log($error); } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
170
            $error = $e->getMessage();
171
            $this->lib_admin->log($error);
172
        }
173
174
    }
175
176
    /**
0 ignored issues
show
introduced by
Doc comment is empty
Loading history...
177
     *
178
     */
179
    public function renderFile() {
180
181
        $file_path = $this->input->post('file_path');
182
        if (file_exists('.' . $file_path)) {
183
            echo htmlspecialchars(file_get_contents('.' . $file_path));
184
        } else {
185
            echo '';
186
        }
187
    }
188
189
    /**
0 ignored issues
show
introduced by
Doc comment is empty
Loading history...
190
     *
191
     */
192
    public function properties() {
193
194 View Code Duplication
        if ($this->input->post()) {
195
            $this->form_validation->set_rules('careKey', lang('careKey must required', 'admin'), 'trim|required');
196
197
            if ($this->form_validation->run() == FALSE) {
198
                showMessage(validation_errors(), '', 'r');
199
            } else {
200
201
                $this->update->setSettings(['careKey' => trim($this->input->post('careKey'))]);
202
                showMessage(lang('Changes saved', 'admin'));
203
            }
204
        } else {
205
            $data = [
206
                'careKey' => $this->update->getSettings('careKey')
207
            ];
208
            $this->template->show('sys_update_properties', FALSE, $data);
209
        }
210
    }
211
212
    /**
0 ignored issues
show
introduced by
Doc comment is empty
Loading history...
213
     *
214
     */
215
    public function get_license() {
216
217
        if (false === $shopPath = getModulePath('shop')) {
218
            echo 0;
219
            return;
220
        }
221
        $licenseFile = $shopPath . 'license.key';
222
        if (!file_exists($licenseFile)) {
223
            echo 0;
224
            return;
225
        }
226
        echo file_get_contents($licenseFile);
227
    }
228
229
    /**
0 ignored issues
show
introduced by
Doc comment is empty
Loading history...
230
     *
231
     */
232
    public function backup() {
233
234
        try {
235
            $this->update->createBackUp();
236
237
        } catch (Exception $e) {
238
239
            $this->lib_admin->log($e->getMessage());
240
            echo $e->getMessage();
241
        }
242
243
    }
244
245
    /**
246
     * @param array $array
247
     * @param string $sort_by
248
     * @param string $order
249
     * @return array
250
     */
251
    public function sort($array, $sort_by, $order) {
252
253
        $arrayCount = count($array);
254
        for ($i = 0; $i < $arrayCount; $i++) {
255
            for ($y = ($i + 1); $y < $arrayCount; $y++) {
256
                if ($order == 'asc') {
257 View Code Duplication
                    if ($array[$i][$sort_by] < $array[$y][$sort_by]) {
258
                        $c = $array[$i];
259
                        $array[$i] = $array[$y];
260
                        $array[$y] = $c;
261
                    }
262 View Code Duplication
                } else {
263
                    if ($array[$i][$sort_by] > $array[$y][$sort_by]) {
264
                        $c = $array[$i];
265
                        $array[$i] = $array[$y];
266
                        $array[$y] = $c;
267
                    }
268
                }
269
            }
270
        }
271
        return $array;
272
    }
273
274
    public function delete_backup($file_name) {
275
276
        echo unlink(BACKUPFOLDER . $file_name);
277
    }
278
279
    /**
280
     * @param string $file
281
     */
282
    public function getQuerys($file = 'backup.sql') {
283
284
        $restore = file_get_contents($file);
285
286
        $string_query = rtrim($restore, "\n;");
287
        $array_query = explode(";\n", $string_query);
288
289
        echo json_encode($array_query);
290
    }
291
292
    /**
293
     * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be false|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
294
     */
295
    public function Querys() {
296
297
        foreach ($this->input->post('data') as $query) {
298
            if ($query) {
299
                if (!$this->db->query($query)) {
300
                    echo 'Невозможно выполнить запрос: <br>';
301
                    return FALSE;
302
                }
303
            }
304
        }
305
        echo $this->db->total_queries();
306
    }
307
308
}