Table of Contents

NP_SEO.php

This plugin will convert external links in articles, external links in comments and external smart user links <%userlink%> into SEO optimized URLs, like http://mydomain.com/?url=http://yourdomain.com

Written by: Radek HULAN and originally available at: http://forum.blogcms.com/viewtopic.php?pid=2570

General Plugin info
Current Version: 1.2
Code: see below on this page

Installing

  1. Save the code below for NP_SEO.php and upload the contents to your plugin directory.
  2. Install the plugin.
  3. Add the code for index.php to your main page (commonly index.php), right before “selector();”
  4. Done!

Code

index.php

Add this code before selector();

// NP_SEO plugin redirect
if (isset($_GET['redirect'])) {
	$query = mysql_query("SELECT url FROM ".sql_table("plug_seo")." WHERE id='".undoMagic($_GET['redirect'])."'");
	if($row = mysql_fetch_object($query)) {
		$row->url = stripslashes($row->url);
		$redirect = true;
		if (strpos($row->url,'|noseo|') !== false) {
			$arr = explode(",",'msnbot,googlebot,crawler,centrum');
			foreach ($arr as $s) {
				if (strstr($_SERVER["HTTP_USER_AGENT"],$s)) {
					$redirect=false;
				}
			}
			$arr = explode(",",'downloader.seznam,inktomi,yahoo,altavista,fasttrack,excite,hotbot,alltheweb,yahoo');
			$dns = strtolower(@gethostbyaddr($_SERVER["REMOTE_ADDR"]));
			foreach ($arr as $s) {
				if (strstr($dns,$s)) {
					$redirect = false;
				}
			}
			$row->url = str_replace('|noseo|','',$row->url);
		}
		if ($redirect) {
			header('Location: '.$row->url);
		}
	} else {
		die("Bad request");
	}
	unset($query);
}

NP_SEO.php

<?php
/**
  * BLOG:CMS: PHP/MySQL Personal Content Management System (CMS)
  * http://blogcms.com/
  * ----------------------------------------------------------------
  *
  * Copyright (C) 2003-2005 Radek HULÁN
  * http://hulan.cz/contact/
  *
  * ----------------------------------------------------------------
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
  * as published by the Free Software Foundation; either version 2
  * of the License, or (at your option) any later version.
**/
class NP_SEO extends NucleusPlugin {
 
	var $server;
	var $_item;
	var $_comment;
	var $_userlink;
	var $hide;
	var $_hide;
 
	function getName( ) { return 'SEO'; }
	function getAuthor( ) { return 'Radek HULAN'; }
	function getURL( ) { return 'http://hulan.cz/blog/'; }
	function getVersion( ) { return '1.2'; }
	function getEventList( ) { return array('PreComment','PreItem'); }
	function getDescription( ) {
	  return 'This plugin will convert all external links in article, comment, and userlinks '.
			 'into http://yourdomain/?url=http://externaldomain to achieve better page ranks';
	}
 
	function supportsFeature($what) {
		switch($what) {
			case 'SqlTablePrefix':
				return 1;
			default:
				return 0;
		}
	}
 
	function install( ) {
		$this->createOption("item", "Redirect from articles:", "yesno", "no");
		$this->createOption("userlink", "Redirect from comments:", "yesno", "yes");
		sql_query("CREATE TABLE IF NOT EXISTS ".sql_table('plug_seo')." (" .
					"`id` varchar(255) NOT NULL default '', ".
					"`url` varchar(255) default NULL, ".
					"PRIMARY KEY  (`id`) ".
					") TYPE=MyISAM COMMENT='Plugin: URL redirects';");
	}
 
	function uninstall() {
		sql_query('DROP TABLE IF EXISTS `nucleus_plug_seo`');
	}
 
	function init(){
		global $CONF;
		$this->server = $CONF['IndexURL'];
		$this->_item = ($this->getOption("item") == 'yes');
		$this->_comment = ($this->getOption("comment") == 'yes');
		$this->_userlink = ($this->getOption("userlink") == 'yes');
		$this->_hide = ($this->getOption("hide") == 'yes');
	}
 
	function event_PreItem(&$data){
		// article body and more
		$parts = array('body','more');
		// never hide articles
		$this->hide = false;
		if ($this->_item) {
			foreach ($parts as $part) {
				$this->doReplace($data['item']->$part);
			}
		}
	}
 
	function event_PreComment(&$data) {
		$this->hide = $this->_hide;
		// smart user link
		if ($this->_userlink) {
			if (strstr($data['comment']['userlinkraw'],$this->server) ||
			( !strstr($data['comment']['userlinkraw'],'http:/') &&
			!strstr($data['comment']['userlinkraw'],'https:/') &&
			!strstr($data['comment']['userlinkraw'],'ftp:/') ) ||
			strstr($data['comment']['userlinkraw'],'mailto:'));
			else {
				$data['comment']['userlinkraw'] = $this->makeURL($data['comment']['userlinkraw']);
			}
		}
	}
 
	function doReplace(&$text){
		// replace links to SEO links - with title
		$text = preg_replace_callback(
			'/<a\s+href=[\"|\'](.*?)[\"|\']\s+title=[\"|\'](.*?)[\"|\']\s*>/',
			array(&$this, 'myLink'),
			$text);
		// without a title - put URL into a title
		$text = preg_replace_callback(
			'/<a\s+href=[\"|\'](.*?)[\"|\']\s*>/',
			array(&$this, 'myLinkTitle'),
			$text);
	}
 
	function myLink($matches){
		if (strstr($matches[1],$this->server) || ( !strstr($matches[1],'http:/') && !strstr($matches[1],'https:/') &&
			!strstr($matches[1],'ftp:/') ) || strstr($matches[1],'mailto:')	) {
			return '<a href="'.$matches[1].'" title="'.$matches[2].'">';
		}
		else {
			return '<a href="'.$this->makeURL($matches[1]).'" title="'.$matches[1].' &mdash; '.$matches[2].'">';
		}
	}
 
	function myLinkTitle($matches){
		if (strstr($matches[1],$this->server) || ( !strstr($matches[1],'http:/') &&	!strstr($matches[1],'https:/') &&
			!strstr($matches[1],'ftp:/') ) || strstr($matches[1],'mailto:')	) {
			return '<a href="'.$matches[1].'">';
		}
		else {
			return '<a href="'.$this->makeURL($matches[1]).'" title="'.$matches[1].'">';
		}
	}
 
	function makeURL($url){
		if ($this->hide) {
			$url .= '|noseo|';
		}
		$md = md5($url);
		$result = sql_query("SELECT ID FROM ".sql_table("plug_seo")." WHERE id='$md'");
		if(mysql_num_rows($result) == 0) {
			sql_query("INSERT INTO ".sql_table("plug_seo")." (id,url) VALUES ('$md','".addslashes($url)."')");
		}
		return $this->server."?redirect=".$md;
	}
 
}
?>

History

  • [Version 1.2, released 2005-11-27]
    • Added table creation/deletion on install/uninstall. Works out of the box for Nucleus.
seo.txt · Last modified: 2010/03/22 19:09 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki