Example #1
0
	function moduleIdsAndChanges( &$params, $my_module = null )
	{
		$Itemid				= JRequest::getVar('Itemid');
		$position			= $my_module->position;
		$my_id				= $my_module->id;
		$id					= JRequest::getVar('id');
		$view				= JRequest::getWord('view');
		$option				= JRequest::getVar('option');
		
		// Retrieve parameters
		$php 				= trim(   $params->get( 'php', '' ) );
		$logged_in_users	= intval( $params->get( 'logged_in_users', '0' ) );
		$start_datetime 	= trim(   $params->get( 'start_datetime', '' ) );
		$end_datetime 		= trim(   $params->get( 'end_datetime', '' ) );
		$timezone 			= trim(   $params->get( 'timezone', 'default' ) );
		$use_geoip 			= intval( $params->get( 'use_geoip', '0' ) );
		$module_ids 		= trim(   $params->get( 'module_ids', '0' ) );
		$debug 				= trim(   $params->get( 'debug', '0' ) );

		if ($debug == 2) { /* advanced debug */
			modMetaModHelper::advanced_debug();
		}
		
		if ($timezone == "default") {
			$config			=& JFactory::getConfig();
			$timezone		= $config->getValue('offset');
		}

		$now				= time();

		if ($debug) {
			echo '<b>' . JText::_('MM_DEBUG_INFO') .'</b><br />';
			if ( $my_id ) echo '<i>Module ID:</i> '.htmlentities($my_id).'<br />';
			echo '<i>$option:</i> '.htmlentities($option).'<br />';
			echo '<i>$view:</i> '.htmlentities($view).'<br />';
			echo '<i>$id:</i> '.htmlentities($id).'<br />';
			echo '<i>$Itemid:</i> '.htmlentities($Itemid).'<br />';
			echo '<i>$timezone:</i> '.htmlentities($timezone).'<br />';
		}
		
		// LANGUAGE HANDLING
		list($language,$language_code,$language_region) = modMetaModHelper::languages($params);

		// START AND END TIMES
		// quit if we're not to start yet

		if ($start_datetime != '') {
		 	if ( modMetaModHelper::strtotime_in_tz( $start_datetime, $timezone ) > $now) {
				if ($debug) {
					echo JText::_('MM_DEBUG_START_NOT_REACHED') . '<br />';
				}
				return;
			}
			if ($debug) {
				echo JText::_('MM_DEBUG_START_REACHED') . '<br />';
			}
		}

		// quit if we are too late to display this item
		if ($end_datetime != '') {
		 	if ( modMetaModHelper::strtotime_in_tz( $end_datetime, $timezone ) <= $now) {
				if ($debug) {
					echo JText::_('MM_DEBUG_END_REACHED') . '<br />';
				}
				return;
			}
			if ($debug) {
				echo JText::_('MM_DEBUG_END_NOT_REACHED') . '<br />';
			}
		}
		
		// INCLUDE AND EXCLUDE COUNTRIES, & GEOIP FUNCTIONALITY
		$include_countries	= strtoupper(trim( $params->get( 'include_countries', '' ) ));
		$exclude_countries	= strtoupper(trim( $params->get( 'exclude_countries', '' ) ));

		$ip = modMetaModHelper::getUserIP( $params );

		if ($use_geoip) {
			
			// if we have already calculated any info for the current IP address, retrieve it.
			list( $fromCountryId, $fromCountryName, $geoip) = modMetaModHelper::geoip_cache_ip_countryid_name_city( $ip );		

			$geoIPFiles = array('','geoip.inc.php','geoipcity.inc.php','geoipcity.inc.php');
			
			// case 1: looking for a country, and we don't already have one
			if ($fromCountryId == '' and $fromCountryName == '' and $use_geoip == 1 ) {

				// find the location of the geoip data file, depending on which we are to use
				$include_file = modMetaModHelper::locateGeoIPInclude($use_geoip);
				if ($include_file == null) {
					$use_geoip = 0;
					if ($debug) {
						echo '<b>' . JText::_('MM_DEBUG_GEOIP_CANNOT_LOCATE') . '</b><br />';
					}
				} else {
				
					// grab the appropriate code for whichever geoip stuff we have 
					if (!function_exists("geoip_open")) {
						include_once(JPATH_SITE . DS . "modules" . DS . "mod_metamod" . DS . "geoip" . DS . $geoIPFiles[$use_geoip]);
					}
				
					// open the data file
					$gi = geoip_open( $include_file, GEOIP_STANDARD );

					$fromCountryId		= utf8_encode(geoip_country_code_by_addr($gi, $ip));
					$fromCountryName	= utf8_encode(geoip_country_name_by_addr($gi, $ip));
					
					modMetaModHelper::geoip_cache_ip_countryid_name_city( $ip, $fromCountryId, $fromCountryName );		
					
					geoip_close( $gi );

				} // we found the geoip data file
			} // we needed to cache the geoip for the current request
			

			// case 2: looking for a city record, and we don't already have one
			if ($geoip == null and $use_geoip > 1 ) {

				// find the location of the geoip data file, depending on which we are to use
				$include_file = modMetaModHelper::locateGeoIPInclude($use_geoip);
				if ($include_file == null) {
					$use_geoip = 0;
					if ($debug) {
						echo '<b>' . JText::_('MM_DEBUG_GEOIP_CANNOT_LOCATE') . '</b><br />';
					}
				} else {
				
					// grab the appropriate code for whichever geoip stuff we have 
					if (!function_exists("geoiprecord")) {
						include_once(JPATH_SITE . DS . "modules" . DS . "mod_metamod" . DS . "geoip" . DS . $geoIPFiles[$use_geoip]);
					}
				
					// open the data file
					$gi = geoip_open( $include_file, GEOIP_STANDARD );

					$geoip				= geoip_record_by_addr($gi, $ip);
					if ( $geoip != null ) {
						modMetaModHelper::convertToUtf8($geoip);
						$fromCountryId		= $geoip->country_code;
						$fromCountryName	= $geoip->country_name;
						modMetaModHelper::geoip_cache_ip_countryid_name_city( $ip, $fromCountryId, $fromCountryName, $geoip );
					}
					if ($geoip == null && $debug) {
						echo JText::sprintf('MM_DEBUG_GEOIP_NO_CITY_FOUND', htmlentities($ip) ) . '<br />';
					}
					geoip_close( $gi );

				} // we found the geoip data file
			} // we needed to cache the geoip for the current request
			
			// set some defaults if necessary (probably can't happen)
			if ($fromCountryId == '')	$fromCountryId = "GB";
			if ($fromCountryName == '')	$fromCountryName = "United Kingdom";
			
			if (($fromCountryId || $fromCountryName) && $debug) {
				echo "<i>" . JText::_("MM_DEBUG_GEOIP_COUNTRY") ."</i> " .$fromCountryId . "<br />";
				echo "<i>" . JText::_("MM_DEBUG_GEOIP_COUNTRY_NAME") ."</i> " . $fromCountryName . "<br />";
			}

			if($geoip && $debug)
			{
				echo "<i>" . JText::_("Country Code 3:") ."</i> " . $geoip->country_code3 . "<br />";
				echo "<i>" . JText::_("Region:") ."</i> " .$geoip->region . "<br />";
				echo "<i>" . JText::_("City:") ."</i> " .$geoip->city . "<br />";
				echo "<i>" . JText::_("Postal Code:") ."</i> " .$geoip->postal_code . "<br />";
				echo "<i>" . JText::_("Latitude:") ."</i> " .$geoip->latitude . "<br />";
				echo "<i>" . JText::_("Longitude:") ."</i> " .$geoip->longitude . "<br />";
				echo "<i>" . JText::_("Area Code:") ."</i> " .$geoip->area_code . "<br />";
				echo "<i>" . JText::_("Metro Code:") ."</i> " .$geoip->metro_code . "<br />";
				echo "<i>" . JText::_("Continent Code:") ."</i> " .$geoip->continent_code . "<br />";
			}						
			
			if ( strlen( $include_countries ) ) {
				// reject if fromCountryId is not in the list
				if ( strpos( $include_countries, $fromCountryId ) === false) {
					if ( $debug ) {
						echo JText::sprintf('MM_DEBUG_GEOIP_REJECTING_INCLUDE', $fromCountryId ) . '<br />';
					}
					return;
				}
				if ( $debug ) {
					echo JText::sprintf('MM_DEBUG_GEOIP_ACCEPTING_INCLUDE', $fromCountryId ) . '<br />';
				}
			}
			if ( strlen( $exclude_countries ) ) {
				// reject if fromCountryId is in the list
				if (! ( strpos( $exclude_countries, $fromCountryId ) === false ) ) {
					if ( $debug ) {
						echo JText::sprintf('MM_DEBUG_GEOIP_REJECTING_EXCLUDE', $fromCountryId ) . '<br />';
					}
					return;
				}
				if ( $debug ) {
					echo JText::sprintf('MM_DEBUG_GEOIP_ACCEPTING_EXCLUDE', $fromCountryId ) . '<br />';
				}
			}
		}

		// for access by eval'ed script
		$db 	=& JFactory::getDBO();
		$user 	=& JFactory::getUser();

		// set some useful for access by eval'ed script
		$user_types = array("Public", "Registered", "Author",
				"Editor", "Publisher", "Manager", "Administrator", "Super Users", "Customer Group", "Shop Suppliers");
		foreach ($user_types as $u) {
			$constant = "MM_USER_" . strtoupper(str_replace(" ","_",$u));
			$constant_not = "MM_USER_NOT_" . strtoupper(str_replace(" ","_",$u));
			
			if (!defined($constant))		define($constant, array_key_exists( $u, $user->groups));
			if (!defined($constant_not))	define($constant_not, !array_key_exists( $u, $user->groups));;
		}
		
		if (!defined("MM_NOT_LOGGED_IN"))	define("MM_NOT_LOGGED_IN", ($user->guest == 1));
		if (!defined("MM_LOGGED_IN"))		define("MM_LOGGED_IN", ! MM_NOT_LOGGED_IN);
		
		
		modMetaModHelper::setDateConstants( $now, $timezone );
		
		// now check the logged-in-user status.
		// If MM is set up for "only logged in users", and the user is not logged in, quit immediately.
		if ($logged_in_users == 1 ) {
			if ( MM_NOT_LOGGED_IN ) {
				if ($debug) {
					echo JText::_('MM_LOGGED_IN_ONLY_FAIL') . '<br />';
				}
				return;
			}
			if ($debug) {
				echo JText::_('MM_LOGGED_IN_ONLY_SUCCEED') . '<br />';
			}
		}
		// If MM is set up for "only non-logged-in users", and the user is logged in, quit immediately.
		if ($logged_in_users == 2 ) {
			if ( MM_LOGGED_IN ) {
				if ($debug) {
					echo JText::_('MM_NON_LOGGED_IN_ONLY_FAIL') . '<br />';
				}
				return;
			}
			if ($debug) {
				echo JText::_('MM_NON_LOGGED_IN_ONLY_SUCCEED') . '<br />';
			}
		}
		
		// set up the changecache
		// this can now be used inside the rules like this:
		// $changes->mod(N)->accessLevel("1")->showTitle()->title("New Title")
		$changes = new MMChangeCache;
		
		// finally, set up some JomGeniuses for use in the script.
		include_once dirname(__FILE__) . DS . 'JomGenius.class.php';
		$content_genius = JomGenius( 'content' );
		$menu_genius = JomGenius( 'menu' );
		$core_genius = JomGenius( 'core' );
		$core_genius->setTimezone( $timezone );
		

		// get results from php script. Add leading and trailing spaces so it's possible to end with < ? p h p
		$mods = strlen($php) ? eval( " " . $php . " " ) : '';

		
		// convert comma-separated list of module identifiers (specified statically) to an array of identifiers.
		// identifiers can be module ids, or module positions.
		$static_ids = strlen( $module_ids ) ? array_map( "trim", explode( ",", $module_ids ) ) : array();

		// convert comma-separated list of module ids (returned from PHP code) to an array of identifiers
		if ( !is_array( $mods ) ) {
			if ( is_numeric( $mods ) ) $mods = array( $mods );
			if ( is_string( $mods) )  $mods = array_map( "trim", explode( ",", $mods ) );
			if ( !is_array( $mods ) )  $mods = array();
		}

		// combine the 2 sets of identifiers
		$mods = array_merge( $static_ids, $mods );

		// finally, ensure that we are not including the module position that this MetaMod is in.
		if ( strlen( $position ) ) {
			for ( $i = 0; $i < count($mods); $i++ ) {
				if ( (string)$mods[$i] == (string)$position ) {
					unset( $mods[$i] );
					if ( $debug ) {
						echo JText::sprintf('MM_DEBUG_IGNORING_MODULE_POSITION', $position) . '<br>';
					}
				}
			}
		}

		 // remove everything equating to "0" / false / ""
		$mods = array_filter( $mods );
		
		if ($debug) {
			if ( is_array($mods) and count($mods) ) {
				$mod_report = implode( ', ', $mods );
			} else {
				$mod_report = "<i>" . JText::_("JNONE") . "</i>";
			}
			echo JText::_('MM_DEBUG_INCLUDING_MODULES') . ' ' . $mod_report .'<br />';
		}
		
		return array( $mods, $changes ); 
	}