aboutsummaryrefslogtreecommitdiff
path: root/lib/phplib/CT_Sql.php
blob: 220af2241d8371541c5c6de0e1ccb1e566720160 (plain)
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
<?php

##
## Copyright (c) 1998-2000 NetUSE AG
##                    Boris Erdmann, Kristian Koehntopp
##
## Copyright (c) 1998-2000 Sascha Schumann <sascha@schumann.cx>
##
## PHPLIB Data Storage Container using a SQL database
## for use with Stud.IP and PDO only!

class CT_Sql {
    
    ##
    ## Define these parameters by overwriting or by
    ## deriving your own class from it (recommened)
    ##
    
    var $database_table = "session_data";
    var $gzip_level = 0;
    var $exists = '';
    
    ## end of configuration
    
    function ac_start() {
    }
    
    function ac_get_lock() {
        return true;
    }
    
    function ac_release_lock() {
        return true;
    }
    
    function ac_gc($gc_time, $name = null) {
        return DBManager::get()->exec(sprintf("DELETE FROM %s WHERE changed < FROM_UNIXTIME(%s) ",
            $this->database_table,
            (time() - ($gc_time * 60))
            ));
    }
    
    function ac_store($id, $name, $str) {
        $db = DBManager::get();
        if ($this->gzip_level){
            $str = gzcompress($str, $this->gzip_level);
        }
        if ($this->exists === $id) {
            $stmt = $db->prepare(sprintf("UPDATE %s SET val = ? WHERE sid = ?", $this->database_table));
        } else {
            $stmt = $db->prepare(sprintf("REPLACE INTO %s ( val, sid ) VALUES (?, ?)", $this->database_table));
        }
        $stmt->execute([$str, $id]);
        return $stmt->rowCount();
    }
    
    function ac_delete($id, $name = null) {
        return DBManager::get()->exec(sprintf("DELETE FROM %s WHERE sid = '%s' LIMIT 1",
            $this->database_table,
            $id));
    }
    
    function ac_get_value($id, $name = null) {
        $rs = DBManager::get()->query(sprintf("SELECT val FROM %s where sid  = '%s'",
            $this->database_table,
            $id));
        $str  = $rs->fetchColumn();
        if ($this->gzip_level){
            $str = @gzuncompress($str);
        }
        if ($str) $this->exists = $id;
        return $str;
    }
    
    function ac_get_changed($id, $name = null){
        $rs = DBManager::get()->query(sprintf("SELECT UNIX_TIMESTAMP(changed) FROM %s WHERE sid  = '%s'",
            $this->database_table,
            $id));
        return $rs->fetchColumn();
    }
    
    function ac_set_changed($id, $name, $timestamp){
        $db = DBManager::get();
        $stmt = $db->prepare(sprintf("UPDATE %s SET changed = FROM_UNIXTIME(?) WHERE sid  = ?", $this->database_table));
        $stmt->execute([$timestamp, $id]);
        return $stmt->rowCount();
    }
    
    function ac_newid($str, $name = null) {
        $db = DBManager::get();
        $query = "SELECT sid FROM " . $this->database_table . " WHERE sid = '$str'";
        if (!$db->query($query)->fetchColumn()) {
            return $str;
        } else {
            return FALSE;
        }
    }
    
    function ac_halt($s) {
    }
}