1: <?php
2: /*
3: You may not change or alter any portion of this comment or credits
4: of supporting developers from this source code or any supporting source code
5: which is considered copyrighted (c) material of the original comment or credit authors.
6:
7: This program is distributed in the hope that it will be useful,
8: but WITHOUT ANY WARRANTY; without even the implied warranty of
9: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10: */
11:
12: /**
13: * System module
14: *
15: * @package System
16: * @author Andricq Nicolas (AKA MusS)
17: * @copyright XOOPS Project (http://xoops.org)
18: * @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
19: */
20: class System
21: {
22:
23: /**
24: * @var null|SystemModule
25: */
26: public $module = null;
27:
28: /**
29: * @var null|Xoops
30: */
31: private $xoops = null;
32:
33: /**
34: * Actual System Module
35: */
36: private function __construct()
37: {
38: $this->xoops = Xoops::getInstance();
39: }
40:
41: /**
42: * Access the only instance of this class
43: *
44: * @return System
45: */
46: public static function getInstance()
47: {
48: static $instance;
49: if (!isset($instance)) {
50: $class = __CLASS__;
51: $instance = new $class();
52: }
53: return $instance;
54: }
55:
56: /**
57: * @return bool
58: */
59: public function checkRight()
60: {
61: if ($this->xoops->isUser()) {
62: $this->xoops->module = $this->xoops->getModuleByDirname('system');
63: if (!$this->xoops->user->isAdmin($this->xoops->module->mid())) {
64: return false;
65: }
66: } else {
67: return false;
68: }
69: return true;
70: }
71:
72: /**
73: * @param $global
74: * @param $key
75: * @param string $default
76: * @param string $type
77: * @return int|mixed|string
78: */
79: public function cleanVars(&$global, $key, $default = '', $type = 'int')
80: {
81: switch ($type) {
82: case 'array':
83: $ret = (isset($global[$key]) && is_array($global[$key])) ? $global[$key] : $default;
84: break;
85: case 'date':
86: $ret = (isset($global[$key])) ? strtotime($global[$key]) : $default;
87: break;
88: case 'string':
89: $ret = (isset($global[$key])) ? filter_var($global[$key], FILTER_SANITIZE_MAGIC_QUOTES) : $default;
90: break;
91: case 'int':
92: default:
93: $ret = (isset($global[$key])) ? filter_var($global[$key], FILTER_SANITIZE_NUMBER_INT) : $default;
94: break;
95: }
96: if ($ret === false) {
97: return $default;
98: }
99: return $ret;
100: }
101:
102: /**
103: * System language loader wrapper
104: *
105: * @param string $name Name of language file to be loaded, without extension
106: * @param string $domain Module dirname; global language file will be loaded
107: * if $domain is set to 'global' or not specified
108: * @param string $language Language to be loaded, current language content will
109: * be loaded if not specified
110: * @return boolean
111: * @todo expand domain to multiple categories, e.g. module:system, framework:filter, etc.
112: *
113: */
114: public function loadLanguage($name, $domain = '', $language = null)
115: {
116: $xoops = Xoops::getInstance();
117: /**
118: * We must check later for an empty value. As xoops_getPageOption could be empty
119: */
120: if (empty($name)) {
121: return false;
122: }
123: $language = empty($language) ? $xoops->getConfig('language') : $language;
124: $path = 'modules/' . $domain . '/language/';
125: if (XoopsLoad::fileExists($file = $xoops->path($path . $language . '/admin/' . $name . '.php'))) {
126: $ret = include_once $file;
127: } else {
128: $ret = include_once $xoops->path($path . 'english/admin/' . $name . '.php');
129: }
130: return $ret;
131: }
132:
133: /**
134: * @param string $version
135: * @param string $value
136: * @return string
137: */
138: public function adminVersion($version, $value = '')
139: {
140: static $tblVersion = array();
141: if (is_array($tblVersion) && array_key_exists($version . '.' . $value, $tblVersion)) {
142: return $tblVersion[$version . '.' . $value];
143: }
144: $xoops = Xoops::getInstance();
145: $path = $xoops->path('modules/system/admin/' . $version . '/xoops_version.php');
146: if (XoopsLoad::fileExists($path)) {
147: $modversion = array();
148: include $path;
149: $retvalue = $modversion[$value];
150: $tblVersion[$version . '.' . $value] = $retvalue;
151: return $retvalue;
152: }
153: return '';
154: }
155:
156: /**
157: * System Clean cache 'xoops_data/caches/'
158: *
159: * @param integer[] $cache cache caches to be cleaned
160: * - 1 = Smarty cache
161: * - 2 = Smarty compile
162: * - 3 = cache
163: *
164: * Note: clearing of the actual cache is now handled by the cache system.
165: * Only the 'default' cache is cleaned by this function. This function returns
166: * only a boolean status, not a count as done before.
167: *
168: * The old behavior of cleaning the directory is maintained, but does not
169: * clean the actual cache, only artifacts of legacy programs that write files
170: * directly in the xoops_cache folder.
171: *
172: * @return array|false associative array of status/count for each cache
173: */
174: public function cleanCache($cache)
175: {
176: $cachePath = \XoopsBaseConfig::get('var-path') . '/caches/';
177: $total_smarty_cache = 0;
178: $total_smarty_compile = 0;
179: $total_xoops_cache = 0;
180: if (!empty($cache)) {
181: for ($i = 0; $i < count($cache); ++$i) {
182: switch ($cache[$i]) {
183: case 1:
184: $files = glob($cachePath . 'smarty_cache/*.*');
185: $total_smarty_cache = 0;
186: foreach ($files as $filename) {
187: if (basename(strtolower($filename)) !== 'index.html') {
188: unlink($filename);
189: ++$total_smarty_cache;
190: }
191: }
192: break;
193:
194: case 2:
195: $files = glob($cachePath . 'smarty_compile/*.*');
196: $total_smarty_compile = 0;
197: foreach ($files as $filename) {
198: if (basename(strtolower($filename)) !== 'index.html') {
199: unlink($filename);
200: ++$total_smarty_compile;
201: }
202: }
203: break;
204:
205: case 3:
206: // ask the cache to clear itself
207: $status = Xoops::getInstance()->cache()->delete('system');
208: // this section captures legacy cache use only
209: $files = glob($cachePath . 'xoops_cache/*.*');
210: $total_xoops_cache = 0;
211: foreach ($files as $filename) {
212: if (basename(strtolower($filename)) != 'index.html') {
213: unlink($filename);
214: ++$total_xoops_cache;
215: }
216: }
217: $total_xoops_cache = $status || ($total_xoops_cache>0);
218: break;
219: }
220: }
221: $ret['smarty_cache'] = $total_smarty_cache;
222: $ret['smarty_compile'] = $total_smarty_compile;
223: $ret['xoops_cache'] = $total_xoops_cache;
224: return $ret;
225: } else {
226: return false;
227: }
228: }
229: }
230: