, modified Stevie's original code on 2005-11-14. Please read comments identified with BDG for more information. 2005-11-14 BDG * sh-autolink.php (shautolink): Modified method to perform string replacements on text not in HTML anchor tags (i.e. stuff). (autolink_search) and (autolink_replace): New functions. License information Copyright 2005 Released under the GPL license http://www.gnu.org/licenses/gpl.txt This file is a plugin for WordPress WordPress 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 */ function shautolink($strText) { global $table_prefix, $wpdb; $strSHAutolink_Table = $table_prefix . "shautolink"; $strSQL = "SELECT id, url, keyword, protocol, target FROM $strSHAutolink_Table "; $rows = $wpdb->get_results ($strSQL); if (is_array($rows)): $strText = " $strText "; //2005-11-14, BDG: Modified the foreach loop to store DB values in arrays. $urls = array(); $keywords = array(); $targets = array(); $protocols = array(); foreach ($rows as $row) { $urls[] = $row->url; $keywords[] = $row->keyword; $targets[] = $row->target; $protocols[] = $row ->protocol; } //2005-11-14, BDG: Call our two new methods to build search and repalce strings $search_strings = array_map('autolink_search', $keywords); $replace_strings = array_map('autolink_replace', $urls, $keywords, $targets, $protocols); //2005-11-14, BDG: Replaced Stevie's preg_replace calls with the following code... $newcontent = ""; $last = 0; $i = 0; while(($i = strpos($strText, " $i){ $char_count = ($last - $i); } else { $char_count = ($i - $last); } $newcontent .= preg_replace($search_strings, $replace_strings, substr($strText, $last, $char_count)); $last = (strpos($strText, "", $i)+4); $char_count = $last - $i; $newcontent .= substr($strText, $i, $char_count); $i = $last; } $newcontent .= preg_replace($search_strings, $replace_strings, substr($strText, $last, (strlen($strText)-$i))); //2005-11-14, BDG: ...added code ends here. return trim($newcontent); else: return $strText; endif; } //2005-11-14, BDG: Added this method for use with array_map, to build search strings function autolink_search($keyword){ return '#\b' . $keyword .'\b#i'; } //2005-11-14, BDG: Added this method for use with array_map, to build replace strings function autolink_replace($url, $keyword, $target, $proto){ return '' . $keyword . ''; } function shautolink_chkkeyword($strKeyword, $intID = 0) { global $table_prefix, $wpdb; $strSHAutolink_Table = $table_prefix . "shautolink"; $strSQL = "SELECT count(*) AS kcount FROM $strSHAutolink_Table WHERE keyword='$strKeyword'"; if($intID != 0): $strSQL = $strSQL . " AND NOT id=$intID"; endif; $resultCount = $wpdb->get_row($strSQL); $intCount = $resultCount->kcount; if($intCount > 0): return true; else: return false; endif; } function shautolink_chkfields($strKeyword, $strUrl) { if($_POST["shautolink_keyword"] == "" || $_POST["shautolink_url" ] == ""): $bitError = 1; endif; return $bitError; } function shautolink_specialchars($strString, $bolSpecialChars) { if($bolSpecialChars == 1): return htmlentities($strString); else: return html_entity_decode($strString); endif; } function shautolink_decodespecialchars($strString) { return str_replace("&","&",$strString); } function shautolink_convertcbsc($strString) { if($strString == "on"): return 1; else: return 0; endif; } function shautolink_chkurl($strURL) { $intCheck = preg_match('^(ht|f)tp(s?)\:\/\/[a-zA-Z0-9\-\._]+(\.[a-zA-Z0-9\-\._]+){2,}(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\?=\$#_]*)?$^', $strURL, $arrRegs); if ($arrRegs[0] == $strURL): return 1; else: return 0; endif; } function shautolink_chkmail($strEmail) { ereg('^([a-zA-Z0-9_\-])+(\.([a-zA-Z0-9_\-])+)*@((\[(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5]))\]))|((([a-zA-Z0-9])+(([\-])+([a-zA-Z0-9])+)*\.)+([a-zA-Z])+(([\-])+([a-zA-Z0-9])+)*))$',$strEmail, $arrRegs); if($arrRegs[0] == $strEmail): return 1; else: return 0; endif; } function shautolink_resetgetvars() { unset($GLOBALS['_GET']["shautolink_action"]); unset($GLOBALS['_GET']["shautolink_id"]); } function shautolink_adminpage() { global $table_prefix, $wpdb; $strSHAutolink_Table = $table_prefix . "shautolink"; $arrTargets = array('New Window' => '_blank', 'Current Window' => '_self', 'Parent Frame' => '_parent', 'Top Frame' => '_top'); $arrProtocols = array('HTTP' => 'http://', 'HTTPS' => 'https://', 'FTP' => 'ftp://', 'E-Mail' => 'mailto:'); if(isset($_REQUEST["shautolink_action"])): if($_POST["shautolink_action"] == "Save"): $strSaveShautolinkKeyword = $_POST["shautolink_keyword"]; $strSaveShautolinkTarget = $_POST["shautolink_target"]; $strSaveShautolinkUrl = $_POST["shautolink_url"]; $bolSaveShautolinkSpecialchars = shautolink_convertcbsc($_POST["shautolink_specialchars"]); $strSaveShautolinkProtocol = $_POST["shautolink_protocol"]; elseif($_POST["shautolink_action"] == "Update"): $strUpdateShautolinkID = $_POST["shautolink_id"]; $strUpdateShautolinkKeyword = $_POST["shautolink_keyword"]; $strUpdateShautolinkTarget = $_POST["shautolink_target"]; $strUpdateShautolinkUrl = $_POST["shautolink_url"]; $bolUpdateShautolinkSpecialchars = shautolink_convertcbsc($_POST["shautolink_specialchars"]); $strUpdateShautolinkProtocol = $_POST["shautolink_protocol"]; $bolUpdate = true; elseif($_GET["shautolink_action"] == "edit"): $strSQL = "SELECT id, url, keyword, protocol, target, specialchars FROM $strSHAutolink_Table WHERE id=".$_GET["shautolink_id"]; $resultEdit = $wpdb->get_row($strSQL); $strUpdateShautolinkID = $_GET["shautolink_id"]; $strUpdateShautolinkKeyword = $resultEdit->keyword; $strUpdateShautolinkTarget = $resultEdit->target; $strUpdateShautolinkUrl = $resultEdit->url; $bolUpdateShautolinkSpecialchars = $resultEdit->specialchars; $strUpdateShautolinkProtocol = $resultEdit->protocol; $bolUpdate = true; shautolink_resetgetvars(); endif; endif; if($_POST["shautolink_action"] == "Save"): if(shautolink_chkfields($strSaveShautolinkKeyword, $_POST["shautolink_url"])==1): $strMessage = "Please fill out all fields."; elseif(shautolink_chkkeyword(shautolink_specialchars($strSaveShautolinkKeyword, $bolSaveShautolinkSpecialchars)) == true): $strMessage = "Keyword \"". $strSaveShautolinkKeyword . "\" already defined."; elseif($strSaveShautolinkProtocol == "mailto:" && shautolink_chkmail($_POST["shautolink_url"]) == 0): $strMessage = $_POST["shautolink_url"] . " is not a valid email-adress"; elseif($strSaveShautolinkProtocol != "mailto:" && shautolink_chkurl($strSaveShautolinkProtocol . $_POST["shautolink_url"]) == 0): $strMessage = $strSaveShautolinkProtocol . $_POST["shautolink_url"] . " is not a valid URL."; endif; if ($strMessage == ""): $strSQL = "INSERT INTO $strSHAutolink_Table (url, keyword, protocol, target, specialchars) VALUES('".$_POST["shautolink_url"]."','".shautolink_specialchars($_POST["shautolink_keyword"],$bolSaveShautolinkSpecialchars)."','$strSaveShautolinkProtocol', '".$_POST["shautolink_target"]."',$bolSaveShautolinkSpecialchars)"; $wpdb->query($strSQL); $strMessage = "Keyword added"; $strSaveShautolinkKeyword = ""; $strSaveShautolinkTarget = ""; $strSaveShautolinkUrl = ""; $bolSaveShautolinkSpecialchars = ""; $strSaveShautolinkProtocol = ""; endif; shautolink_resetgetvars(); elseif($_POST["shautolink_action"] == "Update"): if(shautolink_chkfields($strUpdateShautolinkKeyword, $_POST["shautolink_url"])==1): $strMessage = "Please fill out all fields."; elseif(shautolink_chkkeyword(shautolink_specialchars($strUpdateShautolinkKeyword, $bolUpdateShautolinkSpecialchars),$strUpdateShautolinkID) == true): $strMessage = "Keyword \"". $strUpdateShautolinkKeyword . "\" already defined."; elseif($strUpdateShautolinkProtocol == "mailto:" && shautolink_chkmail($_POST["shautolink_url"]) == 0): $strMessage = $_POST["shautolink_url"] . " is not a valid email-adress"; elseif($strUpdateShautolinkProtocol != "mailto:" && shautolink_chkurl($strUpdateShautolinkProtocol . $_POST["shautolink_url"]) == 0): $strMessage = $strUpdateShautolinkProtocol . $_POST["shautolink_url"] . " is not a valid URL."; endif; if ($strMessage == ""): $strSQL = "UPDATE $strSHAutolink_Table SET protocol='$strUpdateShautolinkProtocol', url='".$_POST["shautolink_url"]."', keyword='".shautolink_specialchars($_POST["shautolink_keyword"], $bolUpdateShautolinkSpecialchars)."', target='".$_POST["shautolink_target"]."', specialchars=$bolUpdateShautolinkSpecialchars WHERE id=" . $_POST["shautolink_id"]; $strMessage = "Keyword updated"; $bolUpdate = false; $wpdb->query($strSQL); shautolink_resetgetvars(); endif; elseif($_GET["shautolink_action"] == "delete"): $strSQL = "DELETE FROM $strSHAutolink_Table WHERE id=" . $_GET["shautolink_id"]; $wpdb->query($strSQL); $strMessage = "Keyword deleted"; shautolink_resetgetvars(); endif; ?>
} function shautolink_add_adminpage() { add_management_page ("SH-Autolink", "SH-Autolink", 8, __FILE__, 'shautolink_adminpage'); } function shautolink_install() { global $table_prefix, $wpdb; $strSHAutolink_Table = $table_prefix . "shautolink"; $strSQL = "SHOW TABLES LIKE '%shautolink'"; $result = $wpdb->query($strSQL); if ($result == 0): $strSQL = " CREATE TABLE ".$strSHAutolink_Table."( id int(11) NOT NULL auto_increment, keyword varchar(255) default NULL, protocol varchar(20) default NULL, url varchar(255) default NULL, target varchar(20) default NULL, specialchars int(11) default NULL, PRIMARY KEY (`id`)) ENGINE=MyISAM"; $wpdb->query($strSQL); endif; } $SCRIPT_NAME = $_SERVER['SCRIPT_NAME']; if (isset($_SERVER['SCRIPT_URL'])) { $SCRIPT_NAME = $_SERVER['SCRIPT_URL']; } $bitInstall = (basename($SCRIPT_NAME) == 'plugins.php' && isset($_GET['activate']));; if ($bitInstall) { shautolink_install(); } add_action ('admin_menu', 'shautolink_add_adminpage'); add_filter('the_content', 'shautolink', 1); add_filter('comment_text', 'shautolink', 1); ?>