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