[ PHPXref.com ] [ Generated: Sun Jul 20 19:13:40 2008 ] [ osCommRes 1.2.0 ]
[ Index ]     [ Variables ]     [ Functions ]     [ Classes ]     [ Constants ]     [ Statistics ]

title

Body

[close]

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

   1  <?php
   2  /*

   3  

   4    osCommerce, Open Source E-Commerce Solutions

   5    http://www.oscommerce.com

   6  

   7    Copyright (c) 2003 osCommerce

   8    

   9    osCommRes, Services Online

  10    http://www.oscommres.com

  11  

  12    Copyright (c) 2005 osCommRes

  13  

  14    Released under the GNU General Public License

  15  */
  16  ////

  17  // Stop from parsing any further PHP code

  18    function tep_exit() {
  19     tep_session_close();
  20     exit();
  21    }
  22  
  23  ////

  24  // Redirect to another page or site

  25  function tep_redirect($url) {
  26  
  27    if ( (strstr($url, "\n") != false) || (strstr($url, "\r") != false) ) {
  28  
  29      tep_redirect(tep_href_link(FILENAME_DEFAULT, '', 'NONSSL', false));
  30  
  31    }
  32      if ( (ENABLE_SSL == true) && (getenv('HTTPS') == 'on') ) { // We are loading an SSL page
  33  
  34      if (substr($url, 0, strlen(HTTP_SERVER)) == HTTP_SERVER) { // NONSSL url
  35  
  36        $url = HTTPS_SERVER . substr($url, strlen(HTTP_SERVER)); // Change it to SSL

  37  
  38      }
  39  
  40    }
  41  
  42      header('Location: ' . $url);
  43       tep_exit();
  44  }
  45  
  46  ////

  47  // Parse the data used in the html tags to ensure the tags will not break

  48    function tep_parse_input_field_data($data, $parse) {
  49      return strtr(trim($data), $parse);
  50    }
  51  
  52    function tep_output_string($string, $translate = false, $protected = false) {
  53      if ($protected == true) {
  54        return htmlspecialchars($string);
  55      } else {
  56        if ($translate == false) {
  57          return tep_parse_input_field_data($string, array('"' => '&quot;'));
  58        } else {
  59          return tep_parse_input_field_data($string, $translate);
  60        }
  61      }
  62    }
  63  
  64    function tep_output_string_protected($string) {
  65      return tep_output_string($string, false, true);
  66    }
  67  
  68    function tep_sanitize_string($string) {
  69      $string = ereg_replace(' +', ' ', trim($string));
  70  
  71      return preg_replace("/[<>]/", '_', $string);
  72    }
  73  
  74  ////

  75  // Return a random row from a database query

  76    function tep_random_select($query) {
  77      $random_product = '';
  78      $random_query = tep_db_query($query);
  79      $num_rows = tep_db_num_rows($random_query);
  80      if ($num_rows > 0) {
  81        $random_row = tep_rand(0, ($num_rows - 1));
  82        tep_db_data_seek($random_query, $random_row);
  83        $random_product = tep_db_fetch_array($random_query);
  84      }
  85  
  86      return $random_product;
  87    }
  88  
  89  ////

  90  // Return a product's name

  91  // TABLES: products

  92    function tep_get_products_name($product_id, $language = '') {
  93      global $languages_id;
  94  
  95      if (empty($language)) $language = $languages_id;
  96  
  97      $product_query = tep_db_query("select products_name from " . TABLE_PRODUCTS_DESCRIPTION . " where products_id = '" . (int)$product_id . "' and language_id = '" . (int)$language . "'");
  98      $product = tep_db_fetch_array($product_query);
  99  
 100      return $product['products_name'];
 101    }
 102  
 103  ////

 104  // Return a product's special price (returns nothing if there is no offer)

 105  // TABLES: products

 106  
 107    function tep_get_customers_groups_id() {
 108      global $customer_id;
 109      $customers_groups_query = tep_db_query("select customers_groups_id from " . TABLE_CUSTOMERS . " where customers_id =  '" . $customer_id . "'");
 110      $customers_groups_id = tep_db_fetch_array($customers_groups_query);
 111      return $customers_groups_id['customers_groups_id'];
 112    }
 113    
 114    function tep_get_products_special_price($product_id) {
 115        global $customer_id;
 116      $product_query = tep_db_query("select products_price, products_model from " . TABLE_PRODUCTS . " where products_id = '" . $product_id . "'");
 117      if (tep_db_num_rows($product_query)) {
 118        $product = tep_db_fetch_array($product_query);
 119        $product_price = $product['products_price'];
 120      } else {
 121        return false;
 122      }
 123      /*$specials_query = tep_db_query("select specials_new_products_price from " . TABLE_SPECIALS . " where products_id = '" . (int)$product_id . "' and status = '1' and customers_id = '" . $customer_id . "' and customers_groups_id = '0'");

 124      if (!tep_db_num_rows($specials_query)) {

 125        $customer_groups_id = tep_get_customers_groups_id();

 126        $specials_query = tep_db_query("select specials_new_products_price from " . TABLE_SPECIALS . " where products_id = '" . (int)$product_id . "' and status = '1' and customers_groups_id = '" . $customer_groups_id . "' and customers_id = '0'");

 127        if (!tep_db_num_rows($specials_query)) {

 128          $specials_query = tep_db_query("select specials_new_products_price from " . TABLE_SPECIALS . " where products_id = '" . (int)$product_id . "' and status = '1' and customers_groups_id = '0' and customers_id = '0'");

 129        }

 130      }*/
 131      //$specials_query = tep_db_query("select specials_new_products_price from " . TABLE_SPECIALS . " where products_id = '" . $product_id . "' and status");

 132      if (!isset($customer_id)) $customer_id = 0;
 133      $customer_groups_id = tep_get_customers_groups_id();
 134  
 135      $specials_query = tep_db_query("select specials_new_products_price,customers_id,customers_groups_id from " . TABLE_SPECIALS . " where products_id = '" . (int)$product_id . "' and status = '1'");
 136  
 137      if (tep_db_num_rows($specials_query)) {
 138        $special = tep_db_fetch_array($specials_query);
 139            if ($customer_id>0 && $customer_id==$special["customers_id"]){
 140            $special_price = $special['specials_new_products_price'];
 141          } else if ($customer_groups_id>0 && $customer_groups_id==$special["customers_groups_id"]){
 142            $special_price = $special['specials_new_products_price'];
 143          } else if ($special["customers_id"]<=0 && $special["customers_groups_id"]<=0){
 144            $special_price = $special['specials_new_products_price'];
 145          } else {
 146              $special_price=false;
 147          }
 148      } else {
 149        $special_price = false;
 150      }
 151      
 152      if(substr($product['products_model'], 0, 4) == 'GIFT') {    //Never apply a salededuction to Ian Wilson's Giftvouchers
 153        return $special_price;
 154      }
 155  
 156      $product_to_categories_query = tep_db_query("select categories_id from " . TABLE_PRODUCTS_TO_CATEGORIES . " where products_id = '" . $product_id . "'");
 157      $product_to_categories = tep_db_fetch_array($product_to_categories_query);
 158      $category = $product_to_categories['categories_id'];
 159  
 160      $sale_query = tep_db_query("select sale_specials_condition, sale_deduction_value, sale_deduction_type from " . TABLE_SALEMAKER_SALES . " where sale_categories_all like '%," . $category . ",%' and sale_status = '1' and (sale_date_start <= now() or sale_date_start = '0000-00-00') and (sale_date_end >= now() or sale_date_end = '0000-00-00') and (sale_pricerange_from <= '" . $product_price . "' or sale_pricerange_from = '0') and (sale_pricerange_to >= '" . $product_price . "' or sale_pricerange_to = '0')");
 161      if (tep_db_num_rows($sale_query)) {
 162        $sale = tep_db_fetch_array($sale_query);
 163      } else {
 164        return $special_price;
 165      }
 166  
 167      if (!$special_price) {
 168        $tmp_special_price = $product_price;
 169      } else {
 170        $tmp_special_price = $special_price;
 171      }
 172  
 173      switch ($sale['sale_deduction_type']) {
 174        case 0:
 175          $sale_product_price = $product_price - $sale['sale_deduction_value'];
 176          $sale_special_price = $tmp_special_price - $sale['sale_deduction_value'];
 177          break;
 178        case 1:
 179          $sale_product_price = $product_price - (($product_price * $sale['sale_deduction_value']) / 100);
 180          $sale_special_price = $tmp_special_price - (($tmp_special_price * $sale['sale_deduction_value']) / 100);
 181          break;
 182        case 2:
 183          $sale_product_price = $sale['sale_deduction_value'];
 184          $sale_special_price = $sale['sale_deduction_value'];
 185          break;
 186        default:
 187          return $special_price;
 188      }
 189  
 190      if ($sale_product_price < 0) {
 191        $sale_product_price = 0;
 192      }
 193  
 194      if ($sale_special_price < 0) {
 195        $sale_special_price = 0;
 196      }
 197  
 198      if (!$special_price) {
 199        return number_format($sale_product_price, 4, '.', '');
 200      } else {
 201        switch($sale['sale_specials_condition']){
 202          case 0:
 203            return number_format($sale_product_price, 4, '.', '');
 204            break;
 205          case 1:
 206            return number_format($special_price, 4, '.', '');
 207            break;
 208          case 2:
 209            return number_format($sale_special_price, 4, '.', '');
 210            break;
 211          default:
 212            return number_format($special_price, 4, '.', '');
 213        }
 214      }
 215    }
 216  
 217  
 218  ////

 219  // Return a product's stock

 220  // TABLES: products

 221    function tep_get_products_stock($products_id) {
 222      $products_id = tep_get_prid($products_id);
 223      $stock_query = tep_db_query("select products_quantity from " . TABLE_PRODUCTS . " where products_id = '" . (int)$products_id . "'");
 224      $stock_values = tep_db_fetch_array($stock_query);
 225  
 226      return $stock_values['products_quantity'];
 227    }
 228  
 229  ////

 230  // Check if the required stock is available

 231  // If insufficent stock is available return an out of stock message

 232    function tep_check_stock($products_id, $products_quantity) {
 233      $stock_left = tep_get_products_stock($products_id) - $products_quantity;
 234      $out_of_stock = '';
 235  
 236      if ($stock_left < 0) {
 237        $out_of_stock = '<span class="markProductOutOfStock">' . STOCK_MARK_PRODUCT_OUT_OF_STOCK . '</span>';
 238      }
 239  
 240      return $out_of_stock;
 241    }
 242  
 243  ////

 244  // Break a word in a string if it is longer than a specified length ($len)

 245    function tep_break_string($string, $len, $break_char = '-') {
 246      $l = 0;
 247      $output = '';
 248      for ($i=0, $n=strlen($string); $i<$n; $i++) {
 249        $char = substr($string, $i, 1);
 250        if ($char != ' ') {
 251          $l++;
 252        } else {
 253          $l = 0;
 254        }
 255        if ($l > $len) {
 256          $l = 1;
 257          $output .= $break_char;
 258        }
 259        $output .= $char;
 260      }
 261  
 262      return $output;
 263    }
 264  
 265  ////

 266  // Return all HTTP GET variables, except those passed as a parameter

 267    function tep_get_all_get_params($exclude_array = '') {
 268      global $HTTP_GET_VARS;
 269  
 270      if (!is_array($exclude_array)) $exclude_array = array();
 271  
 272      $get_url = '';
 273      if (is_array($HTTP_GET_VARS) && (sizeof($HTTP_GET_VARS) > 0)) {
 274        reset($HTTP_GET_VARS);
 275        while (list($key, $value) = each($HTTP_GET_VARS)) {
 276          if ( (strlen($value) > 0) && ($key != tep_session_name()) && ($key != 'error') && (!in_array($key, $exclude_array)) && ($key != 'x') && ($key != 'y') ) {
 277            $get_url .= $key . '=' . rawurlencode(stripslashes($value)) . '&';
 278          }
 279        }
 280      }
 281  
 282      return $get_url;
 283    }
 284  
 285  ////

 286  // Returns an array with countries

 287  // TABLES: countries

 288    function tep_get_countries($countries_id = '', $with_iso_codes = false) {
 289      $countries_array = array();
 290      if (tep_not_null($countries_id)) {
 291        if ($with_iso_codes == true) {
 292          $countries = tep_db_query("select countries_name, countries_iso_code_2, countries_iso_code_3 from " . TABLE_COUNTRIES . " where countries_id = '" . (int)$countries_id . "' order by countries_name");
 293          $countries_values = tep_db_fetch_array($countries);
 294          $countries_array = array('countries_name' => $countries_values['countries_name'],
 295                                   'countries_iso_code_2' => $countries_values['countries_iso_code_2'],
 296                                   'countries_iso_code_3' => $countries_values['countries_iso_code_3']);
 297        } else {
 298          $countries = tep_db_query("select countries_name from " . TABLE_COUNTRIES . " where countries_id = '" . (int)$countries_id . "'");
 299          $countries_values = tep_db_fetch_array($countries);
 300          $countries_array = array('countries_name' => $countries_values['countries_name']);
 301        }
 302      } else {
 303        $countries = tep_db_query("select countries_id, countries_name from " . TABLE_COUNTRIES . " order by countries_name");
 304        while ($countries_values = tep_db_fetch_array($countries)) {
 305          $countries_array[] = array('countries_id' => $countries_values['countries_id'],
 306                                     'countries_name' => $countries_values['countries_name']);
 307        }
 308      }
 309  
 310      return $countries_array;
 311    }
 312  
 313  ////

 314  // Alias function to tep_get_countries, which also returns the countries iso codes

 315    function tep_get_countries_with_iso_codes($countries_id) {
 316      return tep_get_countries($countries_id, true);
 317    }
 318  
 319  ////

 320  // Generate a path to categories

 321    function tep_get_path($current_category_id = '') {
 322      global $cPath_array;
 323  
 324      if (tep_not_null($current_category_id)) {
 325        $cp_size = sizeof($cPath_array);
 326        if ($cp_size == 0) {
 327          $cPath_new = $current_category_id;
 328        } else {
 329          $cPath_new = '';
 330          $last_category_query = tep_db_query("select parent_id from " . TABLE_CATEGORIES . " where categories_id = '" . (int)$cPath_array[($cp_size-1)] . "'");
 331          $last_category = tep_db_fetch_array($last_category_query);
 332  
 333          $current_category_query = tep_db_query("select parent_id from " . TABLE_CATEGORIES . " where categories_id = '" . (int)$current_category_id . "'");
 334          $current_category = tep_db_fetch_array($current_category_query);
 335  
 336          if ($last_category['parent_id'] == $current_category['parent_id']) {
 337            for ($i=0; $i<($cp_size-1); $i++) {
 338              $cPath_new .= '_' . $cPath_array[$i];
 339            }
 340          } else {
 341            for ($i=0; $i<$cp_size; $i++) {
 342              $cPath_new .= '_' . $cPath_array[$i];
 343            }
 344          }
 345          $cPath_new .= '_' . $current_category_id;
 346  
 347          if (substr($cPath_new, 0, 1) == '_') {
 348            $cPath_new = substr($cPath_new, 1);
 349          }
 350        }
 351      } else {
 352        $cPath_new = implode('_', $cPath_array);
 353      }
 354  
 355      return 'cPath=' . $cPath_new;
 356    }
 357  
 358  ////

 359  // Returns the clients browser

 360    function tep_browser_detect($component) {
 361      global $HTTP_USER_AGENT;
 362  
 363      return stristr($HTTP_USER_AGENT, $component);
 364    }
 365  
 366  ////

 367  // Alias function to tep_get_countries()

 368    function tep_get_country_name($country_id) {
 369      $country_array = tep_get_countries($country_id);
 370  
 371      return $country_array['countries_name'];
 372    }
 373  
 374  ////

 375  // Returns the zone (State/Province) name

 376  // TABLES: zones

 377    function tep_get_zone_name($country_id, $zone_id, $default_zone) {
 378      $zone_query = tep_db_query("select zone_name from " . TABLE_ZONES . " where zone_country_id = '" . (int)$country_id . "' and zone_id = '" . (int)$zone_id . "'");
 379      if (tep_db_num_rows($zone_query)) {
 380        $zone = tep_db_fetch_array($zone_query);
 381        return $zone['zone_name'];
 382      } else {
 383        return $default_zone;
 384      }
 385    }
 386  
 387  ////

 388  // Returns the zone (State/Province) code

 389  // TABLES: zones

 390    function tep_get_zone_code($country_id, $zone_id, $default_zone) {
 391      $zone_query = tep_db_query("select zone_code from " . TABLE_ZONES . " where zone_country_id = '" . (int)$country_id . "' and zone_id = '" . (int)$zone_id . "'");
 392      if (tep_db_num_rows($zone_query)) {
 393        $zone = tep_db_fetch_array($zone_query);
 394        return $zone['zone_code'];
 395      } else {
 396        return $default_zone;
 397      }
 398    }
 399  
 400  ////

 401  // Wrapper function for round()

 402    function tep_round($number, $precision) {
 403      if (strpos($number, '.') && (strlen(substr($number, strpos($number, '.')+1)) > $precision)) {
 404        $number = substr($number, 0, strpos($number, '.') + 1 + $precision + 1);
 405  
 406        if (substr($number, -1) >= 5) {
 407          if ($precision > 1) {
 408            $number = substr($number, 0, -1) + ('0.' . str_repeat(0, $precision-1) . '1');
 409          } elseif ($precision == 1) {
 410            $number = substr($number, 0, -1) + 0.1;
 411          } else {
 412            $number = substr($number, 0, -1) + 1;
 413          }
 414        } else {
 415          $number = substr($number, 0, -1);
 416        }
 417      }
 418  
 419      return $number;
 420    }
 421  
 422  ////

 423  // Returns the tax rate for a zone / class

 424  // TABLES: tax_rates, zones_to_geo_zones

 425    function tep_get_tax_rate($class_id, $country_id = -1, $zone_id = -1) {
 426      global $customer_zone_id, $customer_country_id;
 427  
 428      if ( ($country_id == -1) && ($zone_id == -1) ) {
 429        if (!tep_session_is_registered('customer_id')) {
 430          $country_id = STORE_COUNTRY;
 431          $zone_id = STORE_ZONE;
 432        } else {
 433          $country_id = $customer_country_id;
 434          $zone_id = $customer_zone_id;
 435        }
 436      }
 437  
 438      $tax_query = tep_db_query("select sum(tax_rate) as tax_rate from " . TABLE_TAX_RATES . " tr left join " . TABLE_ZONES_TO_GEO_ZONES . " za on (tr.tax_zone_id = za.geo_zone_id) left join " . TABLE_GEO_ZONES . " tz on (tz.geo_zone_id = tr.tax_zone_id) where (za.zone_country_id is null or za.zone_country_id = '0' or za.zone_country_id = '" . (int)$country_id . "') and (za.zone_id is null or za.zone_id = '0' or za.zone_id = '" . (int)$zone_id . "') and tr.tax_class_id = '" . (int)$class_id . "' group by tr.tax_priority");
 439      if (tep_db_num_rows($tax_query)) {
 440        $tax_multiplier = 1.0;
 441        while ($tax = tep_db_fetch_array($tax_query)) {
 442          $tax_multiplier *= 1.0 + ($tax['tax_rate'] / 100);
 443        }
 444        return ($tax_multiplier - 1.0) * 100;
 445      } else {
 446        return 0;
 447      }
 448    }
 449  
 450  ////

 451  // Return the tax description for a zone / class

 452  // TABLES: tax_rates;

 453    function tep_get_tax_description($class_id, $country_id, $zone_id) {
 454      $tax_query = tep_db_query("select tax_description from " . TABLE_TAX_RATES . " tr left join " . TABLE_ZONES_TO_GEO_ZONES . " za on (tr.tax_zone_id = za.geo_zone_id) left join " . TABLE_GEO_ZONES . " tz on (tz.geo_zone_id = tr.tax_zone_id) where (za.zone_country_id is null or za.zone_country_id = '0' or za.zone_country_id = '" . (int)$country_id . "') and (za.zone_id is null or za.zone_id = '0' or za.zone_id = '" . (int)$zone_id . "') and tr.tax_class_id = '" . (int)$class_id . "' order by tr.tax_priority");
 455      if (tep_db_num_rows($tax_query)) {
 456        $tax_description = '';
 457        while ($tax = tep_db_fetch_array($tax_query)) {
 458          $tax_description .= $tax['tax_description'] . ' + ';
 459        }
 460        $tax_description = substr($tax_description, 0, -3);
 461  
 462        return $tax_description;
 463      } else {
 464        return TEXT_UNKNOWN_TAX_RATE;
 465      }
 466    }
 4