[ Index ]
 

Code source de osCommerce 2.2ms2-060817

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

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

   1  <?php
   2  /*
   3    $Id: general.php,v 1.160 2003/07/12 08:32:47 hpdl Exp $
   4  
   5    osCommerce, Open Source E-Commerce Solutions
   6    http://www.oscommerce.com
   7  
   8    Copyright (c) 2003 osCommerce
   9  
  10    Released under the GNU General Public License
  11  */
  12  
  13  ////
  14  // Redirect to another page or site
  15    function tep_redirect($url) {
  16      global $logger;
  17  
  18      if ( (strstr($url, "\n") != false) || (strstr($url, "\r") != false) ) {
  19        tep_redirect(tep_href_link(FILENAME_DEFAULT, '', 'NONSSL', false));
  20      }
  21  
  22      header('Location: ' . $url);
  23  
  24      if (STORE_PAGE_PARSE_TIME == 'true') {
  25        if (!is_object($logger)) $logger = new logger;
  26        $logger->timer_stop();
  27      }
  28  
  29      exit;
  30    }
  31  
  32  ////
  33  // Parse the data used in the html tags to ensure the tags will not break
  34    function tep_parse_input_field_data($data, $parse) {
  35      return strtr(trim($data), $parse);
  36    }
  37  
  38    function tep_output_string($string, $translate = false, $protected = false) {
  39      if ($protected == true) {
  40        return htmlspecialchars($string);
  41      } else {
  42        if ($translate == false) {
  43          return tep_parse_input_field_data($string, array('"' => '&quot;'));
  44        } else {
  45          return tep_parse_input_field_data($string, $translate);
  46        }
  47      }
  48    }
  49  
  50    function tep_output_string_protected($string) {
  51      return tep_output_string($string, false, true);
  52    }
  53  
  54    function tep_sanitize_string($string) {
  55      $string = ereg_replace(' +', ' ', $string);
  56  
  57      return preg_replace("/[<>]/", '_', $string);
  58    }
  59  
  60    function tep_customers_name($customers_id) {
  61      $customers = tep_db_query("select customers_firstname, customers_lastname from " . TABLE_CUSTOMERS . " where customers_id = '" . (int)$customers_id . "'");
  62      $customers_values = tep_db_fetch_array($customers);
  63  
  64      return $customers_values['customers_firstname'] . ' ' . $customers_values['customers_lastname'];
  65    }
  66  
  67    function tep_get_path($current_category_id = '') {
  68      global $cPath_array;
  69  
  70      if ($current_category_id == '') {
  71        $cPath_new = implode('_', $cPath_array);
  72      } else {
  73        if (sizeof($cPath_array) == 0) {
  74          $cPath_new = $current_category_id;
  75        } else {
  76          $cPath_new = '';
  77          $last_category_query = tep_db_query("select parent_id from " . TABLE_CATEGORIES . " where categories_id = '" . (int)$cPath_array[(sizeof($cPath_array)-1)] . "'");
  78          $last_category = tep_db_fetch_array($last_category_query);
  79  
  80          $current_category_query = tep_db_query("select parent_id from " . TABLE_CATEGORIES . " where categories_id = '" . (int)$current_category_id . "'");
  81          $current_category = tep_db_fetch_array($current_category_query);
  82  
  83          if ($last_category['parent_id'] == $current_category['parent_id']) {
  84            for ($i = 0, $n = sizeof($cPath_array) - 1; $i < $n; $i++) {
  85              $cPath_new .= '_' . $cPath_array[$i];
  86            }
  87          } else {
  88            for ($i = 0, $n = sizeof($cPath_array); $i < $n; $i++) {
  89              $cPath_new .= '_' . $cPath_array[$i];
  90            }
  91          }
  92  
  93          $cPath_new .= '_' . $current_category_id;
  94  
  95          if (substr($cPath_new, 0, 1) == '_') {
  96            $cPath_new = substr($cPath_new, 1);
  97          }
  98        }
  99      }
 100  
 101      return 'cPath=' . $cPath_new;
 102    }
 103  
 104    function tep_get_all_get_params($exclude_array = '') {
 105      global $HTTP_GET_VARS;
 106  
 107      if ($exclude_array == '') $exclude_array = array();
 108  
 109      $get_url = '';
 110  
 111      reset($HTTP_GET_VARS);
 112      while (list($key, $value) = each($HTTP_GET_VARS)) {
 113        if (($key != tep_session_name()) && ($key != 'error') && (!in_array($key, $exclude_array))) $get_url .= $key . '=' . $value . '&';
 114      }
 115  
 116      return $get_url;
 117    }
 118  
 119    function tep_date_long($raw_date) {
 120      if ( ($raw_date == '0000-00-00 00:00:00') || ($raw_date == '') ) return false;
 121  
 122      $year = (int)substr($raw_date, 0, 4);
 123      $month = (int)substr($raw_date, 5, 2);
 124      $day = (int)substr($raw_date, 8, 2);
 125      $hour = (int)substr($raw_date, 11, 2);
 126      $minute = (int)substr($raw_date, 14, 2);
 127      $second = (int)substr($raw_date, 17, 2);
 128  
 129      return strftime(DATE_FORMAT_LONG, mktime($hour, $minute, $second, $month, $day, $year));
 130    }
 131  
 132  ////
 133  // Output a raw date string in the selected locale date format
 134  // $raw_date needs to be in this format: YYYY-MM-DD HH:MM:SS
 135  // NOTE: Includes a workaround for dates before 01/01/1970 that fail on windows servers
 136    function tep_date_short($raw_date) {
 137      if ( ($raw_date == '0000-00-00 00:00:00') || ($raw_date == '') ) return false;
 138  
 139      $year = substr($raw_date, 0, 4);
 140      $month = (int)substr($raw_date, 5, 2);
 141      $day = (int)substr($raw_date, 8, 2);
 142      $hour = (int)substr($raw_date, 11, 2);
 143      $minute = (int)substr($raw_date, 14, 2);
 144      $second = (int)substr($raw_date, 17, 2);
 145  
 146      if (@date('Y', mktime($hour, $minute, $second, $month, $day, $year)) == $year) {
 147        return date(DATE_FORMAT, mktime($hour, $minute, $second, $month, $day, $year));
 148      } else {
 149        return ereg_replace('2037' . '$', $year, date(DATE_FORMAT, mktime($hour, $minute, $second, $month, $day, 2037)));
 150      }
 151  
 152    }
 153  
 154    function tep_datetime_short($raw_datetime) {
 155      if ( ($raw_datetime == '0000-00-00 00:00:00') || ($raw_datetime == '') ) return false;
 156  
 157      $year = (int)substr($raw_datetime, 0, 4);
 158      $month = (int)substr($raw_datetime, 5, 2);
 159      $day = (int)substr($raw_datetime, 8, 2);
 160      $hour = (int)substr($raw_datetime, 11, 2);
 161      $minute = (int)substr($raw_datetime, 14, 2);
 162      $second = (int)substr($raw_datetime, 17, 2);
 163  
 164      return strftime(DATE_TIME_FORMAT, mktime($hour, $minute, $second, $month, $day, $year));
 165    }
 166  
 167    function tep_get_category_tree($parent_id = '0', $spacing = '', $exclude = '', $category_tree_array = '', $include_itself = false) {
 168      global $languages_id;
 169  
 170      if (!is_array($category_tree_array)) $category_tree_array = array();
 171      if ( (sizeof($category_tree_array) < 1) && ($exclude != '0') ) $category_tree_array[] = array('id' => '0', 'text' => TEXT_TOP);
 172  
 173      if ($include_itself) {
 174        $category_query = tep_db_query("select cd.categories_name from " . TABLE_CATEGORIES_DESCRIPTION . " cd where cd.language_id = '" . (int)$languages_id . "' and cd.categories_id = '" . (int)$parent_id . "'");
 175        $category = tep_db_fetch_array($category_query);
 176        $category_tree_array[] = array('id' => $parent_id, 'text' => $category['categories_name']);
 177      }
 178  
 179      $categories_query = tep_db_query("select c.categories_id, cd.categories_name, c.parent_id from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.categories_id = cd.categories_id and cd.language_id = '" . (int)$languages_id . "' and c.parent_id = '" . (int)$parent_id . "' order by c.sort_order, cd.categories_name");
 180      while ($categories = tep_db_fetch_array($categories_query)) {
 181        if ($exclude != $categories['categories_id']) $category_tree_array[] = array('id' => $categories['categories_id'], 'text' => $spacing . $categories['categories_name']);
 182        $category_tree_array = tep_get_category_tree($categories['categories_id'], $spacing . '&nbsp;&nbsp;&nbsp;', $exclude, $category_tree_array);
 183      }
 184  
 185      return $category_tree_array;
 186    }
 187  
 188    function tep_draw_products_pull_down($name, $parameters = '', $exclude = '') {
 189      global $currencies, $languages_id;
 190  
 191      if ($exclude == '') {
 192        $exclude = array();
 193      }
 194  
 195      $select_string = '<select name="' . $name . '"';
 196  
 197      if ($parameters) {
 198        $select_string .= ' ' . $parameters;
 199      }
 200  
 201      $select_string .= '>';
 202  
 203      $products_query = tep_db_query("select p.products_id, pd.products_name, p.products_price from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd where p.products_id = pd.products_id and pd.language_id = '" . (int)$languages_id . "' order by products_name");
 204      while ($products = tep_db_fetch_array($products_query)) {
 205        if (!in_array($products['products_id'], $exclude)) {
 206          $select_string .= '<option value="' . $products['products_id'] . '">' . $products['products_name'] . ' (' . $currencies->format($products['products_price']) . ')</option>';
 207        }
 208      }
 209  
 210      $select_string .= '</select>';
 211  
 212      return $select_string;
 213    }
 214  
 215    function tep_options_name($options_id) {
 216      global $languages_id;
 217  
 218      $options = tep_db_query("select products_options_name from " . TABLE_PRODUCTS_OPTIONS . " where products_options_id = '" . (int)$options_id . "' and language_id = '" . (int)$languages_id . "'");
 219      $options_values = tep_db_fetch_array($options);
 220  
 221      return $options_values['products_options_name'];
 222    }
 223  
 224    function tep_values_name($values_id) {
 225      global $languages_id;
 226  
 227      $values = tep_db_query("select products_options_values_name from " . TABLE_PRODUCTS_OPTIONS_VALUES . " where products_options_values_id = '" . (int)$values_id . "' and language_id = '" . (int)$languages_id . "'");
 228      $values_values = tep_db_fetch_array($values);
 229  
 230      return $values_values['products_options_values_name'];
 231    }
 232  
 233    function tep_info_image($image, $alt, $width = '', $height = '') {
 234      if (tep_not_null($image) && (file_exists(DIR_FS_CATALOG_IMAGES . $image)) ) {
 235        $image = tep_image(DIR_WS_CATALOG_IMAGES . $image, $alt, $width, $height);
 236      } else {
 237        $image = TEXT_IMAGE_NONEXISTENT;
 238      }
 239  
 240      return $image;
 241    }
 242  
 243    function tep_break_string($string, $len, $break_char = '-') {
 244      $l = 0;
 245      $output = '';
 246      for ($i=0, $n=strlen($string); $i<$n; $i++) {
 247        $char = substr($string, $i, 1);
 248        if ($char != ' ') {
 249          $l++;
 250        } else {
 251          $l = 0;
 252        }
 253        if ($l > $len) {
 254          $l = 1;
 255          $output .= $break_char;
 256        }
 257        $output .= $char;
 258      }
 259  
 260      return $output;
 261    }
 262  
 263    function tep_get_country_name($country_id) {
 264      $country_query = tep_db_query("select countries_name from " . TABLE_COUNTRIES . " where countries_id = '" . (int)$country_id . "'");
 265  
 266      if (!tep_db_num_rows($country_query)) {
 267        return $country_id;
 268      } else {
 269        $country = tep_db_fetch_array($country_query);
 270        return $country['countries_name'];
 271      }
 272    }
 273  
 274    function tep_get_zone_name($country_id, $zone_id, $default_zone) {
 275      $zone_query = tep_db_query("select zone_name from " . TABLE_ZONES . " where zone_country_id = '" . (int)$country_id . "' and zone_id = '" . (int)$zone_id . "'");
 276      if (tep_db_num_rows($zone_query)) {
 277        $zone = tep_db_fetch_array($zone_query);
 278        return $zone['zone_name'];
 279      } else {
 280        return $default_zone;
 281      }
 282    }
 283  
 284    function tep_not_null($value) {
 285      if (is_array($value)) {
 286        if (sizeof($value) > 0) {
 287          return true;
 288        } else {
 289          return false;
 290        }
 291      } else {
 292        if ( (is_string($value) || is_int($value)) && ($value != '') && ($value != 'NULL') && (strlen(trim($value)) > 0)) {
 293          return true;
 294        } else {
 295          return false;
 296        }
 297      }
 298    }
 299  
 300    function tep_browser_detect($component) {
 301      global $HTTP_USER_AGENT;
 302  
 303      return stristr($HTTP_USER_AGENT, $component);
 304    }
 305  
 306    function tep_tax_classes_pull_down($parameters, $selected = '') {
 307      $select_string = '<select ' . $parameters . '>';
 308      $classes_query = tep_db_query("select tax_class_id, tax_class_title from " . TABLE_TAX_CLASS . " order by tax_class_title");
 309      while ($classes = tep_db_fetch_array($classes_query)) {
 310        $select_string .= '<option value="' . $classes['tax_class_id'] . '"';
 311        if ($selected == $classes['tax_class_id']) $select_string .= ' SELECTED';
 312        $select_string .= '>' . $classes['tax_class_title'] . '</option>';
 313      }
 314      $select_string .= '</select>';
 315  
 316      return $select_string;
 317    }
 318  
 319    function tep_geo_zones_pull_down($parameters, $selected = '') {
 320      $select_string = '<select ' . $parameters . '>';
 321      $zones_query = tep_db_query("select geo_zone_id, geo_zone_name from " . TABLE_GEO_ZONES . " order by geo_zone_name");
 322      while ($zones = tep_db_fetch_array($zones_query)) {
 323        $select_string .= '<option value="' . $zones['geo_zone_id'] . '"';
 324        if ($selected == $zones['geo_zone_id']) $select_string .= ' SELECTED';
 325        $select_string .= '>' . $zones['geo_zone_name'] . '</option>';
 326      }
 327      $select_string .= '</select>';
 328  
 329      return $select_string;
 330    }
 331  
 332    function tep_get_geo_zone_name($geo_zone_id) {
 333      $zones_query = tep_db_query("select geo_zone_name from " . TABLE_GEO_ZONES . " where geo_zone_id = '" . (int)$geo_zone_id . "'");
 334  
 335      if (!tep_db_num_rows($zones_query)) {
 336        $geo_zone_name = $geo_zone_id;
 337      } else {
 338        $zones = tep_db_fetch_array($zones_query);
 339        $geo_zone_name = $zones['geo_zone_name'];
 340      }
 341  
 342      return $geo_zone_name;
 343    }
 344  
 345    function tep_address_format($address_format_id, $address, $html, $boln, $eoln) {
 346      $address_format_query = tep_db_query("select address_format as format from " . TABLE_ADDRESS_FORMAT . " where address_format_id = '" . (int)$address_format_id . "'");
 347      $address_format = tep_db_fetch_array($address_format_query);
 348  
 349      $company = tep_output_string_protected($address['company']);
 350      if (isset($address['firstname']) && tep_not_null($address['firstname'])) {
 351        $firstname = tep_output_string_protected($address['firstname']);
 352        $lastname = tep_output_string_protected($address['lastname']);
 353      } elseif (isset($address['name']) && tep_not_null($address['name'])) {
 354        $firstname = tep_output_string_protected($address['name']);
 355        $lastname = '';
 356      } else {
 357        $firstname = '';
 358        $lastname = '';
 359      }
 360      $street = tep_output_string_protected($address['street_address']);
 361      $suburb = tep_output_string_protected($address['suburb']);
 362      $city = tep_output_string_protected($address['city']);
 363      $state = tep_output_string_protected($address['state']);
 364      if (isset($address['country_id']) && tep_not_null($address['country_id'])) {
 365        $country = tep_get_country_name($address['country_id']);
 366  
 367        if (isset($address['zone_id']) && tep_not_null($address['zone_id'])) {
 368          $state = tep_get_zone_code($address['country_id'], $address['zone_id'], $state);
 369        }
 370      } elseif (isset($address['country']) && tep_not_null($address['country'])) {
 371        $country = tep_output_string_protected($address['country']);
 372      } else {
 373        $country = '';
 374      }
 375      $postcode = tep_output_string_protected($address['postcode']);
 376      $zip = $postcode;
 377  
 378      if ($html) {
 379  // HTML Mode
 380        $HR = '<hr>';
 381        $hr = '<hr>';
 382        if ( ($boln == '') && ($eoln == "\n") ) { // Values not specified, use rational defaults
 383          $CR = '<br>';
 384          $cr = '<br>';
 385          $eoln = $cr;
 386        } else { // Use values supplied
 387          $CR = $eoln . $boln;
 388          $cr = $CR;
 389        }
 390      } else {
 391  // Text Mode
 392        $CR = $eoln;
 393        $cr = $CR;
 394        $HR = '----------------------------------------';
 395        $hr = '----------------------------------------';
 396      }
 397  
 398      $statecomma = '';
 399      $streets = $street;
 400      if ($suburb != '') $streets = $street . $cr . $suburb;
 401      if ($country == '') $country = tep_output_string_protected($address['country']);
 402      if ($state != '') $statecomma = $state . ', ';
 403  
 404      $fmt = $address_format['format'];
 405      eval("\$address = \"$fmt\";");
 406  
 407      if ( (ACCOUNT_COMPANY == 'true') && (tep_not_null($company)) ) {
 408        $address = $company . $cr . $address;
 409      }
 410  
 411      return $address;
 412    }
 413  
 414    ////////////////////////////////////////////////////////////////////////////////////////////////
 415    //
 416    // Function    : tep_get_zone_code
 417    //
 418    // Arguments   : country           country code string
 419    //               zone              state/province zone_id
 420    //               def_state         default string if zone==0
 421    //
 422    // Return      : state_prov_code   state/province code
 423    //
 424    // Description : Function to retrieve the state/province code (as in FL for Florida etc)
 425    //
 426    ////////////////////////////////////////////////////////////////////////////////////////////////
 427    function tep_get_zone_code($country, $zone, $def_state) {
 428  
 429      $state_prov_query = tep_db_query("select zone_code from " . TABLE_ZONES . " where zone_country_id = '" . (int)$country . "' and zone_id = '" . (int)$zone . "'");
 430  
 431      if (!tep_db_num_rows($state_prov_query)) {
 432        $state_prov_code = $def_state;
 433      }
 434      else {
 435        $state_prov_values = tep_db_fetch_array($state_prov_query);
 436        $state_prov_code = $state_prov_values['zone_code'];
 437      }
 438      
 439      return $state_prov_code;
 440    }
 441  
 442    function tep_get_uprid($prid, $params) {
 443      $uprid = $prid;
 444      if ( (is_array($params)) && (!strstr($prid, '{')) ) {
 445        while (list($option, $value) = each($params)) {
 446          $uprid = $uprid . '{' . $option . '}' . $value;
 447        }
 448      }
 449  
 450      return $uprid;
 451    }
 452  
 453    function tep_get_prid($uprid) {
 454      $pieces = explode('{', $uprid);
 455  
 456      return $pieces[0];
 457    }
 458  
 459    function tep_get_languages() {
 460      $languages_query = tep_db_query("select languages_id, name, code, image, directory from " . TABLE_LANGUAGES . " order by sort_order");
 461      while ($languages = tep_db_fetch_array($languages_query)) {
 462        $languages_array[] = array('id' => $languages['languages_id'],
 463                                   'name' => $languages['name'],
 464                                   'code' => $languages['code'],
 465                                   'image' => $languages['image'],
 466                                   'directory' => $languages['directory']);
 467      }
 468  
 469      return $languages_array;
 470    }
 471  
 472    function tep_get_category_name($category_id, $language_id) {
 473      $category_query = tep_db_query("select categories_name from " . TABLE_CATEGORIES_DESCRIPTION . " where categories_id = '" . (int)$category_id . "' and language_id = '" . (int)$language_id . "'");
 474      $category = tep_db_fetch_array($category_query);
 475  
 476      return $category['categories_name'];
 477    }
 478  
 479    function tep_get_orders_status_name($orders_status_id, $language_id = '') {
 480      global $languages_id;
 481  
 482      if (!$language_id) $language_id = $languages_id;
 483      $orders_status_query = tep_db_query("select orders_status_name from " . TABLE_ORDERS_STATUS . " where orders_status_id = '" . (int)$orders_status_id . "' and language_id = '" . (int)$language_id . "'");
 484      $orders_status = tep_db_fetch_array($orders_status_query);
 485  
 486      return $orders_status['orders_status_name'];
 487    }
 488  
 489    function tep_get_orders_status() {
 490      global $languages_id;
 491  
 492      $orders_status_array = array();
 493      $orders_status_query = tep_db_query("select orders_status_id, orders_status_name from " . TABLE_ORDERS_STATUS . " where language_id = '" . (int)$languages_id . "' order by orders_status_id");
 494      while ($orders_status = tep_db_fetch_array($orders_status_query)) {
 495        $orders_status_array[] = array('id' => $orders_status['orders_status_id'],
 496                                       'text' => $orders_status['orders_status_name']);
 497      }
 498  
 499      return $orders_status_array;
 500    }
 501  
 502    function tep_get_products_name($product_id, $language_id = 0) {
 503      global $languages_id;
 504  
 505      if ($language_id == 0) $language_id = $languages_id;
 506      $product_query = tep_db_query("select products_name from " . TABLE_PRODUCTS_DESCRIPTION . " where products_id = '" . (int)$product_id . "' and language_id = '" . (int)$language_id . "'");
 507      $product = tep_db_fetch_array($product_query);
 508  
 509      return $product['products_name'];
 510    }
 511  
 512    function tep_get_products_description($product_id, $language_id) {
 513      $product_query = tep_db_query("select products_description from " . TABLE_PRODUCTS_DESCRIPTION . " where products_id = '" . (int)$product_id . "' and language_id = '" . (int)$language_id . "'");
 514      $product = tep_db_fetch_array($product_query);
 515  
 516      return $product['products_description'];
 517    }
 518  
 519    function tep_get_products_url($product_id, $language_id) {
 520      $product_query = tep_db_query("select products_url from " . TABLE_PRODUCTS_DESCRIPTION . " where products_id = '" . (int)$product_id . "' and language_id = '" . (int)$language_id . "'");
 521      $product = tep_db_fetch_array($product_query);
 522  
 523      return $product['products_url'];
 524    }
 525  
 526  ////
 527  // Return the manufacturers URL in the needed language
 528  // TABLES: manufacturers_info
 529    function tep_get_manufacturer_url($manufacturer_id, $language_id) {
 530      $manufacturer_query = tep_db_query("select manufacturers_url from " . TABLE_MANUFACTURERS_INFO . " where manufacturers_id = '" . (int)$manufacturer_id . "' and languages_id = '" . (int)$language_id . "'");
 531      $manufacturer = tep_db_fetch_array($manufacturer_query);
 532  
 533      return $manufacturer['manufacturers_url'];
 534    }
 535  
 536  ////
 537  // Wrapper for class_exists() function
 538  // This function is not available in all PHP versions so we test it before using it.
 539    function tep_class_exists($class_name) {
 540      if (function_exists('class_exists')) {
 541        return class_exists($class_name);
 542      } else {
 543        return true;
 544      }
 545    }
 546  
 547  ////
 548  // Count how many products exist in a category
 549  // TABLES: products, products_to_categories, categories
 550    function tep_products_in_category_count($categories_id, $include_deactivated = false) {
 551      $products_count = 0;
 552  
 553      if ($include_deactivated) {
 554        $products_query = tep_db_query("select count(*) as total from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c where p.products_id = p2c.products_id and p2c.categories_id = '" . (int)$categories_id . "'");
 555      } else {
 556        $products_query = tep_db_query("select count(*) as total from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c where p.products_id = p2c.products_id and p.products_status = '1' and p2c.categories_id = '" . (int)$categories_id . "'");
 557      }
 558  
 559      $products = tep_db_fetch_array($products_query);
 560  
 561      $products_count += $products['total'];
 562  
 563      $childs_query = tep_db_query("select categories_id from " . TABLE_CATEGORIES . " where parent_id = '" . (int)$categories_id . "'");
 564      if (tep_db_num_rows($childs_query)) {
 565        while ($childs = tep_db_fetch_array($childs_query)) {
 566          $products_count += tep_products_in_category_count($childs['categories_id'], $include_deactivated);
 567        }
 568      }
 569  
 570      return $products_count;
 571    }
 572  
 573  ////
 574  // Count how many subcategories exist in a category
 575  // TABLES: categories
 576    function tep_childs_in_category_count($categories_id) {
 577      $categories_count = 0;
 578  
 579      $categories_query = tep_db_query("select categories_id from " . TABLE_CATEGORIES . " where parent_id = '" . (int)$categories_id . "'");
 580      while ($categories = tep_db_fetch_array($categories_query)) {
 581        $categories_count++;
 582        $categories_count += tep_childs_in_category_count($categories['categories_id']);
 583      }
 584  
 585      return $categories_count;
 586    }
 587  
 588  ////
 589  // Returns an array with countries
 590  // TABLES: countries
 591    function tep_get_countries($default = '') {
 592      $countries_array = array();
 593      if ($default) {
 594        $countries_array[] = array('id' => '',
 595                                   'text' => $default);
 596      }
 597      $countries_query = tep_db_query("select countries_id, countries_name from " . TABLE_COUNTRIES . " order by countries_name");
 598      while ($countries = tep_db_fetch_array($countries_query)) {
 599        $countries_array[] = array('id' => $countries['countries_id'],
 600                                   'text' => $countries['countries_name']);
 601      }
 602  
 603      return $countries_array;
 604    }
 605  
 606  ////
 607  // return an array with country zones
 608    function tep_get_country_zones($country_id) {
 609      $zones_array = array();
 610      $zones_query = tep_db_query("select zone_id, zone_name from " . TABLE_ZONES . " where zone_country_id = '" . (int)$country_id . "' order by zone_name");
 611      while ($zones = tep_db_fetch_array($zones_query)) {
 612        $zones_array[] = array('id' => $zones['zone_id'],
 613                               'text' => $zones['zone_name']);
 614      }
 615  
 616      return $zones_array;
 617    }
 618  
 619    function tep_prepare_country_zones_pull_down($country_id = '') {
 620  // preset the width of the drop-down for Netscape
 621      $pre = '';
 622      if ( (!tep_browser_detect('MSIE')) && (tep_browser_detect('Mozilla/4')) ) {
 623        for ($i=0; $i<45; $i++) $pre .= '&nbsp;';
 624      }
 625  
 626      $zones = tep_get_country_zones($country_id);
 627  
 628      if (sizeof($zones) > 0) {
 629        $zones_select = array(array('id' => '', 'text' => PLEASE_SELECT));
 630        $zones = array_merge($zones_select, $zones);
 631      } else {
 632        $zones = array(array('id' => '', 'text' => TYPE_BELOW));
 633  // create dummy options for Netscape to preset the height of the drop-down
 634        if ( (!tep_browser_detect('MSIE')) && (tep_browser_detect('Mozilla/4')) ) {
 635          for ($i=0; $i<9; $i++) {
 636            $zones[] = array('id' => '', 'text' => $pre);
 637          }
 638        }
 639      }
 640  
 641      return $zones;
 642    }
 643  
 644  ////
 645  // Get list of address_format_id's
 646    function tep_get_address_formats() {
 647      $address_format_query = tep_db_query("select address_format_id from " . TABLE_ADDRESS_FORMAT . " order by address_format_id");
 648      $address_format_array = array();
 649      while ($address_format_values = tep_db_fetch_array($address_format_query)) {
 650        $address_format_array[] = array('id' => $address_format_values['address_format_id'],
 651                                        'text' => $address_format_values['address_format_id']);
 652      }
 653      return $address_format_array;
 654    }
 655  
 656  ////
 657  // Alias function for Store configuration values in the Administration Tool
 658    function tep_cfg_pull_down_country_list($country_id) {
 659      return tep_draw_pull_down_menu('configuration_value', tep_get_countries(), $country_id);
 660    }
 661  
 662    function tep_cfg_pull_down_zone_list($zone_id) {
 663      return tep_draw_pull_down_menu('configuration_value', tep_get_country_zones(STORE_COUNTRY), $zone_id);
 664    }
 665  
 666    function tep_cfg_pull_down_tax_classes($tax_class_id, $key = '') {
 667      $name = (($key) ? 'configuration[' . $key . ']' : 'configuration_value');
 668  
 669      $tax_class_array = array(array('id' => '0', 'text' => TEXT_NONE));
 670      $tax_class_query = tep_db_query("select tax_class_id, tax_class_title from " . TABLE_TAX_CLASS . " order by tax_class_title");
 671      while ($tax_class = tep_db_fetch_array($tax_class_query)) {
 672        $tax_class_array[] = array('id' => $tax_class['tax_class_id'],
 673                                   'text' => $tax_class['tax_class_title']);
 674      }
 675  
 676      return tep_draw_pull_down_menu($name, $tax_class_array, $tax_class_id);
 677    }
 678  
 679  ////
 680  // Function to read in text area in admin
 681   function tep_cfg_textarea($text) {
 682      return tep_draw_textarea_field('configuration_value', false, 35, 5, $text);
 683    }
 684  
 685    function tep_cfg_get_zone_name($zone_id) {
 686      $zone_query = tep_db_query("select zone_name from " . TABLE_ZONES . " where zone_id = '" . (int)$zone_id . "'");
 687  
 688      if (!tep_db_num_rows($zone_query)) {
 689        return $zone_id;
 690      } else {
 691        $zone = tep_db_fetch_array($zone_query);
 692        return $zone['zone_name'];
 693      }
 694    }
 695  
 696  ////
 697  // Sets the status of a banner
 698    function tep_set_banner_status($banners_id, $status) {
 699      if ($status == '1') {
 700        return tep_db_query("update " . TABLE_BANNERS . " set status = '1', expires_impressions = NULL, expires_date = NULL, date_status_change = NULL where banners_id = '" . $banners_id . "'");
 701      } elseif ($status == '0') {
 702        return tep_db_query("update " . TABLE_BANNERS . " set status = '0', date_status_change = now() where banners_id = '" . $banners_id . "'");
 703      } else {
 704        return -1;
 705      }
 706    }
 707  
 708  ////
 709  // Sets the status of a product
 710    function tep_set_product_status($products_id, $status) {
 711      if ($status == '1') {
 712        return tep_db_query("update " . TABLE_PRODUCTS . " set products_status = '1', products_last_modified = now() where products_id = '" . (int)$products_id . "'");
 713      } elseif ($status == '0') {
 714        return tep_db_query("update " . TABLE_PRODUCTS . " set products_status = '0', products_last_modified = now() where products_id = '" . (int)$products_id . "'");
 715      } else {
 716        return -1;
 717      }
 718    }
 719  
 720  ////
 721  // Sets the status of a product on special
 722    function tep_set_specials_status($specials_id, $status) {
 723      if ($status == '1') {
 724        return tep_db_query("update " . TABLE_SPECIALS . " set status = '1', expires_date = NULL, date_status_change = NULL where specials_id = '" . (int)$specials_id . "'");
 725      } elseif ($status == '0') {
 726        return tep_db_query("update " . TABLE_SPECIALS . " set status = '0', date_status_change = now() where specials_id = '" . (int)$specials_id . "'");
 727      } else {
 728        return -1;
 729      }
 730    }
 731  
 732  ////
 733  // Sets timeout for the current script.
 734  // Cant be used in safe mode.
 735    function tep_set_time_limit($limit) {
 736      if (!get_cfg_var('safe_mode')) {
 737        set_time_limit($limit);
 738      }
 739    }
 740  
 741  ////
 742  // Alias function for Store configuration values in the Administration Tool
 743    function tep_cfg_select_option($select_array, $key_value, $key = '') {
 744      $string = '';
 745  
 746      for ($i=0, $n=sizeof($select_array); $i<$n; $i++) {
 747        $name = ((tep_not_null($key)) ? 'configuration[' . $key . ']' : 'configuration_value');
 748  
 749        $string .= '<br><input type="radio" name="' . $name . '" value="' . $select_array[$i] . '"';
 750  
 751        if ($key_value == $select_array[$i]) $string .= ' CHECKED';
 752  
 753        $string .= '> ' . $select_array[$i];
 754      }
 755  
 756      return $string;
 757    }
 758  
 759  ////
 760  // Alias function for module configuration keys
 761    function tep_mod_select_option($select_array, $key_name, $key_value) {
 762      reset($select_array);
 763      while (list($key, $value) = each($select_array)) {
 764        if (is_int($key)) $key = $value;
 765        $string .= '<br><input type="radio" name="configuration[' . $key_name . ']" value="' . $key . '"';
 766        if ($key_value == $key) $string .= ' CHECKED';
 767        $string .= '> ' . $value;
 768      }
 769  
 770      return $string;
 771    }
 772  
 773  ////
 774  // Retreive server information
 775    function tep_get_system_information() {
 776      global $HTTP_SERVER_VARS;
 777  
 778      $db_query = tep_db_query("select now() as datetime");
 779      $db = tep_db_fetch_array($db_query);
 780  
 781      list($system, $host, $kernel) = preg_split('/[\s,]+/', @exec('uname -a'), 5);
 782  
 783      return array('date' => tep_datetime_short(date('Y-m-d H:i:s')),
 784                   'system' => $system,
 785                   'kernel' => $kernel,
 786                   'host' => $host,
 787                   'ip' => gethostbyname($host),
 788                   'uptime' => @exec('uptime'),
 789                   'http_server' => $HTTP_SERVER_VARS['SERVER_SOFTWARE'],
 790                   'php' => PHP_VERSION,
 791                   'zend' => (function_exists('zend_version') ? zend_version() : ''),
 792                   'db_server' => DB_SERVER,
 793                   'db_ip' => gethostbyname(DB_SERVER),
 794                   'db_version' => 'MySQL ' . (function_exists('mysql_get_server_info') ? mysql_get_server_info() : ''),
 795                   'db_date' => tep_datetime_short($db['datetime']));
 796    }
 797  
 798    function tep_generate_category_path($id, $from = 'category', $categories_array = '', $index = 0) {
 799      global $languages_id;
 800  
 801      if (!is_array($categories_array)) $categories_array = array();
 802  
 803      if ($from == 'product') {
 804        $categories_query = tep_db_query("select categories_id from " . TABLE_PRODUCTS_TO_CATEGORIES . " where products_id = '" . (int)$id . "'");
 805        while ($categories = tep_db_fetch_array($categories_query)) {
 806          if ($categories['categories_id'] == '0') {
 807            $categories_array[$index][] = array('id' => '0', 'text' => TEXT_TOP);
 808          } else {
 809            $category_query = tep_db_query("select cd.categories_name, c.parent_id from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.categories_id = '" . (int)$categories['categories_id'] . "' and c.categories_id = cd.categories_id and cd.language_id = '" . (int)$languages_id . "'");
 810            $category = tep_db_fetch_array($category_query);
 811            $categories_array[$index][] = array('id' => $categories['categories_id'], 'text' => $category['categories_name']);
 812            if ( (tep_not_null($category['parent_id'])) && ($category['parent_id'] != '0') ) $categories_array = tep_generate_category_path($category['parent_id'], 'category', $categories_array, $index);
 813            $categories_array[$index] = array_reverse($categories_array[$index]);
 814          }
 815          $index++;
 816        }
 817      } elseif ($from == 'category') {
 818        $category_query = tep_db_query("select cd.categories_name, c.parent_id from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.categories_id = '" . (int)$id . "' and c.categories_id = cd.categories_id and cd.language_id = '" . (int)$languages_id . "'");
 819        $category = tep_db_fetch_array($category_query);
 820        $categories_array[$index][] = array('id' => $id, 'text' => $category['categories_name']);
 821        if ( (tep_not_null($category['parent_id'])) && ($category['parent_id'] != '0') ) $categories_array = tep_generate_category_path($category['parent_id'], 'category', $categories_array, $index);
 822      }
 823  
 824      return $categories_array;
 825    }
 826  
 827    function tep_output_generated_category_path($id, $from = 'category') {
 828      $calculated_category_path_string = '';
 829      $calculated_category_path = tep_generate_category_path($id, $from);
 830      for ($i=0, $n=sizeof($calculated_category_path); $i<$n; $i++) {
 831        for ($j=0, $k=sizeof($calculated_category_path[$i]); $j<$k; $j++) {
 832          $calculated_category_path_string .= $calculated_category_path[$i][$j]['text'] . '&nbsp;&gt;&nbsp;';
 833        }
 834        $calculated_category_path_string = substr($calculated_category_path_string, 0, -16) . '<br>';
 835      }
 836      $calculated_category_path_string = substr($calculated_category_path_string, 0, -4);
 837  
 838      if (strlen($calculated_category_path_string) < 1) $calculated_category_path_string = TEXT_TOP;
 839  
 840      return $calculated_category_path_string;
 841    }
 842  
 843    function tep_get_generated_category_path_ids($id, $from = 'category') {
 844      $calculated_category_path_string = '';
 845      $calculated_category_path = tep_generate_category_path($id, $from);
 846      for ($i=0, $n=sizeof($calculated_category_path); $i<$n; $i++) {
 847        for ($j=0, $k=sizeof($calculated_category_path[$i]); $j<$k; $j++) {
 848          $calculated_category_path_string .= $calculated_category_path[$i][$j]['id'] . '_';
 849        }
 850        $calculated_category_path_string = substr($calculated_category_path_string, 0, -1) . '<br>';
 851      }
 852      $calculated_category_path_string = substr($calculated_category_path_string, 0, -4);
 853  
 854      if (strlen($calculated_category_path_string) < 1) $calculated_category_path_string = TEXT_TOP;
 855  
 856      return $calculated_category_path_string;
 857    }
 858  
 859    function tep_remove_category($category_id) {
 860      $category_image_query = tep_db_query("select categories_image from " . TABLE_CATEGORIES . " where categories_id = '" . (int)$category_id . "'");
 861      $category_image = tep_db_fetch_array($category_image_query);
 862  
 863      $duplicate_image_query = tep_db_query("select count(*) as total from " . TABLE_CATEGORIES . " where categories_image = '" . tep_db_input($category_image['categories_image']) . "'");
 864      $duplicate_image = tep_db_fetch_array($duplicate_image_query);
 865  
 866      if ($duplicate_image['total'] < 2) {
 867        if (file_exists(DIR_FS_CATALOG_IMAGES . $category_image['categories_image'])) {
 868          @unlink(DIR_FS_CATALOG_IMAGES . $category_image['categories_image']);
 869        }
 870      }
 871  
 872      tep_db_query("delete from " . TABLE_CATEGORIES . " where categories_id = '" . (int)$category_id . "'");
 873      tep_db_query("delete from " . TABLE_CATEGORIES_DESCRIPTION . " where categories_id = '" . (int)$category_id . "'");
 874      tep_db_query("delete from " . TABLE_PRODUCTS_TO_CATEGORIES . " where categories_id = '" . (int)$category_id . "'");
 875  
 876      if (USE_CACHE == 'true') {
 877        tep_reset_cache_block('categories');
 878        tep_reset_cache_block('also_purchased');
 879      }
 880    }
 881  
 882    function tep_remove_product($product_id) {
 883      $product_image_query = tep_db_query("select products_image from " . TABLE_PRODUCTS . " where products_id = '" . (int)$product_id . "'");
 884      $product_image = tep_db_fetch_array($product_image_query);
 885  
 886      $duplicate_image_query = tep_db_query("select count(*) as total from " . TABLE_PRODUCTS . " where products_image = '" . tep_db_input($product_image['products_image']) . "'");
 887      $duplicate_image = tep_db_fetch_array($duplicate_image_query);
 888  
 889      if ($duplicate_image['total'] < 2) {
 890        if (file_exists(DIR_FS_CATALOG_IMAGES . $product_image['products_image'])) {
 891          @unlink(DIR_FS_CATALOG_IMAGES . $product_image['products_image']);
 892        }
 893      }
 894  
 895      tep_db_query("delete from " . TABLE_SPECIALS . " where products_id = '" . (int)$product_id . "'");
 896      tep_db_query("delete from " . TABLE_PRODUCTS . " where products_id = '" . (int)$product_id . "'");
 897      tep_db_query("delete from " . TABLE_PRODUCTS_TO_CATEGORIES . " where products_id = '" . (int)$product_id . "'");
 898      tep_db_query("delete from " . TABLE_PRODUCTS_DESCRIPTION . " where products_id = '" . (int)$product_id . "'");
 899      tep_db_query("delete from " . TABLE_PRODUCTS_ATTRIBUTES . " where products_id = '" . (int)$product_id . "'");
 900      tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET . " where products_id = '" . (int)$product_id . "' or products_id like '" . (int)$product_id . "{%'");
 901      tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " where products_id = '" . (int)$product_id . "' or products_id like '" . (int)$product_id . "{%'");
 902  
 903      $product_reviews_query = tep_db_query("select reviews_id from " . TABLE_REVIEWS . " where products_id = '" . (int)$product_id . "'");
 904      while ($product_reviews = tep_db_fetch_array($product_reviews_query)) {
 905        tep_db_query("delete from " . TABLE_REVIEWS_DESCRIPTION . " where reviews_id = '" . (int)$product_reviews['reviews_id'] . "'");
 906      }
 907      tep_db_query("delete from " . TABLE_REVIEWS . " where products_id = '" . (int)$product_id . "'");
 908  
 909      if (USE_CACHE == 'true') {
 910        tep_reset_cache_block('categories');
 911        tep_reset_cache_block('also_purchased');
 912      }
 913    }
 914  
 915    function tep_remove_order($order_id, $restock = false) {
 916      if ($restock == 'on') {
 917        $order_query = tep_db_query("select products_id, products_quantity from " . TABLE_ORDERS_PRODUCTS . " where orders_id = '" . (int)$order_id . "'");
 918        while ($order = tep_db_fetch_array($order_query)) {
 919          tep_db_query("update " . TABLE_PRODUCTS . " set products_quantity = products_quantity + " . $order['products_quantity'] . ", products_ordered = products_ordered - " . $order['products_quantity'] . " where products_id = '" . (int)$order['products_id'] . "'");
 920        }
 921      }
 922  
 923      tep_db_query("delete from " . TABLE_ORDERS . " where orders_id = '" . (int)$order_id . "'");
 924      tep_db_query("delete from " . TABLE_ORDERS_PRODUCTS . " where orders_id = '" . (int)$order_id . "'");
 925      tep_db_query("delete from " . TABLE_ORDERS_PRODUCTS_ATTRIBUTES . " where orders_id = '" . (int)$order_id . "'");
 926      tep_db_query("delete from " . TABLE_ORDERS_STATUS_HISTORY . " where orders_id = '" . (int)$order_id . "'");
 927      tep_db_query("delete from " . TABLE_ORDERS_TOTAL . " where orders_id = '" . (int)$order_id . "'");
 928    }
 929  
 930    function tep_reset_cache_block($cache_block) {
 931      global $cache_blocks;
 932  
 933      for ($i=0, $n=sizeof($cache_blocks); $i<$n; $i++) {
 934        if ($cache_blocks[$i]['code'] == $cache_block) {
 935          if ($cache_blocks[$i]['multiple']) {
 936            if ($dir = @opendir(DIR_FS_CACHE)) {
 937              while ($cache_file = readdir($dir)) {
 938                $cached_file = $cache_blocks[$i]['file'];
 939                $languages = tep_get_languages();
 940                for ($j=0, $k=sizeof($languages); $j<$k; $j++) {
 941                  $cached_file_unlink = ereg_replace('-language', '-' . $languages[$j]['directory'], $cached_file);
 942                  if (ereg('^' . $cached_file_unlink, $cache_file)) {
 943                    @unlink(DIR_FS_CACHE . $cache_file);
 944                  }
 945                }
 946              }
 947              closedir($dir);
 948            }
 949          } else {
 950            $cached_file = $cache_blocks[$i]['file'];
 951            $languages = tep_get_languages();
 952            for ($i=0, $n=sizeof($languages); $i<$n; $i++) {
 953              $cached_file = ereg_replace('-language', '-' . $languages[$i]['directory'], $cached_file);
 954              @unlink(DIR_FS_CACHE . $cached_file);
 955            }
 956          }
 957          break;
 958        }
 959      }
 960    }
 961  
 962    function tep_get_file_permissions($mode) {
 963  // determine type
 964      if ( ($mode & 0xC000) == 0xC000) { // unix domain socket
 965        $type = 's';
 966      } elseif ( ($mode & 0x4000) == 0x4000) { // directory
 967        $type = 'd';
 968      } elseif ( ($mode & 0xA000) == 0xA000) { // symbolic link
 969        $type = 'l';
 970      } elseif ( ($mode & 0x8000) == 0x8000) { // regular file
 971        $type = '-';
 972      } elseif ( ($mode & 0x6000) == 0x6000) { //bBlock special file
 973        $type = 'b';
 974      } elseif ( ($mode & 0x2000) == 0x2000) { // character special file
 975        $type = 'c';
 976      } elseif ( ($mode & 0x1000) == 0x1000) { // named pipe
 977        $type = 'p';
 978      } else { // unknown
 979        $type = '?';
 980      }
 981  
 982  // determine permissions
 983      $owner['read']    = ($mode & 00400) ? 'r' : '-';
 984      $owner['write']   = ($mode & 00200) ? 'w' : '-';
 985      $owner['execute'] = ($mode & 00100) ? 'x' : '-';
 986      $group['read']    = ($mode & 00040) ? 'r' : '-';
 987      $group['write']   = ($mode & 00020) ? 'w' : '-';
 988      $group['execute'] = ($mode & 00010) ? 'x' : '-';
 989      $world['read']    = ($mode & 00004) ? 'r' : '-';
 990      $world['write']   = ($mode & 00002) ? 'w' : '-';
 991      $world['execute'] = ($mode & 00001) ? 'x' : '-';
 992  
 993  // adjust for SUID, SGID and sticky bit
 994      if ($mode & 0x800 ) $owner['execute'] = ($owner['execute'] == 'x') ? 's' : 'S';
 995      if ($mode & 0x400 ) $group['execute'] = ($group['execute'] == 'x') ? 's' : 'S';
 996      if ($mode & 0x200 ) $world['execute'] = ($world['execute'] == 'x') ? 't' : 'T';
 997  
 998      return $type .
 999             $owner['read'] . $owner['write'] . $owner['execute'] .
1000             $group['read'] . $group['write'] . $group['execute'] .
1001             $world['read'] . $world['write'] . $world['execute'];
1002    }
1003  
1004    function tep_remove($source) {
1005      global $messageStack, $tep_remove_error;
1006  
1007      if (isset($tep_remove_error)) $tep_remove_error = false;
1008  
1009      if (is_dir($source)) {
1010        $dir = dir($source);
1011        while ($file = $dir->read()) {
1012          if ( ($file != '.') && ($file != '..') ) {
1013            if (is_writeable($source . '/' . $file)) {
1014              tep_remove($source . '/' . $file);
1015            } else {
1016              $messageStack->add(sprintf(ERROR_FILE_NOT_REMOVEABLE, $source . '/' . $file), 'error');
1017              $tep_remove_error = true;
1018            }
1019          }
1020        }
1021        $dir->close();
1022  
1023        if (is_writeable($source)) {
1024          rmdir($source);
1025        } else {
1026          $messageStack->add(sprintf(ERROR_DIRECTORY_NOT_REMOVEABLE, $source), 'error');
1027          $tep_remove_error = true;
1028        }
1029      } else {
1030        if (is_writeable($source)) {
1031          unlink($source);
1032        } else {
1033          $messageStack->add(sprintf(ERROR_FILE_NOT_REMOVEABLE, $source), 'error');
1034          $tep_remove_error = true;
1035        }
1036      }
1037    }
1038  
1039  ////
1040  // Output the tax percentage with optional padded decimals
1041    function tep_display_tax_value($value, $padding = TAX_DECIMAL_PLACES) {
1042      if (strpos($value, '.')) {
1043        $loop = true;
1044        while ($loop) {
1045          if (substr($value, -1) == '0') {
1046            $value = substr($value, 0, -1);
1047          } else {
1048            $loop = false;
1049            if (substr($value, -1) == '.') {
1050              $value = substr($value, 0, -1);
1051            }
1052          }
1053        }
1054      }
1055  
1056      if ($padding > 0) {
1057        if ($decimal_pos = strpos($value, '.')) {
1058          $decimals = strlen(substr($value, ($decimal_pos+1)));
1059          for ($i=$decimals; $i<$padding; $i++) {
1060            $value .= '0';
1061          }
1062        } else {
1063          $value .= '.';
1064          for ($i=0; $i<$padding; $i++) {
1065            $value .= '0';
1066          }
1067        }
1068      }
1069  
1070      return $value;
1071    }
1072  
1073    function tep_mail($to_name, $to_email_address, $email_subject, $email_text, $from_email_name, $from_email_address) {
1074      if (SEND_EMAILS != 'true') return false;
1075  
1076      // Instantiate a new mail object
1077      $message = new email(array('X-Mailer: osCommerce'));
1078  
1079      // Build the text version
1080      $text = strip_tags($email_text);
1081      if (EMAIL_USE_HTML == 'true') {
1082        $message->add_html($email_text, $text);
1083      } else {
1084        $message->add_text($text);
1085      }
1086  
1087      // Send message
1088      $message->build_message();
1089      $message->send($to_name, $to_email_address, $from_email_name, $from_email_address, $email_subject);
1090    }
1091  
1092    function tep_get_tax_class_title($tax_class_id) {
1093      if ($tax_class_id == '0') {
1094        return TEXT_NONE;
1095      } else {
1096        $classes_query = tep_db_query("select tax_class_title from " . TABLE_TAX_CLASS . " where tax_class_id = '" . (int)$tax_class_id . "'");
1097        $classes = tep_db_fetch_array($classes_query);
1098  
1099        return $classes['tax_class_title'];
1100      }
1101    }
1102  
1103    function tep_banner_image_extension() {
1104      if (function_exists('imagetypes')) {
1105        if (imagetypes() & IMG_PNG) {
1106          return 'png';
1107        } elseif (imagetypes() & IMG_JPG) {
1108          return 'jpg';
1109        } elseif (imagetypes() & IMG_GIF) {
1110          return 'gif';
1111        }
1112      } elseif (function_exists('imagecreatefrompng') && function_exists('imagepng')) {
1113        return 'png';
1114      } elseif (function_exists('imagecreatefromjpeg') && function_exists('imagejpeg')) {
1115        return 'jpg';
1116      } elseif (function_exists('imagecreatefromgif') && function_exists('imagegif')) {
1117        return 'gif';
1118      }
1119  
1120      return false;
1121    }
1122  
1123  ////
1124  // Wrapper function for round() for php3 compatibility
1125    function tep_round($value, $precision) {
1126      if (PHP_VERSION < 4) {
1127        $exp = pow(10, $precision);
1128        return round($value * $exp) / $exp;
1129      } else {
1130        return round($value, $precision);
1131      }
1132    }
1133  
1134  ////
1135  // Add tax to a products price
1136    function tep_add_tax($price, $tax) {
1137      global $currencies;
1138  
1139      if (DISPLAY_PRICE_WITH_TAX == 'true') {
1140        return tep_round($price, $currencies->currencies[DEFAULT_CURRENCY]['decimal_places']) + tep_calculate_tax($price, $tax);
1141      } else {
1142        return tep_round($price, $currencies->currencies[DEFAULT_CURRENCY]['decimal_places']);
1143      }
1144    }
1145  
1146  // Calculates Tax rounding the result
1147    function tep_calculate_tax($price, $tax) {
1148      global $currencies;
1149  
1150      return tep_round($price * $tax / 100, $currencies->currencies[DEFAULT_CURRENCY]['decimal_places']);
1151    }
1152  
1153  ////
1154  // Returns the tax rate for a zone / class
1155  // TABLES: tax_rates, zones_to_geo_zones
1156    function tep_get_tax_rate($class_id, $country_id = -1, $zone_id = -1) {
1157      global $customer_zone_id, $customer_country_id;
1158  
1159      if ( ($country_id == -1) && ($zone_id == -1) ) {
1160        if (!tep_session_is_registered('customer_id')) {
1161          $country_id = STORE_COUNTRY;
1162          $zone_id = STORE_ZONE;
1163        } else {
1164          $country_id = $customer_country_id;
1165          $zone_id = $customer_zone_id;
1166        }
1167      }
1168  
1169      $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");
1170      if (tep_db_num_rows($tax_query)) {
1171        $tax_multiplier = 0;
1172        while ($tax = tep_db_fetch_array($tax_query)) {
1173          $tax_multiplier += $tax['tax_rate'];
1174        }
1175        return $tax_multiplier;
1176      } else {
1177        return 0;
1178      }
1179    }
1180  
1181  ////
1182  // Returns the tax rate for a tax class
1183  // TABLES: tax_rates
1184    function tep_get_tax_rate_value($class_id) {
1185      $tax_query = tep_db_query("select SUM(tax_rate) as tax_rate from " . TABLE_TAX_RATES . " where tax_class_id = '" . (int)$class_id . "' group by tax_priority");
1186      if (tep_db_num_rows($tax_query)) {
1187        $tax_multiplier = 0;
1188        while ($tax = tep_db_fetch_array($tax_query)) {
1189          $tax_multiplier += $tax['tax_rate'];
1190        }
1191        return $tax_multiplier;
1192      } else {
1193        return 0;
1194      }
1195    }
1196  
1197    function tep_call_function($function, $parameter, $object = '') {
1198      if ($object == '') {
1199        return call_user_func($function, $parameter);
1200      } elseif (PHP_VERSION < 4) {
1201        return call_user_method($function, $object, $parameter);
1202      } else {
1203        return call_user_func(array($object, $function), $parameter);
1204      }
1205    }
1206  
1207    function tep_get_zone_class_title($zone_class_id) {
1208      if ($zone_class_id == '0') {
1209        return TEXT_NONE;
1210      } else {
1211        $classes_query = tep_db_query("select geo_zone_name from " . TABLE_GEO_ZONES . " where geo_zone_id = '" . (int)$zone_class_id . "'");
1212        $classes = tep_db_fetch_array($classes_query);
1213  
1214        return $classes['geo_zone_name'];
1215      }
1216    }
1217  
1218    function tep_cfg_pull_down_zone_classes($zone_class_id, $key = '') {
1219      $name = (($key) ? 'configuration[' . $key . ']' : 'configuration_value');
1220  
1221      $zone_class_array = array(array('id' => '0', 'text' => TEXT_NONE));
1222      $zone_class_query = tep_db_query("select geo_zone_id, geo_zone_name from " . TABLE_GEO_ZONES . " order by geo_zone_name");
1223      while ($zone_class = tep_db_fetch_array($zone_class_query)) {
1224        $zone_class_array[] = array('id' => $zone_class['geo_zone_id'],
1225                                    'text' => $zone_class['geo_zone_name']);
1226      }
1227  
1228      return tep_draw_pull_down_menu($name, $zone_class_array, $zone_class_id);
1229    }
1230  
1231    function tep_cfg_pull_down_order_statuses($order_status_id, $key = '') {
1232      global $languages_id;
1233  
1234      $name = (($key) ? 'configuration[' . $key . ']' : 'configuration_value');
1235  
1236      $statuses_array = array(array('id' => '0', 'text' => TEXT_DEFAULT));
1237      $statuses_query = tep_db_query("select orders_status_id, orders_status_name from " . TABLE_ORDERS_STATUS . " where language_id = '" . (int)$languages_id . "' order by orders_status_name");
1238      while ($statuses = tep_db_fetch_array($statuses_query)) {
1239        $statuses_array[] = array('id' => $statuses['orders_status_id'],
1240                                  'text' => $statuses['orders_status_name']);
1241      }
1242  
1243      return tep_draw_pull_down_menu($name, $statuses_array, $order_status_id);
1244    }
1245  
1246    function tep_get_order_status_name($order_status_id, $language_id = '') {
1247      global $languages_id;
1248  
1249      if ($order_status_id < 1) return TEXT_DEFAULT;
1250  
1251      if (!is_numeric($language_id)) $language_id = $languages_id;
1252  
1253      $status_query = tep_db_query("select orders_status_name from " . TABLE_ORDERS_STATUS . " where orders_status_id = '" . (int)$order_status_id . "' and language_id = '" . (int)$language_id . "'");
1254      $status = tep_db_fetch_array($status_query);
1255  
1256      return $status['orders_status_name'];
1257    }
1258  
1259  ////
1260  // Return a random value
1261    function tep_rand($min = null, $max = null) {
1262      static $seeded;
1263  
1264      if (!$seeded) {
1265        mt_srand((double)microtime()*1000000);
1266        $seeded = true;
1267      }
1268  
1269      if (isset($min) && isset($max)) {
1270        if ($min >= $max) {
1271          return $min;
1272        } else {
1273          return mt_rand($min, $max);
1274        }
1275      } else {
1276        return mt_rand();
1277      }
1278    }
1279  
1280  // nl2br() prior PHP 4.2.0 did not convert linefeeds on all OSs (it only converted \n)
1281    function tep_convert_linefeeds($from, $to, $string) {
1282      if ((PHP_VERSION < "4.0.5") && is_array($from)) {
1283        return ereg_replace('(' . implode('|', $from) . ')', $to, $string);
1284      } else {
1285        return str_replace($from, $to, $string);
1286      }
1287    }
1288  
1289    function tep_string_to_int($string) {
1290      return (int)$string;
1291    }
1292  
1293  ////
1294  // Parse and secure the cPath parameter values
1295    function tep_parse_category_path($cPath) {
1296  // make sure the category IDs are integers
1297      $cPath_array = array_map('tep_string_to_int', explode('_', $cPath));
1298  
1299  // make sure no duplicate category IDs exist which could lock the server in a loop
1300      $tmp_array = array();
1301      $n = sizeof($cPath_array);
1302      for ($i=0; $i<$n; $i++) {
1303        if (!in_array($cPath_array[$i], $tmp_array)) {
1304          $tmp_array[] = $cPath_array[$i];
1305        }
1306      }
1307  
1308      return $tmp_array;
1309    }
1310  ?>


Généré le : Mon Nov 26 19:48:25 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics