Topic: My version of AP_News_Generator

this is a modified version of the news generator created by Rickard.
this version will produce a navigation (only back and next) at the end of the page.

<?php
/***********************************************************************

  Copyright (C) 2002-2005  Rickard Andersson (rickard@punbb.org)

  This file is part of PunBB.

  PunBB is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License as published
  by the Free Software Foundation; either version 2 of the License,
  or (at your option) any later version.

  PunBB is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  MA  02111-1307  USA

************************************************************************/
//This a modified version of hte News Generator created by Rickard Andersson
//Piero Orlando

// The forum from which we'll pull the news bits
$forum_id = 1;

// Number of news posts to include in the index
$num_posts_index = 5;

// Path to news item template
$template_path = PUN_ROOT.'plugins/AP_News_Generator/news.tpl';

// Directories in which plugin will save generated markup (must end with slash)
$output_dir_latest = PUN_ROOT.'plugins/AP_News_Generator/';
//no archive needed
//$output_dir_archive = PUN_ROOT.'plugins/AP_Share_Generator/archive/';

/***********************************************************************
                     DO NOT EDIT BELOW THIS LINE
/***********************************************************************/

// Make sure no one attempts to run this script "directly"
if (!defined('PUN'))exit;

// Tell admin_loader.php that this is indeed a plugin and that it is loaded
define('PUN_PLUGIN_LOADED', 1);
$count = $db->query('SELECT COUNT(id) FROM '.$db->prefix.'topics WHERE forum_id='.$forum_id.'AND moved_to IS NULL') or error('Unable to fetch topic list', __FILE__, __LINE__, $db->error());
$Tot_records =  $db->fetch_row($count);
//echo $Tot_records[0];
$tot_pages = ceil($Tot_records[0] / $num_posts_index);
if (isset($_POST['gen_news'])){
    // Generate front page news
    require PUN_ROOT.'include/parser.php';
    // create the so many page as $tot_pages
    for ($i = 1; $i<= $tot_pages; $i++) {
    $first = ($i==1) ? 0 : (($num_posts_index*$i)-$num_posts_index); 
    
    //the query to pull out data from DB
    $result = $db->query('SELECT id, subject FROM '.$db->prefix.'topics WHERE forum_id='.$forum_id.' ORDER BY sticky DESC, posted DESC LIMIT '.$first.','.$num_posts_index) or error('Unable to fetch topic list', __FILE__, __LINE__, $db->error());
    
    if (!$db->num_rows($result))
        message('There are no topics to generate news based on in forum with ID = '.$forum_id.'.');

    

    $news_tpl = file_get_contents($template_path) or error('Unable to open new item template '.$template_path.'. Make sure $template_path is correct', __FILE__, __LINE__);

    $fh = @fopen($output_dir_latest.'news'.$i.'.html', 'wb');
    if (!$fh)
        error('Unable to write news to '.$output_dir_latest.'main.html. Please make sure PHP has write access to the directory '.$output_dir_latest, __FILE__, __LINE__);
        
    while ($cur_topic = $db->fetch_assoc($result))
    {
        $result2 = $db->query('SELECT posted, poster, message, hide_smilies FROM '.$db->prefix.'posts WHERE topic_id='.$cur_topic['id'].' ORDER BY posted ASC LIMIT 1') or error('Unable to fetch topic list', __FILE__, __LINE__, $db->error());
        $cur_post = $db->fetch_assoc($result2);

        $search = array('<news_subject>', '<news_posted>', '<news_poster>', '<news_message>', '<news_comments>');
        $replace = array(pun_htmlspecialchars($cur_topic['subject']), date('Y-m-d H:i', $cur_post['posted']), pun_htmlspecialchars($cur_post['poster']), parse_message($cur_post['message'], $cur_post['hide_smilies']), '<a href="'.$pun_config['o_base_url'].'/viewtopic.php?id='.$cur_topic['id'].'">Comments</a>');


        fwrite($fh, str_replace($search, $replace, $news_tpl));
    }
$previous = ($i>1) ? ("<a href=\"?page=news".($i-1)."\" title=\"Go back to previous page\"><< back</a>") : ($previous = ""); 
$next = ($i < $tot_pages) ? ("|  <a href=\"?page=news".($i+1)."\" title=\"Go to the next page\">next >></a>") : ($next ="");

fwrite($fh, "<div align='center'>$previous   $next</div>");
    fclose($fh);
}

    generate_admin_menu($plugin);

?>
    <div class="block">
        <h2><span>News Generator results</span></h2>
        <div class="box">
            <div class="inbox">
                <p>News generated.</p>
            </div>
        </div>
    </div>
<?php

}
else
{
    generate_admin_menu($plugin);

?>
    <div class="blockform">
        <h2><span>News Generator</span></h2>
        <div class="box">
            <form id="example" method="post" action="<?php echo $_SERVER['REQUEST_URI'] ?>&foo=bar">
                <div class="inform">
                    <fieldset>
                        <legend>Generate static news output</legend>
                        <div class="infldset">
                            <p>This plugin fetches posts from the specified forum and generates static markup for those posts based on the news item template.It outputs (<?=$tot_pages?>) pages like news1.html news2.html etc containing (<?=$num_posts_index ?>) post per page. the plugin create a navigation tool at the end of every page to move to forward and previous pages <strong>Look at the top of the PHP source code for this plugin to change the settings you see below.</strong></p>
                            <table class="aligntop" cellspacing="0">
                                <tr>
                                    <th scope="row">Fetch news from forum</th>
                                    <td><span><?php echo $forum_id ?></span></td>
                                </tr>
                                <tr>
                                    <th scope="row">No of posts for latest</th>
                                    <td><span><?php echo $num_posts_index ?></span></td>
                                </tr>
                                <tr>
                                    <th scope="row">Using template</th>
                                    <td><span><?php echo $template_path ?></span></td>
                                </tr>
                                <tr>
                                    <th scope="row">Saving latest news to</th>
                                    <td><span><?php echo $output_dir_latest ?></span></td>
                                </tr>
                                                            </table>
                            <div class="fsetsubmit"><input type="submit" name="gen_news" value="Generate news" tabindex="1" /></div>
                        </div>
                    </fieldset>
                </div>
            </form>
        </div>
    </div>
<?php

}
?>

the includer will look like this

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Includer</title>
</head>

<body>


    <?php 
    //example how to include inside your home page the news created by the modified News Generator.
    //Piero Orlando
    
    $News_Path = 'folder/';
    
    $page="news1";              //default page 
if(isset($_GET['page'])){      //page we get from our address bar
    $page=$_GET['page'];
    // this is to check the genuineness of the file included
    if($page!=basename($page) || !preg_match("/^[A-Za-z0-9\-_]+$/",$page) || $page=="index" || !file_exists($News_Path.$page.".html")){
  include('error.htm');//error page in case is not a genuine path
    exit(); //exit
        }
    }
    //if genuine include the page here 
    include($News_Path.$shares.'.html');
    ?>
</body>
</html>

Re: My version of AP_News_Generator

Thanks, looks pretty easy to setup