Skip to content

Commit a1d7911

Browse files
Change the path to the version file (#1693)
1 parent a0c24ea commit a1d7911

File tree

1 file changed

+100
-73
lines changed

1 file changed

+100
-73
lines changed

patch.php

+100-73
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,43 @@
1616

1717
define('FILES_JSON_URL', 'https://cdn.easyappointments.org/patch/files.json');
1818

19-
// Setup
19+
// Setup
2020

2121
error_reporting(E_ALL);
2222

23-
ini_set('display_errors', TRUE);
23+
ini_set('display_errors', true);
2424

2525
define('LINE_BREAK', php_sapi_name() === 'cli' ? "\n" : '<br>');
2626

2727
// Functions
2828

2929
function detect_local_version()
3030
{
31-
$config_file_path = __DIR__ . '/application/config/config.php';
31+
$config_file_path = __DIR__ . '/application/config/app.php';
3232

33-
if ( ! file_exists($config_file_path))
34-
{
35-
die('Failed to detect the local Easy!Appointments version, please move the patch.php script in the root directory of your Easy!Appointments installation.');
33+
if (!file_exists($config_file_path)) {
34+
die(
35+
'Failed to detect the local Easy!Appointments version, please move the patch.php script in the root directory of your Easy!Appointments installation.'
36+
);
3637
}
3738

3839
$contents = file_get_contents($config_file_path);
3940

40-
if ($contents === FALSE)
41-
{
42-
die('Could not read the local configuration file, please check the file permissions make sure it is readable: ' . $config_file_path);
41+
if ($contents === false) {
42+
die(
43+
'Could not read the local configuration file, please check the file permissions make sure it is readable: ' .
44+
$config_file_path
45+
);
4346
}
4447

4548
preg_match("/config\['version'].*=.*'(.*)';/", $contents, $matches);
4649

47-
if (empty($matches) || empty($matches[1]))
48-
{
49-
die('Could not parse the version of your installation from "' . $config_file_path . '". Please make sure this file is in its original form.');
50+
if (empty($matches) || empty($matches[1])) {
51+
die(
52+
'Could not parse the version of your installation from "' .
53+
$config_file_path .
54+
'". Please make sure this file is in its original form.'
55+
);
5056
}
5157

5258
return $matches[1];
@@ -56,8 +62,7 @@ function get_applied_patches()
5662
{
5763
$patch_log_file_path = __DIR__ . '/patch-log.php';
5864

59-
if ( ! file_exists($patch_log_file_path))
60-
{
65+
if (!file_exists($patch_log_file_path)) {
6166
return [];
6267
}
6368

@@ -68,38 +73,36 @@ function get_pending_patches($local_version, $applied_patches)
6873
{
6974
$files_json_contents = file_get_contents(FILES_JSON_URL);
7075

71-
if ($files_json_contents === FALSE)
72-
{
73-
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());
76+
if ($files_json_contents === false) {
77+
die(
78+
'Could not read the remote "files.json", make sure the "allow_url_fopen" configuration is "On" inside your "php.ini" file:' .
79+
php_ini_loaded_file()
80+
);
7481
}
7582

76-
$all_patches = json_decode($files_json_contents, TRUE);
83+
$all_patches = json_decode($files_json_contents, true);
7784

78-
if (empty($all_patches))
79-
{
85+
if (empty($all_patches)) {
8086
die('Could not fetch remote patch information, please try again later.');
8187
}
8288

8389
$version_patches = array_filter($all_patches, function ($single_patch_file) use ($local_version) {
84-
return in_array($local_version, $single_patch_file['versions'], FALSE);
90+
return in_array($local_version, $single_patch_file['versions'], false);
8591
});
8692

8793
$pending_patches = array_filter($version_patches, function ($single_patch_file) use ($applied_patches) {
8894
$version_patch_filename = basename($single_patch_file['url']);
8995

90-
foreach ($applied_patches as $applied_patch)
91-
{
92-
if (basename($applied_patch['url']) === $version_patch_filename)
93-
{
94-
return FALSE;
96+
foreach ($applied_patches as $applied_patch) {
97+
if (basename($applied_patch['url']) === $version_patch_filename) {
98+
return false;
9599
}
96100
}
97101

98-
return TRUE;
102+
return true;
99103
});
100104

101-
if (empty($pending_patches))
102-
{
105+
if (empty($pending_patches)) {
103106
die('There are no new patches to apply, you may check again later.');
104107
}
105108

@@ -110,17 +113,18 @@ function apply_pending_patches($local_version, $pending_patches)
110113
{
111114
$new_patches = [];
112115

113-
foreach ($pending_patches as $pending_patch)
114-
{
116+
foreach ($pending_patches as $pending_patch) {
115117
$patch_contents = file_get_contents($pending_patch['url']);
116118

117-
if ($patch_contents === FALSE)
118-
{
119-
die('Could not read the remote "' . basename($pending_patch['url']) . '", make sure the "allow_url_fopen" configuration is "On" inside your "php.ini" file.');
119+
if ($patch_contents === false) {
120+
die(
121+
'Could not read the remote "' .
122+
basename($pending_patch['url']) .
123+
'", make sure the "allow_url_fopen" configuration is "On" inside your "php.ini" file.'
124+
);
120125
}
121126

122-
if (empty($patch_contents))
123-
{
127+
if (empty($patch_contents)) {
124128
die('No contents received while fetching: ' . $pending_patch['url']);
125129
}
126130

@@ -132,14 +136,12 @@ function apply_pending_patches($local_version, $pending_patches)
132136

133137
$patch_body_lines = explode("\n", $patch_body);
134138

135-
array_shift($patch_body_lines); // Remove the first @@ line of the patch body.
139+
array_shift($patch_body_lines); // Remove the first @@ line of the patch body.
136140

137141
$original_code_lines = [];
138142

139-
foreach ($patch_body_lines as $patch_line)
140-
{
141-
if ( ! empty($patch_line[0]) && $patch_line[0] !== '+')
142-
{
143+
foreach ($patch_body_lines as $patch_line) {
144+
if (!empty($patch_line[0]) && $patch_line[0] !== '+') {
143145
$original_code_lines[] = substr($patch_line, 1);
144146
}
145147
}
@@ -148,10 +150,8 @@ function apply_pending_patches($local_version, $pending_patches)
148150

149151
$modified_code_lines = [];
150152

151-
foreach ($patch_body_lines as $patch_line)
152-
{
153-
if ( ! empty($patch_line[0]) && $patch_line[0] !== '-')
154-
{
153+
foreach ($patch_body_lines as $patch_line) {
154+
if (!empty($patch_line[0]) && $patch_line[0] !== '-') {
155155
$modified_code_lines[] = substr($patch_line, 1);
156156
}
157157
}
@@ -160,9 +160,11 @@ function apply_pending_patches($local_version, $pending_patches)
160160

161161
$file_code_contents = file_get_contents($file_path_match[1]);
162162

163-
if ($file_code_contents === FALSE)
164-
{
165-
die('Could not read the local source code file, please check the file permissions make sure it is readable: ' . $file_path_match[1]);
163+
if ($file_code_contents === false) {
164+
die(
165+
'Could not read the local source code file, please check the file permissions make sure it is readable: ' .
166+
$file_path_match[1]
167+
);
166168
}
167169

168170
$file_code_lines = explode("\n", $file_code_contents);
@@ -173,33 +175,47 @@ function apply_pending_patches($local_version, $pending_patches)
173175

174176
$trimmed_affected_code_lines = array_map('trim', $affected_code_lines);
175177

176-
if ($trimmed_affected_code_lines === $trimmed_original_code_lines)
177-
{
178+
if ($trimmed_affected_code_lines === $trimmed_original_code_lines) {
178179
$pre_change_code_lines = array_slice($file_code_lines, 0, abs($affected_position[0]) - 1);
179180

180-
$post_change_code_lines = array_slice($file_code_lines, abs($affected_position[0]) + $affected_position[1] - 1);
181+
$post_change_code_lines = array_slice(
182+
$file_code_lines,
183+
abs($affected_position[0]) + $affected_position[1] - 1,
184+
);
181185

182-
$replaced_file_code_lines = array_merge($pre_change_code_lines, $modified_code_lines, $post_change_code_lines);
186+
$replaced_file_code_lines = array_merge(
187+
$pre_change_code_lines,
188+
$modified_code_lines,
189+
$post_change_code_lines,
190+
);
183191

184192
$patched_file_contents = implode("\n", $replaced_file_code_lines);
185193

186194
$result = file_put_contents($file_path_match[1], $patched_file_contents);
187195

188-
if ($result === FALSE)
189-
{
190-
die('Could not write the local source code file, please check the file permissions make sure it is writable: ' . $file_path_match[1]);
196+
if ($result === false) {
197+
die(
198+
'Could not write the local source code file, please check the file permissions make sure it is writable: ' .
199+
$file_path_match[1]
200+
);
191201
}
192202
}
193203

194-
$success = TRUE;
204+
$success = true;
195205

196206
$message = '';
197207

198-
if ($trimmed_affected_code_lines !== $trimmed_original_code_lines && array_intersect($trimmed_affected_code_lines, $trimmed_modified_code_lines) !== $trimmed_affected_code_lines)
199-
{
200-
$success = FALSE;
208+
if (
209+
$trimmed_affected_code_lines !== $trimmed_original_code_lines &&
210+
array_intersect($trimmed_affected_code_lines, $trimmed_modified_code_lines) !== $trimmed_affected_code_lines
211+
) {
212+
$success = false;
201213

202-
$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'];
214+
$message =
215+
'IMPORTANT: The patch "' .
216+
basename($pending_patch['url']) .
217+
'" cannot be applied, because your local codebase is customized. Download and apply it manually: ' .
218+
$pending_patch['url'];
203219

204220
echo LINE_BREAK . LINE_BREAK . $message . LINE_BREAK;
205221
}
@@ -209,7 +225,7 @@ function apply_pending_patches($local_version, $pending_patches)
209225
'local_version' => $local_version,
210226
'url' => $pending_patch['url'],
211227
'success' => $success,
212-
'message' => $message
228+
'message' => $message,
213229
];
214230
}
215231

@@ -222,16 +238,21 @@ function update_patch_log($applied_patches, $new_patches)
222238

223239
$patch_log_file_path = __DIR__ . '/patch-log.php';
224240

225-
$contents = '
241+
$contents =
242+
'
226243
<?php
227244
228-
return ' . preg_replace("/[0-9]+ \=\>/i", '', var_export($persisted_patches, TRUE)) . ';';
245+
return ' .
246+
preg_replace('/[0-9]+ \=\>/i', '', var_export($persisted_patches, true)) .
247+
';';
229248

230249
$result = file_put_contents($patch_log_file_path, $contents);
231250

232-
if ($result === FALSE)
233-
{
234-
die('Could not write the local "patch-log.php" file, please check the file permissions make sure it is writable: ' . $patch_log_file_path);
251+
if ($result === false) {
252+
die(
253+
'Could not write the local "patch-log.php" file, please check the file permissions make sure it is writable: ' .
254+
$patch_log_file_path
255+
);
235256
}
236257
}
237258

@@ -260,13 +281,19 @@ function get_new_patch_filenames($new_patches)
260281

261282
$new_patches = apply_pending_patches($local_version, $pending_patches);
262283

263-
if (empty($new_patches))
264-
{
265-
echo LINE_BREAK . '➜ No patches were applied, please check the PHP error logs for more information at: ' . ini_get('error_log') . LINE_BREAK;
266-
}
267-
else
268-
{
269-
echo LINE_BREAK . 'The following patches were successfully applied: ' . LINE_BREAK . LINE_BREAK . '' . get_new_patch_filenames($new_patches) . LINE_BREAK;
284+
if (empty($new_patches)) {
285+
echo LINE_BREAK .
286+
'➜ No patches were applied, please check the PHP error logs for more information at: ' .
287+
ini_get('error_log') .
288+
LINE_BREAK;
289+
} else {
290+
echo LINE_BREAK .
291+
'The following patches were successfully applied: ' .
292+
LINE_BREAK .
293+
LINE_BREAK .
294+
'' .
295+
get_new_patch_filenames($new_patches) .
296+
LINE_BREAK;
270297

271298
update_patch_log($applied_patches, $new_patches);
272299
}

0 commit comments

Comments
 (0)