<?php
/*

 

Freeway eCommerce
http://www.openfreeway.org
Copyright (c) 2007 ZacWare
Released under the GNU General Public License
*/

// Check to ensure this file is included in Freeway!
//defined('_FEXEC') or die();


  class seo_url{
	var $lastError;
	var $enabled;
	
	function seo_url(){							
		// check for initial params
		global $joomla_include,$cookie_path;
		$this->enabled=true;
		$this->lastError="";
        if (strtolower(SEARCH_ENGINE_FRIENDLY_URLS) == 'true'){
		    if (strtolower(USE_CACHE) <> 'true'){
				$this->enabled=false;
				$this->lastError.='Use cache needs to be set to \'True\' in the Admin.';
			} else {
				if (!is_writable(DIR_FS_CACHE)){
					$this->enabled=false;
					$this->lastError='The cache directory \'' .DIR_FS_CACHE . '\' needs to be writable to use SEO URL\'s.';
				}
			}
	   		if (strtolower(SESSION_FORCE_COOKIE_USE) <> 'true'){
				$this->enabled=false;
				$this->lastError.='Force cookie use needs to be set to \'True\' in the Admin';
			} 
			if ($this->lastError!='') $this->lastError="SEO URL Error: " . $this->lastError;
		} else {
			$this->enabled=false;
		}
	}
    // Prepares URL characters
    function prepare_url($url) {
		  $url = strtr($url, '������������������������������������������������������������', 'SZszYAAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy');
	
		  $url = str_replace(' ', '-', preg_replace('[^/[:space:]a-zA-Z0-9_-]/', '', $url));
	
		  while (strstr($url, '--')) $url = str_replace('--', '-', $url);
		  return $url;
    }

    // Select which pages to use SEO URLs on
    function pages($page) {
      $page_array = array(FILENAME_DEFAULT, FILENAME_PRODUCT_INFO,FILENAME_EVENTS_MONTH_VIEW,FILENAME_EVENTS_WEEK_VIEW,FILENAME_EVENTS_DAY_VIEW,FILENAME_SUBSCRIPTION_INFO,FILENAME_SERVICES_INFO,FILENAME_SESSION_DETAILS,'link_index.php');
      return in_array($page, $page_array);
    }

    // Transform the URLs   
    function convert_seo_url($url) {
      global $FSESSION;		
      // Split the URL parts into an array
      $url_parts = parse_url($url);
      // Exit if the URL is not specified in the pages function
	  $tempPath=$url_parts['path'];
	  if (strlen(DIR_WS_HTTP_CATALOG)>1) $tempPath=trim(str_replace(DIR_WS_HTTP_CATALOG,'',$url_parts['path']));
	  else if (substr($url_parts['path'],0,1)=="/")
	     $tempPath=substr($url_parts['path'],1);
		 
	  $tempSplt=preg_split('///', $tempPath);
      if ((strpos($url, 'action')!==false || strpos($url, 'command')!==false) || (!$this->pages(current($url_array=explode("/",$tempPath)))))
			return $url;
	  // Remove the page name from the URL array
	  array_shift($url_array);
      $url_parts['path'] = rtrim(DIR_WS_HTTP_CATALOG, '/');

		$item_type='';
		$main_path='';
		$extra_path='';
      for ($i = 0; $i < sizeof($url_array); $i++) {
      	switch ($url_array[$i]) {
          case 'cPath':
            $i++;
            $item_type='products';
			
            $category_array = explode('_', $url_array[$i]);

            foreach ($category_array as $categories_id) {
              $category_query = tep_db_query("select distinct c.categories_id, cd.categories_name from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.categories_id = '" . (int)$categories_id . "' and c.categories_id = cd.categories_id and cd.language_id = '" . (int)$FSESSION->languages_id . "'");
              $category_name = tep_db_fetch_array($category_query);
              $main_path .= '/' . $this->prepare_url($category_name['categories_name']);
            }

            break;

          case 'products_id':
            $i++;
            if (strpos($url, 'cPath') === false) {
              $item_type='products';
              $cat_array = array();
              $cat_query = tep_db_query("SELECT cd.categories_name as name, c.parent_id FROM categories c LEFT JOIN categories_description cd ON (c.categories_id = cd.categories_id) LEFT JOIN products_to_categories p2c ON (p2c.categories_id = c.categories_id) WHERE p2c.products_id = '" . (int)$url_array[$i] . "' AND p2c.categories_id = c.categories_id AND cd.language_id = '" . (int)$FSESSION->languages_id . "'");
              $cat_val = tep_db_fetch_array($cat_query);
              
              $cat_array[] = $cat_val['name'];
              
              //If this category has a parent, get the name
              if (is_numeric($cat_val['parent_id']) && $cat_val['parent_id'] != '0') {
                $parent_id = $cat_val['parent_id'];
                while ($parent_id != '0') {
                  $cat_query = tep_db_query("SELECT cd.categories_name as name, c.parent_id FROM categories c LEFT JOIN categories_description cd ON (c.categories_id = cd.categories_id) WHERE cd.categories_id = '" . (int)$parent_id . "' AND cd.language_id = '" . (int)$FSESSION->languages_id . "'");
                  $cat_val = tep_db_fetch_array($cat_query);
                  
                  $cat_array[] = $cat_val['name'];
                  $parent_id = $cat_val['parent_id'];
                }
              }
              
              //Reverse array order
              $cat_array = array_reverse($cat_array);
              
              for ($x = 0; $x <= sizeof($cat_array); $x++) {
                $main_path .= '/' . $this->prepare_url($cat_array[$x]);
              }
            }
            //ADD CATEGORY NAME MODIFICATION END
            
            $product_query = tep_db_query("select distinct products_name from " . TABLE_PRODUCTS_DESCRIPTION . " where products_id = '" . (int)$url_array[$i] . "' and language_id = '" . (int)$FSESSION->languages_id . "'");
            $product_name = tep_db_fetch_array($product_query);
            $main_path .= '/' . $this->prepare_url($product_name['products_name']);
            break;
          case 'ecPath':
            $i++;
            $item_type='events';
			
            $category_array = explode('_', $url_array[$i]);

            foreach ($category_array as $categories_id) {
              $category_query = tep_db_query("select distinct ec.events_categories_id, ecd.events_categories_name from " . TABLE_EVENTS_CATEGORIES . " ec, " . TABLE_EVENTS_CATEGORIES_DESCRIPTION . " ecd where ec.events_categories_id = '" . (int)$categories_id . "' and ec.events_categories_id = ecd.events_categories_id and ecd.language_id = '" . (int)$FSESSION->languages_id . "'");
              $category_name = tep_db_fetch_array($category_query);
              $main_path .= '/' . $this->prepare_url($category_name['events_categories_name']);
            }

            break;

          case 'events_id':
            $i++;
            if (strpos($url, 'ecPath') === false) {
              $item_type='events';
              $cat_array = array();
              $cat_query = tep_db_query("SELECT ecd.events_categories_name as name, ec.parent_id FROM events_categories ec LEFT JOIN events_categories_description ecd ON (ec.events_categories_id = ecd.events_categories_id) LEFT JOIN events_to_events_categories e2c ON (e2c.events_categories_id = ec.events_categories_id) WHERE e2c.events_id = '" . (int)$url_array[$i] . "' AND e2c.events_categories_id = ec.events_categories_id AND ecd.language_id = '" . (int)$FSESSION->languages_id . "'");
              $cat_val = tep_db_fetch_array($cat_query);
              
              $cat_array[] = $cat_val['name'];
              
              //If this category has a parent, get the name
              if (is_numeric($cat_val['parent_id']) && $cat_val['parent_id'] != '0') {
                $parent_id = $cat_val['parent_id'];
                while ($parent_id != '0') {
                  $cat_query = tep_db_query("SELECT ecd.events_categories_name as name, ec.parent_id FROM events_categories ec LEFT JOIN events_categories_description ecd ON (ec.events_categories_id = ecd.events_categories_id) WHERE ecd.events_categories_id = '" . (int)$parent_id . "' AND ecd.language_id = '" . (int)$FSESSION->languages_id . "'");
                  $cat_val = tep_db_fetch_array($cat_query);
                  
                  $cat_array[] = $cat_val['name'];
                  $parent_id = $cat_val['parent_id'];
                }
              }
              
              //Reverse array order
              $cat_array = array_reverse($cat_array);
              
              for ($x = 0; $x <= sizeof($cat_array); $x++) {
                $main_path .= '/' . $this->prepare_url($cat_array[$x]);
              }
            }

            
            $product_query = tep_db_query("select distinct events_name from " . TABLE_EVENTS_DESCRIPTION . " where events_id = '" . (int)$url_array[$i] . "' and language_id = '" . (int)$FSESSION->languages_id . "'");
            $name = tep_db_fetch_array($product_query);
            $main_path .= '/' . $this->prepare_url($name['events_name']);
            break;
           case 'sucPath':
            $i++;
            $item_type='subscriptions';
            $category_array = explode('_', $url_array[$i]);

            foreach ($category_array as $categories_id) {
              $category_query = tep_db_query("select distinct suc.subscription_categories_id, sucd.subscription_categories_name from " . TABLE_SUBSCRIPTION_CATEGORIES . " suc, " . TABLE_SUBSCRIPTION_CATEGORIES_DESCRIPTION . " sucd where suc.subscription_categories_id = '" . (int)$categories_id . "' and suc.subscription_categories_id = sucd.subscription_categories_id and sucd.language_id = '" . (int)$FSESSION->languages_id . "'");
              $category_name = tep_db_fetch_array($category_query);
              $main_path .= '/' . $this->prepare_url($category_name['subscription_categories_name']);
            }
			
            break;

          case 'subscription_id':
            $i++;
            if (strpos($url, 'sucPath') === false) {
              $item_type='subscriptions';
              $cat_array = array();
              $cat_query = tep_db_query("SELECT sucd.subscription_categories_name as name, suc.parent_id FROM subscription_categories suc LEFT JOIN subscription_categories_description sucd ON (suc.subscription_categories_id = sucd.subscription_categories_id) LEFT JOIN subscription_to_subscription_categories su2c ON (su2c.subscription_categories_id = suc.subscription_categories_id) WHERE su2c.subscription_id = '" . (int)$url_array[$i] . "' AND su2c.subscription_categories_id = suc.subscription_categories_id AND sucd.language_id = '" . (int)$FSESSION->languages_id . "'");
              $cat_val = tep_db_fetch_array($cat_query);
              
              $cat_array[] = $cat_val['name'];
              
              //If this category has a parent, get the name
              if (is_numeric($cat_val['parent_id']) && $cat_val['parent_id'] != '0') {
                $parent_id = $cat_val['parent_id'];
                while ($parent_id != '0') {
                  $cat_query = tep_db_query("SELECT sucd.subscription_categories_name as name, suc.parent_id FROM subscription_categories suc LEFT JOIN subscription_categories_description sucd ON (suc.subscription_categories_id = sucd.subscription_categories_id) WHERE sucd.subscription_categories_id = '" . (int)$parent_id . "' AND sucd.language_id = '" . (int)$FSESSION->languages_id . "'");
                  $cat_val = tep_db_fetch_array($cat_query);
                  
                  $cat_array[] = $cat_val['name'];
                  $parent_id = $cat_val['parent_id'];
                }
              }
              
              //Reverse array order
              $cat_array = array_reverse($cat_array);
              
              for ($x = 0; $x <= sizeof($cat_array); $x++) {
                if($this->prepare_url($cat_array[$x])!='')$main_path .= '/' . $this->prepare_url($cat_array[$x]);
              }
            }

            
            $product_query = tep_db_query("select distinct subscription_name from " . TABLE_SUBSCRIPTIONS_DESCRIPTION . " where subscription_id = '" . (int)$url_array[$i] . "' and language_id = '" . (int)$FSESSION->languages_id . "'");
            $name = tep_db_fetch_array($product_query);
            $main_path .= '/' . $this->prepare_url($name['subscription_name']);
            break;
          case 'secPath':
            $i++;
            $item_type='services';
			
            $category_array = explode('_', $url_array[$i]);

            foreach ($category_array as $categories_id) {
              $category_query = tep_db_query("select distinct sec.services_categories_id, secd.services_categories_name from " . TABLE_SERVICES_CATEGORIES . " sec, " . TABLE_SERVICES_CATEGORIES_DESCRIPTION . " secd where sec.services_categories_id = '" . (int)$categories_id . "' and sec.services_categories_id = secd.services_categories_id and secd.language_id = '" . (int)$FSESSION->languages_id . "'");
              $category_name = tep_db_fetch_array($category_query);
              $main_path .= '/' . $this->prepare_url($category_name['services_categories_name']);
            }

            break;

          case 'service_id':
            $i++;
            if (strpos($url, 'secPath') === false) {
              $item_type='services';
              $cat_array = array();
              $cat_query = tep_db_query("SELECT secd.services_categories_name as name, sec.parent_id FROM services_categories sec LEFT JOIN services_categories_description secd ON (sec.services_categories_id = secd.services_categories_id) LEFT JOIN services_to_services_categories se2c ON (se2c.services_categories_id = sec.services_categories_id) WHERE se2c.service_id = '" . (int)$url_array[$i] . "' AND se2c.services_categories_id = sec.services_categories_id AND secd.language_id = '" . (int)$FSESSION->languages_id . "'");
              $cat_val = tep_db_fetch_array($cat_query);
              
              $cat_array[] = $cat_val['name'];
              
              //If this category has a parent, get the name
              if (is_numeric($cat_val['parent_id']) && $cat_val['parent_id'] != '0') {
                $parent_id = $cat_val['parent_id'];
                while ($parent_id != '0') {
                  $cat_query = tep_db_query("SELECT secd.services_categories_name as name, sec.parent_id FROM services_categories sec LEFT JOIN services_categories_description secd ON (sec.services_categories_id = secd.services_categories_id) WHERE secd.services_categories_id = '" . (int)$parent_id . "' AND secd.language_id = '" . (int)$FSESSION->languages_id . "'");
                  $cat_val = tep_db_fetch_array($cat_query);
                  
                  $cat_array[] = $cat_val['name'];
                  $parent_id = $cat_val['parent_id'];
                }
              }
              
              //Reverse array order
              $cat_array = array_reverse($cat_array);
              
              for ($x = 0; $x <= sizeof($cat_array); $x++) {
                $main_path .= '/' . $this->prepare_url($cat_array[$x]);
              }
            }

            
            $product_query = tep_db_query("select sed.service_name,se.service_costs from " . TABLE_SERVICES_DESCRIPTION . " sed," . TABLE_SERVICES . " se where se.service_id = '" . (int)$url_array[$i] . "' and se.service_id=sed.service_id and sed.language_id = '" . (int)$FSESSION->languages_id . "'");
            $name = tep_db_fetch_array($product_query);
            $main_path .= '/' . $this->prepare_url($name['service_name'] . "-" . number_format($name["service_costs"],0,'',''));
            break;
          case 'manufacturers_id':
			$item_type='products';
            $i++;

            $manufacturer_query = tep_db_query("select distinct manufacturers_name from " . TABLE_MANUFACTURERS . " where manufacturers_id = '" . (int)$url_array[$i] . "'");
            $manufacturer_name = tep_db_fetch_array($manufacturer_query);
            $main_path .= '/' . $this->prepare_url($manufacturer_name['manufacturers_name']);
            break;

          default:

            $extra_path .= '/' . $url_array[$i];

            break;
        }
      } 
	  if ($extra_path!='') $extra_path="/ex" . $extra_path;
	  	  
	  if ($item_type!='') $item_type="/" . $item_type;
	  
	  if ($main_path=="") {
	  	return $url;
	  }
      // Return the converted URL

      return $url_parts['scheme'] . '://' . $url_parts['host'] . ($url_parts['port']!=''?':' .$url_parts['port']:'') . $url_parts['path'] . $item_type . $main_path . $extra_path;

    }

	// Changes the names of URL's into intergers
	function encrypt_url($url) {
		//if( ($url!=' ' || trim($url)!='') ){
	    if( (trim(str_replace(array(" ","&nbsp;"),"",$url))!='' && $url!=' ') ){
			return sprintf('%u', crc32($this->prepare_url(strtolower($url))));
		}	
	}

	function restore_seo_url() {
		global $FREQUEST;
		
		$check_pos=strpos($FREQUEST->servervalue('REQUEST_URI'),"404;");
		if ($check_pos!==false){
			$FREQUEST->setvalue('REQUEST_URI',substr($FREQUEST->servervalue('REQUEST_URI'),$check_pos+4),'SERVER');
			$check_pos=strpos($FREQUEST->servervalue('REQUEST_URI'),DIR_WS_HTTP_CATALOG);
			$FREQUEST->setvalue('REQUEST_URI',substr($FREQUEST->servervalue('REQUEST_URI'),$check_pos),'SERVER');
		}
		// Exit if not being called from the SEO pages or contains 'action'
		if ((!$this->pages(basename($FREQUEST->servervalue('PHP_SELF')))) || ($FREQUEST->servervalue('REQUEST_URI') == '/') || (strpos($FREQUEST->servervalue('REQUEST_URI'), '.php')))
			return;
		$uri=str_replace(DIR_WS_HTTP_CATALOG,"/",$FREQUEST->servervalue('REQUEST_URI'));
		
		$request_url_array = explode('/', trim(trim($uri, '/'), $this->extention));
		
		if ($request_url_array[0]=="events")
			$url_array = $this->cache_events_url();
		else if ($request_url_array[0]=="subscriptions"){
			$url_array = $this->cache_subscriptions_url();
		}else if ($request_url_array[0]=="services")
			$url_array = $this->cache_services_url();
		else
			$url_array = $this->cache_products_url();
		$extra_pos=false;
		
		for ($i = 0; $i < count($request_url_array); $i++) {
			if ($request_url_array[$i]=="ex") {
				$extra_pos=true;
				continue;
			}
			if (!$extra_pos){
				
				if ($url_array[$this->encrypt_url($request_url_array[$i])]['key'] == 'categories_id') {
					if ($request_url_array[0]=="events"){
						if ($FREQUEST->getvalue('ecPath')=="") {
							$FREQUEST->setvalue('ecPath',$url_array[$this->encrypt_url($request_url_array[$i])]['value'],'GET');
						} else {
							$temp_path=$FREQUEST->getvalue('ecPath').'_'.$url_array[$this->encrypt_url($request_url_array[$i])]['value'];
							$FREQUEST->setvalue('ecPath',$temp_path,'GET'); 
						}
					} else if ($request_url_array[0]=="subscriptions"){
						if ($FREQUEST->getvalue('sucPath')=="") {
							$FREQUEST->setvalue('sucPath',$url_array[$this->encrypt_url($request_url_array[$i])]['value'],'GET');
						} else {
							$temp_path=$FREQUEST->getvalue('sucPath').'_' . $url_array[$this->encrypt_url($request_url_array[$i])]['value'];
							$FREQUEST->setvalue('sucPath',$temp_path,'GET'); 
						}
					} else if ($request_url_array[0]=="services"){
						if ($FREQUEST->getvalue('secPath')=="") {
							$FREQUEST->setvalue('secPath',$url_array[$this->encrypt_url($request_url_array[$i])]['value'],'GET');
						} else {
							$temp_path=$FREQUEST->getvalue('secPath').'_' .$url_array[$this->encrypt_url($request_url_array[$i])]['value'];
							$FREQUEST->setvalue('secPath',$temp_path,'GET');
						}
					} else {
						if ($FREQUEST->getvalue('cPath')=="") {
							$FREQUEST->setvalue('cPath',$url_array[$this->encrypt_url($request_url_array[$i])]['value'],'GET');
						} else {
							$temp_path=$FREQUEST->getvalue('cPath').'_' .$url_array[$this->encrypt_url($request_url_array[$i])]['value'];
							$FREQUEST->setvalue('cPath',$temp_path,'GET');
						}
					}
				} else {
					$FREQUEST->setvalue($url_array[$this->encrypt_url($request_url_array[$i])]['key'] ,$url_array[$this->encrypt_url($request_url_array[$i])]['value'],'GET');
				}
			} else {
				$FREQUEST->setvalue($request_url_array[$i],$request_url_array[$i+1],'GET');
				$i++;
			}
		}
	}
	
 
    // Caches the URLs into a file that should be in the cache directory
    function cache_products_url() {
      global $refresh;

      if (($refresh == true) || !read_cache($url_array, 'url.products.cache')) {

        $categories_query = tep_db_query("select categories_id, categories_name from " . TABLE_CATEGORIES_DESCRIPTION);

        while ($categories = tep_db_fetch_array($categories_query)) {
          $url_array[$this->encrypt_url($categories['categories_name'])] = array('key' => 'categories_id', 'value' => $categories['categories_id']);
        }


        $manufacturers_query = tep_db_query("select manufacturers_id, manufacturers_name from " . TABLE_MANUFACTURERS);

        while ($manufacturers = tep_db_fetch_array($manufacturers_query)) {
          $url_array[$this->encrypt_url($manufacturers['manufacturers_name'])] = array('key' => 'manufacturers_id', 'value' => $manufacturers['manufacturers_id']);
        }


        $products_query = tep_db_query("select products_id, products_name from " . TABLE_PRODUCTS_DESCRIPTION . " where products_name!='' ");

        while ($products = tep_db_fetch_array($products_query)) {
          if((trim($products['products_name']))!=''){
		  		$url_array[$this->encrypt_url($products['products_name'])] = array('key' => 'products_id', 'value' => $products['products_id']);
		  }
        }

        write_cache($url_array, 'url.products.cache');
      }
      return $url_array;
    }
	

    function cache_events_url() {
      global $refresh;

      if (($refresh == true) || !read_cache($url_array, 'url.events.cache')) {

        $categories_query = tep_db_query("select events_categories_id, events_categories_name from " . TABLE_EVENTS_CATEGORIES_DESCRIPTION);

        while ($categories = tep_db_fetch_array($categories_query)) {
          $url_array[$this->encrypt_url($categories['events_categories_name'])] = array('key' => 'categories_id', 'value' => $categories['events_categories_id']);
        }


        $events_query = tep_db_query("select events_id, events_name from " . TABLE_EVENTS_DESCRIPTION);

        while ($events = tep_db_fetch_array($events_query)) {
          $url_array[$this->encrypt_url($events['events_name'])] = array('key' => 'events_id', 'value' => $events['events_id']);
        }


        write_cache($url_array, 'url.events.cache');
      }
      return $url_array;
    }
    // Caches the URLs into a file that should be in the cache directory
    function cache_subscriptions_url() {
      global $refresh;


      if (($refresh == true) || !read_cache($url_array, 'url.subscr.cache')) {

        $categories_query = tep_db_query("select subscription_categories_id, subscription_categories_name from " . TABLE_SUBSCRIPTION_CATEGORIES_DESCRIPTION);

        while ($categories = tep_db_fetch_array($categories_query)) {
          $url_array[$this->encrypt_url($categories['subscription_categories_name'])] = array('key' => 'categories_id', 'value' => $categories['subscription_categories_id']);
        }


        $subscription_query = tep_db_query("select subscription_id, subscription_name from " . TABLE_SUBSCRIPTIONS_DESCRIPTION);

        while ($subscription = tep_db_fetch_array($subscription_query)) {
          $url_array[$this->encrypt_url($subscription['subscription_name'])] = array('key' => 'subscription_id', 'value' => $subscription['subscription_id']);
        }


        write_cache($url_array, 'url.subscr.cache');
      }
      return $url_array;
    }
   // Caches the URLs into a file that should be in the cache directory
    function cache_services_url() {
      global $refresh;

      if (($refresh == true) || !read_cache($url_array, 'url.servi.cache')) {

        $categories_query = tep_db_query("select services_categories_id, services_categories_name from " . TABLE_SERVICES_CATEGORIES_DESCRIPTION);

        while ($categories = tep_db_fetch_array($categories_query)) {
          $url_array[$this->encrypt_url($categories['services_categories_name'])] = array('key' => 'categories_id', 'value' => $categories['services_categories_id']);
        }


        $service_query = tep_db_query("select sed.service_id, sed.service_name,se.service_costs from " . TABLE_SERVICES_DESCRIPTION . " sed, " . TABLE_SERVICES .  " se where se.service_id=sed.service_id");

        while ($service = tep_db_fetch_array($service_query)) {
          $url_array[$this->encrypt_url($service['service_name'] . "-" . number_format($service['service_costs'],0,'',''))] = array('key' => 'service_id', 'value' => $service['service_id']);
        }


        write_cache($url_array, 'url.servi.cache');
      }
      return $url_array;
    }
  }
?>
