PageRenderTime 26ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/class_cms.php

http://github.com/MightyGorgon/icy_phoenix
PHP | 486 lines | 385 code | 32 blank | 69 comment | 55 complexity | 26075ed750e134220d3ccf74dc47df97 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. /**
  15. * CMS class
  16. */
  17. class ip_cms
  18. {
  19. var $tables = array();
  20. /**
  21. * Construct
  22. */
  23. function __construct()
  24. {
  25. }
  26. /*
  27. * Initialize variables
  28. */
  29. function init_vars()
  30. {
  31. if (defined('IN_CMS_USERS'))
  32. {
  33. $this->tables = array(
  34. 'blocks_table' => CMS_USERS_BLOCKS_TABLE,
  35. 'block_settings_table' => CMS_USERS_BLOCK_SETTINGS_TABLE,
  36. 'block_position_table' => CMS_USERS_BLOCK_POSITION_TABLE,
  37. 'block_config_table' => CMS_USERS_CONFIG_TABLE,
  38. 'block_variable_table' => CMS_USERS_BLOCK_VARIABLE_TABLE,
  39. 'layout_table' => CMS_USERS_LAYOUT_TABLE,
  40. );
  41. }
  42. else
  43. {
  44. $this->tables = array(
  45. 'blocks_table' => CMS_BLOCKS_TABLE,
  46. 'block_settings_table' => CMS_BLOCK_SETTINGS_TABLE,
  47. 'block_position_table' => CMS_BLOCK_POSITION_TABLE,
  48. 'block_config_table' => CMS_CONFIG_TABLE,
  49. 'block_variable_table' => CMS_BLOCK_VARIABLE_TABLE,
  50. 'layout_table' => CMS_LAYOUT_TABLE,
  51. 'layout_special_table' => CMS_LAYOUT_SPECIAL_TABLE,
  52. );
  53. }
  54. return true;
  55. }
  56. /*
  57. * Checks if the user is allowed to view the element
  58. */
  59. function cms_auth_view()
  60. {
  61. global $user, $config;
  62. /*
  63. * Move these to constants if you want to use...
  64. * define('CMS_AUTH_ALL', 0); // Everyone
  65. * define('CMS_AUTH_GUESTS_ONLY', 1); // Guests Only (Registered won't see this!)
  66. * define('CMS_AUTH_REG', 2); // Registered Users Only
  67. * define('CMS_AUTH_MOD', 3); // Moderators And Admins
  68. * define('CMS_AUTH_ADMIN', 4); // Admins Only
  69. * define('CMS_AUTH_FOUNDER', 5); // Founders Only (NOT USED)
  70. * define('CMS_AUTH_ALL_NO_BOTS', 8); // Everyone but BOTs
  71. */
  72. if (empty($user->data['session_logged_in']))
  73. {
  74. if ($user->data['is_bot'])
  75. {
  76. $result = (!empty($config['bots_reg_auth']) ? array(0, 1, 2) : array(0, 1));
  77. }
  78. else
  79. {
  80. $result = array(0, 1, 8);
  81. }
  82. }
  83. else
  84. {
  85. // User is not a guest here...
  86. switch($user->data['user_level'])
  87. {
  88. case ADMIN:
  89. // If you want admin to see also GUEST ONLY blocks you need to use these settings...
  90. //$result = array(0, 1, 2, 3, 4, 5, 8);
  91. $result = array(0, 2, 3, 4, 5, 8);
  92. break;
  93. case MOD:
  94. $result = array(0, 2, 3, 8);
  95. break;
  96. default:
  97. $result = array(0, 2, 8);
  98. break;
  99. }
  100. }
  101. return $result;
  102. }
  103. /*
  104. * Creates a list of all groups
  105. */
  106. function cms_groups($user_id)
  107. {
  108. global $db;
  109. static $layout_groups;
  110. if(!isset($layout_groups))
  111. {
  112. $sql = "SELECT group_id FROM " . USER_GROUP_TABLE . " WHERE user_id = '" . $user_id . "' AND user_pending = 0";
  113. $result = $db->sql_query($sql);
  114. $layout_groups = array();
  115. $i = 0;
  116. while ($row = $db->sql_fetchrow($result))
  117. {
  118. $layout_groups[$i] = intval($row['group_id']);
  119. $i++;
  120. }
  121. $db->sql_freeresult($result);
  122. }
  123. return $layout_groups;
  124. }
  125. /*
  126. * Blocks parsing function
  127. */
  128. function cms_parse_blocks($layout, $is_special = false, $global_blocks = false, $type = '')
  129. {
  130. global $db, $cache, $config, $auth, $user, $lang, $bbcode, $template;
  131. global $class_plugins;
  132. global $cms_config_vars, $cms_config_layouts, $cms_config_global_blocks, $block_id;
  133. // Let's remove $auth->acl_get('a_') until I finish coding permissions properly... and also add/remove 'a_' when users are added/removed from administrators in ACP
  134. //$is_admin = (($user->data['user_level'] == ADMIN) || $auth->acl_get('a_')) ? true : false;
  135. $is_admin = ($user->data['user_level'] == ADMIN) ? true : false;
  136. $empty_block_tpl = 'cms_block_inc_wrapper.tpl';
  137. if(!$is_special)
  138. {
  139. $id_var_name = 'l_id';
  140. $table_name = $this->tables['layout_table'];
  141. $field_name = 'lid';
  142. }
  143. else
  144. {
  145. $id_var_name = 'ls_id';
  146. $table_name = $this->tables['layout_special_table'];
  147. $field_name = 'lsid';
  148. $layout = (isset($cms_config_layouts[$layout][$field_name]) ? $cms_config_layouts[$layout][$field_name] : 0);
  149. }
  150. if (!defined('CMS_BLOCKS_LANG_INCLUDED'))
  151. {
  152. // We add lang_user_created again here to make sure we override lang_blocks var with customized ones without having to edit lang_blocks directly...
  153. setup_extra_lang(array('lang_blocks', 'lang_user_created'));
  154. define('CMS_BLOCKS_LANG_INCLUDED', true);
  155. }
  156. if(!$global_blocks && !$is_special)
  157. {
  158. $layout_pos = array();
  159. $sql_pos = "SELECT * FROM " . $this->tables['block_position_table'] . " WHERE layout = " . $layout;
  160. $block_pos_result = $db->sql_query($sql_pos, 0, 'cms_bp_', CMS_CACHE_FOLDER);
  161. while ($block_pos_row = $db->sql_fetchrow($block_pos_result))
  162. {
  163. $layout_pos[$block_pos_row['bposition']] = $block_pos_row['pkey'];
  164. }
  165. $db->sql_freeresult($block_pos_result);
  166. }
  167. $block_info = array();
  168. if($is_special || $global_blocks)
  169. {
  170. $temp_type = $type;
  171. }
  172. else
  173. {
  174. $temp_type = 's' . strval($layout);
  175. }
  176. $is_global_block = false;
  177. $is_gh_block = false;
  178. if(!$is_special && !$global_blocks)
  179. {
  180. if (!empty($config['cms_version']))
  181. {
  182. $sql = "SELECT b.*, s.*
  183. FROM " . $this->tables['blocks_table'] . " AS b,
  184. " . $this->tables['block_settings_table'] . " AS s
  185. WHERE b.layout = " . $layout . "
  186. AND b.active = 1
  187. AND " . $db->sql_in_set('s.view', $this->cms_auth_view()) . "
  188. AND b.bposition NOT IN ('gh','gf','gt','gb','gl','gr','hh','hl','hc','fc','fr','ff')
  189. AND b.bs_id = s.bs_id
  190. ORDER BY b.bposition ASC, b.layout ASC, b.layout_special ASC, b.weight ASC";
  191. }
  192. else
  193. {
  194. $sql = "SELECT *
  195. FROM " . $this->tables['blocks_table'] . "
  196. WHERE layout = " . $layout . "
  197. AND active = 1
  198. AND " . $db->sql_in_set('view', $this->cms_auth_view()) . "
  199. AND bposition NOT IN ('gh','gf','gt','gb','gl','gr','hh','hl','hc','fc','fr','ff')
  200. ORDER BY bposition ASC, layout ASC, layout_special ASC, weight ASC";
  201. }
  202. $block_im_result = $db->sql_query($sql, 0, 'cms_blocks_', CMS_CACHE_FOLDER);
  203. $block_info = array();
  204. while ($row = $db->sql_fetchrow($block_im_result))
  205. {
  206. $block_info[] = $row;
  207. }
  208. $db->sql_freeresult($block_im_result);
  209. }
  210. else
  211. {
  212. switch ($type)
  213. {
  214. case 'gheader':
  215. $temp_pos = 'gh';
  216. break;
  217. case 'gfooter':
  218. $temp_pos = 'gf';
  219. break;
  220. case 'ghtop':
  221. $temp_pos = 'gt';
  222. $empty_block_tpl = 'cms_block_inc_wrapper_buttons.tpl';
  223. $is_gh_block = true;
  224. break;
  225. case 'ghbottom':
  226. $temp_pos = 'gb';
  227. $empty_block_tpl = 'cms_block_inc_wrapper_buttons.tpl';
  228. $is_gh_block = true;
  229. break;
  230. case 'ghleft':
  231. $temp_pos = 'gl';
  232. $empty_block_tpl = 'cms_block_inc_wrapper_plain.tpl';
  233. $is_gh_block = true;
  234. break;
  235. case 'ghright':
  236. $temp_pos = 'gr';
  237. $empty_block_tpl = 'cms_block_inc_wrapper_plain.tpl';
  238. $is_gh_block = true;
  239. break;
  240. case 'header':
  241. $temp_pos = 'hh';
  242. break;
  243. case 'headerleft':
  244. $temp_pos = 'hl';
  245. $is_global_block = true;
  246. break;
  247. case 'headercenter':
  248. $temp_pos = 'hc';
  249. $is_global_block = true;
  250. break;
  251. case 'tailcenter':
  252. $temp_pos = 'fc';
  253. $is_global_block = true;
  254. break;
  255. case 'tailright':
  256. $temp_pos = 'fr';
  257. $is_global_block = true;
  258. break;
  259. case 'tail':
  260. $temp_pos = 'ff';
  261. break;
  262. default:
  263. $temp_pos = 'tt';
  264. break;
  265. }
  266. $config['cms_block_pos'] = $temp_pos;
  267. if ($is_special && !$global_blocks)
  268. {
  269. $sql_where = "AND layout_special = " . $layout;
  270. $check_array = array($layout);
  271. }
  272. elseif ($is_special && $global_blocks && ($layout != 0))
  273. {
  274. $sql_where = "AND layout_special IN(0, " . $layout . ")";
  275. $check_array = array(0, $layout);
  276. }
  277. else
  278. {
  279. $sql_where = "AND layout_special = 0";
  280. $check_array = array(0);
  281. }
  282. if (empty($cms_config_global_blocks))
  283. {
  284. $cms_config_global_blocks = $cache->obtain_cms_global_blocks_config(false);
  285. }
  286. $block_info = array();
  287. if (!empty($cms_config_global_blocks[$temp_pos]))
  288. {
  289. foreach ($cms_config_global_blocks[$temp_pos] as $row)
  290. {
  291. if (in_array($row['layout_special'], $check_array))
  292. {
  293. $block_info[] = $row;
  294. }
  295. }
  296. }
  297. /*
  298. $sql = "SELECT *
  299. FROM " . CMS_BLOCKS_TABLE . "
  300. WHERE layout = 0
  301. " . $sql_where . "
  302. AND active = 1
  303. AND " . $db->sql_in_set('view', $this->cms_auth_view()) . "
  304. AND bposition = '" . $temp_pos . "'
  305. ORDER BY layout ASC, weight ASC";
  306. $block_im_result = $db->sql_query($sql, 0, 'cms_blocks_', CMS_CACHE_FOLDER);
  307. $block_info = array();
  308. while ($row = $db->sql_fetchrow($block_im_result))
  309. {
  310. $block_info[] = $row;
  311. }
  312. $db->sql_freeresult($block_im_result);
  313. */
  314. }
  315. $block_count = sizeof($block_info);
  316. if (($is_global_block || $is_gh_block) && ($block_count == 0))
  317. {
  318. return false;
  319. }
  320. for ($b_counter = 0; $b_counter < $block_count; $b_counter++)
  321. {
  322. // We cannot use 'bid' anymore since now blocks settings are identified by 'bs_id'
  323. //$block_id = $block_info[$b_counter]['bid'];
  324. $block_id = $block_info[$b_counter]['bs_id'];
  325. $is_group_allowed = true;
  326. if(!empty($block_info[$b_counter]['groups']))
  327. {
  328. $is_group_allowed = false;
  329. $group_content = explode(',', $block_info[$b_counter]['groups']);
  330. for ($i = 0; $i < sizeof($group_content); $i++)
  331. {
  332. if(in_array(intval($group_content[$i]), $this->cms_groups($user->data['user_id'])))
  333. {
  334. $is_group_allowed = true;
  335. }
  336. }
  337. }
  338. if($is_group_allowed)
  339. {
  340. if($is_special || $global_blocks)
  341. {
  342. $position = $type;
  343. }
  344. else
  345. {
  346. $position = $layout_pos[$block_info[$b_counter]['bposition']];
  347. }
  348. $position_prefix = $position . '_';
  349. $block_name = $block_info[$b_counter]['blockfile'];
  350. if(($block_info[$b_counter]['local'] == 1) && !empty($lang['cms_block_' . $block_name]))
  351. {
  352. $title_string = $lang['cms_block_' . $block_name];
  353. }
  354. else
  355. {
  356. $title_string = $block_info[$b_counter]['title'];
  357. }
  358. $content_type = 'block';
  359. if(!empty($block_info[$b_counter]['blockfile']))
  360. {
  361. $block_handle = $block_name . '_block_' . $block_info[$b_counter]['bid'];
  362. if (false !== strpos($block_name, '/'))
  363. {
  364. list($plugin_name, $block_name) = explode('/', $block_name);
  365. $plugin_config = $config['plugins'][$plugin_name];
  366. // do not render blocks from disabled plugins
  367. if (!$plugin_config['enabled'])
  368. {
  369. continue;
  370. }
  371. // Try to get the TPL path by "guessing" the constant.
  372. $tpl_constant_name = strtoupper($plugin_name) . '_TPL_PATH';
  373. if (defined($tpl_constant_name))
  374. {
  375. $tpl_dir = constant($tpl_constant_name);
  376. }
  377. else
  378. {
  379. $tpl_dir = IP_ROOT_PATH . PLUGINS_PATH . $plugin_config['dir'] . 'templates/';
  380. }
  381. $block_file = $class_plugins->get_tpl_file($tpl_dir, BLOCKS_DIR_NAME . $block_name . '_block.tpl');
  382. $block_php_file = IP_ROOT_PATH . PLUGINS_PATH . $plugin_config['dir'] . BLOCKS_DIR_NAME . $block_name;
  383. }
  384. else
  385. {
  386. $block_file = BLOCKS_DIR_NAME . $block_name . '_block.tpl';
  387. $block_php_file = IP_ROOT_PATH . 'blocks/' . $block_name;
  388. }
  389. $template->set_filenames(array($block_handle => $block_file));
  390. $output_block = '';
  391. include($block_php_file . '.' . PHP_EXT);
  392. $output_block = $template->get_var_from_handle($block_handle);
  393. }
  394. else
  395. {
  396. $content_type = 'text';
  397. $message = $block_info[$b_counter]['content'];
  398. if($block_info[$b_counter]['type'] == true)
  399. {
  400. if (!class_exists('bbcode') || empty($bbcode))
  401. {
  402. @include_once(IP_ROOT_PATH . 'includes/bbcode.' . PHP_EXT);
  403. }
  404. //$message = preg_replace('#(<)([\/]?.*?)(>)#is', "&lt;\\2&gt;", $message);
  405. $bbcode->allow_html = false;
  406. $bbcode->allow_bbcode = true;
  407. $bbcode->allow_smilies = true;
  408. $message = $bbcode->parse($message);
  409. //$message = str_replace("\n", "\n<br />\n", $message);
  410. $message = '<div class="post-text">' . $message . '</div>';
  411. }
  412. else
  413. {
  414. // You shouldn't convert NEW LINES to <br /> because you are parsing HTML, so linebreaks must be inserted as <br />
  415. // If you want linebreaks to be converted automatically, just decomment this line.
  416. //$message = str_replace("\n", "\n<br />\n", $message);
  417. }
  418. $output_block = $message;
  419. }
  420. $b_admin_vars = array();
  421. if ($is_admin || !empty($user->data['user_cms_auth']['cmsb_admin'][$block_id]))
  422. {
  423. $b_admin_vars = array(
  424. 'B_ADMIN' => true,
  425. 'B_EDIT_LINK' => append_sid(CMS_PAGE_CMS . '?mode=block_settings&amp;action=edit&amp;bs_id=' . $block_id . '&amp;sid=' . $user->data['session_id']),
  426. );
  427. }
  428. $block_handle = 'block_' . $block_info[$b_counter]['bid'];
  429. $template->set_filenames(array($block_handle => $empty_block_tpl));
  430. $template->assign_vars($b_admin_vars);
  431. $template->assign_vars(array(
  432. 'POSITION' => $position,
  433. 'CONTENT_TYPE' => $content_type,
  434. 'OUTPUT' => $output_block,
  435. 'TITLE_CONTENT' => (($title_string == '') ? '&nbsp;' : $title_string),
  436. 'TITLE' => (($block_info[$b_counter]['titlebar'] == 1) ? true : false),
  437. 'BORDER' => (($block_info[$b_counter]['border'] == 1) ? true : false),
  438. 'BACKGROUND' => (($block_info[$b_counter]['background'] == 1) ? true : false),
  439. )
  440. );
  441. $cms_block = $template->get_var_from_handle($block_handle);
  442. $template->assign_block_vars($position_prefix . 'blocks_row', $b_admin_vars);
  443. $template->assign_block_vars($position_prefix . 'blocks_row', array(
  444. 'CMS_BLOCK' => $cms_block,
  445. 'OUTPUT' => $output_block
  446. )
  447. );
  448. }
  449. }
  450. return true;
  451. }
  452. }
  453. ?>