<?php 

// pppBlog
// ------------------------------
// Based on Simple PHP blog and TFT gallery
// Joerg Tiedemann, jorgen <at> gmx.net
//	
// pppBlog is released under the GNU Public License.
//


// the following files include functions required by sb_config

include_once('scripts/sb_fileio.php');
include_once('scripts/sb_language.php');
include_once('scripts/sb_menublocks.php');
	
	// ----------------
	// Config Functions
	// ----------------
	
function read_config ( ) {
  // Read config information from file.
  //
  global $blog_config;
		
  $blog_config = array();
		
  // LOAD CONFIG INFORMATION
  $contents = sb_read_file( 'config/config.txt' );
  if ( $contents ) {
    $temp_configs = explode('|', $contents);
    $config_keys = array( 	'blog_title', 
				'blog_author',
				'blog_footer',
				'blog_language',
				'blog_entry_order',
				'blog_comment_order',
				'blog_enable_comments',
				'blog_max_entries',
				'blog_comments_popup',
				'comment_tags_allowed',
				'blog_email',
				'blog_enable_gzip_txt',
				'blog_enable_gzip_output',
				'blog_email_notification',
				'blog_send_pings',
				'blog_ping_urls',
				'blog_enable_voting',
				'blog_trackback_enabled',
				'blog_trackback_auto_discovery',
				// new in pppBLOG 0.3.1:
				'blog_entry_width',
				'blog_max_pic_width',
				'blog_max_pic_height',
				'blog_thumb_width',
				'blog_thumb_height',
				'blog_thumbs_per_page',
				//'blog_pic_extensions'
				'blog_enable_signup',
				'blog_mimetex',
				'blog_menu_width',
				'blog_fckeditor',
				'disable_comment_capcha',
				'blog_album_style',
				'blog_hide_header',
				'blog_hide_title',
				'blog_hide_header_searchbox',
				'blog_hide_header_links'
				);
									
    for ( $i = 0; $i < count( $temp_configs ); $i++ ) {
      $key = $config_keys[ $i ];
      $blog_config[ $key ] = $temp_configs[ $i ];
    }
  }
		
  // I've got a minor issue here. You have to set the language before you
  // can call strings.php. The strings file doesn't get called until after
  // read_config. So... I'm just putting in some english values here....
		
  if ( !isset( $blog_config[ 'blog_language' ] ) ) {
    $blog_config[ 'blog_language' ] = 'english';
  }
  $blog_config[ 'blog_default_language' ] = $blog_config[ 'blog_language' ];

  if (isset($_REQUEST['lang'])){
    if (strlen($_REQUEST['lang'])){
      $_SESSION['language']=$_REQUEST['lang'];
    }
    else{
      $_SESSION['language']=$blog_config[ 'blog_language' ];
    }
  }
  if (isset($_SESSION['language'])){
    $blog_config[ 'blog_language' ] = $_SESSION['language'];
  }

		
  if ( !isset( $blog_config[ 'blog_title' ] ) ) {
    $blog_config[ 'blog_title' ]= 'No Title';
  }
		
  if ( !isset( $blog_config[ 'blog_author' ] ) ) {
    $blog_config[ 'blog_author' ] = 'No Author';
  }
		
  if ( !isset( $blog_config[ 'blog_email' ] ) ) {
    $blog_config[ 'blog_email' ] = 'email@myblog.com';
  }
		
  if ( !isset( $blog_config[ 'blog_footer' ] ) ) {
    $blog_config[ 'blog_footer' ] = 'No Footer'; // $lang_string[ 'sb_default_footer' ];
  }
		
  if ( !isset( $blog_config[ 'blog_entry_order' ] ) ) {
    $blog_config[ 'blog_entry_order' ] = 'new_to_old';
  }
		
  if ( !isset( $blog_config[ 'blog_comment_order' ] ) ) {
    $blog_config[ 'blog_comment_order' ] = 'new_to_old';
  }
		
  if ( !isset( $blog_config[ 'blog_enable_comments' ] ) ) {
    $blog_config[ 'blog_enable_comments' ] = 1;
  }
		
  if ( !isset( $blog_config[ 'blog_max_entries' ] ) ) {
    $blog_config[ 'blog_max_entries' ] = 5;
  }
		
  if ( !isset( $blog_config[ 'blog_comments_popup' ] ) ) {
    $blog_config[ 'blog_comments_popup' ] = 0;
  }
		
  if ( !isset( $blog_config[ 'blog_enable_voting' ] ) ) {
    $blog_config[ 'blog_enable_voting' ] = 1;
  }

  if ( !isset( $blog_config[ 'blog_enable_signup' ] ) ) {
    $blog_config[ 'blog_enable_signup' ] = 0;
  }
		
  if ( !isset( $blog_config[ 'comment_tags_allowed' ] ) ) {
    $blog_config[ 'comment_tags_allowed' ] = explode(',', 'b,i,strong,em,url');
  } else {
    $blog_config[ 'comment_tags_allowed' ] = explode(',', $blog_config[ 'comment_tags_allowed' ]);
  }
		
  if ( !isset( $blog_config[ 'blog_enable_gzip_txt' ] ) ) {
    $blog_config[ 'blog_enable_gzip_txt' ] = 0;
  }
		
  if ( !isset( $blog_config[ 'blog_enable_gzip_output' ] ) ) {
    $blog_config[ 'blog_enable_gzip_output' ] = 0;
  }
		
  if ( !isset( $blog_config[ 'blog_email_notification' ] ) ) {
    $blog_config[ 'blog_email_notification' ] = 0;
  }
		
  if ( !isset( $blog_config[ 'blog_send_pings' ] ) ) {
    $blog_config[ 'blog_send_pings' ] = 0;
  }
		
  if ( !isset( $blog_config[ 'blog_ping_urls' ] ) ) {
    $blog_config[ 'blog_ping_urls' ] = '';
  }
		
  if ( !isset( $blog_config[ 'blog_trackback_enabled' ] ) ) {
    $blog_config[ 'blog_trackback_enabled' ] = 0;
  }
		
  if ( !isset( $blog_config[ 'blog_trackback_auto_discovery' ] ) ) {
    $blog_config[ 'blog_trackback_auto_discovery' ] = 0;
  }

  // new in pppBLOG 0.3.1
  if ( !isset( $blog_config[ 'blog_entry_width' ] ) ) {
    $blog_config[ 'blog_entry_width' ] = 550;
  }
  if ( !isset( $blog_config[ 'blog_max_pic_width' ] ) ) {
    $blog_config[ 'blog_max_pic_width' ]=$blog_config[ 'blog_entry_width' ]-50;
  }
  if ( !isset( $blog_config[ 'blog_max_pic_height' ] ) ) {
    $blog_config['blog_max_pic_height']=$blog_config['blog_max_pic_width']-100;
  }
  if ( !isset( $blog_config[ 'blog_thumb_height' ] ) ) {
    $blog_config[ 'blog_thumb_height' ] = 130;
  }
  if ( !isset( $blog_config[ 'blog_thumb_width' ] ) ) {
    $blog_config[ 'blog_thumb_width' ] = 180;
  }
  if ( !isset( $blog_config['blog_thumbs_per_page'] ) ) {
    $blog_config['blog_thumbs_per_page'] = 15;
  }
  if ( !isset( $blog_config[ 'blog_album_style' ] ) ) {
    $blog_config[ 'blog_album_style' ] = 'bottomleft';
  }
  //  if ( !isset( $blog_config[ 'blog_pic_extensions' ] ) ) {
  //    $blog_config[ 'blog_pic_extensions' ] = "/\.(jpg|jpeg|gif|png)$/i";
  //  }

  if ( !isset( $blog_config[ 'blog_mimetex' ] ) ) {
    $blog_config[ 'blog_mimetex' ]= 'http://www.forkosh.com/mimetex.cgi';
  }

  if ( !isset( $blog_config[ 'blog_menu_width' ] ) ) {
    $blog_config[ 'blog_menu_width' ] = 170;
  }

  if ( !isset( $blog_config[ 'blog_fckeditor' ] ) ) {
    $blog_config[ 'blog_fckeditor' ] = 0;
  }

  if ( !isset( $blog_config[ 'disable_comment_capcha' ] ) ) {
    $blog_config[ 'disable_comment_capcha' ] = 0;
  }

  if ( !isset( $blog_config[ 'blog_hide_header' ] ) ) {
    $blog_config[ 'blog_hide_header' ] = 0;
  }

  if ( !isset( $blog_config[ 'blog_hide_title' ] ) ) {
    $blog_config[ 'blog_hide_title' ] = 0;
  }

  if ( !isset( $blog_config[ 'blog_hide_header_searchbox' ] ) ) {
    $blog_config[ 'blog_hide_header_searchbox' ] = 0;
  }

  if ( !isset( $blog_config[ 'blog_hide_header_links' ] ) ) {
    $blog_config[ 'blog_hide_header_links' ] = 0;
  }
		
  // READ META-DATA INFORMATION
  $contents = sb_read_file( 'config/metainfo.txt' );
  if ( $contents ) {
    $temp_configs = explode('|', $contents);
    $config_keys = array( 	'info_keywords', 
				'info_description',
				'info_copyright', );
    
    for ( $i = 0; $i < count( $temp_configs ); $i++ ) {
      $key = $config_keys[ $i ];
      $blog_config[ $key ] = $temp_configs[ $i ];
    }
  }
		
  if ( !isset( $blog_config[ 'info_keywords' ] ) ) {
    $blog_config[ 'info_keywords' ] = '';
  }
		
  if ( !isset( $blog_config[ 'info_description' ] ) ) {
    $blog_config[ 'info_description' ]= '';
  }
		
  if ( !isset( $blog_config[ 'info_copyright' ] ) ) {
    $blog_config[ 'info_copyright' ] = '';
  }
		
  // LOAD THEME
  read_theme();
		
  global $blog_theme;
  require_once('themes/' . $blog_theme . '/themes.php');
		
  // LOAD COLORS
  read_colors();
		
  // Start GZIP Output
  if ( $blog_config[ 'blog_enable_gzip_output' ] ) {
    sb_gzoutput ();
  }
}
	
function write_config ( $blog_title, $blog_author, $blog_email, $blog_footer, $blog_language, $blog_entry_order, $blog_comment_order, $blog_enable_comments, $blog_max_entries, $blog_comments_popup, $comment_tags_allowed, $blog_enable_gzip_txt, $blog_enable_gzip_output, $blog_email_notification, $blog_send_pings, $blog_ping_urls, $blog_enable_voting, $blog_trackback_enabled, $blog_trackback_auto_discovery, 
			$blog_entry_width,
			$blog_max_pic_width,
			$blog_max_pic_height,
			$blog_thumb_width,
			$blog_thumb_height,
			$blog_thumbs_per_page,
			$blog_enable_signup,
			$blog_mimetex,
			$blog_menu_width,
			$blog_fckeditor,
			$disable_comment_capcha = 0,
			$blog_album_style,
			$blog_hide_header = 0,
			$blog_hide_title = 0,
			$blog_hide_header_searchbox = 0,
			$blog_hide_header_links = 0
 ) {
  // Save config information to file.
  //
  $array = array( clean_post_text( $blog_title ),
		  clean_post_text( $blog_author ),
		  clean_post_text( $blog_footer ),
		  $blog_language,
		  $blog_entry_order,
		  $blog_comment_order,
		  $blog_enable_comments,
		  $blog_max_entries,
		  $blog_comments_popup,
		  $comment_tags_allowed,
		  clean_post_text( $blog_email ),
		  $blog_enable_gzip_txt,
		  $blog_enable_gzip_output,
		  $blog_email_notification,
		  $blog_send_pings,
		  clean_post_text( $blog_ping_urls ),
		  $blog_enable_voting,
		  $blog_trackback_enabled,
		  $blog_trackback_auto_discovery,
                  // new in pppBLOG 0.3.1:
		  $blog_entry_width,
		  $blog_max_pic_width,
		  $blog_max_pic_height,
		  $blog_thumb_width,
		  $blog_thumb_height,
		  $blog_thumbs_per_page,
		  //$blog_pic_extensions,
		  $blog_enable_signup,
		  $blog_mimetex,
		  $blog_menu_width,
		  $blog_fckeditor,
		  $disable_comment_capcha,
		  $blog_album_style,
		  $blog_hide_header,
		  $blog_hide_title,
		  $blog_hide_header_searchbox,
		  $blog_hide_header_links
		  );
						
  $str = implode('|', $array);
		
  if (!file_exists('config')) {
    $oldumask = umask(0);
    $ok = my_mkdir( 'config', 0777 );
    umask( $oldumask );
  }
		
  $filename = 'config/config.txt';
  $result = sb_write_file( $filename, $str );
  
  $filename='config/~blog_entry_listing.tmp';
  sb_delete_file( $filename );

  if ( $result ) {
    return ( true );
  } else {
    // Error:
    // Probably couldn't create file...
    return ( $filename );
  }
}
	
// ----------------
// Meta Information
// ----------------
	
function write_metainfo ( $info_keywords, $info_description, $info_copyright ){
  // Save information to file.
  //
  $array = array( clean_post_text( $info_keywords ),
		  clean_post_text( $info_description ),
		  clean_post_text( $info_copyright ) );
						
  $str = implode('|', $array);
		
  if (!file_exists('config')) {
    $oldumask = umask(0);
    $ok = my_mkdir( 'config', 0777 );
    umask( $oldumask );
  }
		
  $filename = 'config/metainfo.txt';
  $result = sb_write_file( $filename, $str );
		
  if ( $result ) {
    return ( true );
  } else {
    // Error:
    // Probably couldn't create file...
    return ( $filename );
  }
}
	
// --------------
// Theme Settings
// --------------
	
function write_theme ( $blog_theme ) {
  // Save theme information to file.
  //
  $array = array( clean_post_text( $blog_theme ) );
  
  $str = implode('|', $array);
		
  if (!file_exists('config')) {
    $oldumask = umask(0);
    $ok = my_mkdir('config', 0777 );
    umask($oldumask);
  }
		
  $filename = 'config/theme.txt';
  $result = sb_write_file( $filename, $str );
		
  if ( $result ) {
    return ( true );
  } else {
    // Error:
    if ( file_exists( $filename ) ) {
      $str = 'Could not update file: '.$filename.'<br />';
    } else {
      $str = 'Could not create file: '.$filename.'<br />';
    }
    return ( $str );
  }
}
	

function read_theme ( ) {
  // Read theme information from file.
  //
  global $blog_theme;

  $blog_theme = 'blue';

  if (isset($_REQUEST['theme'])){
    $themedir = str_replace('/','',$_REQUEST['theme']);
    if (is_dir('themes/'.$themedir)){
      $_SESSION['theme']=$themedir;
    }
  }
  if (isset($_SESSION['theme'])){
    if (is_dir('themes/'.$_SESSION['theme'])){
      $blog_theme = $_SESSION['theme'];
      return $blog_theme;
    }
  }

  $filename = 'config/theme.txt';
  $result = sb_read_file( $filename );

  if ( $result ) {
    list( $blog_theme ) = explode(':', $result);
  } else {
    // Error:
    // Probably couldn't read file.
    return ( $filename );
  }
} 
	
// ----------------------
// Color Config Functions
// ----------------------
	 
function write_colors ( $post_array, $user_file ) {
  // Save color information to file.
  //
  global $blog_theme;
		
  $str = implode('|', $post_array);
		
  if ( isset( $user_file ) ) {		
    if (!file_exists('config/schemes')) {
      $oldumask = umask(0);
      $ok = my_mkdir('config/schemes', 0777 );
      umask($oldumask);
    }
    $custom_file = 'config/schemes/' . $user_file . '.txt';
    $result = sb_write_file( $custom_file, $str );
  }
		
  $filename = 'config/colors-' . $blog_theme . '.txt';
  $result = sb_write_file( $filename, $str );
		
  if ( $result ) {
    return ( true );
  } else {
    // Error:
    // Probably couldn't create file...
    return ( $filename );
  }
}
	
function read_colors ( ) {
  // Read color information from file.
  //
  global $user_colors, $blog_theme;
  $color_def = theme_default_colors();
  for ( $i = 0; $i < count( $color_def ); $i++ ) {
    $user_colors[ $color_def[$i][ 'id' ] ] = $color_def[$i][ 'default' ];
  }
		
  $filename = 'config/colors-' . $blog_theme . '.txt';
  $result = sb_read_file( $filename );
  if ( $result ) {
    $saved_colors = explode('|', $result);
    for ( $i = 0; $i < count( $saved_colors ); $i = $i + 2 ) {
      $id = $saved_colors[$i];
      $color = $saved_colors[$i+1];
      $user_colors[ $id ] = $color;
    }
  }
}
?>