1: <?php
2:
3: use Xoops\Core\FixedGroups;
4:
5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15:
16:
17: if (!defined("FRAMEWORKS_ART_FUNCTIONS_CACHE")):
18: define("FRAMEWORKS_ART_FUNCTIONS_CACHE", true);
19:
20: 21: 22: 23:
24: function mod_generateCacheId_byGroup($groups = null)
25: {
26: $xoops = Xoops::getInstance();
27:
28: if (!empty($groups) && is_array($groups)) {
29: } elseif ($xoops->isUser()) {
30: $groups = $xoops->user->getGroups();
31: }
32: if (!empty($groups) && is_array($groups)) {
33: sort($groups);
34: $contentCacheId = substr(md5(implode(",", $groups) . \XoopsBaseConfig::get('db-pass') . \XoopsBaseConfig::get('db-name')), 0, strlen(\XoopsBaseConfig::get('db-user')) * 2);
35: } else {
36: $contentCacheId = FixedGroups::ANONYMOUS;
37: }
38:
39: return $contentCacheId;
40: }
41:
42: 43: 44: 45:
46: function mod_generateCacheId($groups = null)
47: {
48: return mod_generateCacheId_byGroup($groups);
49: }
50:
51: 52: 53: 54: 55: 56: 57:
58: function mod_createFile($data, $name = null, $dirname = null, $cache_path = null)
59: {
60: $xoops = Xoops::getInstance();
61:
62: $name = ($name) ? $name : (string)(time());
63: $dirname = ($dirname) ? $dirname : $xoops->moduleDirname;
64: $cache_path = ($cache_path) ? $cache_path : \XoopsBaseConfig::get('caches-path');
65:
66: $key = "{$dirname}_{$name}";
67: return \Xoops\Cache::write($key, $data);
68: }
69:
70: 71: 72: 73: 74: 75:
76: function mod_createCacheFile($data, $name = null, $dirname = null)
77: {
78: return mod_createFile($data, $name, $dirname);
79: }
80:
81: 82: 83: 84: 85: 86: 87:
88: function mod_createCacheFile_byGroup($data, $name = null, $dirname = null, $groups = null)
89: {
90: $name .= mod_generateCacheId_byGroup();
91: return mod_createCacheFile($data, $name, $dirname);
92: }
93:
94: 95: 96: 97: 98: 99:
100: function mod_loadFile($name, $dirname = null, $cache_path = null)
101: {
102: $xoops = Xoops::getInstance();
103:
104: $data = null;
105:
106: if (empty($name)) {
107: return $data;
108: }
109: $dirname = ($dirname) ? $dirname : $xoops->moduleDirname;
110: $cache_path = ($cache_path) ? $cache_path : $xoops->globalData->getVar('cache-path');
111:
112: $key = "{$dirname}_{$name}";
113: return \Xoops\Cache::read($key);
114: }
115:
116: 117: 118: 119: 120:
121: function mod_loadCacheFile($name, $dirname = null)
122: {
123: $data = mod_loadFile($name, $dirname);
124: return $data;
125: }
126:
127: 128: 129: 130: 131: 132:
133: function mod_loadCacheFile_byGroup($name, $dirname = null, $groups = null)
134: {
135: $name .= mod_generateCacheId_byGroup();
136: $data = mod_loadFile($name, $dirname);
137: return $data;
138: }
139:
140:
141: 142: 143: 144: 145: 146:
147: function mod_clearFile($name = "", $dirname = null, $cache_path = null)
148: {
149: $xoops = \Xoops::getInstance();
150: if (empty($dirname)) {
151: $cache_path = ($cache_path) ? $cache_path : \XoopsBaseConfig::get('caches-path');
152: $pattern = ($dirname) ? "{$dirname}_{$name}.*\.php" : "[^_]+_{$name}.*\.php";
153: if ($handle = opendir($cache_path)) {
154: while (false !== ($file = readdir($handle))) {
155: if (is_file($cache_path . '/' . $file) && preg_match("/{$pattern}$/", $file)) {
156: @unlink($cache_path . '/' . $file);
157: }
158: }
159: closedir($handle);
160: }
161: } else {
162: $files = (array)glob($root_path . "/*{$dirname}_{$name}*.php");
163: foreach ($files as $file) {
164: @unlink($file);
165: }
166: }
167: return true;
168: }
169:
170: 171: 172: 173: 174:
175: function mod_clearCacheFile($name = "", $dirname = null)
176: {
177: return mod_clearFile($name, $dirname);
178: }
179:
180: 181: 182: 183:
184: function mod_clearSmartyCache($pattern = "")
185: {
186: $xoops = Xoops::getInstance();
187:
188: if (empty($pattern)) {
189: $dirname = $xoops->moduleDirname;
190: $pattern = "/(^{$dirname}\^.*\.html$|blk_{$dirname}_.*[^\.]*\.html$)/";
191: }
192: $cache_path = $xoops->globalData->getVar('cache-path');
193: if ($handle = opendir($cache_path)) {
194: while (false !== ($file = readdir($handle))) {
195: if (is_file($cache_path . '/' . $file) && preg_match($pattern, $file)) {
196: @unlink($cache_path . '/' . $file);
197: }
198: }
199: closedir($handle);
200: }
201: return true;
202: }
203:
204: endif;
205: