2016-04-01 21:44:31 +00:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* A set of utility helper functions
|
|
|
|
*/
|
|
|
|
|
|
|
|
use Sakura\Config;
|
2016-09-12 11:13:05 +00:00
|
|
|
use Sakura\Exceptions\ConfigValueNotFoundException;
|
2016-04-01 21:44:31 +00:00
|
|
|
use Sakura\Net;
|
2016-09-11 14:25:22 +00:00
|
|
|
use Sakura\Routerv1;
|
2016-07-31 01:32:37 +00:00
|
|
|
use Sakura\Template;
|
2016-04-01 21:44:31 +00:00
|
|
|
|
2016-07-29 19:31:36 +00:00
|
|
|
// Sort of alias for Config::get
|
2016-07-26 17:29:53 +00:00
|
|
|
function config($value)
|
|
|
|
{
|
|
|
|
$split = explode('.', $value);
|
|
|
|
$key = array_pop($split);
|
|
|
|
$section = implode('.', $split);
|
|
|
|
|
|
|
|
try {
|
|
|
|
return Config::get($section, $key);
|
2016-09-12 11:13:05 +00:00
|
|
|
} catch (ConfigValueNotFoundException $e) {
|
|
|
|
try {
|
|
|
|
return Config::get($value);
|
|
|
|
} catch (ConfigValueNotFoundException $e) {
|
|
|
|
return null;
|
|
|
|
}
|
2016-07-26 17:29:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-11 14:25:22 +00:00
|
|
|
// Alias for Routerv1::route
|
2016-08-10 16:10:57 +00:00
|
|
|
function route($name, $args = null, $full = false)
|
2016-07-29 19:31:36 +00:00
|
|
|
{
|
2016-09-11 14:25:22 +00:00
|
|
|
return ($full ? full_domain() : '') . Routerv1::route($name, $args);
|
2016-08-10 16:10:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Getting the full domain (+protocol) of the current host, only works for http
|
|
|
|
function full_domain()
|
|
|
|
{
|
|
|
|
return 'http' . ($_SERVER['HTTPS'] ?? false ? 's' : '') . '://' . $_SERVER['HTTP_HOST'];
|
2016-07-29 19:31:36 +00:00
|
|
|
}
|
|
|
|
|
2016-07-31 01:32:37 +00:00
|
|
|
// Checking if a parameter is equal to session_id()
|
|
|
|
function session_check($param = 'session')
|
|
|
|
{
|
|
|
|
return isset($_REQUEST[$param]) && $_REQUEST[$param] === session_id();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Alias for Template::vars and Template::render
|
|
|
|
function view($name, $vars = [])
|
|
|
|
{
|
|
|
|
Template::vars($vars);
|
|
|
|
return Template::render($name);
|
|
|
|
}
|
|
|
|
|
2016-04-01 21:44:31 +00:00
|
|
|
function clean_string($string, $lower = false, $noSpecial = false, $replaceSpecial = '')
|
|
|
|
{
|
|
|
|
// Run common sanitisation function over string
|
2016-07-26 17:29:53 +00:00
|
|
|
$string = htmlentities($string, ENT_NOQUOTES | ENT_HTML401, 'utf-8');
|
2016-04-01 21:44:31 +00:00
|
|
|
$string = stripslashes($string);
|
|
|
|
$string = strip_tags($string);
|
|
|
|
|
|
|
|
// If set also make the string lowercase
|
|
|
|
if ($lower) {
|
|
|
|
$string = strtolower($string);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If set remove all characters that aren't a-z or 0-9
|
|
|
|
if ($noSpecial) {
|
|
|
|
$string = preg_replace('/[^a-z0-9]/', $replaceSpecial, $string);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return clean string
|
|
|
|
return $string;
|
|
|
|
}
|
|
|
|
|
|
|
|
function check_mx_record($email)
|
|
|
|
{
|
|
|
|
// Get the domain from the e-mail address
|
|
|
|
$domain = substr(strstr($email, '@'), 1);
|
|
|
|
|
|
|
|
// Check the MX record
|
|
|
|
$record = checkdnsrr($domain, 'MX');
|
|
|
|
|
|
|
|
// Return the record data
|
|
|
|
return $record;
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_country_code()
|
|
|
|
{
|
|
|
|
// Attempt to get country code using PHP's built in geo thing
|
|
|
|
if (function_exists("geoip_country_code_by_name")) {
|
|
|
|
try {
|
|
|
|
$code = geoip_country_code_by_name(Net::ip());
|
|
|
|
|
|
|
|
// Check if $code is anything
|
|
|
|
if ($code) {
|
|
|
|
return $code;
|
|
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the required header is set and return it
|
2016-07-29 19:31:36 +00:00
|
|
|
if (isset($_SERVER['HTTP_CF_IPCOUNTRY']) && strlen($_SERVER['HTTP_CF_IPCOUNTRY']) === 2) {
|
2016-04-01 21:44:31 +00:00
|
|
|
return $_SERVER['HTTP_CF_IPCOUNTRY'];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return XX as a fallback
|
|
|
|
return 'XX';
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_country_name($code)
|
|
|
|
{
|
2016-07-26 17:29:53 +00:00
|
|
|
switch (strtolower($code)) {
|
|
|
|
case "xx":
|
|
|
|
return "Unknown";
|
2016-04-01 21:44:31 +00:00
|
|
|
|
2016-07-26 17:29:53 +00:00
|
|
|
case "a1":
|
|
|
|
return "Anonymous Proxy";
|
2016-04-01 21:44:31 +00:00
|
|
|
|
2016-07-26 17:29:53 +00:00
|
|
|
case "a2":
|
|
|
|
return "Satellite Provider";
|
2016-04-25 02:01:14 +00:00
|
|
|
|
2016-07-26 17:29:53 +00:00
|
|
|
default:
|
|
|
|
return locale_get_display_region("-{$code}", 'en');
|
|
|
|
}
|
2016-04-01 21:44:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function password_entropy($password)
|
|
|
|
{
|
|
|
|
$password = utf8_decode($password);
|
|
|
|
|
|
|
|
// Count the amount of unique characters in the password string and calculate the entropy
|
|
|
|
return count(count_chars($password, 1)) * log(256, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
function byte_symbol($bytes)
|
|
|
|
{
|
|
|
|
// Return nothing if the input was 0
|
|
|
|
if (!$bytes) {
|
|
|
|
return "0 B";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Array with byte symbols
|
|
|
|
$symbols = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
|
|
|
|
|
|
|
|
// Calculate byte entity
|
|
|
|
$exp = floor(log($bytes) / log(1024));
|
|
|
|
|
|
|
|
// Format the things
|
|
|
|
$bytes = sprintf("%.2f " . $symbols[$exp], ($bytes / pow(1024, floor($exp))));
|
|
|
|
|
|
|
|
// Return the formatted string
|
|
|
|
return $bytes;
|
|
|
|
}
|
|
|
|
|
2016-07-29 19:31:36 +00:00
|
|
|
// turn this function into a wrapped class!
|
2016-04-01 21:44:31 +00:00
|
|
|
function send_mail($to, $subject, $body)
|
|
|
|
{
|
2016-07-29 19:31:36 +00:00
|
|
|
$transport = Swift_SmtpTransport::newInstance()
|
|
|
|
->setHost(config('mail.smtp.server'))
|
|
|
|
->setPort(config('mail.smtp.port'));
|
2016-04-01 21:44:31 +00:00
|
|
|
|
2016-07-29 19:31:36 +00:00
|
|
|
if (config('mail.smtp.secure')) {
|
|
|
|
$transport->setEncryption(config('mail.smtp.secure'));
|
2016-04-01 21:44:31 +00:00
|
|
|
}
|
|
|
|
|
2016-07-29 19:31:36 +00:00
|
|
|
if (config('mail.smtp.auth')) {
|
|
|
|
$transport
|
|
|
|
->setUsername(config('mail.smtp.username'))
|
|
|
|
->setPassword(config('mail.smtp.password'));
|
2016-04-01 21:44:31 +00:00
|
|
|
}
|
|
|
|
|
2016-07-29 19:31:36 +00:00
|
|
|
$mailer = Swift_Mailer::newInstance($transport);
|
2016-04-01 21:44:31 +00:00
|
|
|
|
2016-08-02 13:17:16 +00:00
|
|
|
$message = Swift_Message::newInstance($subject)
|
2016-07-29 19:31:36 +00:00
|
|
|
->setFrom([config('mail.smtp.from') => config('mail.smtp.name')])
|
|
|
|
->setBcc($to)
|
|
|
|
->setBody($body);
|
2016-04-01 21:44:31 +00:00
|
|
|
|
2016-07-29 19:31:36 +00:00
|
|
|
return $mailer->send($message);
|
2016-04-01 21:44:31 +00:00
|
|
|
}
|