| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 1 | <?php | 
| Marc Kupietz | 0215a44 | 2023-03-05 18:34:16 +0100 | [diff] [blame] | 2 |    include "User.php"; | 
 | 3 |    use \User as User; | 
 | 4 |  | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 5 |    //Connecting to Redis server on localhost  | 
 | 6 |    function connect(){ | 
 | 7 |       include 'config.php'; | 
 | 8 |       $redis = new Redis();  | 
 | 9 |       $redis->connect('127.0.0.1', 6379);  | 
 | 10 |       $redis->auth("$REDIS_PASS"); | 
 | 11 |       return $redis; | 
 | 12 |    } | 
 | 13 |    function redis_get($key){ | 
 | 14 |       $redis = connect(); | 
 | 15 |       return json_decode($redis->get($key)); | 
 | 16 |    } | 
| Marc Kupietz | 0215a44 | 2023-03-05 18:34:16 +0100 | [diff] [blame] | 17 |  | 
 | 18 |    function redis_get_user($key) { | 
 | 19 |       $redis = connect(); | 
| Marc Kupietz | 0314662 | 2023-03-07 12:03:21 +0100 | [diff] [blame] | 20 |       $data = json_decode($redis->get($key), true); | 
 | 21 |       if ($data && gettype($data) == "array") { | 
| Marc Kupietz | ade9e3c | 2023-03-06 21:39:23 +0100 | [diff] [blame] | 22 |          $user = new User(); | 
| Marc Kupietz | 0314662 | 2023-03-07 12:03:21 +0100 | [diff] [blame] | 23 |          $user->init_from_array($data); | 
 | 24 |  | 
 | 25 |          #foreach ($data as $key => $value) $user->{$key} = $value; | 
 | 26 |          #if (isset($data->password)) $user->set_password($data->password); | 
| Marc Kupietz | ade9e3c | 2023-03-06 21:39:23 +0100 | [diff] [blame] | 27 |          #$user = new User($data->username, $data->first_name, $data->last_name, $data->email, $data->password, $data->organization); | 
| Marc Kupietz | 393930c | 2023-03-06 07:05:47 +0100 | [diff] [blame] | 28 |          return $user; | 
 | 29 |       } else { | 
 | 30 |          return null; | 
 | 31 |       } | 
| Marc Kupietz | 0215a44 | 2023-03-05 18:34:16 +0100 | [diff] [blame] | 32 |    } | 
 | 33 |  | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 34 |    function redis_set($key, $data, $timeout=null){ | 
 | 35 |       $redis = connect(); | 
 | 36 |       $redis->set($key, json_encode($data), $timeout); | 
 | 37 |    } | 
 | 38 |  | 
 | 39 |    function redis_inc_ipdata($ip, $attr, $get=false){ | 
 | 40 |       $count = redis_get($ip); | 
 | 41 |       if ($count){ | 
 | 42 |          if (isset($count->$attr))  $count->$attr = $count->$attr+1; | 
 | 43 |          else  $count->$attr = 1; | 
 | 44 |       }else $count = (object)[$attr=>1]; | 
 | 45 |  | 
 | 46 |       if (!$get) redis_set($ip, $count, 3600); | 
 | 47 |       return $count->$attr; | 
 | 48 |    } | 
 | 49 |  | 
 | 50 |    function redis_delete($key){ | 
 | 51 |       $redis = connect(); | 
 | 52 |       $redis->del($key); | 
 | 53 |    } |