Issues (2407)

administration/controller/tool/updater.php (11 issues)

1
<?php
2
3
/* 	Divine CMS - Open source CMS for widespread use.
4
    Copyright (c) 2019 Mykola Burakov ([email protected])
5
6
    See SOURCE.txt for other and additional information.
7
8
    This file is part of Divine CMS.
9
10
    This program is free software: you can redistribute it and/or modify
11
    it under the terms of the GNU General Public License as published by
12
    the Free Software Foundation, either version 3 of the License, or
13
    (at your option) any later version.
14
15
    This program is distributed in the hope that it will be useful,
16
    but WITHOUT ANY WARRANTY; without even the implied warranty of
17
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
    GNU General Public License for more details.
19
20
    You should have received a copy of the GNU General Public License
21
    along with this program. If not, see <http://www.gnu.org/licenses/>. */
22
23
use SebastianBergmann\Diff\Differ;
24
use SebastianBergmann\Diff\Output\StrictUnifiedDiffOutputBuilder;
25
use DiffMatchPatch\DiffMatchPatch;
0 ignored issues
show
The type DiffMatchPatch\DiffMatchPatch was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
27
class ControllerToolUpdater extends \Divine\Engine\Core\Controller
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
28
{
29
    public function index()
0 ignored issues
show
Expected 2 blank lines before function; 0 found
Loading history...
30
    {
0 ignored issues
show
Expected 0 blank lines after opening function brace; 1 found
Loading history...
31
32
        // scripts & styles
33
        // $this->document->addStyle('https://cdnjs.cloudflare.com/ajax/libs/diff2html/2.9.0/diff2html.min.css');
34
        // $this->document->addScript('https://cdnjs.cloudflare.com/ajax/libs/diff2html/2.9.0/diff2html.min.js');
35
        // $this->document->addScript('https://cdnjs.cloudflare.com/ajax/libs/diff2html/2.9.0/diff2html-ui.min.js');
36
37
        // language
38
        // load lang file
39
        $this->load->language('tool/updater');
40
        // set standart defines
41
        $this->document->setTitle($this->language->get('heading_title'));
42
        $data['heading_title'] = $this->language->get('heading_title');
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
43
        // set user defines
44
        $data['updater_update_text'] = $this->language->get('updater_update_text');
45
46
        // alert warning
47
        if (isset($this->error['warning'])) {
48
            $data['error_warning'] = $this->error['warning'];
49
        } else {
50
            $data['error_warning'] = '';
51
        }
52
53
        // alert success
54
        if (isset($this->session->data['success'])) {
55
            $data['success'] = $this->language->get('text_success');
56
57
            unset($this->session->data['success']);
58
        } else {
59
            $data['success'] = '';
60
        }
61
62
        // breadcrumbs
63
        $data['breadcrumbs'] = array();
64
65
        $data['breadcrumbs'][] = array(
66
            'text' => $this->language->get('text_home'),
67
            'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], true)
68
        );
69
70
        $data['breadcrumbs'][] = array(
71
            'text' => $this->language->get('heading_title'),
72
            'href' => $this->url->link('tool/updater', 'token=' . $this->session->data['token'], true)
73
        );
74
75
76
77
78
79
        // links
80
        $data['updater_update_link'] = $this->url->link('tool/updater/update', 'token=' . $this->session->data['token'], true);
81
82
83
84
85
86
87
88
89
90
91
92
93
94
        // page components
95
        $data['header'] = $this->load->controller('common/header');
96
        $data['column'] = $this->load->controller('common/column_left');
97
        $data['footer'] = $this->load->controller('common/footer');
98
99
        // render
100
        $this->response->setOutput($this->load->view('tool/updater', $data));
101
    }
102
103
    // custom function
104
    public function update()
105
    {
106
        // if ($this->validateUpdate()) {
107
108
        // function body
109
        // creating working directory
110
        if (!file_exists($_SERVER['DOCUMENT_ROOT'] . '/storage/update/test')) {
111
            mkdir($_SERVER['DOCUMENT_ROOT'] . '/storage/update/test');
112
        }
113
114
        // set full file path
115
        $file_core_old = ($_SERVER['DOCUMENT_ROOT'] . '/file_core_old.txt');
116
        $file_core_new = ($_SERVER['DOCUMENT_ROOT'] . '/storage/update/test/file_core_new.txt');
117
        $file_result_diff = ($_SERVER['DOCUMENT_ROOT'] . '/file_result_diff.diff');
118
        $file_result_diff_content = file_get_contents(($_SERVER['DOCUMENT_ROOT'] . '/file_result_diff.diff'));
0 ignored issues
show
The assignment to $file_result_diff_content is dead and can be removed.
Loading history...
119
        $file_core_new_test = ($_SERVER['DOCUMENT_ROOT'] . '/storage/update/test/file_core_new_test.txt');
120
121
        // v1. generate diff with SebastianBergmann\Diff\Differ
122
        $cl_differ = new \Differ();
0 ignored issues
show
The type Differ was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
123
        $file_result_diff_to_file = $cl_differ->diff(file_get_contents($file_core_old), file_get_contents($file_core_new));
124
125
        // write generated result to the target file
126
        // file_put_contents($file_result_diff, $file_result_diff_to_file);
127
        // or
128
        $fd = fopen($file_result_diff, 'w+');
129
        fwrite($fd, $file_result_diff_to_file);
0 ignored issues
show
It seems like $fd can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

129
        fwrite(/** @scrutinizer ignore-type */ $fd, $file_result_diff_to_file);
Loading history...
130
        fclose($fd);
0 ignored issues
show
It seems like $fd can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

130
        fclose(/** @scrutinizer ignore-type */ $fd);
Loading history...
131
132
        // https://github.com/yetanotherape/diff-match-patch
133
        $dmp = new \DiffMatchPatch();
0 ignored issues
show
The type DiffMatchPatch was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
134
        $patches = $dmp->patch_make(
135
            file_get_contents($file_core_old),
136
            file_get_contents($file_core_new)
137
        );
138
        $result = $dmp->patch_apply($patches, '1');
139
        $result = $result[0];
140
141
        // write patched result to the target file
142
        $fd = fopen($file_core_new_test, 'w+');
143
        fwrite($fd, $result);
144
        fclose($fd);
145
146
        // \Tracy\Debugger::barDump($patches);
147
        // \Tracy\Debugger::barDump($result);
148
149
        // \Tracy\Debugger::barDump();
150
151
        // alert success after function exec
152
        $this->session->data['success'] = $this->language->get('text_success');
153
154
        // redirect to main module page after function exec
155
        $this->response->redirect($this->url->link('tool/updater', 'token=' . $this->session->data['token'], true));
156
        // }
157
    }
158
159
    public function validateUpdate()
160
    {
161
        // check permissions first
162
        if (!$this->user->hasPermission('modify', 'tool/updater')) {
163
            $this->error['warning'] = $this->language->get('error_permission');
0 ignored issues
show
Bug Best Practice introduced by
The property error does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
164
        }
165
166
        return !$this->error;
167
    }
168
}
169