Your IP : 216.73.216.54


Current Path : /var/www/html/components/com_jvld/
Upload File :
Current File : /var/www/html/components/com_jvld/router.php

<?php
/**
 * @version        $Id$
 * @package        JV-LinkDirectory
 * @subpackage     com_jvld
 * @copyright      Copyright 2008-2013 JV-Extensions. All rights reserved
 * @license        GNU General Public License version 3 or later
 * @author         JV-Extensions
 * @link           http://www.jv-extensions.com
 */

// No direct access
defined('_JEXEC') or die('Restricted access');

class JvldRouter extends JComponentRouterBase
{
	public function build(&$query)
	{
		$db = JFactory::getDbo();

		$segments = array();

		// We need a menu item.  Either the one specified in the query, or the current active one if none specified
		$menuItem = (empty($query['Itemid'])) ? $this->menu->getActive() : $this->menu->getItem($query['Itemid']);

		// Process the task
		if (isset($query['task']))
		{
			$task = $query['task'];
			switch ($task)
			{
				case 'quicknav':
					{
						$segments[] = $this->getSEFWord($task);
						break;
					}
				case 'delphoto':
				case 'delfile':
				case 'delcoupon':
				case 'delvideo':
				case 'dellink':
					{
						$segments[] = $this->getSEFWord($task);
						$segments[] = $query['lid'];

						unset($query['lid']);
						break;
					}
				case 'downloadfile':
					{
						$segments[] = $this->getSEFWord($task);
						$segments[] = $query['id'];

						$db = JFactory::getDbo();
						$db->setQuery("select concat_ws('-', title, version) from #__jvld_files where id = '" . (int) $query['id'] . "'");
						$segments[] = \Joomla\String\StringHelper::strtolower(\Joomla\String\StringHelper::str_ireplace(" ", "-", $db->loadResult()));

						unset($query['id']);
						break;
					}
				case 'delprofile':
					{
						$segments[] = $this->getSEFWord($task);
						$segments[] = $query['id'];

						unset($query['id']);
						break;
					}
				case 'resubmit':
				case 'lecs':
				case 'visit':
					{
						$segments[] = $this->getSEFWord($task);
						$segments[] = $this->getLinkData('sef_string', $query['lid']);

						unset($query['lid']);
						break;
					}
				case 'delbookmark':
					{
						$segments[] = $this->getSEFWord($task);
						$segments[] = $query['bid'];

						$db = JFactory::getDbo();
						$db->setQuery("select lid from #__jvld_bookmarks where id = '" . (int) $query['bid'] . "'");
						$lid = $db->loadResult();

						$segments[] = $this->getLinkData('sef_string', $lid);

						unset($query['bid']);
						break;
					}
				case 'emappr':
					{
						$segments[] = $this->getSEFWord($task);
						$segments[] = ($query['aprrej']) ? 'approve' : 'reject';

						unset($query['aprrej']);
						break;
					}
				default:
					{
						break;
					}
			}

			unset($query['task']);
		}

		// Process the view
		if (isset($query['view']))
		{
			$view = $query['view'];
			switch ($view)
			{
				case 'recommend':
				case 'suggestcat':
				case 'csponsor':
				case 'search':
				case 'addfeatured':
				case 'advertise':
				case 'notify':
				case 'pendinglinks':
				case 'addlink':
				case 'featuredlinks':
				case 'mypurchases':
				case 'mylinks':
				case 'editmyprofile':
				case 'myfavlinks':
					{
						$segments[] = $this->getSEFWord($view);
						break;
					}
				case 'reportbrokenlink':
				case 'newtestimonial':
				case 'tellafriend':
				case 'claimlink':
				case 'addbookmark':
				case 'contact':
				case 'detail':
				case 'profiles':
				case 'photos':
				case 'videos':
				case 'files':
				case 'coupons':
				case 'editphoto':
				case 'editvideo':
				case 'editfile':
				case 'editcoupon':
				case 'editlink':
				case 'switchplan':
				case 'renewlink':
				case 'pinlink':
				case 'setfeatured':
					{
						$segments[] = $this->getSEFWord($view);
						$segments[] = $this->getLinkData('sef_string', $query['lid']);

						unset($query['lid']);
						break;
					}
				case 'links':
					{
						$segments[] = $this->getCategoryData('path', $query['cid'], $menuItem);

						unset($query['cid']);
						break;
					}
				case 'relatedlinks':
					{
						$segments[] = $this->getSEFWord($view);

						$db->setQuery("select alias from #__jvld_tags where id = " . (int) $query['tag']);
						$segments[] = $db->loadResult();

						unset($query['tag']);
						break;
					}
				case 'userprofile':
					{
						$segments[] = $this->getSEFWord($view);

						$user       = JFactory::getUser((int) $query['id']);
						$segments[] = $query['id'] . '-' . htmlspecialchars($user->get('username'));

						unset($query['id']);
						break;
					}
				case 'editprofile':
					{
						$segments[] = $this->getSEFWord($view);
						$segments[] = $this->getLinkData('sef_string', $query['lid']);
						$segments[] = $query['id'];

						unset($query['lid']);
						unset($query['id']);
						break;
					}
				case 'showlinks':
					{
						$segments[] = $this->getSEFWord($view);
						$segments[] = $query['stype'];

						unset($query['stype']);
						break;
					}
				case 'thankyou':
					{
						$segments[] = $this->getSEFWord($view);
						$segments[] = $query['rtype'];

						unset($query['rtype']);
						break;
					}
				case 'alphabar':
					{
						$segments[] = $this->getSEFWord($view);
						$segments[] = $query['alpha'];

						unset($query['alpha']);
						break;
					}
				default:
					{
						break;
					}
			}

			unset($query['view']);
		}

		return $segments;
	}

	public function parse(&$segments)
	{
		$db   = JFactory::getDbo();
		$vars = array();

		if (!count($segments))
		{
			$vars['view'] = 'jvld';

			return $vars;
		}

		// Get the total number of pieces in segments
		for ($i = 0; $i < count($segments); $i++)
		{
			$segments[$i] = \Joomla\String\StringHelper::str_ireplace(":", "-", $segments[$i]);
		}

		// Get the active menu item.
		$menuItem = $this->menu->getActive();

		// Get the view from segments. It is always the first piece in the segments
		$sef_view = $segments[0];
		$view     = $this->getNonSEFWord($sef_view); // can be task also

		switch ($view)
		{
			/* Tasks */
			case 'quicknav':
				{
					$vars['task'] = 'quicknav';
					$vars['tmpl'] = 'component';
					$vars['format'] = 'raw';

					break;
				}
			case 'delphoto':
			case 'delfile':
			case 'delcoupon':
			case 'delvideo':
			case 'dellink':
				{
					$vars['task'] = $view;
					$vars['lid'] = $segments[1];

					break;
				}
			case 'downloadfile':
			case 'delprofile':
				{
					$vars['task'] = $view;
					$vars['id'] = $segments[1];

					break;
				}
			case 'resubmit':
			case 'lecs':
			case 'visit':
				{
					$vars['task'] = $view;
					$vars['lid'] = $this->getLinkData('id', $segments[1]);

					break;
				}
			case 'delbookmark':
				{
					$vars['task'] = $view;
					$vars['bid'] = $segments[1];

					break;
				}
			case 'emappr':
				{
					$vars['task'] = $view;
					$vars['aprrej'] = ($segments[1] == 'approve') ? 1 : 0;

					break;
				}

			/* Views */
			case 'recommend':
			case 'suggestcat':
			case 'csponsor':
			case 'search':
			case 'addfeatured':
			case 'advertise':
			case 'notify':
			case 'pendinglinks':
			case 'addlink':
			case 'featuredlinks':
			case 'mypurchases':
			case 'mylinks':
			case 'editmyprofile':
			case 'myfavlinks':
				{
					$vars['view'] = $view;

					break;
				}
			case 'reportbrokenlink':
			case 'newtestimonial':
			case 'tellafriend':
			case 'claimlink':
			case 'addbookmark':
			case 'contact':
			case 'detail':
			case 'profiles':
			case 'photos':
			case 'videos':
			case 'files':
			case 'coupons':
			case 'editphoto':
			case 'editvideo':
			case 'editfile':
			case 'editcoupon':
			case 'editlink':
			case 'switchplan':
			case 'renewlink':
			case 'pinlink':
			case 'setfeatured':
				{
					$vars['view'] = $view;
					$vars['lid']  = $this->getLinkData('id', $segments[1]);

					break;
				}
			case 'relatedlinks':
				{
					$vars['view'] = $view;

					$db->setQuery("select id from #__jvld_tags where alias = '" . $db->escape($segments[1]) . "'");
					$vars['tag'] = $db->loadResult();

					break;
				}
			case 'userprofile':
				{
					$vars['view'] = $view;

					$usr        = explode("-", $segments[1]);
					$vars['id'] = (int) $usr[0];

					break;
				}
			case 'editprofile':
				{
					$vars['view'] = $view;
					$vars['lid']  = $this->getLinkData('id', $segments[1]);
					$vars['id']   = $segments[2];

					break;
				}
			case 'showlinks':
				{
					$vars['view']  = $view;
					$vars['stype'] = $segments[1];

					break;
				}
			case 'thankyou':
				{
					$vars['view']  = $view;
					$vars['rtype'] = $segments[1];

					break;
				}
			case 'alphabar':
				{
					$vars['view']  = $view;
					$vars['alpha'] = $segments[1];

					break;
				}
			default:
				{
					$vars['view'] = 'links';

					// Combine all segments to form the full path
					$catpath     = implode('/', $segments);
					$vars['cid'] = $this->getCategoryData('id', $catpath, $menuItem);

					break;
				}
		}

		return $vars;
	}

	private function getDbSettingsVal($param)
	{
		static $config = null;
		if ($config == null)
		{
			require_once(JPATH_ROOT . DIRECTORY_SEPARATOR . "plugins" . DIRECTORY_SEPARATOR . "system" . DIRECTORY_SEPARATOR . "jvldframework" . DIRECTORY_SEPARATOR . "library" . DIRECTORY_SEPARATOR . "class.cfg.php");
			$config = JvldCfg::getInstance();
		}

		return $config->get($param);
	}

	private function getSEFWord($nonsef_word)
	{
		$words = explode("~", $this->getDbSettingsVal('sef_words'));
		for ($i = 0; $i < count($words); $i++)
		{
			if (preg_match("/^" . preg_quote($nonsef_word) . "=(.*)=(.*)$/", $words[$i], $matches))
			{
				return $matches[2];
			}
		}

		return '';
	}

	private function getNonSEFWord($sefword)
	{
		$words = explode("~", $this->getDbSettingsVal('sef_words'));
		for ($i = 0; $i < count($words); $i++)
		{
			if ((preg_match("/(.*)=(.*)=" . preg_quote($sefword) . "$/", $words[$i], $matches)) || (preg_match("/(.*)=" . preg_quote($sefword) . "=(.*)$/", $words[$i], $matches)))
			{
				return $matches[1];
			}
		}

		return $sefword;
	}

	private function getCategoryData($what, $key, $menuItem)
	{
		static $csef_info = array();

		if (!count($csef_info))
		{
			$db = JFactory::getDbo();
			$db->setQuery("select id, path from #__categories where extension = 'com_jvld'");
			$rows = $db->loadObjectList();
			foreach ($rows as $row)
			{
				$csef_info[$row->id] = \Joomla\String\StringHelper::trim($row->path);
			}
		}

		if ($what == 'path')
		{
			// Category id is provided. Dont just return the full path. Instead check the menuitem.
			// If the menu item is of global then return full path
			// Else, get the cid from the menu item, get the path for it, and truncate that from the global path and return the rest

			$global_path = $csef_info[$key];

			if (($menuItem == null) || ($menuItem->link == 'index.php?option=com_jvld&view=jvld'))
			{
				// global
				return $global_path;
			}
			else
			{
				// truncate
				$result = array();
				parse_str($menuItem->link, $result);
				if (isset($result['cid']))
				{
					$menuItem_cid = $result['cid'];

					return \Joomla\String\StringHelper::str_ireplace($csef_info[$menuItem_cid], "", $global_path);
				}
				else
				{
					return $global_path;
				}
			}
		}
		else if ($what == 'id')
		{
			// Path is provided. Dont just return the id for it. Instead check the menuitem.
			// If the menu item is of global then return the id.
			// Else, get the cid from the menu item, get the path for it, and merge that with path provided and get the full path. Then return the cid that maps to that full path
			if (($menuItem == null) || ($menuItem->link == 'index.php?option=com_jvld&view=jvld'))
			{
				// global
				return array_search($key, $csef_info);
			}
			else
			{
				$result = array();
				parse_str($menuItem->link, $result);
				if (isset($result['cid']))
				{
					$menuItem_cid = $result['cid'];
					$new_path     = $csef_info[$menuItem_cid] . "/" . $key;

					return array_search($new_path, $csef_info);
				}
				else
				{
					return array_search($key, $csef_info);
				}
			}
		}
		else
			return '';
	}

	private function getLinkData($what, $key)
	{
		static $lsef_info = array();

		if (!count($lsef_info))
		{
			$db = JFactory::getDbo();
			$db->setQuery("select id, sef_string from #__jvld_links");
			$rows = $db->loadObjectList();

			foreach ($rows as $row)
			{
				$lsef_info[$row->id] = \Joomla\String\StringHelper::trim($row->sef_string);
			}
		}

		if ($what == 'sef_string')
		{
			return $lsef_info[$key];
		}
		else if ($what == 'id')
		{
			return array_search($key, $lsef_info);
		}
		else
			return '';
	}

	private function log($msg)
	{
		$dbgFile = JPATH_ROOT . DIRECTORY_SEPARATOR . "tmp" . DIRECTORY_SEPARATOR . "com_jvld.router.html";
		$fp      = (JFile::exists($dbgFile)) ? fopen($dbgFile, "a+") : fopen($dbgFile, "w");
		$tnow    = JvldDate::getDateForDisplay();
		fwrite($fp, "[" . $tnow . "] " . $msg . "<br />");
		fclose($fp);
	}
}

function JvldBuildRoute(&$query)
{
	$router = new JvldRouter();

	return $router->build($query);
}

function JvldParseRoute($segments)
{
	$router = new JvldRouter();

	return $router->parse($segments);
}