-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexecution_functions.php
118 lines (111 loc) · 3.68 KB
/
execution_functions.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
<?php
/**
* This function calls the imapsync executable with the delivered args
* @returns either the output or an error message
*/
function execImapsync($args, $actionid) {
$logfile = "log/".$actionid.".log";
system('imapsync '.escapeshellcmd($args)." 1>".$logfile, $ret);
trimUnnecessaryInfo($logfile);
return;
}
/**
* If an actionid is delivered, don't start a new process but check the running process instead
*/
function getActionAndDie(){
if(isset($_REQUEST['actionid']))
displayStatusAndDie(5, $_REQUEST['actionid']);
return;
}
/**
* Displays the current status of a running action / process and returning a refresh.
* Kills the entire PHP script after displaying the status if the mgration has finished with or without an error.
*
* @param time: time in seconds to kill the script after
* @param actionid: The id of the action that shall be reloaded on
*/
function displayStatusAndDie($time, $actionid){
$logfile = "log/".$actionid.".log";
if (file_exists($logfile) == false) {
setReturnHeader(404);
echo "There is no process with Action ID ".$actionid;
die();
}
$contents = file_get_contents($logfile);
if (strpos($contents, "Exiting with return value ") == false) {
sleep($time);
setReturnHeader(102); // HTTP 102 PROCESSING (commonly used by WebDAV)
header("Refresh: 5; url=migrate.php?actionid=".$actionid); // reload page
echo "The process is still running. Please wait.\n\nAction ID: ".$actionid."\n\nStatus:\n".$contents;
die();
}
$errorcodePosition = strpos($contents, "Exiting with return value");
$errorcode = str_replace(" ", "", substr($contents, $errorcodePosition+26, 2));
setReturnHeader(intval($errorcode));
echo "The migration with Action ID ".$actionid." is finished.\n\n ".errorMessageOf($errorcode)."\n\nContact the system admin if necessary. Provide them the following data:\n\n"."Action ID: ".$actionid."\nStatus: ".errorMessageOf($errorcode)."\n".$contents;
die();
}
/**
* Set the HTTP status code
*/
function setReturnHeader($statuscode){
$protocol = $_SERVER['SERVER_PROTOCOL'];
switch ($statuscode){
case 200:
;
case 0:
header($protocol." 200 OK");
return;
case 202:
header($protocol." 202 ACCEPTED");
return;
case 400:
header($protocol." 400 BAD REQUEST");
return;
case 401:
header($protocol." 401 UNAUTHORIZED");
return;
case 403:
header($protocol." 403 FORBIDDEN");
return;
case 404:
header($protocol." 404 NOT FOUND");
return;
case 503:
header($protocol." 503 SERVICE UNAVAILABLE");
return;
case 102:
header($protocol." 102 PROCESSING");
return;
case 16:
header($protocol." 401 ".errorMessageOf($statuscode));
return;
case 10:
header($protocol." 400 ".errorMessageOf($statuscode)."(host/provider may not exist)");
return;
default:
header($protocol." 500 ".errorMessageOf($statuscode));
return;
}
}
function getLastLine($filename){
$line = '';
$f = fopen($filename, 'r');
$cursor = -1;
fseek($f, $cursor, SEEK_END);
$char = fgetc($f);
//Trim trailing newline characters in the file
while ($char === "\n" || $char === "\r") {
fseek($f, $cursor--, SEEK_END);
$char = fgetc($f);
}
//Read until the next line of the file begins or the first newline char
while ($char !== false && $char !== "\n" && $char !== "\r") {
//Prepend the new character
$line = $char . $line;
fseek($f, $cursor--, SEEK_END);
$char = fgetc($f);
}
return $line;
}
?>