| 1 |
<?php |
| 2 |
|
| 3 |
function acym_escape($text, $addSlashes = true) |
| 4 |
{ |
| 5 |
if (is_array($text) || is_object($text)) { |
| 6 |
if ($addSlashes) { |
| 7 |
$text = str_replace('\\', '\\\\', json_encode($text)); |
| 8 |
} else { |
| 9 |
$text = json_encode($text); |
| 10 |
} |
| 11 |
} |
| 12 |
|
| 13 |
if (empty($text)) { |
| 14 |
return $text; |
| 15 |
} |
| 16 |
|
| 17 |
return htmlspecialchars($text, ENT_QUOTES, 'UTF-8'); |
| 18 |
} |
| 19 |
|
| 20 |
function acym_arrayToInteger(&$array) |
| 21 |
{ |
| 22 |
if (is_array($array)) { |
| 23 |
$array = @array_map('intval', $array); |
| 24 |
} else { |
| 25 |
$array = []; |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
function acym_getIP() |
| 30 |
{ |
| 31 |
$map = [ |
| 32 |
'HTTP_X_FORWARDED_IP', |
| 33 |
'X_FORWARDED_FOR', |
| 34 |
'HTTP_X_FORWARDED_FOR', |
| 35 |
'HTTP_CLIENT_IP', |
| 36 |
'HTTP_X_FORWARDED', |
| 37 |
'HTTP_FORWARDED_FOR', |
| 38 |
'HTTP_FORWARDED', |
| 39 |
'REMOTE_ADDR', |
| 40 |
]; |
| 41 |
|
| 42 |
$ipAddress = ''; |
| 43 |
foreach ($map as $oneAttribute) { |
| 44 |
if (empty($_SERVER[$oneAttribute]) || strlen($_SERVER[$oneAttribute]) < 7) continue; |
| 45 |
|
| 46 |
$ipAddress = $_SERVER[$oneAttribute]; |
| 47 |
break; |
| 48 |
} |
| 49 |
|
| 50 |
if (strstr($ipAddress, ',') !== false) { |
| 51 |
$addresses = explode(',', $ipAddress); |
| 52 |
$ipAddress = trim(end($addresses)); |
| 53 |
} |
| 54 |
|
| 55 |
return strip_tags($ipAddress); |
| 56 |
} |
| 57 |
|
| 58 |
function acym_generateKey($length) |
| 59 |
{ |
| 60 |
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
| 61 |
$randstring = ''; |
| 62 |
$max = strlen($characters) - 1; |
| 63 |
for ($i = 0 ; $i < $length ; $i++) { |
| 64 |
$randstring .= $characters[mt_rand(0, $max)]; |
| 65 |
} |
| 66 |
|
| 67 |
return $randstring; |
| 68 |
} |
| 69 |
|
| 70 |
function acym_isRobot(): bool |
| 71 |
{ |
| 72 |
if (empty($_SERVER)) { |
| 73 |
return false; |
| 74 |
} |
| 75 |
if (!empty($_SERVER['HTTP_USER_AGENT']) && strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'spambayes') !== false) { |
| 76 |
return true; |
| 77 |
} |
| 78 |
if (!empty($_SERVER['REMOTE_ADDR']) && version_compare($_SERVER['REMOTE_ADDR'], '64.235.144.0', '>=') && version_compare($_SERVER['REMOTE_ADDR'], '64.235.159.255', '<=')) { |
| 79 |
return true; |
| 80 |
} |
| 81 |
|
| 82 |
return false; |
| 83 |
} |
| 84 |
|
| 85 |
function acym_displayErrors() |
| 86 |
{ |
| 87 |
error_reporting(E_ALL); |
| 88 |
@ini_set('display_errors', 1); |
| 89 |
} |
| 90 |
|
| 91 |
function acym_checkRobots() |
| 92 |
{ |
| 93 |
if (preg_match('#(libwww-perl|python|googlebot)#i', @$_SERVER['HTTP_USER_AGENT'])) { |
| 94 |
die('Not allowed for robots. Please contact us if you are not a robot'); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
function acym_noCache() |
| 99 |
{ |
| 100 |
acym_header('Cache-Control: no-store, no-cache, must-revalidate'); |
| 101 |
acym_header('Cache-Control: post-check=0, pre-check=0', false); |
| 102 |
acym_header('Pragma: no-cache'); |
| 103 |
acym_header('Expires: Wed, 17 Sep 1975 21:32:10 GMT'); |
| 104 |
} |
| 105 |
|
| 106 |
function acym_isAllowed($controller, $task = ''): bool |
| 107 |
{ |
| 108 |
$config = acym_config(); |
| 109 |
$globalAccess = $config->get('acl_'.$controller, 'all'); |
| 110 |
if ($globalAccess === 'all') { |
| 111 |
return true; |
| 112 |
} |
| 113 |
|
| 114 |
$globalAccess = explode(',', $globalAccess); |
| 115 |
$globalAccess[] = ACYM_ADMIN_GROUP; |
| 116 |
|
| 117 |
$userId = acym_currentUserId(); |
| 118 |
if (empty($userId)) { |
| 119 |
return false; |
| 120 |
} |
| 121 |
|
| 122 |
$userGroups = acym_getGroupsByUser($userId); |
| 123 |
if (empty($userGroups)) { |
| 124 |
return false; |
| 125 |
} |
| 126 |
|
| 127 |
foreach ($userGroups as $oneGroup) { |
| 128 |
if (in_array($oneGroup, $globalAccess)) { |
| 129 |
return true; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
return false; |
| 134 |
} |
| 135 |
|
| 136 |
function acym_raiseError($code, $message) |
| 137 |
{ |
| 138 |
echo '<link type="text/css" rel="stylesheet" href="'.ACYM_CSS.'back_global.min.css?v='.filemtime(ACYM_MEDIA.'css'.DS.'back_global.min.css').'">'; |
| 139 |
echo '<div id="acym_wrapper">'; |
| 140 |
acym_display('Error '.$code.': '.$message, 'error', false); |
| 141 |
echo '</div>'; |
| 142 |
http_response_code($code); |
| 143 |
exit; |
| 144 |
} |
| 145 |
|
| 146 |
function acym_isLicenseValidWeekly(): bool |
| 147 |
{ |
| 148 |
$config = acym_config(); |
| 149 |
$expirationDate = $config->get('expirationdate', 0); |
| 150 |
if (empty($expirationDate) || (time() - 604800) > $config->get('lastlicensecheck', 0)) { |
| 151 |
acym_checkVersion(); |
| 152 |
$config = acym_config(true); |
| 153 |
$expirationDate = $config->get('expirationdate', 0); |
| 154 |
} |
| 155 |
|
| 156 |
return $expirationDate >= time(); |
| 157 |
} |
| 158 |
|