Jump to content

phpfastnews


cropsaius

Recommended Posts

Here is the code in my script in relation to time, I believe using logic this gets the time from my web server at the min which I do not want to do:

 

function addItem( $item ){

// $date = date( "j M Y H:i", time() );

$date = date( "j M Y g:i a", time() );

 

$item['date'] = $date;

$this->news[] = $item;

}

 

 

I read this topic and this is exactly what I want to do: http://www.phpfreaks.com/forums/index.php?topic=279153.0

 

however I do not think this would work by me changing the code above, I am not very good with PHP in all honesty, I know that the script does not use MYSQL.

 

Overall what exact thing would I have to change in the first bit of code out of my script above to change the time to my local time in the U.K.?

Link to comment
https://forums.phpfreaks.com/topic/187052-phpfastnews/#findComment-988435
Share on other sites

than use the date_default_timezone functions.

to just affect that one function U wud do something like

function addItem( $item ){
                $oldtz=date_default_timezone_get();
                date_default_timezone_set  ('Etc/GMT-8');
//		$date = date( "j M Y H:i", time() );
	$date = date( "j M Y g:i a", time() );
                date_default_timezone_set  ($oldtz);

	$item['date'] = $date;
	$this->news[] = $item;
	}

 

but to affect your whole script, place    date_default_timezone_set at the beginning of your script

 

Link to comment
https://forums.phpfreaks.com/topic/187052-phpfastnews/#findComment-988538
Share on other sites

Thanks for the reply there, however this bit of code did not work upon me replacing it exactly in the script, here is the full script as I now believe something else is stopping this from working successfully?

 

<?php

/*

phpFastNews 1.0.0

Last update: 02 Sep 2008

 

Skipping the blah-blah-blah I kindly ask you to keep the link back to phpFastNews website. Thanks!

*/

 

include_once( dirname(__FILE__) . '/fastnews-conf.php' );

define( 'DATAFILE', dirname(__FILE__) . '/fastnews-data.txt' );

 

$JAVASCRIPT = <<<EOT

<SCRIPT LANGUAGE="JavaScript">

function fn_HideElement( elementName ){

ele = document.getElementById( elementName );

if( ! ele ){

alert( 'cannot find ' + elementName );

return;

}

ele.style.display = "none";

}

function fn_ShowElement( elementName ){

ele = document.getElementById( elementName );

if( ! ele ){

alert( 'cannot find ' + elementName );

return;

}

ele.style.display = "block";

}

</SCRIPT>

EOT;

 

/* PROCESS ACTION */

$action = ( isset($_REQUEST['fn_action']) ) ? $_REQUEST['fn_action'] : '';

if( $action ){

$fn = new fastNews;

// NO ACTION IF NOT LOGGED IN

if( $fn->isLoggedIn() || ($action == 'login') ){

switch( $action ){

case 'login':

$password = $_POST['password'];

if( $fn->checkPassword($password) )

$fn->doLogin();

else {

$fn->displayError( 'Incorrect Password!' );

exit;

}

break;

case 'logout':

$fn->doLogout();

break;

case 'add':

$newItem = $fn->grabData();

$fn->addItem( $newItem );

$fn->save();

break;

case 'update':

$newItem = $fn->grabData();

$itemId = $newItem['id'];

$fn->updateItem( $itemId, $newItem );

$fn->save();

break;

case 'delete':

$itemId = $_REQUEST['id'];

$fn->deleteItem( $itemId );

$fn->save();

break;

}

}

 

/* REDIRECT BACK TO THE REFERRER */

$referrer = $_SERVER['HTTP_REFERER'];

header( "Location: $referrer" );

exit;

}

 

class fastNews {

var $news = array();

 

function fastNews(){

$this->news = array();

$this->load();

}

 

function checkPassword( $pass ){

if( $pass == FN_PASSWORD )

return true;

else

return false;

}

 

function doLogin(){

setcookie( FN_COOKIE_NAME, '1', time() + FN_COOKIE_EXPIRES );

}

 

function doLogout(){

setcookie( FN_COOKIE_NAME, '', time() -1 );

}

 

function isLoggedIn(){

if( isset($_COOKIE[FN_COOKIE_NAME]) && $_COOKIE[FN_COOKIE_NAME] )

return true;

else

return false;

}

 

function grabData(){

$subject = $_POST['subject'];

$message = $_POST['message'];

if( get_magic_quotes_gpc() ){

$subject = stripslashes( $subject );

$message = stripslashes( $message );

}

$itemId = isset($_POST['id']) ? $_POST['id'] : 0;

 

$data = array(

'subject' => $subject,

'message' => $message,

'id' => $itemId,

);

 

return $data;

}

 

function addItem( $item ){

// $date = date( "j M Y H:i", time() );

$date = date( "j M Y g:i a", time() );

 

$item['date'] = $date;

$this->news[] = $item;

}

 

function updateItem( $id, $newItem ){

$item = $this->news[$id - 1];

$item = array_merge( $item, $newItem );

$this->news[ $id - 1 ] = $item;

}

 

function deleteItem( $id ){

array_splice( $this->news, $id - 1, 1 );

}

 

function save(){

// CHECK IF THE DATA FILE EXISTS

if( ! file_exists(DATAFILE) ){

$this->error( 'Datafile missing! Please create a new empty file <B>' . DATAFILE . '</B> and make chmod 666' );

return;

}

// CHECK IF THE DATA FILE IS WRITABLE

if( ! is_writable(DATAFILE) ){

$this->error( 'Datafile is not writable! Please make chmod 666 for the <B>' . DATAFILE . '</B> file.' );

return;

}

 

$this->setToFile( DATAFILE );

}

 

function load(){

// CHECK IF THE DATA FILE EXISTS

if( ! file_exists(DATAFILE) ){

$this->error( 'Datafile missing! Please create a new empty file <B>' . DATAFILE . '</B> and make chmod 666' );

return;

}

 

// LOAD THE DATA FILE

$this->getFromFile( DATAFILE );

}

 

function display(){

global $NEWS_ITEM, $NEWS_LIST, $JAVASCRIPT;

$CODE_URL = FN_CODE_URL;

 

$view = '';

$items = '';

$itemId = count($this->news) + 1;

 

// ORDER IN REVERSE

reset( $this->news );

foreach( array_reverse($this->news) as $n ){

$itemId--;

if( $this->isLoggedIn() ){

$editLinks = <<<EOT

<A HREF="#" ONCLICK="fn_ShowElement('fn-edit-$itemId'); fn_HideElement('fn-view-$itemId'); return false;">Edit</A>

<A HREF="$CODE_URL?fn_action=delete&id=$itemId" ONCLICK="return confirm('Are you sure to delete this news item?')">Delete</A>

EOT;

}

else {

$editLinks = '';

}

 

$formSubject = htmlentities( $n['subject'] );

$replaces = array(

'{SUBJECT}' => $n['subject'],

'{FORM_SUBJECT}' => $formSubject,

'{DATE}' => $n['date'],

'{MESSAGE}' => $n['message'],

'{ITEM_ID}' => $itemId,

'{EDIT_LINKS}' => $editLinks,

'{CODE_URL}' => FN_CODE_URL,

);

$itemDisplay = str_replace( array_keys($replaces), array_values($replaces), $NEWS_ITEM );

$items .= $itemDisplay;

}

 

if( $this->isLoggedIn() ){

$addLink = <<<EOT

<A ID="fn-addBar" HREF="#" ONCLICK="fn_ShowElement('fn-addForm'); fn_HideElement('fn-addBar'); return false;">Add News Item</A>

EOT;

}

else {

$addLink = '';

}

 

if( $this->isLoggedIn() ){

$loginLink = <<<EOT

<A HREF="$CODE_URL?fn_action=logout">Logout</A>

EOT;

}

else {

$loginLink = <<<EOT

<A ID="fn-loginBar" HREF="#" ONCLICK="fn_ShowElement('fn-loginForm'); fn_HideElement('fn-loginBar'); return false;">Login</A>

EOT;

}

 

// ADD UP OUR CREDENTIALS

$copyright = <<<EOT

<DIV STYLE="font-size: 0.75em; margin: 1em 0 0 0;">Powered by <A HREF="http://www.phpfastnews.com">phpFastNews</A> - fast and free news display script</DIV>

EOT;

$items = $items . "\n" . $copyright;

 

$replaces = array(

'{NEWS_ITEMS}' => $items,

'{ADD_LINK}' => $addLink,

'{LOGIN_LINK}' => $loginLink,

'{CODE_URL}' => FN_CODE_URL,

);

$view = str_replace( array_keys($replaces), array_values($replaces), $NEWS_LIST );

 

$view = $JAVASCRIPT . "\n" . $view;

 

return $view;

}

 

function setToFile( $fileName ){

reset( $this->news );

$content = '';

foreach( $this->news as $n ){

if( ! $n )

continue;

$content .= 'ITEM_START' . "\n";

$content .= $n['date'] . "\n";

$content .= $n['subject'] . "\n";

$content .= $n['message'] . "\n";

$content .= 'ITEM_END' . "\n";

}

setFileContent( DATAFILE, $content );

}

 

function getFromFile( $fileName ){

$fileContentArray = file( $fileName );

reset( $fileContentArray );

$lineIndex = 0;

foreach( $fileContentArray as $line ){

$line = trim( $line );

if( ! $line )

continue;

 

if( $line == 'ITEM_END' ){

$this->news[] = $item;

continue;

}

 

if( $line == 'ITEM_START' ){

$item = array();

$lineIndex = 1;

}

else {

// PARSE LINE

switch( $lineIndex ){

case 1:

$key = 'date';

break;

case 2:

$key = 'subject';

break;

default:

$key = 'message';

break;

}

 

if( isset($item[$key]) )

$item[$key] .= $line;

else

$item[$key] = $line;

 

$lineIndex++;

}

}

}

 

function displayError( $msg ){

echo $msg;

/* REDIRECT BACK TO THE REFERRER */

$referrer = $_SERVER['HTTP_REFERER'];

echo <<<EOT

<meta http-equiv="Refresh" content="1; URL=$referrer">

EOT;

}

 

function error( $msg ){

echo 'Error: ' . $msg;

}

}

?>

<?php

function _print_r( $thing ){

echo '<PRE>';

print_r( $thing );

echo '</PRE>';

}

 

function setFileContent( $fileName, $content ){

$length = strlen( $content );

$f = fopen( $fileName, 'w' );

$result = fwrite($f, $content, $length);

fclose( $f );

}

?>

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/187052-phpfastnews/#findComment-989258
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.