Esempio n. 1
0
File: Ip.php Progetto: cawaphp/cawa
 /**
  * @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;
 }
Esempio n. 2
0
 /**
  * @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;
 }
Esempio n. 3
0
 /**
  * @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);
 }
Esempio n. 4
0
 /**
  * @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;
 }