Skip to content

Commit 3bc027b

Browse files
Cleanup code
1 parent 28dcb26 commit 3bc027b

File tree

1 file changed

+40
-31
lines changed

1 file changed

+40
-31
lines changed

deploy.php

+40-31
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,26 @@
55
* Documentation: https://github.com/Lyquix/php-git-deploy
66
*/
77

8-
// Measure execution time
9-
$time = -microtime(true);
8+
/* Functions */
109

11-
// Prevent caching
12-
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
13-
header("Cache-Control: post-check=0, pre-check=0", false);
14-
header("Pragma: no-cache");
15-
16-
// Start output buffering
17-
ob_start('obHandler');
18-
$output = '';
1910
// Output buffering handler
2011
function obHandler($buffer) {
2112
global $output;
2213
$output .= $buffer;
2314
return $buffer;
2415
}
2516

26-
// Check if lock file exists
27-
if (file_exists(__DIR__ . '/deploy.lock')) {
17+
// Render error page
18+
function errorPage($msg) {
2819
header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden', true, 403);
29-
echo "<html>\n<body>\n<h2>File deploy.lock detected, another process already running</h2>\n</body>\n</html>\n";
30-
echo "<!--\n~~~~~~~~~~~~~ Prevent browser friendly error page ~~~~~~~~~~~~~~\n" . str_repeat(str_repeat("~", 64) . "\n", 8) . "-->\n";
31-
die();
20+
echo "<html>\n<body>\n"
21+
. $msg . "\n"
22+
. "</body>\n</html>\n"
23+
. "<!--\n~~~~~~~~~~~~~ Prevent browser friendly error page ~~~~~~~~~~~~~~\n"
24+
. str_repeat(str_repeat("~", 64) . "\n", 8)
25+
. "-->\n";
3226
}
3327

34-
// Create lock file
35-
$fh = fopen(__DIR__ . '/deploy.lock', 'w');
36-
fclose($fh);
37-
3828
// Command to execute at the end of the script
3929
function endScript() {
4030
// Remove lock file
@@ -47,9 +37,9 @@ function endScript() {
4737
$output = preg_replace('/<script[\s\w\W\n]+<\/script>/m', '', $output);
4838
$output = preg_replace('/<style[\s\w\W\n]+<\/style>/m', '', $output);
4939
// Add heading and strip tags
50-
$output = str_repeat("~", 80) . "\n" .
51-
'[' . date('c') . '] - ' . $_SERVER['REMOTE_ADDR'] . " - b=" . $_GET['b'] . ' c=' . $_GET['c'] . "\n" .
52-
strip_tags($output);
40+
$output = str_repeat("~", 80) . "\n"
41+
. '[' . date('c') . '] - ' . $_SERVER['REMOTE_ADDR'] . " - b=" . $_GET['b'] . ' c=' . $_GET['c'] . "\n"
42+
. strip_tags($output);
5343
// Decode HTML entities
5444
$output = html_entity_decode($output);
5545
// Collapse multiple blank lines into one
@@ -60,13 +50,36 @@ function endScript() {
6050
if(defined('EMAIL_NOTIFICATIONS') && EMAIL_NOTIFICATIONS !== '') error_log($output, 1, EMAIL_NOTIFICATIONS);
6151
}
6252

53+
/* Begin Script */
54+
55+
// Measure execution time
56+
$time = -microtime(true);
57+
58+
// Prevent caching
59+
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
60+
header("Cache-Control: post-check=0, pre-check=0", false);
61+
header("Pragma: no-cache");
62+
63+
// Start output buffering
64+
ob_start('obHandler');
65+
$output = '';
66+
67+
// Check if lock file exists
68+
if (file_exists(__DIR__ . '/deploy.lock')) {
69+
errorPage('<h2>File deploy.lock detected, another process already running</h2>');
70+
endScript();
71+
die();
72+
}
73+
74+
// Create lock file
75+
$fh = fopen(__DIR__ . '/deploy.lock', 'w');
76+
fclose($fh);
77+
6378
// Check if there is a configuration file
6479
if (file_exists(__DIR__ . '/deploy-config.php')) {
6580
require_once __DIR__ . '/deploy-config.php';
6681
} else {
67-
header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden', true, 403);
68-
echo "<html>\n<body>\n<h2>File deploy-config.php does not exist</h2>\n</body>\n</html>\n";
69-
echo "<!--\n~~~~~~~~~~~~~ Prevent browser friendly error page ~~~~~~~~~~~~~~\n" . str_repeat(str_repeat("~", 64) . "\n", 8) . "-->\n";
82+
errorPage('<h2>File deploy-config.php does not exist</h2>');
7083
endScript();
7184
die();
7285
}
@@ -82,17 +95,13 @@ function endScript() {
8295

8396
// If there's authorization error
8497
if (!isset($_GET['t']) || $_GET['t'] !== ACCESS_TOKEN || DISABLED === true) {
85-
header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden', true, 403);
86-
echo "<html>\n<body>\n<h2>Access Denied</h2>\n</body>\n</html>\n";
87-
echo "<!--\n~~~~~~~~~~~~~ Prevent browser friendly error page ~~~~~~~~~~~~~~\n" . str_repeat(str_repeat("~", 64) . "\n", 8) . "-->\n";
98+
errorPage('<h2>Access Denied</h2>');
8899
endScript();
89100
die();
90101
}
91102
// If there is a configuration error
92103
if (count($err)) {
93-
header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden', true, 403);
94-
echo "<html>\n<body>\n<h2>Configuration Error</h2>\n<pre>\n" . implode("\n", $err) . "\n</pre>\n</body>\n</html>\n";
95-
echo "<!--\n~~~~~~~~~~~~~ Prevent browser friendly error page ~~~~~~~~~~~~~~\n" . str_repeat(str_repeat("~", 64) . "\n", 8) . "-->\n";
104+
errorPage('<h2>Configuration Error</h2>\n<pre>\n" . implode("\n", $err) . "\n</pre>');
96105
endScript();
97106
die();
98107
}

0 commit comments

Comments
 (0)