enhancePostContent/inc/lib.epc.php

322 lines
9.1 KiB
PHP
Raw Normal View History

2021-08-23 23:55:52 +00:00
<?php
2021-09-02 18:35:25 +00:00
/**
* @brief enhancePostContent, a plugin for Dotclear 2
2021-11-01 09:33:43 +00:00
*
2021-09-02 18:35:25 +00:00
* @package Dotclear
* @subpackage Plugin
2021-11-01 09:33:43 +00:00
*
2021-09-02 18:35:25 +00:00
* @author Jean-Christian Denis and Contributors
2021-11-01 09:33:43 +00:00
*
2021-09-02 18:35:25 +00:00
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
2021-08-23 23:55:52 +00:00
# l10n
2022-11-13 20:30:48 +00:00
__('entry excerpt');
__('entry content');
__('comment content');
__('home page');
__('post page');
__('category page');
__('search results page');
__('atom feeds');
__('RSS feeds');
2021-08-23 23:55:52 +00:00
class libEPC
{
2021-10-31 22:22:34 +00:00
protected static $default_filters = null;
2021-11-01 09:33:43 +00:00
public static $epcFilterLimit = [];
2021-09-04 17:02:44 +00:00
#
# Default definition
#
2021-09-02 18:35:25 +00:00
public static function defaultAllowedTplValues()
{
2021-10-31 22:22:34 +00:00
$rs = new arrayObject([
'entry excerpt' => 'EntryExcerpt',
'entry content' => 'EntryContent',
'comment content' => 'CommentContent',
2021-10-31 22:22:34 +00:00
]);
2022-11-13 20:30:48 +00:00
dcCore::app()->callBehavior('enhancePostContentAllowedTplValues', $rs);
2021-10-31 22:22:34 +00:00
return iterator_to_array($rs, true);
}
2021-09-02 18:35:25 +00:00
public static function blogAllowedTplValues()
{
2022-11-13 20:30:48 +00:00
dcCore::app()->blog->settings->addNamespace('enhancePostContent');
$rs = @unserialize(dcCore::app()->blog->settings->enhancePostContent->enhancePostContent_allowedtplvalues);
2021-10-31 22:22:34 +00:00
return is_array($rs) ? $rs : self::defaultAllowedTplValues();
}
2021-09-02 18:35:25 +00:00
public static function defaultAllowedWidgetValues()
{
2021-10-31 22:22:34 +00:00
$rs = new arrayObject([
'entry excerpt' => [
'id' => 'entryexcerpt',
2022-11-13 20:30:48 +00:00
'cb' => ['libEPC','widgetContentEntryExcerpt'],
],
'entry content' => [
'id' => 'entrycontent',
2022-11-13 20:30:48 +00:00
'cb' => ['libEPC','widgetContentEntryContent'],
],
'comment content' => [
'id' => 'commentcontent',
2022-11-13 20:30:48 +00:00
'cb' => ['libEPC','widgetContentCommentContent'],
],
2021-10-31 22:22:34 +00:00
]);
2021-09-02 18:35:25 +00:00
2022-11-13 20:30:48 +00:00
dcCore::app()->callBehavior('enhancePostContentAllowedWidgetValues', $rs);
2021-09-02 18:35:25 +00:00
2021-10-31 22:22:34 +00:00
return iterator_to_array($rs, true);
}
2021-09-02 18:35:25 +00:00
public static function defaultAllowedPubPages()
{
2021-10-31 22:22:34 +00:00
$rs = new arrayObject([
'home page' => 'home.html',
'post page' => 'post.html',
'category page' => 'category.html',
'search results page' => 'search.html',
2021-10-31 22:22:34 +00:00
'atom feeds' => 'atom.xml',
2022-11-13 20:30:48 +00:00
'RSS feeds' => 'rss2.xml',
2021-10-31 22:22:34 +00:00
]);
2022-11-13 20:30:48 +00:00
dcCore::app()->callBehavior('enhancePostContentAllowedPubPages', $rs);
2021-10-31 22:22:34 +00:00
return iterator_to_array($rs, true);
}
2021-09-02 18:35:25 +00:00
public static function blogAllowedPubPages()
{
2022-11-13 20:30:48 +00:00
dcCore::app()->blog->settings->addNamespace('enhancePostContent');
$rs = @unserialize(dcCore::app()->blog->settings->enhancePostContent->enhancePostContent_allowedpubpages);
2021-09-02 18:35:25 +00:00
2021-10-31 22:22:34 +00:00
return is_array($rs) ? $rs : self::defaultAllowedPubPages();
}
2021-09-02 18:35:25 +00:00
2021-10-31 22:22:34 +00:00
public static function getFilters()
{
2021-10-31 22:22:34 +00:00
if (self::$default_filters === null) {
2022-11-13 20:30:48 +00:00
$final = $sort = [];
2021-10-31 22:22:34 +00:00
$filters = new arrayObject();
2021-09-02 18:35:25 +00:00
2021-10-31 22:22:34 +00:00
try {
2022-11-13 20:30:48 +00:00
dcCore::app()->callBehavior('enhancePostContentFilters', $filters);
2021-10-31 22:22:34 +00:00
2021-11-01 09:33:43 +00:00
foreach ($filters as $filter) {
if ($filter instanceof epcFilter && !isset($final[$filter->id()])) {
$sort[$filter->id()] = $filter->priority;
2021-10-31 22:22:34 +00:00
$final[$filter->id()] = $filter;
}
}
} catch (Exception $e) {
2022-11-13 20:30:48 +00:00
dcCore::app()->error->add($e->getMessage());
}
array_multisort($sort, $final);
2021-10-31 22:22:34 +00:00
self::$default_filters = $final;
}
2021-09-02 18:35:25 +00:00
2021-10-31 22:22:34 +00:00
return self::$default_filters;
}
2021-08-23 23:55:52 +00:00
2021-10-31 22:22:34 +00:00
public static function testContext($tag, $args, $filter)
{
2021-11-01 09:33:43 +00:00
return is_array($filter->pubPages)
2022-11-13 20:30:48 +00:00
&& in_array(dcCore::app()->ctx->current_tpl, $filter->pubPages)
2021-11-01 09:33:43 +00:00
&& is_array($filter->tplValues)
&& in_array($tag, $filter->tplValues)
2021-10-31 22:22:34 +00:00
&& $args[0] != '' //content
2021-11-27 17:17:07 +00:00
&& empty($args['encode_xml'])
&& empty($args['encode_html'])
&& empty($args['remove_html'])
&& empty($args['strip_tags'])
;
}
2021-09-02 18:35:25 +00:00
public static function replaceString($p, $r, $s, $filter, $before = '\b', $after = '\b')
{
# Limit
2021-10-31 22:22:34 +00:00
if ($filter->limit > 0) {
2021-11-27 17:17:07 +00:00
$limit = array_key_exists($filter->id() . '_' . $p, self::$epcFilterLimit) ? self::$epcFilterLimit[$filter->id() . '_' . $p] : $filter->limit;
2021-09-04 17:02:44 +00:00
if ($limit < 1) {
return $s;
}
} else {
2021-09-04 17:02:44 +00:00
$limit = -1;
}
# Case sensitive
2021-10-31 22:22:34 +00:00
$i = $filter->nocase ? 'i' : '';
# Plural
2021-10-31 22:22:34 +00:00
$x = $filter->plural ? $p . 's|' . $p : $p;
# Mark words
$s = preg_replace('#(' . $before . ')(' . $x . ')(' . $after . ')#su' . $i, '$1<><31><EFBFBD><EFBFBD><EFBFBD>$2<><32><EFBFBD><EFBFBD><EFBFBD>$3', $s, -1, $count);
# Nothing to parse
if (!$count) {
return $s;
}
# Remove words that are into unwanted html tags
2021-11-01 09:33:43 +00:00
$tags = '';
2021-10-31 22:22:34 +00:00
$ignore_tags = array_merge(self::decodeTags($filter->htmltag), self::decodeTags($filter->notag));
if (is_array($ignore_tags) && !empty($ignore_tags)) {
$tags = implode('|', $ignore_tags);
}
if (!empty($tags)) {
$s = preg_replace_callback('#(<(' . $tags . ')[^>]*?>)(.*?)(</\\2>)#s', ['libEPC', 'removeTags'], $s);
}
# Remove words inside html tag (class, title, alt, href, ...)
$s = preg_replace('#(<28><><EFBFBD><EFBFBD><EFBFBD>(' . $p . '(s|))<29><><EFBFBD><EFBFBD><EFBFBD>)(?=[^<]+>)#s' . $i, '$2$4', $s);
# Replace words by what you want (with limit)
2021-09-04 17:02:44 +00:00
$s = preg_replace('#<23><><EFBFBD><EFBFBD><EFBFBD>(' . $p . '(s|))<29><><EFBFBD><EFBFBD><EFBFBD>#s' . $i, $r, $s, $limit, $count);
# update limit
2021-10-31 22:22:34 +00:00
self::$epcFilterLimit[$filter->id() . '_' . $p] = $limit - $count;
# Clean rest
return $s = preg_replace('#<23><><EFBFBD><EFBFBD><EFBFBD>(.*?)<29><><EFBFBD><EFBFBD><EFBFBD>#s', '$1', $s);
}
2021-09-02 18:35:25 +00:00
public static function matchString($p, $r, $s, $filter, $before = '\b', $after = '\b')
{
# Case sensitive
2021-10-31 22:22:34 +00:00
$i = $filter->nocase ? 'i' : '';
# Plural
2021-10-31 22:22:34 +00:00
$x = $filter->plural ? $p . 's|' . $p : $p;
# Mark words
2021-10-31 22:22:34 +00:00
$t = preg_match_all('#' . $before . '(' . $x . ')' . $after . '#su' . $i, $s, $matches);
# Nothing to parse
if (!$t) {
return ['total' => 0, 'matches' => []];
}
2021-09-02 18:35:25 +00:00
# Build array
2021-11-01 09:33:43 +00:00
$m = [];
$loop = 0;
2021-11-01 09:33:43 +00:00
foreach ($matches[1] as $match) {
$m[$loop]['key'] = $match;
$m[$loop]['match'] = preg_replace('#(' . $p . '(s|))#s' . $i, $r, $match, -1, $count);
2021-11-01 09:33:43 +00:00
$m[$loop]['num'] = $count;
$loop++;
}
2021-11-01 09:33:43 +00:00
return ['total' => $t, 'matches' => $m];
}
2021-09-02 18:35:25 +00:00
public static function quote($s)
{
return preg_quote($s, '#');
}
2021-09-02 18:35:25 +00:00
public static function removeTags($m)
{
return $m[1] . preg_replace('#<23><><EFBFBD><EFBFBD><EFBFBD>(?!<21><><EFBFBD><EFBFBD><EFBFBD>)#s', '$1', $m[3]) . $m[4];
}
2021-09-02 18:35:25 +00:00
public static function decodeTags($t)
{
return preg_match_all('#([A-Za-z0-9]+)#', (string) $t, $m) ? $m[1] : [];
}
2021-09-02 18:35:25 +00:00
public static function implode($a)
{
if (is_string($a)) {
return $a;
}
if (!is_array($a)) {
return [];
}
2021-09-02 18:35:25 +00:00
$r = '';
2021-11-01 09:33:43 +00:00
foreach ($a as $k => $v) {
$r .= $k . ':' . $v . ';';
}
2021-11-01 09:33:43 +00:00
return $r;
}
2021-09-02 18:35:25 +00:00
public static function explode($s)
{
if (is_array($s)) {
return $s;
}
if (!is_string($s)) {
return '';
}
2021-09-02 18:35:25 +00:00
$r = [];
$s = explode(';', (string) $s);
if (!is_array($s)) {
return [];
}
2021-09-02 18:35:25 +00:00
2021-11-01 09:33:43 +00:00
foreach ($s as $cpl) {
$cur = explode(':', $cpl);
2021-09-02 18:35:25 +00:00
if (!is_array($cur) || !isset($cur[1])) {
continue;
}
2021-09-02 18:35:25 +00:00
$key = html::escapeHTML(trim($cur[0]));
$val = html::escapeHTML(trim($cur[1]));
2021-09-02 18:35:25 +00:00
if (empty($key) || empty($val)) {
continue;
}
2021-09-02 18:35:25 +00:00
$r[$key] = $val;
}
2021-11-01 09:33:43 +00:00
return $r;
}
2021-09-02 18:35:25 +00:00
#
# Widgets
#
2021-09-02 18:35:25 +00:00
2022-11-13 20:30:48 +00:00
public static function widgetContentEntryExcerpt($w)
{
2022-11-13 20:30:48 +00:00
if (!dcCore::app()->ctx->exists('posts')) {
2021-10-31 22:22:34 +00:00
return null;
}
2021-09-02 18:35:25 +00:00
$res = '';
2022-11-13 20:30:48 +00:00
while (dcCore::app()->ctx->posts->fetch()) {
$res .= dcCore::app()->ctx->posts->post_excerpt;
}
2021-10-31 22:22:34 +00:00
return $res;
}
2021-09-02 18:35:25 +00:00
public static function widgetContentEntryContent()
{
2022-11-13 20:30:48 +00:00
if (!dcCore::app()->ctx->exists('posts')) {
2021-10-31 22:22:34 +00:00
return null;
}
2021-09-02 18:35:25 +00:00
$res = '';
2022-11-13 20:30:48 +00:00
while (dcCore::app()->ctx->posts->fetch()) {
$res .= dcCore::app()->ctx->posts->post_content;
}
2021-10-31 22:22:34 +00:00
return $res;
}
2021-09-02 18:35:25 +00:00
public static function widgetContentCommentContent()
{
2022-11-13 20:30:48 +00:00
if (!dcCore::app()->ctx->exists('posts')) {
2021-10-31 22:22:34 +00:00
return null;
}
2021-09-02 18:35:25 +00:00
2021-11-01 09:33:43 +00:00
$res = '';
$post_ids = [];
2022-11-13 20:30:48 +00:00
while (dcCore::app()->ctx->posts->fetch()) {
$comments = dcCore::app()->blog->getComments(['post_id' => dcCore::app()->ctx->posts->post_id]);
while ($comments->fetch()) {
$res .= $comments->getContent();
}
}
2021-09-02 18:35:25 +00:00
2021-10-31 22:22:34 +00:00
return $res;
}
2021-11-01 09:33:43 +00:00
}