www_archline_hu/deployed/mightysites/plugins/system/mightysites/mightysites.php
LÁZÁR Imre AI Agent 0e2d7a7410 feat(deployed): add MightySites (no-source, vetted live)
MightySites (AlterBrains, com 3.2.5 / plg 2.1.5); subscription-only, no public download -> deployed files.

Signed-off-by: LÁZÁR Imre <imre@illusion.hu>
Assisted-by: claude-code@claude-opus-4-8
2026-07-16 11:24:44 +02:00

580 lines
19 KiB
PHP

<?php
/**
* @package Mightysites
* @copyright Copyright (C) 2009-2013 AlterBrains.com. All rights reserved.
* @license GNU/GPL, see LICENSE.php
*/
defined('_JEXEC') or die('Restricted access');
// todo - remove once 2.5 is dead
jimport('joomla.plugin.plugin');
// Base class, actually not used but will be useful soon.
class plgSystemMightysitesBase extends JPlugin
{
}
$app = JFactory::getApplication();
$config = JFactory::getConfig();
// Frontend options
if ($app->isSite())
{
class plgSystemMightysites extends plgSystemMightysitesBase
{
// Start single login?
public function onUserLogin($user, $options = array())
{
$app = JFactory::getApplication();
// Not used if we login via our SSI plugin!
if ($app->getCfg('mighty_slogin') && $app->getCfg('mighty_sdomains') && !isset($_REQUEST['mighty_login']))
{
$_SESSION['mightylogin'] = array(
'username' => $user['username'],
'password' => $user['password'],
'remember' => isset($options['remember']) ? $options['remember'] : 0,
);
}
}
// Start single logout?
public function onUserLogout($user, $options = array())
{
$app = JFactory::getApplication();
// Not used if we logout via our SSI plugin!
if ($app->getCfg('mighty_slogout') && $app->getCfg('mighty_sdomains') && !isset($_REQUEST['mighty_logout'])) {
setcookie('mightylogout', 1, time()+3600, '/');
// No page cache! At all, othwerwise logout redirect will load cached page.
$this->disableCache(false);
}
}
// Start single login/logout to other sites!
public function onBeforeCompileHead()
{
$config = JFactory::getConfig();
$document = JFactory::getDocument();
if ($document->getType() == 'html' && isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'GET')
{
// Login there
if (isset($_SESSION['mightylogin']))
{
$user = $_SESSION['mightylogin'];
unset($_SESSION['mightylogin']);
jimport('joomla.utilities.simplecrypt');
jimport('joomla.utilities.utility');
$domains = $config->get('mighty_sdomains');
foreach($domains as $domain => $secret)
{
$key = md5($secret . @$_SERVER['HTTP_USER_AGENT']);
$crypt = new JSimpleCrypt($key);
$hash = base64_encode($crypt->encrypt(serialize($user)));
// possible 'suhosin.get.max_value_length' issue, let's use array
$hash = implode('&mighty_login[]=', str_split($hash, 250));
$document->addScript('http://'.$domain.'/index.php?'.md5($_SERVER['REQUEST_TIME']).'=1&amp;mighty_login[]='.$hash, 'text/javascript', true);
}
// No page cache!
$this->disableCache();
}
// Logout there
if (isset($_COOKIE['mightylogout'])) {
setcookie('mightylogout', '', time()-86400, '/');
$domains = $config->get('mighty_sdomains');
foreach($domains as $domain => $secret) {
$document->addScript('http://'.$domain.'/index.php?mighty_logout=1&amp;'.md5($_SERVER['REQUEST_TIME']).'=1', 'text/javascript', true);
}
// No page cache! But enable it if previously disabled
$this->disableCache(true);
}
// Custom CSS files
if ($config->get('mighty_css')) {
foreach((array)$config->get('mighty_css') as $css_file) {
$document->addStylesheet($css_file);
}
}
// Custom JavaScript
if ($config->get('mighty_js')) {
$document->addScriptDeclaration($config->get('mighty_js'));
}
// Custom Favicon
if ($config->get('mighty_favicon')) {
foreach($document->_links as $i => $link) {
if ($link['relation'] == 'shortcut icon') {
unset($document->_links[$i]);
}
}
$document->addFavicon($config->get('mighty_favicon'));
}
}
}
public function onBeforeRender()
{
$config = JFactory::getConfig();
$document = JFactory::getDocument();
if ($document->getType() == 'html')
{
// Remove modules
$mighty_modules = (array)$config->get('mighty_hidemodules');
if ($mighty_modules && $mighty_modules != array(''))
{
$mighty_modules = array_flip($mighty_modules);
if (version_compare(JVERSION, '3.2', 'ge')) {
$rMethod = new ReflectionMethod('JModuleHelper', 'load');
} else {
$rMethod = new ReflectionMethod('JModuleHelper', '_load');
}
$rMethod->setAccessible(true);
$items = $rMethod->invoke(null);
foreach($items as &$item) {
if (isset($mighty_modules[$item->id]) || isset($mighty_modules[$item->id])) {
$mighty_modules[$item->id] = true;
$item->module = 'unknown'; // ;)
}
}
}
}
}
public function onAfterDispatch()
{
// Lost database handler?
if (JFactory::getConfig()->get('config.mighty_enable') && get_class(JFactory::$database) != 'JDatabaseMightysites') {
JDatabaseMightysites::changeHandler();
}
}
protected function disableCache($plugin_status = null)
{
// Change plugin status
if (isset($plugin_status)) {
$db = JFactory::getDbo();
// Disable only if it's currently enabled
if ($plugin_status === false && class_exists('PlgSystemCache')) {
$db->setQuery('UPDATE #__extensions SET `enabled`=0 WHERE `type`="plugin" AND `element`="cache" AND `folder`="system"');
$db->execute();
setcookie('mightylogout_cache', 1, time()+3600, '/');
}
// Enable only if it was previously disabled
if ($plugin_status === true && isset($_COOKIE['mightylogout_cache'])) {
$db->setQuery('UPDATE #__extensions SET `enabled`=1 WHERE `type`="plugin" AND `element`="cache" AND `folder`="system"');
$db->execute();
setcookie('mightylogout_cache', '', time()-86400, '/');
}
// Clear system cache
if (JFactory::getConfig()->get('caching') > 0) {
$cache = JFactory::getCache();
$cache->clean('com_plugins');
}
}
// Just disable caching of current page.
if (class_exists('PlgSystemCache')) {
$dispatcher = JDispatcher::getInstance();
$_methods = $dispatcher->get('_methods');
foreach($dispatcher->get('_observers') as $oId => $observer) {
if (is_object($observer) && strtolower(get_class($observer)) == 'plgsystemcache') {
foreach($_methods['onafterrender'] as $key => $value) {
if ($value == $oId) {
unset($_methods['onafterrender'][$key]);
$dispatcher->set('_methods', $_methods);
break;
}
}
}
}
}
}
}
// Implicitely setup language, getLanguage() only next!
if ($lang_code = $config->get('mighty_language')) {
$config->set('language', $lang_code);
if (version_compare(JVERSION, '3.2', 'ge')) {
// Build our language object
$lang = JLanguage::getInstance($config->get('language'), $config->get('debug_lang'));
// Load the language to the API
$app->loadLanguage($lang);
// Register the language object with JFactory
JFactory::$language = $app->getLanguage();
}
// Override language in params only if language filter plugin is enabled
if (class_exists('PlgSystemLanguageFilter'))
{
$lparams = JComponentHelper::getParams('com_languages');
$lparams->set('site', $lang_code);
$rProperty = new ReflectionProperty('PlgSystemLanguageFilter', 'default_lang');
$rProperty->setAccessible(true);
$rProperty->setValue('PlgSystemLanguageFilter', $lang_code);
$rProperty2 = new ReflectionProperty('PlgSystemLanguageFilter', 'default_sef');
$rProperty2->setAccessible(true);
$rProperty2->setValue('PlgSystemLanguageFilter', substr($lang_code, 0, 2)); // simple, we don't use language->sef
$rProperty3 = new ReflectionProperty('PlgSystemLanguageFilter', 'cookie');
$rProperty3->setAccessible(true);
$rProperty3->setValue('PlgSystemLanguageFilter', true); // simple, we don't use language->sef
// Create a cookie
$cookie_domain = $config->get('config.cookie_domain', '');
$cookie_path = $config->get('config.cookie_path', '/');
setcookie(JApplication::getHash('language'), $lang_code, time() + 365 * 86400, $cookie_path, $cookie_domain);
$app->input->cookie->set(JApplication::getHash('language'), $lang_code);
// set the request var
$app->input->set('language', $lang_code);
// $uri = JFactory::getURI();
// $uri->setVar('lang', $lang_code);
}
}
// Now we can load language, but override it first!
$lang = JFactory::getLanguage();
// Implicitely setup template
if ($config->get('mighty_template')) {
$app->input->set('templateStyle', $config->get('mighty_template'));
$app->getTemplate();
}
// Implicitely setup home menu item
if ($config->get('mighty_home'))
{
$menu = $app->getMenu();
if ($menu->getDefault()) {
$menu->getDefault()->home = 0;
}
if ($menu->getDefault($lang->getTag())) {
$menu->getDefault($lang->getTag())->home = 0;
}
$menu->setDefault($config->get('mighty_home'), $lang->getTag());
$menu->setDefault($config->get('mighty_home'), '*');
$menu->getDefault()->home = 1;
}
// Remove menu items
$mighty_menuitems = (array)$config->get('mighty_hidemenus');
if ($mighty_menuitems && $mighty_menuitems != array(''))
{
$mighty_menuitems = array_flip($mighty_menuitems);
$menu = $app->getMenu();
// I hate Joomla sometimes... smbd is crazy on privates which are useless
$rProperty = new ReflectionProperty($menu, '_items');
$rProperty->setAccessible(true);
$items = $rProperty->getValue($menu);
foreach($items as $key => &$item) {
if (isset($mighty_menuitems[$item->id]) || isset($mighty_menuitems[$item->id])) {
$mighty_menuitems[$item->id] = true;
unset($items[$key]);
}
}
$rProperty->setValue($menu, $items);
}
// Load language overrides,
if ($config->get('mighty_langoverride'))
{
$domain = $_SERVER['HTTP_HOST'] ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
$domain = (substr($domain, 0, 4) == 'www.') ? substr($domain, 4) : $domain;
$domain = preg_replace('#([^A-Z0-9])#i', '_', $domain);
$override_file = JPATH_SITE.'/language/overrides/'.$domain.'.'.$lang->getTag().'.override.ini';
if (file_exists($override_file)) {
$rProperty = new ReflectionProperty($lang, 'override');
$rProperty->setAccessible(true);
$override = $rProperty->getValue($lang);
$rMethod = new ReflectionMethod($lang, 'parse');
$rMethod->setAccessible(true);
$contents = $rMethod->invoke($lang, $override_file);
if (is_array($contents)) {
$override = array_merge($override, $contents);
}
$lang->set('override', $override);
}
}
// MijoShop ID
if ($config->get('mighty_mijoshopid') !== '') {
$app->input->set('mijoshop_store_id', $config->get('mighty_mijoshopid'));
}
}
// Backend options
if (JFactory::getApplication()->isAdmin())
{
class plgSystemMightysites extends plgSystemMightysitesBase
{
public function onAfterRoute()
{
$app = JFactory::getApplication();
if (isset($_REQUEST['mighty_token']) && strlen($_REQUEST['mighty_token'])) {
$token = $app->input->getString('mighty_token');
$folder = $app->getCfg('tmp_path');
jimport('joomla.filesystem.folder');
jimport('joomla.filesystem.file');
$files = JFolder::files($folder, '\.mighty$');
if (sizeof($files)) {
$data = false;
foreach($files as $file) {
if ($file == md5($token.$app->getCfg('secret')).'.mighty') {
jimport('joomla.filesystem.file');
$data = JFile::read($folder.'/'.$file);
}
JFile::delete($folder.'/'.$file);
}
if ($data) {
$data = unserialize($data);
if (!JFactory::getUser()->id) {
$user = new JUser();
$user->load($data['user_id']);
// try load by username next
if (!$user->id) {
// remove "JUser: :_load: Unable to load user with id: 42" message
$session = JFactory::getSession();
$session->set('application.queue', null);
// load other admin by username
$db = JFactory::getDBO();
$db->setQuery('SELECT id FROM #__users WHERE `username`='.$db->quote($data['username']));
$user->load($db->loadResult());
}
if ($user->id) {
// Mark the user as logged in
$user->set('guest', 0);
// Register the needed session variables
$session = JFactory::getSession();
$session->set('user', $user);
$db = JFactory::getDBO();
// Check to see the the session already exists.
$app = JFactory::getApplication();
$app->checkSession();
// Update the user related fields for the Joomla sessions table.
$db->setQuery(
'UPDATE `#__session`' .
' SET `guest` = '.$db->quote($user->get('guest')).',' .
' `username` = '.$db->quote($user->get('username')).',' .
' `userid` = '.(int) $user->get('id') .
' WHERE `session_id` = '.$db->quote($session->getId())
);
$db->query();
// Hit the user last visit field
$user->setLastVisit();
}
}
if (strpos($data['return'], 'index.php') === 0) {
$data['return'] = JUri::base(true) . '/' . ltrim($data['return'], '/');
}
$app->redirect($data['return']);
}
}
}
}
public function onAfterDispatch()
{
$app = JFactory::getApplication();
// our message from saving modal config
if ($app->input->get('option') == 'com_mightysites') {
if ($app->input->get('mighty_saved')) {
$app->enqueueMessage(JText::sprintf('COM_MIGHTYSITES_CONFIG_UPDATED', $app->input->get('mighty_saved')));
}
}
// let's rock
else if ($app->input->get('option') == 'com_config') {
if ($app->input->get('controller') != 'component' && $app->input->get('view') != 'component' && $app->input->get('tmpl') == 'component') {
require_once(JPATH_ADMINISTRATOR.'/components/com_mightysites/helpers/helper.php');
$document = JFactory::getDocument();
$version = new JVersion;
if ($version->RELEASE == '2.5') {
JToolBarHelper::title(JText::_('COM_CONFIG') . ' :: ' . MightysitesHelper::getHost(), 'config.png');
$html = '
<div id="toolbar-box">
<div class="m">';
$toolbar = $document->getBuffer('modules', 'toolbar', array('name'=>'toolbar'));
$toolbar = strtr($toolbar, array(
'Joomla.submitbutton(\'application.cancel\')' => 'try {window.parent.SqueezeBox.close()} catch(e){self.top.location=\''.base64_decode($app->input->getString('mighty')).'\';}; return false;',
));
$html .= $toolbar;
$html .= $document->getBuffer('modules', 'title', array('name'=>'title'));
$html .= '</div>
</div>';
$html .= $document->getBuffer('modules', 'submenu', array('name'=>'submenu', 'style'=>'rounded', 'id'=>'submenu-box'));
$html .= '<div id="element-box">
<div class="m">';
$contents = $document->getBuffer('component');
$contents = strtr($contents, array(
'index.php?option=com_config" ' => 'index.php?option=com_config&amp;tmpl=component&amp;mighty='.$app->input->getString('mighty').'" '
));
$html .= $contents;
$html .= '<div class="clr"></div>
</div>
</div>
';
}
else {
$html =
'<a class="btn btn-subhead" data-toggle="collapse" data-target=".subhead-collapse">'.JText::_('TPL_ISIS_TOOLBAR').'<i class="icon-wrench"></i></a>
<div class="subhead-collapse">
<div class="subhead">
<div class="container-fluid">
<div id="container-collapse" class="container-collapse"></div>
<div class="row-fluid">
<div class="span12">
'.JToolBar::getInstance('toolbar')->render('toolbar').'
</div>
</div>
</div>
</div>
</div>
<div class="container-fluid container-main">
<section id="content">
<h1 class="content-title">'.JText::_('COM_CONFIG') . ' : ' . MightysitesHelper::getHost().'</h1>
<div class="row-fluid">
<div class="span12">' .
$document->getBuffer('component') .
'</div>
</div>
</section>
</div>';
$html = strtr($html, array(
'Joomla.submitbutton(\'application.cancel\')' => 'try {window.parent.SqueezeBox.close()} catch(e){self.top.location=\''.base64_decode($app->input->getString('mighty')).'\';}; return false;', // < 3.2
'Joomla.submitbutton(\'config.cancel.application\')' => 'try {window.parent.SqueezeBox.close()} catch(e){self.top.location=\''.base64_decode($app->input->getString('mighty')).'\';}; return false;', // 3.2
JRoute::_('index.php?option=com_config') => JRoute::_('index.php?option=com_config&tmpl=component&mighty='.$app->input->getString('mighty')),
));
// Prettify
$document->addStyleDeclaration('html, body {padding:0; margin:0; height:auto;} #sidebar.span2 {display:none;} div.container-fluid.container-main {padding:0;}');
}
$document->setBuffer($html, 'component');
}
}
}
// Let's run the install queries for the component which already exists.
public function onExtensionBeforeInstall($method, $type, $manifest, $extension)
{
if ($method == 'install' && $type == 'component') {
$element = $this->getComponentName($manifest);
if (file_exists(JPATH_SITE.'/components/'.$element) || file_exists(JPATH_ADMINISTRATOR.'/components/'.$element)) {
if (isset($manifest->install->sql)) {
$installer = JInstaller::getInstance();
// Pre-create path to use for getting SQL files
$installer->setPath('extension_root', JPath::clean(JPATH_ADMINISTRATOR . '/components/' . $element));
// Don't use rewrite!
$GLOBALS['no_mightysharing'] = true;
$installer->parseSQLFiles($manifest->install->sql);
unset($GLOBALS['no_mightysharing']);
}
}
}
}
protected function getComponentName($manifest)
{
$name = strtolower(JFilterInput::getInstance()->clean((string) $manifest->name, 'cmd'));
if (substr($name, 0, 4) == 'com_') {
$element = $name;
} else {
$element = 'com_' . $name;
}
return $element;
}
}
}
// Shared options.
// Media Manager overrides.
if ($app->input->get('option') == 'com_media')
{
$mparams = JComponentHelper::getParams('com_media');
if ($config->get('mighty_file_path')) {
$mparams->set('file_path', $config->get('mighty_file_path'));
}
if ($config->get('mighty_image_path')) {
$mparams->set('image_path', $config->get('mighty_image_path'));
}
}
// Users Manager overrides.
if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'POST' && in_array($app->input->get('option'), array('com_users', 'com_comprofiler', 'com_community', 'com_easysocial', 'com_k2')))
{
$uparams = JComponentHelper::getParams('com_users');
if ($config->get('mighty_new_usertype')) {
$uparams->set('new_usertype', $config->get('mighty_new_usertype'));
}
}