-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpreprocessing_functions.php
153 lines (143 loc) · 5.61 KB
/
preprocessing_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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
/**
* Generate and return the Action ID
*/
function getActionID() {
return "".date('YmdHisv').rand(100,999);
}
/**
* Checks if all required arguments are set with the HTTP request.
* @returns true, if all required arguments are set
* false, otherwise
*/
function allRequiredArgsSet() {
if (isset($_REQUEST['email1']) && isset($_REQUEST['email2']) && isset($_REQUEST['password1']) && isset($_REQUEST['password2']) && isset($_REQUEST['host1'])) {
return true;
}
return false;
}
/**
* Checks if the enteres domain is a valid host as defined in the 'hosts_array'.
* @param host: The host/domain to be checked
* @param hosts_array: String Array of hosts/domains that are valid
* @returns true, if the host/domain is valid
* false, otherwise
*/
function isHostValid($host, $hosts_array){
if ($host == false)
return true;
foreach(array_keys($hosts_array) as $validhost) {
if (strcmp($host, $validhost) == 0)
return true;
}
return false;
}
/**
* Turns email1, email2, password1 and password2 into lowercase and returns as an array.
* This is required as below functions will compare those to some pre-defined lowercase values.
*/
function getLowerCaseArray($email1, $email2, $password1, $password2){
return array(
'email1' => strtolower($email1),
'email2' => strtolower($email2),
'passwd1' => strtolower($password1),
'passwd2' => strtolower($password2)
);
}
/**
* Checks if the arguments have blocked values.
* @param args_array: String Array with arguments to be checked
* @param blockedvalues: Array of String values to be blocked // TODO Remove
* @returns true, if args has no blocked values
* false, otherwise
*/
function hasNoBlockedValues($args_array, $blockedvalues){
foreach($args_array as $arg){
foreach($blockedvalues as $blockedvalue){
if(strpos($arg, $blockedvalue) != false){
unset($arg);
unset($blockedvalue);
return false;
}
}
}
unset($arg);
unset($blockedvalue);
return true;
}
/**
* @param email: The email address its domain name is to be retrieved
* @returns the domain name in '[email protected]'
*/
function getDomainName($email){
$atPosition = stripos($email, "@", 0);
return strtolower(substr($email, $atPosition+1));
}
/**
* @param email: the email address to be checked
* @param trusteddomains: String Array of trusted domains
* @returns true, if email domain is in trusteddomains
* false, otherwise
*/
function isInTrustedDomain($email, $trusteddomains){
for($i = 0; $i < count($trusteddomains); $i++){
if(strcasecmp(getDomainName($email), $trusteddomains[$i]) == 0) {
return true;
}
}
return false;
}
/**
* @param email: The email address its IMAP host is to be retrieved
* @param hosts_array: String array containing IMAP hosts for domains and providers // TODO Remove
* @param host: false, if the pre-defined remote host for the domain shall be used
* else, the provider's name in lowercase String, if a predefined provider shall be used
* default: false
* @returns the IMAP host for the email address
*/
function hostOf($email, $hosts_array, $host = false){
if($host != false) {
return $hosts_array[$host];
}
return $hosts_array[getDomainName($email)];
}
/**
* @param email: the email adddress of the user-part
* @param userpart_array: An array telling if the domain requires the full email address
* as the username or just the user-part in '[email protected]' // TODO Remove
* @returns the username to authenticate to the IMAP server with
*/
function getUsername($email, $userpart_array){
if($userpart_array[getDomainName($email)]){
return $email;
}
$atPosition = stripos($email, "@", 0);
$email = substr($email, 0, $atPosition);
}
/**
* Creates the arguments to be passed to 'imapsync'.
*
* Caution: Do NOT pass unchecked args. Always use 'isInTrustedDomain($email)' and 'hasNoBlockedValues($args_array)' first.
* Do NOT pass the returned arguments to the system without 'escapeshellcmd($args)'
*
* Assumes: the username to authenticate with the IMAP server is either the email address or the user-part in '[email protected]'
*
* @param email1, email2: The source email address and the target email address
* @param password1, password2: The source IMAP accounts password and the target IMAP accounts password
* @param hosts_array: String array containing IMAP hosts for domains and providers // TODO remove
* @param userpart_array: An array telling if the domain requires the full email address // TODO Remove
* as the username or just the user-part in '[email protected]'
* @param host1, host2: false, if the pre-defined remote host for the domain shall be used
* else, the provider's name in lowercase String, if a predefined provider shall be used
* default: false
*
* @returns the arguments to be passed to 'imapsync'
*/
function createImapsyncArgs($email1, $email2, $password1, $password2, $userpart_array, $hosts_array, $host1 = false, $host2 = false){
$user1 = getUsername($email1, $userpart_array);
$user2 = getUsername($email2, $userpart_array);
$host1 = hostOf($email1, $hosts_array, $host1);
$host2 = ($host2 == false)? hostOf($email1, $hosts_array, $host2) : $host2;
return "--host1 ".$host1." --user1 ".$user1." --password1 ".$password1." --host2 ".$host2." --user2 ".$user2." --password2 ".$password2." --no-modulesversion --noid";
}
?>