[ Index ]

PHP Cross Reference of osCMax 2.0.4

title

Body

[close]

/includes/functions/ -> database.php (source)

   1  <?php
   2  /*

   3  $Id: database.php 14 2006-07-28 17:42:07Z user $

   4  

   5    osCMax Power E-Commerce

   6    http://oscdox.com

   7  

   8    Copyright 2006 osCMax

   9  

  10    Released under the GNU General Public License

  11  */
  12  
  13    function tep_db_connect($server = DB_SERVER, $username = DB_SERVER_USERNAME, $password = DB_SERVER_PASSWORD, $database = DB_DATABASE, $link = 'db_link') {
  14      global $$link;
  15  
  16      if (USE_PCONNECT == 'true') {
  17        $$link = mysql_pconnect($server, $username, $password);
  18      } else {
  19        $$link = mysql_connect($server, $username, $password);
  20      }
  21  
  22      if ($$link) mysql_select_db($database);
  23  
  24      return $$link;
  25    }
  26  
  27    function tep_db_close($link = 'db_link') {
  28      global $$link;
  29  
  30      return mysql_close($$link);
  31    }
  32  
  33    function tep_db_error($query, $errno, $error) { 
  34      die('<font color="#000000"><b>' . $errno . ' - ' . $error . '<br><br>' . $query . '<br><br><small><font color="#ff0000">[TEP STOP]</font></small><br><br></b></font>');
  35    }
  36  
  37    function tep_db_query($query, $link = 'db_link') {
  38  // LINE CHANGED: Added $debug

  39  global $$link, $debug;
  40  
  41      if (defined('STORE_DB_TRANSACTIONS') && (STORE_DB_TRANSACTIONS == 'true')) {
  42        error_log('QUERY ' . $query . "\n", 3, STORE_PAGE_PARSE_TIME_LOG);
  43      }
  44  
  45      $result = mysql_query($query, $$link) or tep_db_error($query, mysql_errno(), mysql_error());
  46  
  47      if (defined('STORE_DB_TRANSACTIONS') && (STORE_DB_TRANSACTIONS == 'true')) {
  48         $result_error = mysql_error();
  49         error_log('RESULT ' . $result . ' ' . $result_error . "\n", 3, STORE_PAGE_PARSE_TIME_LOG);
  50      }
  51  // LINE ADDED

  52      $debug['QUERIES'][] = $query;
  53      return $result;
  54    }
  55  
  56    function tep_db_perform($table, $data, $action = 'insert', $parameters = '', $link = 'db_link') {
  57      reset($data);
  58      if ($action == 'insert') {
  59        $query = 'insert into ' . $table . ' (';
  60        while (list($columns, ) = each($data)) {
  61          $query .= $columns . ', ';
  62        }
  63        $query = substr($query, 0, -2) . ') values (';
  64        reset($data);
  65        while (list(, $value) = each($data)) {
  66          switch ((string)$value) {
  67            case 'now()':
  68              $query .= 'now(), ';
  69              break;
  70            case 'null':
  71              $query .= 'null, ';
  72              break;
  73            default:
  74              $query .= '\'' . tep_db_input($value) . '\', ';
  75              break;
  76          }
  77        }
  78        $query = substr($query, 0, -2) . ')';
  79      } elseif ($action == 'update') {
  80        $query = 'update ' . $table . ' set ';
  81        while (list($columns, $value) = each($data)) {
  82          switch ((string)$value) {
  83            case 'now()':
  84              $query .= $columns . ' = now(), ';
  85              break;
  86            case 'null':
  87              $query .= $columns .= ' = null, ';
  88              break;
  89            default:
  90              $query .= $columns . ' = \'' . tep_db_input($value) . '\', ';
  91              break;
  92          }
  93        }
  94        $query = substr($query, 0, -2) . ' where ' . $parameters;
  95      }
  96  
  97      return tep_db_query($query, $link);
  98    }
  99  
 100    function tep_db_fetch_array($db_query) {
 101      return mysql_fetch_array($db_query, MYSQL_ASSOC);
 102    }
 103  
 104    function tep_db_num_rows($db_query) {
 105      return mysql_num_rows($db_query);
 106    }
 107  
 108    function tep_db_data_seek($db_query, $row_number) {
 109      return mysql_data_seek($db_query, $row_number);
 110    }
 111  
 112    function tep_db_insert_id($link = 'db_link') {
 113      global $$link;
 114      return mysql_insert_id($$link);
 115    }
 116  
 117    function tep_db_free_result($db_query) {
 118      return mysql_free_result($db_query);
 119    }
 120  
 121    function tep_db_fetch_fields($db_query) {
 122      return mysql_fetch_field($db_query);
 123    }
 124  
 125    function tep_db_output($string) {
 126      return htmlspecialchars($string);
 127    }
 128  
 129  // LINE CHANGED: MS2 update 501112 - Added "$link = 'db_link'"

 130    function tep_db_input($string, $link = 'db_link') {
 131  // BOF: MS2 update 501112 - Added

 132      global $$link;
 133  
 134      if (function_exists('mysql_real_escape_string')) {
 135        return mysql_real_escape_string($string, $$link);
 136      } elseif (function_exists('mysql_escape_string')) {
 137        return mysql_escape_string($string);
 138      }
 139  // EOF: MS2 update 501112 - Added

 140      return addslashes($string);
 141    }
 142  
 143    function tep_db_prepare_input($string) {
 144      if (is_string($string)) {
 145        return trim(tep_sanitize_string(stripslashes($string)));
 146      } elseif (is_array($string)) {
 147        reset($string);
 148        while (list($key, $value) = each($string)) {
 149          $string[$key] = tep_db_prepare_input($value);
 150        }
 151        return $string;
 152      } else {
 153        return $string;
 154      }
 155    }
 156   
 157  // BOF: MOD - Separate Pricing Per Customer, adapted from sample code in user comments on

 158  // http://www.php.net/manual/en/function.mysql-list-tables.php 

 159  // Wrap DB_DATABASE with Back Ticks, Fixes Hyphens in Database Name, code from

 160  // Jef Stumpf/Krumbsnatcher: http://forums.oscommerce.com/index.php?showtopic=53436&view=findpost&p=563454

 161    function tep_db_table_exists($table, $link = 'db_link') {
 162      $result = tep_db_query("show table status from `" . DB_DATABASE . "`");
 163      while ($list_tables = tep_db_fetch_array($result)) {
 164        if ($list_tables['Name'] == $table) {
 165          return true;
 166        }
 167      }
 168      return false;
 169    }
 170    
 171    function tep_db_check_age_specials_retail_table() {
 172      $result = tep_db_query("show table status from `" . DB_DATABASE . "`");
 173      $last_update_table_specials = "2000-01-01 12:00:00";
 174      $table_srp_exists = false;
 175      while ($list_tables = tep_db_fetch_array($result)) {
 176      if ($list_tables['Name'] == TABLE_SPECIALS_RETAIL_PRICES) {
 177        $table_srp_exists = true;
 178        $last_update_table_srp = $list_tables['Update_time'];
 179      }
 180      if ($list_tables['Name'] == TABLE_SPECIALS) {
 181        $last_update_table_specials = $list_tables['Update_time'];
 182      }
 183    } // end while

 184  
 185    if(!$table_srp_exists || ($last_update_table_specials > $last_update_table_srp)) {
 186      if ($table_srp_exists) { 
 187        $query1 = "truncate " . TABLE_SPECIALS_RETAIL_PRICES . "";
 188          if (tep_db_query($query1)) {
 189            $query2 = "insert into " . TABLE_SPECIALS_RETAIL_PRICES . " select s.products_id, s.specials_new_products_price, s.status, s.customers_group_id from " . TABLE_SPECIALS . " s where s.customers_group_id = '0'";
 190            $result =  tep_db_query($query2);  
 191          }
 192        } else { // table specials_retail_prices does not exist
 193          $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, customers_group_id smallint, primary key (products_id) )" ;
 194          $query2 = "insert into " . TABLE_SPECIALS_RETAIL_PRICES . " select s.products_id, s.specials_new_products_price, s.status, s.customers_group_id from " . TABLE_SPECIALS . " s where s.customers_group_id = '0'";
 195          if( tep_db_query($query1) && tep_db_query($query2) ) { 
 196            ; // execution succesfull

 197          }
 198        } // end else

 199      } // end if(!$table_srp_exists || ($last_update_table_specials....

 200    }
 201    
 202    function tep_db_check_age_products_group_prices_cg_table($customer_group_id) {
 203      $result = tep_db_query("show table status from `" . DB_DATABASE . "`");
 204      $last_update_table_pgp = strtotime('2000-01-01 12:00:00');
 205      $table_pgp_exists = false;
 206      while ($list_tables = tep_db_fetch_array($result)) {
 207      if ($list_tables['Name'] == TABLE_PRODUCTS_GROUP_PRICES.$customer_group_id) {
 208        $table_pgp_exists = true;
 209        $last_update_table_pgp = strtotime($list_tables['Update_time']);
 210      } elseif ($list_tables['Name'] == TABLE_SPECIALS ) {
 211        $last_update_table_specials = strtotime($list_tables['Update_time']);
 212      } elseif ($list_tables['Name'] == TABLE_PRODUCTS ) {
 213        $last_update_table_products = strtotime($list_tables['Update_time']);
 214      } elseif ($list_tables['Name'] == TABLE_PRODUCTS_GROUPS ) {
 215       $last_update_table_products_groups = strtotime($list_tables['Update_time']);
 216      }
 217    } // end while

 218        
 219    if ($table_pgp_exists == false) {
 220      $create_table_sql = "create table " . TABLE_PRODUCTS_GROUP_PRICES.$customer_group_id . " (products_id int NOT NULL default '0', products_price decimal(15,4) NOT NULL default '0.0000', specials_new_products_price decimal(15,4) default NULL, status tinyint, primary key (products_id) )" ;
 221      $fill_table_sql1 = "insert into " . TABLE_PRODUCTS_GROUP_PRICES.$customer_group_id ." select p.products_id, p.products_price, NULL as specials_new_products_price, NULL as status FROM " . TABLE_PRODUCTS . " p";
 222      $update_table_sql1 = "update " . TABLE_PRODUCTS_GROUP_PRICES.$customer_group_id ." ppt left join " . TABLE_PRODUCTS_GROUPS . " pg using(products_id) set ppt.products_price = pg.customers_group_price where ppt.products_id = pg.products_id and pg.customers_group_id ='" . $customer_group_id . "'";
 223      $update_table_sql2 = "update " . TABLE_PRODUCTS_GROUP_PRICES.$customer_group_id ." ppt left join " . TABLE_SPECIALS . " s using(products_id) set ppt.specials_new_products_price = s.specials_new_products_price, ppt.status = s.status where ppt.products_id = s.products_id and s.customers_group_id = '" . $customer_group_id . "'";
 224      if ( tep_db_query($create_table_sql) && tep_db_query($fill_table_sql1) && tep_db_query($update_table_sql1) && tep_db_query($update_table_sql2) ) {
 225        return true;
 226      }      
 227    } // end if ($table_pgp_exists == false)

 228     
 229    if ( ($last_update_table_pgp < $last_update_table_products && (time() - $last_update_table_products > (int)MAXIMUM_DELAY_UPDATE_PG_PRICES_TABLE * 60) ) || $last_update_table_specials > $last_update_table_pgp || $last_update_table_products_groups > $last_update_table_pgp ) { // then the table should be updated
 230      $empty_query = "truncate " . TABLE_PRODUCTS_GROUP_PRICES.$customer_group_id . "";
 231      $fill_table_sql1 = "insert into " . TABLE_PRODUCTS_GROUP_PRICES.$customer_group_id ." select p.products_id, p.products_price, NULL as specials_new_products_price, NULL as status FROM " . TABLE_PRODUCTS . " p";
 232      $update_table_sql1 = "update " . TABLE_PRODUCTS_GROUP_PRICES.$customer_group_id ." ppt left join " . TABLE_PRODUCTS_GROUPS . " pg using(products_id) set ppt.products_price = pg.customers_group_price where ppt.products_id = pg.products_id and pg.customers_group_id ='" . $customer_group_id . "'";
 233      $update_table_sql2 = "update " . TABLE_PRODUCTS_GROUP_PRICES.$customer_group_id ." ppt left join " . TABLE_SPECIALS . " s using(products_id) set ppt.specials_new_products_price = s.specials_new_products_price, ppt.status = s.status where ppt.products_id = s.products_id and s.customers_group_id = '" . $customer_group_id . "'";
 234      if ( tep_db_query($empty_query) && tep_db_query($fill_table_sql1) && tep_db_query($update_table_sql1) && tep_db_query($update_table_sql2) ) {
 235        return true;
 236      }
 237    } else { // no need to update
 238      return true;
 239    } // end checking for update

 240  }
 241  // EOF: MOD - Separate Pricing Per Customer

 242  ?>


Generated: Fri Jan 1 13:43:16 2010 Cross-referenced by PHPXref 0.7