blob: 44160c0f2d909857aec88ccbb619ebd1f9cf88f3 [file] [log] [blame]
matheusfillipeabd513e2021-05-11 03:29:11 -03001<?php
Marc Kupietz0215a442023-03-05 18:34:16 +01002
3include_once 'User.php';
4use \User as User;
5
matheusfillipe47cf90b2021-05-13 03:36:21 -03006function generateRandomString($length = 96)
7{
matheusfillipeabd513e2021-05-11 03:29:11 -03008 $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
9 $charactersLength = strlen($characters);
10 $randomString = '';
11 for ($i = 0; $i < $length; $i++) {
12 $randomString .= $characters[rand(0, $charactersLength - 1)];
13 }
14 return $randomString;
15}
16
matheusfillipe47cf90b2021-05-13 03:36:21 -030017function getClientIP(): string
matheusfillipeabd513e2021-05-11 03:29:11 -030018{
matheusfillipe47cf90b2021-05-13 03:36:21 -030019 $keys = array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR');
20 foreach ($keys as $k) {
21 if (!empty($_SERVER[$k]) && filter_var($_SERVER[$k], FILTER_VALIDATE_IP)) {
matheusfillipeabd513e2021-05-11 03:29:11 -030022 return $_SERVER[$k];
23 }
24 }
25 return false;
26}
matheusfillipe47cf90b2021-05-13 03:36:21 -030027
28function format(string $string, array $values)
29{
30 foreach ($values as $key => $value) {
31 $string = str_replace("{{{$key}}}", $value, $string);
32 }
33 return $string;
34}
matheusfillipec0ce7fa2021-05-13 05:15:37 -030035
matheusfillipef43dd962021-05-13 23:27:01 -030036function template_path(string $lang_cc = null)
37{
matheusfillipec0ce7fa2021-05-13 05:15:37 -030038 include "config.php";
matheusfillipef43dd962021-05-13 23:27:01 -030039 if (isset($_SESSION["cc"])) $lang_cc = $GLOBALS["cc"];
40 if (isset($GLOBALS["cc"])) $lang_cc = $GLOBALS["cc"];
Matheus Fillipe3c3bf9e2021-05-14 09:58:52 +030041 if (isset($LANG_CC) && !empty($LANG_CC) && $lang_cc == null) $lang_cc = $LANG_CC;
matheusfillipec0ce7fa2021-05-13 05:15:37 -030042
matheusfillipef43dd962021-05-13 23:27:01 -030043 if ($lang_cc) $INCLUDE_STRINGS_PATH = "templates_" . $lang_cc;
44 else $INCLUDE_STRINGS_PATH = "templates";
45
46 if (isset($lang_cc) && !empty($lang_cc)) $TEMPLATE = $INCLUDE_STRINGS_PATH . "/";
matheusfillipec0ce7fa2021-05-13 05:15:37 -030047 else $TEMPLATE = "templates/";
48
matheusfillipef43dd962021-05-13 23:27:01 -030049 include $TEMPLATE . 'strings.php';
matheusfillipec0ce7fa2021-05-13 05:15:37 -030050
matheusfillipef43dd962021-05-13 23:27:01 -030051 if (!isset($RUNTIME_ERROR)) {
matheusfillipec0ce7fa2021-05-13 05:15:37 -030052 include_once 'templates/strings.php';
53 echo $RUNTIME_ERROR->not_found;
matheusfillipef43dd962021-05-13 23:27:01 -030054 echo format($RUNTIME_ERROR->template_not_found, ["template" => $INCLUDE_STRINGS_PATH, "langcc" => $LANG_CC]);
matheusfillipec0ce7fa2021-05-13 05:15:37 -030055 die();
56 }
57
58 return $TEMPLATE;
59}
Marc Kupietza3f3fdb2023-03-04 18:13:26 +010060
61function replace_all_user_variables(string $string, User $user, string $url)
62{
63 $string = str_replace("{{url}}", $url, $string);
64 $string = str_replace("{{full_name}}", $user->first_name . " " . $user->last_name, $string);
65 $string = str_replace("{{fullname}}", $user->first_name . " " . $user->last_name, $string);
66 $string = str_replace("{{first_name}}", $user->first_name, $string);
67 $string = str_replace("{{last_name}}", $user->last_name, $string);
68 $string = str_replace("{{email}}", $user->email, $string);
69 $string = str_replace("{{organization}}", $user->organization, $string);
70 $string = str_replace("{{username}}", $user->user_name, $string);
71 return $string;
Marc Kupietz0215a442023-03-05 18:34:16 +010072}
73
74function user_to_string(User $user)
75{
76 return $user->first_name . " " . $user->last_name . " <" . $user->email . "> " . $user->organization . " " . $user->user_name;
Marc Kupietza3f3fdb2023-03-04 18:13:26 +010077}