* @link http://wiki.foosel.net/snippets/dokuwiki/linkback
*/
use dokuwiki\Extension\Event;
require_once (DOKU_PLUGIN . 'linkback/tools.php');
class action_plugin_linkback_display extends DokuWiki_Action_Plugin {
/**
* A little helper.
* @var helper_plugin_linkback
*/
var $tools;
/**
* Constructor
*/
function __construct() {
$this->tools = plugin_load('tools', 'linkback');
}
/**
* Register the eventhandlers.
*/
function register(Doku_Event_Handler $controller) {
$controller->register_hook('TPL_ACT_RENDER', 'AFTER', $this, 'handle_act_render', array ());
$controller->register_hook('RENDERER_CONTENT_POSTPROCESS', 'AFTER', $this, 'handle_content_postprocess', array ());
}
/**
* Handler for the TPL_ACT_RENDER event
*/
function handle_act_render(Doku_Event $event, $params) {
global $ID, $INFO;
if ($event->data != 'show')
return;
$file = metaFN($ID, '.linkbacks');
$data = array (
'send' => false,
'receive' => false,
'display' => false,
'sentpings' => array (),
'receivedpings' => array (),
'number' => 0,
);
if (@ file_exists($file))
$data = unserialize(io_readFile($file, false));
if (!isset ($_REQUEST['linkback']) || $INFO['perm'] != AUTH_ADMIN)
$_REQUEST['linkback'] = 'linkback_show';
$lid = isset($_REQUEST['lid']) ? $_REQUEST['lid'] : '';
if (!preg_match('![a-zA-Z0-9]!', $lid))
$_REQUEST['linkback'] = 'linkback_show';
switch ($_REQUEST['linkback']) {
// toggle show/hide
case 'linkback_toggle' :
$linkback = $data['receivedpings'][$lid];
$data = $this->_changeLinkbackVisibilities(array($lid), !$linkback['show']);
break;
// delete linkback
case 'linkback_delete' :
$data = $this->_deleteLinkbacks(array($lid));
break;
// report linkback as ham
case 'linkback_ham' :
$this->_markLinkbacks(array($lid), false);
break;
// report linkback as spam
case 'linkback_spam' :
$this->_markLinkbacks(array($lid), true);
break;
}
$this->_show($data);
}
/**
* Shows all linkbacks for the current page
*/
function _show($data) {
global $ID;
if (!$data['display'])
return;
if ((count($data['receivedpings']) == 0) &&
(!$this->getConf('show_trackback_url') || !$this->getConf('enable_trackback')))
return;
// section title
$title = $this->getLang('linkbacks');
echo '
';
echo '
';
if ($this->getConf('show_trackback_url') && $data['receive']) {
echo '
';
echo $this->getLang('trackback_url');
echo '' . DOKU_URL . 'lib/plugins/linkback/exe/trackback.php/' . $ID . '';
echo '
';
}
echo '
';
// now display the comments
if (isset ($data['receivedpings'])) {
foreach ($data['receivedpings'] as $key => $ping) {
$this->_print($key, $ping);
}
}
echo '
'; // level2
echo '
'; // comment_wrapper
}
/**
* Prints an individual linkback
*/
function _print($lid, $linkback, $visible = true) {
global $INFO;
global $conf;
if (!is_array($linkback))
return; // corrupt datatype
if (!$linkback['show']) { // linkback hidden
if ($INFO['perm'] == AUTH_ADMIN) {
echo '' . NL;
} else {
return;
}
}
$title = $linkback['title'];
$url = $linkback['url'];
$date = $linkback['received'];
$excerpt = $linkback['excerpt'];
$icon = $linkback['favicon'];
$type = $linkback['type'];
// linkback head with date and link
echo '
' . NL .
'
' . NL .
'
';
// show favicon image
if ($this->getConf('usefavicon')) {
if (!$icon) {
$icon = $this->getConf('favicon_default');
}
$size = 16;
$src = ml($icon);
echo '' . NL;
$style = ' style="margin-left: ' . ($size +14) . 'px;"';
} else {
$style = ' style="margin-left: 20px;"';
}
echo $this->external_link($url, ($linkback['blog_title'] ? $linkback['blog_title'] . ': ' : '') . $title, 'urlextern url fn');
echo ',
' . strftime($conf['dformat'], $date) . '';
echo ' (
' . $this->getLang('linkback_type_' . $type) . ')';
echo NL . '
' . NL; // class="linkback_head"
// main linkback content
if (strlen($excerpt) > 0) {
echo '
getConf('usefavicon') ? $style : '') . '>' . NL .
$excerpt . NL . '
' . NL; // class="linkback_body"
}
echo '
' . NL; // class="hentry"
echo '
' . NL;
if ($INFO['perm'] == AUTH_ADMIN) {
if (!$linkback['show']) {
$label = $this->getLang('btn_show');
} else {
$label = $this->getLang('btn_hide');
}
$this->_button($lid, $label, 'linkback_toggle');
$this->_button($lid, $this->getLang('btn_ham'), 'linkback_ham');
$this->_button($lid, $this->getLang('btn_spam'), 'linkback_spam');
$this->_button($lid, $this->getLang('btn_delete'), 'linkback_delete');
}
echo '
';
echo '
getConf('usefavicon') ? $style : '') . '>
' . NL;
if (!$linkback['show']) {
echo '
' . NL; // class="linkback_hidden"
}
}
/**
* General button function.
*
* Code mostly taken from the discussion plugin by Esther Brunner.
*/
function _button($lid, $label, $act) {
global $ID;
?>
data[0] != 'xhtml')
return; // nothing to do for us
$file = metaFN($ID, '.linkbacks');
if (!@ file_exists($file))
return;
$data = unserialize(io_readFile($file, false));
if (!$data['display'] || (count($data['receivedpings']) == 0))
return; // no linkback section
$pattern = '/(.*?)<\/div>\s<\/div>/s';
if (!preg_match($pattern, $event->data[1], $match))
return; // no TOC on this page
$title = $this->getLang('linkbacks');
$section = '#linkback__section';
$level = 3 - $conf['toptoclevel'];
$item = '
';
if ($level == 1)
$search = "\n
";
else
$search = "\n\n";
$new = str_replace($search, $item . $search, $match[0]);
$event->data[1] = preg_replace($pattern, $new, $event->data[1]);
}
function _changeLinkbackVisibilities($lids, $visible) {
global $ID;
$file = metaFN($ID, '.linkbacks');
$data = array (
'send' => false,
'receive' => false,
'display' => false,
'sentpings' => array (),
'receivedpings' => array (),
'number' => 0,
);
if (@ file_exists($file))
$data = unserialize(io_readFile($file, false));
$update = false;
foreach ($lids as $lid) {
$linkback = $data['receivedpings'][$lid];
if ($linkback['show'] == $visible)
continue;
$linkback['show'] = $visible;
if ($linkback['show'])
$data['number']++;
else
$data['number']--;
$data['receivedpings'][$lid] = $linkback;
$this->tools->addLogEntry($linkback['received'], $ID, (($linkback['show']) ? 'sl' : 'hl'), '', $linkback['lid']);
$update = true;
}
if ($update)
io_saveFile($file, serialize($data));
return $data;
}
function _deleteLinkbacks($lids) {
global $ID;
$file = metaFN($ID, '.linkbacks');
$data = array (
'send' => false,
'receive' => false,
'display' => false,
'sentpings' => array (),
'receivedpings' => array (),
'number' => 0,
);
if (@ file_exists($file))
$data = unserialize(io_readFile($file, false));
$update = false;
foreach ($lids as $lid) {
$linkback = $data['receivedpings'][$lid];
unset ($data['receivedpings'][$lid]);
if ($linkback['show'])
$data['number']--;
$this->tools->addLogEntry($linkback['received'], $ID, 'dl', '', $linkback['lid']);
$update = true;
}
if ($update)
io_saveFile($file, serialize($data));
return $data;
}
function _markLinkbacks($lids, $isSpam) {
global $ID;
$file = metaFN($ID, '.linkbacks');
$data = array (
'send' => false,
'receive' => false,
'display' => false,
'sentpings' => array (),
'receivedpings' => array (),
'number' => 0,
);
if (@ file_exists($file))
$data = unserialize(io_readFile($file, false));
foreach ($lids as $lid) {
$linkback = $data['receivedpings'][$lid];
if ($isSpam) {
Event::createAndTrigger('ACTION_LINKBACK_SPAM', $linkback);
} else {
Event::createAndTrigger('ACTION_LINKBACK_HAM', $linkback);
}
}
}
}