| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 1 | <?php | 
| Marc Kupietz | d871d88 | 2023-03-05 18:34:16 +0100 | [diff] [blame] | 2 |  | 
 | 3 | include_once 'User.php'; | 
 | 4 | use \User as User; | 
 | 5 |  | 
| matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 6 | function generateRandomString($length = 96) | 
 | 7 | { | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 8 |     $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 |  | 
| matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 17 | function getClientIP(): string | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 18 | { | 
| matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 19 |     $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)) { | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 22 |             return $_SERVER[$k]; | 
 | 23 |         } | 
 | 24 |     } | 
 | 25 |     return false; | 
 | 26 | } | 
| matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 27 |  | 
 | 28 | function format(string $string, array $values) | 
 | 29 | { | 
 | 30 |     foreach ($values as $key => $value) { | 
 | 31 |         $string = str_replace("{{{$key}}}", $value, $string); | 
 | 32 |     } | 
 | 33 |     return $string; | 
 | 34 | } | 
| matheusfillipe | c0ce7fa | 2021-05-13 05:15:37 -0300 | [diff] [blame] | 35 |  | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 36 | function template_path(string $lang_cc = null) | 
 | 37 | { | 
| matheusfillipe | c0ce7fa | 2021-05-13 05:15:37 -0300 | [diff] [blame] | 38 |     include "config.php"; | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 39 |     if (isset($_SESSION["cc"])) $lang_cc = $GLOBALS["cc"]; | 
 | 40 |     if (isset($GLOBALS["cc"]))  $lang_cc = $GLOBALS["cc"]; | 
| Matheus Fillipe | 3c3bf9e | 2021-05-14 09:58:52 +0300 | [diff] [blame] | 41 |     if (isset($LANG_CC) && !empty($LANG_CC) && $lang_cc == null) $lang_cc = $LANG_CC; | 
| matheusfillipe | c0ce7fa | 2021-05-13 05:15:37 -0300 | [diff] [blame] | 42 |  | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 43 |     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 . "/"; | 
| matheusfillipe | c0ce7fa | 2021-05-13 05:15:37 -0300 | [diff] [blame] | 47 |     else $TEMPLATE = "templates/"; | 
 | 48 |  | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 49 |     include $TEMPLATE . 'strings.php'; | 
| matheusfillipe | c0ce7fa | 2021-05-13 05:15:37 -0300 | [diff] [blame] | 50 |  | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 51 |     if (!isset($RUNTIME_ERROR)) { | 
| matheusfillipe | c0ce7fa | 2021-05-13 05:15:37 -0300 | [diff] [blame] | 52 |         include_once 'templates/strings.php'; | 
 | 53 |         echo $RUNTIME_ERROR->not_found; | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 54 |         echo format($RUNTIME_ERROR->template_not_found, ["template" => $INCLUDE_STRINGS_PATH, "langcc" => $LANG_CC]); | 
| matheusfillipe | c0ce7fa | 2021-05-13 05:15:37 -0300 | [diff] [blame] | 55 |         die(); | 
 | 56 |     } | 
 | 57 |  | 
 | 58 |     return $TEMPLATE; | 
 | 59 | } | 
| Marc Kupietz | 0375b7e | 2023-03-04 18:13:26 +0100 | [diff] [blame] | 60 |  | 
 | 61 | function replace_all_user_variables(string $string, User $user, string $url) | 
 | 62 | { | 
| Marc Kupietz | 3417c88 | 2023-03-07 12:03:21 +0100 | [diff] [blame] | 63 |     foreach ($user as $key => $value) { | 
 | 64 |         $to_replace = "{{" . $key . "}}"; | 
 | 65 |         $string = str_replace($to_replace, $value, $string); | 
 | 66 |     } | 
 | 67 |  | 
| Marc Kupietz | 0375b7e | 2023-03-04 18:13:26 +0100 | [diff] [blame] | 68 |     $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 Kupietz | 3417c88 | 2023-03-07 12:03:21 +0100 | [diff] [blame] | 71 |     $string = str_replace("{{usertable}}", $user->to_table(), $string); | 
| Marc Kupietz | 0375b7e | 2023-03-04 18:13:26 +0100 | [diff] [blame] | 72 |     return $string; | 
| Marc Kupietz | d871d88 | 2023-03-05 18:34:16 +0100 | [diff] [blame] | 73 | } | 
 | 74 |  | 
| Marc Kupietz | 3417c88 | 2023-03-07 12:03:21 +0100 | [diff] [blame] | 75 | function array_to_string(array $array) | 
| Marc Kupietz | d871d88 | 2023-03-05 18:34:16 +0100 | [diff] [blame] | 76 | { | 
| Marc Kupietz | 3417c88 | 2023-03-07 12:03:21 +0100 | [diff] [blame] | 77 |     $string = ""; | 
 | 78 |     foreach ($array as $key => $value) { | 
 | 79 |         $string .= $key . ": " . $value . "\n"; | 
 | 80 |     } | 
 | 81 |     return $string; | 
 | 82 | } |