/** * @return bool */ public static function isAdmin() : bool { $ips = DI::config()->getIfExists('ip/admin'); if (!$ips) { return false; } $ip = Ip::get(); foreach ($ips as $currentIp) { if ($currentIp === $ip) { return true; } } return false; }
/** * @param string $ip * * @return $this|self|null */ public static function getByIp(string $ip = null) { if (is_null($ip)) { $ip = Ip::get(); } $db = self::db('MAXMIND'); $sql = 'SELECT location_id, location_continent, location_country, location_subdivision_1, location_subdivision_2, location_city, location_metro, location_timezone, block_start_ip, block_end_ip, block_anonymous_proxy, block_satellite_provider, block_postal_code, block_latitude, block_longitude FROM tbl_geo_location INNER JOIN ( SELECT * FROM tbl_geo_block WHERE block_start_ip >= INET_ATON(:ip) LIMIT 1 ) AS r ON block_location_id = location_id AND INET_ATON(:ip) <= block_end_ip'; if ($result = $db->fetchOne($sql, ['ip' => $ip])) { $return = new static(); $return->location = new Location(); $return->location->map($result); $return->block = new Block(); $return->block->map($result); return $return; } return null; }
/** * @return array */ public function display() { $hide = true; if (Ip::isAdmin() || Ip::isLocal()) { $hide = false; } if ($this->code == 500 && $hide) { return ['status' => $this->code, 'message' => 'Internal server error']; } $return = ['status' => $this->code, 'message' => $this->message]; if ($this->getPrevious()) { if (isset($return['message'])) { $message = $return['message']; } $return = array_merge($return, $this->export($this->getPrevious())); if (isset($message) && $message) { $return['message'] = $message; } if ($this->getPrevious()->getPrevious()) { $return['previous'] = $this->export($this->getPrevious()->getPrevious()); } } return $return; }
/** * @param string $name * @param mixed $value * * @throws ResponseCode * @throws \Exception * * @return array|bool|float|string */ public function validateCondition(string $name, $value) { foreach ($this->validations as $validation) { $success = true; list($condition, $conditionValue) = $validation; $conditionValue = is_array($conditionValue) ? implode(';', $conditionValue) : $conditionValue; switch ($condition) { case 'gte': if ($value < $conditionValue) { $success = false; } break; case 'gt': if ($value <= $conditionValue) { $success = false; } break; case 'lte': if ($value > $conditionValue) { $success = false; } break; case 'lt': if ($value >= $conditionValue) { $success = false; } break; case 'in': $allValues = strpos($this->getType(), '[]') !== false ? $value : [$value]; $conditionValue = is_array($conditionValue) ? $conditionValue : [$conditionValue]; foreach ($allValues as $currentValue) { if (!in_array($currentValue, $conditionValue)) { $success = false; } } break; case 'isip': $isValid = Ip::isValid(); $success = $isValid && $conditionValue == 'true' || !$isValid && $conditionValue == 'false'; break; default: throw new \Exception(sprintf("Invalid condition parameters '%s: %s' for parameter '%s' with value '%s'", $condition, $conditionValue, $name, $value)); break; } if (!$success) { $value = is_array($value) ? json_encode($value) : $value; throw new ResponseCode(sprintf("Invalid conditions for parameter '%s' with value '%s' for condition '%s : %s'", $name, $value, $condition, is_array($conditionValue) ? json_encode($conditionValue) : $conditionValue), 422); } } return $value; }
/** * @param \Throwable $exception */ public static function log(\Throwable $exception) { $level = 'emergency'; if ($exception instanceof Error) { foreach (self::LEVEL_LOG as $log => $codes) { if (in_array($exception->getCode(), $codes) === true) { $level = $log; } } } $context = []; $reflection = new \ReflectionClass($exception); foreach ($reflection->getProperties() as $property) { if (!$property->isPrivate()) { $property->setAccessible(true); $value = $property->getValue($exception); // can be exported as context if ($value === null || is_scalar($value) || is_callable([$value, '__toString'])) { $context[$property->getName()] = (string) $value; } } } unset($context['message']); $start = self::request()->getServer('REQUEST_TIME_FLOAT'); $end = microtime(true); $context['Duration'] = round(($end - $start) * 1000, 3); $context['Ip'] = Ip::get(); $context['Url'] = self::request()->getUri()->get(false); $context['Trace'] = $exception->getTraceAsString(); $context['Referer'] = self::request()->getHeader('Referer'); self::logger()->log($level, $exception->getMessage(), $context); }
/** * @return bool */ private function getAuth() : bool { // Auth is already done if (sizeof($this->services) > 0) { return true; } $auth = $this->getUserPassword(); if (!$auth) { return false; } list($user, $password) = $auth; $usersList = $this->module->users; if (!isset($usersList[$user])) { return false; } // password check if (md5(strtolower($user) . $password) != $usersList[$user]['password']) { return false; } // ip check if (isset($usersList[$user]['ip']) && sizeof($usersList[$user]['ip']) > 0) { $ipSuccess = false; $currentIp = Ip::get(); foreach ($usersList[$user]['ip'] as $currentRestriction) { $isRange = stripos($currentRestriction, '/') !== false; if ($isRange && Ip::isInRange($currentRestriction, $currentIp) || $currentIp == $currentRestriction) { $ipSuccess = true; break; } } if (!$ipSuccess) { return false; } } $this->services = $usersList[$user]['services']; $this->user = $user; return true; }