OpenSencillo  2015.009
Long live the simplicity of PHP
 All Data Structures Namespaces Files Functions Pages
file.php
1 <?php
2 /*~ file.php
3 .---------------------------------------------------------------------------.
4 | Software: SencilloCache |
5 | Version: 2015.003 |
6 | Contact: ph@mastery.sk |
7 | ------------------------------------------------------------------------- |
8 | Author: Bc. Peter Horváth |
9 | Copyright (c) 2015, Bc. Peter Horváth. All Rights Reserved. |
10 | ------------------------------------------------------------------------- |
11 | License: Distributed under the General Public License (GPL) |
12 | http://www.gnu.org/licenses/gpl-3.0.html |
13 | This program is distributed in the hope that it will be useful - WITHOUT |
14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
15 | FITNESS FOR A PARTICULAR PURPOSE. |
16 '---------------------------------------------------------------------------'
17 ~*/
18 
19 /* Template for other types of cache
20  * You'll need to implement these 5 functions, add
21  * additional functions inhere.
22  *
23  * Add variables you use to quickcache_config.php
24  *
25  * When you've implemented a new storage-system, and think that the world
26  * could/should use it too, please submit it to (andy.prevost@worxteam.com),
27  * and we will include it in a next release (with full credits, ofcourse).
28  */
29 
30 /* quickcache_restore()
31  * Will (try to) restore the cachedata.
32  */
33 function quickcache_restore() {
34  // Construct filename
35  $filename = $GLOBALS["QUICKCACHE_DIR"]."/".$GLOBALS["QUICKCACHE_FILEPREFIX"].$GLOBALS["quickcache_key"];
36 
37  // read file and unserialize the data
38  $cachedata=unserialize(quickcache_fileread($filename));
39  if (is_array($cachedata)) {
40  // Only read cachefiles of my version
41  if ($cachedata["quickcache_version"] == $GLOBALS["QUICKCACHE_VERSION"]) {
42  if (($cachedata["quickcache_expire"] == "0") ||
43  ($cachedata["quickcache_expire"] >= time()))
44  {
45  //Restore data
46  $GLOBALS["quickcachedata_gzdata"] = $cachedata["quickcachedata_gzdata"];
47  $GLOBALS["quickcachedata_datasize"] = $cachedata["quickcachedata_datasize"];
48  $GLOBALS["quickcachedata_datacrc"] = $cachedata["quickcachedata_datacrc"];
49  return TRUE;
50  } else {
51  quickcache_debug("Data in cachefile $filename has expired");
52  }
53  } else {
54  // Invalid version of cache-file
55  quickcache_debug("Invalid version of cache-file $filename");
56  }
57  } else {
58  // Invalid cache-file
59  quickcache_debug("Invalid content of cache-file $filename");
60  }
61 
62  return FALSE;
63 }
64 
65 /* quickcache_write()
66  * Will (try to) write out the cachedata to the db
67  */
68 function quickcache_write($gzdata, $datasize, $datacrc) {
69  // Construct filename
70  $filename = $GLOBALS["QUICKCACHE_DIR"]."/".$GLOBALS["QUICKCACHE_FILEPREFIX"].$GLOBALS["quickcache_key"];
71 
72  // Create and fill cachedata-array
73  $cachedata = array();
74  $cachedata["quickcache_version"] = $GLOBALS["QUICKCACHE_VERSION"];
75  $cachedata["quickcache_expire"] = ($GLOBALS["QUICKCACHE_TIME"] > 0) ?
76  time() + $GLOBALS["QUICKCACHE_TIME"] :
77  0;
78  $cachedata["quickcachedata_gzdata"] = $gzdata;
79  $cachedata["quickcachedata_datasize"] = $datasize;
80  $cachedata["quickcachedata_datacrc"] = $datacrc;
81 
82  // And write the data
83  if (quickcache_filewrite($filename, serialize($cachedata))) {
84  quickcache_debug("Successfully wrote cachefile $filename");
85  } else {
86  quickcache_debug("Unable to write cachefile $filename");
87  }
88 }
89 
90 /* quickcache_do_gc()
91  * Performs the actual garbagecollection
92  */
93 function quickcache_do_gc() {
94  $dp=opendir($GLOBALS["QUICKCACHE_DIR"]);
95 
96  // Can we access directory ?
97  if (!$dp)
98  {
99  quickcache_debug("Error opening ". $GLOBALS["QUICKCACHE_DIR"] ." for garbage-collection");
100  }
101 
102  while (!(($de=readdir($dp))===FALSE))
103  {
104  // To get around strange php-strpos, add additional char
105  // Only read quickcache-files.
106  if (strpos("x$de", $GLOBALS["QUICKCACHE_FILEPREFIX"])==1) {
107  $filename=$GLOBALS["QUICKCACHE_DIR"] . "/" . $de;
108  // read file and unserializes the data
109  $cachedata=unserialize(quickcache_fileread($filename));
110 
111  // Check data in array.
112  if (is_array($cachedata)) {
113  if ($cachedata["quickcache_expire"]!="0" && $cachedata["quickcache_expire"]<=time()) {
114  // Unlink file, we do not need to get a lock
115  $deleted = @unlink($filename);
116  if ($deleted) {
117  quickcache_debug("Successfully unlinked $filename");
118  } else {
119  quickcache_debug("Failed to unlink $filename");
120  }
121  }
122  }
123  }
124  }
125 }
126 
127 /* quickcache_do_start()
128  * Additional code that is executed before real quickcache-code kicks in
129  */
130 function quickcache_do_start() {
131  // Add additional code you might require
132 }
133 
134 /* quickcache_do_end()
135  * Additional code that is executed after caching has been performed,
136  * but just before output is returned. No new output can be added!
137  */
138 function quickcache_do_end() {
139  // Add additional code you might require
140 }
141 
142 /* This internal function reads in the cache-file */
143 function quickcache_fileread($filename) {
144  // php.net suggested I should use rb to make it work under Windows
145  $fp=@fopen($filename, "rb");
146  if (!$fp) {
147  quickcache_debug("Failed to open for read of $filename");
148  return NULL;
149  }
150 
151  // Get a shared lock
152  flock($fp, LOCK_SH);
153 
154  $buff="";
155  // Be gentle, so read in 4k blocks
156  while (($tmp=fread($fp, 4096))) {
157  $buff.=$tmp;
158  }
159 
160  // Release lock
161  flock($fp, LOCK_UN);
162  fclose($fp);
163  // Return
164  return $buff;
165 }
166 
167 /* This internal function writes the cache-file */
168 function quickcache_filewrite($filename, $data) {
169  $return = FALSE;
170  // Lock file, ignore warnings as we might be creating this file
171  $fpt = @fopen($filename, "rb");
172  @flock($fpt, LOCK_EX);
173 
174  // php.net suggested I should use wb to make it work under Windows
175  $fp=@fopen($filename, "wb+");
176  if (!$fp) {
177  // Strange! We are not able to write the file!
178  quickcache_debug("Failed to open for write of $filename");
179  } else {
180  fwrite($fp, $data, strlen($data));
181  fclose($fp);
182  $return = TRUE;
183  }
184 
185  // Release lock
186  @flock($fpt, LOCK_UN);
187  @fclose($fpt);
188  // Return
189  return $return;
190 }
191 
192 // Make sure no additional lines/characters are after the closing-tag!
193 ?>