<?php
/*
  $Id: database.php,v 1.1.1.1 2004/03/04 23:40:48 ccwjr Exp $

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

  Copyright (c) 2003 osCommerce

  Released under the GNU General Public License
*/

  function tep_db_connect($server = DB_SERVER, $username = DB_SERVER_USERNAME, $password = DB_SERVER_PASSWORD, $database = DB_DATABASE, $link = 'db_link') {
    global $$link;

    if (USE_PCONNECT == 'true') {
      $$link = mysql_pconnect($server, $username, $password);
    } else {
      $$link = mysql_connect($server, $username, $password);
    }

    if ($$link) mysql_select_db($database);

    return $$link;
  }

  function tep_db_close($link = 'db_link') {
    global $$link;

    return mysql_close($$link);
  }

  function tep_db_error($query, $errno, $error) {
    global $PHP_SELF;
    $msg = '<b><font color="#000000">Error reported on page ' . $PHP_SELF . '<br /><br />MySQL error: ' . $errno . ' - ' . $error . '<br /><br />' . $query . '<br /><br /></font><font color="#ff0000">'.TEP_DB_ERROR.'</font></b>';
    $email_address = '';
    if (defined('STORE_OWNER_EMAIL_ADDRESS')) $email_address = STORE_OWNER_EMAIL_ADDRESS;
    if (defined('TEP_STOP_NOTIFY')) $email_address = TEP_STOP_NOTIFY;
    if ($email_address != '') {
      tep_mail('Stop Notification', $email_address, 'Critical Store Error', str_replace('<br />', "\r\n", $msg), STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS);
    }
    
    die($msg);
  }

  function tep_db_query($query, $link = 'db_link') {
    global $$link;

    if (defined('STORE_DB_TRANSACTIONS') && (STORE_DB_TRANSACTIONS == 'true')) {
      error_log('QUERY ' . $query . "\n", 3, STORE_PAGE_PARSE_TIME_LOG);
    }

    $result = mysql_query($query, $$link) or tep_db_error($query, mysql_errno(), mysql_error());

    if (defined('STORE_DB_TRANSACTIONS') && (STORE_DB_TRANSACTIONS == 'true')) {
       $result_error = mysql_error();
       error_log('RESULT ' . $result . ' ' . $result_error . "\n", 3, STORE_PAGE_PARSE_TIME_LOG);
    }

    return $result;
  }

  function tep_db_perform($table, $data, $action = 'insert', $parameters = '', $link = 'db_link') {
    reset($data);
    if ($action == 'insert') {
      $query = 'insert into ' . $table . ' (';
      while (list($columns, ) = each($data)) {
        $query .= $columns . ', ';
      }
      $query = substr($query, 0, -2) . ') values (';
      reset($data);
      while (list(, $value) = each($data)) {
        switch ((string)$value) {
          case 'now()':
            $query .= 'now(), ';
            break;
          case 'null':
            $query .= 'null, ';
            break;
          default:
            $query .= '\'' . tep_db_input($value) . '\', ';
            break;
        }
      }
      $query = substr($query, 0, -2) . ')';
    } elseif ($action == 'update') {
      $query = 'update ' . $table . ' set ';
      while (list($columns, $value) = each($data)) {
        switch ((string)$value) {
          case 'now()':
            $query .= $columns . ' = now(), ';
            break;
          case 'null':
            $query .= $columns .= ' = null, ';
            break;
          default:
            $query .= $columns . ' = \'' . tep_db_input($value) . '\', ';
            break;
        }
      }
      $query = substr($query, 0, -2) . ' where ' . $parameters;
    }

    return tep_db_query($query, $link);
  }
  
  function tep_db_insert_delayed($table, $data, $parameters = '', $link = 'db_link') {
    reset($data);
    $query = 'INSERT DELAYED INTO ' . $table . ' (';
    while (list($columns, ) = each($data)) {
      $query .= $columns . ', ';
    }
    $query = substr($query, 0, -2) . ') values (';
    reset($data);
    while (list(, $value) = each($data)) {
      switch ((string)$value) {
        case 'now()':
          $query .= 'now(), ';
          break;
        case 'null':
          $query .= 'null, ';
          break;
        default:
          $query .= '\'' . tep_db_input($value) . '\', ';
          break;
      }
    }
    $query = substr($query, 0, -2) . ')';
      
    return tep_db_query($query, $link);
  }  

  function tep_db_fetch_array($db_query) {
    return mysql_fetch_array($db_query, MYSQL_ASSOC);
  }

  function tep_db_num_rows($db_query) {
    return mysql_num_rows($db_query);
  }

  function tep_db_data_seek($db_query, $row_number) {
    return mysql_data_seek($db_query, $row_number);
  }

  function tep_db_insert_id() {
    return mysql_insert_id();
  }

  function tep_db_free_result($db_query) {
    return mysql_free_result($db_query);
  }

  function tep_db_fetch_fields($db_query) {
    return mysql_fetch_field($db_query);
  }

  function tep_db_output($string) {
    return htmlspecialchars($string);
  }

  function tep_db_input($string) {
    return addslashes($string);
  }

  function tep_db_prepare_input($string) {
    if (is_string($string)) {
      return trim(tep_sanitize_string(stripslashes($string)));
    } elseif (is_array($string)) {
      reset($string);
      while (list($key, $value) = each($string)) {
        $string[$key] = tep_db_prepare_input($value);
      }
      return $string;
    } else {
      return $string;
    }
  }

  //Eversun mod for sppc and qty price breaks
 function tep_db_table_exists($table, $link = 'db_link') {
    $result = tep_db_query("show table status from `" . DB_DATABASE . "`");
    while ($list_tables = tep_db_fetch_array($result)) {
    if ($list_tables['Name'] == $table) {
      return true;
    }
    }
    return false;
  }

  function tep_db_check_age_specials_retail_table() {
    $result = tep_db_query("show table status from `" . DB_DATABASE . "`");
    $last_update_table_specials = "2000-01-01 12:00:00";
    $table_srp_exists = false;
    while ($list_tables = tep_db_fetch_array($result)) {
    if ($list_tables['Name'] == TABLE_SPECIALS_RETAIL_PRICES) {
    $table_srp_exists = true;
    $last_update_table_srp = $list_tables['Update_time'];
    }
    if ($list_tables['Name'] == TABLE_SPECIALS) {
    $last_update_table_specials = $list_tables['Update_time'];
    }
    } // end while

    if(!$table_srp_exists || ($last_update_table_specials > $last_update_table_srp)) {
      if ($table_srp_exists) {
        $query1 = "truncate " . TABLE_SPECIALS_RETAIL_PRICES . "";
        if (tep_db_query($query1)) {
          $query2 = "insert into " . TABLE_SPECIALS_RETAIL_PRICES . " select products_id, specials_new_products_price, status from " . TABLE_SPECIALS;
          $result =  tep_db_query($query2);
        }
      } else { // table specials_retail_prices does not exist
         $query1 = "create table " . TABLE_SPECIALS_RETAIL_PRICES . " (products_id int NOT NULL default '0', specials_new_products_price decimal(15,4) NOT NULL default '0.0000', status tinyint, primary key (products_id) )" ;
         $query2 = "insert into " . TABLE_SPECIALS_RETAIL_PRICES . " select products_id, specials_new_products_price, status from " . TABLE_SPECIALS;
         if( tep_db_query($query1) && tep_db_query($query2) ) {
      ; // execution succesfull
        }
       } // end else
    } // end if(!$table_srp_exists || ($last_update_table_specials....
  }

  function tep_db_decoder($string) {
    $string = str_replace('&#39;', "'", $string);
    $string = str_replace('&#39', "'", $string); //backword compatabiliy
    return $string;
  }
?>