<?php
/**
 * @package Plugin EB Auto SEO Internal Links For Joomla
 * @version 1.18: ebautoseointernallinks.php Oct 2025
 * @author url: https://www/extnbakers.com
 * @copyright Copyright (C) 2021 extnbakers.com. All rights reserved.
 * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html 
**/
// No direct access
defined( '_JEXEC' ) or die('Restricted access');
class plgContentEbautoseointernallinks extends JPlugin {

	public $params;
	
	public function onContentPrepare($context, &$article, &$params, $page = 0)
	{	
		if ($context == 'com_finder.indexer')
		{
			return true;
		}
		$auto_keyword =  $this->params->get('autoletter', 'Visit the page');
		$display_article =  $this->params->get('display_article', 1);
		$display_category =  $this->params->get('display_category', 1);
		$display_module =  $this->params->get('display_module', 1);
		$display_field =  $this->params->get('display_field', 1);
		$display_tag =  $this->params->get('display_tag', 1);	
		$self_linking = $this->params->get('self_linking',1);

		$case_sensitive_keywords = $this->params->get('case_sensitive_keywords',0);
		
		$define_limit = $this->params->get('define_limit',0);
		$limit_count = $this->params->get('limit_count',2);				
		$eb_exclude_tag = $this->params->get('exclude_tag');		

		if($self_linking == 1){$contf=true;} else {$contf=false;}

		$new_content = $article->text;
		foreach ($auto_keyword as $key => $value) {
			$keyword_text =  $value->keyword;
			$keylink = ($value->key_link != '')? $value->key_link : "javascript:void(0);";
			$keylink = trim($keylink);
			
			$org_keylink = $keylink;
			$org_keyword_text = $keyword_text;
		
		
		if($keyword_text != ''){	

			$uri = JUri::getInstance(); 
			$phpself_link = $uri->toString();
		
			if(($self_linking == 0 && $keylink != $phpself_link) || ($self_linking == 1)){

				$nofollow_link = isset($value->nofollow_link)?$value->nofollow_link:0;
				$follow_link_enable = ($nofollow_link == 1)? 'rel="nofollow"':"";

				$regText= '(\b'.preg_quote($keyword_text,'/').'\b)';
				$target = '';
				if($value->link_target == 'new_window' && $value->key_link != ''){
					$target = 'target="_blank"';
				}
				
				$text_content = '<a '.$target.' href="'.$keylink.'" '.$follow_link_enable.' title="'.$keyword_text.'">'.$keyword_text.'</a>';
				
				if($display_article == 1){				
					$new_content = plgContentEbautoseointernallinks::EBReplaceContent($new_content,$keyword_text,$text_content,$define_limit,$limit_count,$org_keylink,$eb_exclude_tag,$case_sensitive_keywords);
				}
				if ($display_category == 1){
					if(isset($article->description)){
						$new_content = plgContentEbautoseointernallinks::EBReplaceContent($new_content,$keyword_text,$text_content,$define_limit,$limit_count,$org_keylink,$eb_exclude_tag,$case_sensitive_keywords);					
					}
				}	
				if($display_module == 1){
					if($context == 'mod_custom.content'){
						$new_content = plgContentEbautoseointernallinks::EBReplaceContent($new_content,$keyword_text,$text_content,$define_limit,$limit_count,$org_keylink,$eb_exclude_tag,$case_sensitive_keywords);
					}
				}
				if($display_field == 1){
					if($context == 'text'){
						$new_content = plgContentEbautoseointernallinks::EBReplaceContent($new_content,$keyword_text,$text_content,$define_limit,$limit_count,$org_keylink,$eb_exclude_tag,$case_sensitive_keywords);
					}
				}
				if($display_tag == 1){
					if($context == 'com_tags.tag'){
						$new_content = plgContentEbautoseointernallinks::EBReplaceContent($new_content,$keyword_text,$text_content,$define_limit,$limit_count,$org_keylink,$eb_exclude_tag,$case_sensitive_keywords);
					}
				}
				if($display_tag == 1){
					if($context == 'com_content.article'){
						$articletags = $article->tags->itemTags;
						foreach ($articletags as $key => $tagsvalue) {
							$art_title =  $tagsvalue->title;
							if(strtolower($art_title) == strtolower($keyword_text)){
								echo "<script>
									var text = '".$art_title."';
									text = text.toLowerCase();
									jQuery(window).bind('load', function() {								
										jQuery('.tags a').each(function(i){								
											var contenttags = jQuery(this).text().trim();
											contenttags = contenttags.toLowerCase();
											if(text == contenttags){								
												jQuery(this).attr('href','".$keylink."');
											}
										});
									});
									</script>";
							}
						}
					}
				}
			}
		} 
		
		}
		$article->text = $new_content;
	} 
	static function maskContent($txt){
		$txt = str_replace("\'","'",$txt);
		$result = base64_encode($txt);
		return $result;
	}
	
	static function unmaskContent($txt){
		$result = base64_decode($txt);
		return $result;
	}
	static function EBReplaceContent($content,$keyword,$link,$define_limit,$limit_count,$org_keylink,$eb_exclude_tag,$case_sensitive_keywords)
	{				
		$languagevar_wt = JFactory::getLanguage()->getTag();
				
		if($languagevar_wt != 'fr-FR'){
			
				
				if($define_limit == 0){	
				
					$keyword= trim($keyword);
					
					if($case_sensitive_keywords == 0){
						$search = "~([\s\.\,\;\!\?\:\>\(\)\'\"\*\/«])(".$keyword.")([\*\/\'\"\(\)\<\s\.\,\;\!\?\:»])~siu";
					}else{
						$search = "/\b" .$keyword."\b/";
					}
					
					if(!empty($eb_exclude_tag)){ 
						foreach ($eb_exclude_tag as $key => $value) {
							$content = preg_replace_callback("~(<".$value."[^>]*>)(.*?)(<\/".$value.">)~si",function($matches) {return "<:ZyX>".plgContentEbautoseointernallinks::maskContent($matches[1].$matches[2].$matches[3])."<:ZyX/>";},$content);
						}
					}
					
					$content = preg_replace_callback("~(<script)(.*?)(<\/script>)~si", function($matches) {return "<:ZyX>".plgContentEbautoseointernallinks::maskContent($matches[1].$matches[2].$matches[3])."<:ZyX/>";} ,$content);				
					$content = preg_replace_callback("~(<\!\-\-)(.*?)(\-\-\>)~si", function($matches) {return "<:ZyX>".plgContentEbautoseointernallinks::maskContent($matches[1].$matches[2].$matches[3])."<:ZyX/>";},$content);
					$content = preg_replace_callback("~(<style)(.*?)(<\/style>)~si", function($matches) {return "<:ZyX>".plgContentEbautoseointernallinks::maskContent($matches[1].$matches[2].$matches[3])."<:ZyX/>";},$content);
					
									
					
					$content = preg_replace_callback("~(<a)(.*?)(?=<\/a>)(<\/a>)~si", function($matches) {return "<:ZyX>".plgContentEbautoseointernallinks::maskContent($matches[1].$matches[2].$matches[3])."<:ZyX/>";},$content);
					$content = preg_replace_callback("~(<[a-z])(.*?)(>)~si", function($matches) {return "<:ZyX>".plgContentEbautoseointernallinks::maskContent($matches[1].$matches[2].$matches[3])."<:ZyX/>";},$content);
					
					$link = '$1'.$link.'$3';
					$content = preg_replace($search, $link, $content,-1);
					$content = preg_replace_callback("~<\:ZyX>(.*?)(?=<\:ZyX\/>)<\:ZyX\/>~si", function($matches) {return plgContentEbautoseointernallinks::unmaskContent($matches[1]);}, $content);
					$content = preg_replace_callback("~<\:ZyX>(.*?)(?=<\:ZyX\/>)<\:ZyX\/>~si", function($matches) {return plgContentEbautoseointernallinks::unmaskContent($matches[1]);}, $content);		


					## if character added in ASCII format than It's encode and replace it.
					
					
					$keyword2 = htmlentities(trim($keyword));
					
					if($case_sensitive_keywords == 0){
						$search2= "~([\s\.\,\;\!\?\:\>\(\)\'\"\*\/«])(".$keyword2.")([\*\/\'\"\(\)\<\s\.\,\;\!\?\:»])~siu";
					}else{						
						$search2 = "/\b" .$keyword2."\b/";
					}
					
					if(!empty($eb_exclude_tag)){ 
						foreach ($eb_exclude_tag as $key => $value) {
							$content = preg_replace_callback("~(<".$value."[^>]*>)(.*?)(<\/".$value.">)~si",function($matches) {return "<:ZyX>".plgContentEbautoseointernallinks::maskContent($matches[1].$matches[2].$matches[3])."<:ZyX/>";},$content);
						}
					}
					
					$content = preg_replace_callback("~(<script)(.*?)(<\/script>)~si", function($matches) {return "<:ZyX>".plgContentEbautoseointernallinks::maskContent($matches[1].$matches[2].$matches[3])."<:ZyX/>";},$content);

					$content = preg_replace_callback("~(<\!\-\-)(.*?)(\-\-\>)~si", function($matches) {return "<:ZyX>".plgContentEbautoseointernallinks::maskContent($matches[1].$matches[2].$matches[3])."<:ZyX/>";},$content);
					$content = preg_replace_callback("~(<style)(.*?)(<\/style>)~si", function($matches) {return "<:ZyX>".plgContentEbautoseointernallinks::maskContent($matches[1].$matches[2].$matches[3])."<:ZyX/>";},$content);		
					$content = preg_replace_callback("~(<a)(.*?)(?=<\/a>)(<\/a>)~si", function($matches) {return "<:ZyX>".plgContentEbautoseointernallinks::maskContent($matches[1].$matches[2].$matches[3])."<:ZyX/>";},$content);
					$content = preg_replace_callback("~(<[a-z])(.*?)(>)~si", function($matches) {return "<:ZyX>".plgContentEbautoseointernallinks::maskContent($matches[1].$matches[2].$matches[3])."<:ZyX/>";},$content);
					$content = preg_replace($search2, $link, $content,-1);
					$content = preg_replace_callback("~<\:ZyX>(.*?)(?=<\:ZyX\/>)<\:ZyX\/>~si", function($matches) {return plgContentEbautoseointernallinks::unmaskContent($matches[1]);}, $content);
					$content = preg_replace_callback("~<\:ZyX>(.*?)(?=<\:ZyX\/>)<\:ZyX\/>~si", function($matches) {return plgContentEbautoseointernallinks::unmaskContent($matches[1]);}, $content);		
					
					
					return $content;
					
				} else {			
					
					$keyword= trim($keyword);
					
					if($case_sensitive_keywords == 0){
						$search = "~([\s\.\,\;\!\?\:\>\(\)\'\"\*\/«])(".$keyword.")([\*\/\'\"\(\)\<\s\.\,\;\!\?\:»])~siu";
					}else{
						$search = "/\b" .$keyword."\b/";
					}
					
					if(!empty($eb_exclude_tag)){ 
						foreach ($eb_exclude_tag as $key => $value) {
							$content = preg_replace_callback("~(<".$value."[^>]*>)(.*?)(<\/".$value.">)~si",function($matches) {return "<:ZyX>".plgContentEbautoseointernallinks::maskContent($matches[1].$matches[2].$matches[3])."<:ZyX/>";},$content);
						}
					}					
					
					$content = preg_replace_callback("~(<script)(.*?)(<\/script>)~si", function($matches) {return "<:ZyX>".plgContentEbautoseointernallinks::maskContent($matches[1].$matches[2].$matches[3])."<:ZyX/>";} ,$content);
					$content = preg_replace_callback("~(<\!\-\-)(.*?)(\-\-\>)~si", function($matches) {return "<:ZyX>".plgContentEbautoseointernallinks::maskContent($matches[1].$matches[2].$matches[3])."<:ZyX/>";},$content);
					$content = preg_replace_callback("~(<style)(.*?)(<\/style>)~si", function($matches) {return "<:ZyX>".plgContentEbautoseointernallinks::maskContent($matches[1].$matches[2].$matches[3])."<:ZyX/>";},$content);
					
					//$content = preg_replace_callback("~(<p>)(.*?)(<\/p>)~si",function($matches) {return "<:ZyX>".plgContentEbautoseointernallinks::maskContent($matches[1].$matches[2].$matches[3])."<:ZyX/>";},$content);					
					$content = preg_replace_callback("~(<a)(.*?)(?=<\/a>)(<\/a>)~si", function($matches) {return "<:ZyX>".plgContentEbautoseointernallinks::maskContent($matches[1].$matches[2].$matches[3])."<:ZyX/>";},$content);
					$content = preg_replace_callback("~(<[a-z])(.*?)(>)~si", function($matches) {return "<:ZyX>".plgContentEbautoseointernallinks::maskContent($matches[1].$matches[2].$matches[3])."<:ZyX/>";},$content);

					$link = '$1'.$link.'$3';
					$content = preg_replace($search, $link, $content,$limit_count);
					$content = preg_replace_callback("~<\:ZyX>(.*?)(?=<\:ZyX\/>)<\:ZyX\/>~si", function($matches) {return plgContentEbautoseointernallinks::unmaskContent($matches[1]);}, $content);
					$content = preg_replace_callback("~<\:ZyX>(.*?)(?=<\:ZyX\/>)<\:ZyX\/>~si", function($matches) {return plgContentEbautoseointernallinks::unmaskContent($matches[1]);}, $content);

					$match_content = substr_count($content,$org_keylink);
					
					if($match_content != $limit_count){
						
						## if character added in ASCII format than It's encode and replace it.
						$keyword2 = htmlentities(trim($keyword));
						
						if($case_sensitive_keywords == 0){
							$search2= "~([\s\.\,\;\!\?\:\>\(\)\'\"\*\/«])(".$keyword2.")([\*\/\'\"\(\)\<\s\.\,\;\!\?\:»])~siu";	
						}else{							
							$search2 = "/\b" .$keyword2."\b/";
						}
						
						if(!empty($eb_exclude_tag)){ 
							foreach ($eb_exclude_tag as $key => $value) {
								$content = preg_replace_callback("~(<".$value."[^>]*>)(.*?)(<\/".$value.">)~si",function($matches) {return "<:ZyX>".plgContentEbautoseointernallinks::maskContent($matches[1].$matches[2].$matches[3])."<:ZyX/>";},$content);
							}
						}
						
						$content = preg_replace_callback("~(<script)(.*?)(<\/script>)~si", function($matches) {return "<:ZyX>".plgContentEbautoseointernallinks::maskContent($matches[1].$matches[2].$matches[3])."<:ZyX/>";},$content);
						//$content = preg_replace_callback("~(<p>)(.*?)(<\/p>)~si",function($matches) {return "<:ZyX>".plgContentEbautoseointernallinks::maskContent($matches[1].$matches[2].$matches[3])."<:ZyX/>";},$content);												
						$content = preg_replace_callback("~(<\!\-\-)(.*?)(\-\-\>)~si", function($matches) {return "<:ZyX>".plgContentEbautoseointernallinks::maskContent($matches[1].$matches[2].$matches[3])."<:ZyX/>";},$content);
						$content = preg_replace_callback("~(<style)(.*?)(<\/style>)~si", function($matches) {return "<:ZyX>".plgContentEbautoseointernallinks::maskContent($matches[1].$matches[2].$matches[3])."<:ZyX/>";},$content);		
						$content = preg_replace_callback("~(<a)(.*?)(?=<\/a>)(<\/a>)~si", function($matches) {return "<:ZyX>".plgContentEbautoseointernallinks::maskContent($matches[1].$matches[2].$matches[3])."<:ZyX/>";},$content);
						$content = preg_replace_callback("~(<[a-z])(.*?)(>)~si", function($matches) {return "<:ZyX>".plgContentEbautoseointernallinks::maskContent($matches[1].$matches[2].$matches[3])."<:ZyX/>";},$content);
						$content = preg_replace($search2, $link, $content,$limit_count);
						$content = preg_replace_callback("~<\:ZyX>(.*?)(?=<\:ZyX\/>)<\:ZyX\/>~si", function($matches) {return plgContentEbautoseointernallinks::unmaskContent($matches[1]);}, $content);
						$content = preg_replace_callback("~<\:ZyX>(.*?)(?=<\:ZyX\/>)<\:ZyX\/>~si", function($matches) {return plgContentEbautoseointernallinks::unmaskContent($matches[1]);}, $content);
					}
					//print_r($eb_exclude_tag);					
					return $content;

				}
				
		}else{
			
			if($define_limit == 0){								
				if(!empty($eb_exclude_tag)){ 					
					foreach ($eb_exclude_tag as $key => $value) {
						$content = preg_replace_callback("~(<".$value."[^>]*>)(.*?)(<\/".$value.">)~si",function($matches) {return "<:ZyX>".plgContentEbautoseointernallinks::maskContent($matches[1].$matches[2].$matches[3])."<:ZyX/>";},$content);					
					}				
				}
				
				$content = preg_replace_callback("~(<style)(.*?)(<\/style>)~si", function($matches) {return "<:ZyX>".plgContentEbautoseointernallinks::maskContent($matches[1].$matches[2].$matches[3])."<:ZyX/>";},$content);		
				$content = preg_replace_callback("~(<a)(.*?)(?=<\/a>)(<\/a>)~si", function($matches) {return "<:ZyX>".plgContentEbautoseointernallinks::maskContent($matches[1].$matches[2].$matches[3])."<:ZyX/>";},$content);
				$content = preg_replace_callback("~(<[a-z])(.*?)(>)~si", function($matches) {return "<:ZyX>".plgContentEbautoseointernallinks::maskContent($matches[1].$matches[2].$matches[3])."<:ZyX/>";},$content);
				
				if($case_sensitive_keywords == 0){
					$search= "/\p{L}*?".preg_quote($keyword)."\p{L}*/ui";	
				}else{					
					$search = "/\b" .$keyword."\b/";
				}
				
				$content = preg_replace($search, $link, $content);
				
				$content = preg_replace_callback("~<\:ZyX>(.*?)(?=<\:ZyX\/>)<\:ZyX\/>~si", function($matches) {return plgContentEbautoseointernallinks::unmaskContent($matches[1]);}, $content);
				$content = preg_replace_callback("~<\:ZyX>(.*?)(?=<\:ZyX\/>)<\:ZyX\/>~si", function($matches) {return plgContentEbautoseointernallinks::unmaskContent($matches[1]);}, $content);
				
				return $content;
			
			} else {
					
					if(!empty($eb_exclude_tag)){
					
						foreach ($eb_exclude_tag as $key => $value) {
							$content = preg_replace_callback("~(<".$value."[^>]*>)(.*?)(<\/".$value.">)~si",function($matches) {return "<:ZyX>".plgContentEbautoseointernallinks::maskContent($matches[1].$matches[2].$matches[3])."<:ZyX/>";},$content);
							
						}
					}
					
					if($case_sensitive_keywords == 0){
						$search= "/\p{L}*?".preg_quote($keyword)."\p{L}*/ui";	
					}else{						
						$search = "/\b" .$keyword."\b/";
					}
					
					$content = preg_replace_callback("~(<style)(.*?)(<\/style>)~si", function($matches) {return "<:ZyX>".plgContentEbautoseointernallinks::maskContent($matches[1].$matches[2].$matches[3])."<:ZyX/>";},$content);
					$content = preg_replace_callback("~(<a)(.*?)(?=<\/a>)(<\/a>)~si", function($matches) {return "<:ZyX>".plgContentEbautoseointernallinks::maskContent($matches[1].$matches[2].$matches[3])."<:ZyX/>";},$content);
					$content = preg_replace_callback("~(<[a-z])(.*?)(>)~si", function($matches) {return "<:ZyX>".plgContentEbautoseointernallinks::maskContent($matches[1].$matches[2].$matches[3])."<:ZyX/>";},$content);
					$content = preg_replace($search, $link, $content, $limit_count);
					$content = preg_replace_callback("~<\:ZyX>(.*?)(?=<\:ZyX\/>)<\:ZyX\/>~si", function($matches) {return plgContentEbautoseointernallinks::unmaskContent($matches[1]);}, $content);
					$content = preg_replace_callback("~<\:ZyX>(.*?)(?=<\:ZyX\/>)<\:ZyX\/>~si", function($matches) {return plgContentEbautoseointernallinks::unmaskContent($matches[1]);}, $content);
					
					$match_content = substr_count($content,$org_keylink);
													
					return $content;
					
			}
			
		}		
	}

}
