Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP issue causing router.php error in menu (SEF Link issue)...

Posted on 2009-04-05
6
Medium Priority
?
4,816 Views
Last Modified: 2013-12-12
Working on a joomla installation on a Linux server. Some nasty malicious code somehow made it onto my desktop which I normally have locked down tight. It attached itself to my ftp program, uploaded snippets of code to the joomla installation (on several domains actually). Well, I found every instance of code and can get into the back end now no problem. But when trying to access the front end of the site (which I had at the moment, put offline), I get this:

Fatal error: Call to a member function getDefault() on a non-object in /XXXX/XXXXX/public_html/XXXXXXX/includes/router.php on line 176

What is found on line 176 is:
$item = $menu->getDefault();

The Full Code of the router.php is attached in the code portion of this post.  I was wondering if it was something in my sql database as I've tried just about everything on the Joomla install...
<?php
/**
* @version		$Id: router.php 8180 2007-07-23 05:52:29Z eddieajau $
* @package		Joomla.Framework
* @subpackage	Application
* @copyright	Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
* @license		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.
*/
 
// Check to ensure this file is within the rest of the framework
defined('JPATH_BASE') or die();
 
/**
 * Class to create and parse routes for the site application
 *
 * @package 	Joomla
 * @since		1.5
 */
class JRouterSite extends JRouter
{
	/**
	 * Class constructor
	 *
	 * @access public
	 */
	function __construct($options = array()) {
		parent::__construct($options);
	}
 
	function parse(&$uri)
	{
		$vars = array();
 
		// Get the application
		$app =& JFactory::getApplication();
 
		if($app->getCfg('force_ssl') == 2 && strtolower($uri->getScheme()) != 'https') {
			//forward to https
			$uri->setScheme('https');
			$app->redirect($uri->toString());
		}
 
 
		// Get the path
		$path = $uri->getPath();
 
		//Remove the suffix
		if($this->_mode == JROUTER_MODE_SEF)
		{
 
			if($app->getCfg('sef_suffix') && !(substr($path, -9) == 'index.php' || substr($path, -1) == '/'))
			{
				if($suffix = pathinfo($path, PATHINFO_EXTENSION))
				{
					$path = str_replace('.'.$suffix, '', $path);
					$vars['format'] = $suffix;
				}
			}
		}
 
		//Remove basepath
		$path = substr_replace($path, '', 0, strlen(JURI::base(true)));
 
		//Remove prefix
		$path = str_replace('index.php', '', $path);
 
		//Set the route
		$uri->setPath(trim($path , '/'));
 
		$vars += parent::parse($uri);
 
		return $vars;
	}
 
	function &build($url)
	{
		$uri =& parent::build($url);
 
		// Get the path data
		$route = $uri->getPath();
 
		//Add the suffix to the uri
		if($this->_mode == JROUTER_MODE_SEF && $route)
		{
			$app =& JFactory::getApplication();
 
			if($app->getCfg('sef_suffix') && !(substr($route, -9) == 'index.php' || substr($route, -1) == '/'))
			{
				if($format = $uri->getVar('format', 'html'))
				{
					$route .= '.'.$format;
					$uri->delVar('format');
				}
			}
 
			if($app->getCfg('sef_rewrite'))
			{
				//Transform the route
				$route = str_replace('index.php/', '', $route);
			}
		}
 
		//Add basepath to the uri
		$uri->setPath(JURI::base(true).'/'.$route);
 
		return $uri;
	}
 
	function _parseRawRoute(&$uri)
	{
		$vars   = array();
 
		$menu =& JSite::getMenu(true);
 
		//Handle an empty URL (special case)
		if(!$uri->getVar('Itemid') && !$uri->getVar('option'))
		{
			$item = $menu->getDefault();
			if(!is_object($item)) return $vars; // No default item set
 
			//Set the information in the request
			$vars = $item->query;
 
			//Get the itemid
			$vars['Itemid'] = $item->id;
 
			// Set the active menu item
			$menu->setActive($vars['Itemid']);
 
			return $vars;
		}
 
		//Get the variables from the uri
		$this->setVars($uri->getQuery(true));
 
		//Get the itemid, if it hasn't been set force it to null
		$this->setVar('Itemid', JRequest::getInt('Itemid', null));
 
		//Only an Itemid ? Get the full information from the itemid
		if(count($this->getVars()) == 1)
		{
			$item = $menu->getItem($this->getVar('Itemid'));
			$vars = $vars + $item->query;
		}
 
		// Set the active menu item
		$menu->setActive($this->getVar('Itemid'));
 
		return $vars;
	}
 
	function _parseSefRoute(&$uri)
	{
		$vars   = array();
 
		$menu  =& JSite::getMenu(true);
		$route = $uri->getPath();
 
		//Get the variables from the uri
		$vars = $uri->getQuery(true);
 
		//Handle an empty URL (special case)
		if(empty($route))
		{
 
			//If route is empty AND option is set in the query, assume it's non-sef url, and parse apropriately
			if(isset($vars['option']) || isset($vars['Itemid'])) {
				return $this->_parseRawRoute($uri);
			}
 
			$item = $menu->getDefault();
 
			//Set the information in the request
			$vars = $item->query;
 
			//Get the itemid
			$vars['Itemid'] = $item->id;
 
			// Set the active menu item
			$menu->setActive($vars['Itemid']);
 
			return $vars;
		}
 
 
		/*
		 * Parse the application route
		 */
 
		if(substr($route, 0, 9) == 'component')
		{
			$segments	= explode('/', $route);
			$route      = str_replace('component/'.$segments[1], '', $route);
 
			$vars['option'] = 'com_'.$segments[1];
			$vars['Itemid'] = null;
		}
		else
		{
			//Need to reverse the array (highest sublevels first)
			$items = array_reverse($menu->getMenu());
 
			foreach ($items as $item)
			{
				$lenght = strlen($item->route); //get the lenght of the route
 
				if($lenght > 0 && strpos($route.'/', $item->route.'/') === 0 && $item->type != 'menulink')
				{
					$route   = substr($route, $lenght);
 
					$vars['Itemid'] = $item->id;
					$vars['option'] = $item->component;
					break;
				}
			}
		}
 
		// Set the active menu item
		if ( isset($vars['Itemid']) ) {
			$menu->setActive(  $vars['Itemid'] );
		}
 
		//Set the variables
		$this->setVars($vars);
 
		/*
		 * Parse the component route
		 */
		if(!empty($route) && isset($this->_vars['option']) )
		{
			$segments = explode('/', $route);
			array_shift($segments);
 
			// Handle component	route
			$component = preg_replace('/[^A-Z0-9_\.-]/i', '', $this->_vars['option']);
 
			// Use the component routing handler if it exists
			$path = JPATH_SITE.DS.'components'.DS.$component.DS.'router.php';
 
			if (file_exists($path) && count($segments))
			{
				if ($component != "com_search") { // Cheep fix on searches
					//decode the route segments
					$segments = $this->_decodeSegments($segments);
				}
 
				require_once $path;
				$function =  substr($component, 4).'ParseRoute';
				$vars =  $function($segments);
 
				$this->setVars($vars);
			}
		}
		else
		{
			//Set active menu item
			if($item =& $menu->getActive()) {
				$vars = $item->query;
			}
		}
 
		return $vars;
	}
 
	function _buildRawRoute(&$uri)
	{
	}
 
	function _buildSefRoute(&$uri)
	{
		// Get the route
		$route = $uri->getPath();
 
		// Get the query data
		$query = $uri->getQuery(true);
 
		if(!isset($query['option'])) {
			return;
		}
 
		$menu =& JSite::getMenu();
 
		/*
		 * Build the component route
		 */
		$component	= preg_replace('/[^A-Z0-9_\.-]/i', '', $query['option']);
		$tmp 		= '';
 
		// Use the component routing handler if it exists
		$path = JPATH_SITE.DS.'components'.DS.$component.DS.'router.php';
 
		// Use the custom routing handler if it exists
		if (file_exists($path) && !empty($query))
		{
			require_once $path;
			$function	= substr($component, 4).'BuildRoute';
			$parts		= $function($query);
 
			// encode the route segments
			if ($component != "com_search") { // Cheep fix on searches
				$parts = $this->_encodeSegments($parts);
			}
			else { // fix up search for URL
				$total = count($parts);
				for($i=0; $i<$total; $i++) {
					// urlencode twice because it is decoded once after redirect
					$parts[$i] = urlencode(urlencode(stripcslashes($parts[$i])));
				}
			}
 
			$result = implode('/', $parts);
			$tmp	= ($result != "") ? '/'.$result : '';
		}
 
		/*
		 * Build the application route
		 */
		$built = false;
		if (isset($query['Itemid']) && !empty($query['Itemid']))
		{
			$item = $menu->getItem($query['Itemid']);
 
			if (is_object($item) && $query['option'] == $item->component) {
				$tmp = !empty($tmp) ? $item->route.'/'.$tmp : $item->route;
				$built = true;
			}
		}
 
		if(!$built) {
			$tmp = 'component/'.substr($query['option'], 4).'/'.$tmp;
		}
 
		$route .= '/'.$tmp;
 
		// Unset unneeded query information
		unset($query['Itemid']);
		unset($query['option']);
 
		//Set query again in the URI
		$uri->setQuery($query);
		$uri->setPath($route);
	}
 
	function _processParseRules(&$uri)
	{
		// Process the attached parse rules
		$vars = parent::_processParseRules($uri);
 
		// Process the pagination support
		if($this->_mode == JROUTER_MODE_SEF)
		{
			$app =& JFactory::getApplication();
 
			if($start = $uri->getVar('start'))
			{
				$uri->delVar('start');
				$vars['limitstart'] = $start;
			}
		}
 
		return $vars;
	}
 
	function _processBuildRules(&$uri)
	{
		// Make sure any menu vars are used if no others are specified
		if(($this->_mode != JROUTER_MODE_SEF) && $uri->getVar('Itemid') && count($uri->getQuery(true)) == 2)
		{
			$menu =& JSite::getMenu();
 
			// Get the active menu item
			$itemid = $uri->getVar('Itemid');
			$item   = $menu->getItem($itemid);
 
			$uri->setQuery($item->query);
			$uri->setVar('Itemid', $itemid);
		}
 
		// Process the attached build rules
		parent::_processBuildRules($uri);
 
		// Get the path data
		$route = $uri->getPath();
 
		if($this->_mode == JROUTER_MODE_SEF && $route)
		{
			$app =& JFactory::getApplication();
 
			if ($limitstart = $uri->getVar('limitstart'))
			{
				$uri->setVar('start', (int) $limitstart);
				$uri->delVar('limitstart');
			}
		}
 
		$uri->setPath($route);
	}
 
	function &_createURI($url)
	{
		//Create the URI
		$uri =& parent::_createURI($url);
 
		// Set URI defaults
		$menu =& JSite::getMenu();
 
		// Get the itemid form the URI
		$itemid = $uri->getVar('Itemid');
 
		if(is_null($itemid))
		{
			if($option = $uri->getVar('option'))
			{
				$item  = $menu->getItem($this->getVar('Itemid'));
				if(isset($item) && $item->component == $option) {
					$uri->setVar('Itemid', $item->id);
				}
			}
			else
			{
				if($option = $this->getVar('option')) {
					$uri->setVar('option', $option);
				}
 
				if($itemid = $this->getVar('Itemid')) {
					$uri->setVar('Itemid', $itemid);
				}
			}
		}
		else
		{
			if(!$uri->getVar('option'))
			{
				$item  = $menu->getItem($itemid);
				$uri->setVar('option', $item->component);
			}
		}
 
		return $uri;
	}
}

Open in new window

0
Comment
Question by:EvinceUnlimited
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 12

Expert Comment

by:Richard Davis
ID: 24080847
That error is typically defined as meaning that the object referred to in the error statement was probably not created. I would confirm that the JSite class object is actually being created at some earlier stage in your code. I have no idea what all comes before this class file is implemented, but whatever file is including and using this class obviously requires that the JSite object be created prior to using this class.

Hope this opened up some courses of troubleshooting for you.

~A~
0
 

Author Comment

by:EvinceUnlimited
ID: 24081817
Hey Adrian,

This gives me a little more to go on..but not much.  I'm looking through now but still a little unsure. I'm on rocky territory here.  Upon deeper searching, it looks like this is definitely an issue with Search Engine Friendly Links in Joomla. When I toggle it to enable, I get error on line 176, when I toggle it for non SEF links, error appears on 123. Digging a bit deeper.
0
 
LVL 12

Expert Comment

by:Richard Davis
ID: 24083165
Due to my lack of experience with the execution structure of the Joomla CMS package, I, unfortunately, can only offer you logically driven advise. But I think you are definitely on the right track.

I would be focusing very strongly on the creation of that object variable $menu as that appears to be common to both errors. Something either is preventing the execution of some code earlier that creates that instance of the JSite class that that $menu var is referencing.

~A~
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 4

Expert Comment

by:dharmanerd
ID: 24112840
do a var_dump on line 119 to see what $menu holds. if it's an array and not an object, that could be your issue. if it's empty, then do a debug backtrace to figure out where the menu function is.
0
 

Accepted Solution

by:
EvinceUnlimited earned 0 total points
ID: 24112849
Sorry to mention that the fix was really simple. The includes folder was corrupted on upload. I reuploaded and all was fixed... Thanks for all the assistance though...
0
 
LVL 12

Expert Comment

by:Richard Davis
ID: 24138228
Glad you figured it out. I have had that same issue and it's extremely frustrating when everything "appears" fine from the 5000 ft level.

Take care :)

~A~
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question