Jump to content


Photo

PHP flat file blog script


  • Please log in to reply
2 replies to this topic

#1 borikenmedia

borikenmedia

    Young Padawan

  • Members
  • Pip
  • 30 posts

Posted 07 April 2011 - 04:18 PM

Well greetings, first i was writing a flat file blog script that allow me post in the diferent categories sections, the thing is i achive to post in the categories and
display a sigle post of the file array the thing is that im trying to display the most recent post and where thinking enumarating the array and using ksort().

microblog class
<?php

class micro_blog{
 
 var $stmp;
 var $cmp;
 var $set;

 public function __Constructor(){
  $this->stmp = microtime(true);
  return $this->stmp;
 }
 
 public function ping($class, $caption){
  $this->cmp = '<script type="text/javascript">'.$class.'("'.$caption.'"); history.go(-1);</script>';
  return $this->cmp;
 }
 
 public function phtml($data){
  return htmlentities($data, ENT_QUOTES);
 }
 
 public function digest($path, $data, $id){
  if(empty($data[0]) || empty($data[3]) || empty($data[5])){
   exit($this->ping('alert', 'All fields required'));   
  }else{
   $posfile = $id.'_'.$data[6].'.txt';
   if($hdl = fopen($path.'/'.$posfile, "w+")){
    fwrite($hdl, $data[0]."\n".$data[4]."\n".$data[2]."\n".$data[3]."\n".$data[5]."\n");
    fclose($hdl);
    exit($this->ping('alert', 'Message sent @Borikenmedia'));
   }
  }
 }
 
 function buffer($path){
  ob_start();
  $ctrl = !is_dir($path)? file($path): opendir($path);
  ob_end_flush();
  return $ctrl;
 }
 
 public function feed($path){
  $dir = is_dir($path)? $this->buffer($path): $this->ping('alert', 'No post found');
  while(false !== ($data = readdir($dir))){
   if($data !== '.' && $data !== '..'){
    $this->set[] = $data;
   }
  }
  return $this->set;
 }


}

?>


blog feed
<?php

$cat = !empty($_REQUEST['cat'])? phtml($_REQUEST['cat']): 'blog';
#Request topic

$form_path = 'editor/path';
$form_type = '.tpl';
$news_path = 'post/path'.$cat.'/';
#Set variable values

#Set Micro Blog instance [[Do not edit]]
$motd = new micro_blog();

if($array = $motd->feed($news_path)){
 if(count($array) >= 1){
  $post = $array[0];
 }
}

if(isset($_POST['submit'])){

 $posdata = array(
	$motd->phtml($_POST['blog_user']),
	$motd->phtml($_POST['blog_mail']),
	$motd->phtml($_POST['blog_cat']),
	$motd->phtml($_POST['blog_title']),
	date('F j, Y, g:i a'),
	$motd->phtml($_POST['blog_message']),
	date('YmdHis'));
  
 $motd->digest($news_path, $posdata, $cat); //Process request
 $uri = $_SERVER['SERVER_NAME'].'/node?task=feed&cat='.$cat;
 header('Location: http://'.$uri);
 
}else{
  $data = $news_path.$post;
  $feed = file_exists($data)? file($data): 'Empty thread';
  print '<div class="article"><h2><span>'.$feed[3].'</span></h2>';
  print '<div class="clr"></div>';
  print '<p style="margin: 0px; padding: 0px; font: normal 12px Arial; color: #333333;">';
  print 'By '.$feed[0].' @ '.$feed[1].'</p>';
  print '<div class="clr"></div>';
  print '<p>'.$feed[4].'</p></div>';
}
 
include 'module/misc/blog_edit.tpl';


?>


Greetings Rc69, hope yall can help

Edited by borikenmedia, 07 April 2011 - 06:20 PM.


#2 rc69

rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 08 April 2011 - 10:16 PM

See comments in code.
 public function digest($path, $data, $id){ 
if(empty($data[0]) || empty($data[3]) || empty($data[5])){
/* Path 1 exits */
exit($this->ping('alert', 'All fields required'));
}else{
/* What is $data[6]? */
$posfile = $id.'_'.$data[6].'.txt';
if($hdl = fopen($path.'/'.$posfile, "w+")){
fwrite($hdl, $data[0]."\n".$data[4]."\n".$data[2]."\n".$data[3]."\n".$data[5]."\n");
fclose($hdl);

/* On sucessful write, path 2 also exits */
exit($this->ping('alert', 'Message sent @Borikenmedia'));
}

/* Path 3 safely returns nothing and allows processing to continue */
}
}

public function buffer($path){
/* Why are you using output buffering around functions that don't output anything? */
ob_start();
$ctrl = !is_dir($path)? file($path): opendir($path);
ob_end_flush();
return $ctrl;
}

public function feed($path){
/* BAD mixing of datatypes!
1. $this->buffer() returns either a directory handle (though it could return a file array,
in this case it won't)
2. $this->ping() returns a string

That's technically three data types that $dir could be and immediately after
you are assuming it's a directory handle. You shouldn't make that assumption,
nor should you write code/functions that force you to type check $dir before using it */
$dir = is_dir($path)? $this->buffer($path): $this->ping('alert', 'No post found');
while(false !== ($data = readdir($dir))){
if($data !== '.' && $data !== '..'){
$this->set[] = $data;
}
}

/* Returns array of file names */
return $this->set;
}

/* ... */

if($array = $motd->feed($news_path)){
if(count($array) >= 1){
/* Gets an arbitrary file based on how readdir() decided to order things */
$post = $array[0];
}
}

/* ... */

/* Again, bad mixing of data types! Immediately after this line you expect $feed to be
an array of strings. Instead you can have a single string, which is actually an array
of chracters, so it kind of works, but is not what we want */
$feed = file_exists($data)? file($data): 'Empty thread';


If $data[6] in digest() is a sortable string (meaning it has some meaning that could be used to identify the age of a post), then you could just do a regular sort() on the array returned by $motd->feed(), then get either the first or last entry in the array (depending on the sort order).

If $data[6] is not sortable, then you would probably have to check the filemtime() of each file to determine which post is the newest. Of course, if you ever edit your posts in the future, the filemtime() will change, so that can't guarantee proper order of the posts.

My last, and definitely not recommended method, would be to read every file, parse the post date out of each of them, build up a map to associate files to dates, and the pick the entry in the map with the most recent date. But again, i REALLY don't recommend doing this.

#3 borikenmedia

borikenmedia

    Young Padawan

  • Members
  • Pip
  • 30 posts

Posted 08 April 2011 - 11:46 PM

Thanks for the suggestions. Was the array of strings after ,$feed !

Pd: Big Ups rc69, thanks for the help. data[6] is a stamp :404:

Edited by borikenmedia, 09 April 2011 - 12:41 AM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users