SSI script | > Script written by Matt Mecham | > Date started: 29th April 2002 | > UPDATED: 1st July 2004 | +-------------------------------------------------------------------------- +-------------------------------------------------------------------------- | USAGE: +-------------------------------------------------------------------------- Simply call this script via PHP includes, or SSI .shtml tags to generate content on the fly, streamed into your own webpage. +-------------------------------------------------------------------------- | To show the last 10 topics and posts in the news forums... +-------------------------------------------------------------------------- include("http://domain.com/forums/ssi.php?a=news&show=10"); You can adjust the "show" attribute to display a different amount of topics. +-------------------------------------------------------------------------- | To show the board statistics +-------------------------------------------------------------------------- include("http://domain.com/forums/ssi.php?a=stats"); +-------------------------------------------------------------------------- | To show the active users stats (x Members, X Guests, etc) +-------------------------------------------------------------------------- include("http://domain.com/forums/ssi.php?a=active"); +-------------------------------------------------------------------------- | RSS / XML Syndication.. +-------------------------------------------------------------------------- RSS: http://domain.com/forums/ssi.php?a=out&f=1,2,3,4,5&show=10&type=rss XML: http://domain.com/forums/ssi.php?a=out&f=1,2,3,4,5&show=10&type=xml Will show last 10 topics in reverse chronological last post date order from all the forums in the comma separated list */ //----------------------------------------------- // USER CONFIGURABLE ELEMENTS //----------------------------------------------- // Root path define( 'ROOT_PATH' , "./" ); define( 'KERNEL_PATH', ROOT_PATH.'ips_kernel/' ); // Enable module usage? // (Vital for some mods and IPB enhancements) define ( 'USE_MODULES', 1 ); // Enable shut down features? // Uses PHPs register_shutdown_function to save // low priority tasks until end of exec define ( 'USE_SHUTDOWN', 1 ); $templates_dir = ROOT_PATH."ssi_templates"; $max_show = 100; // Maximum number of topics possible to show... $allow_syndication = 1; // To turn off, use $allow_syndication = 0; //----------------------------------------------- // NO USER EDITABLE SECTIONS BELOW //----------------------------------------------- define ( 'IN_IPB', 1 ); define ( 'IN_DEV', 0 ); error_reporting (E_ERROR | E_WARNING | E_PARSE); set_magic_quotes_runtime(0); class info { var $input = array(); var $base_url = ""; var $vars = ""; function info() { global $sess, $std, $DB, $INFO; $this->vars = &$INFO; } } //-------------------------------- // Import $INFO, now! //-------------------------------- require ROOT_PATH."conf_global.php"; //-------------------------------- // Require our global functions //-------------------------------- require ROOT_PATH."sources/functions.php"; $std = new FUNC; //-------------------------------- // Load the DB driver and such //-------------------------------- $INFO['sql_driver'] = ! $INFO['sql_driver'] ? 'mysql' : strtolower($INFO['sql_driver']); require ( KERNEL_PATH.'class_db_'.$INFO['sql_driver'].".php" ); $DB = new db_driver; $DB->obj['sql_database'] = $INFO['sql_database']; $DB->obj['sql_user'] = $INFO['sql_user']; $DB->obj['sql_pass'] = $INFO['sql_pass']; $DB->obj['sql_host'] = $INFO['sql_host']; $DB->obj['sql_tbl_prefix'] = $INFO['sql_tbl_prefix']; $DB->obj['query_cache_file'] = ROOT_PATH.'sources/sql/'.$INFO['sql_driver'].'_queries.php'; $DB->obj['use_shutdown'] = USE_SHUTDOWN; //----------------------------------- // Required vars? //----------------------------------- if ( is_array( $DB->connect_vars ) and count( $DB->connect_vars ) ) { foreach( $DB->connect_vars as $k => $v ) { $DB->connect_vars[ $k ] = $INFO[ $k ]; } } //-------------------------------- // Get a DB connection //-------------------------------- $DB->connect(); //-------------------------------- // Make CONSTANT //-------------------------------- define( 'SQL_PREFIX', $DB->obj['sql_tbl_prefix'] ); define( 'SQL_DRIVER', $INFO['sql_driver'] ); //-------------------------------- // Wrap it all up in a nice easy to // transport super class //-------------------------------- $ibforums = new info(); //-------------------------------- // Set up our vars //-------------------------------- $ibforums->input = $std->parse_incoming(); $ibforums->base_url = $ibforums->vars['board_url'].'/index.'.$ibforums->vars['php_ext']; //=========================================================================== // Get cache... //=========================================================================== $DB->simple_construct( array( 'select' => '*', 'from' => 'cache_store', 'where' => "cs_key IN ( 'banfilters', 'settings', 'group_cache', 'systemvars', 'skin_id_cache', 'forum_cache', 'moderators', 'stats', 'languages' )" ) ); $DB->simple_exec(); while ( $r = $DB->fetch_row() ) { if ( $r['cs_key'] == 'settings' ) { $tmp = unserialize(stripslashes($r['cs_value'])); if ( is_array( $tmp ) and count( $tmp ) ) { foreach( $tmp as $k => $v ) { $ibforums->vars[ $k ] = $v; } } unset( $tmp ); } else { if ( $r['cs_array'] ) { $ibforums->cache[ $r['cs_key'] ] = unserialize(stripslashes($r['cs_value'])); } else { $ibforums->cache[ $r['cs_key'] ] = $r['cs_value']; } } } //-------------------------------- // Initialize the FUNC //-------------------------------- $std->FUNC_init(); $std->load_skin(); //-------------------------------- // What to do? //-------------------------------- switch ($ibforums->input['a']) { case 'news': do_news(); break; case 'active': do_active(); break; case 'stats': do_stats(); break; case 'out': if ( $allow_syndication == 1 ) { do_syndication(); } else { exit(); } break; default: echo("An error occured whilst processing this directive"); exit(); break; } //+------------------------------------------------- // SYNDICATION! //+------------------------------------------------- function do_syndication() { global $DB, $ibforums, $templates_dir, $std, $max_show; //---------------------------------------- // Sort out the forum ids //---------------------------------------- $tmp_forums = array(); $forums = array(); if ( $ibforums->input['f'] ) { $tmp_forums = explode( ",", $ibforums->input['f'] ); } else { fatal_error("Fatal error: no forum id specified"); } foreach ($tmp_forums as $f ) { $f = intval($f); if ( $f ) { $forums[] = $f; } } if ( count($forums) < 1 ) { fatal_error("Fatal error: no forum id specified"); } $sql_fields = implode( ",", $forums ); //---------------------------------------- // Number of topics to return? //---------------------------------------- $perpage = intval($ibforums->input['show']) ? intval($ibforums->input['show']) : 10; $perpage = ( $perpage > $max_show ) ? $max_show : $perpage; //---------------------------------------- // Load the template... //---------------------------------------- if ( $ibforums->input['type'] == 'xml' ) { $template = load_template("syndicate_xml.html"); } else { $template = load_template("syndicate_rss.html"); } //---------------------------------------- // parse.. //---------------------------------------- $to_echo = ""; $top = ""; $row = ""; $bottom = ""; preg_match( "#\[TOP\](.+?)\[/TOP\]#is", $template, $match ); $top = trim($match[1]); preg_match( "#\[ROW\](.+?)\[/ROW\]#is", $template, $match ); $row = trim($match[1]); preg_match( "#\[BOTTOM\](.+?)\[/BOTTOM\]#is", $template, $match ); $bottom = trim($match[1]); //---------------------------------------- // Header parse... //---------------------------------------- @header('Content-Type: text/xml'); @header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT'); @header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); @header('Pragma: public'); $to_echo .= parse_template( $top, array ( 'board_url' => $ibforums->base_url , 'board_name' => $ibforums->vars['board_name'], ) ); $group = $ibforums->cache['group_cache'][ $ibforums->vars['guest_group'] ]; $ibforums->perm_id_array = explode( ",", $group['g_perm_id'] ); //---------------------------------------- // Get the topics, member info and other stuff //---------------------------------------- $DB->simple_construct( array( 'select' => '*', 'from' => 'topics', 'where' => "forum_id IN ($sql_fields) AND approved=1", 'order' => 'last_post DESC', 'limit' => array( 0, $perpage ) ) ); $DB->simple_exec(); if ( ! $DB->get_num_rows() ) { fatal_error("Could not get the information from the database"); } while ( $i = $DB->fetch_row() ) { $forum = $ibforums->cache['forum_cache'][ $i['forum_id'] ]; if ( $std->check_perms( $forum['read_perms'] ) != TRUE ) { continue; } if ($forum['password'] != "" ) { continue; } $to_echo .= parse_template( $row, array ( 'topic_title' => str_replace( '&#', '&#', $i['title'] ), 'topic_id' => $i['tid'], 'topic_link' => $ibforums->base_url."?showtopic=".$i['tid'], 'forum_title' => htmlspecialchars($i['forum_name']), 'forum_id' => $i['forum_id'], 'last_poster_id' => $i['last_poster_id'], 'last_post_name' => $i['last_poster_name'], 'last_post_time' => $std->get_date( $i['last_post'] , 'LONG' ), 'timestamp' => $i['last_post'], 'starter_id' => $i['starter_id'], 'starter_name' => $i['starter_name'], 'board_url' => $ibforums->base_url , 'board_name' => $ibforums->vars['board_name'], 'rfc_date' => date( 'r', $i['last_post'] ), ) ) . "\r\n"; } echo $to_echo."\r\n".$bottom; exit(); } //+------------------------------------------------- // Import the stats! WOOHOO //+------------------------------------------------- function do_stats() { global $DB, $ibforums, $templates_dir, $std; // Load the template... $template = load_template("stats.html"); $to_echo = ""; // Get the topics, member info and other stuff $time = time() - 900; if ( ! is_array( $ibforums->cache['stats'] ) ) { $ibforums->cache['stats'] = array(); $DB->simple_exec_query( array( 'delete' => 'cache_store', 'where' => "cs_key='stats'" ) ); $DB->do_insert( 'cache_store', array( 'cs_array' => 1, 'cs_key' => 'stats', 'cs_value' => addslashes(serialize($ibforums->cache['stats'])) ) ); } $stats = $ibforums->cache['stats']; $total_posts = $stats['total_replies']+$stats['total_topics']; $to_echo = parse_template( $template, array ( 'total_posts' => $total_posts, 'topics' => $stats['total_topics'], 'replies' => $stats['total_replies'], 'members' => $stats['mem_count'] ) ); echo $to_echo; exit(); } function do_news() { global $DB, $ibforums, $templates_dir, $std, $max_show; if ( (! $ibforums->vars['news_forum_id']) or ($ibforums->vars['news_forum_id'] == "" ) ) { fatal_error("No news forum assigned"); } require ROOT_PATH."sources/lib/post_parser.php"; $parser = new post_parser(); $perpage = intval($ibforums->input['show']) > 0 ? intval($ibforums->input['show']) : 10; $perpage = ( $perpage > $max_show ) ? $max_show : $perpage; //----------------------------------------- // Load the template... //----------------------------------------- $template = load_template("news.html"); $to_echo = ""; //----------------------------------------- // Get the topics, member info and other stuff //----------------------------------------- $DB->query("SELECT m.name as member_name, m.mgroup, m.id as member_id,m.title as member_title, t.*, p.* FROM ".SQL_PREFIX."topics t LEFT JOIN ".SQL_PREFIX."posts p ON (p.new_topic=1 AND p.topic_id=t.tid) LEFT JOIN ".SQL_PREFIX."members m ON (m.id=t.starter_id) WHERE t.forum_id={$ibforums->vars['news_forum_id']} AND t.approved=1 ORDER BY t.tid DESC LIMIT 0, $perpage"); if ( ! $DB->get_num_rows() ) { fatal_error("Could not get the information from the database"); } while ( $row = $DB->fetch_row() ) { $row['post'] = str_replace( '
', '
', $row['post'] ); $parser->pp_do_html = ( $ibforums->cache['forum_cache'][ $row['forum_id'] ]['use_html'] and $ibforums->cache['group_cache'][ $row['mgroup'] ]['g_dohtml'] and $row['post_htmlstate'] ) ? 1 : 0; $parser->pp_wordwrap = $ibforums->vars['post_wordwrap']; $parser->pp_nl2br = $row['post_htmlstate'] == 2 ? 1 : 0; $row['post'] = $parser->post_db_parse( $row['post'] ); $to_echo .= parse_template( $template, array ( 'profile_link' => $ibforums->base_url."?act=Profile&CODE=03&MID=".$row['member_id'], 'member_name' => $row['member_name'], 'post_date' => $std->get_date( $row['post_date'], 'LONG', 1 ), 'topic_title' => $row['title'], 'post' => $row['post'], 'comments' => $row['posts'], 'view_all_link' => $ibforums->base_url."?act=ST&f={$row['forum_id']}&t={$row['tid']}" ) ); } $to_echo = str_replace( "<#IMG_DIR#>", $ibforums->skin['_imagedir'], $to_echo ); $to_echo = str_replace( "<#EMO_DIR#>", $ibforums->skin['_emodir'] , $to_echo ); echo $to_echo; exit(); } function do_active() { global $DB, $ibforums, $templates_dir, $std; // Load the template... $template = load_template("active.html"); $to_echo = ""; if ($ibforums->vars['au_cutoff'] == "") { $ibforums->vars['au_cutoff'] = 15; } //-------------------------------- // Get the users from the DB //-------------------------------- $cut_off = $ibforums->vars['au_cutoff'] * 60; $time = time() - $cut_off; $rows = array( 0 => array( 'login_type' => substr($ibforums->member['login_anonymous'],0, 1), 'running_time' => time(), 'member_id' => $ibforums->member['id'], 'member_name' => $ibforums->member['name'], 'member_group' => $ibforums->member['mgroup'] ) ); $DB->simple_construct( array( 'select' => 'id, member_id, member_name, login_type, running_time, member_group', 'from' => 'sessions', 'where' => "running_time > $time", 'order' => "running_time DESC" ) ); $DB->simple_exec(); //----------------------------------------- // FETCH... //----------------------------------------- while ($r = $DB->fetch_row() ) { $rows[] = $r; } //-------------------------------- // cache all printed members so we // don't double print them //-------------------------------- $cached = array(); $active = array(); foreach( $rows as $result ) { if ($result['member_id'] == 0) { $active['GUESTS']++; } else { if (empty( $cached[ $result['member_id'] ] ) ) { $cached[ $result['member_id'] ] = 1; if ($result['login_type'] == 1) { $active['ANON']++; } else { $active['MEMBERS']++; } } } } $active['TOTAL'] = $active['MEMBERS'] + $active['GUESTS'] + $active['ANON']; $to_echo = parse_template( $template, array ( 'total' => $active['TOTAL'] ? $active['TOTAL'] : 0 , 'members' => $active['MEMBERS'] ? $active['MEMBERS'] : 0, 'guests' => $active['GUESTS'] ? $active['GUESTS'] : 0, 'anon' => $active['ANON'] ? $active['ANON'] : 0, ) ); echo $to_echo; exit(); } //+------------------------------------------------- // GLOBAL ROUTINES //+------------------------------------------------- function parse_template( $template, $assigned=array() ) { foreach( $assigned as $word => $replace) { $template = preg_replace( "/\{$word\}/i", "$replace", $template ); } return $template; } function load_template($template="") { global $templates_dir; $filename = $templates_dir."/".$template; if ( file_exists($filename) ) { if ( $FH = fopen($filename, 'r') ) { $template = fread( $FH, filesize($filename) ); fclose($FH); } else { fatal_error("Couldn't open the template file"); } } else { fatal_error("Template file does not exist"); } return $template; } function fatal_error($message="") { echo("An error occured whilst processing this directive"); if ($message) { echo("
$message"); } exit(); } ?>