| @@ -1,329 +1,0 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -use AcyMailing\Helpers\UpdatemeHelper; | |
| 4 | - | |
| 5 | -function acym_escape($text, bool $addSlashes = true): string | |
| 6 | -{ | |
| 7 | - if (is_array($text) || is_object($text)) { | |
| 8 | - $text = json_encode($text); | |
| 9 | - | |
| 10 | - if ($addSlashes) { | |
| 11 | - $text = str_replace('\\', '\\\\', $text); | |
| 12 | - } | |
| 13 | - } | |
| 14 | - | |
| 15 | - if (empty($text) && !is_numeric($text)) { | |
| 16 | - return ''; | |
| 17 | - } | |
| 18 | - | |
| 19 | - if (!preg_match('#[&<>"\']#', $text)) { | |
| 20 | - return $text; | |
| 21 | - } | |
| 22 | - | |
| 23 | - return htmlspecialchars($text, ENT_QUOTES, 'UTF-8'); | |
| 24 | -} | |
| 25 | - | |
| 26 | -function acym_arrayToInteger(array &$array): void | |
| 27 | -{ | |
| 28 | - $array = @array_map('intval', $array); | |
| 29 | -} | |
| 30 | - | |
| 31 | -function acym_getIP(bool $full = false): string | |
| 32 | -{ | |
| 33 | - $config = acym_config(); | |
| 34 | - $ipCollection = $config->get('ip_collection', 'yes'); | |
| 35 | - | |
| 36 | - if (!$full && $ipCollection === 'no') { | |
| 37 | - return ''; | |
| 38 | - } | |
| 39 | - | |
| 40 | - $map = [ | |
| 41 | - 'HTTP_X_FORWARDED_IP', | |
| 42 | - 'X_FORWARDED_FOR', | |
| 43 | - 'HTTP_X_FORWARDED_FOR', | |
| 44 | - 'HTTP_CLIENT_IP', | |
| 45 | - 'HTTP_X_FORWARDED', | |
| 46 | - 'HTTP_FORWARDED_FOR', | |
| 47 | - 'HTTP_FORWARDED', | |
| 48 | - 'REMOTE_ADDR', | |
| 49 | - ]; | |
| 50 | - | |
| 51 | - $ipAddress = ''; | |
| 52 | - foreach ($map as $oneAttribute) { | |
| 53 | - if (empty($_SERVER[$oneAttribute]) || strlen($_SERVER[$oneAttribute]) < 7) continue; | |
| 54 | - | |
| 55 | - $ipAddress = $_SERVER[$oneAttribute]; | |
| 56 | - break; | |
| 57 | - } | |
| 58 | - | |
| 59 | - if (strstr($ipAddress, ',') !== false) { | |
| 60 | - $addresses = explode(',', $ipAddress); | |
| 61 | - $ipAddress = trim(end($addresses)); | |
| 62 | - } | |
| 63 | - | |
| 64 | - $ipAddress = strip_tags($ipAddress); | |
| 65 | - | |
| 66 | - if ($full || $ipCollection === 'yes') { | |
| 67 | - return $ipAddress; | |
| 68 | - } | |
| 69 | - | |
| 70 | - return acym_anonymizeIP($ipAddress); | |
| 71 | -} | |
| 72 | - | |
| 73 | -function acym_anonymizeIP(string $ipAddress): string | |
| 74 | -{ | |
| 75 | - if (empty($ipAddress)) { | |
| 76 | - return ''; | |
| 77 | - } | |
| 78 | - | |
| 79 | - if (strpos($ipAddress, ':') !== false) { | |
| 80 | - $packed = inet_pton($ipAddress); | |
| 81 | - if ($packed === false) { | |
| 82 | - return ''; | |
| 83 | - } | |
| 84 | - | |
| 85 | - $anonymized = substr($packed, 0, 8).str_repeat("\x00", 8); | |
| 86 | - | |
| 87 | - return inet_ntop($anonymized); | |
| 88 | - } | |
| 89 | - | |
| 90 | - $parts = explode('.', $ipAddress); | |
| 91 | - if (count($parts) !== 4) { | |
| 92 | - return ''; | |
| 93 | - } | |
| 94 | - | |
| 95 | - $parts[3] = '0'; | |
| 96 | - | |
| 97 | - return implode('.', $parts); | |
| 98 | -} | |
| 99 | - | |
| 100 | -function acym_generateKey(int $length): string | |
| 101 | -{ | |
| 102 | - $charactersPool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; | |
| 103 | - $randomString = ''; | |
| 104 | - $max = strlen($charactersPool) - 1; | |
| 105 | - for ($i = 0; $i < $length; $i++) { | |
| 106 | - $randomString .= $charactersPool[mt_rand(0, $max)]; | |
| 107 | - } | |
| 108 | - | |
| 109 | - return $randomString; | |
| 110 | -} | |
| 111 | - | |
| 112 | -function acym_isRobot(): bool | |
| 113 | -{ | |
| 114 | - if (empty($_SERVER)) { | |
| 115 | - return false; | |
| 116 | - } | |
| 117 | - | |
| 118 | - if (!empty($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'HEAD') { | |
| 119 | - return true; | |
| 120 | - } | |
| 121 | - | |
| 122 | - if (!empty($_SERVER['HTTP_USER_AGENT']) && strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'spambayes') !== false) { | |
| 123 | - return true; | |
| 124 | - } | |
| 125 | - if (!empty($_SERVER['REMOTE_ADDR']) && version_compare($_SERVER['REMOTE_ADDR'], '64.235.144.0', '>=') && version_compare($_SERVER['REMOTE_ADDR'], '64.235.159.255', '<=')) { | |
| 126 | - return true; | |
| 127 | - } | |
| 128 | - | |
| 129 | - return false; | |
| 130 | -} | |
| 131 | - | |
| 132 | -function acym_displayErrors(): void | |
| 133 | -{ | |
| 134 | - error_reporting(E_ALL); | |
| 135 | - @ini_set('display_errors', 1); | |
| 136 | -} | |
| 137 | - | |
| 138 | -function acym_checkRobots(): void | |
| 139 | -{ | |
| 140 | - if (preg_match('#(libwww-perl|python|googlebot)#i', @$_SERVER['HTTP_USER_AGENT'])) { | |
| 141 | - die('Not allowed for robots. Please contact us if you are not a robot'); | |
| 142 | - } | |
| 143 | -} | |
| 144 | - | |
| 145 | -function acym_noCache(): void | |
| 146 | -{ | |
| 147 | - acym_header('Cache-Control: no-store, no-cache, must-revalidate'); | |
| 148 | - acym_header('Cache-Control: post-check=0, pre-check=0', false); | |
| 149 | - acym_header('Pragma: no-cache'); | |
| 150 | - acym_header('Expires: Wed, 17 Sep 1975 21:32:10 GMT'); | |
| 151 | -} | |
| 152 | - | |
| 153 | -function acym_isAllowed(string $controller, string $task = ''): bool | |
| 154 | -{ | |
| 155 | - $controller = str_replace('front', '', $controller); | |
| 156 | - | |
| 157 | - $config = acym_config(); | |
| 158 | - $globalAccess = $config->get('acl_'.$controller, ACYM_ADMIN_GROUP); | |
| 159 | - if ($globalAccess === 'all') { | |
| 160 | - return true; | |
| 161 | - } | |
| 162 | - | |
| 163 | - $globalAccess = explode(',', $globalAccess); | |
| 164 | - $globalAccess[] = ACYM_ADMIN_GROUP; | |
| 165 | - | |
| 166 | - $userId = acym_currentUserId(); | |
| 167 | - if (empty($userId)) { | |
| 168 | - return false; | |
| 169 | - } | |
| 170 | - | |
| 171 | - $userGroups = acym_getGroupsByUser($userId); | |
| 172 | - if (empty($userGroups)) { | |
| 173 | - return false; | |
| 174 | - } | |
| 175 | - | |
| 176 | - foreach ($userGroups as $oneGroup) { | |
| 177 | - if (in_array($oneGroup, $globalAccess)) { | |
| 178 | - return true; | |
| 179 | - } | |
| 180 | - } | |
| 181 | - | |
| 182 | - if ($controller === 'mails' && in_array($task, ['autoSave', 'getTemplateAjax']) && acym_isAllowed('campaigns')) { | |
| 183 | - return true; | |
| 184 | - } | |
| 185 | - | |
| 186 | - if ($controller === 'language' && acym_isAllowed('configuration')) { | |
| 187 | - return true; | |
| 188 | - } | |
| 189 | - | |
| 190 | - if ($controller === 'file' && (acym_isAllowed('campaigns') || acym_isAllowed('mails'))) { | |
| 191 | - return true; | |
| 192 | - } | |
| 193 | - | |
| 194 | - if (in_array($task, ['countResultsTotal', 'countGlobalBySegmentId', 'countResults']) && acym_isAllowed('campaigns')) { | |
| 195 | - return true; | |
| 196 | - } | |
| 197 | - | |
| 198 | - if ($controller === 'lists' && $task === 'setAjaxListing' && (acym_isAllowed('campaigns') || acym_isAllowed('mails'))) { | |
| 199 | - return true; | |
| 200 | - } | |
| 201 | - | |
| 202 | - if ( | |
| 203 | - $controller === 'zones' | |
| 204 | - && ( | |
| 205 | - acym_isAllowed('campaigns') | |
| 206 | - || acym_isAllowed('mails') | |
| 207 | - ) | |
| 208 | - ) { | |
| 209 | - return true; | |
| 210 | - } | |
| 211 | - | |
| 212 | - if ( | |
| 213 | - $controller === 'toggle' | |
| 214 | - && ( | |
| 215 | - acym_isAllowed('campaigns') | |
| 216 | - || acym_isAllowed('mails') | |
| 217 | - || acym_isAllowed('automations') | |
| 218 | - || acym_isAllowed('segments') | |
| 219 | - || acym_isAllowed('scenarios') | |
| 220 | - || acym_isAllowed('forms') | |
| 221 | - || acym_isAllowed('users') | |
| 222 | - || acym_isAllowed('lists') | |
| 223 | - || acym_isAllowed('fields') | |
| 224 | - || acym_isAllowed('bounces') | |
| 225 | - ) | |
| 226 | - ) { | |
| 227 | - return true; | |
| 228 | - } | |
| 229 | - | |
| 230 | - if ( | |
| 231 | - $controller === 'dynamics' | |
| 232 | - && ( | |
| 233 | - acym_isAllowed('campaigns') | |
| 234 | - || acym_isAllowed('mails') | |
| 235 | - || acym_isAllowed('automations') | |
| 236 | - || acym_isAllowed('segments') | |
| 237 | - || acym_isAllowed('scenarios') | |
| 238 | - ) | |
| 239 | - ) { | |
| 240 | - return true; | |
| 241 | - } | |
| 242 | - | |
| 243 | - if ( | |
| 244 | - $task === 'loadEntityFront' | |
| 245 | - && ( | |
| 246 | - acym_isAllowed('campaigns') | |
| 247 | - || acym_isAllowed('mails') | |
| 248 | - || acym_isAllowed('users') | |
| 249 | - || acym_isAllowed('lists') | |
| 250 | - ) | |
| 251 | - ) { | |
| 252 | - return true; | |
| 253 | - } | |
| 254 | - | |
| 255 | - if ( | |
| 256 | - $task === 'getOption' | |
| 257 | - && ( | |
| 258 | - acym_isAllowed('campaigns') | |
| 259 | - || acym_isAllowed('mails') | |
| 260 | - || acym_isAllowed('plugins') | |
| 261 | - ) | |
| 262 | - ) { | |
| 263 | - return true; | |
| 264 | - } | |
| 265 | - | |
| 266 | - return false; | |
| 267 | -} | |
| 268 | - | |
| 269 | -function acym_raiseError(int $code, string $message): void | |
| 270 | -{ | |
| 271 | - echo '<link type="text/css" rel="stylesheet" href="'.ACYM_CSS.'back_global.min.css?v='.filemtime(ACYM_MEDIA.'css'.DS.'back_global.min.css').'">'; | |
| 272 | - echo '<div id="acym_wrapper">'; | |
| 273 | - acym_display('Error '.$code.': '.$message, 'error', false); | |
| 274 | - echo '</div>'; | |
| 275 | - http_response_code($code); | |
| 276 | - exit; | |
| 277 | -} | |
| 278 | - | |
| 279 | -function acym_isLicenseValidWeekly(): bool | |
| 280 | -{ | |
| 281 | - $config = acym_config(); | |
| 282 | - $expirationDate = $config->get('expirationdate', 0); | |
| 283 | - if (empty($expirationDate) || (time() - 604800) > $config->get('lastlicensecheck', 0)) { | |
| 284 | - UpdatemeHelper::getLicenseInfo(); | |
| 285 | - $config = acym_config(true); | |
| 286 | - $expirationDate = $config->get('expirationdate', 0); | |
| 287 | - } | |
| 288 | - | |
| 289 | - return $expirationDate >= time(); | |
| 290 | -} | |
| 291 | - | |
| 292 | -function acym_generateAutologinToken(int $subId, string $subKey): string | |
| 293 | -{ | |
| 294 | - $timestamp = time(); | |
| 295 | - $payload = $subId.'|'.$timestamp; | |
| 296 | - $secret = $subKey.acym_getSiteSalt(); | |
| 297 | - $signature = hash_hmac('sha256', $payload, $secret); | |
| 298 | - | |
| 299 | - return dechex($timestamp).'.'.$signature; | |
| 300 | -} | |
| 301 | - | |
| 302 | -function acym_verifyAutologinToken(int $subId, string $token, string $storedKey): bool | |
| 303 | -{ | |
| 304 | - $parts = explode('.', $token, 2); | |
| 305 | - if (count($parts) !== 2) { | |
| 306 | - return false; | |
| 307 | - } | |
| 308 | - | |
| 309 | - $timestamp = @hexdec($parts[0]); | |
| 310 | - $signature = $parts[1]; | |
| 311 | - | |
| 312 | - if (empty($timestamp) || $timestamp <= 0) { | |
| 313 | - return false; | |
| 314 | - } | |
| 315 | - | |
| 316 | - $config = acym_config(); | |
| 317 | - $maxAgeHours = intval($config->get('autologin_token_duration', 48)); | |
| 318 | - $maxAge = $maxAgeHours * 3600; | |
| 319 | - | |
| 320 | - if ((time() - $timestamp) > $maxAge) { | |
| 321 | - return false; | |
| 322 | - } | |
| 323 | - | |
| 324 | - $payload = $subId.'|'.$timestamp; | |
| 325 | - $secret = $storedKey.acym_getSiteSalt(); | |
| 326 | - $expectedSignature = hash_hmac('sha256', $payload, $secret); | |
| 327 | - | |
| 328 | - return hash_equals($expectedSignature, $signature); | |
| 329 | -} | |