rendered paste body<?php
/*
Plugin Name: Title Grabber
Plugin URI: http://preow.com/plugins/title-grabber/
Description: Adds a title attribute to links that don't have one, grabs title's from the page the link goes to.
Author: Nick Dienstbier
Version: 1.3
Author URI: http://preow.com
*/
if ( !defined('site_url') ) {
define('site_url', trailingslashit(get_settings('siteurl')));
}
if ( !defined('site_path') ) {
define('site_path', str_replace(('http://' . $_SERVER['HTTP_HOST']), '', site_url));
}
if ( !defined('site_host') ) {
if ( preg_match("/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/", $_SERVER['HTTP_HOST']) || ( strpos($_SERVER['HTTP_HOST'], '.') === false )) {
define('site_host', $_SERVER['HTTP_HOST']);
}
else {
preg_match("/([^\.]+\.)*([^\.]+\.[^\.]+)/", $_SERVER['HTTP_HOST'], $out);
define('site_host', end($out));
}
}
if ( !defined('pattern_anchor') ) {
define('pattern_anchor', "/<a[^>]*?href=(\".+?\"|'.+?')[^>]*>/");
}
class title_grabber {
function title_grabber() {
$this->path = str_replace(str_replace("\\", "/", ABSPATH), '', str_replace("\\", "/", dirname(__FILE__)));
add_filter('publish_post', array(&$this, 'change_links'));
}
function change_links($id) {
global $wpdb;
$text = $wpdb->get_var("SELECT post_content FROM $wpdb->posts WHERE ID = $id");
$newText = addslashes(preg_replace_callback(pattern_anchor, array(&$this, 'change_link'), stripslashes($text)));
$wpdb->query("UPDATE $wpdb->posts SET post_content = '$newText' WHERE ID = $id");
return $id;
}
function grab_title($url) {
$filetypes = array(".zip", ".png", ".gif", ".jpg", ".rar", ".exe", ".txt", ".pdf", ".psd");
if(strpos($url, $filetypes)) {
return $url;
}
$pURL = parse_url($url);
if (empty($pURL['host'])) {
return $url;
}
if (empty($pURL['path'])) {
$pURL['path'] = '/';
}
if (empty($pURL['query'])) {
$document = $pURL['path'];
} else {
$document = $pURL['path'] . '?' . $pURL['query'];
}
$fp = fsockopen($pURL['host'], 80, $errno, $errstr, 30);
if (!$fp) {
return $url;
} else {
$out = "GET $document HTTP/1.1\r\n";
$out .= "Host: {$pURL['host']}\r\n";
$out .= "Connection: Close\r\n\r\n";
$received = '';
fwrite($fp, $out);
while (!feof($fp)) {
$received .= fgets($fp, 128);
}
fclose($fp);
}
if (!preg_match('~<title>(.*)</title>~isu', $received, $matches)) {
return $url;
}
return trim(strip_tags($matches[1]));
}
function change_link($link) {
$href = trim($link[1], "\"'");
if (strpos($href, "http://") !== false && strpos($href, site_host) === false) {
$newlink = str_replace(">", ' title="'.$this->grab_title($href).'">', $link[0]);
}
else { $newlink = $link[0]; }
return $newlink;
}
}
$title_grabber =& new title_grabber;
?>