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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/**
 * Data Caching Script
 * Easily Cache Information to a MySQL Table or a Flat-File
 * 
 * Email me possible bugs and suggestions to help improve
 * future releases and patches.
 * 
 * @author JMan <jman@bedpan.ca>
 * @link http://www.bedpan.ca
 * @version 2.2.0
 */
class Cache{
    
/**
     * Toggles whether to use MySQL or Flat-File Caching.
     *
     * @var bool
     */
    
public $useMySQL true;
    
/**
     * Your MySQL server address.
     *
     * @var string
     */
    
public $dbServer "localhost";
    
/**
     * The user name for your MySQL with read and write privleges
     * to the cache table that you've already created.
     *
     * @var string
     */
    
public $dbUser "root";
    
/**
     * The password for the MySQL account above.
     *
     * @var string
     */
    
public $dbPass "";
    
/**
     * The name of the database where the cache table is located.
     *
     * @var string
     */
    
public $dbName "db_name";
    
/**
     * Directory on your server where cache data is to be kept
     * with the trailing slash, relative to the script.
     * Must exist, and be readable and writable in order to work.
     *
     * @var string
     */
    
public $cacheDir "testCache/";
    
/**
     * Stores a back-up of old cached data (if available).
     *
     * @var array
     */
    
public $oldCache = array();

    
/**
     * Function used to connect to your MySQL server.
     *
     * @return bool
     */
    
private function mysqlConnect(){
        if (
$con = @mysql_connect($this->dbServer$this->dbUser$this->dbPass)){
            
mysql_select_db($this->dbName);
            return 
true;
        }else return 
false;
    }
    
/**
     * Adds the appropriate slashes to the data which is to be cached.
     *
     * @param string $item1
     * @param int $clean
     * @return string
     */
    
private function slash($item1$clean=false){
        if (
$clean == 1){
            return 
stripslashes($item1);
        }elseif (
$clean == 2){
            return 
str_replace("\\""\\\\"$item1);
        }else{
            return 
addslashes(str_replace("\\""\\\\"$item1));
        }
    }
    
/**
     * Decodes the slashed and serialized cached data.
     *
     * @param unknown_type $data
     * @return var
     */
    
private function deCode($data){
        
$data unserialize($this->slash($data1));
        if (
is_array($data)){
            foreach (
$data as $key => $value){
                
$data[$key] = $this->slash($value2);
            }
        }else{
            
$data $this->slash($data2);
        }
        return 
$data;
    }
    
/**
     * The main function, which caches, and retrieves previously
     * caches data from your MySQL server.
     * Returns cached data (if available), or false on failure.
     *
     * @param string $id
     * @param int $seconds
     * @param var $data
     * @return var/bool
     */
    
public function doCache($id$seconds=0$data=""){
        
$id addslashes($id);
        
$exptime time() + $seconds;
        if (
$this->useMySQL){
            if (
$this->mysqlConnect()){
                
$result mysql_query("SELECT * FROM cache WHERE id = '$id'") or die(mysql_error());
                if (
mysql_num_rows($result)){
                    
$db mysql_fetch_row($result);
                    
$this->oldCache[$id] = $this->deCode($db[1]);
                    
mysql_query("DELETE FROM cache WHERE id = '$id' && timestamp <= UNIX_TIMESTAMP()") or die(mysql_error());
                }
                
$result mysql_query("SELECT * FROM cache WHERE id = '$id'") or die(mysql_error());
                if (
mysql_num_rows($result)){
                    
$db mysql_fetch_row($result);
                    return 
$this->deCode($db[1]);
                }else{
                    
$data $this->slash(serialize($data));
                    if (
$datamysql_query("INSERT INTO cache (id, stored, timestamp) VALUES ('$id', '$data', '$exptime')") or die(mysql_error());
                    return 
false;
                }
            }else return 
false;
        }else{
            if (!
is_dir(dirname($_SERVER['DOCUMENT_ROOT'] . $_SERVER['PHP_SELF']) . DIRECTORY_SEPARATOR $this->cacheDir)) die("ERROR: \$cacheDir [" dirname($_SERVER['DOCUMENT_ROOT'] . $_SERVER['PHP_SELF']) . DIRECTORY_SEPARATOR $this->cacheDir "] Must Exist, and be Readable and Writable");
            
$fileName realpath(dirname($_SERVER['DOCUMENT_ROOT'] . $_SERVER['PHP_SELF']) . DIRECTORY_SEPARATOR $this->cacheDir) . DIRECTORY_SEPARATOR $id;
            if (
file_exists($fileName)){
                
$this->oldCache $this->deCode(file_get_contents($fileName));
                if (
filemtime($fileName) <= time()) unlink($fileName);
            }
            if (
file_exists($fileName)) return $this->deCode(file_get_contents($fileName));
            else{
                
$data $this->slash(serialize($data));
                
$handle fopen($fileName'w+');
                if (
$data && $handle){
                    
fwrite($handle$data);
                    
fclose($handle);
                    
touch($fileName$exptime);
                }
                return 
false;
            }
        }
    }
}
?>

Return to Script