Location: PHPKode > projects > PhpGiggle > phpGiggle/phpgiggle.inc.php
<?

/**
    * Copyright (C) 2004 Tony Bierman
    * 
    * This program 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.
    *
*/

/*
    phpgiggle.inc.php contains main phpGiggle HTML processing engine
    see README.html for complete documentation
*/


define('phpGiggle_ABSPATH', dirname(__FILE__).'/');
require_once(phpGiggle_ABSPATH.'includes/configreader.class.php');
require_once(phpGiggle_ABSPATH.'phpgiggle-config.inc.php');
require_once(phpGiggle_ABSPATH.'includes/phpgiggle-functions.inc.php');

/*
    creates anchors based on config file key => values
    tried to make it smart, fast
*/
function giggle_autolink($string,$arr_keywords=-1,$b_noprint=0)
{

    if($arr_keywords == -1 )
    {
        $config = new Config(phpgiggle_wordsfile); 
        if(is_array($config->param))
        {
            $arr_keywords = $config->param;   
        }
        else
        {
            return $string;
        }        
    }
    
    // allow single keyword
    if(!is_array($arr_keywords)) 
    {
        $match = preg_split('/ = /', $arr_keywords);
        unset($arr_keywords);
        $arr_keywords[$match[0]]=$match[1];
    } 
    
    /*
        regular expressions
    */
    require(phpGiggle_ABSPATH.'includes/phpgiggle-regexp.inc.php');
    
    // pre-procesor - get rid of unneeded key words
    // saves time for potentially large lists
    foreach($arr_keywords as $giggle_word => $giggle_link )
    {
        $temp_word = str_replace("_"," ",$giggle_word);
        //get rid of evil chars
        if(preg_match("/".$valid_key_regexp."/i", $temp_word)) { unset($arr_keywords[$giggle_word]); }
        if(! preg_match("/$l_regexp($temp_word)$r_regexp/i", $string)) { unset($arr_keywords[$giggle_word]); }
    }
     
    if (is_array($arr_keywords)) { // if keyword array still has elements, matches have been found
            
        foreach($arr_keywords as $giggle_word => $giggle_link)  // for each keyword
        {					
            
            if(is_array($giggle_word)) { $temp_word=$giggle_word[0]; unset($giggle_word); $giggle_word=$temp_word; }
            if(is_array($giggle_link)) { $temp_link=$giggle_link[0]; unset($giggle_link); $giggle_link=$temp_link; }
            $giggle_word = str_replace("_"," ",$giggle_word);

            // matches all text between "> <" in HTML
            foreach(preg_split("/$no_tags_regexp/", $string) as $match) {
                
                // no point to continue if $match is empty or cointains garbage
                if(($match<" ") || (strlen($match) < 2) || (!preg_match("/\w/", $match)) || ( preg_match("/http\:\/\//", $match)) ) { continue; }
                
                
                // avoid some tags
                preg_match('/('.$no_tags_regexp.')('.preg_quote($match,'/').')('.$no_tags_regexp.')/U',$string, $out);
                if(preg_match('/^\<'.$avoid_tags_regexp.'/i',$out[1])) { continue; } // opening tags to avoid
                if(preg_match('/^\<\/'.$avoid_tags_regexp.'/i',$out[3])) { continue; } // closing tags to avoid

                $match_left = $out[1];
                $match_right = $out[3];
                
                if(preg_match("/$l_regexp($giggle_word)$r_regexp/i", $match, $out))
                {
                    $case_sensitive_search_word = $out[1];
                    switch ($giggle_link) {
                        // phpGiggle template
                        case function_exists("phpgiggle_".$giggle_link):
                            $giggle_function = "phpgiggle_".$giggle_link;
                            $newtext = str_replace($case_sensitive_search_word, $giggle_function($case_sensitive_search_word), $match);
                            break;
                        // highlight
                        case (eregi("$valid_html_color", $giggle_link)?$giggle_link:!$giggle_link):
                            $newtext = str_replace($case_sensitive_search_word,"<span class=\"phpGiggle\" title=\"phpGiggle Highlighter !\" style=\"background-color: ".
                                $giggle_link.";\">$case_sensitive_search_word</span>", $match);     
                            break;
                        // just link
                        case (eregi("$valid_url_regexp", $giggle_link)?$giggle_link:!$giggle_link):
                            $newtext = str_replace($case_sensitive_search_word,"<a ".phpgiggle_target." class=\"phpGiggle\" title=\"phpGiggle it!\" href=\"phpGiggle?".
                                $giggle_link ."\">".$case_sensitive_search_word."</a>", $match);
                            break;
                        // Acronym or just alt-text
                        default:
                            $newtext = str_replace($case_sensitive_search_word,'<abbr class="phpGiggle" title="'.str_replace("'", "", $giggle_link).'">'.$case_sensitive_search_word.'</abbr>', $match);    
                    }                      

                    $string = str_replace($match_left.$match.$match_right, $match_left.$newtext.$match_right, $string); // replace $match with $newtext in $string

                } // if keyword found
            } // for each HTML match
        }  // for each keyword	
    } // if config has elements
    unset($arr_keywords);
    if($b_noprint)
    {
            return $string;
    }
    else
    {
            echo $string;
    }
}

function giggle_pivot ($text)
{ 
    return giggle_autolink($text,-1,1);   
}

function giggle_smarty ($text, &$smarty)
{
    return giggle_autolink($text,-1,1);
}

function giggle_wordpress ($text)
{ 
    return giggle_autolink($text,-1,1);   
}
 
?>
Return current item: PhpGiggle