blob: 2c2d29b74d327f8a5253727f3637bd821817f6f6 [file] [log] [blame]
matheusfillipeabd513e2021-05-11 03:29:11 -03001<?php
Marc Kupietzd871d882023-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 Kupietz0375b7e2023-03-04 18:13:26 +010060
61function replace_all_user_variables(string $string, User $user, string $url)
62{
Marc Kupietz3417c882023-03-07 12:03:21 +010063 foreach ($user as $key => $value) {
64 $to_replace = "{{" . $key . "}}";
65 $string = str_replace($to_replace, $value, $string);
66 }
67
Marc Kupietz0375b7e2023-03-04 18:13:26 +010068 $string = str_replace("{{url}}", $url, $string);
69 $string = str_replace("{{full_name}}", $user->first_name . " " . $user->last_name, $string);
70 $string = str_replace("{{fullname}}", $user->first_name . " " . $user->last_name, $string);
Marc Kupietz3417c882023-03-07 12:03:21 +010071 $string = str_replace("{{usertable}}", $user->to_table(), $string);
Marc Kupietz0375b7e2023-03-04 18:13:26 +010072 return $string;
Marc Kupietzd871d882023-03-05 18:34:16 +010073}
74
Marc Kupietz3417c882023-03-07 12:03:21 +010075function array_to_string(array $array)
Marc Kupietzd871d882023-03-05 18:34:16 +010076{
Marc Kupietz3417c882023-03-07 12:03:21 +010077 $string = "";
78 foreach ($array as $key => $value) {
79 $string .= $key . ": " . $value . "\n";
80 }
81 return $string;
82}