2008-02-01 18:09:53 +00:00
< ? php
/*
2009-11-17 10:46:35 +00:00
* e107 website system
*
2010-01-16 20:49:55 +00:00
* Copyright ( C ) 2008 - 2010 e107 Inc ( e107 . org )
2009-11-17 10:46:35 +00:00
* Released under the terms and conditions of the
* GNU General Public License ( http :// www . gnu . org / licenses / gpl . txt )
*
*
*
* $Source : / cvs_backup / e107_0 . 8 / e107_plugins / linkwords / e_tohtml . php , v $
2010-01-16 20:49:55 +00:00
* $Revision : 1.11 $
* $Date : 2010 - 01 - 16 20 : 49 : 53 $
2010-01-02 22:57:41 +00:00
* $Author : e107steved $
2009-11-17 10:46:35 +00:00
*/
2008-02-01 18:09:53 +00:00
2010-01-02 22:57:41 +00:00
/**
* e107 Linkword plugin
*
* @ package e107_plugins
* @ subpackage linkwords
2010-01-16 20:49:55 +00:00
* @ version $Id : e_tohtml . php , v 1.11 2010 - 01 - 16 20 : 49 : 53 e107steved Exp $ ;
2010-01-02 22:57:41 +00:00
*
* 'Hook' page
* The class is 'hooked' by the parser , to add linkword capability to any context where its enabled .
2010-01-16 20:49:55 +00:00
*
* @ todo Link to capability for clever display options on tooltips
2010-01-02 22:57:41 +00:00
*/
2008-02-01 18:09:53 +00:00
if ( ! defined ( 'e107_INIT' )) { exit ; }
2009-10-22 13:00:37 +00:00
// if (!plugInstalled('linkwords')) exit; // This will completely break a site during upgrades.
2008-02-01 18:09:53 +00:00
2009-02-03 11:59:12 +00:00
define ( 'LW_CACHE_ENABLE' , TRUE );
2008-02-01 18:09:53 +00:00
class e_tohtml_linkwords
{
var $lw_enabled = FALSE ; // Default to disabled to start
2008-12-07 21:55:02 +00:00
var $lwAjaxEnabled = FALSE ; // Adds in Ajax-compatible links
var $utfMode = '' ; // Flag to enable utf-8 on regex
var $word_list = array (); // List of link words/phrases
var $link_list = array (); // Corresponding list of links to apply
var $ext_list = array (); // Flags to determine 'open in new window' for link
var $tip_list = array (); // Store for tooltips
2008-12-13 18:04:52 +00:00
var $LinkID = array (); // Unique ID for each linkword
2008-12-07 21:55:02 +00:00
var $area_opts = array (); // Process flags for the various contexts
2008-02-01 18:09:53 +00:00
var $block_list = array (); // Array of 'blocked' pages
2008-12-07 21:55:02 +00:00
2008-02-01 18:09:53 +00:00
2008-12-07 21:55:02 +00:00
/* constructor */
function e_tohtml_linkwords ()
2008-02-01 18:09:53 +00:00
{
2008-12-13 18:04:52 +00:00
global $pref , $tp , $e107 ;
// See whether they should be active on this page - if not, no point doing anything!
if (( strpos ( e_SELF , ADMINDIR ) !== FALSE ) || ( strpos ( e_PAGE , " admin_ " ) !== FALSE )) return ; // No linkwords on admin directories
2008-02-01 18:09:53 +00:00
2008-12-13 18:04:52 +00:00
// Now see if disabled on specific pages
$check_url = e_SELF . ( e_QUERY ? " ? " . e_QUERY : '' );
$this -> block_list = explode ( " | " , substr ( varset ( $pref [ 'lw_page_visibility' ], '' ), 2 )); // Knock off the 'show/hide' flag
foreach ( $this -> block_list as $p )
2008-02-01 18:09:53 +00:00
{
2008-12-13 18:04:52 +00:00
if ( $p = trim ( $p ))
{
if ( substr ( $p , - 1 ) == '!' )
{
$p = substr ( $p , 0 , - 1 );
if ( substr ( $check_url , strlen ( $p ) *- 1 ) == $p ) return ;
}
else
{
if ( strpos ( $check_url , $p ) !== FALSE ) return ;
}
}
}
2008-02-01 18:09:53 +00:00
2008-12-13 18:04:52 +00:00
// Will probably need linkwords on this page - so get the info
2010-01-02 22:57:41 +00:00
define ( 'LW_CACHE_TAG' , 'nomd5_linkwords' ); // Put it here to avoid conflict on admin pages
2008-12-13 18:04:52 +00:00
if ( LW_CACHE_ENABLE && ( $temp = $e107 -> ecache -> retrieve_sys ( LW_CACHE_TAG )))
{
$ret = eval ( $temp );
if ( $ret )
{
echo " Error reading linkwords cache: { $ret } <br /> " ;
$temp = '' ;
2008-02-01 18:09:53 +00:00
}
else
{
2008-12-13 18:04:52 +00:00
$this -> lw_enabled = TRUE ;
2008-02-01 18:09:53 +00:00
}
2008-12-13 18:04:52 +00:00
}
if ( ! $temp )
{ // Either cache disabled, or no info in cache (or error reading/processing cache)
$link_sql = new db ;
if ( $link_sql -> db_Select ( " linkwords " , " * " , " linkword_active!=1 " ))
{
$this -> lw_enabled = TRUE ;
while ( $row = $link_sql -> db_Fetch ())
{
extract ( $row );
$lw = $tp -> uStrToLower ( $linkword_word ); // It was trimmed when saved *utf
if ( $linkword_active == 2 ) $linkword_link = '' ; // Make sure linkword disabled
if ( $linkword_active < 2 ) $linkword_tooltip = '' ; // Make sure tooltip disabled
$lwID = max ( $row [ 'linkword_tip_id' ], $row [ 'linkword_id' ]); // If no specific ID defined, use the DB record ID
if ( strpos ( $lw , ',' ))
{ // Several words to same link
$lwlist = explode ( ',' , $lw );
foreach ( $lwlist as $lw )
{
$this -> word_list [] = trim ( $lw );
$this -> link_list [] = $linkword_link ;
$this -> tip_list [] = $linkword_tooltip ;
$this -> ext_list [] = $linkword_newwindow ;
$this -> LinkID [] = $lwID ;
}
}
else
{
$this -> word_list [] = $lw ;
$this -> link_list [] = $linkword_link ;
$this -> tip_list [] = $linkword_tooltip ;
$this -> ext_list [] = $linkword_newwindow ;
$this -> LinkID [] = $lwID ;
}
}
if ( LW_CACHE_ENABLE )
{ // Write to file for next time
$temp = '' ;
foreach ( array ( 'word_list' , 'link_list' , 'tip_list' , 'ext_list' , 'LinkID' ) as $var )
{
$temp .= '$this->' . $var . '=' . var_export ( $this -> $var , TRUE ) . " ; \n " ;
}
$e107 -> ecache -> set_sys ( LW_CACHE_TAG , $temp );
}
}
}
2008-12-07 21:55:02 +00:00
$this -> area_opts = $pref [ 'lw_context_visibility' ];
2009-07-19 11:44:30 +00:00
//@TODO utfMode probably obsolete
2008-12-07 21:55:02 +00:00
$this -> utfMode = ( strtolower ( CHARSET ) == 'utf-8' ) ? 'u' : '' ; // Flag to enable utf-8 on regex
$this -> lwAjaxEnabled = varset ( $pref [ 'lw_ajax_enable' ], 0 );
2008-02-01 18:09:53 +00:00
}
2008-12-07 21:55:02 +00:00
function to_html ( $text , $area = 'olddefault' )
2008-02-01 18:09:53 +00:00
{
2008-12-07 21:55:02 +00:00
if ( ! $this -> lw_enabled || ! array_key_exists ( $area , $this -> area_opts ) || ! $this -> area_opts [ $area ]) return $text ; // No linkwords in disabled areas
2008-02-01 18:09:53 +00:00
// Split up by HTML tags and process the odd bits here
2008-12-07 21:55:02 +00:00
$ptext = " " ;
$lflag = FALSE ;
2008-02-01 18:09:53 +00:00
2008-12-07 21:55:02 +00:00
// Shouldn't need utf-8 on next line - just looking for HTML tags
$content = preg_split ( '#(<.*?>)#mis' , $text , - 1 , PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE );
foreach ( $content as $cont )
{
if ( $cont [ 0 ] == " < " )
{ // Its some HTML
$ptext .= $cont ;
if ( substr ( $cont , 0 , 2 ) == " <a " ) $lflag = TRUE ;
if ( substr ( $cont , 0 , 3 ) == " </a " ) $lflag = FALSE ;
}
else
{ // Its the text in between
if ( $lflag )
{ // Its probably within a link - leave unchanged
$ptext .= $cont ;
}
else
{
if ( trim ( $cont ))
{ // Some non-white space - worth word matching
$ptext .= $this -> linksproc ( $cont , 0 , count ( $this -> word_list ));
// echo "Check linkwords: ".count($this->word_list).'<br />';
}
else
{
$ptext .= $cont ;
}
}
2008-02-01 18:09:53 +00:00
}
}
2008-12-07 21:55:02 +00:00
return $ptext ;
2008-02-01 18:09:53 +00:00
}
2010-01-16 20:49:55 +00:00
2008-02-01 18:09:53 +00:00
function linksproc ( $text , $first , $limit )
{ // This function is called recursively - it splits the text up into blocks - some containing a particular linkword
2010-01-16 20:49:55 +00:00
$tp = e107 :: getParser ();
$doSamePage = ! e107 :: getPref ( 'lw_notsamepage' );
2008-12-07 21:55:02 +00:00
// Consider next line - stripos is PHP5, and mb_stripos is PHP >= 5.2 - so may well often require handling
// while (($first < $limit) && (stripos($text,$this->word_list[$first]) === FALSE)) { $first++; }; // *utf (stripos is PHP5 - compatibility handler implements)
2010-01-16 20:49:55 +00:00
while (( $first < $limit ) && ( strpos ( $tp -> ustrtolower ( $text ), $this -> word_list [ $first ]) === FALSE )) { $first ++ ; }; // *utf
2008-12-07 21:55:02 +00:00
if ( $first == $limit ) return $text ; // Return if no linkword found
// There's at least one occurrence of the linkword in the text
// Prepare all info once only
// If supporting Ajax, use the following:
// <a href='link url' rel='external linkwordId::122' class='linkword-ajax'>
// linkwordId::122 is a unique ID
$ret = '' ;
$linkwd = '' ;
$linkrel = array ();
// $linkwd = "href='#' "; // Not relevant for Prototype, but needed with 'pure' JS to make tooltip stuff work - doesn't find link elements without href
$lwClass = array ();
$lw = $this -> word_list [ $first ]; // This is the word we're matching - in lower case in our 'master' list
$tooltip = '' ;
if ( $this -> tip_list [ $first ])
{ // Got tooltip
if ( $this -> lwAjaxEnabled )
{
$linkrel [] = 'linkwordID::' . $this -> LinkID [ $first ];
$lwClass [] = 'lw_ajax' ;
}
else
{
$tooltip = " title=' { $this -> tip_list [ $first ] } ' " ;
$lwClass [] = 'lw_tip' ;
}
2008-02-01 18:09:53 +00:00
}
2008-12-07 21:55:02 +00:00
if ( $this -> link_list [ $first ])
{ // Got link
2010-01-16 20:49:55 +00:00
$newLink = $tp -> replaceConstants ( $this -> link_list [ $first ], 'full' );
if ( $doSamePage || ( $newLink != e_SELF . '?' . e_QUERY ))
{
$linkwd = " href=' " . $newLink . " ' " ;
if ( $this -> ext_list [ $first ]) { $linkrel [] = 'external' ; } // Determine external links
$lwClass [] = 'lw_link' ;
}
2008-02-01 18:09:53 +00:00
}
2008-12-07 21:55:02 +00:00
if ( ! count ( $lwClass ))
2008-02-01 18:09:53 +00:00
{
2008-12-07 21:55:02 +00:00
return $this -> linksproc ( $sl , $first + 1 , $limit ); // Nothing to do - move on to next word (shouldn't really get here)
2008-02-01 18:09:53 +00:00
}
2008-12-07 21:55:02 +00:00
if ( count ( $linkrel ))
{
$linkwd .= " rel=' " . implode ( ' ' , $linkrel ) . " ' " ;
}
// This splits the text into blocks, some of which will precisely contain a linkword
$split_line = preg_split ( '#\b(' . $lw . ')\b#i' . $this -> utfMode , $text , - 1 , PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE ); // *utf (selected)
$class = " class=' " . implode ( ' ' , $lwClass ) . " ' " ;
foreach ( $split_line as $sl )
{
if ( $tp -> uStrToLower ( $sl ) == $lw ) // We know the linkword is already lower case // *utf
{ // Do linkword replace
$ret .= ' <a ' . $class . $linkwd . $tooltip . '>' . $sl . '</a>' ;
}
elseif ( trim ( $sl ))
{ // Something worthwhile left - look for more linkwords in it
$ret .= $this -> linksproc ( $sl , $first + 1 , $limit );
}
else
{
$ret .= $sl ; // Probably just some white space
}
}
return $ret ;
}
2008-02-01 18:09:53 +00:00
}
?>