-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
patch.php
272 lines (193 loc) · 8.71 KB
/
patch.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?php // Place this file in the root E!A directory and open it with the browser or execute in terminal.
/* ----------------------------------------------------------------------------
* Easy!Appointments - Patch Utility Script
*
* @package EasyAppointmentsPatch
* @version 1.0.0
* @author A.Tselegidis <[email protected]>
* @copyright Copyright (c) 2013 - 2022, Alex Tselegidis
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
* @link https://easyappointments.org
* @support Easy!Appointments v1.x.x
* ---------------------------------------------------------------------------- */
// Config
define('FILES_JSON_URL', 'https://cdn.easyappointments.org/patch/files.json');
// Setup
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
define('LINE_BREAK', php_sapi_name() === 'cli' ? "\n" : '<br>');
// Functions
function detect_local_version()
{
$config_file_path = __DIR__ . '/application/config/config.php';
if ( ! file_exists($config_file_path))
{
die('Failed to detect the local Easy!Appointments version, please move the patch.php script in the root directory of your Easy!Appointments installation.');
}
$contents = file_get_contents($config_file_path);
if ($contents === FALSE)
{
die('Could not read the local configuration file, please check the file permissions make sure it is readable: ' . $config_file_path);
}
preg_match("/config\['version'].*=.*'(.*)';/", $contents, $matches);
if (empty($matches) || empty($matches[1]))
{
die('Could not parse the version of your installation from "' . $config_file_path . '". Please make sure this file is in its original form.');
}
return $matches[1];
}
function get_applied_patches()
{
$patch_log_file_path = __DIR__ . '/patch-log.php';
if ( ! file_exists($patch_log_file_path))
{
return [];
}
return require $patch_log_file_path;
}
function get_pending_patches($local_version, $applied_patches)
{
$files_json_contents = file_get_contents(FILES_JSON_URL);
if ($files_json_contents === FALSE)
{
die('Could not read the remote "files.json", make sure the "allow_url_fopen" configuration is "On" inside your "php.ini" file:' . php_ini_loaded_file());
}
$all_patches = json_decode($files_json_contents, TRUE);
if (empty($all_patches))
{
die('Could not fetch remote patch information, please try again later.');
}
$version_patches = array_filter($all_patches, function ($single_patch_file) use ($local_version) {
return in_array($local_version, $single_patch_file['versions'], FALSE);
});
$pending_patches = array_filter($version_patches, function ($single_patch_file) use ($applied_patches) {
$version_patch_filename = basename($single_patch_file['url']);
foreach ($applied_patches as $applied_patch)
{
if (basename($applied_patch['url']) === $version_patch_filename)
{
return FALSE;
}
}
return TRUE;
});
if (empty($pending_patches))
{
die('There are no new patches to apply, you may check again later.');
}
return $pending_patches;
}
function apply_pending_patches($local_version, $pending_patches)
{
$new_patches = [];
foreach ($pending_patches as $pending_patch)
{
$patch_contents = file_get_contents($pending_patch['url']);
if ($patch_contents === FALSE)
{
die('Could not read the remote "' . basename($pending_patch['url']) . '", make sure the "allow_url_fopen" configuration is "On" inside your "php.ini" file.');
}
if (empty($patch_contents))
{
die('No contents received while fetching: ' . $pending_patch['url']);
}
preg_match('/Index: (.*)/', $patch_contents, $file_path_match);
preg_match('/@@ (.*) (.*) @@/', $patch_contents, $position_match);
$patch_body = substr($patch_contents, strpos($patch_contents, '@@'));
$patch_body_lines = explode("\n", $patch_body);
array_shift($patch_body_lines); // Remove the first @@ line of the patch body.
$original_code_lines = [];
foreach ($patch_body_lines as $patch_line)
{
if ( ! empty($patch_line[0]) && $patch_line[0] !== '+')
{
$original_code_lines[] = substr($patch_line, 1);
}
}
$trimmed_original_code_lines = array_map('trim', $original_code_lines);
$modified_code_lines = [];
foreach ($patch_body_lines as $patch_line)
{
if ( ! empty($patch_line[0]) && $patch_line[0] !== '-')
{
$modified_code_lines[] = substr($patch_line, 1);
}
}
$trimmed_modified_code_lines = array_map('trim', $modified_code_lines);
$file_code_contents = file_get_contents($file_path_match[1]);
if ($file_code_contents === FALSE)
{
die('Could not read the local source code file, please check the file permissions make sure it is readable: ' . $file_path_match[1]);
}
$file_code_lines = explode("\n", $file_code_contents);
$affected_position = explode(',', $position_match[1]);
$affected_code_lines = array_slice($file_code_lines, abs($affected_position[0]) - 1, $affected_position[1]);
$trimmed_affected_code_lines = array_map('trim', $affected_code_lines);
if ($trimmed_affected_code_lines === $trimmed_original_code_lines)
{
$pre_change_code_lines = array_slice($file_code_lines, 0, abs($affected_position[0]) - 1);
$post_change_code_lines = array_slice($file_code_lines, abs($affected_position[0]) + $affected_position[1] - 1);
$replaced_file_code_lines = array_merge($pre_change_code_lines, $modified_code_lines, $post_change_code_lines);
$patched_file_contents = implode("\n", $replaced_file_code_lines);
$result = file_put_contents($file_path_match[1], $patched_file_contents);
if ($result === FALSE)
{
die('Could not write the local source code file, please check the file permissions make sure it is writable: ' . $file_path_match[1]);
}
}
$success = TRUE;
$message = '';
if ($trimmed_affected_code_lines !== $trimmed_original_code_lines && array_intersect($trimmed_affected_code_lines, $trimmed_modified_code_lines) !== $trimmed_affected_code_lines)
{
$success = FALSE;
$message = 'IMPORTANT: The patch "' . basename($pending_patch['url']) . '" cannot be applied, because your local codebase is customized. Download and apply it manually: ' . $pending_patch['url'];
echo LINE_BREAK . LINE_BREAK . $message . LINE_BREAK;
}
$new_patches[] = [
'applied_at' => date('Y-m-d H:i:s'),
'local_version' => $local_version,
'url' => $pending_patch['url'],
'success' => $success,
'message' => $message
];
}
return $new_patches;
}
function update_patch_log($applied_patches, $new_patches)
{
$persisted_patches = array_merge($applied_patches, $new_patches);
$patch_log_file_path = __DIR__ . '/patch-log.php';
$contents = '
<?php
return ' . preg_replace("/[0-9]+ \=\>/i", '', var_export($persisted_patches, TRUE)) . ';';
$result = file_put_contents($patch_log_file_path, $contents);
if ($result === FALSE)
{
die('Could not write the local "patch-log.php" file, please check the file permissions make sure it is writable: ' . $patch_log_file_path);
}
}
function get_new_patch_filenames($new_patches)
{
$successful_new_patches = array_filter($new_patches, function ($new_patch) {
return $new_patch['success'];
});
$patch_filenames = array_map(function ($successful_new_patch) {
return basename($successful_new_patch['url']);
}, $successful_new_patches);
return implode(LINE_BREAK . LINE_BREAK . '○ ', $patch_filenames);
}
// Run
echo LINE_BREAK . '➜ Easy!Appointments - Patch Utility Script' . LINE_BREAK . LINE_BREAK;
$local_version = detect_local_version();
$applied_patches = get_applied_patches();
$pending_patches = get_pending_patches($local_version, $applied_patches);
$new_patches = apply_pending_patches($local_version, $pending_patches);
if (empty($new_patches))
{
echo LINE_BREAK . '➜ No patches were applied, please check the PHP error logs for more information at: ' . ini_get('error_log') . LINE_BREAK;
}
else
{
echo LINE_BREAK . 'The following patches were successfully applied: ' . LINE_BREAK . LINE_BREAK . '○ ' . get_new_patch_filenames($new_patches) . LINE_BREAK;
update_patch_log($applied_patches, $new_patches);
}