<?php
/*
  $Id: redirect.php,v 1.1.1.1 2004/03/04 23:38:02 ccwjr Exp $

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2003 osCommerce

  Released under the GNU General Public License
*/

  switch ($_GET['action']) {
    case 'banner':
      $banner_query = tep_db_query("select banners_url from " . TABLE_BANNERS . " where banners_id = '" . (int)$_GET['goto'] . "'");
      if (tep_db_num_rows($banner_query)) {
        $banner = tep_db_fetch_array($banner_query);
        tep_update_banner_click_count($_GET['goto']);

        tep_redirect($banner['banners_url']);
      }
      break;

    case 'url':
      if (isset($_GET['goto']) && tep_not_null($_GET['goto'])) {
        // the product page does an urlencode, so it must be decode for a match to occur
        $check_query = tep_db_query("select products_url from " . TABLE_PRODUCTS_DESCRIPTION . " where products_url = '" . tep_db_input(urldecode($_GET['goto'])) . "' limit 1");
        if (tep_db_num_rows($check_query) > 0) {
          $check = tep_db_fetch_array($check_query);
          tep_redirect('http://' . $check['products_url']);
        }
      }
      break;

    case 'manufacturer':
      if (isset($_GET['manufacturers_id']) && tep_not_null($_GET['manufacturers_id'])) {
        $manufacturer_query = tep_db_query("select manufacturers_url from " . TABLE_MANUFACTURERS_INFO . " where manufacturers_id = '" . (int)$_GET['manufacturers_id'] . "' and languages_id = '" . (int)$languages_id . "'");
        if (tep_db_num_rows($manufacturer_query)) {
// url exists in selected language
          $manufacturer = tep_db_fetch_array($manufacturer_query);

          if (tep_not_null($manufacturer['manufacturers_url'])) {
            tep_db_query("update " . TABLE_MANUFACTURERS_INFO . " set url_clicked = url_clicked+1, date_last_click = now() where manufacturers_id = '" . (int)$_GET['manufacturers_id'] . "' and languages_id = '" . (int)$languages_id . "'");

            tep_redirect($manufacturer['manufacturers_url']);
          }
        } else {
// no url exists for the selected language, lets use the default language then
          $manufacturer_query = tep_db_query("select mi.languages_id, mi.manufacturers_url from " . TABLE_MANUFACTURERS_INFO . " mi, " . TABLE_LANGUAGES . " l where mi.manufacturers_id = '" . (int)$_GET['manufacturers_id'] . "' and mi.languages_id = l.languages_id and l.code = '" . DEFAULT_LANGUAGE . "'");
          if (tep_db_num_rows($manufacturer_query)) {
            $manufacturer = tep_db_fetch_array($manufacturer_query);

            if (tep_not_null($manufacturer['manufacturers_url'])) {
              tep_db_query("update " . TABLE_MANUFACTURERS_INFO . " set url_clicked = url_clicked+1, date_last_click = now() where manufacturers_id = '" . (int)$_GET['manufacturers_id'] . "' and languages_id = '" . (int)$manufacturer['languages_id'] . "'");

              tep_redirect($manufacturer['manufacturers_url']);
            }
          }
        }
      }
      break;

    case 'links':
      require(DIR_WS_FUNCTIONS . 'links.php');

      $links_query = tep_db_query("select links_url from " . TABLE_LINKS . " where links_id = '" . (int)$_GET['goto'] . "'");
      if (tep_db_num_rows($links_query)) {
        $link = tep_db_fetch_array($links_query);
        tep_update_links_click_count($_GET['goto']);

        tep_redirect($link['links_url']);
      }
      break;

    case 'article':
      if (isset($_GET['goto']) && tep_not_null($_GET['goto'])) {
        $check_query = tep_db_query("select articles_url from " . TABLE_ARTICLES_DESCRIPTION . " where articles_url = '" . tep_db_input(urldecode($_GET['goto'])) . "' limit 1");
        if (tep_db_num_rows($check_query) > 0) {
          $check = tep_db_fetch_array($check_query);
          tep_redirect('http://' . $check['articles_url']);
        }
      }
      break;
  }

?>
