09 Cached News

Started by Mindless, July 20, 2012, 08:23:09 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Mindless

09 Cached news - requires 09 Textbbcode mod.
Xhtml vaild.

Code (sql) Select
CREATE TABLE `news` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `userid` int(11) NOT NULL default '0',
  `added` int(11) NOT NULL default '0',
  `body` text character set latin1 NOT NULL,
  `title` varchar(255) character set latin1 NOT NULL default '',
  `sticky` enum('yes','no') character set latin1 NOT NULL default 'no',
  PRIMARY KEY  (`id`),
  KEY `added` (`added`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;


Or keep exsisting table and change headline to title and add this single entry :

Code (sql) Select
ALTER TABLE news ADD `sticky` enum('yes','no') character set latin1 NOT NULL default 'no';

Add to stdhead in bittorrent.php or template.php

Code (html) Select
<script type='text/javascript' src='./scripts/java_klappe.js'></script>

save java_klappe.js to scripts folder :

Code (html) Select
function klappe(id){var klappText=document.getElementById('k'+id);var klappBild=document.getElementById('pic'+id);if(klappText.style.display=='none'){klappText.style.display='block';}else{klappText.style.display='none';}}function klappe_descr(id){var klappText=document.getElementById('k'+id);if(klappText.style.display=='none'){klappText.style.display='';}else{klappText.style.display='none';}}function klappe_news(id){var klappText=document.getElementById('k'+id);var klappBild=document.getElementById('pic'+id);if(klappText.style.display=='none'){klappText.style.display='block';klappBild.src='pic/minus.gif';}else{klappText.style.display='none';klappBild.src='pic/plus.gif';}}function klappe_changelog(id){var klappText=document.getElementById('k'+id);var klappBild=document.getElementById('pic'+id);if(klappText.style.display=='none'){klappText.style.display='block';klappBild.src='pic/minus.gif';}else{klappText.style.display='none';klappBild.src='pic/plus.gif';}}

Add to include/config.php :

Code (php) Select
$TBDEV['cache'] = ROOT_PATH.'/cache';

Replace your old news block on index.php with this :

Remember to change = $prefix = 'ChangeMe'; to something unique to your site :

   
Code (php) Select
//==09 Cached News
    $news2  = '';
    $adminbutton = '';
    if ($CURUSER >= UC_MODERATOR){
    $adminbutton = "<span style='float:right;'><a href='admin.php?action=news'>News page</a></span>\n";
    }
    $HTMLOUT.="<div class='roundedCorners' style='text-align:left;width:80%;border:1px solid black;padding:5px;'>
    <div style='background:transparent;height:25px;'><span style='font-weight:bold;font-size:12pt;'>{$lang['news_title']}</span>{$adminbutton}</div>";
    $news_file = "{$TBDEV['cache']}/news.txt";
    $expire =  15 * 60; // 15min
    if (file_exists($news_file) && filemtime($news_file) > (time() - $expire)) {
    $news2 = unserialize(file_get_contents($news_file));
    } else {
    $prefix = 'ChangeMe';
    $res = mysql_query("SELECT ".$prefix.".id, ".$prefix.".userid, ".$prefix.".added, ".$prefix.".title, ".$prefix.".body, ".$prefix.".sticky, u.username FROM news AS ".$prefix." LEFT JOIN users AS u ON u.id = ".$prefix.".userid WHERE ".$prefix.".added + ( 3600 *24 *45 ) > ".time()." ORDER BY sticky, ".$prefix.".added DESC LIMIT 10") or sqlerr(__FILE__, __LINE__);
    while ($news1 = mysql_fetch_assoc($res) ) {
    $news2[] = $news1;
    }
    $output = serialize($news2);
    $fp = fopen($news_file,"w");
    fputs($fp, $output);
    fclose($fp);
    }
    $news_flag = 0;
    if ($news2)
    {
    foreach ($news2 as $array)
    {
    $button='';
    if ($CURUSER['class'] >= UC_MODERATOR)
    {
    $hash = md5('the@@saltto66??' . $array['id']. 'add' . '@##mu55y==');
    $button = "<br /><div style='float:right;'><a href='admin.php?action=news&amp;mode=edit&amp;newsid={$array['id']}&amp;returnto=".urlencode($_SERVER['PHP_SELF'])."'><img src='{$TBDEV['pic_base_url']}button_edit2.gif' border='0' alt=\"Edit news\"  title=\"Edit news\" /></a>&nbsp;<a href='admin.php?action=news&amp;mode=delete&amp;newsid={$array['id']}&amp;h=$hash&amp;returnto=".urlencode($_SERVER['PHP_SELF'])."'><img src='{$TBDEV['pic_base_url']}del.png' border='0' alt=\"Delete news\" title=\"Delete news\" /></a></div>";
    }
    $HTMLOUT .= "<div style='background:transparent;height:20px;'><span style='font-weight:bold;font-size:10pt;'>";
    if ($news_flag < 2) {
    $HTMLOUT .="<a href=\"javascript: klappe_news('a".$array['id']."')\"><img border=\"0\" src='pic/plus.gif' id=\"pica".$array['id']."\" alt=\"Show/Hide\" />" . " - " .get_date( $array['added'],'DATE') . " - " ."{$array['title']}</a></span>{$button}</div>";
    $HTMLOUT .="<div id=\"ka".$array['id']."\" style=\"display:".($array["sticky"] == "yes" ? "" : "none").";margin-left:30px;margin-top:10px;\"> ".format_comment($array["body"],0)." </div><br /> ";

    $news_flag = ($news_flag + 1);
    }
    else {
    $HTMLOUT .="<a href=\"javascript: klappe_news('a".$array['id']."')\"><img border=\"0\" src='pic/plus.gif' id=\"pica".$array['id']."\" alt=\"Show/Hide\" />" . " - " .get_date( $array['added'],'DATE') . " - " ."{$array['title']}</a></span>{$button}</div>";
    $HTMLOUT .="<div id=\"ka".$array['id']."\" style=\"display:".($array["sticky"] == "yes" ? "" : "none").";margin-left:30px;margin-top:10px;\"> ".format_comment($array["body"],0)." </div><br /> ";

    }
    $HTMLOUT .= "<div style='margin-top:10px;padding:5px;'></div><hr />\n";
    }
    $HTMLOUT .= "</div><br />\n";
    }
    if (empty($news2))
    $HTMLOUT .= "</div><br />\n";
    //==End


Save and upload news.php to admin folder :

Code (php) Select
<?php
/*
+------------------------------------------------
|   $Date$ 070810
|   $Revision$ 2.0
|   $Author$ Bigjoos
|   $inputs$ putyn,pdq,snuggs
|   $URL$
|   $news$
|   
+------------------------------------------------
*/
if ( ! defined'IN_TBDEV_ADMIN' ) )
{
$HTMLOUT='';
$HTMLOUT .= "<!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>
<title>Error!</title>
</head>
<body>
<div style='font-size:33px;color:white;background-color:red;text-align:center;'>Incorrect access<br />You cannot access this file directly.</div>
</body></html>"
;
print $HTMLOUT;
exit();
}


require_once 
"include/html_functions.php";
require_once 
"include/user_functions.php";
require_once 
"include/bbcode_functions.php";

$HTMLOUT='';

$HTMLOUT .="<script type=\"text/javascript\" src=\"./scripts/shout.js\"></script>";

$lang array_merge$langload_language('ad_news') );

if (
$CURUSER['class'] < UC_STAFF)
stderr("Error""Permission denied.");

$mode = isset($_GET["mode"]) ?$_GET["mode"] : '';

//==Delete news
if ($mode == 'delete') {
    
$newsid = (int)$_GET['newsid'];
    if (!
is_valid_id($newsid))
    
stderr("Error""Invalid ID.");
    
$hash md5('the@@saltto66??' $newsid 'add' '@##mu55y==');
    
$sure='';
    
$returnto = isset($_POST['returnto']) ? htmlentities($_POST['returnto']) : '';
    isset(
$_GET['sure']) && $sure htmlspecialchars($_GET['sure']);
    if (!
$sure)
        
stderr("Confirm Delete""Do you really want to delete this news entry? Click\n" "<a href='admin.php?action=news&amp;mode=delete&amp;sure=1&amp;h=$hash&amp;newsid=$newsid&amp;returnto=admin.php?action=news'>here</a> if you are sure."false);
    if (
$_GET['h'] != $hash)
        
stderr('Error''what are you doing?');
    function 
deletenewsid($newsid)
    {
        
        global 
$CURUSER;
        
mysql_query("DELETE FROM news WHERE id = $newsid AND userid = $CURUSER[id]");
        @
unlink("cache/news.txt");
    }
    
$HTMLOUT.= deletenewsid($newsid);
    
header("Refresh: 3; url=admin.php?action=news");
    
$HTMLOUT .="<h2>News entry deleted - Please wait while you are redirected!</h2>";
    print 
stdhead('News') . $HTMLOUT stdfoot();
    die;
    }

//==Add news
if ($mode == 'add') {
    
$body = isset($_POST['body']) ? $_POST['body'] : '';
    
$sticky = isset($_POST['sticky']) ? $_POST['sticky'] : 'yes';
    if (!
$body)
        
stderr("Error""The news item cannot be empty!");
    
$title htmlentities($_POST['title']);
    if (!
$title)
        
stderr("Error""The news title cannot be empty!");
    
$added = isset($_POST["added"]) ?$_POST["added"] : '';
    if (!
$added)
        
$added time();
    
mysql_query("INSERT INTO news (userid, added, body, title, sticky) VALUES (" $CURUSER['id'] . "," sqlesc($added) . ", " sqlesc($body) . ", " sqlesc($title) . ", " sqlesc($sticky) . ")") or sqlerr(__FILE____LINE__);
    
mysql_affected_rows() == ?$warning "News entry was added successfully." stderr("oopss""Something's wrong !! .");
    @
unlink("cache/news.txt");
}

//==Edit/change news
if ($mode == 'edit') {
    
$newsid = (int)$_GET["newsid"];
    if (!
is_valid_id($newsid))
        
stderr("Error""Invalid news item ID.");
    
$res mysql_query("SELECT * FROM news WHERE id=" sqlesc($newsid)) or sqlerr(__FILE____LINE__);
    if (
mysql_num_rows($res) != 1)
        
stderr("Error""No news item with that ID .");
    
$arr mysql_fetch_assoc($res);
    if (
$_SERVER['REQUEST_METHOD'] == 'POST') {
        
$body = isset($_POST['body']) ? $_POST['body'] : '';
        
$sticky = isset($_POST['sticky']) ? $_POST['sticky'] : 'yes';
        if (
$body == "")
        
stderr("Error""Body cannot be empty!");
        
$title htmlentities($_POST['title']);
        if (
$title == "")
        
stderr("Error""Title cannot be empty!");
        
$body sqlesc($body);
        
$sticky sqlesc($sticky);
        
$editedat sqlesc(time());
        
mysql_query("UPDATE news SET body=$body, sticky=$sticky, title=" sqlesc($title) . " WHERE id=$newsid") or sqlerr(__FILE____LINE__);
        @
unlink("cache/news.txt");
        
$returnto = isset($_POST['returnto']) ? htmlentities($_POST['returnto']) : '';
        if (
$returnto != "")
        
header("Location: $returnto");
        else
        
$warning "News item was edited successfully.";
        } else {
        
$returnto = isset($_POST['returnto']) ? htmlentities($_POST['returnto']) : '';
        
$HTMLOUT .="<h1>Edit News Item</h1>
        <form method='post' name='compose' action='admin.php?action=news&amp;mode=edit&amp;newsid=
$newsid'>
        <input type='hidden' name='returnto' value='
$returnto' />
        <table border='1' cellspacing='0' cellpadding='5'>
        <tr><td><input type='text' name='title' value='" 
htmlspecialchars($arr['title']) . "' /></td></tr>
        <tr><td align='left' style='padding: 0px'>
         "
.textbbcode("compose""body"htmlspecialchars($arr["body"])) . "</td></tr>
        <tr><td colspan='2' class='rowhead'>Sticky<input type='radio' " 
. ($arr["sticky"] == "yes" " checked='checked'" "") . " name='sticky' value='yes' />Yes<input name='sticky' type='radio' value='no' " . ($arr["sticky"] == "no" " checked='checked'" "") . " />No</td></tr>
        <tr><td colspan='2' align='center'><input type='submit' value='Okay' class='btn' /></td></tr>
        </table>
        </form>\n"
;
        print  
stdhead('News Page') . $HTMLOUT stdfoot();
        die;
    }
}

//==Final Actions
$res mysql_query("SELECT * FROM news ORDER BY sticky, added DESC") or sqlerr(__FILE____LINE__);
   
$HTMLOUT .= begin_main_frame();
   
$HTMLOUT .= begin_frame();
    if (!empty(
$warning))
    
$HTMLOUT .="<p><font size='-3'>($warning)</font></p>";
    
$HTMLOUT .="<form method='post' name='compose' action='admin.php?action=news&amp;mode=add'>
    <h1>Submit News Item</h1><table border='1' cellspacing='0' cellpadding='5'>
    <tr><td><input type='text' name='title' value='" 
htmlspecialchars($res['title']) . "' /></td></tr>\n";
    
$HTMLOUT .="<tr>
    <td align='left' style='padding: 0px'>"
.textbbcode("compose""body")."</td></tr>";
    
$HTMLOUT .="<tr><td colspan='2' class='rowhead'>Sticky<input type='radio' checked='checked' name='sticky' value='yes' />Y<input name='sticky' type='radio' value='no' />N</td></tr>\n
    <tr><td colspan='2' class='rowhead'><input type='submit' value='Okay' class='btn' /></td></tr>\n
    </table></form><br /><br />\n"
;
    
    while (
$arr mysql_fetch_assoc($res)) {
        
$newsid $arr["id"];
        
$body $arr["body"];
        
$title $arr["title"];
        
$userid $arr["userid"];
        
$added get_date($arr["added"], 'LONG',0,1);
        
$res2 mysql_query("SELECT id, username, class, enabled, donor, added FROM users WHERE id =$userid") or sqlerr(__FILE____LINE__);
        
$arr2 mysql_fetch_assoc($res2);
        
$postername $arr2["username"];
        if (
$postername == "")
        
$by "unknown[$userid]";
        else
        
$by "<a href='userdetails.php?id=$userid'><b>$postername</b></a>" .($arr2["donor"] == "yes" "<img src='pic/star.gif' alt='Donor' title='Donor' />" "");
        
$hash md5('the@@saltto66??' $newsid 'add' '@##mu55y==');
        
$returnto = isset($_POST['returnto']) ? htmlentities($_POST['returnto']) : '';
        
$HTMLOUT .="<table border='0' cellspacing='0' cellpadding='0'><tr><td class='embedded'>
        
$added&nbsp;---&nbsp;by&nbsp$by
        - [<a href='admin.php?action=news&amp;mode=edit&amp;newsid=
$newsid'><b>Edit</b></a>]
        - [<a href='admin.php?action=news&amp;mode=delete&amp;newsid=
$newsid&amp;sure=1&amp;h=$hash'><b>Delete</b></a>]
        </td></tr></table>\n"
;
        
$HTMLOUT .= begin_table(true);
        
$HTMLOUT .="<tr valign='top'><td class='comment'><b>" htmlentities($title) . "</b><br />" format_comment($body) . "</td></tr>\n";
        
$HTMLOUT .= end_table();
    }
    
$HTMLOUT .= end_frame();
    
$HTMLOUT .= end_main_frame();
print  
stdhead('News Page') . $HTMLOUT stdfoot();
die;
?>


Still not a finished mod - may improve parts or change so remember to check the post.