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
*/
define('TG_SITE_URL', trailingslashit(get_settings('siteurl')));
define('TG_PATTERN_ANCHOR', "/<a[^>]*?href\s?=\s?[\"|'](.+?)[\"|'][^>]*>/i");
class TitleGrabber {
var $site_host;
var $pattern_anchor;
var $wpdb;
function TitleGrabber($site_host, $pattern_anchor, &$wpdb) {
$this->site_host = $site_host;
$this->pattern_anchor = $pattern_anchor;
$this->wpdb = & $wpdb;
add_filter('publish_post', array(&$this, 'changeLinks'));
}
function changeLinks($id) {
$text = $this->wpdb->get_var("SELECT post_content FROM {$this->wpdb->posts} WHERE ID = $id");
$newText = addslashes(preg_replace_callback($this->pattern_anchor, array(&$this, 'changeLink'), stripslashes($text)));
$this->wpdb->query("UPDATE {$this->wpdb->posts} SET post_content = '$newText' WHERE ID = $id");
return $id;
}
function grabTitle($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, 8);
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 changeLink($link) {
if (!stristr($link[1], 'http')) {
$link[1] = $this->site_host . $link[1];
}
return str_replace(">", ' title="' . $this->grabTitle($link[1]) . '">', $link[0]);
}
}
$title_grabber = & new TitleGrabber(TG_SITE_URL, TG_PATTERN_ANCHOR, $wpdb);
?>