primo commit

This commit is contained in:
2024-12-17 17:34:10 +01:00
commit e650f8df99
16435 changed files with 2451012 additions and 0 deletions

View File

@ -0,0 +1,26 @@
<?php
/**
* @package Conditional Content
* @version 2.7.1
*
* @author Peter van Westen <info@regularlabs.com>
* @link http://www.regularlabs.com
* @copyright Copyright © 2020 Regular Labs All Rights Reserved
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
*/
namespace RegularLabs\Plugin\System\ConditionalContent;
defined('_JEXEC') or die;
class Clean
{
/**
* Just in case you can't figure the method name out: this cleans the left-over junk
*/
public static function cleanLeftoverJunk(&$string)
{
Protect::unprotectTags($string);
//RL_Protect::removeInlineComments($string, 'ConditionalContent');
}
}

View File

@ -0,0 +1,72 @@
<?php
/**
* @package Conditional Content
* @version 2.7.1
*
* @author Peter van Westen <info@regularlabs.com>
* @link http://www.regularlabs.com
* @copyright Copyright © 2020 Regular Labs All Rights Reserved
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
*/
namespace RegularLabs\Plugin\System\ConditionalContent;
defined('_JEXEC') or die;
use Joomla\CMS\Factory as JFactory;
use RegularLabs\Library\Article as RL_Article;
use RegularLabs\Library\Document as RL_Document;
use RegularLabs\Library\Html as RL_Html;
/**
* Plugin that replaces stuff
*/
class Helper
{
public function onContentPrepare($context, &$article, &$params)
{
$area = isset($article->created_by) ? 'article' : 'other';
$context = (($params instanceof \JRegistry) && $params->get('rl_search')) ? 'com_search.' . $params->get('readmore_limit') : $context;
RL_Article::process($article, $context, $this, 'replaceTags', [$area, $context]);
}
public function onAfterDispatch()
{
if ( ! $buffer = RL_Document::getBuffer())
{
return;
}
if ( ! Replace::replaceTags($buffer, 'component'))
{
return;
}
RL_Document::setBuffer($buffer);
}
public function onAfterRender()
{
$html = JFactory::getApplication()->getBody();
if ($html == '')
{
return;
}
// only do stuff in body
list($pre, $body, $post) = RL_Html::getBody($html);
Replace::replaceTags($body, 'body');
$html = $pre . $body . $post;
Clean::cleanLeftoverJunk($html);
JFactory::getApplication()->setBody($html);
}
public function replaceTags(&$string, $area = 'article', $context = '')
{
Replace::replaceTags($string, $area, $context);
}
}

View File

@ -0,0 +1,146 @@
<?php
/**
* @package Conditional Content
* @version 5.2.2
*
* @author Peter van Westen <info@regularlabs.com>
* @link https://regularlabs.com
* @copyright Copyright © 2024 Regular Labs All Rights Reserved
* @license GNU General Public License version 2 or later
*/
namespace RegularLabs\Plugin\System\ConditionalContent;
defined('_JEXEC') or die;
use RegularLabs\Library\Parameters as RL_Parameters;
use RegularLabs\Library\PluginTag as RL_PluginTag;
use RegularLabs\Library\RegEx as RL_RegEx;
class Params
{
protected static $params = null;
protected static $regexes = [];
public static function get()
{
if ( ! is_null(self::$params))
{
return self::$params;
}
$params = RL_Parameters::getPlugin('conditionalcontent');
$params->tag_show = RL_PluginTag::clean($params->tag_show);
$params->tag_hide = RL_PluginTag::clean($params->tag_hide);
self::$params = $params;
return self::$params;
}
public static function getRegex()
{
if (isset(self::$regexes['main']))
{
return self::$regexes['main'];
}
$params = self::get();
// Tag character start and end
[$tag_start, $tag_end] = Params::getTagCharacters();
$pre = RL_PluginTag::getRegexSurroundingTagsPre();
$post = RL_PluginTag::getRegexSurroundingTagsPost();
$inside_tag = RL_PluginTag::getRegexInsideTag($tag_start, $tag_end);
$spaces = RL_PluginTag::getRegexSpaces();
$tag_start = RL_RegEx::quote($tag_start);
$tag_end = RL_RegEx::quote($tag_end);
self::$regexes['main'] =
'(?<start_pre>' . $pre . ')'
. $tag_start . '(?<tag>' . RL_RegEx::quote($params->tag_show) . '|' . RL_RegEx::quote($params->tag_hide) . ')'
. '(?<data>(?:(?:' . $spaces . '|<)' . $inside_tag . ')?)' . $tag_end
. '(?<start_post>' . $post . ')'
. '(?<content>.*?)'
. '(?<end_pre>' . $pre . ')'
. $tag_start . '/\2' . $tag_end
. '(?<end_post>' . $post . ')';
return self::$regexes['main'];
}
public static function getRegexElse($type = 'show')
{
if (isset(self::$regexes[$type]))
{
return self::$regexes[$type];
}
$params = self::get();
// Tag character start and end
[$tag_start, $tag_end] = Params::getTagCharacters();
$pre = RL_PluginTag::getRegexSurroundingTagsPre();
$post = RL_PluginTag::getRegexSurroundingTagsPost();
$inside_tag = RL_PluginTag::getRegexInsideTag($tag_start, $tag_end);
$spaces = RL_PluginTag::getRegexSpaces();
$tag_start = RL_RegEx::quote($tag_start);
$tag_end = RL_RegEx::quote($tag_end);
$type = $type === 'hide' ? $params->tag_hide : $params->tag_show;
self::$regexes[$type] =
'(?<else_pre>' . $pre . ')'
. $tag_start . RL_RegEx::quote($type) . '-else'
. '(?<data>(?:(?:' . $spaces . '|<)' . $inside_tag . ')?)' . $tag_end
. '(?<else_post>' . $post . ')';
return self::$regexes[$type];
}
public static function getTagCharacters()
{
$params = self::get();
if ( ! isset($params->tag_character_start))
{
self::setTagCharacters();
}
return [$params->tag_character_start, $params->tag_character_end];
}
public static function getTags($only_start_tags = false)
{
$params = self::get();
[$tag_start, $tag_end] = self::getTagCharacters();
$tags = [
[
$tag_start . $params->tag_show,
$tag_start . $params->tag_hide,
],
[
$tag_start . '/' . $params->tag_show . $tag_end,
$tag_start . '/' . $params->tag_hide . $tag_end,
],
];
return $only_start_tags ? $tags[0] : $tags;
}
public static function setTagCharacters()
{
$params = self::get();
[self::$params->tag_character_start, self::$params->tag_character_end] = explode('.', $params->tag_characters);
}
}

View File

@ -0,0 +1,402 @@
<?php
/**
* @package Conditional Content
* @version 2.7.1
*
* @author Peter van Westen <info@regularlabs.com>
* @link http://www.regularlabs.com
* @copyright Copyright © 2020 Regular Labs All Rights Reserved
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
*/
/*
* This class is used as template (extend) for most Regular Labs plugins
* This class is not placed in the Regular Labs Library as a re-usable class because
* it also needs to be working when the Regular Labs Library is not installed
*/
namespace RegularLabs\Plugin\System\ConditionalContent;
defined('_JEXEC') or die;
if (is_file(JPATH_LIBRARIES . '/regularlabs/autoload.php'))
{
require_once JPATH_LIBRARIES . '/regularlabs/autoload.php';
}
use Joomla\CMS\Factory as JFactory;
use Joomla\CMS\Installer\Installer as JInstaller;
use Joomla\CMS\Language\Text as JText;
use Joomla\CMS\Plugin\CMSPlugin as JPlugin;
use Joomla\CMS\Plugin\PluginHelper as JPluginHelper;
use ReflectionMethod;
use RegularLabs\Library\Document as RL_Document;
use RegularLabs\Library\Language as RL_Language;
use RegularLabs\Library\Protect as RL_Protect;
class Plugin extends JPlugin
{
public $_alias = '';
public $_title = '';
public $_lang_prefix = '';
public $_has_tags = false;
public $_enable_in_frontend = true;
public $_enable_in_admin = false;
public $_can_disable_by_url = true;
public $_disable_on_components = false;
public $_protected_formats = [];
public $_page_types = [];
private $_init = false;
private $_pass = null;
private $_helper = null;
protected function run()
{
if ( ! $this->passChecks())
{
return false;
}
if ( ! $this->getHelper())
{
return false;
}
$caller = debug_backtrace()[1];
if (empty($caller))
{
return false;
}
$event = $caller['function'];
if ( ! method_exists($this->_helper, $event))
{
return false;
}
$reflect = new ReflectionMethod($this->_helper, $event);
$parameters = $reflect->getParameters();
$arguments = [];
// Check if arguments should be passed as reference or not
foreach ($parameters as $count => $parameter)
{
if ($parameter->isPassedByReference())
{
$arguments[] = &$caller['args'][$count];
continue;
}
$arguments[] = $caller['args'][$count];
}
// Work-around for K2 stuff :(
if ($event == 'onContentPrepare'
&& empty($arguments[1]->id)
&& strpos($arguments[0], 'com_k2') === 0
)
{
return false;
}
return call_user_func_array([$this->_helper, $event], $arguments);
}
/**
* Create the helper object
*
* @return object|null The plugins helper object
*/
private function getHelper()
{
// Already initialized, so return
if ($this->_init)
{
return $this->_helper;
}
$this->_init = true;
RL_Language::load('plg_' . $this->_type . '_' . $this->_name);
$this->init();
$this->_helper = new Helper;
return $this->_helper;
}
private function passChecks()
{
if ( ! is_null($this->_pass))
{
return $this->_pass;
}
$this->_pass = false;
if ( ! $this->isFrameworkEnabled())
{
return false;
}
if ( ! self::passPageTypes())
{
return false;
}
// allow in frontend?
if ( ! $this->_enable_in_frontend
&& ! RL_Document::isAdmin())
{
return false;
}
// allow in admin?
if ( ! $this->_enable_in_admin
&& RL_Document::isAdmin()
&& ( ! isset(Params::get()->enable_admin) || ! Params::get()->enable_admin))
{
return false;
}
// disabled by url?
if ($this->_can_disable_by_url
&& RL_Protect::isDisabledByUrl($this->_alias))
{
return false;
}
// disabled by component?
if ($this->_disable_on_components
&& RL_Protect::isRestrictedComponent(isset(Params::get()->disabled_components) ? Params::get()->disabled_components : [], 'component'))
{
return false;
}
// restricted page?
if (RL_Protect::isRestrictedPage($this->_has_tags, $this->_protected_formats))
{
return false;
}
if ( ! $this->extraChecks())
{
return false;
}
$this->_pass = true;
return true;
}
public function passPageTypes()
{
if (empty($this->_page_types))
{
return true;
}
if (in_array('*', $this->_page_types))
{
return true;
}
if (empty(JFactory::$document))
{
return true;
}
if (RL_Document::isFeed())
{
return in_array('feed', $this->_page_types);
}
if (RL_Document::isPDF())
{
return in_array('pdf', $this->_page_types);
}
$page_type = JFactory::getDocument()->getType();
if (in_array($page_type, $this->_page_types))
{
return true;
}
return false;
}
public function extraChecks()
{
$input = JFactory::getApplication()->input;
// Disable on Gridbox edit form: option=com_gridbox&view=gridbox
if ($input->get('option') == 'com_gridbox' && $input->get('view') == 'gridbox')
{
return false;
}
// Disable on SP PageBuilder edit form: option=com_sppagebuilder&view=form
if ($input->get('option') == 'com_sppagebuilder' && $input->get('view') == 'form')
{
return false;
}
return true;
}
public function init()
{
return;
}
/**
* Check if the Regular Labs Library is enabled
*
* @return bool
*/
private function isFrameworkEnabled()
{
if ( ! defined('REGULAR_LABS_LIBRARY_ENABLED'))
{
$this->setIsFrameworkEnabled();
}
if ( ! REGULAR_LABS_LIBRARY_ENABLED)
{
$this->throwError('REGULAR_LABS_LIBRARY_NOT_ENABLED');
}
return REGULAR_LABS_LIBRARY_ENABLED;
}
/**
* Set the define with whether the Regular Labs Library is enabled
*/
private function setIsFrameworkEnabled()
{
// Return false if Regular Labs Library is not installed
if ( ! $this->isFrameworkInstalled())
{
define('REGULAR_LABS_LIBRARY_ENABLED', false);
return;
}
if ( ! JPluginHelper::isEnabled('system', 'regularlabs'))
{
$this->throwError('REGULAR_LABS_LIBRARY_NOT_ENABLED');
define('REGULAR_LABS_LIBRARY_ENABLED', false);
return;
}
define('REGULAR_LABS_LIBRARY_ENABLED', true);
}
/**
* Check if the Regular Labs Library is installed
*
* @return bool
*/
private function isFrameworkInstalled()
{
if ( ! defined('REGULAR_LABS_LIBRARY_INSTALLED'))
{
$this->setIsFrameworkInstalled();
}
switch (REGULAR_LABS_LIBRARY_INSTALLED)
{
case 'outdated':
$this->throwError('REGULAR_LABS_LIBRARY_OUTDATED');
return false;
case 'no':
$this->throwError('REGULAR_LABS_LIBRARY_NOT_INSTALLED');
return false;
case 'yes':
default:
return true;
}
}
/**
* set the define with whether the Regular Labs Library is installed
*/
private function setIsFrameworkInstalled()
{
if (
! is_file(JPATH_PLUGINS . '/system/regularlabs/regularlabs.xml')
|| ! is_file(JPATH_LIBRARIES . '/regularlabs/autoload.php')
)
{
define('REGULAR_LABS_LIBRARY_INSTALLED', 'no');
return;
}
$plugin = JInstaller::parseXMLInstallFile(JPATH_PLUGINS . '/system/regularlabs/regularlabs.xml');
$library = JInstaller::parseXMLInstallFile(JPATH_LIBRARIES . '/regularlabs/regularlabs.xml');
if (empty($plugin) || empty($library))
{
define('REGULAR_LABS_LIBRARY_INSTALLED', 'no');
return;
}
if (version_compare($plugin['version'], '20.7.20564', '<')
|| version_compare($library['version'], '20.7.20564', '<'))
{
define('REGULAR_LABS_LIBRARY_INSTALLED', 'outdated');
return;
}
define('REGULAR_LABS_LIBRARY_INSTALLED', 'yes');
}
/**
* Place an error in the message queue
*/
private function throwError($error)
{
// Return if page is not an admin page or the admin login page
if (
! JFactory::getApplication()->isClient('administrator')
|| JFactory::getUser()->get('guest')
)
{
return;
}
// load the admin language file
JFactory::getLanguage()->load('plg_' . $this->_type . '_' . $this->_name, JPATH_PLUGINS . '/' . $this->_type . '/' . $this->_name);
$text = JText::sprintf($this->_lang_prefix . '_' . $error, JText::_($this->_title));
$text = JText::_($text) . ' ' . JText::sprintf($this->_lang_prefix . '_EXTENSION_CAN_NOT_FUNCTION', JText::_($this->_title));
// Check if message is not already in queue
$messagequeue = JFactory::getApplication()->getMessageQueue();
foreach ($messagequeue as $message)
{
if ($message['message'] == $text)
{
return;
}
}
JFactory::getApplication()->enqueueMessage($text, 'error');
}
}

View File

@ -0,0 +1,50 @@
<?php
/**
* @package Conditional Content
* @version 5.2.2
*
* @author Peter van Westen <info@regularlabs.com>
* @link https://regularlabs.com
* @copyright Copyright © 2024 Regular Labs All Rights Reserved
* @license GNU General Public License version 2 or later
*/
namespace RegularLabs\Plugin\System\ConditionalContent;
defined('_JEXEC') or die;
use RegularLabs\Library\Protect as RL_Protect;
class Protect
{
static $name = 'ConditionalContent';
public static function _(&$string)
{
RL_Protect::protectHtmlCommentTags($string);
RL_Protect::protectFields($string, Params::getTags(true));
RL_Protect::protectSourcerer($string);
}
public static function protectTags(&$string)
{
RL_Protect::protectTags($string, Params::getTags(true));
}
public static function unprotectTags(&$string)
{
RL_Protect::unprotectTags($string, Params::getTags(true));
}
/**
* Wrap the comment in comment tags
*
* @param string $comment
*
* @return string
*/
public static function wrapInCommentTags($comment)
{
return RL_Protect::wrapInCommentTags(self::$name, $comment);
}
}

View File

@ -0,0 +1,230 @@
<?php
/**
* @package Conditional Content
* @version 5.2.2
*
* @author Peter van Westen <info@regularlabs.com>
* @link https://regularlabs.com
* @copyright Copyright © 2024 Regular Labs All Rights Reserved
* @license GNU General Public License version 2 or later
*/
namespace RegularLabs\Plugin\System\ConditionalContent;
defined('_JEXEC') or die;
use RegularLabs\Component\Conditions\Administrator\Api\Conditions as Api_Conditions;
use RegularLabs\Library\Html as RL_Html;
use RegularLabs\Library\PluginTag as RL_PluginTag;
use RegularLabs\Library\Protect as RL_Protect;
use RegularLabs\Library\RegEx as RL_RegEx;
use RegularLabs\Library\StringHelper as RL_String;
class Replace
{
static $article;
public static function replaceTags(&$string, $area = 'article', $context = '', $article = null)
{
self::$article = $article;
if ( ! is_string($string) || $string == '')
{
return false;
}
if ( ! RL_String::contains($string, Params::getTags(true)))
{
return false;
}
// Check if tags are in the text snippet used for the search component
if (str_starts_with($context, 'com_search.'))
{
$limit = explode('.', $context, 2);
$limit = (int) array_pop($limit);
$string_check = substr($string, 0, $limit);
if ( ! RL_String::contains($string_check, Params::getTags(true)))
{
return false;
}
}
$params = Params::get();
$regex = Params::getRegex();
// allow in component?
if (RL_Protect::isRestrictedComponent($params->disabled_components ?? [], $area))
{
Protect::_($string);
$string = RL_RegEx::replace($regex, '\2', $string);
RL_Protect::unprotect($string);
return true;
}
Protect::_($string);
[$start_tags, $end_tags] = Params::getTags();
[$pre_string, $string, $post_string] = RL_Html::getContentContainingSearches(
$string,
$start_tags,
$end_tags
);
RL_RegEx::matchAll($regex, $string, $matches);
foreach ($matches as $match)
{
self::replaceTag($string, $match);
}
$string = $pre_string . $string . $post_string;
RL_Protect::unprotect($string);
return true;
}
private static function getContent($match)
{
$params = Params::get();
$parts = self::getParts($match);
foreach ($parts as $part)
{
$attributes = self::getTagValues($part->data);
unset($attributes->trim);
$has_access = self::hasAccess($attributes);
$has_access = $match['tag'] == $params->tag_hide ? ! $has_access : $has_access;
if ($has_access)
{
return $part->content;
}
}
return '';
}
private static function getParts($data)
{
$params = Params::get();
$regex = Params::getRegexElse($data['tag'] == $params->tag_hide ? 'hide' : 'show');
RL_RegEx::matchAll($regex, $data['content'], $matches, null, PREG_OFFSET_CAPTURE);
if (empty($matches) || empty($matches[0]))
{
return [
(object) [
'data' => $data['data'],
'content' => $data['content'],
],
];
}
$parts = [
(object) [
'data' => $data['data'],
'content' => substr($data['content'], 0, $matches[0][0][1]),
],
];
foreach ($matches[0] as $i => $match)
{
$offset = $match[1] + strlen($match[0]);
$next_pos = isset($matches[0][$i + 1])
? $matches[0][$i + 1][1]
: strlen($data['content']);
$length = $next_pos - $offset;
$parts[] = (object) [
'data' => $matches['data'][$i][0],
'content' => substr($data['content'], $offset, $length),
];
}
return $parts;
}
private static function getTagValues($string)
{
$string = html_entity_decode($string);
return RL_PluginTag::getAttributesFromString($string, null, [], false);
}
private static function hasAccess($attributes)
{
if (empty($attributes))
{
return true;
}
$conditions = [
'menu__menu_item',
'menu__home_page',
'date__date',
'visitor__access_level',
'visitor__user_group',
'visitor__language',
'agent__device',
'other__condition',
];
return (new Api_Conditions(self::$article))
->setConditionByAttributes($attributes)
->pass($conditions);
}
private static function replaceTag(&$string, $match)
{
$params = Params::get();
$content = self::getContent($match);
$attributes = self::getTagValues($match['data']);
$trim = $attributes->trim ?? $params->trim;
if ($trim)
{
$tags = RL_Html::cleanSurroundingTags([
'start_pre' => $match['start_pre'],
'start_post' => $match['start_post'],
], ['p', 'span', 'div']);
$match = [...$match, ...$tags];
$tags = RL_Html::cleanSurroundingTags([
'end_pre' => $match['end_pre'],
'end_post' => $match['end_post'],
], ['p', 'span', 'div']);
$match = [...$match, ...$tags];
$tags = RL_Html::cleanSurroundingTags([
'start_pre' => $match['start_pre'],
'end_post' => $match['end_post'],
], ['p', 'span', 'div']);
$match = [...$match, ...$tags];
}
if ($params->place_comments)
{
$content = Protect::wrapInCommentTags($content);
}
$replace = $match['start_pre'] . $match['start_post'] . $content . $match['end_pre'] . $match['end_post'];
$string = str_replace($match[0], $replace, $string);
}
}