##############################################################
## MOD Title:		Update phpBB3 SEO Dynamic Metatags 0.2.0RC1 => 0.2.0
## 
## MOD Author:		dcz <n/a> http://www.phpbb-seo.com/
##
## MOD Description:	This are the update steps for the phpBB3 SEO Dynamic Metatags 0.2.0RC1 => 0.2.0
##			Check http://www.phpbb-seo.com/boards/phpbb3-seo-toolkit/seo-dynamic-meta-tags-vt1308.html
## 			for the latest version or to get help with this MOD
##
## MOD Version: 	1.0
##
## Installation Level: 	Easy
## Installation Time: 	3 Minutes
## Files To Edit:	3
##       		includes/functions.php,
##			index.php,
##       		viewtopic.php,
##
## Included Files: 	n/a
##
## License: 		http://opensource.org/licenses/gpl-license.php GNU General Public License v2
##############################################################
## Author Notes:
## _____________
##
## This are the update steps for the phpBB3 SEO Dynamic Metatags 0.2.0RC1 => 0.2.0 update.
## 
##############################################################
## Before Adding This MOD To Your Forum, You Should Back Up All Files Related To This MOD
##############################################################

#
#-----[ OPEN ]------------------------------------------
#

includes/functions.php

#
#-----[ FIND ]------------------------------------------
#

	// www.phpBB-SEO.com SEO TOOLKIT BEGIN  - META
	seo_meta_tags();
	$seo_metas = build_meta();
	// www.phpBB-SEO.com SEO TOOLKIT END  - META

#
#-----[ REPLACE WITH ]------------------------------------------
#

	// www.phpBB-SEO.com SEO TOOLKIT BEGIN  - META
	global $seo_meta;
	$seo_meta->seo_meta_tags();
	// www.phpBB-SEO.com SEO TOOLKIT END  - META

#
#-----[ FIND ]------------------------------------------
#

		// www.phpBB-SEO.com - META
		'META_TAG' => $seo_metas,
 		// www.phpBB-SEO.com - META

#
#-----[ REPLACE WITH ]------------------------------------------
#

		// www.phpBB-SEO.com - META
		'META_TAG' => $seo_meta->build_meta($page_title),
 		// www.phpBB-SEO.com - META

#
#-----[ FIND ]------------------------------------------
#

// www.phpBB-SEO.com SEO TOOLKIT BEGIN  - META
$seo_meta = array();
/**
* Returns meta tag code
*/
function build_meta( $page_title = '') {
	global $seo_meta;
	$seo_meta['meta_desc'] = ( !empty($seo_meta['meta_desc']) ) ? $seo_meta['meta_desc'] : meta_filter_txt($page_title . ' : ' . $seo_meta['meta_desc_def']);
	$seo_meta['keywords'] = ( !empty($seo_meta['keywords']) ) ? $seo_meta['keywords'] : make_keywords( $page_title . ' ' . $seo_meta['meta_keywords_def']);
	$seo_meta['meta_title'] = ( !empty($seo_meta['meta_title']) ) ? meta_filter_txt($seo_meta['meta_title']) : $page_title;
	return sprintf( $seo_meta['meta_tpl'], $seo_meta['meta_title'], $seo_meta['meta_lang'], $seo_meta['meta_desc'], $seo_meta['keywords'], $seo_meta['meta_cat'], $seo_meta['meta_robots'], $seo_meta['meta_distrib'], $seo_meta['meta_restype'], $seo_meta['meta_copy'] );
}
/**
* Returns a word list separated by comas
* you can as well change the minimum word size here : if ( utf8_strlen($word) >= 3 ) {
* Possible Options :
* - $limit = 0 means no limit.
*   By default, ' . and , are deleted.
*/
function make_keywords($text, $limit = 15) {
	$keywords = '';
	$num = 0;
	$text = preg_replace(array('`&(amp;)?[a-z0-9]+;`i', "`[[:punct:]]+`", "`[\s]+`"), ' ', strip_tags($text) );
	$text = explode(' ', $text);
	// We take the most used words first
	$text = array_count_values($text);
	arsort($text);
	foreach ($text as $word => $count) {
		if ( utf8_strlen($word) > 2 ) {
			$keywords .= ($keywords == '') ? $word : ', ' . $word;
			$num++;
			if ( $limit > 0 && $num >= $limit ) {
				break;
			}
		}	
	}
	return $keywords;
}
/**
* Filter php/html tags and white spaces and returns htmlspecialchared string
*/
function meta_filter_txt($text, $bbcode = true) {
	if ($bbcode) {
		return htmlspecialchars(preg_replace(array('`\[/?[a-z*]+([^\[\]]*)?\]`mi', '`[\s]+`'), ' ', strip_tags($text) ) );
	}
	return htmlspecialchars(preg_replace("`[\s]+`", " ", strip_tags($text) ) );
}
/**
* Cut the text according to the number of words.
* Borrowed from www.php.net http://www.php.net/preg_replace
*/
function word_limit($string, $length = 25, $ellipsis = ' ...') {
	return count($words = preg_split('/\s+/', ltrim($string), $length + 1)) > $length ? rtrim(utf8_substr($string, 0, utf8_strlen($string) - utf8_strlen(end($words)))) . $ellipsis : $string;
}
/**
* Initialize meta tags
*/
function seo_meta_tags() {
	global $config, $seo_meta;
	$seo_meta['meta_tpl'] =  '<meta name="title" content="%s"/>' . "\n" . '<meta name="description" lang="%s" content="%s"/>' . "\n" . '<meta name="keywords"    content="%s"/>' . "\n" . '<meta name="category"    content="%s"/>' . "\n" . '<meta name="robots"      content="%s"/>'. "\n" . '<meta name="distribution" content="%s"/>' . "\n" . '<meta name="resource-type" content="%s"/>' . "\n" . '<meta name="copyright" content="%s"/>' . "\n";
	// Here you can hard code a static default title, description and keywords
	// As is, the mod will return information based on the phpbb config
	$seo_meta['meta_title_def'] = $config['sitename'];
	$seo_meta['meta_desc_def'] = $config['site_desc'];
	$seo_meta['meta_keywords_def'] =  $config['site_desc'];
	$seo_meta['meta_lang'] =  $config['default_lang'];
	$seo_meta['meta_cat'] =  'general';
	$seo_meta['meta_robots'] =  'index,follow';
	$seo_meta['meta_distrib'] =  'global';
	$seo_meta['meta_restype'] =  'document';
	$seo_meta['meta_copy'] =  $config['sitename'];
	return;
}
// www.phpBB-SEO.com SEO TOOLKIT END - META

#
#-----[ REPLACE WITH ]------------------------------------------
#

// www.phpBB-SEO.com SEO TOOLKIT BEGIN  - META
class seo_meta {
	var $meta = array();
	/**
	* Some config :
	*	=> keywordlimit : number of keywords (max) in the keyword tag,
	*	=> wordlimit : number of words (max) in the desc tag,
	*	=> wordminlen : only words with more than wordminlen letters will be used, default is 2,
	*	=> bbcodestrip : | separated list of bbcode to fully delete, tag + content, default is 'img|url|flash',
	*	=> ellipsis : ellipsis to use if clipping,
	*	=> topic_sql : Do a SQL to build topic meta keywords or just use the meta desc tag,
	*	=> kewd_adddesc : If you query for topic keywords, you can still add the meta desc tag in the keyword list.
	**/
	var $mconfig = array('keywordlimit' => 15, 'wordlimit' => 25, 'wordminlen' => 2, 'bbcodestrip' => 'img|url|flash', 'ellipsis' => ' ...', 'topic_sql' => true, 'kewd_adddesc' => false);
	/**
	* Returns meta tag code
	*/
	function build_meta( $page_title = '') {
		$this->meta['meta_desc'] = ( !empty($this->meta['meta_desc']) ) ? $this->meta['meta_desc'] : $this->meta_filter_txt($page_title . ' : ' . $this->meta['meta_desc_def']);
		$this->meta['keywords'] = ( !empty($this->meta['keywords']) ) ? $this->meta['keywords'] : $this->make_keywords( $page_title . ' ' . $this->meta['meta_keywords_def']);
		$this->meta['meta_title'] = ( !empty($this->meta['meta_title']) ) ? meta_filter_txt($this->meta['meta_title']) : $page_title;
		return sprintf( $this->meta['meta_tpl'], $this->meta['meta_title'], $this->meta['meta_lang'], $this->meta['meta_desc'], $this->meta['keywords'], $this->meta['meta_cat'], $this->meta['meta_robots'], $this->meta['meta_distrib'], $this->meta['meta_restype'], $this->meta['meta_copy'] );
	}
	/**
	* Returns a word list separated by comas
	*   By default, "'" "." and "," are deleted.
	*/
	function make_keywords($text) {
		$keywords = '';
		$num = 0;
		$text = preg_replace(array('`&(amp;)?[a-z0-9]+;`i', '`[[:punct:]]+`', '`[\s]+`'), ' ', strip_tags($text) );
		$text = explode(' ', $text);
		// We take the most used words first
		$text = array_count_values($text);
		arsort($text);
		foreach ($text as $word => $count) {
			if ( utf8_strlen($word) > $this->mconfig['wordminlen'] ) {
				$keywords .= empty($keywords) ? $word : ', ' . $word;
				$num++;
				if ( $num >= $this->mconfig['keywordlimit'] ) {
					unset($text);
					break;
				}
			}	
		}
		return $keywords;
	}
	/**
	* Filter php/html tags and white spaces and returns htmlspecialchared string with limit in words
	*/
	function meta_filter_txt($text, $bbcode = true) {
		if ($bbcode) {
			static $RegEx = array();
			static $replace = array(' ', ' ', '', ' ');
			if (empty($RegEx)) {
				$RegEx = array('`<[^>]*>(.*<[^>]*>)?`Usi', // HTML code
					'`\[(' . $this->mconfig['bbcodestrip'] . ')[^\[\]]+\].*\[/\1[^\[\]]+\]`Usi', // bbcode to strip
					'`\[/?[^\[\]]+\]`i', // Strip all bbcode tags
					'`[\s]+`' // Multiple spaces
				);
			}
			return $this->word_limit( utf8_htmlspecialchars( htmlspecialchars_decode( preg_replace($RegEx, $replace, $text ) ) ) );
		}
		return $this->word_limit( utf8_htmlspecialchars( htmlspecialchars_decode( preg_replace('`<[^>]*>(.*<[^>]*>)?`Usi', '`[\s]+`', array(' ', ''), $text ) ) ) );
	}
	/**
	* Cut the text according to the number of words.
	* Borrowed from www.php.net http://www.php.net/preg_replace
	*/
	function word_limit($string) {
		return count($words = preg_split('/\s+/', ltrim($string), $this->mconfig['wordlimit'] + 1)) > $this->mconfig['wordlimit'] ? rtrim(utf8_substr($string, 0, utf8_strlen($string) - utf8_strlen(end($words)))) . $this->mconfig['ellipsis'] : $string;
	}
	/**
	* Initialize meta tags
	*/
	function seo_meta_tags() {
		global $config;
		$this->meta['meta_tpl'] =  '<meta name="title" content="%s" />' . "\n" . '<meta name="description" lang="%s" content="%s" />' . "\n" . '<meta name="keywords"    content="%s" />' . "\n" . '<meta name="category"    content="%s" />' . "\n" . '<meta name="robots"      content="%s" />'. "\n" . '<meta name="distribution" content="%s" />' . "\n" . '<meta name="resource-type" content="%s" />' . "\n" . '<meta name="copyright" content="%s" />';
		// Here you can hard code a static default title, description and keywords
		// As is, the mod will return information based on the phpbb config
		$this->meta['meta_title_def'] = $config['sitename'];
		$this->meta['meta_desc_def'] = $config['site_desc'];
		$this->meta['meta_keywords_def'] = $config['site_desc'];
		$this->meta['meta_lang'] =  $config['default_lang'];
		$this->meta['meta_cat'] =  'general';
		$this->meta['meta_robots'] =  'index,follow';
		$this->meta['meta_distrib'] =  'global';
		$this->meta['meta_restype'] =  'document';
		$this->meta['meta_copy'] =  $config['sitename'];
		return;
	}
}
$seo_meta = new seo_meta();
// www.phpBB-SEO.com SEO TOOLKIT END - META

#
#-----[ OPEN ]------------------------------------------
#

index.php

#
#-----[ FIND ]------------------------------------------
#

// www.phpBB-SEO.com SEO TOOLKIT BEGIN - META
$seo_meta['meta_desc'] = meta_filter_txt($config['sitename'] . ' : ' .  $config['site_desc']);
$seo_meta['keywords'] = make_keywords($seo_meta['meta_desc']);
// www.phpBB-SEO.com SEO TOOLKIT END - META

#
#-----[ REPLACE WITH ]------------------------------------------
#

// www.phpBB-SEO.com SEO TOOLKIT BEGIN - META
$seo_meta->meta['meta_desc'] = $seo_meta->meta_filter_txt($config['sitename'] . ' : ' .  $config['site_desc']);
$seo_meta->meta['keywords'] = $seo_meta->make_keywords($seo_meta->meta['meta_desc']);
// www.phpBB-SEO.com SEO TOOLKIT END - META

#
#-----[ OPEN ]------------------------------------------
#

viewforum.php

#
#-----[ FIND ]------------------------------------------
#

// www.phpBB-SEO.com SEO TOOLKIT BEGIN - META
$seo_meta['meta_desc'] = meta_filter_txt($forum_data['forum_name'] . $extra_title .  ' : ' . (!empty($forum_data['forum_desc']) ? $forum_data['forum_desc'] : $config['site_desc']));
$seo_meta['keywords'] = make_keywords($seo_meta['meta_desc']);
// www.phpBB-SEO.com SEO TOOLKIT END - META

#
#-----[ REPLACE WITH ]------------------------------------------
#

// www.phpBB-SEO.com SEO TOOLKIT BEGIN - META
$seo_meta->meta['meta_desc'] = $seo_meta->meta_filter_txt($forum_data['forum_name'] . ' : ' . (!empty($forum_data['forum_desc']) ? $forum_data['forum_desc'] : $config['site_desc']));
$seo_meta->meta['keywords'] = $seo_meta->make_keywords($seo_meta->meta['meta_desc']);
// www.phpBB-SEO.com SEO TOOLKIT END - META

#
#-----[ OPEN ]------------------------------------------
#

viewtopic.php

#
#-----[ FIND ]------------------------------------------
#

	// www.phpBB-SEO.com SEO TOOLKIT BEGIN  - META
	if ($i == 0) {
		$m_kewrd = '';
		$seo_meta['meta_desc'] = meta_filter_txt(word_limit($message));
		$sql = "SELECT w.word_text
			FROM " . SEARCH_WORDMATCH_TABLE . " m, " . SEARCH_WORDLIST_TABLE . " w
			WHERE m.post_id = {$row['post_id']}
				AND w.word_id = m.word_id
				ORDER BY w.word_count DESC LIMIT 20";
		if( ($result = $db->sql_query($sql)) ) {
			while ( $meta_row = $db->sql_fetchrow($result) ) {
				$m_kewrd .= " " . $meta_row['word_text'];
			}
		}
		$db->sql_freeresult($result);
		$seo_meta['keywords'] = !empty($m_kewrd) ? make_keywords($m_kewrd) : make_keywords($seo_meta['meta_desc']);
	}
	// www.phpBB-SEO.com SEO TOOLKIT END  - META


#
#-----[ REPLACE WITH ]------------------------------------------
#

	// www.phpBB-SEO.com SEO TOOLKIT BEGIN  - META
	if ($i == 0) {
		$m_kewrd = censor_text($row['post_subject']) . ' ' . $topic_data['forum_name'] . ' ';
$m_kewrd = '';
		$seo_meta->meta['meta_desc'] = $seo_meta->meta_filter_txt( $message );
		if($seo_meta->mconfig['topic_sql']) {
			$sql = "SELECT w.word_text
				FROM " . SEARCH_WORDMATCH_TABLE . " m, " . SEARCH_WORDLIST_TABLE . " w
				WHERE m.post_id = {$row['post_id']}
					AND w.word_id = m.word_id
					AND w.word_common = 0
					ORDER BY w.word_count DESC LIMIT 20";
			if( ($result = $db->sql_query($sql)) ) {
				while ( $meta_row = $db->sql_fetchrow($result) ) {
					$m_kewrd .= ' ' . $meta_row['word_text'];
				}
			}
			$db->sql_freeresult($result);
			$seo_meta->meta['keywords'] = $seo_meta->make_keywords( $m_kewrd  . ($seo_meta->mconfig['kewd_adddesc'] ? $seo_meta->meta['meta_desc'] : ''));
		} else {
			$seo_meta->meta['keywords'] = $seo_meta->make_keywords( $m_kewrd . $seo_meta->meta['meta_desc'] );
		}
	}
	// www.phpBB-SEO.com SEO TOOLKIT END  - META

#
#---[ SAVE/CLOSE ALL FILES ]-----------------------
#
# EoM
