Hack: Hide any module for logged in users ONLY

Your code modifications and patches you want to share with others.
chris666uk1
Joomla! Apprentice
Joomla! Apprentice
Posts: 14
Joined: Mon Feb 27, 2006 7:49 pm

Re: Hack: Hide any module for logged in users ONLY

Post by chris666uk1 » Sun May 11, 2008 9:19 pm

can someone post this for joomla 1.5.3 that would be great

Mtif
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 212
Joined: Wed Nov 02, 2005 8:54 am
Location: London and Cork
Contact:

Re: Hack: Hide any module for logged in users ONLY

Post by Mtif » Mon May 19, 2008 7:29 am

This a great contribution, and I agree it should be part of core. Its now part of my preferred core.

BUT, I have an issue with the following part. The params in the XML file throw an error and I think its to do with the > comparitor used.
<option value=">19">Author and above</option>
<option value=">20">Editor and above</option>
<option value=">21">Publisher and above</option>
<option value=">23">Manager and above</option>
<option value=">24">Administrator and above</option>
<option value="<19">Author and below</option>
<option value="<20">Editor and below</option>
<option value="<21">Publisher and below</option>
<option value="<23">Manager and below</option>
<option value="<24">Administrator and below</option>

I think its because the > character (in >19 etc..) is used in the XML to define the fields, so it is prematurely ending the field definition and failing to read the XML file correctly.

I don't really know XML so does anyone know if is there any way I can 'escape' the > character to let the XML work. Or do I have to try and use an alternate character and hack the hack ??

I am using 1.0.15 and am not sure if this is a new error for this version only.

mic
Joomla! Guru
Joomla! Guru
Posts: 692
Joined: Thu Aug 18, 2005 10:51 pm
Location: Austria
Contact:

Re: Hack: Hide any module for logged in users ONLY

Post by mic » Mon May 19, 2008 4:53 pm

Should be possible to use > for > and > for < (not tested yet, but should work)
http://www.joomx.com - custom extensions and development
http://www.joomlasupportdesk.com - support, migration, training and consulting
Member of the German Joomla Translation Team

Mtif
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 212
Joined: Wed Nov 02, 2005 8:54 am
Location: London and Cork
Contact:

Re: Hack: Hide any module for logged in users ONLY

Post by Mtif » Sat May 24, 2008 5:47 am

I really struggled with this. For whatever reason with my setup and running 1.0.15 the XML would not parse the > and < symbols inside the params. I tried > and < as was kindly suggested, no joy. I tried putting the < and > encpasulated inside CDATA - nada.

Then I looked back at the hack inside frontend.php and realised that the > and < are not used as logical comparitors by the hack, they are just used as a text string, so they could be any text. In my case "L" (less than) and "M" (more than).

So I got it working and I am delighted. This is so cool ! :D

I have made tiny changes, but made all the difference to me. I hope it helps someone else.

My variant of the 2nd frontent.php hacks looks like this (the first hack is unchanged) ...
// HACK: to hide modules for certain groups of logged in users
$myRealGid = intval($acl->get_group_id($my->usertype));
// --- end HACK ---
foreach ($modules as $module) {
// HACK: added code to check if module should be shown when a user is logged in and is in a particular group
$params = new mosParameters( $module->params );
if (!$params->get('show_registered', 1) && $my->id) {
// check the gid if param set
if ($params->get('hide_for_gid', 0) != '0') {
if (strlen($params->get('hide_for_gid')) > 2) {
// if we're looking at ranges, extract the comparator and the gid to compare
$comparator = substr($params->get('hide_for_gid'), 0, 1);
$hideGid = intval(substr($params->get('hide_for_gid'), 1));

if (($comparator == "M" && $myRealGid >= $hideGid) || ($comparator == "L" && $myRealGid <= $hideGid)) {
continue;
}

} elseif (intval($params->get('hide_for_gid')) == $myRealGid) {
// if the user's gid is equal to the parameter, hide the module
continue;
}
} else {
// simply continue the loop without outputting this module
continue;
}
}
// --- end HACK ---
$GLOBALS['_MOS_MODULES'][$module->position][] = $module;
}


My variant of the Module XML is like this ...
<param name="show_registered" type="radio" default="1" label="Show Menu When Logged In" description="Show the menu also when a registered user is logged in">
<option value="0">No</option>
<option value="1">Yes</option>
</param>
<param name="hide_for_gid" type="list" default="0" label="Hide Module for this User Type Only" description="If above is set to 'No', you can specify which registered user type you want to hide this module for. Default hides the module for any logged in user.">
<option value="0">Default</option>
<option value="18">Registered</option>
<option value="19">Author</option>
<option value="20">Editor</option>
<option value="21">Publisher</option>
<option value="23">Manager</option>
<option value="24">Administrator</option>
<option value="25">Super Administrator</option>
<option value="M19">Author and above</option>
<option value="M20">Editor and above</option>
<option value="M21">Publisher and above</option>
<option value="M23">Manager and above</option>
<option value="M24">Administrator and above</option>
<option value="L19">Author and below</option>
<option value="L20">Editor and below</option>
<option value="L21">Publisher and below</option>
<option value="L23">Manager and below</option>
<option value="L24">Administrator and below</option>
</param>

kevincam
Joomla! Apprentice
Joomla! Apprentice
Posts: 17
Joined: Thu Jan 03, 2008 8:37 am

Re: Hack: Hide any module for logged in users ONLY

Post by kevincam » Sat Jun 14, 2008 9:02 am

Ok so I decided I really needed this too so I got it to work. But I'm no expert programmer either so if somebody knows a better way please let us know but here goes what worked for me.

Added this after the last parameter of the XML file for whatever module you want to hide for example the login module which is located here (modules\mod_login\mod_login.xml):

<param name="show_registered" type="radio" default="1" label="Show Menu When Logged In" description="Show the menu also when a registered user is logged in">
<option value="0">No</option>
<option value="1">Yes</option>
</param>


Then replace the function render( $position, $params = array(), $content = null ) in \libraries\joomla\document\html\renderer\modules.php with the following


function render( $position, $params = array(), $content = null )
{
$renderer =& $this->_doc->loadRenderer('module');

$contents = '';
foreach (JModuleHelper::getModules($position) as $mod) {
// HACK: added code to check if module should be shown when a user is logged in
$pattern = '/show_registered=0/';
$match = preg_match($pattern, $mod->params);
$userId = $_SESSION["__default"]['user']->id;
if ($match && $userId) {
// simply continue the loop without outputting this module
continue;
}
// --- end HACK ---
$contents .= $renderer->render($mod, $params, $content);
}
return $contents;
}


UPDATED: I noticed that modules that use the module count need an additional hack to the file:
\libraries\joomla\application\module\helper.php

after the line $modules = array();
and before the line $db->setQuery( $query );

//HACK check in logged in then run query to exclude the correct modules
if($userId = $_SESSION["__default"]['user']->id >0){
$wheremenu = isset( $Itemid ) ? ' AND ( mm.menuid = '. (int) $Itemid .' OR mm.menuid = 0 )' : '';
$query = 'SELECT id, title, module, position, content, showtitle, control, params'
. ' FROM #__modules AS m'
. ' LEFT JOIN #__modules_menu AS mm ON mm.moduleid = m.id'
. ' WHERE m.published = 1'
. ' AND m.access <= '. (int)$aid
. ' AND m.client_id = '. (int)$mainframe->getClientId()
. " AND m.id NOT IN (SELECT id FROM #__modules WHERE params LIKE '%show_registered=0%')"
. $wheremenu
. ' ORDER BY position, ordering';
}else{
$wheremenu = isset( $Itemid ) ? ' AND ( mm.menuid = '. (int) $Itemid .' OR mm.menuid = 0 )' : '';
$query = 'SELECT id, title, module, position, content, showtitle, control, params'
. ' FROM #__modules AS m'
. ' LEFT JOIN #__modules_menu AS mm ON mm.moduleid = m.id'
. ' WHERE m.published = 1'
. ' AND m.access <= '. (int)$aid
. ' AND m.client_id = '. (int)$mainframe->getClientId()
. $wheremenu
. ' ORDER BY position, ordering';
}
//HACK
/*ORIGINAL CODE
$wheremenu = isset( $Itemid ) ? ' AND ( mm.menuid = '. (int) $Itemid .' OR mm.menuid = 0 )' : '';
$query = 'SELECT id, title, module, position, content, showtitle, control, params'
. ' FROM #__modules AS m'
. ' LEFT JOIN #__modules_menu AS mm ON mm.moduleid = m.id'
. ' WHERE m.published = 1'
. ' AND m.access <= '. (int)$aid
. ' AND m.client_id = '. (int)$mainframe->getClientId()
. $wheremenu
. ' ORDER BY position, ordering';
ORIGINAL CODE*/

User avatar
abasel
Joomla! Explorer
Joomla! Explorer
Posts: 338
Joined: Wed Nov 07, 2007 7:58 pm
Location: Auckland, New Zealand

Re: Hack: Hide any module for logged in users ONLY

Post by abasel » Wed Jul 02, 2008 10:53 am

Do you have a 1.5 hack for this?
Andre
==============================
To learn is to live!

kevincam
Joomla! Apprentice
Joomla! Apprentice
Posts: 17
Joined: Thu Jan 03, 2008 8:37 am

Re: Hack: Hide any module for logged in users ONLY

Post by kevincam » Wed Jul 02, 2008 3:19 pm

That hack I posted is for 1.5

User avatar
abasel
Joomla! Explorer
Joomla! Explorer
Posts: 338
Joined: Wed Nov 07, 2007 7:58 pm
Location: Auckland, New Zealand

Re: Hack: Hide any module for logged in users ONLY

Post by abasel » Wed Jul 02, 2008 9:19 pm

That will teach me for trying to understand something late at night :-)

:-[

Great Hack.. works great!
Andre
==============================
To learn is to live!

User avatar
LeisaWatkins
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 148
Joined: Mon Oct 08, 2007 9:13 am
Contact:

Re: Hack: Hide any module for logged in users ONLY

Post by LeisaWatkins » Fri Jul 11, 2008 9:40 am

I think I've found the solution to most of the problems I experience. I can search for hours and hours and try to resolve the problem. As soon as I post a question I often find the solution within minutes. Anyway, that was the case here. I did try to resolve it on my own for some time.

The solution was later in this thread. It was written that there was an error in the code. I thought I selected the code form the correct post. I didn't. Once I had the correct post to copy and paste from it was fixed.

So disregard the following:

I really need this function and would love it if someone could tell me what I need to fix. I did the changes as outlined and get the following error:

Fatal error: Call to a member function on a non-object in /includes/frontend.php on line 96

I believe it is related to this code, but I'm not sure if I counted the code correctly.

Code: Select all

// set up some global variables for use by frontend 

components
	global $mainframe, $database, $my , $acl;
	global $task, $Itemid, $id, $option, $gid;

	include( $mainframe->getCfg( 'absolute_path' 

)."/components/com_$name/$name.php" );
}
Can someone point out a fix? Thanks!

mic
Joomla! Guru
Joomla! Guru
Posts: 692
Joined: Thu Aug 18, 2005 10:51 pm
Location: Austria
Contact:

Re: Hack: Hide any module for logged in users ONLY

Post by mic » Fri Jul 11, 2008 10:02 am

As far i can see, the error is not inside the lines you provided.
Please post more or count right and post the correct line(s)
http://www.joomx.com - custom extensions and development
http://www.joomlasupportdesk.com - support, migration, training and consulting
Member of the German Joomla Translation Team

piersol
Joomla! Intern
Joomla! Intern
Posts: 65
Joined: Wed Sep 26, 2007 6:11 pm
Location: Indianapolis

Re: Hack: Hide any module for logged in users ONLY

Post by piersol » Fri Jul 11, 2008 3:38 pm

This is basic, but in case it helps someone, for custom modules they must edit the custom.xml file in the modules folder.
Only exact URLs allowed in signature. Please read Forum Rules: http://forum.joomla.org/viewtopic.php?f=8&t=65

User avatar
LeisaWatkins
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 148
Joined: Mon Oct 08, 2007 9:13 am
Contact:

Re: Hack: Hide any module for logged in users ONLY

Post by LeisaWatkins » Fri Jul 11, 2008 4:15 pm

piersol wrote:This is basic, but in case it helps someone, for custom modules they must edit the custom.xml file in the modules folder.
Would you mind explaining what the function is of the custom.xml file?
Is it a basic module that one would use as a starting point for creating their own modules?
If we are editing an existing module we don't need to do anything with the custom.xml module do we?

Thanks for helping me to learn more about how Joomla works.

Leisa

User avatar
LeisaWatkins
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 148
Joined: Mon Oct 08, 2007 9:13 am
Contact:

Re: Hack: Hide any module for logged in users ONLY

Post by LeisaWatkins » Fri Jul 11, 2008 4:49 pm

I believe I've modified all the files as outlined. I edited the frontend.php and am not getting an error messages. Then I edited the module and the parameters to show the module when logged in or not to show it shows in the module parameter section. However when I set it to not show when the user is logged in it still shows up.

This is the code for the module:

Code: Select all

<?xml version="1.0" encoding="iso-8859-1"?>
<mosinstall type="module">
	<name>Jumi</name>
	<author>Martin Hajek</author>
	<creationDate>March 2008</creationDate>
	<copyright>(c) 2006 - 2008 Martin Hajek. All rights reserved.</copyright>
	<license>http://www.gnu.org/copyleft/gpl.html GNU/GPL</license>
	<authorEmail>admin@vedeme.cz</authorEmail>
	<authorUrl>jumi.vedeme.cz</authorUrl>
	<version>1.2.0</version>
	<description><![CDATA[
	Jumi module for Joomla! and Mambo includes into a module position any file.<br />In an addition it can pass into the included *.php file any number of argument values.<br />There is also a compatible Jumi plugin that includes the same files into Joomla! articles.<br />Jumi manuals, demo, tips and tricks and snippets can be found at <a href="http://jumi.vedeme.cz" target="_blank">jumi.vedeme.cz</a>
	]]></description>
	<files>
		<filename module="mod_jumi">mod_jumi.php</filename>
		<filename>jumi_demo.php</filename>
	</files>
	<params>
		<param name="moduleclass_sfx" type="text" default="" label="Module Class Suffix" description="A suffix to be applied to the css class of the module (table.moduletable), this allows individual module styling" />


      <param name="show_registered" type="radio" default="1" label="Show Module When Logged In" description="Show the menu also when a registered user is logged in">
         <option value="0">No</option>
         <option value="1">Yes</option>
      </param>
		<param name="default_absolute_path" type="text" size ="97" default="" label="Default Absolute Path" description="Optional Default Absolute Path to the included file. If left blank then it is identical to Joomla! root directory. No slash at the end." />
		<param name="file_pathname" type="text" size ="97" default="modules/jumi_demo.php" label="File Pathname" description="The pathname of the file to be included. The pathname is relative with respect to Default Absolute Path." />
		<param name="php_args" type="textarea" default="[first][second] notice you can put anything out of the brackets [and third]" label="Argument Values" description="Any number of values to be passed into the included php file. Each value must be contained in [ ] brackets. Everything outside the brackets is ignored. Argument values can be referrenced by $jumi[] array in an included php file." cols="70" rows="7" />
		<param name="notepad" type="textarea" default="Put your notes here ..." label="Notepad" description="Your notes, reminders, etc." cols="70" rows="7" />    
	</params>
</mosinstall>

This is the code for the frontend.php file:

Code: Select all

<?php
/**
* @version $Id: frontend.php 5930 2006-12-06 00:49:07Z friesengeist $
* @package Joomla
* @copyright Copyright (C) 2005 Open Source Matters. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/

defined( '_VALID_MOS' ) or die( 'Restricted access' );
/**
* Displays the capture output of the main element
*/
function mosMainBody() {
	global $mosConfig_live_site;
	// message passed via the url
	$mosmsg = stripslashes( strval( mosGetParam( $_REQUEST, 'mosmsg', '' ) ) );

	$popMessages = false;

	// Browser Check
	$browserCheck = 0;
	if ( isset( $_SERVER['HTTP_USER_AGENT'] ) && isset( $_SERVER['HTTP_REFERER'] ) && strpos($_SERVER['HTTP_REFERER'], $mosConfig_live_site) !== false ) {
		$browserCheck = 1;
	}

	// Session Check
	$sessionCheck = 0;
	// Session Cookie `name`
	$sessionCookieName 	= mosMainFrame::sessionCookieName();
	// Get Session Cookie `value`
	$sessioncookie 		= mosGetParam( $_COOKIE, $sessionCookieName, null );
	if ( (strlen($sessioncookie) == 32 || $sessioncookie == '-') ) {
		$sessionCheck = 1;
	}

	// limit mosmsg to 150 characters
	if ( strlen( $mosmsg ) > 150 ) {
		$mosmsg = substr( $mosmsg, 0, 150 );
	}

	// mosmsg outputed within html
	if ($mosmsg && !$popMessages && $browserCheck && $sessionCheck) {
		echo "\n<div class=\"message\">$mosmsg</div>";
	}

	echo $GLOBALS['_MOS_OPTION']['buffer'];

	// mosmsg outputed in JS Popup
	if ($mosmsg && $popMessages && $browserCheck && $sessionCheck) {
		echo "\n<script language=\"javascript\">alert('" . addslashes( $mosmsg ) . "');</script>";
	}
}
/**
* Utility functions and classes
*/
function mosLoadComponent( $name ) {
	// set up some global variables for use by frontend components
	global $mainframe, $database, $my;
	global $task, $Itemid, $id, $option, $gid;

	include( $mainframe->getCfg( 'absolute_path' )."/components/com_$name/$name.php" );
}
/**
* Cache some modules information
* @return array
*/
function &initModules() {
	global $database, $my, $Itemid;

	if (!isset( $GLOBALS['_MOS_MODULES'] )) {
		$Itemid 		= intval($Itemid);
		$check_Itemid 	= '';
		if ($Itemid) {
			$check_Itemid = "OR mm.menuid = " . (int) $Itemid;
		}

		$query = "SELECT id, title, module, position, content, showtitle, params"
		. "\n FROM #__modules AS m"
		. "\n INNER JOIN #__modules_menu AS mm ON mm.moduleid = m.id"
		. "\n WHERE m.published = 1"
		. "\n AND m.access <= ". (int) $my->gid
		. "\n AND m.client_id != 1"
		. "\n AND ( mm.menuid = 0 $check_Itemid )"
		. "\n ORDER BY ordering";

		$database->setQuery( $query );
		$modules = $database->loadObjectList();

		 foreach ($modules as $module) {
                       // HACK: added code to check if module should be shown when a user is logged in
$params = new mosParameters( $module->params );
if (!$params->get('show_registered', 1) && $my->id) {
// simply continue the loop without outputting this module
continue;
}
// --- end HACK ---

      foreach ($modules as $module) {
         // HACK: added code to check if module should be shown when a user is logged in and is in a particular group
         $params = new mosParameters( $module->params );
         if (!$params->get('show_registered', 1) && $my->id) {
            // check the gid if param set
            if ($params->get('hide_for_gid', 0) != '0') {
               if (strlen($params->get('hide_for_gid')) > 2) {
                  // if we're looking at ranges, extract the comparator and the gid to compare
                  $comparator = substr($params->get('hide_for_gid'), 0, 1);
                  $hideGid = intval(substr($params->get('hide_for_gid'), 1));

                  if (($comparator == ">" && $myRealGid >= $hideGid) || ($comparator == "<" && $myRealGid <= $hideGid)) {
                     continue;
                  }

               } elseif (intval($params->get('hide_for_gid')) == $myRealGid) {
                  // if the user's gid is equal to the parameter, hide the module
                  continue;
               }
            } else {
               // simply continue the loop without outputting this module
               continue;
            }
         }
         // --- end HACK ---
         $GLOBALS['_MOS_MODULES'][$module->position][] = $module;
      }


                        $GLOBALS['_MOS_MODULES'][$module->position][] = $module;
                }      
	}
	return $GLOBALS['_MOS_MODULES'];
}
/**
* @param string THe template position
*/
function mosCountModules( $position='left' ) {
	global $database, $my, $Itemid, $acl;

	$tp = intval( mosGetParam( $_GET, 'tp', 0 ) );
	if ($tp) {
		return 1;
	}

	$modules =& initModules();
	if (isset( $GLOBALS['_MOS_MODULES'][$position] )) {
		return count( $GLOBALS['_MOS_MODULES'][$position] );
	} else {
		return 0;
	}
}
/**
* @param string The position
* @param int The style.  0=normal, 1=horiz, -1=no wrapper
*/
function mosLoadModules( $position='left', $style=0 ) {
	global $mosConfig_gzip, $mosConfig_absolute_path, $database, $my, $Itemid, $mosConfig_caching;

	$tp = intval( mosGetParam( $_GET, 'tp', 0 ) );
	if ($tp) {
		echo '<div style="height:50px;background-color:#eee;margin:2px;padding:10px;border:1px solid #f00;color:#700;">';
		echo $position;
		echo '</div>';
		return;
	}
	$style = intval( $style );
	$cache =& mosCache::getCache( 'com_content' );

	require_once( $mosConfig_absolute_path . '/includes/frontend.html.php' );

	$allModules =& initModules();
	if (isset( $GLOBALS['_MOS_MODULES'][$position] )) {
		$modules = $GLOBALS['_MOS_MODULES'][$position];
	} else {
		$modules = array();
	}

	if (count( $modules ) < 1) {
		$style = 0;
	}
	if ($style == 1) {
		echo "<table cellspacing=\"1\" cellpadding=\"0\" border=\"0\" width=\"100%\">\n";
		echo "<tr>\n";
	}
	$prepend = ($style == 1) ? "<td valign=\"top\">\n" : '';
	$postpend = ($style == 1) ? "</td>\n" : '';

	$count = 1;
	foreach ($modules as $module) {
		$params = new mosParameters( $module->params );

		echo $prepend;

		if ((substr("$module->module",0,4))=='mod_') {
		// normal modules
			if ($params->get('cache') == 1 && $mosConfig_caching == 1) {
			// module caching
				$cache->call('modules_html::module2', $module, $params, $Itemid, $style, $my->gid  );
			} else {
				modules_html::module2( $module, $params, $Itemid, $style, $count );
			}
		} else {
		// custom or new modules
			if ($params->get('cache') == 1 && $mosConfig_caching == 1) {
			// module caching
				$cache->call('modules_html::module', $module, $params, $Itemid, $style, 0, $my->gid );
			} else {
				modules_html::module( $module, $params, $Itemid, $style );
			}
		}

		echo $postpend;

		$count++;
	}
	if ($style == 1) {
		echo "</tr>\n</table>\n";
	}
}
/**
* Assembles head tags
*/
function mosShowHead() {
	global $database, $option, $my, $mainframe, $_VERSION, $task, $id;
	global $mosConfig_MetaDesc, $mosConfig_MetaKeys, $mosConfig_live_site, $mosConfig_sef, $mosConfig_absolute_path, $mosConfig_sitename, $mosConfig_favicon;

	$mainframe->appendMetaTag( 'description', $mosConfig_MetaDesc );
	$mainframe->appendMetaTag( 'keywords', $mosConfig_MetaKeys );
	$mainframe->addMetaTag( 'Generator', $_VERSION->PRODUCT . ' - ' . $_VERSION->COPYRIGHT);
	$mainframe->addMetaTag( 'robots', 'index, follow' );

	// cache activation
	if (isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING'])) {
		$cache =& mosCache::getCache('com_content');
		echo $cache->call('mainframe->getHead', @$_SERVER['QUERY_STRING'], $id);
	} else {
		echo $mainframe->getHead();
	}

	if ( isset($mosConfig_sef) && $mosConfig_sef ) {
		echo "<base href=\"$mosConfig_live_site/\" />\r\n";
	}

	if ($my->id || $mainframe->get( 'joomlaJavascript' )) {
		?>
		<script src="<?php echo $mosConfig_live_site;?>/includes/js/joomla.javascript.js" type="text/javascript"></script>
		<?php
	}

	$row = new mosComponent( $database );
	$query = "SELECT a.*"
	. "\n FROM #__components AS a"
	. "\n WHERE ( a.admin_menu_link = 'option=com_syndicate' OR a.admin_menu_link = 'option=com_syndicate&hidemainmenu=1' )"
	. "\n AND a.option = 'com_syndicate'"
	;
	$database->setQuery( $query );
	$database->loadObject( $row );

	// get params definitions
	$syndicateParams = new mosParameters( $row->params, $mainframe->getPath( 'com_xml', $row->option ), 'component' );

	// needed to reduce query
	$GLOBALS['syndicateParams'] = $syndicateParams;

	$live_bookmark = $syndicateParams->get( 'live_bookmark', 0 );

	// and to allow disabling/enabling of selected feed types
	switch ( $live_bookmark ) {
		case 'RSS0.91':
			if ( !$syndicateParams->get( 'rss091', 1 ) ) {
				$live_bookmark = 0;
			}
			break;

		case 'RSS1.0':
			if ( !$syndicateParams->get( 'rss10', 1 ) ) {
				$live_bookmark = 0;
			}
			break;

		case 'RSS2.0':
			if ( !$syndicateParams->get( 'rss20', 1 ) ) {
				$live_bookmark = 0;
			}
			break;

		case 'ATOM0.3':
			if ( !$syndicateParams->get( 'atom03', 1 ) ) {
				$live_bookmark = 0;
			}
			break;
	}

	// support for Live Bookmarks ability for site syndication
	if ($live_bookmark) {
		$show = 1;

		$link_file 	= $mosConfig_live_site . '/index2.php?option=com_rss&feed='. $live_bookmark .'&no_html=1';

		// xhtml check
		$link_file = ampReplace( $link_file );

		// security chcek
		$check = $syndicateParams->def( 'check', 1 );
		if($check) {
			// test if rssfeed module is published
			// if not disable access
			$query = "SELECT m.id"
			. "\n FROM #__modules AS m"
			. "\n WHERE m.module = 'mod_rssfeed'"
			. "\n AND m.published = 1"
			;
			$database->setQuery( $query );
			$check = $database->loadResultArray();
			if(empty($check)) {
				$show = 0;
			}
		}
		// outputs link tag for page
		if ($show) {
			// test if security check is enbled
			?>
			<link rel="alternate" type="application/rss+xml" title="<?php echo $mosConfig_sitename; ?>" href="<?php echo $link_file; ?>" />
			<?php
		}
	}

	// favourites icon
	if ( !$mosConfig_favicon ) {
		$mosConfig_favicon = 'favicon.ico';
	}
	$icon = $mosConfig_absolute_path .'/images/'. $mosConfig_favicon;
	// checks to see if file exists
	if ( !file_exists( $icon ) ) {
		$icon = $mosConfig_live_site .'/images/favicon.ico';
	} else {
		$icon = $mosConfig_live_site .'/images/' .$mosConfig_favicon;
	}

	// outputs link tag for page
	?>
	<link rel="shortcut icon" href="<?php echo $icon;?>" />
	<?php
}
?>
Any idea on what I missed?

Your help is greatly appreciated!

~ Leisa

piersol
Joomla! Intern
Joomla! Intern
Posts: 65
Joined: Wed Sep 26, 2007 6:11 pm
Location: Indianapolis

Re: Hack: Hide any module for logged in users ONLY

Post by piersol » Fri Jul 11, 2008 7:13 pm

Leisa, I am quite the novice overall. But i followed the directions here and my site works as they said it would in regards to showing modules. I messed up the first time and had to grab a fresh frontend.php file from another install and then re-embedded the hack as they said to.

It works fine now.

My need was for modules i created, not ones i installed. So that is what led me to the custom.xml file.

As for the custom.xml, that is merely the standard xml file for any NEW modules you would create using the administrator backend. I use the NEW MODULE option to include modules with custom HTML within them, for example a SIGN ON image for a better look. If you edit and embed the params hack given in this topic, you now can HIDE OR NOT all NEW modules you create in the admin interface.

Hope that helps.
Only exact URLs allowed in signature. Please read Forum Rules: http://forum.joomla.org/viewtopic.php?f=8&t=65

User avatar
LeisaWatkins
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 148
Joined: Mon Oct 08, 2007 9:13 am
Contact:

Re: Hack: Hide any module for logged in users ONLY

Post by LeisaWatkins » Fri Jul 11, 2008 7:22 pm

Thanks for the reply.

I messed up a few times and figured that since I wasn't getting an errors the frontpage.php file was probably correct. The first three times I got errors. I'll try it again. My parameters show up ok in the modules so I think that is ok. So it must be related to the frontpage.php file I would guess. I've looked over it and don't see an error. Maybe the sixth time is a charm. We shall see.

I was wondering if it could have to do with cache not revealing the new page. But I've done a hard refresh.

~ Leisa

User avatar
LeisaWatkins
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 148
Joined: Mon Oct 08, 2007 9:13 am
Contact:

Re: Hack: Hide any module for logged in users ONLY

Post by LeisaWatkins » Sat Jul 12, 2008 6:14 pm

I don't know what I did wrong because in spite of not getting any error messages I still can't get this to work. Here is a work-around I did in case it may be of use to someone else.

I needed to hide modules for logged in users on the front page. So I included the module it in the intro of some content and selected to show on the front page. I included it by loading it to an unused position, and then added this to my content:

Code: Select all

{mosloadposition user10}
.)

Then I wrapped it all in a

Code: Select all

{noreg} Content {/noreg}
so that it wouldn't display once someone can sign-on.

So now I have a different frontpage for logged in users and for non-logged in users.

User avatar
abasel
Joomla! Explorer
Joomla! Explorer
Posts: 338
Joined: Wed Nov 07, 2007 7:58 pm
Location: Auckland, New Zealand

Re: Hack: Hide any module for logged in users ONLY

Post by abasel » Wed Aug 13, 2008 7:39 pm

See quote bellow:

I just recieved an e-mailing detailing the need to upgrade to 1.5.6 for security reasons. While trying to make changes to the patch (inorder to use this GREAT hack) I noticed that the render function looks different.... can this hack be applied to the 1.5.6 upgrade? :eek:
kevincam wrote:Ok so I decided I really needed this too so I got it to work. But I'm no expert programmer either so if somebody knows a better way please let us know but here goes what worked for me.

Added this after the last parameter of the XML file for whatever module you want to hide for example the login module which is located here (modules\mod_login\mod_login.xml):

<param name="show_registered" type="radio" default="1" label="Show Menu When Logged In" description="Show the menu also when a registered user is logged in">
<option value="0">No</option>
<option value="1">Yes</option>
</param>


Then replace the function render( $position, $params = array(), $content = null ) in \libraries\joomla\document\html\renderer\modules.php with the following


function render( $position, $params = array(), $content = null )
{
$renderer =& $this->_doc->loadRenderer('module');

$contents = '';
foreach (JModuleHelper::getModules($position) as $mod) {
// HACK: added code to check if module should be shown when a user is logged in
$pattern = '/show_registered=0/';
$match = preg_match($pattern, $mod->params);
$userId = $_SESSION["__default"]['user']->id;
if ($match && $userId) {
// simply continue the loop without outputting this module
continue;
}
// --- end HACK ---
$contents .= $renderer->render($mod, $params, $content);
}
return $contents;
}


UPDATED: I noticed that modules that use the module count need an additional hack to the file:
\libraries\joomla\application\module\helper.php

after the line $modules = array();
and before the line $db->setQuery( $query );

//HACK check in logged in then run query to exclude the correct modules
if($userId = $_SESSION["__default"]['user']->id >0){
$wheremenu = isset( $Itemid ) ? ' AND ( mm.menuid = '. (int) $Itemid .' OR mm.menuid = 0 )' : '';
$query = 'SELECT id, title, module, position, content, showtitle, control, params'
. ' FROM #__modules AS m'
. ' LEFT JOIN #__modules_menu AS mm ON mm.moduleid = m.id'
. ' WHERE m.published = 1'
. ' AND m.access <= '. (int)$aid
. ' AND m.client_id = '. (int)$mainframe->getClientId()
. " AND m.id NOT IN (SELECT id FROM #__modules WHERE params LIKE '%show_registered=0%')"
. $wheremenu
. ' ORDER BY position, ordering';
}else{
$wheremenu = isset( $Itemid ) ? ' AND ( mm.menuid = '. (int) $Itemid .' OR mm.menuid = 0 )' : '';
$query = 'SELECT id, title, module, position, content, showtitle, control, params'
. ' FROM #__modules AS m'
. ' LEFT JOIN #__modules_menu AS mm ON mm.moduleid = m.id'
. ' WHERE m.published = 1'
. ' AND m.access <= '. (int)$aid
. ' AND m.client_id = '. (int)$mainframe->getClientId()
. $wheremenu
. ' ORDER BY position, ordering';
}
//HACK
/*ORIGINAL CODE
$wheremenu = isset( $Itemid ) ? ' AND ( mm.menuid = '. (int) $Itemid .' OR mm.menuid = 0 )' : '';
$query = 'SELECT id, title, module, position, content, showtitle, control, params'
. ' FROM #__modules AS m'
. ' LEFT JOIN #__modules_menu AS mm ON mm.moduleid = m.id'
. ' WHERE m.published = 1'
. ' AND m.access <= '. (int)$aid
. ' AND m.client_id = '. (int)$mainframe->getClientId()
. $wheremenu
. ' ORDER BY position, ordering';
ORIGINAL CODE*/
Andre
==============================
To learn is to live!

kevincam
Joomla! Apprentice
Joomla! Apprentice
Posts: 17
Joined: Thu Jan 03, 2008 8:37 am

Re: Hack: Hide any module for logged in users ONLY

Post by kevincam » Wed Aug 13, 2008 8:30 pm

Good question. I'd say if you got test platform give it try and let us know. I can't remember which site I used this on but I have patached all my sites.

kwoolf
Joomla! Apprentice
Joomla! Apprentice
Posts: 43
Joined: Wed Oct 24, 2007 8:49 pm

Re: Hack: Hide any module for logged in users ONLY

Post by kwoolf » Fri Sep 12, 2008 5:30 pm

kevincam wrote:Good question. I'd say if you got test platform give it try and let us know. I can't remember which site I used this on but I have patached all my sites.
Thanks Kevin! Works great on 1.5.7.

webone
Joomla! Apprentice
Joomla! Apprentice
Posts: 8
Joined: Tue Sep 23, 2008 5:16 am

Re: Hack: Hide any module for logged in users ONLY

Post by webone » Sat Oct 04, 2008 9:36 am

Thanks kevincam... it works like a charm on 1.5.7

Elephantman
Joomla! Apprentice
Joomla! Apprentice
Posts: 20
Joined: Thu Nov 03, 2005 4:32 pm

Re: Hack: Hide any module for logged in users ONLY

Post by Elephantman » Thu Oct 09, 2008 10:01 am

I had the same problem but didn't like the hack solution, so I made a quick module that allows you to select the module position to show to public and the one to show to members. It's joined with this message.
You do not have the required permissions to view the files attached to this post.

radicalz
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Fri Nov 16, 2007 8:13 am

Re: Hack: Hide any module for logged in users ONLY

Post by radicalz » Sun Oct 12, 2008 4:39 am

hi guys.. thanks Kevin for the great workaround for Joomla 1.5 :D

But I love how phlux0r did with the user category. And I really need each user category to be able to see or not some modules. Is there any1 have the workaround?

With Kevin's workaround, we can only hide modules from registered user, regardless of the status (author, editor, etc). So.. any1 can add this little feature into it?

FYI, I'm not a programmer at all ;)

radicalz
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Fri Nov 16, 2007 8:13 am

Re: Hack: Hide any module for logged in users ONLY

Post by radicalz » Tue Oct 14, 2008 11:49 am

any1?

kwoolf
Joomla! Apprentice
Joomla! Apprentice
Posts: 43
Joined: Wed Oct 24, 2007 8:49 pm

Re: Hack: Hide any module for logged in users ONLY

Post by kwoolf » Tue Oct 14, 2008 12:03 pm

radicalz wrote:any1?
I'm just going to throw my 2 cents in on this one since I'm not a programmer either, but I've read that Joomla 1.6 will have better ACL, so it might be worth waiting for this instead of hacking the core now. If it's really necessary, try joomsuite.com JPermissions. This will surely help you with your problem this case if you are running Joomla 1.5.x. Sorry I couldn't be more helpful.

bf2005
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Tue May 16, 2006 9:58 pm

Re: Hack: Hide any module for logged in users ONLY

Post by bf2005 » Mon Oct 20, 2008 7:44 pm

this hack works great on joomla 1.57 thanks, i am new to all the coding side of joomla and i was wondering if there is anyway this code can be converted to use jaclplus or jaclplus pro.
thanks for any help you can give :)

kwoolf
Joomla! Apprentice
Joomla! Apprentice
Posts: 43
Joined: Wed Oct 24, 2007 8:49 pm

Re: Hack: Hide any module for logged in users ONLY

Post by kwoolf » Fri Oct 24, 2008 3:12 pm

This hack was working perfectly for me before populating my site, but now that I have a YooTheme template, I keep getting the following error:


Fatal error: Call to undefined method JDocumentError::countModules() in /public_html/templates/yoo_shuffle/lib/php/yoolayout.php on line 101

Code: Select all

// set css-class for layoutstyle
if ($this->countModules('left')) {
	if ($this->params->get('layout') == 'left') {
		$this->params->set('leftcolumn', 'left');
	} else {
		$this->params->set('leftcolumn', 'right');
	}
}
I notified YooTheme but no response. Hopefully I can find someone to save me here! Thanks!

ewel
Joomla! Guru
Joomla! Guru
Posts: 522
Joined: Mon Oct 01, 2007 11:35 am

Re: Hack: Hide any module for logged in users ONLY

Post by ewel » Wed Nov 12, 2008 3:29 pm

I happened across this thread thanks to this article: http://www.welcometojoomla.com/how-do-i ... odule.html.

For all those who want to hide a module from registered users who are logged in, there is a simple solution: just download Mod OnAnyPage from the JED (http://extensions.joomla.org/component/ ... Itemid,35/).

This is a module that can show or hide other modules dependent on various checks, and one of the checks is for registered users. All you would need to do is to install Mod OnAnypage, to choose some settings and enable it, and to pubish the target module to a different position. Much quicker and simpler than hacking!

By publishing a (copy of a) menu module (with selected items) through Mod OnAnyPage you can also hide menus for registered users. It does much more than that, but what it does not do (yet, but these are on the wishlist) is checking for various levels of users and for https. However, Mod OnAnyPage does work with any version of J!1.5. There is a slightly more limited J!1.0.x version which is no longer maintained but on strong demand could be updated.

Now that it is no longer necessary, I would avoid hacking this functionality into Joomla unless you want to repeat the exercise everytime a new version comes out, such as the security release this week.

Mod OnAnyPage is new (actually I made it a year ago but never got to distribute it) but unless I am mistaken Meta Mod was already capable of hiding modules from logged in users - be it with more effort (see the JED: http://extensions.joomla.org/component/ ... Itemid,35/).

If you use it and you have any feedback please let me know (but preferably not in this thread). I am not a coder and merely a hobbyist, but I am trying to do what I can to improve or work around the Joomla menu assignment system (see also this thread: http://forum.joomla.org/viewtopic.php?f=502&t=266002).

kwoolf
Joomla! Apprentice
Joomla! Apprentice
Posts: 43
Joined: Wed Oct 24, 2007 8:49 pm

Re: Hack: Hide any module for logged in users ONLY

Post by kwoolf » Wed Nov 12, 2008 3:46 pm

ewel wrote:Mod OnAnyPage is new (actually I made it a year ago but never got to distribute it) but unless I am mistaken Meta Mod was already capable of hiding modules from logged in users - be it with more effort (see the JED: http://extensions.joomla.org/component/ ... Itemid,35/).
Meta Mod does a great job of hiding modules for various situations. I originally posted here to commend the hack, but after 2 updates it's just a pain to keep up. Meta Mod works well, but I would say it's an advanced level module that requires at least a basic understanding of php. It's worth the time to figure it out, though!

Kevin

ewel
Joomla! Guru
Joomla! Guru
Posts: 522
Joined: Mon Oct 01, 2007 11:35 am

Re: Hack: Hide any module for logged in users ONLY

Post by ewel » Wed Nov 12, 2008 3:58 pm

kwoolf wrote:Meta Mod does a great job of hiding modules for various situations. I originally posted here to commend the hack, but after 2 updates it's just a pain to keep up. Meta Mod works well, but I would say it's an advanced level module that requires at least a basic understanding of php. It's worth the time to figure it out, though!

Kevin
I thought so.. and I agree Meta Mod is for advanced users (it revolves around geo-location so it is probably most popular with developers of ecommerce sites). One of the reasons why I decided to finally publish Mod OnAnyPage is because it requires zero understanding of php so anyone can use it.

da8iwr
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 190
Joined: Sat Jan 14, 2006 1:59 pm
Location: Sunderland UK
Contact:

Re: Hack: Hide any module for logged in users ONLY

Post by da8iwr » Fri May 22, 2009 1:37 am

I used this a while ago and it worked a treat

for example i needed if you were logged out, to see a login module, or if you were logged in to see content.

Code: Select all

			    <?php
				if ($my->id ) {
					echo "<div id=\"rightmain0\"><jdoc:include type=\"component\" /></div>
		</div>";
				} else {
					echo "<div id=\"rightmainlogged\">
					<div id=\"divContentlogged\">
					<jdoc:include type=\"modules\" name=\"user5\" headerLevel=\"3\" />
				</div> ";
				}
				?>
And it worked perfect

Just add that in where you need to add in the module position etc, of course modify the code to suit, such as you could use

Code: Select all

<?php
				if ($my->id ) {
					echo "<jdoc:include type=\"modules\" name=\"user2\" headerLevel=\"3\" />";
				} else {
					echo "<jdoc:include type=\"modules\" name=\"user3\" headerLevel=\"3\" />";
				}
?>
To just switch between modules

You can see it by going to the test site i made it on
http://host2.newworldhosting.co.uk/~wwwcarr/
Then clicking on "available works" and logging in or out with
Username - test
Password - test

That is working on Joomla 1.5.10 at the moment.


Locked

Return to “Core Hacks and Patches”