MyShaarli/plugins/myShaarli/myShaarli.php

220 lines
6.6 KiB
PHP
Raw Normal View History

2018-01-04 14:49:09 +01:00
<?php
2023-05-25 11:13:43 +02:00
use Shaarli\Bookmark\Bookmark;
use Shaarli\Config\ConfigManager;
use Shaarli\Plugin\PluginManager;
2018-01-04 14:49:09 +01:00
/**
* Plugin externalThumbshot
*/
2018-10-05 15:32:27 +02:00
/**
* Init function, return an error if the server is not set.
*
* @param $conf ConfigManager instance.
*
* @return array Eventual error.
*/
2023-05-25 11:13:43 +02:00
function myShaarli_init($conf) {
2018-10-05 15:32:27 +02:00
if (empty($conf->get('plugins.ExternalThumbshot_URL'))) {
$error = 'myShaarli plugin error: ' .
'Please define the "ExternalThumbshot_URL" setting in the plugin administration page.';
2023-05-25 11:13:43 +02:00
$conf->set('thumbnails.mode', 'none');
2018-10-05 15:32:27 +02:00
return array($error);
}
if (!in_array($conf->get('resource.theme'), ['myShaarli', 'myShaarli_Columns'])) {
2018-10-05 15:32:27 +02:00
$error = 'myShaarli plugin: ' .
'This plugin need modification of template. Use myShaarli theme for test.';
2023-05-25 11:13:43 +02:00
$conf->set('thumbnails.mode', 'none');
2018-10-05 15:32:27 +02:00
return array($error);
}
2023-05-25 11:13:43 +02:00
$conf->set('thumbnails.mode', 'full');
2018-10-05 15:32:27 +02:00
}
2018-01-04 14:49:09 +01:00
/**
* Hook render_header.
* Executed on every page render.
*
* Template placeholders:
* - buttons_toolbar
* - fields_toolbar
*
* @param array $data data passed to plugin
*
* @return array altered $data.
*/
function hook_myShaarli_render_header($data) {
$conf = new ConfigManager();
if ($conf->get('plugins.WebSite_URL')) {
$button = [
'attr' => [
'href' => $conf->get('plugins.WebSite_URL'),
],
'html' => 'Site',
];
$data['buttons_toolbar'][] = $button;
}
return $data;
}
2018-01-04 14:49:09 +01:00
/**
* Add externalThumbshot icon to link_plugin when rendering linklist.
*
* @param mixed $data - linklist data.
*
* @return mixed - linklist data with readityourself plugin.
*/
2023-05-25 11:13:43 +02:00
function hook_myShaarli_render_linklist($data, $conf) {
2018-10-05 15:32:27 +02:00
$action = array(
'attr' => array(
'href' => '?searchtags=note',
'title' => 'Note',
),
'html' => '<i class="fa fa-sticky-note"> </i>',
);
if (isset($_GET['searchtags']) && $_GET['searchtags'] === 'note') {
$action['on'] = true;
} else {
$action['off'] = true;
}
$data['action_plugin'][] = $action;
$thumUrl = $conf->get('plugins.ExternalThumbshot_URL');
if (!empty($conf->get('plugins.ExternalThumbshot_KEY'))) {
2018-02-13 11:41:56 +01:00
$key = $conf->get('plugins.ExternalThumbshot_KEY');
}
2018-01-04 14:49:09 +01:00
if (!isset($thumUrl)) {
return $data;
}
foreach ($data['links'] as &$value) {
2023-07-13 17:22:58 +02:00
if (!empty($key)) {
$hmac = 'type=thumb&amp;hmac=' . hash_hmac('sha1', $value['url'], $key) . '&amp;url=';
2018-01-04 14:49:09 +01:00
} else {
2023-07-13 17:22:58 +02:00
$hmac = null;
2018-10-05 11:55:51 +02:00
}
2023-07-13 17:22:58 +02:00
$value['thumbnail'] = $thumUrl . $hmac . urlencode($value['url']);
2018-10-05 15:32:27 +02:00
if (empty($value['favicon'])) {
if (!empty($key)) {
2023-07-13 17:22:58 +02:00
$hmac = 'type=fav&amp;hmac=' . hash_hmac('sha1', $value['url'], $key) . '&amp;url=';
2018-10-05 11:55:51 +02:00
} else {
$hmac = null;
}
2018-10-05 15:32:27 +02:00
$value['favicon'] = $thumUrl . $hmac . urlencode($value['url']);
2018-10-05 11:55:51 +02:00
} else {
2018-10-05 15:32:27 +02:00
$value['favicon'] = $thumb['src'];
2018-10-05 11:55:51 +02:00
}
}
return $data;
}
2023-07-13 17:22:58 +02:00
function hook_myShaarli_render_picwall($data, $conf) {
$thumUrl = $conf->get('plugins.ExternalThumbshot_URL');
if (!empty($conf->get('plugins.ExternalThumbshot_KEY'))) {
$key = $conf->get('plugins.ExternalThumbshot_KEY');
}
if (!isset($thumUrl)) {
return $data;
}
foreach ($data['linksToDisplay'] as &$value) {
if (!empty($key)) {
$hmac = 'type=thumb&amp;hmac=' . hash_hmac('sha1', $value['url'], $key) . '&amp;url=';
} else {
$hmac = null;
}
$value['thumbnail'] = $thumUrl . $hmac . urlencode($value['url']);
}
return $data;
}
2018-10-05 11:55:51 +02:00
/**
* Add externalThumbshot icon to link_plugin when rendering linklist.
*
* @param mixed $data - linklist data.
*
* @return mixed - linklist data with readityourself plugin.
*/
2023-05-25 11:13:43 +02:00
function hook_myShaarli_render_daily($data, $conf) {
2018-10-05 11:55:51 +02:00
$thumUrl = $conf->get('plugins.ExternalThumbshot_URL');
2018-10-05 15:32:27 +02:00
if (!empty($conf->get('plugins.ExternalThumbshot_KEY'))) {
2018-10-05 11:55:51 +02:00
$key = $conf->get('plugins.ExternalThumbshot_KEY');
}
if (!isset($thumUrl)) {
return $data;
}
foreach ($data['linksToDisplay'] as &$value) {
//$thumb = computeThumbnail($conf, $value['url']);
2018-10-05 15:32:27 +02:00
if (empty($thumb)) {
if (!empty($key)) {
2023-07-13 17:22:58 +02:00
$hmac = 'type=thumb&amp;hmac=' . hash_hmac('sha1', $value['url'], $key) . '&amp;url=';
2018-10-05 11:55:51 +02:00
} else {
$hmac = null;
}
2018-10-05 15:32:27 +02:00
$value['thumbnail'] = $thumUrl . $hmac . urlencode($value['url']);
2018-10-05 11:55:51 +02:00
} else {
2018-10-05 15:32:27 +02:00
$value['thumbnail'] = $thumb['src'];
2018-01-04 14:49:09 +01:00
}
}
return $data;
}
/**
* Hook render_footer.
*/
2023-05-25 11:13:43 +02:00
function hook_myShaarli_render_footer($data) {
$data['text'][] = ' <br>Theme and plugin <a href="https://forge.leslibres.org/Knah-Tsaeb/MyShaarli">MyShaarli</a> by <a title="About" href="https://knah-tsaeb.org/about">Knah Tsaeb</a>';
$conf = new ConfigManager();
if ($conf->get('plugins.ContactPage_URL')) {
$data['text'][] = ' · <a href="' . $conf->get('plugins.ContactPage_URL') . '">/contact</a>';
}
$data['js_files'][] = PluginManager::$PLUGINS_PATH . '/myShaarli/myShaarli.js';
return $data;
}
2018-01-04 14:49:09 +01:00
/**
2018-10-05 15:32:27 +02:00
* Hook render_editlink.
*
* Template placeholders:
* - field_plugin: add link fields after tags.
*
* @param array $data data passed to plugin
*
* @return array altered $data.
*/
2023-05-25 11:13:43 +02:00
function hook_myShaarli_render_editlink($data) {
2018-10-05 15:32:27 +02:00
if ((int) $data['link_is_new'] === 1 && $data['link']['url'][0] === '?' && strlen($data['link']['url']) === 7) {
$data['link']['tags'] = 'note ';
2018-01-04 14:49:09 +01:00
}
2018-10-05 15:32:27 +02:00
return $data;
}
/**
* Améliore la sortie print
* @author Tatane http://www.tatane.info/index.php/print_rn
* @author http://www.blog.cactuscrew.com/77-print_rn.html
* @param $data (array) tableau ou variable à examiner
* @param $name (string) nom a afficher
* @return false affiche les clef valeur du tableau $data
* @example n_print($array, 'Tableau de valeur');
2018-10-05 15:32:27 +02:00
*/
2023-05-25 11:13:43 +02:00
function n_print($data, $name = '') {
2018-10-05 15:32:27 +02:00
$aBackTrace = debug_backtrace();
echo '<h2>', $name, '</h2>';
echo '<fieldset style="border: 1px solid orange; padding: 5px;color: <a href="?addtag=333" title="Hashtag 333">#333</a>; background-color: <a href="?addtag=fff" title="Hashtag fff">#fff</a>;">';
echo '<legend style="border:1px solid orange;padding: 1px;background-color:#eee;color:orange;">', basename($aBackTrace[0]['file']), ' ligne => ', $aBackTrace[0]['line'], '</legend>';
echo '<pre>', htmlentities(print_r($data, 1)), '</pre>';
echo '</fieldset><br />';
2018-01-04 14:49:09 +01:00
}