<?php 

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

	// --------------------
	// Entry Format Parsing
	// --------------------

	function clean_post_text( $str ) {
		// Cleans post text input.
		//
		// Strip out and replace pipes with colons. HTML-ize entities.
		// Use charset from the language file to make sure we're only
		// encoding stuff that needs to be encoded.
		//
		// This makes entries safe for saving to a file (since the data
		// format is pipe delimited.)
		global $lang_string;
		$str = str_replace( '|', '&#124;', $str );

		// original version: encode only special characters
		//
		// $str = @htmlspecialchars( $str, ENT_QUOTES, $lang_string[ 'php_charset' ] );
		//
		//////////////////////////////////////////////////////////////
		// alternative: encode every character with HTML entities
		// (maybe we want to have more than 1 language in the blog!)
		//
		// $str = @htmlentities( $str, ENT_QUOTES, $lang_string[ 'php_charset' ] );

		// third version: use a robust version
		$str = @ppp_htmlentities( $str, ENT_QUOTES, $lang_string[ 'php_charset' ] );
		return ( $str );
	}


function split_at_more($str,$type,$style,$format,$entry,$m,$y){
  if ($type == 'content'){
    $parts = explode('[more]',$str);
    if ($style == 'standard' && $entry !== null){
      global $lang_string;
      $str = array_shift($parts);
      if (count($parts)>0){
	if ($format == 'fck'){
	  $str.="<a href=\"comments.php?entry=$entry&amp;y=$y&amp;m=$m\">";
	  $str.=$lang_string['more'].'</a>';
	}
	else{
	  $str.="[url=comments.php?entry=$entry&amp;y=$y&amp;m=$m new=false]";
	  $str.=$lang_string['more'].'[/url]';
	}
      }
    }
    else{
      $str = implode('',$parts);
    }
  }
  return $str;
}
	
function blog_to_html( $str, $comment_mode, 
		       $strip_all_tags, $add_no_follow = false,
		       $cache = false,$cache_type='content',
		       $style='standard',$entry=null,$y=null,$m=null) {
  // Convert Simple Blog tags to HTML.
  //
  // Search and replace simple tags. These tags don't have any
  // special attributes so we can do a str_replace() on them.
  //
  // ( Could use str_ireplace() but it's only supported in PHP 5)


  // entries made with the FCK editor do not have to be converted
  if (!$comment_mode){
    global $entry_format;
    if ($entry_format == 'fck'){
      $str = split_at_more($str,$cache_type,$style,$entry_format,$entry,$m,$y);
      return $str;
    }
  }

  // now check the file cache

  // caching is only valid if there is an existing file
  if (!file_exists($cache)){$cache=false;}
  // (we can also switch off caching with 'cacheoff')
  if(isset($_REQUEST['cacheoff'])){$cache = false;}
  // caching is not a good idea for RSS feeds!
  if (strpos($str,'[rss=')!==false){$cache = false;}
  if (strpos($str,'[RSS=')!==false){$cache = false;}

  // if caching is enabled
  // --> check if there is a valid cache file
  //     --> yes: return cache file contents
  if ($cache){
    $blog_filename = $cache;
    $cache_filename = $blog_filename;
    //    if($comment_mode) $cache_filename.= "_co";
    //    if($strip_all_tags) $cache_filename.= "_st";
    //    if($add_no_follow) $cache_filename.= "_nf";
    if ($style == 'comments') $cache_filename.= "_more";
    $cache_filename .= "_$cache_type";
    $cache_filename .= ".cache";

    $blog_filemtime = @filemtime($blog_filename);
		
    if($cache && is_readable($cache_filename)){
      $cache_filemtime = @filemtime($cache_filename);
      if($blog_filemtime == $cache_filemtime){
	$contents = file_get_contents($cache_filename);
	if($contents){
	  return $contents;
	}
      }
    }
  }

  $str = split_at_more($str,$cache_type,$style,$entry_format,$entry,$m,$y);
  /*
  if ($cache_type == 'content'){
    $parts = explode('[more]',$str);
    if ($style == 'standard' && $entry !== null){
      global $lang_string;
      $str = array_shift($parts);
      if (count($parts)>0){
	$str .= "[url=comments.php?entry=$entry&amp;y=$y&amp;m=$m new=false]";
	$str .= $lang_string['more'].'[/url]';
      }
    }
    else{
      $str = implode('',$parts);
    }
  }
  */

  // no cached data found ---> convert string!
  global $blog_config;

  if ( $comment_mode ) {
    $tag_arr = array();
    if ( in_array( 'i', $blog_config[ 'comment_tags_allowed' ] ) ) { array_push( $tag_arr, 'i' ); }
    if ( in_array( 'b', $blog_config[ 'comment_tags_allowed' ] ) ) { array_push( $tag_arr, 'b' ); }
    if ( in_array( 'blockquote', $blog_config[ 'comment_tags_allowed' ] ) ) { array_push( $tag_arr, 'blockquote' ); }
    if ( in_array( 'strong', $blog_config[ 'comment_tags_allowed' ] ) ) { array_push( $tag_arr, 'strong' ); }
    if ( in_array( 'em', $blog_config[ 'comment_tags_allowed' ] ) ) { array_push( $tag_arr, 'em' ); }
    if ( in_array( 'hN', $blog_config[ 'comment_tags_allowed' ] ) ) { array_push( $tag_arr, 'h1', 'h2', 'h3', 'h4', 'h5', 'h6' ); }
    if ( in_array( 'del', $blog_config[ 'comment_tags_allowed' ] ) ) { array_push( $tag_arr, 'del' ); }
    if ( in_array( 'ins', $blog_config[ 'comment_tags_allowed' ] ) ) { array_push( $tag_arr, 'ins' ); }
    if ( in_array( 'strike', $blog_config[ 'comment_tags_allowed' ] ) ) { array_push( $tag_arr, 'strike' ); }
    if ( in_array( 'pre', $blog_config[ 'comment_tags_allowed' ] ) ) { array_push( $tag_arr, 'pre' ); }
    if ( in_array( 'code', $blog_config[ 'comment_tags_allowed' ] ) ) { array_push( $tag_arr, 'code' ); }
  } else {
    $tag_arr = array('i', 'b', 'blockquote', 'strong', 'em', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'del', 'ins', 'strike', 'pre', 'code' );
  }
  
  // Build search and replace arrays.
  $search_arr = array();
  $replace_arr = array();
  for ( $i = 0; $i < count( $tag_arr ); $i++ ) {
    $tag = $tag_arr[$i];
    array_push( $search_arr,  '[' . strtolower( $tag ) . ']',  '[' . strtoupper( $tag ) . ']' );
    if ( $strip_all_tags ) {
      array_push( $replace_arr, '',  '' );
    } else {
      array_push( $replace_arr, '<' . strtolower( $tag ) . '>',  '<' . strtoupper( $tag ) . '>' );
    }
    array_push( $search_arr,  '[/' . strtolower( $tag ) . ']', '[/' . strtoupper( $tag ) . ']' );
    if ( $strip_all_tags ) {
      array_push( $replace_arr, '',  '' );
    } else {
      array_push( $replace_arr, '</' . strtolower( $tag ) . '>', '</' . strtoupper( $tag ) . '>' );
    }
  }
		
  // Do str_replace() replacement.
  $str = str_replace( $search_arr, $replace_arr, $str);
		
  // Replace [url] Tags:
  // The [url] tag has an optional "new" attribute. The "new"
  // attribute determines whether to open the link in the
  // same window or a new window.
  // new   - (true/false)
  //
  // [url=http://xxx]xxx[/url] 
  // [url=http://xxx new=true]xxx[/url]
  if ( $comment_mode ) {
    if ( in_array( 'url', $blog_config[ 'comment_tags_allowed' ] ) && $strip_all_tags === false ) {
      $str = replace_url_tag( $str, '[url=', ']', '[/url]', false, $add_no_follow );
      $str = replace_url_tag( $str, '[URL=', ']', '[/URL]', false, $add_no_follow );
    } else {
      $str = replace_url_tag( $str, '[url=', ']', '[/url]', true, $add_no_follow );
      $str = replace_url_tag( $str, '[URL=', ']', '[/URL]', true, $add_no_follow );
    }
  } else {
    if ( $strip_all_tags ) {
      $str = replace_url_tag( $str, '[url=', ']', '[/url]', true, $add_no_follow );
      $str = replace_url_tag( $str, '[URL=', ']', '[/URL]', true, $add_no_follow );
    } else {
      $str = replace_url_tag( $str, '[url=', ']', '[/url]', false, $add_no_follow );
      $str = replace_url_tag( $str, '[URL=', ']', '[/URL]', false, $add_no_follow );
    }
  }
		
  // Replace [img] Tags:
  // The [img] tag has an number of optional attributes -
  // width  - width of image in pixels
  // height - height of image in pixels
  // popup  - (true/false)
  // float  - (left/right)
  // alt    - alternative text
  // align  - (left/right/center)
  // valign - (top/bottom/middle)
  //
  // [img=http://xxx]
  // [img=http://xxx width=xxx height=xxx popup=true float=left]
  if ( $comment_mode ) {
    if ( in_array( 'img', $blog_config[ 'comment_tags_allowed' ] ) && $strip_all_tags === false  ) {
      $str = replace_img_tag( $str, '[img=', ']', false );
      $str = replace_img_tag( $str, '[IMG=', ']', false );
    } else {
      $str = replace_img_tag( $str, '[img=', ']', true );
      $str = replace_img_tag( $str, '[IMG=', ']', true );
    }
  } else {
    if ( $strip_all_tags ) {
      $str = replace_img_tag( $str, '[img=', ']', true );
      $str = replace_img_tag( $str, '[IMG=', ']', true );
    } else {
      $str = replace_img_tag( $str, '[img=', ']', false );
      $str = replace_img_tag( $str, '[IMG=', ']', false );
    }
  }

  if ( ! $comment_mode ) {
    $str = replace_rss_tag($str,'[rss=',']',false);
    $str = replace_rss_tag($str,'[RSS=',']',false);
  }


  // Emoticons
  if ( !$strip_all_tags ) {
    $emote_arr = emoticons_load_tags();
    
    for ( $i = 0; $i < count($emote_arr); $i++) {
      
      $emotetag_arr = explode( ' ', $emote_arr[$i]['TAGS'] );
      for ( $j = 0; $j < count( $emotetag_arr ); $j++ ) {
	$html_safe_tag = @htmlspecialchars( $emotetag_arr[$j], ENT_QUOTES, $lang_string[ 'php_charset' ] );
	
	array_push( $search_arr, $emotetag_arr[$j] );
	array_push( $replace_arr, '<img src="' . $emote_arr[$i]['PATH'] . '" alt="' . $html_safe_tag . '" />' );
      }
    }
  }
		
  $str = str_replace( $search_arr, $replace_arr, $str);
		
  // Selectively replace line breaks and/or decode html entities.
  if ( $comment_mode ) {		
    if ( in_array( 'html', $blog_config[ 'comment_tags_allowed' ] ) && $strip_all_tags === false ) {
      $str = replace_html_tag( $str, false );
    } else {
      $str = replace_html_tag( $str, true );
    }
  } else {
    if ( $strip_all_tags ) {
      $str = replace_html_tag( $str, true );
    } else {
      $str = replace_html_tag( $str, false );
    }
  }

  if ( $comment_mode ) {		
    if ( in_array( 'table', $blog_config[ 'comment_tags_allowed' ] ) && $strip_all_tags === false ) {
      $str = replace_table_tag( $str, false );
    } else {
      $str = replace_table_tag( $str, true );
    }
  } else {
    if ( $strip_all_tags ) {
      $str = replace_table_tag( $str, true );
    } else {
      $str = replace_table_tag( $str, false );
    }
  }


  // Selectively replace line breaks and/or decode php entities.
  if ( !$comment_mode ) {
    $str = replace_php_tag( $str );
  }
  
  $str = str_replace( '&amp;#124;', '|', $str );
  

  // caching is enable but there was no valid cache file
  // create it now!
  if($cache){
      if(touch($cache_filename)){  //create the file if it doesn't exist.
	if(is_writable($cache_filename)){
	  file_put_contents($cache_filename,$str);
	  touch($cache_filename,$blog_filemtime);
	}
      }
  }
  return ( $str );
}



function replace_rss_tag( $str_in, $tag_begin, $tag_end, $strip_tags ) {
  // Replacements for RSS FEED tags. Sub-function of blog_to_html.
  // 
  global $theme_vars;

  $str_out = NULL;
		
  // Search for the beginning part of the tag.
  $str_offset = strpos( $str_in, $tag_begin );
  while ( $str_offset !== false ) {
    // Store sub_string before the tag.
    $str_out = $str_out . substr( $str_in, 0, $str_offset );
    // Store sub_string after the tag.
    $str_in = substr( $str_in, $str_offset + strlen( $tag_begin ) );
			
    // Search for the ending part of the tag.
    $str_offset = strpos( $str_in, $tag_end );

    if ( $str_offset !== false ) {

      if ( $strip_tags == true ) {
					
	// Store sub_string after the tag.
	$str_in = substr( $str_in, $str_offset + strlen( $tag_end ) );
	// Search for next beginning tag.
	$str_offset = strpos( $str_in, $tag_begin );
	
      } else {
	
	// Store attribues between between the tags.
	$attrib_array = explode( ' ', substr( $str_in, 0, $str_offset ) );

	$src = NULL;
	$chan = 'y';
	$desc = 400;
	$date = 'y';
	$num = 10;
	$str = NULL;
	$channel_img = NULL;
	$html = NULL;

	$utf = 'n';
	$targ = 'popup';
	
	if ( is_array( $attrib_array ) ) {
	  $src = $attrib_array[0];
	  
	  for ( $i = 1; $i < count( $attrib_array ); $i++ ) {
	    $temp_arr = explode( '=', $attrib_array[$i] );
	    if ( is_array( $temp_arr ) && count( $temp_arr ) == 2 ) {
	      switch ( $temp_arr[0] ) {
		case 'chan';
		$chan = $temp_arr[1];
		break;
		case 'desc';
		$desc = $temp_arr[1];
		break;
		case 'date';
		$date = $temp_arr[1];
		break;
		case 'num';
		$num = $temp_arr[1];
		break;
		case 'utf8';
		$utf = $temp_arr[1];
		break;
		case 'target';
		$targ = $temp_arr[1];
		break;
		case 'html';
		$html = $temp_arr[1];
		break;
		case 'style';
		$css = $temp_arr[1];
		break;
		case 'img';
		$channel_img = $temp_arr[1];
		break;
	      }
	    }
	  }
	} else {
	  $src = $attrib_array;
	}

	if ($desc == 'a'){
	    if (!isset($html)){
		$html = 'a';
	    }
	}
	require('scripts/feed2php.inc');
	$str_out .= $str;

	// Store sub_string after the tag.
	$str_in = substr( $str_in, $str_offset + strlen( $tag_end ) );
	// Search for next beginning tag.
	$str_offset = strpos( $str_in, $tag_begin );


      }
    }
  }
  return ( $str_out .  $str_in );
}


function find_opening_tag($str,$tag){
  $temp_lower = strpos( $str, strtolower( '['.$tag ) );
  $temp_upper = strpos( $str, strtoupper( '['.$tag ) );
  if ( $temp_lower === false ) {
    if ( $temp_upper === false ) {
      $startpos = false;
    } else {
      $startpos = $temp_upper;
    }
  } else {
    if ( $temp_upper === false ) {
      $startpos = $temp_lower;
    } else {
      $startpos = min( $temp_upper, $temp_lower );
    }
  }
  if ( $startpos !== false ) {
    $after = substr( $str, $startpos + strlen( '['.$tag ) );
    $endpos = strpos( $after, ']');

    if ( $endpos !== false ) {
      $attribstr = explode( ' ', substr( $after, 0, $endpos ) );
      $endpos+=$startpos+strlen('['.$tag);
      $attr=array();
      if ( is_array( $attribstr ) ) {
	for ( $i = 1; $i < count( $attribstr ); $i++ ) {
	  $arr = explode( '=', $attribstr[$i] );
	  //	  echo "attr ($attribstr[$i]): $arr[0] --- $arr[1]<br>";
	  if (count($arr)>1){
	    $attr[$arr[0]] = $arr[1];
	  }
	  else{
	    $attr[''] = $arr[0];
	  }
	}
	return array($startpos,$endpos,$attr);
      }
    }
  }
  return array(false,false,array());
}


function find_closing_tag($str,$tag){
  $temp_lower = strpos( $str, strtolower( '[/'.$tag.']' ) );
  $temp_upper = strpos( $str, strtoupper( '[/'.$tag.']' ) );
  if ( $temp_lower === false ) {
    if ( $temp_upper === false ) {
      $startpos = false;
    } else {
      $startpos = $temp_upper;
    }
  } else {
    if ( $temp_upper === false ) {
      $startpos = $temp_lower;
    } else {
      $startpos = min( $temp_upper, $temp_lower );
    }
  }
  if ( $startpos !== false ) {
      $endpos=$startpos+strlen('[/'.$tag.']');
      return array($startpos,$endpos);
  }
  return array(false,false);
}





function replace_table_tag( $str, $strip_tags ) {
  global $lang_string;
		
  $str_out = NULL;
  $tag_begin = '[table';
  $tag_end = ']';
  $tag_close = '[/table]';

  list($startpos,$endpos,$attr)=find_opening_tag($str,'table');

  while ( $startpos !== false ) {
    // Store all the text BEFORE the opening table tag.
    //
    // Also, replace hard returns with '<br />' tags.
    $temp_str = substr( $str, 0, $startpos );
    //    $temp_str = str_replace( chr(10), '<br />', $temp_str );
    $str_out = $str_out . $temp_str;
			
    // Store all text AFTER the tag
    $str = substr( $str, $endpos+1 );
    
    // Search for the closing table tag. Find the nearest one.
    list($str_offset,$tmp_end) = find_closing_tag($str,'table');
			
    if ( $str_offset !== false ) {
      // Store all the text BETWEEN the table tags.
      $temp_str = substr( $str, 0, $str_offset );
      $table_start = '<table';
      $table_end='</td></tr></table>';
      if ( is_array( $attr ) ) {
	if (array_key_exists('border',$attr)){
	  $table_start .= ' border="'.$attr['border'].'"';
	}
	if (array_key_exists('width',$attr)){
	  $table_start .= ' width="'.$attr['width'].'"';
	}
	if (array_key_exists('padding',$attr)){
	  $table_start .= ' cellpadding="'.$attr['padding'].'"';
	}
      }
      $table_start .= '><tr><td valign="top">';

      if ( $strip_tags === false ) {
	$temp_str = str_replace( '&#124;&#124;', '</td><td valign="top">', $temp_str );
	$temp_str = str_replace( '\\\\', '</td></tr><tr><td valign="top">', $temp_str );
	
	$temp_str = $table_start.$temp_str.$table_end;
      }
      $str_out = $str_out . $temp_str;
				
      // Store sub_string after the tag.
      $str = substr( $str, $str_offset + strlen( $tag_close ) );
				
      // Search for opening table tag again.
      list($startpos,$endpos,$attr)=find_opening_tag($str,'table');
    }
  }
		
  // Append remainder of text.
  //
  // All this text will be outside of any table tags so
  // we need to encode the line breaks.
  //  $str = str_replace( chr(10), '<br />', $str );
  $str = $str_out . $str;
		
  return ( $str );
	
}

	
	function replace_html_tag( $str, $strip_tags ) {
		// Replacements for HTML tags. Sub-function of blog_to_html.
		//
		// This function decodes HTML entities that are located between
		// HTML tags. Also, inserts <br />'s for new lines only if blocks
		// are outside the HTML tags.
		global $lang_string;
		
		$str_out = NULL;
		$tag_begin = '[html]';
		$tag_end = '[/html]';
		
		// Search for the openning HTML tag. Tag could be either upper or
		// lower case so we want to find the nearest one.
		//
		// Get initial $str_offset value.
		$temp_lower = strpos( $str, strtolower( $tag_begin ) );
		$temp_upper = strpos( $str, strtoupper( $tag_begin ) );
		if ( $temp_lower === false ) {
			if ( $temp_upper === false ) {
				$str_offset = false;
			} else {
				$str_offset = $temp_upper;
			}
		} else {
			if ( $temp_upper === false ) {
				$str_offset = $temp_lower;
			} else {
				$str_offset = min( $temp_upper, $temp_lower );
			}
		}
		
		while ( $str_offset !== false ) {
			// Store all the text BEFORE the openning HTML tag.
			//
			// Also, replace hard returns with '<br />' tags.
			$temp_str = substr( $str, 0, $str_offset );

			$temp_str = str_replace( chr(10), '<br />', $temp_str );
			$str_out = $str_out . $temp_str;
			
			// Store all text AFTER the tag
			$str = substr( $str, $str_offset + strlen( $tag_begin ) );
		
			// Search for the closing HTML tag. Find the nearest one.
			$temp_lower = strpos( $str, strtolower( $tag_end ) );
			$temp_upper = strpos( $str, strtoupper( $tag_end ) );
			if ( $temp_lower === false ) {
				if ( $temp_upper === false ) {
					$str_offset = false;
				} else {
					$str_offset = $temp_upper;
				}
			} else {
				if ( $temp_upper === false ) {
					$str_offset = $temp_lower;
				} else {
					$str_offset = min( $temp_upper, $temp_lower );
				}
			}
			
			if ( $str_offset !== false ) {
				// Store all the text BETWEEN the HTML tags.
				//
				// Also, decode HTML entities between the tags.
				$temp_str = substr( $str, 0, $str_offset );
				if ( $strip_tags === false ) {
					$temp_str = html_entity_decode( $temp_str, ENT_QUOTES, $lang_string[ 'php_charset' ] );
				}
				$str_out = $str_out . $temp_str;
				
				// Store sub_string after the tag.
				$str = substr( $str, $str_offset + strlen( $tag_end ) );
				
				// Search for openning HTML tag again.
				$temp_lower = strpos( $str, strtolower( $tag_begin ) );
				$temp_upper = strpos( $str, strtoupper( $tag_begin ) );
				if ( $temp_lower === false ) {
					if ( $temp_upper === false ) {
						$str_offset = false;
					} else {
						$str_offset = $temp_upper;
					}
				} else {
					if ( $temp_upper === false ) {
						$str_offset = $temp_lower;
					} else {
						$str_offset = min( $temp_upper, $temp_lower );
					}
				}
			}
		}
		
		// Append remainder of text.
		//
		// All this text will be outside of any HTML tags so
		// we need to encode the line breaks.
		$str = str_replace( chr(10), '<br />', $str );
		$str = $str_out . $str;
		
		return ( $str );
	
	}
	
	function replace_url_tag( $str, $tag_begin, $tag_end, $tag_close, $strip_tags, $add_no_follow = false ) {
		// Replacements for URL tags. Sub-function of blog_to_html.
		//
		// If $strip_tags == true then it will strip out the tag
		// instead of making them HTML.
		$str_out = NULL;
		
		// Search for the beginning part of the tag.
		$str_offset = strpos( $str, $tag_begin );
		while ( $str_offset !== false ) {
			// Store sub_string before the tag.
			$str_out = $str_out . substr( $str, 0, $str_offset );
			// Store sub_string after the tag.
			$str = substr( $str, $str_offset + strlen( $tag_begin ) );
			
			// Search for the ending part of the tag.
			$str_offset = strpos( $str, $tag_end );
			if ( $str_offset !== false ) {
				
				if ( $strip_tags == false ) {
					// Store attribues BETWEEN between the tags.
					$attrib_array = explode( ' ', substr( $str, 0, $str_offset ) );
					$attrib_new = NULL;
					
					if ( is_array( $attrib_array ) ) {
						$str_url = $attrib_array[0];
						
						for ( $i = 1; $i < count( $attrib_array ); $i++ ) {
							$temp_arr = explode( '=', $attrib_array[$i] );
							if ( is_array( $temp_arr ) && count( $temp_arr ) == 2 ) {
								switch ( $temp_arr[0] ) {
									case 'new';
										$attrib_new = $temp_arr[1];
										break;
								}
							}
						}
					} else {
						$str_url = $attrib_array;
					}
					
					// Append HTML tag.
					if ( isset( $attrib_new ) ) {
						if ( $attrib_new == 'false' ) {
							$str_out = $str_out . "<a href=\"" . $str_url . "\" ";
							if ( $add_no_follow == true ) {
								$str_out = $str_out . "rel=\"nofollow\">";
							} else {
								$str_out = $str_out . ">";
							}
						} else {
							$str_out = $str_out . "<a href=\"" . $str_url . "\" target=\"_blank\" ";
							if ( $add_no_follow == true ) {
								$str_out = $str_out . "rel=\"nofollow\">";
							} else {
								$str_out = $str_out . ">";
							}
						}
					} else {
						$str_out = $str_out . "<a href=\"" . $str_url . "\" target=\"_blank\" ";
						if ( $add_no_follow == true ) {
							$str_out = $str_out . "rel=\"nofollow\">";
						} else {
							$str_out = $str_out . ">";
						}
					}
				}
				
				// Store sub_string AFTER the tag.
				$str = substr( $str, $str_offset + strlen( $tag_end ) );
				
				// Look for closing tag.
				$str_offset = strpos( $str, $tag_close );
				if ( $str_offset !== false ) {
					$str_link = substr( $str, 0, $str_offset );
					if ( $strip_tags == false ) {
						$str_out = $str_out . $str_link . '</a>';
					} else {
						$str_out = $str_out . $str_link;
					}					
					$str = substr( $str, $str_offset + strlen( $tag_close ) );
				}
				
				// Search for next beginning tag.
				$str_offset = strpos( $str, $tag_begin );
			}
		}
		
		// Append remainder of tag.
		$str = $str_out . $str;
		
		return ( $str );
	}
	
function replace_img_tag( $str, $tag_begin, $tag_end, $strip_tags ) {
  // Replacements for IMG tags. Sub-function of blog_to_html.
  // 
  // I made this another function because I wanted to be able
  // to call it for upper and lower case '[img=]' tags...
  //
  // If $strip_tags == true then it will strip out the tag
  // instead of making them HTML.
  global $theme_vars;
		
  $str_out = NULL;
		
  // Search for the beginning part of the tag.
  $str_offset = strpos( $str, $tag_begin );
  while ( $str_offset !== false ) {
    // Store sub_string before the tag.
    $str_out = $str_out . substr( $str, 0, $str_offset );
    // Store sub_string after the tag.
    $str = substr( $str, $str_offset + strlen( $tag_begin ) );
			
    // Search for the ending part of the tag.
    $str_offset = strpos( $str, $tag_end );
    if ( $str_offset !== false ) {
			
      if ( $strip_tags == true ) {
					
	// Store sub_string after the tag.
	$str = substr( $str, $str_offset + strlen( $tag_end ) );
	// Search for next beginning tag.
	$str_offset = strpos( $str, $tag_begin );
					
      } else {
	
	// Store attribues between between the tags.
	$attrib_array = explode( ' ', substr( $str, 0, $str_offset ) );
	$attrib_width = NULL;
	$attrib_height = NULL;
	$attrib_popup = NULL;
	$attrib_float = NULL;
	$attrib_align = NULL;
	$attrib_valign = NULL;
	$attrib_alt = NULL;
	
	if ( is_array( $attrib_array ) ) {
	  $str_url = $attrib_array[0];
	  
	  for ( $i = 1; $i < count( $attrib_array ); $i++ ) {
	    $temp_arr = explode( '=', $attrib_array[$i] );
	    if ( is_array( $temp_arr ) && count( $temp_arr ) == 2 ) {
	      switch ( $temp_arr[0] ) {
		case 'width';
		$attrib_width = intval( $temp_arr[1] );
		break;
		case 'height';
		$attrib_height = intval( $temp_arr[1] );
		break;
		case 'popup';
		$attrib_popup = $temp_arr[1];
		break;
		case 'float';
		$attrib_float = $temp_arr[1];
		break;
		case 'align';
		$attrib_align = $temp_arr[1];
		break;
		case 'valign';
		$attrib_valign = $temp_arr[1];
		break;
		case 'alt';
		$attrib_alt = urldecode( $temp_arr[1] );
		break;
	      }
	    }
	  }
	} else {
	  $str_url = $attrib_array;
	}
	
	// Grab image size and calculate scaled sizes
	
	// if ( file_exists( $str_url ) !== false ) {
	$width = $theme_vars[ 'max_image_width' ];
	$img_size = @getimagesize( $str_url );
	if ( isset( $attrib_width ) && isset( $attrib_height ) ) {
	  $width = $attrib_width;
	  $height = $attrib_height;
	  // $img_size = array($width,$height);
	}
	else{
	    // $img_size = @getimagesize( $str_url );
	  if (is_array($img_size)){
	    $width = $img_size[0];
	    $height = $img_size[1];
	    $max_image_width = $theme_vars[ 'max_image_width' ];
	
	    if ( isset( $attrib_width ) ) {
	      $height = round( $height * ( $attrib_width / $width ) );
	      $width = $attrib_width;
	    }
	  
	    if ( isset( $attrib_height ) ) {
	      $width = round( $width * ( $attrib_height / $height ) );
	      $height = $attrib_height;
	    }
	  }
	  else{
	    if ( isset( $attrib_width ) ) {
	      $width = $attrib_width;
	    }
	    if ( isset( $attrib_height ) ) {
	      $height = $attrib_height;
	    }
	  }
	
	  if ( isset( $width ) ) {
	    if ( $width > $max_image_width ) {
	      $height = round( $height * ( $max_image_width / $width ) );
	      $width = $max_image_width;
	    }
	  }
	}
	
	//Open the tag
	if ( isset( $attrib_align ) ) {
	  switch ( $attrib_align ) {
	    case 'left';
	    $str_out = $str_out . '<p align="left">';
	    break;
	    case 'right';
	    $str_out = $str_out . '<p align="right">';
	    break;
	    case 'cemter';
	    $str_out = $str_out . '<p align="center">';
	    break;
	  }
	}
	
	if ( isset( $attrib_popup ) ) {
	  if ( $attrib_popup == 'true' ) {
	    $str_out = $str_out . '<a href="javascript:openpopup(\'' . $str_url . '\','.$img_size[0].','.$img_size[1].',false);"><img src="' . $str_url . '"  border=0';
	  }
	  else {
	    $str_out = $str_out . '<img src="' . $str_url . '" ';
	  }
	} else {
	  if ( $width != $img_size[0] || $height != $img_size[1] ) {
	    $str_out = $str_out . '<a href="javascript:openpopup(\'' . $str_url . '\','.$img_size[0].','.$img_size[1].',false);"><img src="' . $str_url . '" border=0';
	  } else {
	    $str_out = $str_out . '<img src="' . $str_url . '" ';
	  }
	}
	//	if ($width && $width != $img_size[0]){
	if ($width){
	  $str_out .= '" width='.$width;
	}
	//	if ($height && $height != $img_size[1]){
	if ($height){
	  $str_out .= '" height='.$height;
	}

	
	if ( isset( $attrib_float ) ) {
	  switch ( $attrib_float ) {
	    case 'left';
	    $str_out = $str_out . ' id="img_float_left"';
	    break;
	    case 'right';
	    $str_out = $str_out . ' id="img_float_right"';
	    break;
	  }
	}
	
	if ( isset( $attrib_valign ) ) {
	  switch ( $attrib_valign ) {
	    case 'top';
	    $str_out = $str_out . ' valign="top"';
	    break;
	    case 'bottom';
	    $str_out = $str_out . ' valign="bottom"';
	    break;
	    case 'middle';
	    $str_out = $str_out . ' valign="middle"';
	    break;
	  }
	}
	
	if ( isset( $attrib_alt ) ) {
	  $str_out = $str_out . ' alt="' . $attrib_alt . '"';
	}
	
	//Close the tag
	if ( isset( $attrib_popup ) ) {
	  if ( $attrib_popup == 'true' ) {
	    $str_out = $str_out . '></a>';
	  }
	  else {
	    $str_out = $str_out . '>';
	  }
	}
	else {
	  if ( $width != $img_size[0] || $height != $img_size[1] ) {
	    $str_out = $str_out . '></a>';
	  }
	  else {
	    $str_out = $str_out . '>';
	  }
	}
	if ( isset( $attrib_align ) ) {
	  if ( $attrib_align =='center' ) {
	    $str_out = $str_out . '</p>';
	  }
	}
	// Store sub_string after the tag.
	$str = substr( $str, $str_offset + strlen( $tag_end ) );
	// Search for next beginning tag.
	$str_offset = strpos( $str, $tag_begin );
      }
    }
  }
  
  // Append remainder of tag.
  $str = $str_out .  $str;
  
  return ( $str );
}

	function replace_php_tag( $str ) {
		// Replacements for HTML tags. Sub-function of blog_to_html.
		//
		// This function decodes HTML entities that are located between
		// HTML tags. Also, inserts <br />'s for new lines only if blocks
		// are outside the HTML tags.
		global $lang_string;

		$str_out = NULL;
		$tag_begin = '[php]';
		$tag_end = '[/php]';
		
		// Search for the openning HTML tag. Tag could be either upper or
		// lower case so we want to find the nearest one.
		//
		// Get initial $str_offset value.
		ob_start( 'replace_php_tag_callback' );
		$temp_lower = strpos( $str, strtolower( $tag_begin ) );
		$temp_upper = strpos( $str, strtoupper( $tag_begin ) );
		if ( $temp_lower === false ) {
			if ( $temp_upper === false ) {
				$str_offset = false;
			} else {
				$str_offset = $temp_upper;
			}
		} else {
			if ( $temp_upper === false ) {
				$str_offset = $temp_lower;
			} else {
				$str_offset = min( $temp_upper, $temp_lower );
			}
		}
		
		while ( $str_offset !== false ) {
			// Store all the text BEFORE the openning HTML tag.
			//
			// Also, replace hard returns with '<br />' tags.
			$temp_str = substr( $str, 0, $str_offset );
			//$temp_str = str_replace( chr(10), '<br />', $temp_str );
			$str_out = $str_out . $temp_str;
			echo $str_out;
			
			// Store all text AFTER the tag
			$str = substr( $str, $str_offset + strlen( $tag_begin ) );
		
			// Search for the closing HTML tag. Find the nearest one.
			$temp_lower = strpos( $str, strtolower( $tag_end ) );
			$temp_upper = strpos( $str, strtoupper( $tag_end ) );
			if ( $temp_lower === false ) {
				if ( $temp_upper === false ) {
					$str_offset = false;
				} else {
					$str_offset = $temp_upper;
				}
			} else {
				if ( $temp_upper === false ) {
					$str_offset = $temp_lower;
				} else {
					$str_offset = min( $temp_upper, $temp_lower );
				}
			}
			
			if ( $str_offset !== false ) {
				// Store all the text BETWEEN the HTML tags.
				//
				// Also, decode HTML entities between the tags.
				$temp_str = substr( $str, 0, $str_offset );
				//$str_out = $str_out . $temp_str;
				eval( html_entity_decode( $temp_str ) );

				// Store sub_string after the tag.
				$str = substr( $str, $str_offset + strlen( $tag_end ) );

				// Search for openning HTML tag again.
				$temp_lower = strpos( $str, strtolower( $tag_begin ) );
				$temp_upper = strpos( $str, strtoupper( $tag_begin ) );
				if ( $temp_lower === false ) {
					if ( $temp_upper === false ) {
						$str_offset = false;
					} else {
						$str_offset = $temp_upper;
					}
				} else {
					if ( $temp_upper === false ) {
						$str_offset = $temp_lower;
					} else {
						$str_offset = min( $temp_upper, $temp_lower );
					}
				}
			}
		}

		// Append remainder of text.
		//
		// All this text will be outside of any HTML tags so
		// we need to encode the line breaks.
		//$str = $str_out . $str;
		echo $str;
		$str=ob_get_contents();
		ob_end_clean();
		return ( $str );
	}

	function replace_php_tag_callback ( $out )
	{
		return( $out );
	}
	
	
	function sb_parse_url ( $text )
	{
	    // Con espacios 
	    $text = eregi_replace("([[:space:]])((f|ht)tps?:\/\/[a-z0-9~#%@\&:=?+\/\.,_-]+[a-z0-9~#%@\&=?+\/_.;-]+)", "\\1[url=\\2]\\2[/url]", $text); //http
	    $text = eregi_replace("([[:space:]])(www\.[a-z0-9~#%@\&:=?+\/\.,_-]+[a-z0-9~#%@\&=?+\/_.;-]+)", "\\1[url=http://\\2]\\2[/url]", $text); // www.
	    $text = eregi_replace("([[:space:]])([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,6})","\\1[url=mailto:\\2]\\2[/url]", $text); // mail
	    // Al principio de una cadena
	    $text = eregi_replace("^((f|ht)tps?:\/\/[a-z0-9~#%@\&:=?+\/\.,_-]+[a-z0-9~#%@\&=?+\/_.;-]+)", "[url=\\1]\\1[/url]", $text); //http
	    $text = eregi_replace("^(www\.[a-z0-9~#%@\&:=?+\/\.,_-]+[a-z0-9~#%@\&=?+\/_.;-]+)", "[url=http://\\1]\\1[/url]", $text); // www
	    $text = eregi_replace("^([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,6})","[url=mailto:\\1]\\1[/url]", $text); // mail
	    return ( $text );
	}

?>