PageRenderTime 34ms CodeModel.GetById 5ms RepoModel.GetById 0ms app.codeStats 0ms

/doweneed/forums/includes/functions.php

https://bitbucket.org/natis/masscap-main
PHP | 678 lines | 578 code | 40 blank | 60 comment | 58 complexity | d1f4c886b85fcf31e2875125e340f127 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /***************************************************************************
  3. * functions.php
  4. * -------------------
  5. * begin : Saturday, Feb 13, 2001
  6. * copyright : (C) 2001 The phpBB Group
  7. * email : support@phpbb.com
  8. *
  9. * $Id: functions.php,v 1.133.2.5 2002/07/08 10:30:41 psotfx Exp $
  10. *
  11. *
  12. ***************************************************************************/
  13. /***************************************************************************
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. *
  21. ***************************************************************************/
  22. function get_db_stat($mode)
  23. {
  24. global $db;
  25. switch( $mode )
  26. {
  27. case 'usercount':
  28. $sql = "SELECT COUNT(user_id) AS total
  29. FROM " . USERS_TABLE . "
  30. WHERE user_id <> " . ANONYMOUS;
  31. break;
  32. case 'newestuser':
  33. $sql = "SELECT user_id, username
  34. FROM " . USERS_TABLE . "
  35. WHERE user_id <> " . ANONYMOUS . "
  36. ORDER BY user_id DESC
  37. LIMIT 1";
  38. break;
  39. case 'postcount':
  40. case 'topiccount':
  41. $sql = "SELECT SUM(forum_topics) AS topic_total, SUM(forum_posts) AS post_total
  42. FROM " . FORUMS_TABLE;
  43. break;
  44. }
  45. if ( !($result = $db->sql_query($sql)) )
  46. {
  47. return false;
  48. }
  49. $row = $db->sql_fetchrow($result);
  50. switch ( $mode )
  51. {
  52. case 'usercount':
  53. return $row['total'];
  54. break;
  55. case 'newestuser':
  56. return $row;
  57. break;
  58. case 'postcount':
  59. return $row['post_total'];
  60. break;
  61. case 'topiccount':
  62. return $row['topic_total'];
  63. break;
  64. }
  65. return false;
  66. }
  67. function get_userdata($user)
  68. {
  69. global $db;
  70. $sql = "SELECT *
  71. FROM " . USERS_TABLE . "
  72. WHERE ";
  73. $sql .= ( ( is_integer($user) ) ? "user_id = $user" : "username = '" . str_replace("\'", "''", $user) . "'" ) . " AND user_id <> " . ANONYMOUS;
  74. if ( !($result = $db->sql_query($sql)) )
  75. {
  76. message_die(GENERAL_ERROR, 'Tried obtaining data for a non-existent user', '', __LINE__, __FILE__, $sql);
  77. }
  78. return ( $row = $db->sql_fetchrow($result) ) ? $row : false;
  79. }
  80. function make_jumpbox($action, $match_forum_id = 0)
  81. {
  82. global $template, $lang, $db, $SID, $nav_links, $phpEx;
  83. // $is_auth = auth(AUTH_VIEW, AUTH_LIST_ALL, $userdata);
  84. $sql = "SELECT c.cat_id, c.cat_title, c.cat_order
  85. FROM " . CATEGORIES_TABLE . " c, " . FORUMS_TABLE . " f
  86. WHERE f.cat_id = c.cat_id
  87. GROUP BY c.cat_id, c.cat_title, c.cat_order
  88. ORDER BY c.cat_order";
  89. if ( !($result = $db->sql_query($sql)) )
  90. {
  91. message_die(GENERAL_ERROR, "Couldn't obtain category list.", "", __LINE__, __FILE__, $sql);
  92. }
  93. $category_rows = array();
  94. while ( $row = $db->sql_fetchrow($result) )
  95. {
  96. $category_rows[] = $row;
  97. }
  98. if ( $total_categories = count($category_rows) )
  99. {
  100. $sql = "SELECT *
  101. FROM " . FORUMS_TABLE . "
  102. ORDER BY cat_id, forum_order";
  103. if ( !($result = $db->sql_query($sql)) )
  104. {
  105. message_die(GENERAL_ERROR, 'Could not obtain forums information', '', __LINE__, __FILE__, $sql);
  106. }
  107. $boxstring = '<select name="' . POST_FORUM_URL . '" onChange="if(this.options[this.selectedIndex].value != -1){ forms[\'jumpbox\'].submit() }"><option value="-1">' . $lang['Select_forum'] . '</option>';
  108. $forum_rows = array();
  109. while ( $row = $db->sql_fetchrow($result) )
  110. {
  111. $forum_rows[] = $row;
  112. }
  113. if ( $total_forums = count($forum_rows) )
  114. {
  115. for($i = 0; $i < $total_categories; $i++)
  116. {
  117. $boxstring_forums = '';
  118. for($j = 0; $j < $total_forums; $j++)
  119. {
  120. if ( $forum_rows[$j]['cat_id'] == $category_rows[$i]['cat_id'] && $forum_rows[$j]['auth_view'] <= AUTH_REG )
  121. {
  122. // if ( $forum_rows[$j]['cat_id'] == $category_rows[$i]['cat_id'] && $is_auth[$forum_rows[$j]['forum_id']]['auth_view'] )
  123. // {
  124. $selected = ( $forum_rows[$j]['forum_id'] == $match_forum_id ) ? 'selected="selected"' : '';
  125. $boxstring_forums .= '<option value="' . $forum_rows[$j]['forum_id'] . '"' . $selected . '>' . $forum_rows[$j]['forum_name'] . '</option>';
  126. //
  127. // Add an array to $nav_links for the Mozilla navigation bar.
  128. // 'chapter' and 'forum' can create multiple items, therefore we are using a nested array.
  129. //
  130. $nav_links['chapter forum'][$forum_rows[$j]['forum_id']] = array (
  131. 'url' => append_sid("viewforum.$phpEx?" . POST_FORUM_URL . "=" . $forum_rows[$j]['forum_id']),
  132. 'title' => $forum_rows[$j]['forum_name']
  133. );
  134. }
  135. }
  136. if ( $boxstring_forums != '' )
  137. {
  138. $boxstring .= '<option value="-1">&nbsp;</option>';
  139. $boxstring .= '<option value="-1">' . $category_rows[$i]['cat_title'] . '</option>';
  140. $boxstring .= '<option value="-1">----------------</option>';
  141. $boxstring .= $boxstring_forums;
  142. }
  143. }
  144. }
  145. $boxstring .= '</select>';
  146. }
  147. else
  148. {
  149. $boxstring .= '<select name="' . POST_FORUM_URL . '" onChange="if(this.options[this.selectedIndex].value != -1){ forms[\'jumpbox\'].submit() }"></select>';
  150. }
  151. if ( isset($SID) )
  152. {
  153. $boxstring .= '<input type="hidden" name="sid" value="' . $SID . '" />';
  154. }
  155. $template->set_filenames(array(
  156. 'jumpbox' => 'jumpbox.tpl')
  157. );
  158. $template->assign_vars(array(
  159. 'L_GO' => $lang['Go'],
  160. 'L_JUMP_TO' => $lang['Jump_to'],
  161. 'L_SELECT_FORUM' => $lang['Select_forum'],
  162. 'S_JUMPBOX_SELECT' => $boxstring,
  163. 'S_JUMPBOX_ACTION' => append_sid($action))
  164. );
  165. $template->assign_var_from_handle('JUMPBOX', 'jumpbox');
  166. return;
  167. }
  168. //
  169. // Initialise user settings on page load
  170. function init_userprefs($userdata)
  171. {
  172. global $board_config, $theme, $images;
  173. global $template, $lang, $phpEx, $phpbb_root_path;
  174. if ( $userdata['user_id'] != ANONYMOUS )
  175. {
  176. if ( !empty($userdata['user_lang']))
  177. {
  178. $board_config['default_lang'] = $userdata['user_lang'];
  179. }
  180. if ( !empty($userdata['user_dateformat']) )
  181. {
  182. $board_config['default_dateformat'] = $userdata['user_dateformat'];
  183. }
  184. if ( isset($userdata['user_timezone']) )
  185. {
  186. $board_config['board_timezone'] = $userdata['user_timezone'];
  187. }
  188. }
  189. if ( !file_exists($phpbb_root_path . 'language/lang_' . $board_config['default_lang'] . '/lang_main.'.$phpEx) )
  190. {
  191. $board_config['default_lang'] = 'english';
  192. }
  193. include($phpbb_root_path . 'language/lang_' . $board_config['default_lang'] . '/lang_main.' . $phpEx);
  194. if ( defined('IN_ADMIN') )
  195. {
  196. if( !file_exists($phpbb_root_path . 'language/lang_' . $board_config['default_lang'] . '/lang_admin.'.$phpEx) )
  197. {
  198. $board_config['default_lang'] = 'english';
  199. }
  200. include($phpbb_root_path . 'language/lang_' . $board_config['default_lang'] . '/lang_admin.' . $phpEx);
  201. }
  202. //
  203. // Set up style
  204. //
  205. if ( !$board_config['override_user_style'] )
  206. {
  207. if ( $userdata['user_id'] != ANONYMOUS && $userdata['user_style'] > 0 )
  208. {
  209. if ( $theme = setup_style($userdata['user_style']) )
  210. {
  211. return;
  212. }
  213. }
  214. }
  215. $theme = setup_style($board_config['default_style']);
  216. return;
  217. }
  218. function setup_style($style)
  219. {
  220. global $db, $board_config, $template, $images, $phpbb_root_path;
  221. $sql = "SELECT *
  222. FROM " . THEMES_TABLE . "
  223. WHERE themes_id = $style";
  224. if ( !($result = $db->sql_query($sql)) )
  225. {
  226. message_die(CRITICAL_ERROR, 'Could not query database for theme info');
  227. }
  228. if ( !($row = $db->sql_fetchrow($result)) )
  229. {
  230. message_die(CRITICAL_ERROR, "Could not get theme data for themes_id [$style]");
  231. }
  232. $template_path = 'templates/' ;
  233. $template_name = $row['template_name'] ;
  234. $template = new Template($phpbb_root_path . $template_path . $template_name, $board_config, $db);
  235. if ( $template )
  236. {
  237. $current_template_path = $template_path . $template_name;
  238. @include($phpbb_root_path . $template_path . $template_name . '/' . $template_name . '.cfg');
  239. if ( !defined('TEMPLATE_CONFIG') )
  240. {
  241. message_die(CRITICAL_ERROR, "Could not open $template_name template config file", '', __LINE__, __FILE__);
  242. }
  243. $img_lang = ( file_exists($current_template_path . '/images/lang_' . $board_config['default_lang']) ) ? $board_config['default_lang'] : 'english';
  244. while( list($key, $value) = @each($images) )
  245. {
  246. if ( !is_array($value) )
  247. {
  248. $images[$key] = str_replace('{LANG}', 'lang_' . $img_lang, $value);
  249. }
  250. }
  251. }
  252. return $row;
  253. }
  254. function encode_ip($dotquad_ip)
  255. {
  256. $ip_sep = explode('.', $dotquad_ip);
  257. return sprintf('%02x%02x%02x%02x', $ip_sep[0], $ip_sep[1], $ip_sep[2], $ip_sep[3]);
  258. }
  259. function decode_ip($int_ip)
  260. {
  261. $hexipbang = explode('.', chunk_split($int_ip, 2, '.'));
  262. return hexdec($hexipbang[0]). '.' . hexdec($hexipbang[1]) . '.' . hexdec($hexipbang[2]) . '.' . hexdec($hexipbang[3]);
  263. }
  264. //
  265. // Create date/time from format and timezone
  266. //
  267. function create_date($format, $gmepoch, $tz)
  268. {
  269. global $board_config, $lang;
  270. static $translate;
  271. if ( empty($translate) && $board_config['default_lang'] != 'english' )
  272. {
  273. @reset($lang['datetime']);
  274. while ( list($match, $replace) = @each($lang['datetime']) )
  275. {
  276. $translate[$match] = $replace;
  277. }
  278. }
  279. return ( !empty($translate) ) ? strtr(@gmdate($format, $gmepoch + (3600 * $tz)), $translate) : @gmdate($format, $gmepoch + (3600 * $tz));
  280. }
  281. //
  282. // Pagination routine, generates
  283. // page number sequence
  284. //
  285. function generate_pagination($base_url, $num_items, $per_page, $start_item, $add_prevnext_text = TRUE)
  286. {
  287. global $lang;
  288. $total_pages = ceil($num_items/$per_page);
  289. if ( $total_pages == 1 )
  290. {
  291. return '';
  292. }
  293. $on_page = floor($start_item / $per_page) + 1;
  294. $page_string = '';
  295. if ( $total_pages > 10 )
  296. {
  297. $init_page_max = ( $total_pages > 3 ) ? 3 : $total_pages;
  298. for($i = 1; $i < $init_page_max + 1; $i++)
  299. {
  300. $page_string .= ( $i == $on_page ) ? '<b>' . $i . '</b>' : '<a href="' . append_sid($base_url . "&amp;start=" . ( ( $i - 1 ) * $per_page ) ) . '">' . $i . '</a>';
  301. if ( $i < $init_page_max )
  302. {
  303. $page_string .= ", ";
  304. }
  305. }
  306. if ( $total_pages > 3 )
  307. {
  308. if ( $on_page > 1 && $on_page < $total_pages )
  309. {
  310. $page_string .= ( $on_page > 5 ) ? ' ... ' : ', ';
  311. $init_page_min = ( $on_page > 4 ) ? $on_page : 5;
  312. $init_page_max = ( $on_page < $total_pages - 4 ) ? $on_page : $total_pages - 4;
  313. for($i = $init_page_min - 1; $i < $init_page_max + 2; $i++)
  314. {
  315. $page_string .= ($i == $on_page) ? '<b>' . $i . '</b>' : '<a href="' . append_sid($base_url . "&amp;start=" . ( ( $i - 1 ) * $per_page ) ) . '">' . $i . '</a>';
  316. if ( $i < $init_page_max + 1 )
  317. {
  318. $page_string .= ', ';
  319. }
  320. }
  321. $page_string .= ( $on_page < $total_pages - 4 ) ? ' ... ' : ', ';
  322. }
  323. else
  324. {
  325. $page_string .= ' ... ';
  326. }
  327. for($i = $total_pages - 2; $i < $total_pages + 1; $i++)
  328. {
  329. $page_string .= ( $i == $on_page ) ? '<b>' . $i . '</b>' : '<a href="' . append_sid($base_url . "&amp;start=" . ( ( $i - 1 ) * $per_page ) ) . '">' . $i . '</a>';
  330. if( $i < $total_pages )
  331. {
  332. $page_string .= ", ";
  333. }
  334. }
  335. }
  336. }
  337. else
  338. {
  339. for($i = 1; $i < $total_pages + 1; $i++)
  340. {
  341. $page_string .= ( $i == $on_page ) ? '<b>' . $i . '</b>' : '<a href="' . append_sid($base_url . "&amp;start=" . ( ( $i - 1 ) * $per_page ) ) . '">' . $i . '</a>';
  342. if ( $i < $total_pages )
  343. {
  344. $page_string .= ', ';
  345. }
  346. }
  347. }
  348. if ( $add_prevnext_text )
  349. {
  350. if ( $on_page > 1 )
  351. {
  352. $page_string = ' <a href="' . append_sid($base_url . "&amp;start=" . ( ( $on_page - 2 ) * $per_page ) ) . '">' . $lang['Previous'] . '</a>&nbsp;&nbsp;' . $page_string;
  353. }
  354. if ( $on_page < $total_pages )
  355. {
  356. $page_string .= '&nbsp;&nbsp;<a href="' . append_sid($base_url . "&amp;start=" . ( $on_page * $per_page ) ) . '">' . $lang['Next'] . '</a>';
  357. }
  358. }
  359. $page_string = $lang['Goto_page'] . ' ' . $page_string;
  360. return $page_string;
  361. }
  362. //
  363. // This does exactly what preg_quote() does in PHP 4-ish
  364. // If you just need the 1-parameter preg_quote call, then don't bother using this.
  365. //
  366. function phpbb_preg_quote($str, $delimiter)
  367. {
  368. $text = preg_quote($str);
  369. $text = str_replace($delimiter, '\\' . $delimiter, $text);
  370. return $text;
  371. }
  372. //
  373. // Obtain list of naughty words and build preg style replacement arrays for use by the
  374. // calling script, note that the vars are passed as references this just makes it easier
  375. // to return both sets of arrays
  376. //
  377. function obtain_word_list(&$orig_word, &$replacement_word)
  378. {
  379. global $db;
  380. //
  381. // Define censored word matches
  382. //
  383. $sql = "SELECT word, replacement
  384. FROM " . WORDS_TABLE;
  385. if( !($result = $db->sql_query($sql)) )
  386. {
  387. message_die(GENERAL_ERROR, 'Could not get censored words from database', '', __LINE__, __FILE__, $sql);
  388. }
  389. if ( $row = $db->sql_fetchrow($result) )
  390. {
  391. do
  392. {
  393. $orig_word[] = '#\b(' . str_replace('\*', '\w*?', phpbb_preg_quote($row['word'], '#')) . ')\b#i';
  394. $replacement_word[] = $row['replacement'];
  395. }
  396. while ( $row = $db->sql_fetchrow($result) );
  397. }
  398. return true;
  399. }
  400. //
  401. // This is general replacement for die(), allows templated
  402. // output in users (or default) language, etc.
  403. //
  404. // $msg_code can be one of these constants:
  405. //
  406. // GENERAL_MESSAGE : Use for any simple text message, eg. results
  407. // of an operation, authorisation failures, etc.
  408. //
  409. // GENERAL ERROR : Use for any error which occurs _AFTER_ the
  410. // common.php include and session code, ie. most errors in
  411. // pages/functions
  412. //
  413. // CRITICAL_MESSAGE : Used when basic config data is available but
  414. // a session may not exist, eg. banned users
  415. //
  416. // CRITICAL_ERROR : Used when config data cannot be obtained, eg
  417. // no database connection. Should _not_ be used in 99.5% of cases
  418. //
  419. function message_die($msg_code, $msg_text = '', $msg_title = '', $err_line = '', $err_file = '', $sql = '')
  420. {
  421. global $db, $template, $board_config, $theme, $lang, $phpEx, $phpbb_root_path, $nav_links, $gen_simple_header;
  422. global $userdata, $user_ip, $session_length;
  423. global $starttime;
  424. $sql_store = $sql;
  425. //
  426. // Get SQL error if we are debugging. Do this as soon as possible to prevent
  427. // subsequent queries from overwriting the status of sql_error()
  428. //
  429. if ( DEBUG && ( $msg_code == GENERAL_ERROR || $msg_code == CRITICAL_ERROR ) )
  430. {
  431. $sql_error = $db->sql_error();
  432. $debug_text = '';
  433. if ( $sql_error['message'] != '' )
  434. {
  435. $debug_text .= '<br /><br />SQL Error : ' . $sql_error['code'] . ' ' . $sql_error['message'];
  436. }
  437. if ( $sql_store != '' )
  438. {
  439. $debug_text .= "<br /><br />$sql_store";
  440. }
  441. if ( $err_line != '' && $err_file != '' )
  442. {
  443. $debug_text .= '</br /><br />Line : ' . $err_line . '<br />File : ' . $err_file;
  444. }
  445. }
  446. if( empty($userdata) && ( $msg_code == GENERAL_MESSAGE || $msg_code == GENERAL_ERROR ) )
  447. {
  448. $userdata = session_pagestart($user_ip, PAGE_INDEX);
  449. init_userprefs($userdata);
  450. }
  451. //
  452. // If the header hasn't been output then do it
  453. //
  454. if ( !defined('HEADER_INC') && $msg_code != CRITICAL_ERROR )
  455. {
  456. if ( empty($lang) )
  457. {
  458. if ( !empty($board_config['default_lang']) )
  459. {
  460. include($phpbb_root_path . 'language/lang_' . $board_config['default_lang'] . '/lang_main.'.$phpEx);
  461. }
  462. else
  463. {
  464. include($phpbb_root_path . 'language/lang_english/lang_main.'.$phpEx);
  465. }
  466. }
  467. if ( empty($template) )
  468. {
  469. $template = new Template($phpbb_root_path . 'templates/' . $board_config['board_template']);
  470. }
  471. if ( empty($theme) )
  472. {
  473. $theme = setup_style($board_config['default_style']);
  474. }
  475. //
  476. // Load the Page Header
  477. //
  478. if ( !defined('IN_ADMIN') )
  479. {
  480. include($phpbb_root_path . 'includes/page_header.'.$phpEx);
  481. }
  482. else
  483. {
  484. include($phpbb_root_path . 'admin/page_header_admin.'.$phpEx);
  485. }
  486. }
  487. switch($msg_code)
  488. {
  489. case GENERAL_MESSAGE:
  490. if ( $msg_title == '' )
  491. {
  492. $msg_title = $lang['Information'];
  493. }
  494. break;
  495. case CRITICAL_MESSAGE:
  496. if ( $msg_title == '' )
  497. {
  498. $msg_title = $lang['Critical_Information'];
  499. }
  500. break;
  501. case GENERAL_ERROR:
  502. if ( $msg_text == '' )
  503. {
  504. $msg_text = $lang['An_error_occured'];
  505. }
  506. if ( $msg_title == '' )
  507. {
  508. $msg_title = $lang['General_Error'];
  509. }
  510. break;
  511. case CRITICAL_ERROR:
  512. //
  513. // Critical errors mean we cannot rely on _ANY_ DB information being
  514. // available so we're going to dump out a simple echo'd statement
  515. //
  516. include($phpbb_root_path . 'language/lang_english/lang_main.'.$phpEx);
  517. if ( $msg_text == '' )
  518. {
  519. $msg_text = $lang['A_critical_error'];
  520. }
  521. if ( $msg_title == '' )
  522. {
  523. $msg_title = 'phpBB : <b>' . $lang['Critical_Error'] . '</b>';
  524. }
  525. break;
  526. }
  527. //
  528. // Add on DEBUG info if we've enabled debug mode and this is an error. This
  529. // prevents debug info being output for general messages should DEBUG be
  530. // set TRUE by accident (preventing confusion for the end user!)
  531. //
  532. if ( DEBUG && ( $msg_code == GENERAL_ERROR || $msg_code == CRITICAL_ERROR ) )
  533. {
  534. if ( $debug_text != '' )
  535. {
  536. $msg_text = $msg_text . '<br /><br /><b><u>DEBUG MODE</u></b>' . $debug_text;
  537. }
  538. }
  539. if ( $msg_code != CRITICAL_ERROR )
  540. {
  541. if ( !empty($lang[$msg_text]) )
  542. {
  543. $msg_text = $lang[$msg_text];
  544. }
  545. if ( !defined('IN_ADMIN') )
  546. {
  547. $template->set_filenames(array(
  548. 'message_body' => 'message_body.tpl')
  549. );
  550. }
  551. else
  552. {
  553. $template->set_filenames(array(
  554. 'message_body' => 'admin/admin_message_body.tpl')
  555. );
  556. }
  557. $template->assign_vars(array(
  558. 'MESSAGE_TITLE' => $msg_title,
  559. 'MESSAGE_TEXT' => $msg_text)
  560. );
  561. $template->pparse('message_body');
  562. if ( !defined('IN_ADMIN') )
  563. {
  564. include($phpbb_root_path . 'includes/page_tail.'.$phpEx);
  565. }
  566. else
  567. {
  568. include($phpbb_root_path . 'admin/page_footer_admin.'.$phpEx);
  569. }
  570. }
  571. else
  572. {
  573. echo "<html>\n<body>\n" . $msg_title . "\n<br /><br />\n" . $msg_text . "</body>\n</html>";
  574. }
  575. exit;
  576. }
  577. ?>