|
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; |
|
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
class ControllerToolUpdater extends \Divine\Engine\Core\Controller |
|
|
|
|
|
|
28
|
|
|
{ |
|
29
|
|
|
public function index() |
|
|
|
|
|
|
30
|
|
|
{ |
|
|
|
|
|
|
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'); |
|
|
|
|
|
|
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')); |
|
|
|
|
|
|
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(); |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
130
|
|
|
fclose($fd); |
|
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
// https://github.com/yetanotherape/diff-match-patch |
|
133
|
|
|
$dmp = new \DiffMatchPatch(); |
|
|
|
|
|
|
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'); |
|
|
|
|
|
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
return !$this->error; |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths