PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/class_settings.php

http://github.com/MightyGorgon/icy_phoenix
PHP | 500 lines | 347 code | 68 blank | 85 comment | 78 complexity | ad54416da2497ca38869f50d78c60636 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. *
  4. * @package Icy Phoenix
  5. * @version $Id$
  6. * @copyright (c) 2008 Icy Phoenix
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. if (!defined('IN_ICYPHOENIX'))
  11. {
  12. die('Hacking attempt');
  13. }
  14. define('BOARD_ADMIN', 98);
  15. /**
  16. * Settings management
  17. */
  18. class class_settings
  19. {
  20. var $modules = array();
  21. var $list_yes_no = array('Yes' => 1, 'No' => 0);
  22. var $list_time_intervals = array(
  23. 'Cron_Disabled' => 0,
  24. '15M' => 900,
  25. '30M' => 1800,
  26. '1H' => 3600,
  27. '2H' => 7200,
  28. '3H' => 10800,
  29. '6H' => 21600,
  30. '12H' => 43200,
  31. '1D' => 86400,
  32. '3D' => 259200,
  33. '7D' => 604800,
  34. '14D' => 1209600,
  35. '30D' => 2592000,
  36. );
  37. /**
  38. * Construct
  39. */
  40. function __construct()
  41. {
  42. }
  43. /**
  44. * Setup settings
  45. */
  46. function setup_settings()
  47. {
  48. global $db, $cache, $config, $lang;
  49. // Get all settings
  50. $this->modules = array();
  51. foreach ($cache->obtain_settings() as $settings_file)
  52. {
  53. @include(IP_ROOT_PATH . 'includes/' . SETTINGS_PATH . $settings_file . '.' . PHP_EXT);
  54. }
  55. return true;
  56. }
  57. /**
  58. * Set config
  59. */
  60. function set_config($config_name, $config_value, $clear_cache = false, $return = false)
  61. {
  62. global $db, $cache, $config, $lang;
  63. set_config($config_name, $config_value, false, $return);
  64. if ($clear_cache)
  65. {
  66. $this->cache_clear();
  67. }
  68. }
  69. /**
  70. * Remove plugin config
  71. */
  72. function remove_config($config_name, $config_value, $clear_cache = true, $return = false)
  73. {
  74. global $db, $cache, $config, $lang;
  75. $sql = "DELETE FROM " . CONFIG_TABLE . " WHERE config_name = '" . $db->sql_escape($config_name) . "'";
  76. $db->sql_return_on_error($return);
  77. $db->sql_query($sql);
  78. $db->sql_return_on_error(false);
  79. if ($clear_cache)
  80. {
  81. $this->cache_clear();
  82. }
  83. }
  84. /*
  85. * Get the user config if defined
  86. */
  87. function user_config_key($key, $user_field = '', $over_field = '')
  88. {
  89. global $config, $user;
  90. // Get the user fields name if not given
  91. if (empty($user_field))
  92. {
  93. $user_field = 'user_' . $key;
  94. }
  95. // Get the overwrite allowed switch name if not given
  96. if (empty($over_field))
  97. {
  98. $over_field = $key . '_over';
  99. }
  100. // Does the key exists?
  101. if (!isset($config[$key])) return;
  102. // Does the user field exists ?
  103. if (!isset($user->data[$user_field])) return;
  104. // Does the overwrite switch exists?
  105. if (!isset($config[$over_field]))
  106. {
  107. $config[$over_field] = 0; // no overwrite
  108. }
  109. // Overwrite with the user data only if not overwrite set, not anonymous, logged in
  110. // If the user is admin we will not overwrite his setting either...
  111. if ((!intval($config[$over_field]) && ($user->data['user_id'] != ANONYMOUS) && $user->data['session_logged_in']) || ($user->data['user_level'] == ADMIN))
  112. {
  113. $config[$key] = $user->data[$user_field];
  114. }
  115. else
  116. {
  117. $user->data[$user_field] = $config[$key];
  118. }
  119. }
  120. /*
  121. * Initialize configuration
  122. */
  123. function init_config($settings_details, $settings_data)
  124. {
  125. global $db, $cache, $config, $lang;
  126. $this->modules[$settings_details['menu_name']]['data'][$settings_details['name']]['id'] = array($settings_details['id'] => $settings_details['name']);
  127. @reset($settings_data);
  128. while (list($config_key, $config_data) = each($settings_data))
  129. {
  130. if (!isset($config_data['user_only']) || !$config_data['user_only'])
  131. {
  132. // Create the key value
  133. $config_value = (isset($config_data['values'][$config_data['default']]) ? $config_data['values'][$config_data['default']] : $config_data['default']);
  134. if (!isset($config[$config_key]))
  135. {
  136. $this->set_config($config_key, $config_value, true, true);
  137. }
  138. if (!empty($config_data['user']))
  139. {
  140. $config_key_over = $config_key . '_over';
  141. if (!isset($config[$config_key_over]))
  142. {
  143. // Create the "overwrite user choice" value
  144. $this->set_config($config_key_over, 0, true, true);
  145. }
  146. // Get user choice value
  147. $this->user_config_key($config_key, $config_data['user']);
  148. }
  149. }
  150. // Deliver it for input only if not hidden
  151. if (!isset($config_data['hide']) || !$config_data['hide'])
  152. {
  153. $this->modules[$settings_details['menu_name']]['data'][$settings_details['name']]['data'][$settings_details['sub_name']]['data'][$config_key] = $config_data;
  154. // Sort values: overwrite only if not yet provided
  155. if (empty($this->modules[$settings_details['menu_name']]['sort']) || ($this->modules[$settings_details['menu_name']]['sort'] == 0))
  156. {
  157. $this->modules[$settings_details['menu_name']]['sort'] = $settings_details['menu_sort'];
  158. }
  159. if (empty($this->modules[$settings_details['menu_name']]['data'][$settings_details['name']]['sort']) || ($this->modules[$settings_details['menu_name']]['data'][$settings_details['name']]['sort'] == 0))
  160. {
  161. $this->modules[$settings_details['menu_name']]['data'][$settings_details['name']]['sort'] = $settings_details['sort'];
  162. }
  163. if (empty($this->modules[$settings_details['menu_name']]['data'][$settings_details['name']]['data'][$settings_details['sub_name']]['sort']) || ($this->modules[$settings_details['menu_name']]['data'][$settings_details['name']]['data'][$settings_details['sub_name']]['sort'] == 0))
  164. {
  165. $this->modules[$settings_details['menu_name']]['data'][$settings_details['name']]['data'][$settings_details['sub_name']]['sort'] = $settings_details['sub_sort'];
  166. }
  167. }
  168. }
  169. if ($settings_details['clear_cache'])
  170. {
  171. $this->cache_clear();
  172. }
  173. }
  174. /*
  175. * Process settings modules
  176. */
  177. function process_settings_modules($settings_modules, $in_acp = true, $target_userdata = false)
  178. {
  179. // Menu
  180. $menu_id = array();
  181. $menu_keys = array();
  182. $menu_sort = array();
  183. // Mod ID
  184. $mod_id = array();
  185. $mod_keys = array();
  186. $mod_sort = array();
  187. // Fields
  188. $sub_id = array();
  189. $sub_keys = array();
  190. $sub_sort = array();
  191. // Process
  192. @reset($settings_modules);
  193. while (list($menu_name, $menu) = each($settings_modules))
  194. {
  195. // Check if there are some config fields in the mods under this menu
  196. $found = false;
  197. // Menu
  198. @reset($menu['data']);
  199. while ((list($mod_name, $mod) = @each($menu['data'])) && !$found)
  200. {
  201. // Sub menu
  202. @reset($mod['data']);
  203. while ((list($sub_name, $sub) = @each($mod['data'])) && !$found)
  204. {
  205. // Fields
  206. @reset($sub['data']);
  207. while ((list($field_name, $field) = @each($sub['data'])) && !$found)
  208. {
  209. if ($this->is_auth_display($field_name, $field, $in_acp, $target_userdata))
  210. {
  211. $found = true;
  212. break;
  213. }
  214. }
  215. }
  216. }
  217. // Menu ok
  218. if ($found)
  219. {
  220. $i = sizeof($menu_keys);
  221. $menu_id[$i] = !empty($menu['id']) ? $menu['id'] : '';
  222. $menu_keys[$i] = $menu_name;
  223. $menu_sort[$i] = $menu['sort'];
  224. // Init mod level
  225. $mod_id[$i] = array();
  226. $mod_keys[$i] = array();
  227. $mod_sort[$i] = array();
  228. @reset($menu['data']);
  229. while (list($mod_name, $mod) = @each($menu['data']))
  230. {
  231. // Check if there are some config fields
  232. $found = false;
  233. @reset($mod['data']);
  234. while (list($sub_name, $sub) = @each($mod['data']))
  235. {
  236. @reset($sub['data']);
  237. while (list($field_name, $field) = @each($sub['data']))
  238. {
  239. if ($this->is_auth_display($field_name, $field, $in_acp, $target_userdata))
  240. {
  241. $found = true;
  242. break;
  243. }
  244. }
  245. }
  246. if ($found)
  247. {
  248. $j = sizeof($mod_keys[$i]);
  249. $mod_id[$i][$j] = !empty($mod['id']) ? $mod['id'] : '';
  250. $mod_keys[$i][$j] = $mod_name;
  251. $mod_sort[$i][$j] = $mod['sort'];
  252. // Init sub levels
  253. $sub_id[$i][$j] = array();
  254. $sub_keys[$i][$j] = array();
  255. $sub_sort[$i][$j] = array();
  256. // Sub names
  257. @reset($mod['data']);
  258. while (list($sub_name, $sub) = @each($mod['data']))
  259. {
  260. if (!empty($sub_name))
  261. {
  262. // Check if there is some config fields in this level
  263. $found = false;
  264. @reset($sub['data']);
  265. while (list($field_name, $field) = @each($sub['data']))
  266. {
  267. if ($this->is_auth_display($field_name, $field, $in_acp, $target_userdata))
  268. {
  269. $found = true;
  270. break;
  271. }
  272. }
  273. if ($found)
  274. {
  275. $sub_id[$i][$j][] = $sub['id'];
  276. $sub_keys[$i][$j][] = $sub_name;
  277. $sub_sort[$i][$j][] = $sub['sort'];
  278. }
  279. }
  280. }
  281. @array_multisort($sub_sort[$i][$j], $sub_keys[$i][$j]);
  282. }
  283. }
  284. @array_multisort($mod_sort[$i], $mod_keys[$i], $sub_sort[$i], $sub_keys[$i]);
  285. }
  286. }
  287. @array_multisort($menu_sort, $menu_keys, $mod_sort, $mod_keys, $sub_sort, $sub_keys);
  288. $return_values = array();
  289. $return_values = array(
  290. 'menu_id' => $menu_id,
  291. 'menu_keys' => $menu_keys,
  292. 'menu_sort' => $menu_sort,
  293. 'mod_id' => $mod_id,
  294. 'mod_keys' => $mod_keys,
  295. 'mod_sort' => $mod_sort,
  296. 'sub_id' => $sub_id,
  297. 'sub_keys' => $sub_keys,
  298. 'sub_sort' => $sub_sort,
  299. );
  300. return $return_values;
  301. }
  302. /*
  303. * Check if user is permitted to access the data
  304. */
  305. function is_auth($level_required)
  306. {
  307. global $user;
  308. $return = false;
  309. // add also JUNIOR_ADMIN?
  310. //if (($user->data['user_level'] == ADMIN) || ($user->data['user_level'] == JUNIOR_ADMIN))
  311. if ($user->data['user_level'] == ADMIN)
  312. {
  313. $return = true;
  314. }
  315. elseif (($level_required == USER) || empty($level_required))
  316. {
  317. $return = true;
  318. }
  319. return $return;
  320. }
  321. /*
  322. * Check if the field is to be displayed or not
  323. */
  324. function is_auth_display($config_name, $config_data, $in_acp = true, $target_userdata = false)
  325. {
  326. global $config, $user;
  327. $return = false;
  328. if ($in_acp || empty($target_userdata))
  329. {
  330. if (!isset($config_data['user_only']) || !$config_data['user_only'])
  331. {
  332. $return = true;
  333. }
  334. }
  335. else
  336. {
  337. if (((!empty($config_data['user']) && isset($target_userdata[$config_data['user']]) && (!$config[$config_name . '_over'] || ($user->data['user_level'] == ADMIN))) || $config_data['system']) && $this->is_auth($config_data['auth']))
  338. {
  339. $return = true;
  340. }
  341. }
  342. return $return;
  343. }
  344. /*
  345. * Get template file: check if template file exists and set the correct path to template file
  346. */
  347. function get_tpl_file($tpl_base_path, $tpl_file)
  348. {
  349. global $theme;
  350. $tpl_path = $tpl_base_path . 'default/' . $tpl_file;
  351. $tpl_temp_file = $tpl_base_path . $theme['template_name'] . '/' . $tpl_file;
  352. if (file_exists($tpl_temp_file))
  353. {
  354. $tpl_path = $tpl_temp_file;
  355. }
  356. return $tpl_path;
  357. }
  358. /*
  359. * Get lang var
  360. */
  361. function get_lang($key)
  362. {
  363. global $lang;
  364. return ((!empty($key) && isset($lang[$key])) ? $lang[$key] : $key);
  365. }
  366. /**
  367. * Setup modules
  368. */
  369. function setup_modules($modules_path, $modules_prefix = 'settings_')
  370. {
  371. global $db, $cache, $config, $lang;
  372. // We need to reset the modules to avoid modules in memory to be parsed again
  373. $this->modules = array();
  374. $modules_path = IP_ROOT_PATH . 'includes/' . SETTINGS_PATH . (!empty($modules_path) ? (trim(basename($modules_path)) . '/') : '');
  375. $modules_prefix = (empty($modules_prefix) ? 'settings_' : trim(basename($modules_prefix)));
  376. // Search for modules...
  377. if (@is_dir($modules_path))
  378. {
  379. $dir = @opendir($modules_path);
  380. if ($dir)
  381. {
  382. while (($file = @readdir($dir)) !== false)
  383. {
  384. if ((strpos($file, $modules_prefix) === 0) && (substr($file, -(strlen(PHP_EXT) + 1)) === '.' . PHP_EXT))
  385. {
  386. @include($modules_path . $file);
  387. }
  388. }
  389. @closedir($dir);
  390. }
  391. }
  392. return true;
  393. }
  394. /**
  395. * Obtain lang files...
  396. */
  397. function obtain_lang_files($suffix = 'settings_')
  398. {
  399. global $cache, $config;
  400. $suffix = !empty($suffix) ? trim(basename($suffix)) : 'settings_';
  401. if (($lang_files = $cache->get('_lang_' . $suffix . $config['default_lang'])) === false)
  402. {
  403. $lang_files = array();
  404. // Now search for langs...
  405. $dir = @opendir(IP_ROOT_PATH . 'language/lang_' . $config['default_lang'] . '/');
  406. if ($dir)
  407. {
  408. while (($file = @readdir($dir)) !== false)
  409. {
  410. if ((strpos($file, 'lang_' . $suffix) === 0) && (substr($file, -(strlen(PHP_EXT) + 1)) === '.' . PHP_EXT))
  411. {
  412. $lang_files[] = substr($file, 0, -(strlen(PHP_EXT) + 1));
  413. }
  414. }
  415. @closedir($dir);
  416. }
  417. $cache->put('_lang_' . $suffix . $config['default_lang'], $lang_files);
  418. }
  419. return $lang_files;
  420. }
  421. /**
  422. * Cache clear
  423. */
  424. function cache_clear()
  425. {
  426. global $db, $cache, $config, $lang;
  427. $cache->destroy('config');
  428. $db->clear_cache('config_');
  429. return true;
  430. }
  431. }
  432. ?>