1: <?php
2: /**
3: * Cache engine For XOOPS
4: *
5: * You may not change or alter any portion of this comment or credits
6: * of supporting developers from this source code or any supporting source code
7: * which is considered copyrighted (c) material of the original comment or credit authors.
8: * This program is distributed in the hope that it will be useful,
9: * but WITHOUT ANY WARRANTY; without even the implied warranty of
10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11: *
12: * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org)
13: * @license GNU GPL 2 (http://www.gnu.org/licenses/gpl-2.0.html)
14: * @package class
15: * @subpackage cache
16: * @since 2.3.0
17: * @author Taiwen Jiang <phppp@users.sourceforge.net>
18: */
19: defined('XOOPS_ROOT_PATH') || exit('Restricted access');
20:
21: /**
22: * Xcache storage engine for cache.
23: *
24: *
25: * PHP versions 4 and 5
26: *
27: * CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/>
28: * Copyright 2005-2008, Cake Software Foundation, Inc.
29: * 1785 E. Sahara Avenue, Suite 490-204
30: * Las Vegas, Nevada 89104
31: *
32: * Licensed under The MIT License
33: * Redistributions of files must retain the above copyright notice.
34: *
35: * @filesource
36: * @copyright Copyright 2005-2008, Cake Software Foundation, Inc.
37: * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
38: * @package cake
39: * @subpackage cake.cake.libs.cache
40: * @since CakePHP(tm) v 1.2.0.4947
41: * @modifiedby $LastChangedBy: beckmi $
42: * @lastmodified $Date: 2015-06-06 17:59:41 -0400 (Sat, 06 Jun 2015) $
43: * @license http://www.opensource.org/licenses/mit-license.php The MIT License
44: */
45:
46: /**
47: * Xcache storage engine for cache
48: *
49: * @link http://trac.lighttpd.net/xcache/ Xcache
50: * @package cake
51: * @subpackage cake.cake.libs.cache
52: */
53: class XoopsCacheXcache extends XoopsCacheEngine
54: {
55: /**
56: * settings
57: * PHP_AUTH_USER = xcache.admin.user, default cake
58: * PHP_AUTH_PW = xcache.admin.password, default cake
59: *
60: * @var array
61: * @access public
62: */
63: public $settings = array();
64:
65: /**
66: * Initialize the Cache Engine
67: *
68: * Called automatically by the cache frontend
69: * To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array());
70: *
71: * @param array $settings array of setting for the engine
72: * @return boolean True if the engine has been successfully initialized, false if not
73: * @access public
74: */
75: public function init($settings = array())
76: {
77: parent::init($settings);
78: $defaults = array('PHP_AUTH_USER' => 'cake', 'PHP_AUTH_PW' => 'cake');
79: $this->settings = array_merge($defaults, $this->settings);
80:
81: return function_exists('xcache_info');
82: }
83:
84: /**
85: * Write data for key into cache
86: *
87: * @param string $key Identifier for the data
88: * @param mixed $value Data to be cached
89: * @param integer $duration How long to cache the data, in seconds
90: * @return boolean True if the data was successfully cached, false on failure
91: * @access public
92: */
93: public function write($key, $value, $duration = null)
94: {
95: return xcache_set($key, $value, $duration);
96: }
97:
98: /**
99: * Read a key from the cache
100: *
101: * @param string $key Identifier for the data
102: * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
103: * @access public
104: */
105: public function read($key)
106: {
107: if (xcache_isset($key)) {
108: return xcache_get($key);
109: }
110:
111: return false;
112: }
113:
114: /**
115: * Delete a key from the cache
116: *
117: * @param string $key Identifier for the data
118: * @return boolean True if the value was successfully deleted, false if it didn't exist or couldn't be removed
119: * @access public
120: */
121: public function delete($key)
122: {
123: return xcache_unset($key);
124: }
125:
126: /**
127: * Delete all keys from the cache
128: *
129: * @return boolean True if the cache was successfully cleared, false otherwise
130: * @access public
131: */
132: public function clear($check = null)
133: {
134: $result = true;
135: $this->__auth();
136: for ($i = 0, $max = xcache_count(XC_TYPE_VAR); $i < $max; ++$i) {
137: if (!xcache_clear_cache(XC_TYPE_VAR, $i)) {
138: $result = false;
139: break;
140: }
141: }
142: $this->__auth(true);
143:
144: return $result;
145: }
146:
147: /**
148: * Populates and reverses $_SERVER authentication values
149: * Makes necessary changes (and reverting them back) in $_SERVER
150: *
151: * This has to be done because xcache_clear_cache() needs to pass Basic Http Auth
152: * (see xcache.admin configuration settings)
153: *
154: * @param bool $reverse Revert changes
155: * @access private
156: */
157: private function __auth($reverse = false)
158: {
159: static $backup = array();
160: $keys = array('PHP_AUTH_USER', 'PHP_AUTH_PW');
161: foreach ($keys as $key) {
162: if ($reverse) {
163: if (isset($backup[$key])) {
164: $_SERVER[$key] = $backup[$key];
165: unset($backup[$key]);
166: } else {
167: unset($_SERVER[$key]);
168: }
169: } else {
170: $value = env($key);
171: if (!empty($value)) {
172: $backup[$key] = $value;
173: }
174: $varName = '__' . $key;
175: $_SERVER[$key] = $this->settings[$varName];
176: }
177: }
178: }
179: }
180: