mirror of
https://github.com/e107inc/e107.git
synced 2025-07-24 00:12:24 +02:00
Linkwords plugin update.
Fixes issue with admin preferences not being respected or no linking occurring. Prefs now moved out of core and into plugin prefs. Test page added to admin area. Relationship field added. e_tohtml.php deprecated in favor of e_parse.php Tests updated.
This commit is contained in:
@@ -19,9 +19,10 @@ if (!getperms('P') || !e107::isInstalled('linkwords'))
|
||||
}
|
||||
|
||||
e107::lan('linkwords', true);
|
||||
|
||||
define('LW_CACHE_TAG', 'nomd5_linkwords');
|
||||
|
||||
if(!defined('LW_CACHE_TAG'))
|
||||
{
|
||||
define('LW_CACHE_TAG', 'nomd5_linkwords');
|
||||
}
|
||||
|
||||
class linkwords_admin extends e_admin_dispatcher
|
||||
{
|
||||
@@ -40,7 +41,7 @@ class linkwords_admin extends e_admin_dispatcher
|
||||
'main/list' => array('caption'=> LAN_MANAGE, 'perm' => 'P'),
|
||||
'main/create' => array('caption'=> LAN_CREATE, 'perm' => 'P'),
|
||||
'main/prefs' => array('caption'=> LAN_PREFS, 'perm' => 'P'),
|
||||
// 'main/custom' => array('caption'=> 'Custom Page', 'perm' => 'P')
|
||||
'main/test' => array('caption'=> LAN_TEST, 'perm' => 'P')
|
||||
);
|
||||
|
||||
protected $adminMenuAliases = array(
|
||||
@@ -79,6 +80,8 @@ class linkwords_ui extends e_admin_ui
|
||||
'linkword_limit' => array ( 'title' => LWLAN_67, 'type' => 'number', 'data' => 'int', 'width' => '10%', 'help' => LAN_LW_HELP_15, 'readParms' => '', 'writeParms' => array('default'=>3), 'class' => 'right', 'thclass' => 'right', ),
|
||||
'linkword_tip_id' => array ( 'title' => LAN_ID, 'type' => 'number', 'data' => 'int', 'width' => '5%', 'help' => LAN_LW_HELP_16, 'readParms' => '', 'writeParms' => '', 'class' => 'right', 'thclass' => 'right', ),
|
||||
'linkword_newwindow' => array ( 'title' => LWLAN_55, 'type' => 'boolean', 'data' => 'int', 'width' => 'auto', 'inline' => true, 'help' => LAN_LW_HELP_17, 'filter'=>true, 'readParms' => '', 'writeParms' => '', 'class' => 'center', 'thclass' => 'center', ),
|
||||
'linkword_rel' => array ( 'title' => LAN_RELATIONSHIP, 'type' => 'tags', 'data' => 'str', 'width' => 'auto', 'inline' => true, 'help' => LAN_RELATIONSHIP_HELP, 'filter'=>false, 'readParms' => '', 'writeParms'=>array('placeholder'=>'eg.nofollow,noreferrer','size'=>'xlarge'), 'class' => 'center', 'thclass' => 'center', ),
|
||||
|
||||
'options' => array ( 'title' => LAN_OPTIONS, 'type' => null, 'data' => null, 'width' => '10%', 'thclass' => 'center last', 'class' => 'center last', 'forced' => '1', ),
|
||||
);
|
||||
|
||||
@@ -176,6 +179,35 @@ class linkwords_ui extends e_admin_ui
|
||||
|
||||
}
|
||||
|
||||
public function testPage()
|
||||
{
|
||||
$text = '';
|
||||
|
||||
if(!empty($_POST['runLinkwordTest']))
|
||||
{
|
||||
// $text .= "<strong>Result:</strong><br />";
|
||||
$result = e107::getParser()->toHTML($_POST['test_body'], false, 'BODY');
|
||||
|
||||
$text .= "<div class='well' style='padding:30px'>".$result."</div>";
|
||||
$text .= "<div class='well' style='padding:30px; margin-bottom:30px'>".htmlentities($result)."</div>";
|
||||
}
|
||||
|
||||
$frm = $this->getUI();
|
||||
$text .= $frm->open('linkwordsTest');
|
||||
$text .= "<div style='width:800px'>";
|
||||
$text .= $frm->textarea('test_body', varset($_POST['test_body']), 10, 80, ['class'=>'form-control','placeholder'=>'Start writing...']);
|
||||
|
||||
$text .= "<div class='buttons-bar center'><p>";
|
||||
$text .= $frm->submit('runLinkwordTest', LAN_TEST);
|
||||
$text .= "</p></div>";
|
||||
$text .= "</div>";
|
||||
$text .= $frm->close();
|
||||
|
||||
return $text;
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
if(USER_AREA === true) // Don't include in admin area.
|
||||
if(defset('USER_AREA') === true) // Don't include in admin area.
|
||||
{
|
||||
e107::css('linkwords', 'linkwords.css');
|
||||
}
|
403
e107_plugins/linkwords/e_parse.php
Normal file
403
e107_plugins/linkwords/e_parse.php
Normal file
@@ -0,0 +1,403 @@
|
||||
<?php
|
||||
/*
|
||||
* e107 website system
|
||||
*
|
||||
* Copyright (C) 2008-2021 e107 Inc (e107.org)
|
||||
* Released under the terms and conditions of the
|
||||
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
|
||||
if (!defined('e107_INIT')) { exit; }
|
||||
|
||||
if(!defined('LW_CACHE_ENABLE'))
|
||||
{
|
||||
define('LW_CACHE_ENABLE', false);
|
||||
}
|
||||
|
||||
class linkwords_parse
|
||||
{
|
||||
protected $lw_enabled = FALSE; // Default to disabled to start
|
||||
protected $lwAjaxEnabled = FALSE; // Adds in Ajax-compatible links
|
||||
protected $utfMode = ''; // Flag to enable utf-8 on regex
|
||||
|
||||
protected $word_list = array(); // List of link words/phrases
|
||||
private $link_list = array(); // Corresponding list of links to apply
|
||||
private $ext_list = array(); // Flags to determine 'open in new window' for link
|
||||
private $tip_list = array(); // Store for tooltips
|
||||
private $rel_list = array();
|
||||
private $LinkID = array(); // Unique ID for each linkword
|
||||
private $area_opts; // Process flags for the various contexts
|
||||
private $block_list = array(); // Array of 'blocked' pages
|
||||
|
||||
protected $word_class = array();
|
||||
|
||||
protected $customClass = '';
|
||||
protected $wordCount = array();
|
||||
protected $word_limit = array();
|
||||
// protected $maxPerWord = 3;
|
||||
|
||||
public function enable()
|
||||
{
|
||||
$this->lw_enabled = true;
|
||||
}
|
||||
|
||||
public function setWordData($arr = array())
|
||||
{
|
||||
foreach($arr as $val)
|
||||
{
|
||||
$this->word_list[] = $val['word'];
|
||||
$this->link_list[] = $val['link'];
|
||||
$this->ext_list[] = $val['ext'];
|
||||
$this->tip_list[] = $val['tip'];
|
||||
$this->word_limit[] = $val['limit'];
|
||||
}
|
||||
}
|
||||
|
||||
public function setAreaOpts($arr = array())
|
||||
{
|
||||
$this->area_opts = $arr;
|
||||
}
|
||||
|
||||
|
||||
public function setLink($arr)
|
||||
{
|
||||
$this->word_list = $arr;
|
||||
}
|
||||
|
||||
|
||||
/* constructor */
|
||||
function __construct()
|
||||
{
|
||||
|
||||
$tp = e107::getParser();
|
||||
$pref = e107::pref('linkwords');
|
||||
$frm = e107::getForm();
|
||||
|
||||
// $this->maxPerWord = vartrue($pref['lw_max_per_word'], 25);
|
||||
$this->customClass = vartrue($pref['lw_custom_class']);
|
||||
$this->area_opts = (array) varset($pref['lw_context_visibility']);
|
||||
$this->utfMode = (strtolower(CHARSET) == 'utf-8') ? 'u' : ''; // Flag to enable utf-8 on regex //@TODO utfMode probably obsolete
|
||||
$this->lwAjaxEnabled = varset($pref['lw_ajax_enable'],0);
|
||||
|
||||
// See whether they should be active on this page - if not, no point doing anything!
|
||||
if(e_ADMIN_AREA === true && empty($_POST['runLinkwordTest'])) { return; }
|
||||
|
||||
// Now see if disabled on specific pages
|
||||
$check_url = e_SELF.(defined('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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Will probably need linkwords on this page - so get the info
|
||||
if(!defined('LW_CACHE_TAG'))
|
||||
{
|
||||
define('LW_CACHE_TAG', 'nomd5_linkwords'); // Put it here to avoid conflict on admin pages
|
||||
}
|
||||
|
||||
if(LW_CACHE_ENABLE && ($temp = e107::getCache()->retrieve_sys(LW_CACHE_TAG)))
|
||||
{
|
||||
$ret = eval($temp);
|
||||
if ($ret)
|
||||
{
|
||||
echo "Error reading linkwords cache: {$ret}<br />";
|
||||
$temp = '';
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->lw_enabled = TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
if(!vartrue($temp)) // Either cache disabled, or no info in cache (or error reading/processing cache)
|
||||
{
|
||||
$link_sql = e107::getDb('link_sql');
|
||||
|
||||
if($link_sql->select("linkwords", "*", "linkword_active!=1"))
|
||||
{
|
||||
$this->lw_enabled = true;
|
||||
|
||||
while($row = $link_sql->fetch())
|
||||
{
|
||||
|
||||
$lw = $tp->ustrtolower($row['linkword_word']); // It was trimmed when saved *utf
|
||||
|
||||
if($row['linkword_active'] == 2)
|
||||
{
|
||||
$row['linkword_link'] = ''; // Make sure linkword disabled
|
||||
}
|
||||
|
||||
if($row['linkword_active'] < 2)
|
||||
{
|
||||
$row['linkword_tooltip'] = ''; // Make sure tooltip disabled
|
||||
}
|
||||
|
||||
|
||||
if(strpos($lw,',')) // Several words to same link
|
||||
{
|
||||
$lwlist = explode(',',$lw);
|
||||
foreach ($lwlist as $lw)
|
||||
{
|
||||
$this->loadRow($lw,$row);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->loadRow($lw,$row);
|
||||
}
|
||||
}
|
||||
|
||||
if(deftrue('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::getCache()->set_sys(LW_CACHE_TAG,$temp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
private function loadRow($lw, $row)
|
||||
{
|
||||
$lw = trim($lw);
|
||||
|
||||
if(empty($lw))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
$frm = e107::getForm();
|
||||
|
||||
$this->word_list[] = $lw;
|
||||
$this->word_class[] = 'lw-'.$frm->name2id($lw);
|
||||
$this->word_limit[] = vartrue($row['linkword_limit'],3);
|
||||
$this->link_list[] = $row['linkword_link'];
|
||||
$this->tip_list[] = $row['linkword_tooltip'];
|
||||
$this->ext_list[] = $row['linkword_newwindow'];
|
||||
$this->rel_list[] = str_replace(",", ' ', $row['linkword_rel']);
|
||||
$this->LinkID[] = max($row['linkword_tip_id'], $row['linkword_id']); // If no specific ID defined, use the DB record ID
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function toHTML($text,$area = 'olddefault')
|
||||
{
|
||||
|
||||
if(is_string($this->area_opts))
|
||||
{
|
||||
$this->area_opts = e107::unserialize($this->area_opts);
|
||||
}
|
||||
|
||||
if($this->area_opts === null)
|
||||
{
|
||||
$this->area_opts = array();
|
||||
}
|
||||
|
||||
if (!$this->lw_enabled || empty($this->area_opts) || !array_key_exists($area,$this->area_opts) || !$this->area_opts[$area])
|
||||
{
|
||||
// e107::getDebug()->log("Link words skipped on ".substr($text, 0, 50));
|
||||
return $text; // No linkwords in disabled areas
|
||||
}
|
||||
|
||||
// Split up by HTML tags and process the odd bits here
|
||||
$ptext = "";
|
||||
$lflag = FALSE;
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// print_a($this->wordCount);
|
||||
return $ptext;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This function is called recursively - it splits the text up into blocks - some containing a particular linkword
|
||||
* @param $text
|
||||
* @param $first
|
||||
* @param $limit
|
||||
* @return string
|
||||
*/
|
||||
function linksproc($text,$first,$limit)
|
||||
{
|
||||
$tp = e107::getParser();
|
||||
$doSamePage = !e107::getPref('lw_notsamepage');
|
||||
|
||||
|
||||
for (; $first < $limit; $first ++)
|
||||
{
|
||||
if (empty($this->word_list[$first])) continue;
|
||||
if (strpos($tp->ustrtolower($text), $this->word_list[$first]) !== false) break;
|
||||
}
|
||||
|
||||
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 '.$this->customClass;
|
||||
}
|
||||
else
|
||||
{
|
||||
$tooltip = " title=\"{$this->tip_list[$first]}\" ";
|
||||
$lwClass[] = 'lw-tip '.$this->customClass;
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->link_list[$first]) // Got link
|
||||
{
|
||||
$newLink = $tp->replaceConstants($this->link_list[$first], 'full');
|
||||
if ($doSamePage || ($newLink != e_SELF.'?'.e_QUERY))
|
||||
{
|
||||
$linkwd = " href=\"".$newLink."\" ";
|
||||
|
||||
if(!empty($this->rel_list[$first]))
|
||||
{
|
||||
$linkrel[] = $this->rel_list[$first];
|
||||
}
|
||||
elseif($this->ext_list[$first]) // Determine external links
|
||||
{
|
||||
$linkrel[] = 'noopener external';
|
||||
}
|
||||
|
||||
$lwClass[] = 'lw-link '.$this->customClass;
|
||||
}
|
||||
}
|
||||
elseif(!empty($this->word_class[$first]))
|
||||
{
|
||||
$lwClass[] = $this->word_class[$first];
|
||||
}
|
||||
|
||||
// if (!count($lwClass))
|
||||
// {
|
||||
// return $this->linksproc($sl,$first+1,$limit); // Nothing to do - move on to next word (shouldn't really get here)
|
||||
// }
|
||||
|
||||
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.')(\s|\b)#i'.$this->utfMode, $text, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE ); // *utf (selected)
|
||||
// $class = "".implode(' ',$lwClass)."' ";
|
||||
$class = implode(' ',$lwClass);
|
||||
|
||||
$hash = md5($lw);
|
||||
|
||||
if(!isset($this->wordCount[$hash]))
|
||||
{
|
||||
$this->wordCount[$hash] = 0;
|
||||
}
|
||||
|
||||
foreach ($split_line as $count=>$sl)
|
||||
{
|
||||
if ($tp->ustrtolower($sl) == $lw && $this->wordCount[$hash] < (int) $this->word_limit[$first]) // Do linkword replace // We know the linkword is already lower case // *utf
|
||||
{
|
||||
$this->wordCount[$hash]++;
|
||||
|
||||
$classCount = " lw-".$this->wordCount[$hash];
|
||||
|
||||
if(empty($linkwd))
|
||||
{
|
||||
$ret .= "<span class=\"".$class.$classCount."\" ".$tooltip.">".$sl."</span>";
|
||||
}
|
||||
else
|
||||
{
|
||||
$ret .= "<a class=\"".$class.$classCount."\" ".$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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@@ -9,387 +9,7 @@
|
||||
*/
|
||||
|
||||
/**
|
||||
* e107 Linkword plugin
|
||||
*
|
||||
* 'Hook' page
|
||||
* The class is 'hooked' by the parser, to add linkword capability to any context where its enabled.
|
||||
*
|
||||
* @todo Link to capability for clever display options on tooltips
|
||||
* @deprecated see linkwords/e_parse.php
|
||||
*/
|
||||
|
||||
if (!defined('e107_INIT')) { exit; }
|
||||
// if (!e107::isInstalled('linkwords')) exit; // This will completely break a site during upgrades.
|
||||
|
||||
define('LW_CACHE_ENABLE', FALSE);
|
||||
|
||||
class e_tohtml_linkwords
|
||||
{
|
||||
protected $lw_enabled = FALSE; // Default to disabled to start
|
||||
var $lwAjaxEnabled = FALSE; // Adds in Ajax-compatible links
|
||||
var $utfMode = ''; // Flag to enable utf-8 on regex
|
||||
|
||||
protected $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
|
||||
var $LinkID = array(); // Unique ID for each linkword
|
||||
var $area_opts = array(); // Process flags for the various contexts
|
||||
var $block_list = array(); // Array of 'blocked' pages
|
||||
|
||||
protected $word_class = array();
|
||||
|
||||
protected $customClass = '';
|
||||
protected $wordCount = array();
|
||||
protected $word_limit = array();
|
||||
// protected $maxPerWord = 3;
|
||||
|
||||
public function enable()
|
||||
{
|
||||
$this->lw_enabled = true;
|
||||
}
|
||||
|
||||
public function setWordData($arr = array())
|
||||
{
|
||||
foreach($arr as $val)
|
||||
{
|
||||
$this->word_list[] = $val['word'];
|
||||
$this->link_list[] = $val['link'];
|
||||
$this->ext_list[] = $val['ext'];
|
||||
$this->tip_list[] = $val['tip'];
|
||||
$this->word_limit[] = $val['limit'];
|
||||
}
|
||||
}
|
||||
|
||||
public function setAreaOpts($arr = array())
|
||||
{
|
||||
$this->area_opts = $arr;
|
||||
}
|
||||
|
||||
|
||||
public function setLink($arr)
|
||||
{
|
||||
$this->word_list = $arr;
|
||||
}
|
||||
|
||||
|
||||
/* constructor */
|
||||
function __construct()
|
||||
{
|
||||
$tp = e107::getParser();
|
||||
$pref = e107::pref('core');
|
||||
$frm = e107::getForm();
|
||||
|
||||
// $this->maxPerWord = vartrue($pref['lw_max_per_word'], 25);
|
||||
$this->customClass = vartrue($pref['lw_custom_class'],'');
|
||||
$this->area_opts = varset($pref['lw_context_visibility']);
|
||||
$this->utfMode = (strtolower(CHARSET) == 'utf-8') ? 'u' : ''; // Flag to enable utf-8 on regex //@TODO utfMode probably obsolete
|
||||
$this->lwAjaxEnabled = varset($pref['lw_ajax_enable'],0);
|
||||
|
||||
// See whether they should be active on this page - if not, no point doing anything!
|
||||
if(e_ADMIN_AREA === true) { return; }
|
||||
// if ((strpos(e_SELF, ADMINDIR) !== FALSE) || (strpos(e_PAGE, "admin_") !== FALSE)) return; // No linkwords on admin directories
|
||||
|
||||
// Now see if disabled on specific pages
|
||||
$check_url = e_SELF.(defined('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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Will probably need linkwords on this page - so get the info
|
||||
define('LW_CACHE_TAG', 'nomd5_linkwords'); // Put it here to avoid conflict on admin pages
|
||||
|
||||
if(LW_CACHE_ENABLE && ($temp = e107::getCache()->retrieve_sys(LW_CACHE_TAG)))
|
||||
{
|
||||
$ret = eval($temp);
|
||||
if ($ret)
|
||||
{
|
||||
echo "Error reading linkwords cache: {$ret}<br />";
|
||||
$temp = '';
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->lw_enabled = TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
if(!vartrue($temp)) // Either cache disabled, or no info in cache (or error reading/processing cache)
|
||||
{
|
||||
$link_sql = e107::getDb('link_sql');
|
||||
|
||||
if($link_sql->select("linkwords", "*", "linkword_active!=1"))
|
||||
{
|
||||
$this->lw_enabled = true;
|
||||
|
||||
while($row = $link_sql->fetch())
|
||||
{
|
||||
|
||||
$lw = $tp->ustrtolower($row['linkword_word']); // It was trimmed when saved *utf
|
||||
|
||||
if($row['linkword_active'] == 2)
|
||||
{
|
||||
$row['linkword_link'] = ''; // Make sure linkword disabled
|
||||
}
|
||||
|
||||
if($row['linkword_active'] < 2)
|
||||
{
|
||||
$row['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)
|
||||
{
|
||||
$lw = trim($lw);
|
||||
|
||||
if(empty($lw))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->word_list[] = $lw;
|
||||
$this->word_class[] = 'lw-'.$frm->name2id($lw);
|
||||
$this->word_limit[] = vartrue($row['linkword_limit'],3);
|
||||
|
||||
$this->link_list[] = $row['linkword_link'];
|
||||
$this->tip_list[] = $row['linkword_tooltip'];
|
||||
$this->ext_list[] = $row['linkword_newwindow'];
|
||||
|
||||
$this->LinkID[] = $lwID;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
$lw = trim($lw);
|
||||
|
||||
if(empty($lw))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->word_list[] = $lw;
|
||||
$this->word_class[] = 'lw-'.$frm->name2id($lw);
|
||||
$this->word_limit[] = vartrue($row['linkword_limit'],3);
|
||||
$this->link_list[] = $row['linkword_link'];
|
||||
$this->tip_list[] = $row['linkword_tooltip'];
|
||||
$this->ext_list[] = $row['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::getCache()->set_sys(LW_CACHE_TAG,$temp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
function to_html($text,$area = 'olddefault')
|
||||
{
|
||||
|
||||
if(is_string($this->area_opts))
|
||||
{
|
||||
$this->area_opts = e107::unserialize($this->area_opts);
|
||||
}
|
||||
|
||||
if($this->area_opts === null)
|
||||
{
|
||||
$this->area_opts = array();
|
||||
}
|
||||
|
||||
if (!$this->lw_enabled || empty($this->area_opts) || !array_key_exists($area,$this->area_opts) || !$this->area_opts[$area])
|
||||
{
|
||||
// e107::getDebug()->log("Link words skipped on ".substr($text, 0, 50));
|
||||
return $text; // No linkwords in disabled areas
|
||||
}
|
||||
|
||||
// Split up by HTML tags and process the odd bits here
|
||||
$ptext = "";
|
||||
$lflag = FALSE;
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// print_a($this->wordCount);
|
||||
return $ptext;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This function is called recursively - it splits the text up into blocks - some containing a particular linkword
|
||||
* @param $text
|
||||
* @param $first
|
||||
* @param $limit
|
||||
* @return string
|
||||
*/
|
||||
function linksproc($text,$first,$limit)
|
||||
{
|
||||
$tp = e107::getParser();
|
||||
$doSamePage = !e107::getPref('lw_notsamepage');
|
||||
|
||||
for (; $first < $limit; $first ++)
|
||||
{
|
||||
if (empty($this->word_list[$first])) continue;
|
||||
if (strpos($tp->ustrtolower($text), $this->word_list[$first]) !== false) break;
|
||||
}
|
||||
|
||||
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 '.$this->customClass;
|
||||
}
|
||||
else
|
||||
{
|
||||
$tooltip = " title=\"{$this->tip_list[$first]}\" ";
|
||||
$lwClass[] = 'lw-tip '.$this->customClass;
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->link_list[$first]) // Got link
|
||||
{
|
||||
$newLink = $tp->replaceConstants($this->link_list[$first], 'full');
|
||||
if ($doSamePage || ($newLink != e_SELF.'?'.e_QUERY))
|
||||
{
|
||||
$linkwd = " href=\"".$newLink."\" ";
|
||||
if ($this->ext_list[$first]) { $linkrel[] = 'noopener external'; } // Determine external links
|
||||
$lwClass[] = 'lw-link '.$this->customClass;
|
||||
}
|
||||
}
|
||||
elseif(!empty($this->word_class[$first]))
|
||||
{
|
||||
$lwClass[] = $this->word_class[$first];
|
||||
}
|
||||
|
||||
// if (!count($lwClass))
|
||||
// {
|
||||
// return $this->linksproc($sl,$first+1,$limit); // Nothing to do - move on to next word (shouldn't really get here)
|
||||
// }
|
||||
|
||||
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.')(\s|\b)#i'.$this->utfMode, $text, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE ); // *utf (selected)
|
||||
// $class = "".implode(' ',$lwClass)."' ";
|
||||
$class = implode(' ',$lwClass);
|
||||
|
||||
$hash = md5($lw);
|
||||
|
||||
if(!isset($this->wordCount[$hash]))
|
||||
{
|
||||
$this->wordCount[$hash] = 0;
|
||||
}
|
||||
|
||||
foreach ($split_line as $count=>$sl)
|
||||
{
|
||||
if ($tp->ustrtolower($sl) == $lw && $this->wordCount[$hash] < (int) $this->word_limit[$first]) // Do linkword replace // We know the linkword is already lower case // *utf
|
||||
{
|
||||
$this->wordCount[$hash]++;
|
||||
|
||||
$classCount = " lw-".$this->wordCount[$hash];
|
||||
|
||||
if(empty($linkwd))
|
||||
{
|
||||
$ret .= "<span class=\"".$class.$classCount."\" ".$tooltip.">".$sl."</span>";
|
||||
}
|
||||
else
|
||||
{
|
||||
$ret .= "<a class=\"".$class.$classCount."\" ".$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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,73 +1,13 @@
|
||||
<?php
|
||||
/*
|
||||
/**
|
||||
* e107 website system
|
||||
*
|
||||
* Copyright (C) 2008-2009 e107 Inc (e107.org)
|
||||
* Released under the terms and conditions of the
|
||||
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* @deprecated see linkwords/e_parse.php
|
||||
*/
|
||||
if (!defined('e107_INIT')) { exit; }
|
||||
// if (!e107::isInstalled('linkwords')) exit; // This will break a site completely under some circumstance.
|
||||
|
||||
class e_linkwords
|
||||
{
|
||||
function __construct()
|
||||
{
|
||||
global $pref, $admin_log;
|
||||
/* constructor */
|
||||
// Do an auto-update on the variable used to hook parsers - so we should only be called once
|
||||
|
||||
e107::lan('linkwords', e_LANGUAGE); // e_PLUGIN."linkwords/languages/".e_LANGUAGE.".php"
|
||||
|
||||
$hooks = explode(",", $pref['tohtml_hook']);
|
||||
|
||||
if(($key=array_search('linkwords',$hooks)) !== FALSE)
|
||||
{
|
||||
unset($hooks[$key]);
|
||||
}
|
||||
|
||||
if (count($hooks) == 0)
|
||||
{
|
||||
unset($pref['tohtml_hook']);
|
||||
}
|
||||
else
|
||||
{
|
||||
$pref['tohtml_hook'] = implode(',',$hooks);
|
||||
}
|
||||
|
||||
if (!isset($pref['e_tohtml_list']))
|
||||
{
|
||||
$pref['e_tohtml_list'] = array();
|
||||
}
|
||||
|
||||
if (!in_array('linkwords',$pref['e_tohtml_list']))
|
||||
{
|
||||
$pref['e_tohtml_list'][] = 'linkwords';
|
||||
}
|
||||
|
||||
save_prefs();
|
||||
e107::getLog()->add('LINKWD_05',LWLAN_58.'[!br!]'.$pref['tohtml_hook'],''); // Log that the update was done
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// This avoids confusing the parser!
|
||||
function to_html($text,$area = 'olddefault')
|
||||
{
|
||||
return $text;
|
||||
}
|
||||
|
||||
|
||||
function linkwords($text,$area = 'olddefault')
|
||||
{
|
||||
return $text;
|
||||
}
|
||||
|
||||
function linksproc($text,$first,$limit)
|
||||
{
|
||||
return $text; // Shouldn't get called - but just in case
|
||||
}
|
||||
}
|
||||
|
||||
|
82
e107_plugins/linkwords/linkwords_setup.php
Normal file
82
e107_plugins/linkwords/linkwords_setup.php
Normal file
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
/*
|
||||
* e107 website system
|
||||
*
|
||||
* Copyright (C) 2008-2013 e107 Inc (e107.org)
|
||||
* Released under the terms and conditions of the
|
||||
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
|
||||
*
|
||||
* Custom install/uninstall/update routines for blank plugin
|
||||
**
|
||||
*/
|
||||
|
||||
|
||||
if(!class_exists("linkwords_setup"))
|
||||
{
|
||||
class linkwords_setup
|
||||
{
|
||||
/*
|
||||
function install_pre($var)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
function install_post($var)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
function uninstall_options()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
function uninstall_post($var)
|
||||
{
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
* Call During Upgrade Check.
|
||||
*
|
||||
* @return bool true = upgrade required; false = upgrade not required
|
||||
*/
|
||||
function upgrade_required()
|
||||
{
|
||||
|
||||
$pref = e107::pref();
|
||||
|
||||
if(isset($pref['lw_page_visibility']) || isset($pref['lw_ajax_enable']))
|
||||
{
|
||||
e107::getMessage()->addDebug("Prefs need to be migrated out of core prefs and into linkwords prefs.");
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
function upgrade_post($var)
|
||||
{
|
||||
|
||||
$plugPrefs = array(
|
||||
'lw_context_visibility' => 'lw_context_visibility',
|
||||
'lw_ajax_enable' => 'lw_ajax_enable',
|
||||
'lw_notsamepage' => 'lw_notsamepage',
|
||||
'linkword_omit_pages' => 'linkword_omit_pages',
|
||||
'lw_custom_class' => 'lw_custom_class',
|
||||
);
|
||||
|
||||
if($saveData = e107::getConfig()->migrateData($plugPrefs, true))
|
||||
{
|
||||
e107::getPlugConfig('linkwords')->setPref($saveData)->save(true,true,true);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -7,5 +7,6 @@ linkword_tooltip text NOT NULL,
|
||||
linkword_tip_id int(10) UNSIGNED NOT NULL Default '0',
|
||||
linkword_newwindow tinyint(1) unsigned NOT NULL default '1',
|
||||
linkword_limit int(3) UNSIGNED NOT NULL default '0',
|
||||
linkword_rel varchar(250) NOT NULL default '',
|
||||
PRIMARY KEY (linkword_id)
|
||||
) ENGINE=MyISAM AUTO_INCREMENT=1;
|
@@ -1,12 +1,12 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<e107Plugin name="Linkwords" lan="LAN_PLUGIN_LINKWORDS_NAME" version="1.1" date="2012-08-01" compatibility="2.0" installRequired="true" >
|
||||
<e107Plugin name="Linkwords" lan="LAN_PLUGIN_LINKWORDS_NAME" version="2.0" date="2021-09-28" compatibility="2.0" installRequired="true" >
|
||||
<author name="e107 Inc." url="http://e107.org" />
|
||||
<description lan="LAN_PLUGIN_LINKWORDS_DESCRIPTION">This plugin will link specified words with a defined link and/or tooltip.</description>
|
||||
<category>content</category>
|
||||
<adminLinks>
|
||||
<link url='admin_config.php' description='Configure LinkWords' icon='images/linkwords_32.png' iconSmall='images/linkwords_16.png' primary='true' >Configure LinkWords</link>
|
||||
</adminLinks>
|
||||
<mainPrefs>
|
||||
<pluginPrefs>
|
||||
<pref name="lw_page_visibility"></pref>
|
||||
<pref name="lw_ajax_enable">0</pref>
|
||||
<pref name="lw_max_per_word">2</pref>
|
||||
@@ -23,5 +23,5 @@
|
||||
'RAWTEXT' => ''
|
||||
)
|
||||
</pref>
|
||||
</mainPrefs>
|
||||
</pluginPrefs>
|
||||
</e107Plugin>
|
@@ -17,10 +17,10 @@
|
||||
|
||||
protected function _before()
|
||||
{
|
||||
require_once(e_PLUGIN."linkwords/e_tohtml.php");
|
||||
require_once(e_PLUGIN."linkwords/e_parse.php");
|
||||
try
|
||||
{
|
||||
$this->lw = $this->make('e_tohtml_linkwords');
|
||||
$this->lw = $this->make('linkwords_parse');
|
||||
} catch(Exception $e)
|
||||
{
|
||||
$this->assertTrue(false, "Couldn't load e_tohtml_linkwords object");
|
||||
@@ -65,7 +65,7 @@
|
||||
|
||||
foreach($tests as $val)
|
||||
{
|
||||
$result = $this->lw->to_html($val['text'], 'BODY');
|
||||
$result = $this->lw->toHTML($val['text'], 'BODY');
|
||||
$this->assertEquals($val['expected'],$result);
|
||||
|
||||
}
|
||||
|
@@ -308,15 +308,15 @@
|
||||
{
|
||||
$this->pluginInstall('linkwords');
|
||||
|
||||
$pref1 = e107::pref('core', 'lw_custom_class');
|
||||
$pref1 = e107::pref('linkwords', 'lw_custom_class');
|
||||
$this->assertNotEmpty($pref1);
|
||||
|
||||
$pref2 = e107::pref('core', 'lw_context_visibility');
|
||||
$pref2 = e107::pref('linkwords', 'lw_context_visibility');
|
||||
$this->assertNotEmpty($pref2['SUMMARY']);
|
||||
|
||||
$this->pluginUninstall('linkwords');
|
||||
|
||||
$pref2 = e107::pref('core', 'lw_context_visibility');
|
||||
$pref2 = e107::pref('linkwords', 'lw_context_visibility');
|
||||
$this->assertEmpty($pref2);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user