5
5
* Documentation: https://github.com/Lyquix/php-git-deploy
6
6
*/
7
7
8
- // Measure execution time
9
- $ time = -microtime (true );
8
+ /* Functions */
10
9
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 = '' ;
19
10
// Output buffering handler
20
11
function obHandler ($ buffer ) {
21
12
global $ output ;
22
13
$ output .= $ buffer ;
23
14
return $ buffer ;
24
15
}
25
16
26
- // Check if lock file exists
27
- if ( file_exists ( __DIR__ . ' /deploy.lock ' ) ) {
17
+ // Render error page
18
+ function errorPage ( $ msg ) {
28
19
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" ;
32
26
}
33
27
34
- // Create lock file
35
- $ fh = fopen (__DIR__ . '/deploy.lock ' , 'w ' );
36
- fclose ($ fh );
37
-
38
28
// Command to execute at the end of the script
39
29
function endScript () {
40
30
// Remove lock file
@@ -47,9 +37,9 @@ function endScript() {
47
37
$ output = preg_replace ('/<script[\s\w\W\n]+<\/script>/m ' , '' , $ output );
48
38
$ output = preg_replace ('/<style[\s\w\W\n]+<\/style>/m ' , '' , $ output );
49
39
// 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 );
53
43
// Decode HTML entities
54
44
$ output = html_entity_decode ($ output );
55
45
// Collapse multiple blank lines into one
@@ -60,13 +50,36 @@ function endScript() {
60
50
if (defined ('EMAIL_NOTIFICATIONS ' ) && EMAIL_NOTIFICATIONS !== '' ) error_log ($ output , 1 , EMAIL_NOTIFICATIONS );
61
51
}
62
52
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
+
63
78
// Check if there is a configuration file
64
79
if (file_exists (__DIR__ . '/deploy-config.php ' )) {
65
80
require_once __DIR__ . '/deploy-config.php ' ;
66
81
} 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> ' );
70
83
endScript ();
71
84
die ();
72
85
}
@@ -82,17 +95,13 @@ function endScript() {
82
95
83
96
// If there's authorization error
84
97
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> ' );
88
99
endScript ();
89
100
die ();
90
101
}
91
102
// If there is a configuration error
92
103
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> ' );
96
105
endScript ();
97
106
die ();
98
107
}
0 commit comments