<?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;
class PlgAvatarSeo
{
public static function isHome()
{
$app = JFactory::getApplication();
$menu = $app->getMenu();
if ($menu->getActive() == $menu->getDefault()) {
return true;
}
return false;
}
/**
* @author Juan Padial (www.shikle.com)
* This plugin will automatically generate Meta Description tags and Meta keywords from your content.
* version 2.0.3
*/
public static function cleanText( $text ) {
// Remove tags
$text = preg_replace( "'<script[^>]*>.*?</script>'si", '', $text );
$text = preg_replace( '/<!--.+?-->/', '', $text );
$text = preg_replace( '/{.+?}/', '', $text );
//$text = strip_tags( $text );
$text = preg_replace( '/<a\s+.*?href="([^"]+)"[^>]*>([^<]+)<\/a>/is', '\2 (\1)', $text );
$text = preg_replace('/<[^>]*>/', ' ', $text);
// Remove any email addresses
$regex = '/(([_A-Za-z0-9-]+)(\\.[_A-Za-z0-9-]+)*@([A-Za-z0-9-]+)(\\.[A-Za-z0-9-]+)*)/iex';
$text = preg_replace($regex, '', $text);
// convert html entities to chars
$text = html_entity_decode($text,ENT_QUOTES,'UTF-8');
$text = str_replace('"', '\'', $text); //Make sure all quotes play nice with meta.
$text = str_replace(array("\r\n", "\r", "\n", "\t"), " ", $text); //Change spaces to spaces
//convert all separators to a normal space
$text = preg_replace(array('/\s/u',),' ',$text ); //http://www.fileformat.info/info/unicode/category/index.htm
// remove any extra spaces
while (strchr($text," ")) {
$text = str_replace(" ", " ",$text);
}
// general sentence tidyup
for ($cnt = 1; $cnt < JString::strlen($text)-1; $cnt++) {
// add a space after any full stops or comma's for readability
// added as strip_tags was often leaving no spaces
if ( ($text{$cnt} == '.') || (($text{$cnt} == ',') && !(is_numeric($text{$cnt+1})))) {
if ($text{$cnt+1} != ' ') {
$text = JString::substr_replace($text, ' ', $cnt + 1, 0);
}
}
}
return trim($text);
}
public static function autoKeywords($desc,$blacklist,$sticklist,$count,$minlength)
{
$desc = JString::strtolower($desc);
$keysArray = explode(" ", $desc);
// Sort words from up to down
$keysArray = array_count_values($keysArray);
$stickArray = explode(",", $sticklist);
if (JString::strlen($blacklist) > 0)
{
$blackArray = explode(",", $blacklist);
foreach($blackArray as $blackWord)
{
if(isset($keysArray[JString::trim($blackWord)]))
unset($keysArray[JString::trim($blackWord)]);
}
}
arsort($keysArray);
$i = 1;
$keywords = "";
$gkeywords = "";
if (JString::strlen($sticklist) > 0)
{
foreach($keysArray as $word => $instances)
{
if($i > $count) break;
if(in_array($word,$stickArray)) {
$gkeywords .= $word . ",";
$i++;
}
}
}
foreach($keysArray as $word => $instances)
{
if($i > $count) break;
if(JString::strlen(JString::trim($word)) >= $minlength && is_string($word) && in_array($word,$stickArray)==false) {
$keywords .= $word . ",";
$i++;
}
}
$keywords = $gkeywords.$keywords;
$keywords = JString::rtrim($keywords, ",");
return $keywords;
}
public static function cleanAllSymbols( $text )
{
//remove symbols
$text = preg_replace(array('/[\p{Cc}\p{Pd}\p{Pe}\p{Pf}\p{Pi}\p{Po}\p{Ps}\p{Sc}\p{Sm}\p{So}\p{Zl}\p{Zp}\p{Zs}]/u',),' ',$text ); //http://www.fileformat.info/info/unicode/category/index.htm
// remove any extra spaces
while (strchr($text," ")) {
$text = str_replace(" ", " ",$text);
}
return $text;
}
public static function autoMeta($article)
{
$app = JFactory::getApplication();
$document = JFactory::getDocument();
if ($document->getDescription() != '' && $document->getDescription() != $app->getCfg('MetaDesc')) {
return;
}
$isHome = self::isHome();
require_once ( dirname(__FILE__) . DS . 'params.php');
$avatarParams = PlgAvatarParams::getParams();
if (isset($avatarParams->site))
{
$seoSite = $avatarParams->site;
if (!$seoSite->auto_meta) return;
if ($isHome && !$seoSite->auto_meta && $document->getDescription() == '') {
$document->setDescription($app->getCfg('MetaDesc'));
return;
}
$view = JRequest::getVar('view');
if ($article->metakey == '' || $article->metadesc == '') {
$content = JString::substr($article->text,0, 500);
}
if ($view == 'category')
{
$db = JFactory::getDBO();
$catid = JRequest::getVar('id');
$db->setQuery('SELECT cat.description FROM #__categories cat WHERE cat.id='.$catid);
$catdesc = $db->loadResult();
if ($catdesc) {
$content = $catdesc;
}
}
$content = self::cleanText($content);
if ($article->metakey == '')
{
$content = self::cleanAllSymbols($content);
$keywords = self::autoKeywords($content, '', '', 20, 5);
if ($keywords != '') {
$document->setMetaData('keywords', $keywords);
}
}
if ($article->metadesc == '') {
$document->setDescription($content);
}
}
}
public static function canonicalMetaTag()
{
require_once ( dirname(__FILE__) . DS . 'params.php');
$avatarParams = PlgAvatarParams::getParams();
if (isset($avatarParams->site))
{
$seoSite = $avatarParams->site;
if (!$seoSite->canonical) return;
$sitedomain = $seoSite->canonical_domain;
if (!$sitedomain) return;
$uri = JFactory::getUri();
if($uri->isSSL()) {
$sitedomain = 'https://'.$sitedomain;
} else {
$sitedomain = 'http://'.$sitedomain;
}
$option = JRequest::getVar('option', '');
$view = JRequest::getVar('view','');
if($option == 'com_content')
{
$thestart = JRequest::getInt('start',0);
$limitstart = JRequest::getInt('limitstart',0);
$start="";
if($thestart > 0) {
$start = '?start='.$thestart;
} elseif ($limitstart > 0) {
$start = '?limitstart='.$limitstart;
}
if($view=='article')
{
$id = JRequest::getInt('id');
if ($id>0)
{
$db = JFactory::getDbo();
$query = "SELECT b.title as cattitle,".
" CASE WHEN CHAR_LENGTH(a.alias) THEN CONCAT_WS(':', a.id, a.alias) ELSE a.id END as slug,".
" CASE WHEN CHAR_LENGTH(b.alias) THEN CONCAT_WS(':', a.catid, b.alias) ELSE a.catid END as catslug".
" FROM #__content AS a LEFT JOIN #__categories AS b ON b.id = a.catid WHERE a.id = $id";
$row = $db->SetQuery($query);
$row = $db->loadObject();
$urlCanonical = $sitedomain.JRoute::_(ContentHelperRoute::getArticleRoute($row->slug, $row->catslug));
}
}
if($view=='category')
{
$urlCanonical = $sitedomain.JRoute::_(ContentHelperRoute::getCategoryRoute(JRequest::getInt('id')));
if(strpos($urlCanonical, '&') !== 0) {
$start = str_replace('&','?',$start);
}
$urlCanonical = $urlCanonical.$start;
}
if (self::isHome())
{
if($start!='') {
$urlCanonical = $sitedomain.JRoute::_('index.php').$start;
} else {
$urlCanonical = $sitedomain;
}
}
if (isset($urlCanonical) && $urlCanonical != '') {
$document = JFactory::getDocument();
$document->addHeadLink( $urlCanonical, 'canonical', 'rel', '' );
}
}
}
}
public static function canonicalRedirectDomain()
{
$app =& JFactory::getApplication();
if($app->isAdmin()) return false;
require_once ( dirname(__FILE__) . DS . 'params.php');
$avatarParams = PlgAvatarParams::getParams();
if (isset($avatarParams->site))
{
$seoSite = $avatarParams->site;
$siteDomain = $seoSite->canonical_domain;
if (!$seoSite->canonical_redirect) return;
if (!$siteDomain) return;
if(@$_SERVER['HTTP_HOST'] == $siteDomain) {
return true;
}
$uri = JFactory::getUri();
if($uri->isSSL()) {
$url = 'https://';
} else {
$url = 'http://';
}
// And maybe this, though probably not
$url .= $siteDomain . $_SERVER['REQUEST_URI'];
header('Location: '. $url, true, 301);
$app->close();
}
}
/**
* Run on after render. Find and replace header tag to heigher level
*/
public static function headerTags ()
{
$app =& JFactory::getApplication();
if($app->isAdmin()) return false;
require_once ( dirname(__FILE__) . DS . 'params.php');
$avatarParams = PlgAvatarParams::getParams();
if (isset($avatarParams->site))
{
$seoSite = $avatarParams->site;
if ($seoSite->header_tag_replace)
{
$body = JResponse::getBody();
$tags = self::headerFindTag($body);
self::headerReplace($tags, $body);
JResponse::setBody($body);
}
}
}
/**
* find all header tags
* @param string class or id of element
*/
public static function headerFindTag($content)
{
$matches = array();
$regex = '/\<(h[1-6]{1})(.*?)\>(.*?)<\/h[1-6]{1}\>/is';
preg_match_all($regex, $content, $matches, PREG_SET_ORDER );
if(count($matches))
{
$tags = array();
for($i=1; $i<7;$i++)
{
$ht = 'h'.$i;
$tags[$ht]['count'] = 0;
$tags[$ht]['matches'] = array();
}
foreach($matches as $match)
{
$ht = $match[1];
if(isset($tags[$ht]))
{
$tags[$ht]['count']++;
$tags[$ht]['matches'][] = $match;
}
}
return $tags;
}
return false;
}
/**
* replace to H1 tag if H1 tag not exists
*/
public static function headerReplace(&$tags, &$content)
{
if (!count($tags)) return;
if (!$tags['h1']['count'])
{
foreach ($tags as $ht => $tag)
{
if($ht != 'h1')
{
if($tag['count'] > 0)
{
$match = $tag['matches'][0];
$newHt = '<h1 ' . $match[2] . '>' . trim($match[3]) . '</h1>';
$content = substr_replace($content, $newHt, strpos($content, $match[0]), strlen($match[0]));
return;
}
}
}
}
}
/*
* A plugin that allows you to add rel="nofollow" on all external link
* Plugin for Joomla 1.5 / 1.6 - Version 1.2.2
* License: http://www.gnu.org/copyleft/gpl.html
* Authors: marco maria leoni
* Copyright (c) 2010 - 2011 marco maria leoni web consulting - http: www.mmleoni.net
* Project page at http://www.mmleoni.net/marco-rel-nofollow
* *** Last update: Feb 9th, 2011 ***
*/
public static function outLinkOptimize()
{
$app =& JFactory::getApplication();
if($app->isAdmin()) return false;
require_once ( dirname(__FILE__) . DS . 'params.php');
$avatarParams = PlgAvatarParams::getParams();
if (isset($avatarParams->site))
{
$seoSite = $avatarParams->site;
if ($seoSite->out_link_optimize)
{
$body = JResponse::getBody();
$tags = array( 'a' => '/<a\s+([^>]*)>/i', 'area' => '/<area\s+([^>]*)>/i' );
foreach($tags as $tag => $pattern)
{
$links = array();
preg_match_all( $pattern, $body, &$links, PREG_SET_ORDER );
foreach ($links as $link)
{
$pattern = "/(\w+)(\s*=\s*(?:\".*?\"|'.*?'|[^'\">\s]+))?/i"; // get attributes
$attribs = array();
preg_match_all ( $pattern, $link[1], &$attribs, PREG_SET_ORDER );
$list = array();
foreach ($attribs as $attrib) {
$list[strtolower($attrib[1])] = preg_replace("/=\s*[\"']?([^'\"]*)[\"']?/", "$1", $attrib[2]);
}
// skip if non http link
if (stripos($list['href'], 'http') !== 0) continue;
$href = preg_replace("/https?:\/\//i", '', $list['href']);
// skip if internal link
if (stripos($href, $_SERVER['SERVER_NAME']) === 0) continue;
$list['rel'] = 'nofollow';
$list['target'] = "_blank";
$ahref = "<$tag ";
foreach ($list as $k=>$v) {
$ahref .= "{$k}=\"{$v}\" ";
}
$ahref .= '>';
$body = str_replace($link[0], $ahref, $body);
}
}
JResponse::setBody($body);
}
}
}
}