<?php
/*
Plugin Name: WordPress AutoLink
Plugin URI: http://rudd-o.com/projects/wp-autolink/
Description: Lets you break free from long, absolute URLs in your blog. <a href="http://rudd-o.com/projects/wp-autolink/wp-autolink-usage/">Manual</a>.
Version: 0.1.0
Author: Manuel Amador (Rudd-O)
Author URI: http://rudd-o.com/
*/

/*  Copyright 2005  Manuel Amador

Derivative work from bsuite, written by Casey Bisson

    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.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

/* TODO
maybe blog: para paginas a partir del blog, y home: para paginas a partir del home

make post: work on embedded javascript events
*/

$autolink_recognized_attributes = array("href","src","cite","onclick","onfocus","onblur","onmouseover","onmouseout");

function filter_autolink_callback_attribute($attribute) {
	global $autolink_recognized_attributes;

	$attribute = $attribute[0];


	preg_match("/(\w+?)=(\")(.*?)(\")(\/|)/s",$attribute,$matches);
	if (!$matches) preg_match("/(\w+?)=(')(.*?)(')(\/|)/s",$attribute,$matches); //because attr could be using single quotes

	$attrname = $matches[1];
	$openingquote = $matches[2];
	$attrvalue = $matches[3];
	$closingquote = $matches[4];
	$endslash = $matches[5];

	if (!in_array(strtolower($attrname),$autolink_recognized_attributes)) return $attribute;

	if (substr($attrvalue,0,strlen('post:')) == "post:") {
		/* we have a post link */
		$handle = substr($attrvalue,strlen('post:'),strlen($attrvalue));
		
		$splits = explode("#",$handle,2);
		$handle = $splits[0];
		if (count($splits) == 2) { /* there WAS an anchor */ $anchor = $splits[1];}
		else { $anchor = null; }
		
		global $wpdb;
		if (intval($handle)."" === $handle) { /* we have a numeric post link */
			$sql = "SELECT ID FROM $wpdb->posts WHERE ID=$handle limit 1;";
		} else { /* we have a text post link */
			$handle = $wpdb->escape($handle);
			$sql = "SELECT ID FROM $wpdb->posts WHERE post_name='$handle' limit 1;";
		}
		/* to verify its existence in the database */
		$post_id = $wpdb->get_var( $sql );
		if ($post_id) {
			if ($anchor === null) { $permalink = get_permalink($post_id); }
			else { $permalink = get_permalink($post_id) . "#" . $anchor ; }
			$attrvalue = apply_filters('the_permalink', $permalink );
			if ($attrname == "href") { /* we only supply the title if it's an a href link */
				$title = apply_filters('the_title', get_the_title($post_id));
				$endslash = ' title="' . $title . '"' . $endslash;
			}
		}
	} elseif (substr($attrvalue,0,strlen('content:')) == "content:") {
		/* we have a content link */
		$handle = substr($attrvalue,strlen('content:'),strlen($attrvalue));
		$attrvalue = get_settings("fileupload_url") . "/" . $handle;
	} elseif (substr(strtolower($attrname),0,strlen('on')) == "on") {
		$attrvalue = preg_replace("/'content:/s", "'" . get_settings("fileupload_url") . "/",$attrvalue);
		$attrvalue = preg_replace("/\"content:/s", "\"" . get_settings("fileupload_url") . "/",$attrvalue);
	}

	$attribute = "{$attrname}={$openingquote}{$attrvalue}{$closingquote}{$endslash}";
	return $attribute;
}

function filter_autolink_callback($matches) {
	$tag_name = $matches[1];
	$tag_contents = $matches[2];
	$new_tag_contents = preg_replace_callback("/\w+?=(\".*?\"|'.*?')/s",filter_autolink_callback_attribute,$tag_contents);
	return "<$tag_name $new_tag_contents>";
}

function filter_autolink($content) {
	$regexp = '/<(\w+?)\s+?(.*?)>/s';
	$content = preg_replace_callback($regexp,filter_autolink_callback,$content);
	return $content;
}


/* reverse for replacement of absolute URIs into content:-based ones */

/* this function needs to use a doublebackslash because for some reason the content_pre gives a string with single and double quotes escaped.  who understands that! */

function filter_autolink_reverse_callback_attribute($attribute) {
	global $autolink_recognized_attributes;

	$attribute = $attribute[0];

	preg_match("/(\w+?)=(\\\\\")(.*?)(\\\\\")(\/|)/s",$attribute,$matches);
	if (!$matches) preg_match("/(\w+?)=(\\\')(.*?)(\\\')(\/|)/s",$attribute,$matches); //because attr could be using single quotes

	$attrname = $matches[1];
	$openingquote = $matches[2];
	$attrvalue = $matches[3];
	$closingquote = $matches[4];
	$endslash = $matches[5];

	if (!in_array(strtolower($attrname),$autolink_recognized_attributes)) return $attribute;

	$prefix = get_settings("fileupload_url") . "/";
	if (substr($attrvalue,0,strlen($prefix)) == $prefix) {
		/* we have a link in need of replacement for content */
		$handle = substr($attrvalue,strlen($prefix),strlen($attrvalue));
		$attrvalue = "content:" . $handle;
	} elseif (substr(strtolower($attrname),0,strlen('on')) == "on") {
		$attrvalue = str_replace($prefix , "content:", $attrvalue);
		$attrvalue = str_replace($prefix , "content:", $attrvalue);
	}

	$attribute = "{$attrname}={$openingquote}{$attrvalue}{$closingquote}{$endslash}";
	return $attribute;
}

/* this function needs to use a doublebackslash because for some reason the content_pre gives a string with single and double quotes escaped.  who understands that! */
function filter_autolink_reverse_callback($matches) {
	$tag_name = $matches[1];
	$tag_contents = $matches[2];
	$new_tag_contents = preg_replace_callback("/\w+?=(\\\\\".*?\\\\\"|\\\'.*?\\\')/s",filter_autolink_reverse_callback_attribute,$tag_contents);
	return "<$tag_name $new_tag_contents>";
}

function filter_autolink_reverse($content) {
	$regexp = '/<(\w+?)\s+?(.*?)>/s';
	$content = preg_replace_callback($regexp,filter_autolink_reverse_callback,$content);
	return $content;
}

/* copied from markdown */
add_filter('the_content', 'filter_autolink');
add_filter('the_excerpt', 'filter_autolink');
add_filter('the_excerpt_rss', 'filter_autolink');
add_filter('content_save_pre', 'filter_autolink_reverse',0);
?>