blob: 44160c0f2d909857aec88ccbb619ebd1f9cf88f3 [file] [log] [blame]
<?php
include_once 'User.php';
use \User as User;
function generateRandomString($length = 96)
{
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
function getClientIP(): string
{
$keys = array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR');
foreach ($keys as $k) {
if (!empty($_SERVER[$k]) && filter_var($_SERVER[$k], FILTER_VALIDATE_IP)) {
return $_SERVER[$k];
}
}
return false;
}
function format(string $string, array $values)
{
foreach ($values as $key => $value) {
$string = str_replace("{{{$key}}}", $value, $string);
}
return $string;
}
function template_path(string $lang_cc = null)
{
include "config.php";
if (isset($_SESSION["cc"])) $lang_cc = $GLOBALS["cc"];
if (isset($GLOBALS["cc"])) $lang_cc = $GLOBALS["cc"];
if (isset($LANG_CC) && !empty($LANG_CC) && $lang_cc == null) $lang_cc = $LANG_CC;
if ($lang_cc) $INCLUDE_STRINGS_PATH = "templates_" . $lang_cc;
else $INCLUDE_STRINGS_PATH = "templates";
if (isset($lang_cc) && !empty($lang_cc)) $TEMPLATE = $INCLUDE_STRINGS_PATH . "/";
else $TEMPLATE = "templates/";
include $TEMPLATE . 'strings.php';
if (!isset($RUNTIME_ERROR)) {
include_once 'templates/strings.php';
echo $RUNTIME_ERROR->not_found;
echo format($RUNTIME_ERROR->template_not_found, ["template" => $INCLUDE_STRINGS_PATH, "langcc" => $LANG_CC]);
die();
}
return $TEMPLATE;
}
function replace_all_user_variables(string $string, User $user, string $url)
{
$string = str_replace("{{url}}", $url, $string);
$string = str_replace("{{full_name}}", $user->first_name . " " . $user->last_name, $string);
$string = str_replace("{{fullname}}", $user->first_name . " " . $user->last_name, $string);
$string = str_replace("{{first_name}}", $user->first_name, $string);
$string = str_replace("{{last_name}}", $user->last_name, $string);
$string = str_replace("{{email}}", $user->email, $string);
$string = str_replace("{{organization}}", $user->organization, $string);
$string = str_replace("{{username}}", $user->user_name, $string);
return $string;
}
function user_to_string(User $user)
{
return $user->first_name . " " . $user->last_name . " <" . $user->email . "> " . $user->organization . " " . $user->user_name;
}