<?php
/*
* @name article tool 1.0
* Created By Guarneri Iacopo
* http://www.the-html-tool.com/
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
*/
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.plugin.plugin' );

class plgContentarticletool extends JPlugin
{
	function onContentPrepare( $context, &$article, &$params, $page = 0)
	{ 
		$cosa_mostrare=$this->params->get("cosa_mostrare");
		$cats=$this->params->get("category");
		$counter=$this->params->get("counter","0");
		$position=$this->params->get("position","top");
		$css=$this->params->get("css","color:#000; font-family:Arial; font-size:14px; line-height:19px; padding:20px;");
		$memory_limit=$this->params->get("memory_limit","25");
		$visual_pdf=$this->params->get("visual_pdf","0");
		
		if($visual_pdf==1){$visual_pdf=0;}else{$visual_pdf=1;}

		//se l'articolo non è della giusta categoria esce subito
		$categoria_presente=0;
		foreach($cats as $cat){if(@$article->catid==$cat){$categoria_presente=1;}}
		if($categoria_presente==0){return false;}
		else{
			echo '<link rel="stylesheet" href="'.JURI::base().'plugins/content/articletool/style/style.css" type="text/css" />';
			if(JRequest::getVar('print', 0, 'get')!=1 && JRequest::getVar('download', 0, 'get')!=1){	
				//inserisce gli script per far funzionare sharethis
				foreach($cosa_mostrare as $ico){
					if($ico!="stampa" && $ico!="scarica"){
						$article->text.='<script type="text/javascript">var switchTo5x=true;</script>
						<script type="text/javascript" src="http://w.sharethis.com/button/buttons.js"></script>
						<script type="text/javascript">stLight.options({publisher: "cf4e376e-1d9c-43b1-8df7-617e403ae918", doNotHash: false, doNotCopy: false, hashAddressBar: false});</script>';
						break;
					}
				}
				
				$icone="";
				$uri = JFactory::getURI();
				//mostro le icone dei social, invia per mail, stampa e scarica
				foreach($cosa_mostrare as $ico){
					if($ico=="stampa"){
						$uri->setVar('print', 1);
						$uri->setVar('download', 0);
						if($counter==0){
							$icone.="
							<span class='stButton stButton_print'>
							<a id='article_tool_print' href='".$uri->toString()."' target='_blank'>
								<img src='".JURI::base()."plugins/content/articletool/images/print.png' />
							</a></span>";
						}else{
							$icone.="<a id='article_tool_print' href='".$uri->toString()."' target='_blank'>
							<span class='stButton'><span class='stButton_gradient'>
								<img src='".JURI::base()."plugins/content/articletool/images/print.png' />
							<span class='chicklets chicklets_article_tool_print'>Print</span></span></span></a>";
						}
						
					}else if($ico=="scarica"){
						$uri->setVar('print', 0);
						$uri->setVar('download', 1);
						if($counter==0){
							$icone.="
							<span class='stButton stButton_pdf'>
							<a id='article_tool_pdf' href='".$uri->toString()."' target='_blank'>
								<img src='".JURI::base()."plugins/content/articletool/images/pdf.png' />
							</a></span>";
						}else{
							$icone.="<a id='article_tool_pdf' href='".$uri->toString()."' target='_blank'>
							<span class='stButton'><span class='stButton_gradient'>
								<img src='".JURI::base()."plugins/content/articletool/images/pdf.png' />
							<span class='chicklets chicklets_article_tool_pdf'>Download</span></span></span></a>";
						}
					}else{
						$url_corrente=JRoute::_(ContentHelperRoute::getArticleRoute($article->slug, $article->catid));
						if($counter==0){$type="large";}else{$type="hcount";}
						$icone.='<span class="st_'.$ico.'_'.$type.'" st_url="'.$url_corrente.'" displayText="'.$ico.'"></span>';
					}
				}
				
				if($position=="top"){
					$article->text = "<div class='article_tool_container'>".$icone."</div>".$article->text;
				}
				if($position=="bottom"){
					$article->text.="<div class='article_tool_container'>".$icone."</div>";
				}
			}
			
			if(!function_exists('relative_to_absolute')) {
				function relative_to_absolute($article,$home){
					preg_match_all('/src="(.+?)"/', $article->text, $src);
					preg_match_all('/href="(.+?)"/', $article->text, $href);
					$urls=array_merge($src[1], $href[1]);
					foreach($urls as $url){
						if(!strstr($url,"http://") && !strstr($url,"https://")){
							$article->text=str_replace($url,$home."/".$url,$article->text);
						}
					}
					return $article->text;
				}
			}
			$article->text=relative_to_absolute($article,JURI::base());
			
			if(JRequest::getVar('print', 0, 'get')==1){
				$article->text.='
					<script type="text/javascript">
						window.print();
					</script>
				';
				
				echo "<meta http-equiv='content-type' content='text/html; charset=utf-8' /><div style='".$css."'>".$article->text."</div>";
				JFactory::getApplication()->close();
				die("");
			}
			if(JRequest::getVar('download', 0, 'get')==1){

				//$txt_to_rend=file_get_contents(JURI::current());
				$txt_to_rend="<meta http-equiv='content-type' content='text/html; charset=utf-8' /><div style='".$css."'>".$article->text."</div>";
				require_once("plugins/content/articletool/dompdf/dompdf_config.inc.php");

				if ( get_magic_quotes_gpc() )
					$txt_to_rend = stripslashes($txt_to_rend);

				$old_limit = ini_set("memory_limit", $memory_limit."M");

				$dompdf = new DOMPDF();
				$dompdf->load_html($txt_to_rend);
				$dompdf->set_paper("portrait","letter");
				$dompdf->render();

				/*i pdf possono essere messi in cache, vedere se nella jed ci sono componenti article to pdf*/
				$dompdf->stream($article->title.".pdf", array("Attachment" => $visual_pdf));
				
				JFactory::getApplication()->close();
				die("");
			}
			
			return true;
		}
	}
}
