home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 April / PCWorld_2005-04_cd.bin / akce / web / unitednuke / unitednuke.exe / html / includes / functions.php < prev    next >
PHP Script  |  2004-07-11  |  36KB  |  866 lines

  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.29 2003/06/10 01:38:08 psotfx Exp $
  10.  *
  11.  *
  12.  ***************************************************************************/
  13. /***************************************************************************
  14. * phpbb2 forums port version 2.0.5 (c) 2003 - Nuke Cops (http://nukecops.com)
  15. *
  16. * Ported by Nuke Cops to phpbb2 standalone 2.0.5 Test
  17. * and debugging completed by the Elite Nukers and site members.
  18. *
  19. * You run this package at your sole risk. Nuke Cops and affiliates cannot
  20. * be held liable if anything goes wrong. You are advised to test this
  21. * package on a development system. Backup everything before implementing
  22. * in a production environment. If something goes wrong, you can always
  23. * backout and restore your backups.
  24. *
  25. * Installing and running this also means you agree to the terms of the AUP
  26. * found at Nuke Cops.
  27. *
  28. * This is version 2.0.5 of the phpbb2 forum port for PHP-Nuke. Work is based
  29. * on Tom Nitzschner's forum port version 2.0.6. Tom's 2.0.6 port was based
  30. * on the phpbb2 standalone version 2.0.3. Our version 2.0.5 from Nuke Cops is
  31. * now reflecting phpbb2 standalone 2.0.5 that fixes some bugs and the
  32. * invalid_session error message.
  33. ***************************************************************************/
  34. /***************************************************************************
  35.  *   This file is part of the phpBB2 port to Nuke 6.0 (c) copyright 2002
  36.  *   by Tom Nitzschner (tom@toms-home.com)
  37.  *   http://bbtonuke.sourceforge.net (or http://www.toms-home.com)
  38.  *
  39.  *   As always, make a backup before messing with anything. All code
  40.  *   release by me is considered sample code only. It may be fully
  41.  *   functual, but you use it at your own risk, if you break it,
  42.  *   you get to fix it too. No waranty is given or implied.
  43.  *
  44.  *   Please post all questions/request about this port on http://bbtonuke.sourceforge.net first,
  45.  *   then on my site. All original header code and copyright messages will be maintained
  46.  *   to give credit where credit is due. If you modify this, the only requirement is
  47.  *   that you also maintain all original copyright messages. All my work is released
  48.  *   under the GNU GENERAL PUBLIC LICENSE. Please see the README for more information.
  49.  *
  50.  ***************************************************************************/
  51. /***************************************************************************
  52.  *
  53.  *   This program is free software; you can redistribute it and/or modify
  54.  *   it under the terms of the GNU General Public License as published by
  55.  *   the Free Software Foundation; either version 2 of the License, or
  56.  *   (at your option) any later version.
  57.  *
  58.  *
  59.  ***************************************************************************/
  60.  
  61. function get_db_stat($mode)
  62. {
  63.         global $db;
  64.  
  65.         switch( $mode )
  66.         {
  67.                 case 'usercount':
  68.                         $sql = "SELECT COUNT(user_id) AS total
  69.                                 FROM " . USERS_TABLE . "
  70.                                 WHERE user_id <> " . ANONYMOUS;
  71.                         break;
  72.  
  73.                 case 'newestuser':
  74.                         $sql = "SELECT user_id, username
  75.                                 FROM " . USERS_TABLE . "
  76.                                 WHERE user_id <> " . ANONYMOUS . "
  77.                                 ORDER BY user_id DESC
  78.                                 LIMIT 1";
  79.                         break;
  80.  
  81.                 case 'postcount':
  82.                 case 'topiccount':
  83.                         $sql = "SELECT SUM(forum_topics) AS topic_total, SUM(forum_posts) AS post_total
  84.                                 FROM " . FORUMS_TABLE;
  85.                         break;
  86.         }
  87.  
  88.         if ( !($result = $db->sql_query($sql)) )
  89.         {
  90.                 return false;
  91.         }
  92.  
  93.         $row = $db->sql_fetchrow($result);
  94.  
  95.         switch ( $mode )
  96.         {
  97.                 case 'usercount':
  98.                         return $row['total'];
  99.                         break;
  100.                 case 'newestuser':
  101.                         return $row;
  102.                         break;
  103.                 case 'postcount':
  104.                         return $row['post_total'];
  105.                         break;
  106.                 case 'topiccount':
  107.                         return $row['topic_total'];
  108.                         break;
  109.         }
  110.  
  111.         return false;
  112. }
  113.  
  114. function get_userdata($user)
  115. {
  116.         global $db;
  117.  
  118.         $sql = "SELECT *
  119.                 FROM " . USERS_TABLE . "
  120.                 WHERE ";
  121.         $sql .= ( ( is_integer($user) ) ? "user_id = $user" : "username = '" .  $user . "'" ) . " AND user_id <> " . ANONYMOUS;
  122.         if ( !($result = $db->sql_query($sql)) )
  123.         {
  124.                 message_die(GENERAL_ERROR, 'Tried obtaining data for a non-existent user', '', __LINE__, __FILE__, $sql);
  125.         }
  126.  
  127.         return ( $row = $db->sql_fetchrow($result) ) ? $row : false;
  128. }
  129.  
  130. function make_jumpbox($action, $match_forum_id = 0)
  131. {
  132.         global $template, $userdata, $lang, $db, $nav_links, $phpEx, $SID;
  133.  
  134. //        $is_auth = auth(AUTH_VIEW, AUTH_LIST_ALL, $userdata);
  135.  
  136.         $sql = "SELECT c.cat_id, c.cat_title, c.cat_order
  137.                 FROM " . CATEGORIES_TABLE . " c, " . FORUMS_TABLE . " f
  138.                 WHERE f.cat_id = c.cat_id
  139.                 GROUP BY c.cat_id, c.cat_title, c.cat_order
  140.                 ORDER BY c.cat_order";
  141.         if ( !($result = $db->sql_query($sql)) )
  142.         {
  143.                 message_die(GENERAL_ERROR, "Couldn't obtain category list.", "", __LINE__, __FILE__, $sql);
  144.         }
  145.  
  146.         $category_rows = array();
  147.         while ( $row = $db->sql_fetchrow($result) )
  148.         {
  149.                 $category_rows[] = $row;
  150.         }
  151.  
  152.         if ( $total_categories = count($category_rows) )
  153.         {
  154.                 $sql = "SELECT *
  155.                         FROM " . FORUMS_TABLE . "
  156.                         ORDER BY cat_id, forum_order";
  157.                 if ( !($result = $db->sql_query($sql)) )
  158.                 {
  159.                         message_die(GENERAL_ERROR, 'Could not obtain forums information', '', __LINE__, __FILE__, $sql);
  160.                 }
  161.  
  162.                 $boxstring = '<select name="' . POST_FORUM_URL . '" onchange="if(this.options[this.selectedIndex].value != -1){ forms[\'jumpbox\'].submit() }"><option value="-1">' . $lang['Select_forum'] . '</option>';
  163.  
  164.                 $forum_rows = array();
  165.                 while ( $row = $db->sql_fetchrow($result) )
  166.                 {
  167.                         $forum_rows[] = $row;
  168.                 }
  169.  
  170.                 if ( $total_forums = count($forum_rows) )
  171.                 {
  172.                         for($i = 0; $i < $total_categories; $i++)
  173.                         {
  174.                                 $boxstring_forums = '';
  175.                                 for($j = 0; $j < $total_forums; $j++)
  176.                                 {
  177.                                         if ( $forum_rows[$j]['cat_id'] == $category_rows[$i]['cat_id'] && $forum_rows[$j]['auth_view'] <= AUTH_REG )
  178.                                         {
  179.  
  180. //                                        if ( $forum_rows[$j]['cat_id'] == $category_rows[$i]['cat_id'] && $is_auth[$forum_rows[$j]['forum_id']]['auth_view'] )
  181. //                                        {
  182.                                                 $selected = ( $forum_rows[$j]['forum_id'] == $match_forum_id ) ? 'selected="selected"' : '';
  183.                                                 $boxstring_forums .=  '<option value="' . $forum_rows[$j]['forum_id'] . '"' . $selected . '>' . $forum_rows[$j]['forum_name'] . '</option>';
  184.  
  185.                                                 //
  186.                                                 // Add an array to $nav_links for the Mozilla navigation bar.
  187.                                                 // 'chapter' and 'forum' can create multiple items, therefore we are using a nested array.
  188.                                                 //
  189.                                                 $nav_links['chapter forum'][$forum_rows[$j]['forum_id']] = array (
  190.                                                         'url' => append_sid("viewforum.$phpEx?" . POST_FORUM_URL . "=" . $forum_rows[$j]['forum_id']),
  191.                                                         'title' => $forum_rows[$j]['forum_name']
  192.                                                 );
  193.  
  194.                                         }
  195.                                 }
  196.  
  197.                                 if ( $boxstring_forums != '' )
  198.                                 {
  199.                                         $boxstring .= '<option value="-1"> </option>';
  200.                                         $boxstring .= '<option value="-1">' . $category_rows[$i]['cat_title'] . '</option>';
  201.                                         $boxstring .= '<option value="-1">----------------</option>';
  202.                                         $boxstring .= $boxstring_forums;
  203.                                 }
  204.                         }
  205.                 }
  206.  
  207.                 $boxstring .= '</select>';
  208.         }
  209.         else
  210.         {
  211.                 $boxstring .= '<select name="' . POST_FORUM_URL . '" onchange="if(this.options[this.selectedIndex].value != -1){ forms[\'jumpbox\'].submit() }"></select>';
  212.         }
  213.  
  214.         if ( !empty($SID) )
  215.         {
  216.                 $boxstring .= '<input type="hidden" name="sid" value="' . $SID . '" />';
  217.         }
  218.  
  219.         $template->set_filenames(array(
  220.                 'jumpbox' => 'jumpbox.tpl')
  221.         );
  222.         $template->assign_vars(array(
  223.                 'L_GO' => $lang['Go'],
  224.                 'L_JUMP_TO' => $lang['Jump_to'],
  225.                 'L_SELECT_FORUM' => $lang['Select_forum'],
  226.  
  227.                 'S_JUMPBOX_SELECT' => $boxstring,
  228.                 'S_JUMPBOX_ACTION' => append_sid($action))
  229.         );
  230.         $template->assign_var_from_handle('JUMPBOX', 'jumpbox');
  231.  
  232.         return;
  233. }
  234.  
  235. //
  236. // Initialise user settings on page load
  237. function init_userprefs($userdata)
  238. {
  239.         global $board_config, $theme, $images;
  240.         global $template, $lang, $phpEx, $phpbb_root_path;
  241.         global $nav_links;
  242.  
  243.         if ( $userdata['user_id'] != ANONYMOUS )
  244.         {
  245.                 if ( !empty($userdata['user_lang']))
  246.                 {
  247.                         $board_config['default_lang'] = $userdata['user_lang'];
  248.                 }
  249.  
  250.                 if ( !empty($userdata['user_dateformat']) )
  251.                 {
  252.                         $board_config['default_dateformat'] = $userdata['user_dateformat'];
  253.                 }
  254.  
  255.                 if ( isset($userdata['user_timezone']) )
  256.                 {
  257.                         $board_config['board_timezone'] = $userdata['user_timezone'];
  258.                 }
  259.         }
  260.  
  261.         if ( !file_exists(@phpbb_realpath($phpbb_root_path . 'language/lang_' . $board_config['default_lang'] . '/lang_main.'.$phpEx)) )
  262.         {
  263.                 $board_config['default_lang'] = 'czech';
  264.         }
  265.  
  266.         include($phpbb_root_path . 'language/lang_' . $board_config['default_lang'] . '/lang_main.' . $phpEx);
  267.  
  268.         if ( defined('IN_ADMIN') )
  269.         {
  270.                 if( !file_exists(@phpbb_realpath($phpbb_root_path . 'language/lang_' . $board_config['default_lang'] . '/lang_admin.'.$phpEx)) )
  271.                 {
  272.                         $board_config['default_lang'] = 'czech';
  273.                 }
  274.  
  275.                 include($phpbb_root_path . 'language/lang_' . $board_config['default_lang'] . '/lang_admin.' . $phpEx);
  276.         }
  277.  
  278.         //
  279.         // Set up style
  280.         //
  281.         if ( !$board_config['override_user_style'] )
  282.         {
  283.                 if ( $userdata['user_id'] != ANONYMOUS && $userdata['user_style'] > 0 )
  284.                 {
  285.                         if ( $theme = setup_style($userdata['user_style']) )
  286.                         {
  287.                                 return;
  288.                         }
  289.                 }
  290.         }
  291.  
  292.         $theme = setup_style($board_config['default_style']);
  293.  
  294.         //
  295.         // Mozilla navigation bar
  296.         // Default items that should be valid on all pages.
  297.         // Defined here to correctly assign the Language Variables
  298.         // and be able to change the variables within code.
  299.         //
  300.         $nav_links['top'] = array (
  301.                 'url' => append_sid($phpbb_root_path . 'index.' . $phpEx),
  302.                 'title' => sprintf($lang['Forum_Index'], $board_config['sitename'])
  303.         );
  304.         $nav_links['search'] = array (
  305.                 'url' => append_sid($phpbb_root_path . 'search.' . $phpEx),
  306.                 'title' => $lang['Search']
  307.         );
  308.         $nav_links['help'] = array (
  309.                 'url' => append_sid($phpbb_root_path . 'faq.' . $phpEx),
  310.                 'title' => $lang['FAQ']
  311.         );
  312.         $nav_links['author'] = array (
  313.                 'url' => append_sid($phpbb_root_path . 'memberlist.' . $phpEx),
  314.                 'title' => $lang['Memberlist']
  315.         );
  316.  
  317.         return;
  318. }
  319.  
  320. function setup_style($style)
  321. {
  322.         global $db, $board_config, $template, $images, $phpbb_root_path;
  323.         if($name == "Forums"){
  324.                 cookiedecode($user);
  325.             $info=sql_query("select * from ".$prefix."_bbconfig where config_name='default_style'", $dbi);
  326.             $get_info=mysql_fetch_array($info);
  327.             $default_style=$get_info[config_value];
  328.             if($cookie[1] == "" AND $style != "$default_style") {
  329.                 $style = "$default_style";
  330.             }
  331.         }
  332.  
  333.         $sql = "SELECT *
  334.                 FROM " . THEMES_TABLE . "
  335.                 WHERE themes_id = $style";
  336.         if ( !($result = $db->sql_query($sql)) )
  337.         {
  338.                 message_die(CRITICAL_ERROR, 'Could not query database for theme info');
  339.         }
  340.  
  341.         if ( !($row = $db->sql_fetchrow($result)) )
  342.         {
  343.                 message_die(CRITICAL_ERROR, "Could not get theme data for themes_id [$style]");
  344.         }
  345.  
  346.         $ThemeSel = get_theme();
  347.         if (file_exists("themes/$ThemeSel/forums/index_body.tpl")) {
  348.                 $template_path = "themes/$ThemeSel/";
  349.             $template_name = "forums";
  350.             $template = new Template($template_path . $template_name, $board_config, $db);
  351.         } else {
  352.                 $template_path = 'templates/' ;
  353.             $template_name = $row['template_name'] ;
  354.             $template = new Template($phpbb_root_path . $template_path . $template_name, $board_config, $db);
  355.         }
  356.  
  357.  
  358.  
  359.         if ( $template )
  360.         {
  361.                 $current_template_path = $template_path . $template_name;
  362.                 $ThemeSel = get_theme();
  363.                 if (file_exists("themes/$ThemeSel/$template_name/index_body.tpl")) {
  364.                     @include($template_path . $template_name . '/' . $template_name . '.cfg');
  365.                 } else {
  366.                     @include($phpbb_root_path . $template_path . $template_name . '/' . $template_name . '.cfg');
  367.                 }
  368.                 if ( !defined('TEMPLATE_CONFIG') )
  369.                 {
  370.                         message_die(CRITICAL_ERROR, "Could not open $template_name template config file", '', __LINE__, __FILE__);
  371.                 }
  372.  
  373.                 $img_lang = ( file_exists(@phpbb_realpath($phpbb_root_path . $current_template_path . '/images/lang_' . $board_config['default_lang'])) ) ? $board_config['default_lang'] : 'czech';
  374.  
  375.                 while( list($key, $value) = @each($images) )
  376.                 {
  377.                         if ( !is_array($value) )
  378.                         {
  379.                                 $images[$key] = str_replace('{LANG}', 'lang_' . $img_lang, $value);
  380.                         }
  381.                 }
  382.         }
  383.  
  384.         return $row;
  385. }
  386.  
  387. function encode_ip($dotquad_ip)
  388. {
  389.         $ip_sep = explode('.', $dotquad_ip);
  390.         return sprintf('%02x%02x%02x%02x', $ip_sep[0], $ip_sep[1], $ip_sep[2], $ip_sep[3]);
  391. }
  392.  
  393. function decode_ip($int_ip)
  394. {
  395.         $hexipbang = explode('.', chunk_split($int_ip, 2, '.'));
  396.         return hexdec($hexipbang[0]). '.' . hexdec($hexipbang[1]) . '.' . hexdec($hexipbang[2]) . '.' . hexdec($hexipbang[3]);
  397. }
  398.  
  399. //
  400. // Create date/time from format and timezone
  401. //
  402. function create_date($format, $gmepoch, $tz)
  403. {
  404.         global $board_config, $lang;
  405.         static $translate;
  406.  
  407.         if ( empty($translate) && $board_config['default_lang'] != 'english' )
  408.         {
  409.                 @reset($lang['datetime']);
  410.                 while ( list($match, $replace) = @each($lang['datetime']) )
  411.                 {
  412.                         $translate[$match] = $replace;
  413.                 }
  414.         }
  415.  
  416.     // Begin Daylight Saving Time MOD
  417.     //   return ( !empty($translate) ) ? strtr(@gmdate($format, $gmepoch + (3600 * $tz)), $translate) : @gmdate($format, $gmepoch + (3600 * $tz));
  418.     
  419.     $dst = date('I', $gmepoch) * 3600;
  420.     
  421.     return ( !empty($translate) ) ? strtr(@gmdate($format, $gmepoch + (3600 * $tz) + $dst), $translate) : @gmdate($format, $gmepoch + (3600 * $tz) + $dst);
  422.     // End Daylight Saving Time MOD 
  423. }
  424.  
  425. //
  426. // Pagination routine, generates
  427. // page number sequence
  428. //
  429. function generate_pagination($base_url, $num_items, $per_page, $start_item, $add_prevnext_text = TRUE)
  430. {
  431.         global $lang;
  432.  
  433.         $total_pages = ceil($num_items/$per_page);
  434.  
  435.         if ( $total_pages == 1 )
  436.         {
  437.                 return '';
  438.         }
  439.  
  440.         $on_page = floor($start_item / $per_page) + 1;
  441.  
  442.         $page_string = '';
  443.         if ( $total_pages > 10 )
  444.         {
  445.                 $init_page_max = ( $total_pages > 3 ) ? 3 : $total_pages;
  446.  
  447.                 for($i = 1; $i < $init_page_max + 1; $i++)
  448.                 {
  449.                         $page_string .= ( $i == $on_page ) ? '<b>' . $i . '</b>' : '<a href="' . append_sid($base_url . "&start=" . ( ( $i - 1 ) * $per_page ) ) . '">' . $i . '</a>';
  450.                         if ( $i <  $init_page_max )
  451.                         {
  452.                                 $page_string .= ", ";
  453.                         }
  454.                 }
  455.  
  456.                 if ( $total_pages > 3 )
  457.                 {
  458.                         if ( $on_page > 1  && $on_page < $total_pages )
  459.                         {
  460.                                 $page_string .= ( $on_page > 5 ) ? ' ... ' : ', ';
  461.  
  462.                                 $init_page_min = ( $on_page > 4 ) ? $on_page : 5;
  463.                                 $init_page_max = ( $on_page < $total_pages - 4 ) ? $on_page : $total_pages - 4;
  464.  
  465.                                 for($i = $init_page_min - 1; $i < $init_page_max + 2; $i++)
  466.                                 {
  467.                                         $page_string .= ($i == $on_page) ? '<b>' . $i . '</b>' : '<a href="' . append_sid($base_url . "&start=" . ( ( $i - 1 ) * $per_page ) ) . '">' . $i . '</a>';
  468.                                         if ( $i <  $init_page_max + 1 )
  469.                                         {
  470.                                                 $page_string .= ', ';
  471.                                         }
  472.                                 }
  473.  
  474.                                 $page_string .= ( $on_page < $total_pages - 4 ) ? ' ... ' : ', ';
  475.                         }
  476.                         else
  477.                         {
  478.                                 $page_string .= ' ... ';
  479.                         }
  480.  
  481.                         for($i = $total_pages - 2; $i < $total_pages + 1; $i++)
  482.                         {
  483.                                 $page_string .= ( $i == $on_page ) ? '<b>' . $i . '</b>'  : '<a href="' . append_sid($base_url . "&start=" . ( ( $i - 1 ) * $per_page ) ) . '">' . $i . '</a>';
  484.                                 if( $i <  $total_pages )
  485.                                 {
  486.                                         $page_string .= ", ";
  487.                                 }
  488.                         }
  489.                 }
  490.         }
  491.         else
  492.         {
  493.                 for($i = 1; $i < $total_pages + 1; $i++)
  494.                 {
  495.                         $page_string .= ( $i == $on_page ) ? '<b>' . $i . '</b>' : '<a href="' . append_sid($base_url . "&start=" . ( ( $i - 1 ) * $per_page ) ) . '">' . $i . '</a>';
  496.                         if ( $i <  $total_pages )
  497.                         {
  498.                                 $page_string .= ', ';
  499.                         }
  500.                 }
  501.         }
  502.  
  503.         if ( $add_prevnext_text )
  504.         {
  505.                 if ( $on_page > 1 )
  506.                 {
  507.                         $page_string = ' <a href="' . append_sid($base_url . "&start=" . ( ( $on_page - 2 ) * $per_page ) ) . '">' . $lang['Previous'] . '</a>  ' . $page_string;
  508.                 }
  509.  
  510.                 if ( $on_page < $total_pages )
  511.                 {
  512.                         $page_string .= '  <a href="' . append_sid($base_url . "&start=" . ( $on_page * $per_page ) ) . '">' . $lang['Next'] . '</a>';
  513.                 }
  514.  
  515.         }
  516.  
  517.         $page_string = $lang['Goto_page'] . ' ' . $page_string;
  518.  
  519.         return $page_string;
  520. }
  521.  
  522. //
  523. // This does exactly what preg_quote() does in PHP 4-ish
  524. // If you just need the 1-parameter preg_quote call, then don't bother using this.
  525. //
  526. function phpbb_preg_quote($str, $delimiter)
  527. {
  528.         $text = preg_quote($str);
  529.         $text = str_replace($delimiter, '\\' . $delimiter, $text);
  530.  
  531.         return $text;
  532. }
  533.  
  534. //
  535. // Obtain list of naughty words and build preg style replacement arrays for use by the
  536. // calling script, note that the vars are passed as references this just makes it easier
  537. // to return both sets of arrays
  538. //
  539. function obtain_word_list(&$orig_word, &$replacement_word)
  540. {
  541.         global $db;
  542.         //
  543.         // Define censored word matches
  544.         //
  545.         $sql = "SELECT word, replacement
  546.                 FROM  " . WORDS_TABLE;
  547.         if( !($result = $db->sql_query($sql)) )
  548.         {
  549.                 message_die(GENERAL_ERROR, 'Could not get censored words from database', '', __LINE__, __FILE__, $sql);
  550.         }
  551.         if ( $row = $db->sql_fetchrow($result) )
  552.         {
  553.                 do
  554.                 {
  555.                         $orig_word[] = '#\b(' . str_replace('\*', '\w*?', phpbb_preg_quote($row['word'], '#')) . ')\b#i';
  556.                         $replacement_word[] = $row['replacement'];
  557.                 }
  558.                 while ( $row = $db->sql_fetchrow($result) );
  559.         }
  560.         return true;
  561. }
  562. //
  563. // This is general replacement for die(), allows templated
  564. // output in users (or default) language, etc.
  565. //
  566. // $msg_code can be one of these constants:
  567. //
  568. // GENERAL_MESSAGE : Use for any simple text message, eg. results
  569. // of an operation, authorisation failures, etc.
  570. //
  571. // GENERAL ERROR : Use for any error which occurs _AFTER_ the
  572. // common.php include and session code, ie. most errors in
  573. // pages/functions
  574. //
  575. // CRITICAL_MESSAGE : Used when basic config data is available but
  576. // a session may not exist, eg. banned users
  577. //
  578. // CRITICAL_ERROR : Used when config data cannot be obtained, eg
  579. // no database connection. Should _not_ be used in 99.5% of cases
  580. //
  581. function message_die($msg_code, $msg_text = '', $msg_title = '', $err_line = '', $err_file = '', $sql = '')
  582. {
  583.         global $db, $template, $board_config, $theme, $lang, $phpEx, $phpbb_root_path, $nav_links, $gen_simple_header, $images;
  584.         global $userdata, $user_ip, $session_length;
  585.         global $starttime;
  586.  
  587.         if(defined('HAS_DIED'))
  588.         {
  589.                 die("message_die() was called multiple times. This isn't supposed to happen. Was message_die() used in page_tail.php?");
  590.         }
  591.  
  592.         define(HAS_DIED, 1);
  593.  
  594.  
  595.         $sql_store = $sql;
  596.  
  597.         //
  598.         // Get SQL error if we are debugging. Do this as soon as possible to prevent
  599.         // subsequent queries from overwriting the status of sql_error()
  600.         //
  601.         if ( DEBUG && ( $msg_code == GENERAL_ERROR || $msg_code == CRITICAL_ERROR ) )
  602.         {
  603.                 $sql_error = $db->sql_error();
  604.  
  605.                 $debug_text = '';
  606.  
  607.                 if ( $sql_error['message'] != '' )
  608.                 {
  609.                         $debug_text .= '<br /><br />SQL Error : ' . $sql_error['code'] . ' ' . $sql_error['message'];
  610.                 }
  611.  
  612.                 if ( $sql_store != '' )
  613.                 {
  614.                         $debug_text .= "<br /><br />$sql_store";
  615.                 }
  616.  
  617.                 if ( $err_line != '' && $err_file != '' )
  618.                 {
  619.                         $debug_text .= '</br /><br />Line : ' . $err_line . '<br />File : ' . $err_file;
  620.                 }
  621.         }
  622.  
  623.         if( empty($userdata) && ( $msg_code == GENERAL_MESSAGE || $msg_code == GENERAL_ERROR ) )
  624.         {
  625.                 $userdata = session_pagestart($user_ip, PAGE_INDEX, $nukeuser);
  626.                 init_userprefs($userdata);
  627.         }
  628.  
  629.         //
  630.         // If the header hasn't been output then do it
  631.         //
  632.         if ( !defined('HEADER_INC') && $msg_code != CRITICAL_ERROR )
  633.         {
  634.                 if ( empty($lang) )
  635.                 {
  636.                         if ( !empty($board_config['default_lang']) )
  637.                         {
  638.                                 include($phpbb_root_path . 'language/lang_' . $board_config['default_lang'] . '/lang_main.'.$phpEx);
  639.                         }
  640.                         else
  641.                         {
  642.                                 include($phpbb_root_path . 'language/lang_czech/lang_main.'.$phpEx);
  643.                         }
  644.                 }
  645.  
  646.                 if ( empty($template) )
  647.                 {
  648.                         $ThemeSel = get_theme();
  649.                         if (file_exists("themes/$ThemeSel/forums/".$board_config['board_template']."/index_body.tpl")) {
  650.                             $template = new Template("themes/$ThemeSel/forums/".$board_config['board_template']."");
  651.                         } else {
  652.                             $template = new Template($phpbb_root_path . 'templates/' . $board_config['board_template']);
  653.                         }
  654.                 }
  655.                 if ( empty($theme) )
  656.                 {
  657.                         $theme = setup_style($board_config['default_style']);
  658.                 }
  659.  
  660.                 //
  661.                 // Load the Page Header
  662.                 //
  663.                 if ( !defined('IN_ADMIN') )
  664.                 {
  665.                         include("includes/page_header.php");
  666.                 }
  667.                 else
  668.                 {
  669.                         include($phpbb_root_path . 'admin/page_header_admin.'.$phpEx);
  670.                 }
  671.         }
  672.  
  673.         switch($msg_code)
  674.         {
  675.                 case GENERAL_MESSAGE:
  676.                         if ( $msg_title == '' )
  677.                         {
  678.                                 $msg_title = $lang['Information'];
  679.                         }
  680.                         break;
  681.  
  682.                 case CRITICAL_MESSAGE:
  683.                         if ( $msg_title == '' )
  684.                         {
  685.                                 $msg_title = $lang['Critical_Information'];
  686.                         }
  687.                         break;
  688.  
  689.                 case GENERAL_ERROR:
  690.                         if ( $msg_text == '' )
  691.                         {
  692.                                 $msg_text = $lang['An_error_occured'];
  693.                         }
  694.  
  695.                         if ( $msg_title == '' )
  696.                         {
  697.                                 $msg_title = $lang['General_Error'];
  698.                         }
  699.                         break;
  700.  
  701.                 case CRITICAL_ERROR:
  702.                         //
  703.                         // Critical errors mean we cannot rely on _ANY_ DB information being
  704.                         // available so we're going to dump out a simple echo'd statement
  705.                         //
  706.                         include($phpbb_root_path . 'language/lang_czech/lang_main.'.$phpEx);
  707.  
  708.                         if ( $msg_text == '' )
  709.                         {
  710.                                 $msg_text = $lang['A_critical_error'];
  711.                         }
  712.  
  713.                         if ( $msg_title == '' )
  714.                         {
  715.                                 $msg_title = 'phpBB : <b>' . $lang['Critical_Error'] . '</b>';
  716.                         }
  717.                         break;
  718.         }
  719.  
  720.         //
  721.         // Add on DEBUG info if we've enabled debug mode and this is an error. This
  722.         // prevents debug info being output for general messages should DEBUG be
  723.         // set TRUE by accident (preventing confusion for the end user!)
  724.         //
  725.         if ( DEBUG && ( $msg_code == GENERAL_ERROR || $msg_code == CRITICAL_ERROR ) )
  726.         {
  727.                 if ( $debug_text != '' )
  728.                 {
  729.                         $msg_text = $msg_text . '<br /><br /><b><u>DEBUG MODE</u></b>' . $debug_text;
  730.                 }
  731.         }
  732.  
  733.         if ( $msg_code != CRITICAL_ERROR )
  734.         {
  735.                 if ( !empty($lang[$msg_text]) )
  736.                 {
  737.                         $msg_text = $lang[$msg_text];
  738.                 }
  739.  
  740.                 if ( !defined('IN_ADMIN') )
  741.                 {
  742.                         $template->set_filenames(array(
  743.                                 'message_body' => 'message_body.tpl')
  744.                         );
  745.                 }
  746.                 else
  747.                 {
  748.                         $template->set_filenames(array(
  749.                                 'message_body' => 'admin/admin_message_body.tpl')
  750.                         );
  751.                 }
  752.  
  753.                 $template->assign_vars(array(
  754.                         'MESSAGE_TITLE' => $msg_title,
  755.                         'MESSAGE_TEXT' => $msg_text)
  756.                 );
  757.                 $template->pparse('message_body');
  758.  
  759.                 if ( !defined('IN_ADMIN') )
  760.                 {
  761.                         include("includes/page_tail.php");
  762.                 }
  763.                 else
  764.                 {
  765.                         include($phpbb_root_path . 'admin/page_footer_admin.'.$phpEx);
  766.                 }
  767.         }
  768.         else
  769.         {
  770.                 echo "<html>\n<body>\n" . $msg_title . "\n<br /><br />\n" . $msg_text . "</body>\n</html>";
  771.         }
  772.  
  773.         exit;
  774. }
  775.  
  776. //
  777. // This function is for compatibility with PHP 4.x's realpath()
  778. // function.  In later versions of PHP, it needs to be called
  779. // to do checks with some functions.  Older versions of PHP don't
  780. // seem to need this, so we'll just return the original value.
  781. // dougk_ff7 <October 5, 2002>
  782. function phpbb_realpath($path)
  783. {
  784.         global $phpbb_root_path, $phpEx;
  785.  
  786.         return (!@function_exists('realpath') || !@realpath($phpbb_root_path . 'includes/functions.'.$phpEx)) ? $path : @realpath($path);
  787. }
  788.  
  789. function redirect($url)
  790. {
  791.         global $db, $board_config;
  792.  
  793.         if (!empty($db))
  794.         {
  795.                 $db->sql_close();
  796.         }
  797.  
  798.         $server_protocol = ($board_config['cookie_secure']) ? 'https://' : 'http://';
  799.         $server_name = preg_replace('#^\/?(.*?)\/?$#', '\1', trim($board_config['server_name']));
  800.         $server_port = ($board_config['server_port'] <> 80) ? ':' . trim($board_config['server_port']) : '';
  801.         $script_name = preg_replace('#^\/?(.*?)\/?$#', '\1', trim($board_config['script_path']));
  802.         $script_name = ($script_name == '') ? $script_name : '/' . $script_name;
  803.         $url = preg_replace('#^\/?(.*?)\/?$#', '/\1', trim($url));
  804.  
  805.         // Redirect via an HTML form for PITA webservers
  806.         if (@preg_match('/Microsoft|WebSTAR|Xitami/', getenv('SERVER_SOFTWARE')))
  807.         {
  808.                 header('Refresh: 0; URL=' . $server_protocol . $server_name . $server_port . $script_name . $url);
  809.                 echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><meta http-equiv="refresh" content="0; url=' . $server_protocol . $server_name . $server_port . $script_name . $url . '"><title>Redirect</title></head><body><div align="center">If your browser does not support meta redirection please click <a href="' . $server_protocol . $server_name . $server_port . $script_name . $url . '">HERE</a> to be redirected</div></body></html>';
  810.                 exit;
  811.         }
  812.  
  813.         // Behave as per HTTP/1.1 spec for others
  814.         header('Location: ' . $server_protocol . $server_name . $server_port . $script_name . $url);
  815.         exit;
  816. }
  817. function bblogin($nukeuser, $session_id) {
  818.         global $nukeuser, $userdata, $user_ip, $session_length, $session_id, $db, $nuke_file_path;
  819.         define("IN_LOGIN", true);
  820.         $cookie = explode(":", $nukeuser);
  821.         $nuid = $cookie[0];
  822.         $sql = "SELECT s.*
  823.                 FROM " . SESSIONS_TABLE . " s
  824.                 WHERE s.session_id = '$session_id'
  825.                 AND s.session_ip = '$user_ip'";
  826.         if ( !($result = $db->sql_query($sql)) )
  827.         {
  828.                 message_die(CRITICAL_ERROR, 'Error doing DB query userdata row fetch : session_pagestar');
  829.         }
  830.         $logindata = $db->sql_fetchrow($result);
  831.         if( $nuid != $logindata['session_user_id'] ) {
  832.             $nusername = $cookie[1];
  833.             $sql = "SELECT user_id, username, user_password, user_active, user_level
  834.                     FROM ".USERS_TABLE."
  835.                     WHERE username = '" . str_replace("\'", "''", $nusername) . "'";
  836.             $result = $db->sql_query($sql);
  837.             if(!$result) {
  838.                 message_die(GENERAL_ERROR, "Error in obtaining userdata : login", "", __LINE__, __FILE__, $sql);
  839.             }
  840.             $rowresult = $db->sql_fetchrow($result);
  841.             $password = $cookie[2];
  842.             if(count($rowresult) ) {
  843.                 if( $rowresult['user_level'] != ADMIN && $board_config['board_disable'] ) {
  844.                     header("Location: " . append_sid("index.php", true));
  845.                 } else {
  846.                     if( $password == $rowresult['user_password'] && $rowresult['user_active'] ) {
  847.                         $autologin = 0;
  848.                         $userdata = session_begin($rowresult['user_id'], $user_ip, PAGE_INDEX, $session_length, FALSE, $autologin);
  849.                         $session_id = $userdata['session_id'];
  850.                         if(!$session_id ) {
  851.                             message_die(CRITICAL_ERROR, "Couldn't start session : login", "", __LINE__, __FILE__);
  852.                         } else {
  853.                         }
  854.                     } else {
  855.                         $message = $lang['Error_login'] . "<br /><br />" . sprintf($lang['Click_return_login'], "<a href=\"" . append_sid("modules.php?name=Forums&file=login&$redirect") . "\">", "</a> ") . "<br /><br />" .  sprintf($lang['Click_return_index'], "<a href=\"" . append_sid("index.php") . "\">", "</a> ");
  856.                         message_die(GENERAL_MESSAGE, $message);
  857.                     }
  858.                 }
  859.             } else {
  860.                 $message = $lang['Error_login'] . "<br /><br />" . sprintf($lang['Click_return_login'], "<a href=\"" . append_sid("modules.php?name=Forums&file=login&$redirect") . "\">", "</a> ") . "<br /><br />" .  sprintf($lang['Click_return_index'], "<a href=\"" . append_sid("index.php") . "\">", "</a> ");
  861.                 message_die(GENERAL_MESSAGE, $message);
  862.             }
  863.         }
  864.  
  865. }
  866. ?>