« Back to the NP_AutoLink description

<?
/*
  0.1 - initial release
  0.2 - new option: rewrite mail addresses with [at] and [dot]
  0.3 - userdefined placeholders for [at] and [dot]
  0.4 - new option: links can be open in new window (default: off)
  0.5 - support for SqlTablePrefix
*/
 
class NP_AutoLink extends NucleusPlugin {
 
   function getName() { return 'AutoLink'; }
   function getAuthor()  { return 'Kai Greve'; }
   function getURL()  { return 'http://kgblog.de/'; }
   function getVersion() { return '0.5'; }
   function getDescription() {
     return 'Automatically creates links for internet and mail addresses';
   }
 
   function install() {
     $this->createOption('InternetAddress','Automatically create links for internet addresses ?','yesno','yes');
     $this->createOption('NewWindow','Open links in a new window?','yesno','no');
     $this->createOption('MailAddress','Automatically create links for mail addresses ?','yesno','yes');
     $this->createOption('RewriteMailAddress','Rewrite mail addresses with [at] and [dot]?','yesno','yes');
     $this->createOption('at','Placeholder for @','text','[at]');
     $this->createOption('dot','Placeholder for .','text','[dot]');
   }
 
   function getEventList() {
     return array('PreItem', 'PreComment');
   }
 
   function Treatment($_text) {
     global $CONF, $blog;
 
     if ($this->getOption('NewWindow') == 'yes') {
         $nw="onclick=\"javascript:window.open(this.href, '_blank'); return false;\"";
     }
 
     if ($this->getOption('InternetAddress') == 'yes') {
         $_text = preg_replace('/(\s)([http|https|ftp|file]+:\/\/[a-zA-Z0-9_?=&%;+-.\/]*)/si','\1<a href="\2" '.$nw.'>\2</a>',$_text);
         $_text = preg_replace('/(\s)(www\.[a-zA-Z0-9_?=&%;+-.\/]*)/si','\1<a href="http://\2" '.$nw.'>\2</a>',$_text);
     }
 
     $at = $this->getOption('at');
     $dot = $this->getOption('dot');
 
     if ($this->getOption('MailAddress') == 'yes') {
       if ($this->getOption('RewriteMailAddress') == 'no') {
         $_text = preg_replace('/(\s)([a-zA-Z0-9\._-]+@[a-zA-Z0-9\._-]+\.[a-zA-Z]{2,5})/s','\1<a href="mailto:\2">\2</a>',$_text);
       }
       else {
         $_text = preg_replace('/(\s)([a-zA-Z0-9\._-]+)@([a-zA-Z0-9\._-]+)\.([a-zA-Z]{2,5})/s','\1<a href="mailto:\2@\3.\4">\2'.$at.'\3'.$dot.'\4</a>',$_text);
       }
     }
 
     if ($this->getOption('MailAddress') == 'no' && $this->getOption('RewriteMailAddress') == 'yes'){
              $_text = preg_replace('/(\s)([a-zA-Z0-9\._-]+)@([a-zA-Z0-9\._-]+)\.([a-zA-Z]{2,5})/s','\1\2'.$at.'\3'.$dot.'\4',$_text);
     }
 
     return $_text;
   }
 
   function event_PreItem($_data) {
     $_data[item]->body = $this->Treatment($_data[item]->body);
     $_data[item]->more = $this->Treatment($_data[item]->more);
   }
 
   function event_PreComment($_data) {
     $_data['comment']['body'] = $this->Treatment($_data['comment']['body']);
   }
 
   function supportsFeature ($what)
   {
       switch ($what)
       {
           case 'SqlTablePrefix':
               return 1;
           default:
               return 0;
       }
   }
}
?>
np_autolink_code.txt · Last modified: 2006/07/05 13:03 (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