Location: PHPKode > scripts > Avatar Tools > avatar_tool.php
<?php
/**
 * @version		$Id: coolfeed.php 100 2012-04-14 17:42:51Z hide@address.com $
 * @copyright	JoomAvatar.com
 * @author		Nguyen Quang Trung
 * @link		http://joomavatar.com
 * @license		License GNU General Public License version 2 or later http://www.gnu.org/licenses/gpl-2.0.html
 * @package		Avatar Dream Framework Template
 * @facebook 	http://www.facebook.com/pages/JoomAvatar/120705031368683
 * @twitter	    https://twitter.com/#!/JoomAvatar
 * @support 	http://joomavatar.com/forum/
 */

// no direct access
defined('_JEXEC') or die;

jimport('joomla.plugin.plugin');
require_once 'defined.php';
require_once ( dirname(__FILE__) . DS . 'helpers' . DS . 'optimize.php');

class plgSystemAvatar_Tool extends JPlugin
{
	public $_loadToolBar = true;
	public $_ATAPP;
	// Construction function
	public function __construct(& $subject, $config) {
		parent::__construct($subject, $config);
		
		JError::setErrorHandling(E_ERROR, 'callback', array('plgSystemAvatar_Tool', 'handleError'));
		
		$this->atapp();
		
		$this->checkToolbar();
	}
	
	/**
	 * build Avatar Tool App
	 */
	public function atapp()
	{
		if (!$this->_ATAPP) 
		{
			require_once AT_PATH.DS.'app.php';
			$this->_ATAPP = new ATAPP;
		}	
	}
	
	public function onAfterInitialise()
	{
		require_once ( dirname(__FILE__) . DS . 'helpers' . DS . 'seo.php');
		
		PlgAvatarSeo::canonicalRedirectDomain();
		
		$this->_controller();
	}

	public function _controller()
	{
		$this->loadLanguage('plg_system_avatar_tool');
		
		require_once ( dirname(__FILE__) . DS . 'helpers' . DS . 'controller.php');
		$controller = JRequest::getString('controller');
		if ($controller == 'plgavatartools') {
			echo PlgAvatarController::combine();
			exit();
		}
		
		$app = JFactory::getApplication();
		$user = JFactory::getUser();
		
		if ($app->isAdmin() && $user->get('isRoot')) 
		{
			// controller
			$option = JRequest::getString('option');
			$controller = JRequest::getString('controller');
			$task = JRequest::getString('task');
			
			if ($option == 'avatar-tool' && $controller != ''){
				echo PlgAvatarController::callController($controller, $task);
				exit();
			}
			// end controller
			
			if ($this->_loadToolBar) { 
				$document = JFactory::getDocument();
				$document->addStyleSheet(dirname(JURI::base()).'/plugins/system/avatar_tool/lib/jqueryui/css/vader/jquery-ui-1.8.22.custom.css');
				$document->addStyleSheet(dirname(JURI::base()).'/plugins/system/avatar_tool/assets/css/toolbar.css');
			}
		}	
	}
	
	public function onAfterRender() 
	{
		$this->_optimize();
		
		$this->_toolBar();	
			
		$this->_seoHeaderReplace();
		
		$this->_seoOutLinkOptimize();
		
		$this->copyright();
	}
	
	function _seoHeaderReplace() 
	{
		require_once ( dirname(__FILE__) . DS . 'helpers' . DS . 'seo.php');
		
		PlgAvatarSeo::headerTags();
	}
	
	function _seoOutLinkOptimize() {
		require_once ( dirname(__FILE__) . DS . 'helpers' . DS . 'seo.php');
		
		PlgAvatarSeo::outLinkOptimize();
	}
	
	function _toolBar()
	{
		if (!$this->_loadToolBar) return;  
		
		$app = JFactory::getApplication();
		if ($app->isSite()) {
			return;
		}
		
		$user = JFactory::getUser();
		if (!$user->get('isRoot')) {
			return;
		}
	
		$tmpl = JRequest::getString('tmpl');
		if ($tmpl == 'component') return;
		
		$this->body = JResponse::getBody();
		$aCss = array();
		ob_start();
		$oView = $this->_ATAPP->view();
		$oView->main();
		$oView->extensionDisplay();
		$aCss = $oView->css();
		$views = ob_get_clean();
		
		ob_start();
		require_once dirname(__FILE__).DS.'views/menu.php';
		$menu = ob_get_clean();
		
		$this->body = str_replace('</body>', '</body><div id="avatar-toolbar"><div id="avatar-tool-overlay"></div><div id="avatar-tool-box"><div class="avatar-tool-menu">'.$menu.'</div><div class="avatar-toolbar-main">'.$views.'</div><div style="clear:both;"></div><div class="avatar-control-button"></div></div></div><div class="avatar-tool-panel"><span id="avatar-tool-panel-open">'. JText::_('AVATAR_TOOL_PANEL_OPEN') .'</span><span id="avatar-tool-ajax-loader"></span></div>', $this->body);
		
		//load Javascript 
		$toolbar = '<script type="text/javascript" src="'.dirname(JURI::base()).'/plugins/system/avatar_tool/assets/js/toolbar.js"></script>';
		$js = '<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>';
		$js .= '<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.21/jquery-ui.js"></script>';
		$js .= '<script type="text/javascript">jQuery.noConflict();</script>';
		
		//if($jquery = PlgAvatarOptimize::loadjQuery($this->body)) {
			//$this->body = str_replace('</head>', $toolbar.'</head>', $this->body);
		//} else {
			//$this->body = str_replace('</head>', $js.$toolbar.'</head>', $this->body);
		//}
		
		$this->body = str_replace('</head>', $js.$toolbar.'</head>', $this->body);
		
		$css = '';
		
		foreach ($aCss as $link) {
			$css .= "\r\t".'<link rel="stylesheet" href="'. $link .'" type="text/css" />'."\r";
		}
		
		$this->body = str_replace('</title>', '</title>'.$css, $this->body);
		
		JResponse::setBody($this->body);
	}

	function _optimize() 
	{
		$app = JFactory::getApplication();
		
		if ($app->isAdmin()) {
			return;
		}
		
		require_once ( dirname(__FILE__) . DS . 'lib' . DS . 'optimize' . DS . 'CSS.php' );
		require_once ( dirname(__FILE__) . DS . 'lib' . DS . 'optimize' . DS . 'HTML.php' );
		require_once ( dirname(__FILE__) . DS . 'lib' . DS . 'optimize' . DS . 'JSMin.php' );
		
		$type = $this->params->get('optimize', 0);
		
		if (!$type) {
			return;
		}
		
		$this->optimizeCSS = false;
		$this->optimizeJS = false;
		$this->optimizeGzip = false;
		
		if ($type == 4) {
			$this->optimizeGzip = true;
		}
		
		if ($type == 1 || $type == 3 || $type == 4) {
			$this->optimizeCSS = true;
		}
		
		if ($type == 2 || $type == 3 || $type == 4) {
			$this->optimizeJS = true;
		}

		$document = JFactory::getDocument();
        $doctype = $document->getType();
		
		if ($doctype != 'html') return;

        $this->body = JResponse::getBody();
		 
        $this->cache = JFactory::getCache(PlgAvatarOptimize::$_cacheGroup, 'callback', 'file');
		$this->cache->setCaching(1);
        $this->cache->setLifetime((int) $this->params->get('lifetime', '10') * 24 * 60 * 60);
		
		$this->_scripts = PlgAvatarOptimize::scanScripts($this->body);
		$this->_styles = PlgAvatarOptimize::scanStyles($this->body);
		
		// remove scripts and styles 
		if ($this->optimizeJS) {
			$this->body = PlgAvatarOptimize::removeScripts($this->body, $this->_scripts['pattern']);
		}
		
		if ($this->optimizeCSS) {
			$this->body = PlgAvatarOptimize::removeScripts($this->body, $this->_styles['pattern']);
		}
		
		// combine css & js
		$this->body = PlgAvatarOptimize::combine($this);
		
        $this->body = Minify_HTML::minify($this->body, array(
	        'cssMinifier' => array('Minify_CSS', 'minify')
	        ,'jsMinifier' => array('JSMin', 'minify')
	    ));
		
        JResponse::setBody($this->body);	
	}

	public function onAfterDispatch()
	{
	 	$this->setSiteName();	
	}
	
	protected function setSiteName()
	{
		$app = JFactory::getApplication();
		
		if (!$app->isSite()) {
			return;
		}

		$document = JFactory::getDocument();
		$menu = JSite::getMenu();
		$isHomePage = ($menu->getActive() == $menu->getDefault());
		$sitename = $app->getCfg('sitename');
		
		require_once ( dirname(__FILE__) . DS . 'helpers' . DS . 'params.php');
		$avatarParams = PlgAvatarParams::getParams();	
		
		if (isset($avatarParams->site)) 
		{
			$seoSite = $avatarParams->site;
			
			if (isset($seoSite->sitename)) {
				$sitename = ($seoSite->sitename != '') ? $seoSite->sitename : $app->getCfg('sitename');
			}	
		}
		
		if ($isHomePage) 
		{
			if ($seoSite->home_page_title != '') {
				$sitename = $seoSite->home_page_title;
			}
		}

		$separator = $seoSite->title_separator;
		$current = $document->getTitle();
		
		if ($seoSite->sitename_position == 1) {
			$title = $current.' '. $separator. ' ' .$sitename;
		} else if ($seoSite->sitename_position == 0){
			$title = $sitename.' '. $separator.' '. $current;
		}
		
		$document->setTitle($title);
		
		if (isset($avatarParams->meta) && isset($avatarParams->meta->meta_desc) && isset($avatarParams->meta->meta_keywords)) 
		{
			if ($document->getDescription() == '' && $avatarParams->meta->meta_desc != '') {
				$document->setDescription($avatarParams->meta->meta_desc);			
			}
			
			if ($document->getMetaData('keywords') == '' && $avatarParams->meta->meta_keywords != '') {
				$document->setMetadata('keywords',$avatarParams->meta->meta_keywords);
			}	
		}
		
		$document->setGenerator($document->getGenerator(). ' | Avatar Tool - Joomla UI with Drag & Drop, Ajax, SEO and Compress http://joomavatar.com');
	}
	
	// function which handles the errors
	public static function handleError(& $error) 
	{
		$app = JFactory::getApplication();	
		
		if (!$app->isAdmin() && ($error->getCode() == 404))
		{
			require_once ( dirname(__FILE__) . DS . 'helpers' . DS . 'params.php');
			$params = PlgAvatarParams::getParams();	
			
			if (isset($params->site)) 
			{
				$siteSeo  = $params->site;
				if (isset($siteSeo->redirect_link) && $siteSeo->redirect_link != '') {
					$app->redirect($siteSeo->redirect_link, null, null, true, true);
				}	
			}
		}
		
		JError::customErrorPage($error);
	}
	
	public function copyright()
	{
		$body = JResponse::getBody();
		$copyright = '<div class="avatarslide-copyright" style="width:100%;">
				&copy; JoomAvatar.com
				<a target="_blank" href="http://www.facebook.com/pages/JoomAvatar/120705031368683" title="JoomAvatar\'s Facebook joomla tips, joomla forum">Facebook</a> -
				<a target="_blank" href="http://joomavatar.com" title="Joomavatar\'s Google Plus joomla extensions, joomla forum">Google + </a> - 
				<a target="_blank" href="https://twitter.com/JoomAvatar" title="JoomAvatar\'s Twitter joomla templates, joomla forum">Twitter</a> 
		</div>';
		
		$body = str_replace('</body>', $copyright.'</body>', $body);
		
        JResponse::setBody($body);
	}
	
	function onContentPrepare( $context, &$article, &$params, $limitstart )
	{
		require_once ( dirname(__FILE__) . DS . 'helpers' . DS . 'seo.php');
		PlgAvatarSeo::autoMeta($article);
	}

	function onBeforeCompileHead() 
	{
		require_once ( dirname(__FILE__) . DS . 'helpers' . DS . 'seo.php');
		PlgAvatarSeo::canonicalMetaTag();
	}
	
	public function checkToolbar() 
	{
		$exceptExts = $this->params->get('except_extension', '');
		
		if ($exceptExts != '') 
		{
			$posComma = strpos(',', $exceptExts);
			$option = JRequest::getString('option');
			
			if ($posComma > 0) 
			{
				$exceptExts = explode(',', $exceptExts);
				
				foreach ($exceptExts as $ext) 
				{
					if (strpos(strtolower($option), strtolower($ext)) !== false) {
						$this->_loadToolBar = false;
					}
				}
			} 
			else 
			{
				if (strpos(strtolower($option), strtolower($exceptExts)) !== false) {
					$this->_loadToolBar = false;
				}
			}	
		} 
		else 
		{
			$this->_loadToolBar = true;			
		}
	}
}
Return current item: Avatar Tools