1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407: 408: 409: 410: 411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421: 422: 423: 424: 425: 426: 427: 428: 429: 430: 431: 432: 433: 434: 435: 436: 437: 438: 439: 440: 441: 442: 443: 444: 445: 446: 447: 448: 449: 450: 451: 452: 453: 454: 455: 456: 457: 458: 459: 460:
<?php
namespace MvcCore\Request;
trait InternalInits {
public static function ParseHttpAcceptLang ($languagesList) {
$languages = [];
$languageRanges = explode(',', trim($languagesList));
foreach ($languageRanges as $languageRange) {
$regExpResult = preg_match(
"/(\*|[a-zA-Z0-9]{1,8}(?:-[a-zA-Z0-9]{1,8})*)(?:\s*;\s*q\s*=\s*(0(?:\.\d{0,3})|1(?:\.0{0,3})))?/",
trim($languageRange),
$match
);
if ($regExpResult) {
$priority = isset($match[2])
? (string) floatval($match[2])
: '1.0';
if (!isset($languages[$priority])) $languages[$priority] = [];
$langOrLangWithLocale = str_replace('-', '_', $match[1]);
$delimiterPos = strpos($langOrLangWithLocale, '_');
if ($delimiterPos !== FALSE) {
$languages[$priority][] = [
strtolower(substr($langOrLangWithLocale, 0, $delimiterPos)),
strtoupper(substr($langOrLangWithLocale, $delimiterPos + 1))
];
} else {
$languages[$priority][] = [
strtolower($langOrLangWithLocale),
NULL
];
}
}
}
krsort($languages);
reset($languages);
return $languages;
}
protected function initCli () {
$hostName = gethostname();
$this->scheme = 'file:';
$this->secure = FALSE;
$this->hostName = $hostName;
$this->host = $hostName;
$this->port = '';
$this->path = '';
$this->query = '';
$this->fragment = '';
$this->ajax = FALSE;
$this->basePath = '';
$this->requestPath = '';
$this->domainUrl = '';
$this->baseUrl = '';
$this->requestUrl = '';
$this->fullUrl = '';
$this->referer = '';
$this->serverIp = '127.0.0.1';
$this->clientIp = $this->serverIp;
$this->contentLength = 0;
$this->headers = [];
$this->params = [];
$this->appRequest = FALSE;
$this->method = 'GET';
$backtraceItems = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
$indexFilePath = str_replace('\\', '/', $backtraceItems[count($backtraceItems) - 1]['file']);
$lastSlashPos = mb_strrpos($indexFilePath, '/');
$this->appRoot = mb_substr($indexFilePath, 0, $lastSlashPos);
$this->scriptName = mb_substr($indexFilePath, $lastSlashPos);
$args = $this->globalServer['argv'];
array_shift($args);
$params = [];
if ($args) {
foreach ($args as $arg) {
parse_str($arg, $paramsLocal);
if (!$paramsLocal) continue;
foreach ($paramsLocal as $paramName => $paramValue) {
if (is_array($paramValue)) {
$params = array_merge(
$params,
[$paramName => array_merge(
$params[$paramName] ?: [], $paramValue
)]
);
} else {
$params[$paramName] = $paramValue;
}
}
}
}
$this->params = $params;
$this->globalGet = $params;
}
protected function initUrlSegments () {
$this->portDefined = FALSE;
$this->port = '';
$this->path = '';
$this->query = '';
$this->fragment = '';
$uri = $this->GetScheme() . '//'
. $this->globalServer['HTTP_HOST'];
if (isset($this->globalServer['UNENCODED_URL'])) {
$uri .= rawurldecode($this->globalServer['UNENCODED_URL']);
} else if (isset($this->globalServer['UNENCODED_URL'])) {
$uri .= rawurldecode($this->globalServer['UNENCODED_URL']);
} else {
$uri .= rawurldecode($this->globalServer['REQUEST_URI']);
}
$firstColonPos = mb_strpos($uri, ':');
if ($firstColonPos !== FALSE)
$uri = mb_substr($uri, $firstColonPos + 1);
if (mb_substr($uri, 0, 2) === '//') {
$nextSlashPos = mb_strpos($uri, '/', 2);
if ($nextSlashPos !== FALSE) {
$authority = mb_substr($uri, 2, $nextSlashPos - 2);
$uri = mb_substr($uri, $nextSlashPos);
$colonsCount = mb_substr_count($authority, ':');
if ($colonsCount === 1) {
$colonPos = mb_strpos($authority, ':');
$this->port = mb_substr($authority, $colonPos + 1);
if ($this->port !== '')
$this->portDefined = TRUE;
}
} else {
return;
}
}
$basePath = $this->GetBasePath();
$uri = mb_substr($uri, mb_strlen($basePath));
$questionMarkPos = mb_strpos($uri, '?');
$hashPos = mb_strpos($uri, '#');
$questionMarkContained = $questionMarkPos !== FALSE;
$hashContained = $hashPos !== FALSE;
if (!$questionMarkContained && !$hashContained) {
$this->path = $uri;
} else if ($questionMarkContained && !$hashContained) {
$this->path = mb_substr($uri, 0, $questionMarkPos);
$this->query = trim(mb_substr($uri, $questionMarkPos + 1), '&');
} else if (!$questionMarkContained && $hashContained) {
$this->path = mb_substr($uri, 0, $hashPos);
$this->fragment = mb_substr($uri, $hashPos + 1);
} else if ($questionMarkContained && $hashContained && $questionMarkPos < $hashPos) {
$this->path = mb_substr($uri, 0, $questionMarkPos);
$this->query = trim(mb_substr($uri, $questionMarkPos + 1, $questionMarkPos + 1 - $hashPos), '&');
$this->fragment = mb_substr($uri, $hashPos + 1);
} else {
$this->path = mb_substr($uri, 0, $questionMarkPos);
$this->fragment = mb_substr($uri, $hashPos + 1);
}
}
protected function initHeaders () {
if (function_exists('getallheaders')) {
$headers = getallheaders();
} else {
$headers = [];
foreach ($this->globalServer as $name => $value) {
if (substr($name, 0, 5) == 'HTTP_') {
$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
} else if ($name == "CONTENT_TYPE") {
$headers["Content-Type"] = $value;
} else if ($name == "CONTENT_LENGTH") {
$headers["Content-Length"] = $value;
}
}
}
$this->headers = $headers;
}
protected function initParams () {
$params = array_merge($this->globalGet);
$method = $this->GetMethod();
if ($method == self::METHOD_POST || $method == self::METHOD_PUT) {
$postValues = [];
$contentValues = [];
$postHasBeenSerialized = FALSE;
if (count($this->globalPost) > 0) {
$postValues = $this->globalPost;
$postHasBeenSerialized = TRUE;
}
$contentType = $this->GetHeader('Content-Type', ' \-/;_=a-zA-Z0-9', '');
$multiPartHeader = 'multipart/form-data';
$multiPartContent = mb_strpos($contentType, $multiPartHeader) !== FALSE;
if (!$postHasBeenSerialized && !$multiPartContent) {
if ($this->body === NULL)
$this->initBody();
$contentValues = $this->parseBodyParams($contentType);
}
$params = array_merge($params, $postValues, $contentValues);
}
$this->params = $params;
}
protected function initBody () {
$this->body = file_get_contents($this->inputStream);
}
protected function parseBodyParams ($contentType) {
$result = [];
$app = self::$app ?: (self::$app = \MvcCore\Application::GetInstance());
$toolClass = $app->GetToolClass();
$urlEncType = mb_strpos($contentType, 'application/x-www-form-urlencoded') !== FALSE;
if ($urlEncType) {
parse_str(trim($this->body, '&='), $result);
} else {
$jsonType = (
mb_strpos($contentType, 'application/json') !== FALSE ||
mb_strpos($contentType, 'text/javascript') !== FALSE ||
mb_strpos($contentType, 'application/ld+json') !== FALSE
);
if ($jsonType) {
try {
$result = $toolClass::DecodeJson($this->body);
} catch (\Exception $e) {
} catch (\Throwable $e) {
}
} else {
$probablyAJsonType = !$toolClass::IsQueryString($this->body);
if ($probablyAJsonType) {
try {
$result = $toolClass::DecodeJson($this->body);
} catch (\Exception $e) {
$probablyAJsonType = FALSE;
} catch (\Throwable $e) {
$probablyAJsonType = FALSE;
}
}
if (!$probablyAJsonType)
parse_str(trim($this->body, '&='), $result);
}
}
return $result;
}
protected function getParamFromCollection (
& $paramsCollection = [],
$name = "",
$pregReplaceAllowedChars = "a-zA-Z0-9_;, /\-\@\:",
$ifNullValue = NULL,
$targetType = NULL
) {
if (!isset($paramsCollection[$name])) return $ifNullValue;
if (is_array($paramsCollection[$name])) {
if ($targetType !== NULL) {
$targetTypeBracketsPos = strpos($targetType, '[]');
$targetTypeEndsWithBrackets = $targetTypeBracketsPos !== strlen($targetType) - 3;
$targetTypeIsArray = $targetType == 'array';
if (!$targetTypeEndsWithBrackets && !$targetTypeIsArray)
throw new \InvalidArgumentException(
"Collection member `{$name}` is not an `array`."
);
if ($targetTypeEndsWithBrackets)
$targetType = substr($targetType, 0, $targetTypeBracketsPos);
}
$result = [];
$paramsCollectionArr = $paramsCollection[$name];
foreach ($paramsCollectionArr as $key => $value) {
$cleanedKey = is_numeric($key)
? $key
: $this->cleanParamValue($key, $pregReplaceAllowedChars);
$result[$cleanedKey] = $this->getParamItem(
$value, $pregReplaceAllowedChars, $ifNullValue, $targetType
);
}
return $result;
} else {
return $this->getParamItem(
$paramsCollection[$name], $pregReplaceAllowedChars, $ifNullValue, $targetType
);
}
}
protected function initScriptNameAndBasePath () {
$this->basePath = '';
$this->scriptName = str_replace('\\', '/', $this->globalServer['SCRIPT_NAME']);
$lastSlashPos = mb_strrpos($this->scriptName, '/');
if ($lastSlashPos !== 0) {
$redirectUrl = rawurldecode(isset($this->globalServer['REDIRECT_URL']) ? $this->globalServer['REDIRECT_URL'] : '');
$redirectUrlLength = mb_strlen($redirectUrl);
$requestUri = rawurldecode($this->globalServer['REQUEST_URI']);
$questionMarkPos = mb_strpos($requestUri, '?');
if ($questionMarkPos !== FALSE) $requestUri = mb_substr($requestUri, 0, $questionMarkPos);
if ($redirectUrlLength === 0 || ($redirectUrlLength > 0 && $redirectUrl === $requestUri)) {
$this->basePath = mb_substr($this->scriptName, 0, $lastSlashPos);
$this->scriptName = '/' . mb_substr($this->scriptName, $lastSlashPos + 1);
} else {
$requestUriPosInRedirectUri = mb_strrpos($redirectUrl, $requestUri);
$apacheRedirectedPath = mb_substr($redirectUrl, 0, $requestUriPosInRedirectUri);
$this->scriptName = mb_substr($this->scriptName, mb_strlen($apacheRedirectedPath));
$lastSlashPos = mb_strrpos($this->scriptName, '/');
$this->basePath = mb_substr($this->scriptName, 0, $lastSlashPos);
}
} else {
$this->scriptName = '/' . mb_substr($this->scriptName, $lastSlashPos + 1);
}
}
protected function initLangAndLocale () {
if (!isset($this->globalServer['HTTP_ACCEPT_LANGUAGE'])) {
$this->lang = '';
$this->locale = '';
return;
}
$rawUaLanguages = $this->globalServer['HTTP_ACCEPT_LANGUAGE'];
if (extension_loaded('Intl')) {
$langAndLocaleStr = \locale_accept_from_http($rawUaLanguages);
$langAndLocaleArr = $langAndLocaleStr !== NULL
? explode('_', $langAndLocaleStr)
: [NULL, NULL];
} else {
$languagesAndLocales = static::ParseHttpAcceptLang($rawUaLanguages);
$langAndLocaleArr = current($languagesAndLocales);
if (is_array($langAndLocaleArr))
$langAndLocaleArr = current($langAndLocaleArr);
}
if ($langAndLocaleArr[0] === NULL) $langAndLocaleArr[0] = '';
if (!isset($langAndLocaleArr[1])) $langAndLocaleArr[1] = '';
list($this->lang, $this->locale) = $langAndLocaleArr;
}
protected function initDomainSegments () {
$hostName = $this->GetHostName();
$this->domainParts = [];
$lastDotPos = mb_strrpos($hostName, '.');
if ($lastDotPos === FALSE) {
$this->domainParts = [NULL, NULL, $hostName];
} else {
$first = mb_substr($hostName, $lastDotPos + 1);
$second = mb_substr($hostName, 0, $lastDotPos);
if (self::$twoSegmentTlds) {
$lastDotPos = mb_strrpos($second, '.');
if ($lastDotPos !== FALSE) {
$firstTmp = mb_substr($second, $lastDotPos + 1) . '.' . $first;
if (isset(self::$twoSegmentTlds[$firstTmp])) {
$first = $firstTmp;
$second = $firstTmp = mb_substr($second, 0, $lastDotPos);
}
}
}
$lastDotPos = mb_strrpos($second, '.');
if ($lastDotPos === FALSE) {
$this->domainParts = [NULL, $second, $first];
} else {
$third = mb_substr($second, 0, $lastDotPos);
$second = mb_substr($second, $lastDotPos + 1);
$this->domainParts = [$third, $second, $first];
}
}
}
}