@@ -24,12 +24,12 @@ function errorPage($msg) {
24
24
. $ msg . "\n"
25
25
. "</body> \n</html> \n"
26
26
. "<!-- \n~~~~~~~~~~~~~ Prevent browser friendly error page ~~~~~~~~~~~~~~ \n"
27
- . str_repeat (str_repeat ("~ " , 64 ) . "\n" , 8 )
27
+ . str_repeat (str_repeat ("~ " , 64 ) . "\n" , 8 )
28
28
. "--> \n" ;
29
29
}
30
30
31
31
// Command to execute at the end of the script
32
- function endScript ($ errorMessage = "" ) {
32
+ function endScript ($ msg = "" ) {
33
33
// Remove lock file
34
34
unlink (__DIR__ . '/deploy.lock ' );
35
35
// Flush buffer and prepare output for log and email
@@ -51,7 +51,7 @@ function endScript($errorMessage = "") {
51
51
if (defined ('LOG_FILE ' ) && LOG_FILE !== '' ) error_log ($ output , 3 , LOG_FILE );
52
52
// Send email notification
53
53
if (defined ('EMAIL_NOTIFICATIONS ' ) && EMAIL_NOTIFICATIONS !== '' ) error_log ($ output , 1 , EMAIL_NOTIFICATIONS );
54
- die ($ errorMessage );
54
+ die ($ msg );
55
55
}
56
56
57
57
/* Begin Script Execution */
@@ -69,8 +69,9 @@ function endScript($errorMessage = "") {
69
69
if (file_exists (__DIR__ . '/deploy-config.php ' )) {
70
70
require_once __DIR__ . '/deploy-config.php ' ;
71
71
} else {
72
- errorPage ('<h2>File deploy-config.php does not exist</h2> ' );
73
- endScript ();
72
+ $ msg = 'File deploy-config.php does not exist ' ;
73
+ errorPage ('<h2> ' . $ msg . '</h2> ' );
74
+ endScript ($ msg );
74
75
}
75
76
76
77
// Check configuration errors
@@ -87,13 +88,14 @@ function endScript($errorMessage = "") {
87
88
// If there is a configuration error
88
89
if (count ($ err )) {
89
90
errorPage ("<h2>Configuration Error</h2> \n<pre> \n" . implode ("\n" , $ err ) . "\n</pre> " );
90
- endScript ();
91
+ endScript (" Configuration Error: \n\n" . implode ( "\n" , $ err ) );
91
92
}
92
93
93
94
// Check if lock file exists
94
95
if (file_exists (__DIR__ . '/deploy.lock ' )) {
95
- errorPage ('<h2>File deploy.lock detected, another process already running</h2> ' );
96
- endScript ();
96
+ $ msg = 'File deploy.lock detected, another process already running ' ;
97
+ errorPage ('<h2> ' . $ msg . '</h2> ' );
98
+ endScript ($ msg );
97
99
}
98
100
99
101
// Create lock file
@@ -129,15 +131,17 @@ function endScript($errorMessage = "") {
129
131
}
130
132
}
131
133
if (!$ allow ) {
132
- errorPage ('<h2>Access Denied</h2> ' );
133
- endScript ();
134
+ $ msg = 'Access Denied ' ;
135
+ errorPage ('<h2> ' . $ msg . '</h2> ' );
136
+ endScript ($ msg );
134
137
}
135
138
}
136
139
137
140
// If there's authorization error
138
141
if (!isset ($ _GET ['t ' ]) || $ _GET ['t ' ] !== ACCESS_TOKEN || DISABLED === true ) {
139
- errorPage ('<h2>Access Denied</h2> ' );
140
- endScript ();
142
+ $ msg = 'Access Denied ' ;
143
+ errorPage ('<h2> ' . $ msg . '</h2> ' );
144
+ endScript ($ msg );
141
145
}
142
146
?>
143
147
<!DOCTYPE html>
@@ -187,8 +191,9 @@ function getallheaders() {
187
191
// Check branch
188
192
$ branch = $ payload ->pullrequest ->destination ->branch ->name ;
189
193
} else {
190
- echo "\nOnly push and merged pull request events are processed \n\nDone. \n</pre></body></html> " ;
191
- endScript ();
194
+ $ msg = 'Only push and merged pull request events are processed ' ;
195
+ echo "\n" . $ msg . "\n\nDone. \n</pre></body></html> " ;
196
+ endScript ($ msg );
192
197
}
193
198
} else if (isset ($ headers ['X-GitHub-Event ' ])) {
194
199
// Github webhook
@@ -204,17 +209,19 @@ function getallheaders() {
204
209
// Check branch
205
210
$ branch = $ payload ->pull_request ->head ->ref ;
206
211
} else {
207
- echo "\nOnly push and merged pull request events are processed \n\nDone. \n</pre></body></html> " ;
208
- endScript ();
212
+ $ msg = 'Only push and merged pull request events are processed ' ;
213
+ echo "\n" . $ msg . "\n\nDone. \n</pre></body></html> " ;
214
+ endScript ($ msg );
209
215
}
210
216
}
211
217
212
218
// Branch from webhook?
213
219
if ($ branch ) {
214
220
// Only main branch is allowed for webhook deployments
215
221
if ($ branch != unserialize (BRANCH )[0 ]) {
216
- echo "\nBranch $ branch not allowed, stopping execution. \n</pre></body></html> " ;
217
- endScript ();
222
+ $ msg = 'Branch ' . $ branch . ' not allowed, stopping execution. ' ;
223
+ echo "\n" . $ msg . "\n</pre></body></html> " ;
224
+ endScript ($ msg );
218
225
}
219
226
220
227
} else {
@@ -223,9 +230,10 @@ function getallheaders() {
223
230
$ branch = $ _GET ['b ' ];
224
231
// Check if branch is allowed
225
232
if (!in_array ($ branch , unserialize (BRANCH ))) {
226
- echo "\nBranch $ branch not allowed, stopping execution. \n</pre></body></html> " ;
227
- endScript ();
228
- }
233
+ $ msg = 'Branch ' . $ branch . ' not allowed, stopping execution. ' ;
234
+ echo "\n" . $ msg . "\n</pre></body></html> " ;
235
+ endScript ($ msg );
236
+ }
229
237
} else {
230
238
$ branch = unserialize (BRANCH )[0 ];
231
239
echo "No branch specified, assuming default branch $ branch \n" ;
@@ -241,7 +249,9 @@ function getallheaders() {
241
249
$ path = trim (shell_exec ('which ' .$ command ));
242
250
if ($ path == '' ) {
243
251
header ($ _SERVER ['SERVER_PROTOCOL ' ] . ' 500 Internal Server Error ' , true , 500 );
244
- endScript (sprintf ('<div class="error"><b>%s</b> not available. It needs to be installed on the server for this script to work.</div> ' , $ command ));
252
+ $ msg = $ command . ' not available. It needs to be installed on the server for this script to work. ' ;
253
+ echo $ msg ;
254
+ endScript ($ msg );
245
255
} else {
246
256
$ version = explode ("\n" , shell_exec ($ command .' --version ' ));
247
257
printf ('<b>%s</b> : %s ' ."\n"
@@ -279,11 +289,9 @@ function cmd($command, $print = true, $dir = GIT_DIR) {
279
289
// Error handling and cleanup
280
290
if ($ print && $ return_code !== 0 ) {
281
291
header ($ _SERVER ['SERVER_PROTOCOL ' ] . ' 500 Internal Server Error ' , true , 500 );
282
- printf ('<span class="error">Error encountered! Stopping the script to prevent possible data loss.
283
- CHECK THE DATA IN YOUR TARGET DIR!</span>
284
- '
285
- );
286
- endScript ();
292
+ $ msg = "Error encountered! Stopping the script to prevent possible data loss. \nCHECK THE DATA IN YOUR TARGET DIR! " ;
293
+ echo '<span class="error"> ' . $ msg . '</span> ' ;
294
+ endScript ($ msg );
287
295
}
288
296
289
297
return $ tmp ;
@@ -358,15 +366,15 @@ function cmd($command, $print = true, $dir = GIT_DIR) {
358
366
, GIT_DIR
359
367
, GIT_DIR
360
368
, $ checkout
361
- ));
369
+ ));
362
370
363
371
// Update the submodules
364
372
echo "\nUpdating git submodules in git directory \n" ;
365
373
cmd (sprintf (
366
374
'git --git-dir="%s.git" --work-tree="%s" submodule update --init --recursive '
367
375
, GIT_DIR
368
376
, GIT_DIR
369
- ));
377
+ ));
370
378
371
379
// Get current version or assume oldest commit
372
380
if (file_exists (TARGET_DIR . 'VERSION ' )) {
@@ -473,5 +481,5 @@ function cmd($command, $print = true, $dir = GIT_DIR) {
473
481
</pre>
474
482
</body>
475
483
</html>
476
- <?php
477
- endScript ();
484
+ <?php
485
+ endScript ();
0 commit comments