blob: 323a74b7be3a62e92a46f33ba80f331420d9ca75 [file] [log] [blame]
matheusfillipeabd513e2021-05-11 03:29:11 -03001<?php
Marc Kupietz0215a442023-03-05 18:34:16 +01002 include "User.php";
3 use \User as User;
4
matheusfillipeabd513e2021-05-11 03:29:11 -03005 //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 Kupietz0215a442023-03-05 18:34:16 +010017
18 function redis_get_user($key) {
19 $redis = connect();
20 $data = json_decode($redis->get($key));
Marc Kupietz393930c2023-03-06 07:05:47 +010021 if ($data && gettype($data) == "object") {
Marc Kupietzade9e3c2023-03-06 21:39:23 +010022 $user = new User();
23 foreach ($data as $key => $value) $user->{$key} = $value;
24 if (isset($data->password)) $user->set_password($data->password);
25 #$user = new User($data->username, $data->first_name, $data->last_name, $data->email, $data->password, $data->organization);
Marc Kupietz393930c2023-03-06 07:05:47 +010026 return $user;
27 } else {
28 return null;
29 }
Marc Kupietz0215a442023-03-05 18:34:16 +010030 }
31
matheusfillipeabd513e2021-05-11 03:29:11 -030032 function redis_set($key, $data, $timeout=null){
33 $redis = connect();
34 $redis->set($key, json_encode($data), $timeout);
35 }
36
37 function redis_inc_ipdata($ip, $attr, $get=false){
38 $count = redis_get($ip);
39 if ($count){
40 if (isset($count->$attr)) $count->$attr = $count->$attr+1;
41 else $count->$attr = 1;
42 }else $count = (object)[$attr=>1];
43
44 if (!$get) redis_set($ip, $count, 3600);
45 return $count->$attr;
46 }
47
48 function redis_delete($key){
49 $redis = connect();
50 $redis->del($key);
51 }