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:
<?php
namespace MvcCore\Environment;
trait Detection {
public static function DetectByIps () {
$request = \MvcCore\Application::GetInstance()->GetRequest();
$serverAddress = $request->GetServerIp();
$remoteAddress = $request->GetClientIp();
if ($serverAddress == $remoteAddress) {
$name = static::DEVELOPMENT;
} else {
$name = static::PRODUCTION;
}
return $name;
}
public static function DetectBySystemConfig (array $environmentsSectionData = []) {
$name = NULL;
$app = self::$app ?: self::$app = \MvcCore\Application::GetInstance();
$request = $app->GetRequest();
$clientIp = $request->GetClientIp();
$appRoot = NULL;
$serverHostName = NULL;
$serverGlobals = NULL;
foreach ($environmentsSectionData as $environmentName => $environmentSection) {
$sectionData = static::detectByConfigSectionData($environmentSection, $clientIp);
$detected = static::detectByConfigSection(
$sectionData, $request, $clientIp, $appRoot, $serverHostName, $serverGlobals
);
if ($detected) {
$name = $environmentName;
break;
}
}
if ($name == NULL)
$name = \MvcCore\IEnvironment::PRODUCTION;
return $name;
}
protected static function detectByConfigSectionData ($environmentSection, $clientIp) {
$data = (object) [
'clientIps' => (object) [
'check' => FALSE,
'values' => [],
'regExeps' => []
],
'paths' => (object) [
'check' => FALSE,
'values' => [],
'regExeps' => []
],
'serverHostNames' => (object) [
'check' => FALSE,
'values' => [],
'regExeps' => []
],
'serverVariables' => (object) [
'check' => FALSE,
'existence' => [],
'values' => [],
'regExeps' => []
]
];
if (is_string($environmentSection) && strlen($environmentSection) > 0) {
if ($clientIp !== NULL)
static::detectByConfigClientIps($data, $environmentSection);
} else if (is_array($environmentSection) || $environmentSection instanceof \stdClass) {
foreach ((array) $environmentSection as $key => $value) {
if (is_numeric($key) || $key == 'clients') {
if ($clientIp !== NULL)
static::detectByConfigClientIps($data, $value);
} else if ($key == 'paths') {
static::detectByConfigPaths($data, $value);
} else if ($key == 'servers') {
static::detectByConfigServerNames($data, $value);
} else if ($key == 'variables') {
static::detectByConfigEnvVariables($data, $value);
}
}
}
return $data;
}
protected static function detectByConfigClientIps (& $data, $rawClientIps) {
$data->clientIps->check = TRUE;
if (is_string($rawClientIps)) {
if (static::detectRegExpCheck($rawClientIps)) {
$data->clientIps->regExeps[] = $rawClientIps;
} else {
$data->clientIps->values = array_merge(
$data->clientIps->values,
explode(',', str_replace(' ', '', $rawClientIps))
);
}
} else if (is_array($rawClientIps) || $rawClientIps instanceof \stdClass) {
foreach ((array) $rawClientIps as $rawClientIpsItem) {
if (static::detectRegExpCheck($rawClientIpsItem)) {
$data->clientIps->regExeps[] = $rawClientIpsItem;
} else {
$data->clientIps->values = array_merge(
$data->clientIps->values,
explode(',', str_replace(' ', '', $rawClientIpsItem))
);
}
}
}
}
protected static function detectByConfigPaths (& $data, $rawPaths) {
$data->paths->check = TRUE;
if (is_string($rawPaths)) {
if (static::detectRegExpCheck($rawPaths)) {
$data->paths->regExeps[] = $rawPaths;
} else {
$data->paths->values = array_merge(
$data->paths->values,
explode(',', str_replace(' ', '', $rawPaths))
);
}
} else if (is_array($rawPaths) || $rawPaths instanceof \stdClass) {
foreach ((array) $rawPaths as $rawPathsItem) {
if (static::detectRegExpCheck($rawPathsItem)) {
$data->paths->regExeps[] = $rawPathsItem;
} else {
$data->paths->values = array_merge(
$data->paths->values,
explode(',', str_replace(' ', '', $rawPathsItem))
);
}
}
}
}
protected static function detectByConfigServerNames (& $data, $rawHostNames) {
$data->serverHostNames->check = TRUE;
if (is_string($rawHostNames)) {
if (static::detectRegExpCheck($rawHostNames)) {
$data->serverHostNames->regExeps[] = $rawHostNames;
} else {
$data->serverHostNames->values = array_merge(
$data->serverHostNames->values,
explode(',', str_replace(' ', '', $rawHostNames))
);
}
} else if (is_array($rawHostNames) || $rawHostNames instanceof \stdClass) {
foreach ((array) $rawHostNames as $rawHostNamesItem) {
if (static::detectRegExpCheck($rawHostNamesItem)) {
$data->serverHostNames->regExeps[] = $rawHostNamesItem;
} else {
$data->serverHostNames->values = array_merge(
$data->serverHostNames->values,
explode(',', str_replace(' ', '', $rawHostNamesItem))
);
}
}
}
}
protected static function detectByConfigEnvVariables (& $data, $rawServerVariable) {
$data->serverVariables->check = TRUE;
if (is_string($rawServerVariable)) {
$data->serverVariables->existence[] = $rawServerVariable;
} else if (is_array($rawServerVariable) || $rawServerVariable instanceof \stdClass) {
foreach ((array) $rawServerVariable as $key => $value) {
if (is_numeric($key)) {
$data->serverVariables->existence[] = $value;
} else if (static::detectRegExpCheck($value)) {
$data->serverVariables->regExeps[$key] = $value;
} else {
$data->serverVariables->values[$key] = $value;
}
}
}
}
protected static function detectByConfigSection (& $data, $req, & $clientIp, & $appRoot, & $serverHostName, & $serverGlobals) {
if ($data->clientIps->check) {
$clientIp = $clientIp ?: $req->GetClientIp();
if ($data->clientIps->values) {
$clientIpToMatch = ',' . $clientIp . ',';
$clientIpsToMatch = ',' . implode(',', $data->clientIps->values) . ',';
if (strpos($clientIpsToMatch, $clientIpToMatch) !== FALSE)
return TRUE;
}
if ($data->clientIps->regExeps) {
foreach ($data->clientIps->regExeps as $rawRegExep) {
$regExep = static::detectRegExpConvert($rawRegExep);
if (preg_match($regExep, $clientIp))
return TRUE;
}
}
}
if ($data->paths->check) {
$appRoot = $appRoot ?: $req->GetAppRoot();
if ($data->paths->values)
foreach ($data->paths->values as $pathToMatch)
if (mb_strpos($appRoot, $pathToMatch) !== FALSE)
return TRUE;
if ($data->paths->regExeps) {
foreach ($data->paths->regExeps as $rawRegExep) {
$regExep = static::detectRegExpConvert($rawRegExep);
if (preg_match($regExep, $appRoot))
return TRUE;
}
}
}
if ($data->serverHostNames->check) {
$serverHostName = $serverHostName ?: gethostname();
if ($data->serverHostNames->values) {
$serverHostNamesToMatch = ','.implode(',', $data->serverHostNames->values).',';
if (strpos($serverHostNamesToMatch, ','.$serverHostName.',') !== FALSE)
return TRUE;
}
if ($data->serverHostNames->regExeps) {
foreach ($data->serverHostNames->regExeps as $rawRegExep) {
$regExep = static::detectRegExpConvert($rawRegExep);
if (preg_match($regExep, $serverHostName))
return TRUE;
}
}
}
if ($data->serverVariables->check) {
$serverGlobals = $serverGlobals ?: array_merge(getenv(), $req->GetGlobalCollection('server'));
if ($data->serverVariables->existence)
foreach ($data->serverVariables->existence as $serverVariableName)
if (array_key_exists($serverVariableName, $serverGlobals))
return TRUE;
if ($data->serverVariables->values)
foreach ($data->serverVariables->values as $serverVariableName => $serverVariableValue)
if (
isset($serverGlobals[$serverVariableName]) &&
$serverGlobals[$serverVariableName] == $serverVariableValue
) return TRUE;
if ($data->serverVariables->regExeps) {
foreach ($data->serverVariables->regExeps as $serverVariableName => $rawRegExep) {
$serverVariableRegExp = static::detectRegExpConvert($rawRegExep);
if (
isset($serverGlobals[$serverVariableName]) &&
preg_match($serverVariableRegExp, (string) $serverGlobals[$serverVariableName])
) return TRUE;
}
}
}
return FALSE;
}
protected static function detectRegExpCheck ($rawValue) {
return (bool) preg_match("#^/(.+)/([imsxADSUXJu]*)$#", $rawValue);
}
protected static function detectRegExpConvert ($regExpWithTrailingSlashes) {
return preg_replace("#^/(.+)/([imsxADSUXJu]*)$#", "#$1#$2", $regExpWithTrailingSlashes);
}
}