<?php


function blog_entry_history($dir,$entry){

  $history = array();

  if (is_dir($dir)){
    if (is_dir($dir . '/' . $entry)){
      if (is_dir($dir . '/' . $entry . '/history')){
	$handle=opendir($dir . '/' . $entry . '/history');
	while ($file = readdir($handle)) {
	  $parts = explode('.',$file);
	  if ($parts[1] == 'txt'){
	    $time = $parts[0];
	    $history[$time] = $dir . '/' . $entry . '/history/' . $file;
	  }
	}
	closedir($handle);
      }
    }
  }
  return $history;
}

function history_file($entry,$time){
  if (is_dir($entry)){
    if (is_dir($entry.'/history')){
      if (file_exists($entry.'/history/'.$time.'.txt')){
	return $entry.'/history/'.$time.'.txt';
      }
      if (file_exists($entry.'/history/'.$time.'.txt.gz')){
	return $entry.'/history/'.$time.'.txt.gz';
      }
    }
  }
  if (file_exists($entry.'.txt')){
    return $entry.'.txt';
  }
  if (file_exists($entry.'.txt.gz')){
    return $entry.'.txt.gz';
  }
  return false;
}

function history_entry($entry,$time){
  if (is_dir($entry)){
    if (is_dir($entry.'/history')){
      if (file_exists($entry.'/history/'.$time.'.txt')){
	return $entry.'/history/'.$time;
      }
      if (file_exists($entry.'/history/'.$time.'.txt.gz')){
	return $entry.'/history/'.$time;
      }
    }
  }
  return $entry;
}

function history_static($dir,$entry,$time){
  if (is_dir($dir.'/'.$entry)){
    if (is_dir($dir.'/'.$entry.'/history')){
      if (file_exists($dir.'/'.$entry.'/history/'.$time.'.txt')){
	return $entry.'/history/'.$time;
      }
      if (file_exists($dir.'/'.$entry.'/history/'.$time.'.txt.gz')){
	return $entry.'/history/'.$time;
      }
    }
  }
  return $entry;
}

function save_author($entry){
    if (isset($_SESSION['user'])){
	if (!is_dir($entry)){
	    my_mkdir($entry);
	}
	if (is_dir($entry)){
	    $result = sb_write_file( $entry.'/author.txt', $_SESSION['user']);
	}
    }
}

function delete_blog_entry($file){
  global $blog_config;

  //  $dir = dirname($file);
  //  $entry = basename($file);
  $ext = '.txt';

  if ( $blog_config[ 'blog_enable_gzip_txt' ] ) {
    if ( file_exists( $file . '.txt.gz' ) ) {
      $ext = '.txt.gz';
    }
  }

  if (file_exists($file . $ext)){ // check if entry file exists
    if (!file_exists($file)){     // check if sub-dir for this entry exists
      my_mkdir($file);              // if not -> make it with default permissions
    }
    if (is_dir($file)){
      if (!file_exists($file.'/history')){ // check if there's a history dir
	my_mkdir($file.'/history');          // if not -> make it
      }
      if (is_dir($file.'/history')){
	@$log_time = filemtime($file . $ext);
	rename($file . $ext , $file.'/history/'.$log_time . $ext);
	if (file_exists($file.'/author.txt')){
	    @rename($file . '/author.txt' , 
		    $file . '/history/'.$log_time . '.author');
	}
      }
    }
    if (file_exists($file . $ext)){    // if the file still exists
      sb_delete_file( $file . $ext );  // --> remove it!
    }
  }
  return $file . $ext;
}


?>