Changeset View
Changeset View
Standalone View
Standalone View
src/app/Utils.php
Show All 35 Lines | class Utils | ||||
/** | /** | ||||
* Return the country ISO code for an IP address. | * Return the country ISO code for an IP address. | ||||
* | * | ||||
* @return string | * @return string | ||||
*/ | */ | ||||
public static function countryForIP($ip) | public static function countryForIP($ip) | ||||
{ | { | ||||
if (strpos(':', $ip) === false) { | if (strpos($ip, ':') === false) { | ||||
$query = " | $query = " | ||||
SELECT country FROM ip4nets | SELECT country FROM ip4nets | ||||
WHERE INET_ATON(net_number) <= INET_ATON(?) | WHERE INET_ATON(net_number) <= INET_ATON(?) | ||||
AND INET_ATON(net_broadcast) >= INET_ATON(?) | AND INET_ATON(net_broadcast) >= INET_ATON(?) | ||||
ORDER BY INET_ATON(net_number), net_mask DESC LIMIT 1 | ORDER BY INET_ATON(net_number), net_mask DESC LIMIT 1 | ||||
"; | "; | ||||
} else { | } else { | ||||
$query = " | $query = " | ||||
▲ Show 20 Lines • Show All 98 Lines • ▼ Show 20 Lines | public static function downloadFile($source, $target, $force = false) | ||||
return; | return; | ||||
} | } | ||||
curl_close($curl); | curl_close($curl); | ||||
fclose($fp); | fclose($fp); | ||||
} | } | ||||
/** | /** | ||||
* Find an object that is the recipient for the specified address. | |||||
* | |||||
* @param string $address | |||||
* | |||||
* @return array | |||||
*/ | |||||
public static function findObjectsByRecipientAddress($address) | |||||
{ | |||||
$address = \App\Utils::normalizeAddress($address); | |||||
list($local, $domainName) = explode('@', $address); | |||||
$domain = \App\Domain::where('namespace', $domainName)->first(); | |||||
if (!$domain) { | |||||
return []; | |||||
} | |||||
$user = \App\User::where('email', $address)->first(); | |||||
if ($user) { | |||||
return [$user]; | |||||
} | |||||
$userAliases = \App\UserAlias::where('alias', $address)->get(); | |||||
if (count($userAliases) > 0) { | |||||
$users = []; | |||||
foreach ($userAliases as $userAlias) { | |||||
$users[] = $userAlias->user; | |||||
} | |||||
return $users; | |||||
} | |||||
$userAliases = \App\UserAlias::where('alias', "catchall@{$domain->namespace}")->get(); | |||||
if (count($userAliases) > 0) { | |||||
$users = []; | |||||
foreach ($userAliases as $userAlias) { | |||||
$users[] = $userAlias->user; | |||||
} | |||||
return $users; | |||||
} | |||||
return []; | |||||
} | |||||
/** | |||||
* Generate a passphrase. Not intended for use in production, so limited to environments that are not production. | |||||
* | |||||
* @return string | |||||
*/ | |||||
public static function generatePassphrase() | |||||
{ | |||||
$alphaLow = 'abcdefghijklmnopqrstuvwxyz'; | |||||
$alphaUp = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; | |||||
$num = '0123456789'; | |||||
$stdSpecial = '~`!@#$%^&*()-_+=[{]}\\|\'";:/?.>,<'; | |||||
$source = $alphaLow . $alphaUp . $num . $stdSpecial; | |||||
$result = ''; | |||||
for ($x = 0; $x < 16; $x++) { | |||||
$result .= substr($source, rand(0, (strlen($source) - 1)), 1); | |||||
} | |||||
return $result; | |||||
} | |||||
/** | |||||
* Retrieve the network ID and Type from a client address | |||||
* | |||||
* @param string $clientAddress The IPv4 or IPv6 address. | |||||
* | |||||
* @return array An array of ID and class or null and null. | |||||
*/ | |||||
public static function getNetFromAddress($clientAddress) | |||||
{ | |||||
if (strpos($clientAddress, ':') === false) { | |||||
$net = \App\IP4Net::getNet($clientAddress); | |||||
if ($net) { | |||||
return [$net->id, \App\IP4Net::class]; | |||||
} | |||||
} else { | |||||
$net = \App\IP6Net::getNet($clientAddress); | |||||
if ($net) { | |||||
return [$net->id, \App\IP6Net::class]; | |||||
} | |||||
} | |||||
return [null, null]; | |||||
} | |||||
/** | |||||
* Calculate the broadcast address provided a net number and a prefix. | * Calculate the broadcast address provided a net number and a prefix. | ||||
* | * | ||||
* @param string $net A valid IPv6 network number. | * @param string $net A valid IPv6 network number. | ||||
* @param int $prefix The network prefix. | * @param int $prefix The network prefix. | ||||
* | * | ||||
* @return string | * @return string | ||||
*/ | */ | ||||
public static function ip6Broadcast($net, $prefix) | public static function ip6Broadcast($net, $prefix) | ||||
machniak: Utils is not that big yet, but maybe we should have NetUtils. | |||||
{ | { | ||||
$netHex = bin2hex(inet_pton($net)); | $netHex = bin2hex(inet_pton($net)); | ||||
// Overwriting first address string to make sure notation is optimal | // Overwriting first address string to make sure notation is optimal | ||||
$net = inet_ntop(hex2bin($netHex)); | $net = inet_ntop(hex2bin($netHex)); | ||||
// Calculate the number of 'flexible' bits | // Calculate the number of 'flexible' bits | ||||
$flexbits = 128 - $prefix; | $flexbits = 128 - $prefix; | ||||
Show All 31 Lines | public static function ip6Broadcast($net, $prefix) | ||||
// And create an IPv6 address from the binary string | // And create an IPv6 address from the binary string | ||||
$lastaddrstr = inet_ntop($lastaddrbin); | $lastaddrstr = inet_ntop($lastaddrbin); | ||||
return $lastaddrstr; | return $lastaddrstr; | ||||
} | } | ||||
/** | /** | ||||
* Normalize an email address. | |||||
* | |||||
* This means to lowercase and strip components separated with recipient delimiters. | |||||
* | |||||
* @param string $address The address to normalize. | |||||
* | |||||
* @return string | |||||
*/ | |||||
public static function normalizeAddress($address) | |||||
{ | |||||
$address = strtolower($address); | |||||
list($local, $domain) = explode('@', $address); | |||||
if (strpos($local, '+') === false) { | |||||
return "{$local}@{$domain}"; | |||||
} | |||||
$localComponents = explode('+', $local); | |||||
$local = array_pop($localComponents); | |||||
return "{$local}@{$domain}"; | |||||
} | |||||
/** | |||||
* Provide all unique combinations of elements in $input, with order and duplicates irrelevant. | * Provide all unique combinations of elements in $input, with order and duplicates irrelevant. | ||||
* | * | ||||
* @param array $input The input array of elements. | * @param array $input The input array of elements. | ||||
* | * | ||||
* @return array[] | * @return array[] | ||||
*/ | */ | ||||
public static function powerSet(array $input): array | public static function powerSet(array $input): array | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 117 Lines • Show Last 20 Lines |
Utils is not that big yet, but maybe we should have NetUtils.