<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?xml-stylesheet type="text/xsl" href="./modx/1.2.5.xsl" ?>
<!--NOTICE: Please open this file in your web browser. If presented with a security warning, you may safely tell it to allow the blocked content.-->
<!--For security purposes, please check: http://www.phpbb.com/mods/ for the latest version of this MOD.\nAlthough MODs are checked before being allowed in the MODs Database there is no guarantee that there are no security problems within the MOD.\nNo support will be given for MODs not found within the MODs Database which can be found at http://www.phpbb.com/mods/-->
<mod xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.phpbb.com/mods/xml/modx-1.2.5.xsd">
	<header>
		<meta name="generator" content="MODX file generated with PP MODX Creator by tumba25 (online version)"/>
		<license><![CDATA[http://opensource.org/licenses/gpl-license.php GNU General Public License v2]]></license>
		<title lang="en"><![CDATA[Autolink MOD]]></title>
		<description lang="en"><![CDATA[You can easily set words that the MOD will replace with a link in the posts. You can set one or more URLs for each word, and set set the rate of the usage. You can limit the number of replacement.]]></description>
		<author-group>
			<author>
				<realname><![CDATA[Máté Bartus]]></realname>
				<username><![CDATA[CHItA]]></username>
				<homepage><![CDATA[http://bartusmate.hu]]></homepage>
				<email><![CDATA[bartus.mate@root.hu]]></email>
			</author>
		</author-group>
		<mod-version>1.0.3</mod-version>
		<installation>
			<level>easy</level>
			<time>300</time>
			<target-version>3.0.7-PL1</target-version>
		</installation>
		<history>
			<entry>
				<date>2010-03-24</date>
				<rev-version>1.0.2</rev-version>
				<changelog lang="en">
					<change><![CDATA[[Add] SQL query caching]]></change>
					<change><![CDATA[[Fix] ACP form validating]]></change>
					<change><![CDATA[[Fix] The multiple link generation]]></change>
				</changelog>
			</entry>
			<entry>
				<date>2010-07-07</date>
				<rev-version>1.0.3</rev-version>
				<changelog lang="en">
					<change><![CDATA[[Fix] Word replace in HTML tags]]></change>
					<change><![CDATA[[Add] German translation]]></change>
				</changelog>
			</entry>
		</history>
		<link-group>
			<link type="language" href="hu.xml" lang="hu">Magyar</link>
			<link type="language" href="de.xml" lang="de">Deutsch</link>
			<link type="contrib" href="update_1.0.x-1.0.3.xml" lang="en">Update 1.0.x to 1.0.3</link>
		</link-group>
	</header>
	<action-group>
		<copy>
			<file from="root/adm/style/acp_autolink.html" to="adm/style/acp_autolink.html"/>
			<file from="root/includes/acp/acp_autolink.php" to="includes/acp/acp_autolink.php"/>
			<file from="root/includes/acp/info/acp_autolink.php" to="includes/acp/info/acp_autolink.php"/>
			<file from="root/language/en/mods/info_acp_autolink.php" to="language/en/mods/info_acp_autolink.php"/>
			<file from="root/language/es/mods/info_acp_autolink.php" to="language/es/mods/info_acp_autolink.php"/>
			<file from="root/umil/*.*" to="umil/*.*"/>
			<file from="root/autolink_install.php" to="autolink_install.php"/>
		</copy>
		<open src="viewtopic.php">
			<edit>
				<find><![CDATA[// Output the posts]]></find>
				<action type="before-add"><![CDATA[// Autolink MOD [start]
// Get the words which we want to replace with a link
$autolink_sql    = 'SELECT a.*
					FROM      ' . AUTOLINK_MOD_WORDS_TABLE . ' a
					LEFT JOIN ' . LANG_TABLE . ' l
					ON a.autolink_word_lang = l.lang_id
					WHERE l.lang_iso = "' . $db->sql_escape($user->data['user_lang']) . '"
					OR a.autolink_word_lang = 0';
$autolink_result = $db->sql_query($autolink_sql, 86400);
$autolinks = array();
$i = 0;
while ($autolink_row = $db->sql_fetchrow($autolink_result))
{
	$autolinks[$i]['word']			= $autolink_row['autolink_word'];
	$autolinks[$i]['url']			= $autolink_row['autolink_url'];
	$autolinks[$i]['rate']			= $autolink_row['autolink_rate'];
	$autolinks[$i]['times_used']	= 0;
	$i++;
}
$db->sql_freeresult($autolink_result);
// Autolink MOD [end]]]></action>
			</edit>
			<edit>
				<find><![CDATA[	$message = censor_text($row['post_text']);]]></find>
				<action type="after-add"><![CDATA[	// Autolink MOD [start]
	foreach ($autolinks as $key => $value)
	{		
		$autolink_word	= $value['word'];
		$autolink_url	= $value['url'];
		
		if (preg_match_all("#(?:[\s]|\]|^)" . $autolink_word . "(?:[\s]|\[|$)#uis", $message, $autolink_matches))
		{			
			if (strpos($autolink_url, '|') !== false)
			{
				$autolink_url_parts = explode('|', $autolink_url);
				$autolink_rates = explode(':', $value['rate']);
				
				// Check the two array
				if (sizeof($autolink_url_parts) != sizeof($autolink_rates))
				{
					trigger_error("AUTOLINK_DIFFERENT_SIZE_ARRAY");
				}
				
				$maximum = array_sum($autolink_rates);				
				$num = rand(1, $maximum);
				$temp = 0;
				
				$total = sizeof($autolink_rates);
				for ($autolink_i = 0; $autolink_i < $total; $autolink_i++)
				{
					if ($num <= ($autolink_rates[$autolink_i] + $temp))
					{
						$autolink_url = $autolink_url_parts[$autolink_i];
						break;
					}
					
					$temp += $autolink_rates[$autolink_i];
				}
			}
			
			$num_of_replace = (sizeof($autolink_matches) < $config['autolink_replace_times_in_a_post']) ? sizeof($autolink_matches) : $config['autolink_replace_times_in_a_post'];
			$num_of_replace = (($num_of_replace + $value['times_used']) < $config['autolink_max_num_of_a_word']) ? $num_of_replace : $config['autolink_max_num_of_a_word'] - $value['times_used'];
			//Now replace the word to a link
			$message = preg_replace("#([\s]|\]|^)(" . $autolink_word . ")([\s]|\[|$)#uis", "$1<a href=\"" . $autolink_url . "\" class=\"autolink\">$2</a>$3", $message, $num_of_replace, $amount);
			$autolinks[$key]['times_used'] += $amount;
			
			// Delete the words that we used enough...
			if ($autolinks[$key]['times_used'] >= $config['autolink_max_num_of_a_word'])
			{
				unset($autolinks[$key]);
				
				if (sizeof($autolinks) == 0)
				{
					$autolinks = array();
					break;
				}
			}
		}
	}
	
	$message = preg_replace("#(\[url.*?\])<a href=\".*?\" class=\"autolink\">(.*?)</a>(\[/url.*?\])#", "$1$2$3", $message);
	// Autolink MOD [end]]]></action>
			</edit>
		</open>
		<open src="includes/constants.php">
			<edit>
				<find><![CDATA[// Table names]]></find>
				<action type="after-add"><![CDATA[// Autolink MOD tables [start]
define('AUTOLINK_MOD_WORDS_TABLE',	$table_prefix . 'autolink_words');
// Autolink MOD tables [end]]]></action>
			</edit>
		</open>
		<php-installer><![CDATA[autolink_install.php]]></php-installer>
		<diy-instructions lang="en"><![CDATA[Once you finish the installer, you may delete autolink_install.php.]]></diy-instructions>
	</action-group>
</mod>
