Location: PHPKode > projects > ProNuke. Postnuke To Oracle > postnuke/html/includes/autolinkinc.php
<?php
// $Id: autolinkinc.php,v 1.24 2001/12/03 23:03:44 gregorrothfuss Exp $
// ----------------------------------------------------------------------
// Autolink Tool for Post-Nuke
// Version 0.45b
// Copyright (C) 2001 by Jens Wallenhort (hide@address.com).
// http://www.nusite.de/
// ----------------------------------------------------------------------
// LICENSE
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License (GPL)
// 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.
//
// To read the license please visit http://www.gnu.org/copyleft/gpl.html
// ----------------------------------------------------------------------
// Original Author of file: Jens Wallenhorst (hide@address.com)
// Purpose of file: "include file" for autolink
// ----------------------------------------------------------------------
// Changelog
// 2001-09-29 slw - modified into a php4 class and resolved multilinking
//                  issue (would attempt to link a link!)
// 2001-09-27 slw - modified to correctly link words with punctuation
// 2001-09-14 jw - added all code for sidebar linking 
// 2001-09-12 slw - replaced $1 $2 $3 with \\1 \\2 \\3 
// 2001-09-07  jw - code cleanup thanks to MYSQL hint from SLW and Bjorn Sodergren
// 2001-09-07  jw - prepared for sidebar linking
//               jw - removed pntable und tablesvars definitions
// 2001-08-24  jw - added title and comment entries as title-tag
//               jw - added pntable und tablesvars definitions
// 2001-08-20  jw - first public version
// ----------------------------------------------------------------------


// replaces words with an word-url combo if an entry is found in the database

// 2001-09-07 added $sidebar parameter to linkIt function
// takes textstring input and calls the lookup procedure

class autolinker {
    var $linksarray= array();
      
    //callback function used by preg_replace_callback
    //takes an array, converts it to a string, then adds this string
    //into a new element at the end of $linksarray
    function substarray($inputtext){

        $this->linksarray[]=implode("",$inputtext);
        end($this->linksarray);
        $outtext= ".\$linksarray[".key($this->linksarray)."].";
        return $outtext;
    }

    //callback function used by preg_replace_callback
    //takes array, converts it to a string then replaces all instances
    //of .linksarray[\d]. with the contents of $linksarray[\d]
    function evalarray($inputtext){

        $inputtext=implode("",$inputtext);
        $keytxt=preg_replace("/(\.\Wlinksarray\[)(\d+)(\])\./i","\\2",$inputtext);
        $keynum=intval($keytxt);
        return $this->linksarray[$keynum];
    }
      
    //this is for those of you who dont have php4 4.05 onwards!
    //takes a text string and searches it for any link tags that may be present
    //takes these tags and add them to an array, then strips the tags from the $string
    //and replaces them with the array reference and returns resulting string
    function substlinks($text){
          
        $success=preg_match_all("/(<a href=.*<\/a>)/iU",$text,$out,PREG_PATTERN_ORDER);
        if ($success){
            $out[0]=array_unique($out[0]);
            foreach ($out[0] as $link){
                $this->linksarray[]=$link;
                end($this->linksarray);
                $subst=".linksarray[".key($this->linksarray)."].";
                $text=preg_replace("/(".preg_quote($link,"/").")/U",$subst,$text);
            }
        }
        return $text;
    }
      
    //this is for those of you who dont have php4 4.05 onwards!
    //takes a text string and searches it for any references created by substlinks()
    //and replaces them with their original text
    function substref($text){

        $success=preg_match_all("/(\.\linksarray\[)(\d+)(\])\./",$text,$out, PREG_PATTERN_ORDER);
        if ($success){
            foreach ($out[0] as $ref){
                $keytxt=preg_replace("/(\.\linksarray\[)(\d+)(\])\./","\\2",$ref);
                $keynum=intval($keytxt);
                $subst=$this->linksarray[$keynum];
                $text=preg_replace("/(".preg_quote($ref).")/U",$subst,$text);
            }
        }
        return $text;
    }

    //actually does the bulk ofthe work - turns text that matches
    //keywords in teh database into linked text
    function linkIt($textinput, $sidebar = false) {
        global $pntable, $dbconn, $boxlink;
        // added to solve SF Bug rept #464887
        // sluxford (sam or slw) - 27/09/01
        //
        // firstly strip all HTML from textinput

        $nopunct = strip_tags($textinput, '<br>');
        $nopunct = preg_replace("/(<br>)|(<br\s+\\/>)/i"," ",$nopunct);

        // now strip out all punctuation that is not part of a word
        // ie - everything except word.word or word-word or even
        //      hide@address.com
        $nopunct = preg_replace("/(\W{2,})|(?<!\w)\W+(?!\w+)|(\W\z)/"," ",$nopunct);
        
        //remove trailing spaces from string
        $nopunct = preg_replace("/\s+\z/","",$nopunct);
        //
        // end of added section - sluxford(27/09/01)

        // puts single words into an array
        $linkwords =  preg_split("/[\s]+/",$nopunct);

        // filters out doubles
        $linkwords = array_unique ($linkwords );
         
        // replaces all instances of ' with '' for db query safety
        $linkwords = preg_replace ("/'/", "''", $linkwords);

        $column = &$pntable['autolinks_column'];

        //puts array elements in a string
        $linkwords = implode("%' OR $column[keyword] LIKE '%", $linkwords);

        $query = "SELECT $column[lid], $column[keyword], $column[title], $column[url], $column[comment]
                  FROM  $pntable[autolinks]
                  WHERE $column[keyword] LIKE '%{$linkwords}%'";
        $result = $dbconn->Execute($query);
          
        // FTO Check database error
         if (!$result) {
               PN_DBMsgError($dbconn, __FILE__, __LINE__, "An error ocurred");
               die();
          }

        // do this once to remove and store any user added links that may exist in the text
        // use:
        // $textinput=preg_replace_callback("/<a href=.*<\/a>/iU",array(&$this, "substarray"),$textinput);
        // instead of the following line if you are running php 4.05 or higher:
        $textinput = $this->substlinks($textinput);
          
        //loop through all the keywords - create the link, then remove and store it
        //for substitution later
        while(!$result->EOF) {
            list($lid, $keyword,$title,$url,$comment) = $result->fields;
//FTO list instead GetrowAssoc
//FTO            $data = $result->GetRowAssoc(false);
            $result->MoveNext();
//FTO            $comment = $data['comment']; // get the comment
            if ($comment != ""){ $comment = "| ".$comment;} // get the comment
//FTO                $hinttext = $data['title']." ".$comment;
                $hinttext = $title." ".$comment;
                // modified to resolve SF Bug rept #464887
                // used \W instead of \s and also look for start and end of string

//FTO                $linkword = "/(\W|\A)(".$data['keyword'].")(\W|\z)/i";
//FTO                $textinput = preg_replace($linkword, "\\1<a href=\"".$data['url']."\" title=\"$hinttext\" target=\"_blank\">\\2</a>\\3", $textinput);
                $linkword = "/(\W|\A)(".$keyword.")(\W|\z)/i";
                $textinput = preg_replace($linkword, "\\1<a href=\"".$url."\" title=\"$hinttext\" target=\"_blank\">\\2</a>\\3", $textinput);


                // modified to resolve SF Bug rept #464887
                // we need to go through and strip out all the <a href= ...</a> links that we have just added,
                // place them in an array and replace their instance in the text with the array reference.
                // use:
                // $textinput=preg_replace_callback("/<a href=.*<\/a>/iU",array(&$this, "substarray"),$textinput);
                // instead of the following line if you are running php 4.05 or higher:
                $textinput=$this->substlinks($textinput);

                if ($sidebar == true) {
//FTO                    $boxlink .= "<strong><big>&middot;</big></strong>&nbsp;<a class=\"pn-normal\" href=\"".$data['url']."\" title=\"$hinttext\"  target=\"_blank\">".$data['title']."</a><br>\n";
                    $boxlink .= "<strong><big>&middot;</big></strong>&nbsp;<a class=\"pn-normal\" href=\"".$url."\" title=\"$hinttext\"  target=\"_blank\">".$title."</a><br>\n";
                }//if

        }//while
          
        // now we need to parse $textinput to evaluate all the array refs that are present
        // before returning $textinput
        // sluxford - 29/09/01 SF BUG #464887
        // use:
        // $textinput=preg_replace_callback("/(\.\Wlinksarray\[)(\d+)(\])\./i",array(&$this, "evalarray"),$textinput);
        // instead of the following line if you are running php 4.05 or higher:
        $textinput=$this->substref($textinput);
          
        // returns the "url'ed" text.
        return $textinput;
    }
}
?>
Return current item: ProNuke. Postnuke To Oracle