PageRenderTime 75ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/shoutbox.php

https://github.com/igorw-forks/icy_phoenix
PHP | 227 lines | 185 code | 21 blank | 21 comment | 46 complexity | 02c38222c5905da0456b540e99c6f4a4 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. define('IN_ICYPHOENIX', true);
  11. if (!defined('IP_ROOT_PATH')) define('IP_ROOT_PATH', './');
  12. if (!defined('PHP_EXT')) define('PHP_EXT', substr(strrchr(__FILE__, '.'), 1));
  13. include(IP_ROOT_PATH . 'common.' . PHP_EXT);
  14. include_once(IP_ROOT_PATH . 'includes/bbcode.' . PHP_EXT);
  15. define ('NUM_SHOUT', 20);
  16. // Start session management
  17. $userdata = session_pagestart($user_ip, false);
  18. init_userprefs($userdata);
  19. // End session management
  20. $cms_page['page_id'] = 'shoutbox';
  21. $cms_page['page_nav'] = (!empty($cms_config_layouts[$cms_page['page_id']]['page_nav']) ? true : false);
  22. $cms_page['global_blocks'] = (!empty($cms_config_layouts[$cms_page['page_id']]['global_blocks']) ? true : false);
  23. // Force to false...
  24. $cms_page['page_nav'] = false;
  25. $cms_page['global_blocks'] = false;
  26. $cms_auth_level = (isset($cms_config_layouts[$cms_page['page_id']]['view']) ? $cms_config_layouts[$cms_page['page_id']]['view'] : AUTH_ALL);
  27. check_page_auth($cms_page['page_id'], $cms_auth_level);
  28. // Start auth check
  29. switch ($userdata['user_level'])
  30. {
  31. case ADMIN :
  32. case MOD : $is_auth['auth_mod'] = 1;
  33. default:
  34. $is_auth['auth_read'] = 1;
  35. $is_auth['auth_view'] = 1;
  36. if ($userdata['user_id']==ANONYMOUS)
  37. {
  38. $is_auth['auth_delete'] = 0;
  39. $is_auth['auth_post'] = 0;
  40. }
  41. else
  42. {
  43. $is_auth['auth_delete'] = 1;
  44. $is_auth['auth_post'] = 1;
  45. }
  46. }
  47. if(!$is_auth['auth_read'])
  48. {
  49. message_die(GENERAL_MESSAGE, $lang['Not_Authorized']);
  50. }
  51. // End auth check
  52. //$refresh = (isset($_POST['auto_refresh']) || isset($_POST['refresh'])) ? 1 : 0;
  53. $refresh = (isset($_GET['auto_refresh']) || isset($_GET['refresh'])) ? 1 : 0;
  54. $submit = (isset($_POST['shout']) && isset($_POST['message'])) ? 1 : 0;
  55. $mode = request_var('mode', '');
  56. // Set toggles for various options
  57. if (!$config['allow_html'])
  58. {
  59. $html_on = 0;
  60. }
  61. else
  62. {
  63. $html_on = ($submit || $refresh || $preview) ? ((!empty($_POST['disable_html'])) ? 0 : 1) : (($userdata['user_id'] == ANONYMOUS) ? $config['allow_html'] : $userdata['user_allowhtml']);
  64. }
  65. if (!$config['allow_bbcode'])
  66. {
  67. $bbcode_on = 0;
  68. }
  69. else
  70. {
  71. $bbcode_on = ($submit || $refresh || $preview) ? ((!empty($_POST['disable_bbcode'])) ? 0 : 1) : (($userdata['user_id'] == ANONYMOUS) ? $config['allow_bbcode'] : $userdata['user_allowbbcode']);
  72. }
  73. if (!$config['allow_smilies'])
  74. {
  75. $smilies_on = 0;
  76. }
  77. else
  78. {
  79. $smilies_on = ($submit || $refresh || $preview) ? ((!empty($_POST['disable_smilies'])) ? 0 : 1) : (($userdata['user_id'] == ANONYMOUS) ? $config['allow_smilies'] : $userdata['user_allowsmile']);
  80. if ($smilies_on)
  81. {
  82. include(IP_ROOT_PATH . 'includes/functions_post.' . PHP_EXT);
  83. generate_smilies('inline');
  84. if ($mode == 'smilies')
  85. {
  86. generate_smilies('window');
  87. exit;
  88. }
  89. }
  90. }
  91. if ($refresh)
  92. {
  93. $message = request_post_var('message', '', true);
  94. if (!empty($message))
  95. {
  96. $template->assign_var('MESSAGE',$message);
  97. }
  98. }
  99. elseif ($submit || isset($_POST['message']))
  100. {
  101. $current_time = time();
  102. // Flood control
  103. $where_sql = ($userdata['user_id'] == ANONYMOUS) ? "shout_ip = '$user_ip'" : 'shout_user_id = ' . $userdata['user_id'];
  104. $sql = "SELECT MAX(shout_session_time) AS last_post_time
  105. FROM " . SHOUTBOX_TABLE . "
  106. WHERE $where_sql";
  107. $db->sql_return_on_error(true);
  108. $result = $db->sql_query($sql);
  109. $db->sql_return_on_error(false);
  110. if ($result)
  111. {
  112. if ($row = $db->sql_fetchrow($result))
  113. {
  114. if (($row['last_post_time'] > 0) && (($current_time - $row['last_post_time']) < $config['flood_interval']) && ($userdata['user_level'] != ADMIN))
  115. {
  116. $error = true;
  117. $error_msg .= (!empty($error_msg)) ? '<br />' . $lang['Flood_Error'] : $lang['Flood_Error'];
  118. }
  119. }
  120. }
  121. // Check username
  122. $username = $userdata['session_logged_in'] ? htmlspecialchars($userdata['username']) : request_post_var('username', '', true);
  123. if (!$userdata['session_logged_in'] && !empty($username))
  124. {
  125. include(IP_ROOT_PATH . 'includes/functions_validate.' . PHP_EXT);
  126. $result = validate_username($username);
  127. if ($result['error'])
  128. {
  129. $error_msg .= (!empty($error_msg)) ? '<br />' . $result['error_msg'] : $result['error_msg'];
  130. }
  131. }
  132. $message = request_post_var('message', '', true);
  133. $message = htmlspecialchars_decode($message, ENT_COMPAT);
  134. // insert shout !
  135. if (!empty($message) && $is_auth['auth_post'] && !$error)
  136. {
  137. include_once(IP_ROOT_PATH . 'includes/functions_post.' . PHP_EXT);
  138. $message = prepare_message(trim($message), $html_on, $bbcode_on, $smilies_on);
  139. if ($config['img_shoutbox'] == true)
  140. {
  141. $message = preg_replace ("#\[url=(http://)([^ \"\n\r\t<]*)\]\[img\](http://)([^ \"\n\r\t<]*)\[/img\]\[/url\]#i", '[url=\\1\\2]\\4[/url]', $message);
  142. $message = preg_replace ("#\[img\](http://)([^ \"\n\r\t<]*)\[/img\]#i", '[url=\\1\\2]\\2[/url]', $message);
  143. $message = preg_replace ("#\[img align=left\](http://)([^ \"\n\r\t<]*)\[/img\]#i", '[url=\\1\\2]\\2[/url]', $message);
  144. $message = preg_replace ("#\[img align=right\](http://)([^ \"\n\r\t<]*)\[/img\]#i", '[url=\\1\\2]\\2[/url]', $message);
  145. }
  146. $sql = "INSERT INTO " . SHOUTBOX_TABLE . " (shout_text, shout_session_time, shout_user_id, shout_ip, shout_username, enable_bbcode, enable_html, enable_smilies)
  147. VALUES ('" . $db->sql_escape($message) . "', '" . time() . "', '" . $userdata['user_id'] . "', '$user_ip', '" . $db->sql_escape($username) . "', $bbcode_on, $html_on, $smilies_on)";
  148. $result = $db->sql_query($sql);
  149. // auto prune
  150. if ($config['prune_shouts'])
  151. {
  152. $sql = "DELETE FROM " . SHOUTBOX_TABLE . " WHERE shout_session_time<=" . (time() - (86400 * $config['prune_shouts']));
  153. $result = $db->sql_query($sql);
  154. }
  155. }
  156. }
  157. // see if we need offset
  158. $start = request_var('start', 0);
  159. $start = ($start < 0) ? 0 : $start;
  160. if ($submit)
  161. {
  162. $start = 0;
  163. }
  164. // Show simple shoutbox
  165. if ($is_auth['auth_post'])
  166. {
  167. $template->assign_block_vars('switch_auth_post', array());
  168. }
  169. else
  170. {
  171. $template->assign_block_vars('switch_auth_no_post', array());
  172. }
  173. if ($bbcode_on)
  174. {
  175. $template->assign_block_vars('switch_auth_post.switch_bbcode', array());
  176. }
  177. $template->set_filenames(array('body' => 'shoutbox_body.tpl'));
  178. $template->assign_vars(array(
  179. 'U_SHOUTBOX' => append_sid('shoutbox.' . PHP_EXT . '?start=' . $start),
  180. 'U_SHOUTBOX_VIEW' => append_sid('shoutbox_view.' . PHP_EXT . '?start=' . $start),
  181. 'T_HEAD_STYLESHEET' => $theme['head_stylesheet'],
  182. 'T_NAME' => $theme['template_name'],
  183. 'L_SHOUTBOX' => $lang['Shoutbox'],
  184. 'L_SHOUT_PREVIEW' => $lang['Preview'],
  185. 'L_SHOUT_SUBMIT' => $lang['Go'],
  186. 'L_SHOUT_TEXT' => $lang['Shout_text'],
  187. 'L_SHOUT_REFRESH' => $lang['Shout_refresh'],
  188. 'L_SMILIES' => $lang['Smilies'],
  189. 'T_URL' => 'templates/' . $theme['template_name'],
  190. 'S_CONTENT_ENCODING' => $lang['ENCODING'],
  191. 'L_BBCODE_CLOSE_TAGS' => $lang['Close_Tags'],
  192. 'L_SHOUTBOX_LOGIN' => $lang['Login_join'],
  193. 'SHOUT_VIEW_SIZE' => ($max) ? $max : 0,
  194. 'S_HIDDEN_FIELDS' => $s_hidden_fields
  195. )
  196. );
  197. if($error_msg != '')
  198. {
  199. $template->set_filenames(array('reg_header' => 'error_body.tpl'));
  200. $template->assign_vars(array('ERROR_MESSAGE' => $error_msg));
  201. $template->assign_var_from_handle('ERROR_BOX', 'reg_header');
  202. $message = request_var('message', '', true);
  203. $template->assign_var('MESSAGE', $message);
  204. }
  205. $template->pparse('body');
  206. ?>