The system will generate a folder structure like the folloing
Year Month Day Newsfile
—————————————————————
2007 -
|- 01 -|
|- 30 -|
|-timestamp.news
|-timestamp.news
|-timestamp.news
|- 31
|- 02
|- 1
|- 2
|- 03
|- etc
Programmed by Christian Haensel, christian@chftp.com, http://www.chftp.com
Exclusively published on weberdev.com
function datefolders() {
// This function checks the existance of the news directory for this day
// and, if necessary, creates the directories for year, month and day.
$checkyear = date("Y");
$checkmonth = date("m");
$checkday = date("d");
// Checking for this year's folder
if(!is_dir("news/".$checkyear)) {
mkdir("news/".$checkyear,0755);
}
// Checking for this month's folder
if(!is_dir("news/".$checkyear."/".$checkmonth)) {
mkdir("news/".$checkyear."/".$checkmonth,0755);
}
// Checking for today's folder
if(!is_dir("news/".$checkyear."/".$checkmonth."/".$checkday)) {
mkdir("news/".$checkyear."/".$checkmonth."/".$checkday,0755);
}
}
function readnews($day,$month,$year,$number) {
$nfile = file("news/".$year."/".$month."/".$day."/".$number);
$i = 1;
$date_ex = explode(".", $number);
$timest = $date_ex[0];
$news_date = date("d.m.Y - H:i", $timest);
foreach($nfile as $nline) {
if($i==1) {
echo '<h1>'.$nline.'</h1>';
} elseif($i==2) {
echo '<h3>'.$nline.'</h3>';
} else {
echo $nline."<br>";
}
$i++;
}
echo '<p><small>Posted '.$news_date.'</small><p>';
echo '--------------------------------------------------------------';
}
function check_news() {
// This function checks for news in the directories. It will take URL-Parameters
// like day, month and year. If thos are not set, it will take the current date
if(eregi("\.", $_GET['day']) || eregi("\.", $_GET['month']) || eregi("\.", $_GET['year'])) {
die("No hacking");
}
if(isset($_GET['year'])) {
$newsyear = $_GET['year'];
} else {
$newsyear = date("Y");
}
if(isset($_GET['month'])) {
$newsmonth = $_GET['month'];
} else {
$newsmonth = date("m");
}
if(isset($_GET['day'])) {
$newsday = $_GET['day'];
} else {
$newsday = date("d");
}
// Check for news in the directory
$news_arr = array();
$newsfolder = @opendir("news/".$newsyear."/".$newsmonth."/".$newsday) or die("No news for this time");
while($news = readdir($newsfolder)) {
if(eregi(".news", $news)) {
array_push($news_arr, $news);
}
}
sort($news_arr);
$news_arr = array_reverse($news_arr);
foreach($news_arr as $news) {
readnews($newsday,$newsmonth,$newsyear,$news);
}
}
function showaddform() {
// Set the access password here
$mypass = "mypassword";
if(!isset($_POST['pass'])) {
echo '
<form name="login" action="#" method="POST";
Login:
<input type="password" name="pass">
<input type="submit" value="Login">
</form>';
die;
}
if(isset($_POST['pass']) && $_POST['pass'] != $mypass) {
echo '
Login failed.<p>
<form name="login" action="#" method="POST";
Login:
<input type="password" name="pass">
<input type="submit" value="Login">
</form>';
die;
} elseif(isset($_POST['pass']) && $_POST['pass'] == $mypass) {
echo '
<div style="padding:5px; border:1px solid #000000;">
<b>Add News</b><p>
<form name="addnews" action="#" method="POST">
<table>
<tr>
<td>Title:</td><td><input type="text" name="title">
</td>
</tr>
<tr>
<td>
Header:</td><td><input type="text" name="header">
</td>
</tr>
<tr>
<td>
Text:</td><td><textarea name="text"></textarea>
</td>
</tr>
<tr>
<td colspan="2">
<input type="Submit" value="Add news">
<input type="hidden" name="pass" value="'.$_POST[pass].'">
<input type="hidden" name="action" value="addnews">
</td>
</tr>
</table>
</form>
</div>
';
}
}
function doadd() {
$nowyear = date("Y");
$nowmonth = date("m");
$nowday = date("d");
$ntitle = $_POST['title'];
$nheader = $_POST['header'];
$ntext = $_POST['text'];
$ntimest = time();
$newfile = fopen("news/".$nowyear."/".$nowmonth."/".$nowday."/".$ntimest.".news", "w+");
$newstext = $ntitle."\n".$nheader."\n".$ntext;
fwrite($newfile, $newstext);
fclose($newfile);
echo 'News added';
}
function checkadd() {
if($_GET['add'] == "true") {
showaddform();
}
if($_POST['action'] == "addnews") {
doadd();
}
}
// Do it all
checkadd(); datefolders(); check_news();
?>-
horechek liked this
-
blackflowtocode reblogged this from kloningspoon
-
kloningspoon posted this
