##############################################################
## MOD Title:		Update Meta Tags Dynamiques phpBB SEO 0.2.8 => 0.4.0
## 
## MOD Author: dcz <n/a> http://www.phpbb-seo.com/
##
## MOD Description: 	Ceci est la procédure de mise à jour du MOD Meta Tags Dynamiques phpBB SEO pour la transition version 0.2.8 => 0.4.0.
## 			Rendez-vous sur http://www.phpbb-seo.com/fr/toolkit-phpbb-seo/meta-tags-dynamiques-seo-t1678.html
## 			pour toujours obtenir la dernière version ou de l'aide pour ce MOD.
##
## MOD Version: 	1.0
##
## Installation Level: 	Easy
## Installation Time: 	3 Minutes
## Files To Edit:	2
##			common.php,
##			index.php,
##       	includes/functions.php,
##			viewforum.php,
##       	viewtopic.php.
##
## Included Files: 	n/a
##
## License: 		http://opensource.org/licenses/gpl-license.php GNU General Public License v2
##############################################################
## Author Notes:
## _____________
##
## Voici les instructions de mise à jour : Meta Tags Dynamiques phpBB SEO 0.2.8 => 0.4.0
##
##############################################################
## Before Adding This MOD To Your Forum, You Should Back Up All Files Related To This MOD
##############################################################

#
#-----[ COPY ]--------------------------------------------------
#

copy root/*.* to phpBB/*.*

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

common.php

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

// Grab global variables, re-cache if necessary
$config = $cache->obtain_config();

#
#-----[ AFTER, ADD ]------------------------------------------
#

// www.phpBB-SEO.com SEO TOOLKIT BEGIN - META
require_once($phpbb_root_path . 'phpbb_seo/phpbb_seo_meta.'.$phpEx);
$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['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

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

// www.phpBB-SEO.com SEO TOOLKIT BEGIN - META
$seo_meta->collect('description', $config['sitename'] . ' : ' .  $config['site_desc']);
$seo_meta->collect('keywords', $config['sitename'] . ' ' . $seo_meta->meta['description']);
// www.phpBB-SEO.com SEO TOOLKIT END - META

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

includes/functions.php

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

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

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

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

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

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

#
#-----[ REPLACE WITH ]------------------------------------------
# Nothing, eg delete

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

// 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,
	*	=> check_ignore : Check the search_ignore_words.php list.
	*		Please note : 
	*			This will require some more work for the server.
	*			And this is mostly useless if you have re-enabled the search_ignore_words.php list
	*			filtering in includes/search/fulltest_native.php (and of course use fulltest_native index).
	*	=> bypass_common : Bypass common words in viewtopic.php.
	*		Set to true by default because the most interesting keywords are as well among the most common.
	*		This of course provides with even better results when fulltest_native is used 
	*		and search_ignore_words.php list was re-enabled.
	*	=> disallowed : Disallow tag based on GET var used : varname => 1|0, 1 will through a disallow meta tag.
	*	=> noindex_files : Disallow tag based on the physical script file name
	* Some default values are set bellow in the seo_meta_tags() method
	**/
	var $mconfig = array('keywordlimit' => 15, 'wordlimit' => 25, 'wordminlen' => 2, 'bbcodestrip' => 'img|url|flash|code', 'ellipsis' => ' ...', 'topic_sql' => true, 'check_ignore' => false, 'bypass_common' => true,
		// Consider adding ", 'p' => 1" if your forum is no indexed yet or if no post urls are to be redirected
		// to add a noindex tag on post urls
		'disallowed' => array('style' => 1, 'hilit' => 1, 'print' => 1, 'sid' => 1),
		// noindex based on physical script file name
		'noindex_files' => array("ucp" => 1),
	);
	/**
	* Returns meta tag code
	*/
	function build_meta( $page_title = '') {
		global $phpEx, $user;
		$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']) ) ? $this->meta_filter_txt($this->meta['meta_title']) : $page_title;
		// Do we allow indexing based on get variable
		$this->meta['meta_robots'] = $this->meta['meta_robots_def'];
		foreach ( $this->mconfig['disallowed'] as $get => $disallow) {
			if ($disallow && isset($_GET[$get])) {
				$this->meta['meta_robots'] = 'noindex,follow';
				break;
			}
		}
		// Do we allow indexing based on physical script file name
		if (@isset($this->mconfig['noindex_files'][str_replace(".$phpEx", '', $user->page['page_name'])])) {
			$this->meta['meta_robots'] = 'noindex,follow';
		}
		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 coma separated keyword list
	*/
	function make_keywords($text) {
		global $phpbb_root_path, $phpEx;
		static $stop_words = array();
		$keywords = '';
		$num = 0;
		$text = utf8_strtolower(preg_replace(array('`&(amp;)?[^:]+;`i', '`[[:punct:]]+`', '`[0-9]+`',  '`[\s]+`'), ' ', strip_tags($text)));
		$text = explode(' ', trim($text), 50);
		if ($this->mconfig['check_ignore']) {
			if (empty($stop_words)) {
				global $user, $phpEx;
				$words = array();
				if (file_exists("{$user->lang_path}/search_ignore_words.$phpEx")){
					// include the file containing ignore words
					include("{$user->lang_path}/search_ignore_words.$phpEx");
				}
				$stop_words = & $words;
			}
			$text = array_diff($text, $stop_words);
		}
		// 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 .= ', ' . $word;
				$num++;
				if ( $num >= $this->mconfig['keywordlimit'] ) {	
					break;
				}
			}	
		}
		return trim($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
					'`\[/?[^\[\]]+\]`mi', // Strip all bbcode tags
					'`[\s]+`' // Multiple spaces
				);
			}
			return $this->word_limit(htmlspecialchars(preg_replace($RegEx, $replace, $text), ENT_COMPAT, 'UTF-8'));
		}
		return $this->word_limit(htmlspecialchars(preg_replace(array('`<[^>]*>(.*<[^>]*>)?`Usi', '`[\s]+`'), ' ', $text), ENT_COMPAT, 'UTF-8'));
	}
	/**
	* 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, $phpbb_seo;
		$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" />' . "\n";
		// If url Rewriting is on, we shall be more strict on noindex (since we can :p)
		if (!empty($phpbb_seo->seo_opt['url_rewrite'])) {
			$this->mconfig['disallowed'] = array_merge($this->mconfig['disallowed'], array('st' => 1, 'sk' => 1, 'sd' => 1, 'ch' => 1,));
		}
		// 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_def'] =  '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

#
#-----[ REPLACE WITH ]------------------------------------------
# Nothing, eg delete

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

viewforum.php

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

$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']);

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

$seo_meta->collect('description', $forum_data['forum_name'] . ' : ' . (!empty($forum_data['forum_desc']) ? $forum_data['forum_desc'] : $seo_meta->meta_def['description']));
$seo_meta->collect('keywords', $forum_data['forum_name'] . ' ' . $seo_meta->meta['description']);

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

viewtopic.php

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

	// www.phpBB-SEO.com SEO TOOLKIT BEGIN  - META
	if ($i == 0) {
		$m_kewrd = '';
		$seo_meta->meta['meta_desc'] = $seo_meta->meta_filter_txt($message);
		if ($seo_meta->mconfig['topic_sql']) {
			$common_sql = $seo_meta->mconfig['bypass_common'] ? '' : 'AND w.word_common = 0';
			$sql = "SELECT w.word_text
				FROM " . SEARCH_WORDMATCH_TABLE . " m, " . SEARCH_WORDLIST_TABLE . " w
				WHERE m.post_id = " . (int) $row['post_id'] . "
					AND w.word_id = m.word_id
					$common_sql
				ORDER BY w.word_count DESC";
			$result = $db->sql_query_limit($sql, 15);
			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($row['post_subject'] . (!empty($m_kewrd) ? $m_kewrd : $seo_meta->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 = '';
		$seo_meta->collect('description', $message);
		if ($seo_meta->mconfig['topic_sql']) {
			$common_sql = $seo_meta->mconfig['bypass_common'] ? '' : 'AND w.word_common = 0';
			// collect keywords from all post in page
			$post_id_sql = $db->sql_in_set('m.post_id', $post_list, false, true);
			$sql = "SELECT w.word_text
				FROM " . SEARCH_WORDMATCH_TABLE . " m, " . SEARCH_WORDLIST_TABLE . " w
				WHERE $post_id_sql
					AND w.word_id = m.word_id
					$common_sql
				ORDER BY w.word_count DESC";
			$result = $db->sql_query_limit($sql, min(25, (int) $seo_meta->mconfig['keywordlimit']));
			while ( $meta_row = $db->sql_fetchrow($result) ) {
				$m_kewrd .= ' ' . $meta_row['word_text'];
			}
			$db->sql_freeresult($result);
		}
		$seo_meta->collect('keywords', $topic_data['topic_title'] . ' ' . $row['post_subject'] . ' ' . (!empty($m_kewrd) ? $m_kewrd : $seo_meta->meta['description']));
	}
	// www.phpBB-SEO.com SEO TOOLKIT END  - META

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