| [ Index ] |
|
Code source de osCommerce 2.2ms2-060817 |
1 <?php 2 /* 3 $Id: general.php,v 1.231 2003/07/09 01:15:48 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 // Stop from parsing any further PHP code 15 function tep_exit() { 16 tep_session_close(); 17 exit(); 18 } 19 20 //// 21 // Redirect to another page or site 22 function tep_redirect($url) { 23 if ( (strstr($url, "\n") != false) || (strstr($url, "\r") != false) ) { 24 tep_redirect(tep_href_link(FILENAME_DEFAULT, '', 'NONSSL', false)); 25 } 26 27 if ( (ENABLE_SSL == true) && (getenv('HTTPS') == 'on') ) { // We are loading an SSL page 28 if (substr($url, 0, strlen(HTTP_SERVER)) == HTTP_SERVER) { // NONSSL url 29 $url = HTTPS_SERVER . substr($url, strlen(HTTP_SERVER)); // Change it to SSL 30 } 31 } 32 33 header('Location: ' . $url); 34 35 tep_exit(); 36 } 37 38 //// 39 // Parse the data used in the html tags to ensure the tags will not break 40 function tep_parse_input_field_data($data, $parse) { 41 return strtr(trim($data), $parse); 42 } 43 44 function tep_output_string($string, $translate = false, $protected = false) { 45 if ($protected == true) { 46 return htmlspecialchars($string); 47 } else { 48 if ($translate == false) { 49 return tep_parse_input_field_data($string, array('"' => '"')); 50 } else { 51 return tep_parse_input_field_data($string, $translate); 52 } 53 } 54 } 55 56 function tep_output_string_protected($string) { 57 return tep_output_string($string, false, true); 58 } 59 60 function tep_sanitize_string($string) { 61 $string = ereg_replace(' +', ' ', trim($string)); 62 63 return preg_replace("/[<>]/", '_', $string); 64 } 65 66 //// 67 // Return a random row from a database query 68 function tep_random_select($query) { 69 $random_product = ''; 70 $random_query = tep_db_query($query); 71 $num_rows = tep_db_num_rows($random_query); 72 if ($num_rows > 0) { 73 $random_row = tep_rand(0, ($num_rows - 1)); 74 tep_db_data_seek($random_query, $random_row); 75 $random_product = tep_db_fetch_array($random_query); 76 } 77 78 return $random_product; 79 } 80 81 //// 82 // Return a product's name 83 // TABLES: products 84 function tep_get_products_name($product_id, $language = '') { 85 global $languages_id; 86 87 if (empty($language)) $language = $languages_id; 88 89 $product_query = tep_db_query("select products_name from " . TABLE_PRODUCTS_DESCRIPTION . " where products_id = '" . (int)$product_id . "' and language_id = '" . (int)$language . "'"); 90 $product = tep_db_fetch_array($product_query); 91 92 return $product['products_name']; 93 } 94 95 //// 96 // Return a product's special price (returns nothing if there is no offer) 97 // TABLES: products 98 function tep_get_products_special_price($product_id) { 99 $product_query = tep_db_query("select specials_new_products_price from " . TABLE_SPECIALS . " where products_id = '" . (int)$product_id . "' and status"); 100 $product = tep_db_fetch_array($product_query); 101 102 return $product['specials_new_products_price']; 103 } 104 105 //// 106 // Return a product's stock 107 // TABLES: products 108 function tep_get_products_stock($products_id) { 109 $products_id = tep_get_prid($products_id); 110 $stock_query = tep_db_query("select products_quantity from " . TABLE_PRODUCTS . " where products_id = '" . (int)$products_id . "'"); 111 $stock_values = tep_db_fetch_array($stock_query); 112 113 return $stock_values['products_quantity']; 114 } 115 116 //// 117 // Check if the required stock is available 118 // If insufficent stock is available return an out of stock message 119 function tep_check_stock($products_id, $products_quantity) { 120 $stock_left = tep_get_products_stock($products_id) - $products_quantity; 121 $out_of_stock = ''; 122 123 if ($stock_left < 0) { 124 $out_of_stock = '<span class="markProductOutOfStock">' . STOCK_MARK_PRODUCT_OUT_OF_STOCK . '</span>'; 125 } 126 127 return $out_of_stock; 128 } 129 130 //// 131 // Break a word in a string if it is longer than a specified length ($len) 132 function tep_break_string($string, $len, $break_char = '-') { 133 $l = 0; 134 $output = ''; 135 for ($i=0, $n=strlen($string); $i<$n; $i++) { 136 $char = substr($string, $i, 1); 137 if ($char != ' ') { 138 $l++; 139 } else { 140 $l = 0; 141 } 142 if ($l > $len) { 143 $l = 1; 144 $output .= $break_char; 145 } 146 $output .= $char; 147 } 148 149 return $output; 150 } 151 152 //// 153 // Return all HTTP GET variables, except those passed as a parameter 154 function tep_get_all_get_params($exclude_array = '') { 155 global $HTTP_GET_VARS; 156 157 if (!is_array($exclude_array)) $exclude_array = array(); 158 159 $get_url = ''; 160 if (is_array($HTTP_GET_VARS) && (sizeof($HTTP_GET_VARS) > 0)) { 161 reset($HTTP_GET_VARS); 162 while (list($key, $value) = each($HTTP_GET_VARS)) { 163 if ( (strlen($value) > 0) && ($key != tep_session_name()) && ($key != 'error') && (!in_array($key, $exclude_array)) && ($key != 'x') && ($key != 'y') ) { 164 $get_url .= $key . '=' . rawurlencode(stripslashes($value)) . '&'; 165 } 166 } 167 } 168 169 return $get_url; 170 } 171 172 //// 173 // Returns an array with countries 174 // TABLES: countries 175 function tep_get_countries($countries_id = '', $with_iso_codes = false) { 176 $countries_array = array(); 177 if (tep_not_null($countries_id)) { 178 if ($with_iso_codes == true) { 179 $countries = tep_db_query("select countries_name, countries_iso_code_2, countries_iso_code_3 from " . TABLE_COUNTRIES . " where countries_id = '" . (int)$countries_id . "' order by countries_name"); 180 $countries_values = tep_db_fetch_array($countries); 181 $countries_array = array('countries_name' => $countries_values['countries_name'], 182 'countries_iso_code_2' => $countries_values['countries_iso_code_2'], 183 'countries_iso_code_3' => $countries_values['countries_iso_code_3']); 184 } else { 185 $countries = tep_db_query("select countries_name from " . TABLE_COUNTRIES . " where countries_id = '" . (int)$countries_id . "'"); 186 $countries_values = tep_db_fetch_array($countries); 187 $countries_array = array('countries_name' => $countries_values['countries_name']); 188 } 189 } else { 190 $countries = tep_db_query("select countries_id, countries_name from " . TABLE_COUNTRIES . " order by countries_name"); 191 while ($countries_values = tep_db_fetch_array($countries)) { 192 $countries_array[] = array('countries_id' => $countries_values['countries_id'], 193 'countries_name' => $countries_values['countries_name']); 194 } 195 } 196 197 return $countries_array; 198 } 199 200 //// 201 // Alias function to tep_get_countries, which also returns the countries iso codes 202 function tep_get_countries_with_iso_codes($countries_id) { 203 return tep_get_countries($countries_id, true); 204 } 205 206 //// 207 // Generate a path to categories 208 function tep_get_path($current_category_id = '') { 209 global $cPath_array; 210 211 if (tep_not_null($current_category_id)) { 212 $cp_size = sizeof($cPath_array); 213 if ($cp_size == 0) { 214 $cPath_new = $current_category_id; 215 } else { 216 $cPath_new = ''; 217 $last_category_query = tep_db_query("select parent_id from " . TABLE_CATEGORIES . " where categories_id = '" . (int)$cPath_array[($cp_size-1)] . "'"); 218 $last_category = tep_db_fetch_array($last_category_query); 219 220 $current_category_query = tep_db_query("select parent_id from " . TABLE_CATEGORIES . " where categories_id = '" . (int)$current_category_id . "'"); 221 $current_category = tep_db_fetch_array($current_category_query); 222 223 if ($last_category['parent_id'] == $current_category['parent_id']) { 224 for ($i=0; $i<($cp_size-1); $i++) { 225 $cPath_new .= '_' . $cPath_array[$i]; 226 } 227 } else { 228 for ($i=0; $i<$cp_size; $i++) { 229 $cPath_new .= '_' . $cPath_array[$i]; 230 } 231 } 232 $cPath_new .= '_' . $current_category_id; 233 234 if (substr($cPath_new, 0, 1) == '_') { 235 $cPath_new = substr($cPath_new, 1); 236 } 237 } 238 } else { 239 $cPath_new = implode('_', $cPath_array); 240 } 241 242 return 'cPath=' . $cPath_new; 243 } 244 245 //// 246 // Returns the clients browser 247 function tep_browser_detect($component) { 248 global $HTTP_USER_AGENT; 249 250 return stristr($HTTP_USER_AGENT, $component); 251 } 252 253 //// 254 // Alias function to tep_get_countries() 255 function tep_get_country_name($country_id) { 256 $country_array = tep_get_countries($country_id); 257 258 return $country_array['countries_name']; 259 } 260 261 //// 262 // Returns the zone (State/Province) name 263 // TABLES: zones 264 function tep_get_zone_name($country_id, $zone_id, $default_zone) { 265 $zone_query = tep_db_query("select zone_name from " . TABLE_ZONES . " where zone_country_id = '" . (int)$country_id . "' and zone_id = '" . (int)$zone_id . "'"); 266 if (tep_db_num_rows($zone_query)) { 267 $zone = tep_db_fetch_array($zone_query); 268 return $zone['zone_name']; 269 } else { 270 return $default_zone; 271 } 272 } 273 274 //// 275 // Returns the zone (State/Province) code 276 // TABLES: zones 277 function tep_get_zone_code($country_id, $zone_id, $default_zone) { 278 $zone_query = tep_db_query("select zone_code from " . TABLE_ZONES . " where zone_country_id = '" . (int)$country_id . "' and zone_id = '" . (int)$zone_id . "'"); 279 if (tep_db_num_rows($zone_query)) { 280 $zone = tep_db_fetch_array($zone_query); 281 return $zone['zone_code']; 282 } else { 283 return $default_zone; 284 } 285 } 286 287 //// 288 // Wrapper function for round() 289 function tep_round($number, $precision) { 290 if (strpos($number, '.') && (strlen(substr($number, strpos($number, '.')+1)) > $precision)) { 291 $number = substr($number, 0, strpos($number, '.') + 1 + $precision + 1); 292 293 if (substr($number, -1) >= 5) { 294 if ($precision > 1) { 295 $number = substr($number, 0, -1) + ('0.' . str_repeat(0, $precision-1) . '1'); 296 } elseif ($precision == 1) { 297 $number = substr($number, 0, -1) + 0.1; 298 } else { 299 $number = substr($number, 0, -1) + 1; 300 } 301 } else { 302 $number = substr($number, 0, -1); 303 } 304 } 305 306 return $number; 307 } 308 309 //// 310 // Returns the tax rate for a zone / class 311 // TABLES: tax_rates, zones_to_geo_zones 312 function tep_get_tax_rate($class_id, $country_id = -1, $zone_id = -1) { 313 global $customer_zone_id, $customer_country_id; 314 315 if ( ($country_id == -1) && ($zone_id == -1) ) { 316 if (!tep_session_is_registered('customer_id')) { 317 $country_id = STORE_COUNTRY; 318 $zone_id = STORE_ZONE; 319 } else { 320 $country_id = $customer_country_id; 321 $zone_id = $customer_zone_id; 322 } 323 } 324 325 $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"); 326 if (tep_db_num_rows($tax_query)) { 327 $tax_multiplier = 1.0; 328 while ($tax = tep_db_fetch_array($tax_query)) { 329 $tax_multiplier *= 1.0 + ($tax['tax_rate'] / 100); 330 } 331 return ($tax_multiplier - 1.0) * 100; 332 } else { 333 return 0; 334 } 335 } 336 337 //// 338 // Return the tax description for a zone / class 339 // TABLES: tax_rates; 340 function tep_get_tax_description($class_id, $country_id, $zone_id) { 341 $tax_query = tep_db_query("select tax_description from " . TABLE_TAX_RATES . " tr left join " . TABLE_ZONES_TO_GEO_ZONES . " za on (tr.tax_zone_id = za.geo_zone_id) left join " . TABLE_GEO_ZONES . " tz on (tz.geo_zone_id = tr.tax_zone_id) where (za.zone_country_id is null or za.zone_country_id = '0' or za.zone_country_id = '" . (int)$country_id . "') and (za.zone_id is null or za.zone_id = '0' or za.zone_id = '" . (int)$zone_id . "') and tr.tax_class_id = '" . (int)$class_id . "' order by tr.tax_priority"); 342 if (tep_db_num_rows($tax_query)) { 343 $tax_description = ''; 344 while ($tax = tep_db_fetch_array($tax_query)) { 345 $tax_description .= $tax['tax_description'] . ' + '; 346 } 347 $tax_description = substr($tax_description, 0, -3); 348 349 return $tax_description; 350 } else { 351 return TEXT_UNKNOWN_TAX_RATE; 352 } 353 } 354 355 //// 356 // Add tax to a products price 357 function tep_add_tax($price, $tax) { 358 global $currencies; 359 360 if ( (DISPLAY_PRICE_WITH_TAX == 'true') && ($tax > 0) ) { 361 return tep_round($price, $currencies->currencies[DEFAULT_CURRENCY]['decimal_places']) + tep_calculate_tax($price, $tax); 362 } else { 363 return tep_round($price, $currencies->currencies[DEFAULT_CURRENCY]['decimal_places']); 364 } 365 } 366 367 // Calculates Tax rounding the result 368 function tep_calculate_tax($price, $tax) { 369 global $currencies; 370 371 return tep_round($price * $tax / 100, $currencies->currencies[DEFAULT_CURRENCY]['decimal_places']); 372 } 373 374 //// 375 // Return the number of products in a category 376 // TABLES: products, products_to_categories, categories 377 function tep_count_products_in_category($category_id, $include_inactive = false) { 378 $products_count = 0; 379 if ($include_inactive == true) { 380 $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)$category_id . "'"); 381 } else { 382 $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)$category_id . "'"); 383 } 384 $products = tep_db_fetch_array($products_query); 385 $products_count += $products['total']; 386 387 $child_categories_query = tep_db_query("select categories_id from " . TABLE_CATEGORIES . " where parent_id = '" . (int)$category_id . "'"); 388 if (tep_db_num_rows($child_categories_query)) { 389 while ($child_categories = tep_db_fetch_array($child_categories_query)) { 390 $products_count += tep_count_products_in_category($child_categories['categories_id'], $include_inactive); 391 } 392 } 393 394 return $products_count; 395 } 396 397 //// 398 // Return true if the category has subcategories 399 // TABLES: categories 400 function tep_has_category_subcategories($category_id) { 401 $child_category_query = tep_db_query("select count(*) as count from " . TABLE_CATEGORIES . " where parent_id = '" . (int)$category_id . "'"); 402 $child_category = tep_db_fetch_array($child_category_query); 403 404 if ($child_category['count'] > 0) { 405 return true; 406 } else { 407 return false; 408 } 409 } 410 411 //// 412 // Returns the address_format_id for the given country 413 // TABLES: countries; 414 function tep_get_address_format_id($country_id) { 415 $address_format_query = tep_db_query("select address_format_id as format_id from " . TABLE_COUNTRIES . " where countries_id = '" . (int)$country_id . "'"); 416 if (tep_db_num_rows($address_format_query)) { 417 $address_format = tep_db_fetch_array($address_format_query); 418 return $address_format['format_id']; 419 } else { 420 return '1'; 421 } 422 } 423 424 //// 425 // Return a formatted address 426 // TABLES: address_format 427 function tep_address_format($address_format_id, $address, $html, $boln, $eoln) { 428 $address_format_query = tep_db_query("select address_format as format from " . TABLE_ADDRESS_FORMAT . " where address_format_id = '" . (int)$address_format_id . "'"); 429 $address_format = tep_db_fetch_array($address_format_query); 430 431 $company = tep_output_string_protected($address['company']); 432 if (isset($address['firstname']) && tep_not_null($address['firstname'])) { 433 $firstname = tep_output_string_protected($address['firstname']); 434 $lastname = tep_output_string_protected($address['lastname']); 435 } elseif (isset($address['name']) && tep_not_null($address['name'])) { 436 $firstname = tep_output_string_protected($address['name']); 437 $lastname = ''; 438 } else { 439 $firstname = ''; 440 $lastname = ''; 441 } 442 $street = tep_output_string_protected($address['street_address']); 443 $suburb = tep_output_string_protected($address['suburb']); 444 $city = tep_output_string_protected($address['city']); 445 $state = tep_output_string_protected($address['state']); 446 if (isset($address['country_id']) && tep_not_null($address['country_id'])) { 447 $country = tep_get_country_name($address['country_id']); 448 449 if (isset($address['zone_id']) && tep_not_null($address['zone_id'])) { 450 $state = tep_get_zone_code($address['country_id'], $address['zone_id'], $state); 451 } 452 } elseif (isset($address['country']) && tep_not_null($address['country'])) { 453 $country = tep_output_string_protected($address['country']['title']); 454 } else { 455 $country = ''; 456 } 457 $postcode = tep_output_string_protected($address['postcode']); 458 $zip = $postcode; 459 460 if ($html) { 461 // HTML Mode 462 $HR = '<hr>'; 463 $hr = '<hr>'; 464 if ( ($boln == '') && ($eoln == "\n") ) { // Values not specified, use rational defaults 465 $CR = '<br>'; 466 $cr = '<br>'; 467 $eoln = $cr; 468 } else { // Use values supplied 469 $CR = $eoln . $boln; 470 $cr = $CR; 471 } 472 } else { 473 // Text Mode 474 $CR = $eoln; 475 $cr = $CR; 476 $HR = '----------------------------------------'; 477 $hr = '----------------------------------------'; 478 } 479 480 $statecomma = ''; 481 $streets = $street; 482 if ($suburb != '') $streets = $street . $cr . $suburb; 483 if ($state != '') $statecomma = $state . ', '; 484 485 $fmt = $address_format['format']; 486 eval("\$address = \"$fmt\";"); 487 488 if ( (ACCOUNT_COMPANY == 'true') && (tep_not_null($company)) ) { 489 $address = $company . $cr . $address; 490 } 491 492 return $address; 493 } 494 495 //// 496 // Return a formatted address 497 // TABLES: customers, address_book 498 function tep_address_label($customers_id, $address_id = 1, $html = false, $boln = '', $eoln = "\n") { 499 $address_query = tep_db_query("select entry_firstname as firstname, entry_lastname as lastname, entry_company as company, entry_street_address as street_address, entry_suburb as suburb, entry_city as city, entry_postcode as postcode, entry_state as state, entry_zone_id as zone_id, entry_country_id as country_id from " . TABLE_ADDRESS_BOOK . " where customers_id = '" . (int)$customers_id . "' and address_book_id = '" . (int)$address_id . "'"); 500 $address = tep_db_fetch_array($address_query); 501 502 $format_id = tep_get_address_format_id($address['country_id']); 503 504 return tep_address_format($format_id, $address, $html, $boln, $eoln); 505 } 506 507 function tep_row_number_format($number) { 508 if ( ($number < 10) && (substr($number, 0, 1) != '0') ) $number = '0' . $number; 509 510 return $number; 511 } 512 513 function tep_get_categories($categories_array = '', $parent_id = '0', $indent = '') { 514 global $languages_id; 515 516 if (!is_array($categories_array)) $categories_array = array(); 517 518 $categories_query = tep_db_query("select c.categories_id, cd.categories_name from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where parent_id = '" . (int)$parent_id . "' and c.categories_id = cd.categories_id and cd.language_id = '" . (int)$languages_id . "' order by sort_order, cd.categories_name"); 519 while ($categories = tep_db_fetch_array($categories_query)) { 520 $categories_array[] = array('id' => $categories['categories_id'], 521 'text' => $indent . $categories['categories_name']); 522 523 if ($categories['categories_id'] != $parent_id) { 524 $categories_array = tep_get_categories($categories_array, $categories['categories_id'], $indent . ' '); 525 } 526 } 527 528 return $categories_array; 529 } 530 531 function tep_get_manufacturers($manufacturers_array = '') { 532 if (!is_array($manufacturers_array)) $manufacturers_array = array(); 533 534 $manufacturers_query = tep_db_query("select manufacturers_id, manufacturers_name from " . TABLE_MANUFACTURERS . " order by manufacturers_name"); 535 while ($manufacturers = tep_db_fetch_array($manufacturers_query)) { 536 $manufacturers_array[] = array('id' => $manufacturers['manufacturers_id'], 'text' => $manufacturers['manufacturers_name']); 537 } 538 539 return $manufacturers_array; 540 } 541 542 //// 543 // Return all subcategory IDs 544 // TABLES: categories 545 function tep_get_subcategories(&$subcategories_array, $parent_id = 0) { 546 $subcategories_query = tep_db_query("select categories_id from " . TABLE_CATEGORIES . " where parent_id = '" . (int)$parent_id . "'"); 547 while ($subcategories = tep_db_fetch_array($subcategories_query)) { 548 $subcategories_array[sizeof($subcategories_array)] = $subcategories['categories_id']; 549 if ($subcategories['categories_id'] != $parent_id) { 550 tep_get_subcategories($subcategories_array, $subcategories['categories_id']); 551 } 552 } 553 } 554 555 // Output a raw date string in the selected locale date format 556 // $raw_date needs to be in this format: YYYY-MM-DD HH:MM:SS 557 function tep_date_long($raw_date) { 558 if ( ($raw_date == '0000-00-00 00:00:00') || ($raw_date == '') ) return false; 559 560 $year = (int)substr($raw_date, 0, 4); 561 $month = (int)substr($raw_date, 5, 2); 562 $day = (int)substr($raw_date, 8, 2); 563 $hour = (int)substr($raw_date, 11, 2); 564 $minute = (int)substr($raw_date, 14, 2); 565 $second = (int)substr($raw_date, 17, 2); 566 567 return strftime(DATE_FORMAT_LONG, mktime($hour,$minute,$second,$month,$day,$year)); 568 } 569 570 //// 571 // Output a raw date string in the selected locale date format 572 // $raw_date needs to be in this format: YYYY-MM-DD HH:MM:SS 573 // NOTE: Includes a workaround for dates before 01/01/1970 that fail on windows servers 574 function tep_date_short($raw_date) { 575 if ( ($raw_date == '0000-00-00 00:00:00') || empty($raw_date) ) return false; 576 577 $year = substr($raw_date, 0, 4); 578 $month = (int)substr($raw_date, 5, 2); 579 $day = (int)substr($raw_date, 8, 2); 580 $hour = (int)substr($raw_date, 11, 2); 581 $minute = (int)substr($raw_date, 14, 2); 582 $second = (int)substr($raw_date, 17, 2); 583 584 if (@date('Y', mktime($hour, $minute, $second, $month, $day, $year)) == $year) { 585 return date(DATE_FORMAT, mktime($hour, $minute, $second, $month, $day, $year)); 586 } else { 587 return ereg_replace('2037' . '$', $year, date(DATE_FORMAT, mktime($hour, $minute, $second, $month, $day, 2037))); 588 } 589 } 590 591 //// 592 // Parse search string into indivual objects 593 function tep_parse_search_string($search_str = '', &$objects) { 594 $search_str = trim(strtolower($search_str)); 595 596 // Break up $search_str on whitespace; quoted string will be reconstructed later 597 $pieces = split('[[:space:]]+', $search_str); 598 $objects = array(); 599 $tmpstring = ''; 600 $flag = ''; 601 602 for ($k=0; $k<count($pieces); $k++) { 603 while (substr($pieces[$k], 0, 1) == '(') { 604 $objects[] = '('; 605 if (strlen($pieces[$k]) > 1) { 606 $pieces[$k] = substr($pieces[$k], 1); 607 } else { 608 $pieces[$k] = ''; 609 } 610 } 611 612 $post_objects = array(); 613 614 while (substr($pieces[$k], -1) == ')') { 615 $post_objects[] = ')'; 616 if (strlen($pieces[$k]) > 1) { 617 $pieces[$k] = substr($pieces[$k], 0, -1); 618 } else { 619 $pieces[$k] = ''; 620 } 621 } 622 623 // Check individual words 624 625 if ( (substr($pieces[$k], -1) != '"') && (substr($pieces[$k], 0, 1) != '"') ) { 626 $objects[] = trim($pieces[$k]); 627 628 for ($j=0; $j<count($post_objects); $j++) { 629 $objects[] = $post_objects[$j]; 630 } 631 } else { 632 /* This means that the $piece is either the beginning or the end of a string. 633 So, we'll slurp up the $pieces and stick them together until we get to the 634 end of the string or run out of pieces. 635 */ 636 637 // Add this word to the $tmpstring, starting the $tmpstring 638 $tmpstring = trim(ereg_replace('"', ' ', $pieces[$k])); 639 640 // Check for one possible exception to the rule. That there is a single quoted word. 641 if (substr($pieces[$k], -1 ) == '"') { 642 // Turn the flag off for future iterations 643 $flag = 'off'; 644 645 $objects[] = trim($pieces[$k]); 646 647 for ($j=0; $j<count($post_objects); $j++) { 648 $objects[] = $post_objects[$j]; 649 } 650 651 unset($tmpstring); 652 653 // Stop looking for the end of the string and move onto the next word. 654 continue; 655 } 656 657 // Otherwise, turn on the flag to indicate no quotes have been found attached to this word in the string. 658 $flag = 'on'; 659 660 // Move on to the next word 661 $k++; 662 663 // Keep reading until the end of the string as long as the $flag is on 664 665 while ( ($flag == 'on') && ($k < count($pieces)) ) { 666 while (substr($pieces[$k], -1) == ')') { 667 $post_objects[] = ')'; 668 if (strlen($pieces[$k]) > 1) { 669 $pieces[$k] = substr($pieces[$k], 0, -1); 670 } else { 671 $pieces[$k] = ''; 672 } 673 } 674 675 // If the word doesn't end in double quotes, append it to the $tmpstring. 676 if (substr($pieces[$k], -1) != '"') { 677 // Tack this word onto the current string entity 678 $tmpstring .= ' ' . $pieces[$k]; 679 680 // Move on to the next word 681 $k++; 682 continue; 683 } else { 684 /* If the $piece ends in double quotes, strip the double quotes, tack the 685 $piece onto the tail of the string, push the $tmpstring onto the $haves, 686 kill the $tmpstring, turn the $flag "off", and return. 687 */ 688 $tmpstring .= ' ' . trim(ereg_replace('"', ' ', $pieces[$k])); 689 690 // Push the $tmpstring onto the array of stuff to search for 691 $objects[] = trim($tmpstring); 692 693 for ($j=0; $j<count($post_objects); $j++) { 694 $objects[] = $post_objects[$j]; 695 } 696 697 unset($tmpstring); 698 699 // Turn off the flag to exit the loop 700 $flag = 'off'; 701 } 702 } 703 } 704 } 705 706 // add default logical operators if needed 707 $temp = array(); 708 for($i=0; $i<(count($objects)-1); $i++) { 709 $temp[] = $objects[$i]; 710 if ( ($objects[$i] != 'and') && 711 ($objects[$i] != 'or') && 712 ($objects[$i] != '(') && 713 ($objects[$i+1] != 'and') && 714 ($objects[$i+1] != 'or') && 715 ($objects[$i+1] != ')') ) { 716 $temp[] = ADVANCED_SEARCH_DEFAULT_OPERATOR; 717 } 718 } 719 $temp[] = $objects[$i]; 720 $objects = $temp; 721 722 $keyword_count = 0; 723 $operator_count = 0; 724 $balance = 0; 725 for($i=0; $i<count($objects); $i++) { 726 if ($objects[$i] == '(') $balance --; 727 if ($objects[$i] == ')') $balance ++; 728 if ( ($objects[$i] == 'and') || ($objects[$i] == 'or') ) { 729 $operator_count ++; 730 } elseif ( ($objects[$i]) && ($objects[$i] != '(') && ($objects[$i] != ')') ) { 731 $keyword_count ++; 732 } 733 } 734 735 if ( ($operator_count < $keyword_count) && ($balance == 0) ) { 736 return true; 737 } else { 738 return false; 739 } 740 } 741 742 //// 743 // Check date 744 function tep_checkdate($date_to_check, $format_string, &$date_array) { 745 $separator_idx = -1; 746 747 $separators = array('-', ' ', '/', '.'); 748 $month_abbr = array('jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec'); 749 $no_of_days = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); 750 751 $format_string = strtolower($format_string); 752 753 if (strlen($date_to_check) != strlen($format_string)) { 754 return false; 755 } 756 757 $size = sizeof($separators); 758 for ($i=0; $i<$size; $i++) { 759 $pos_separator = strpos($date_to_check, $separators[$i]); 760 if ($pos_separator != false) { 761 $date_separator_idx = $i; 762 break; 763 } 764 } 765 766 for ($i=0; $i<$size; $i++) { 767 $pos_separator = strpos($format_string, $separators[$i]); 768 if ($pos_separator != false) { 769 $format_separator_idx = $i; 770 break; 771 } 772 } 773 774 if ($date_separator_idx != $format_separator_idx) { 775 return false; 776 } 777 778 if ($date_separator_idx != -1) { 779 $format_string_array = explode( $separators[$date_separator_idx], $format_string ); 780 if (sizeof($format_string_array) != 3) { 781 return false; 782 } 783 784 $date_to_check_array = explode( $separators[$date_separator_idx], $date_to_check ); 785 if (sizeof($date_to_check_array) != 3) { 786 return false; 787 } 788 789 $size = sizeof($format_string_array); 790 for ($i=0; $i<$size; $i++) { 791 if ($format_string_array[$i] == 'mm' || $format_string_array[$i] == 'mmm') $month = $date_to_check_array[$i]; 792 if ($format_string_array[$i] == 'dd') $day = $date_to_check_array[$i]; 793 if ( ($format_string_array[$i] == 'yyyy') || ($format_string_array[$i] == 'aaaa') ) $year = $date_to_check_array[$i]; 794 } 795 } else { 796 if (strlen($format_string) == 8 || strlen($format_string) == 9) { 797 $pos_month = strpos($format_string, 'mmm'); 798 if ($pos_month != false) { 799 $month = substr( $date_to_check, $pos_month, 3 ); 800 $size = sizeof($month_abbr); 801 for ($i=0; $i<$size; $i++) { 802 if ($month == $month_abbr[$i]) { 803 $month = $i; 804 break; 805 } 806 } 807 } else { 808 $month = substr($date_to_check, strpos($format_string, 'mm'), 2); 809 } 810 } else { 811 return false; 812 } 813 814 $day = substr($date_to_check, strpos($format_string, 'dd'), 2); 815 $year = substr($date_to_check, strpos($format_string, 'yyyy'), 4); 816 } 817 818 if (strlen($year) != 4) { 819 return false; 820 } 821 822 if (!settype($year, 'integer') || !settype($month, 'integer') || !settype($day, 'integer')) { 823 return false; 824 } 825 826 if ($month > 12 || $month < 1) { 827 return false; 828 } 829 830 if ($day < 1) { 831 return false; 832 } 833 834 if (tep_is_leap_year($year)) { 835 $no_of_days[1] = 29; 836 } 837 838 if ($day > $no_of_days[$month - 1]) { 839 return false; 840 } 841 842 $date_array = array($year, $month, $day); 843 844 return true; 845 } 846 847 //// 848 // Check if year is a leap year 849 function tep_is_leap_year($year) { 850 if ($year % 100 == 0) { 851 if ($year % 400 == 0) return true; 852 } else { 853 if (($year % 4) == 0) return true; 854 } 855 856 return false; 857 } 858 859 //// 860 // Return table heading with sorting capabilities 861 function tep_create_sort_heading($sortby, $colnum, $heading) { 862 global $PHP_SELF; 863 864 $sort_prefix = ''; 865 $sort_suffix = ''; 866 867 if ($sortby) { 868 $sort_prefix = '<a href="' . tep_href_link(basename($PHP_SELF), tep_get_all_get_params(array('page', 'info', 'sort')) . 'page=1&sort=' . $colnum . ($sortby == $colnum . 'a' ? 'd' : 'a')) . '" title="' . tep_output_string(TEXT_SORT_PRODUCTS . ($sortby == $colnum . 'd' || substr($sortby, 0, 1) != $colnum ? TEXT_ASCENDINGLY : TEXT_DESCENDINGLY) . TEXT_BY . $heading) . '" class="productListing-heading">' ; 869 $sort_suffix = (substr($sortby, 0, 1) == $colnum ? (substr($sortby, 1, 1) == 'a' ? '+' : '-') : '') . '</a>'; 870 } 871 872 return $sort_prefix . $heading . $sort_suffix; 873 } 874 875 //// 876 // Recursively go through the categories and retreive all parent categories IDs 877 // TABLES: categories 878 function tep_get_parent_categories(&$categories, $categories_id) { 879 $parent_categories_query = tep_db_query("select parent_id from " . TABLE_CATEGORIES . " where categories_id = '" . (int)$categories_id . "'"); 880 while ($parent_categories = tep_db_fetch_array($parent_categories_query)) { 881 if ($parent_categories['parent_id'] == 0) return true; 882 $categories[sizeof($categories)] = $parent_categories['parent_id']; 883 if ($parent_categories['parent_id'] != $categories_id) { 884 tep_get_parent_categories($categories, $parent_categories['parent_id']); 885 } 886 } 887 } 888 889 //// 890 // Construct a category path to the product 891 // TABLES: products_to_categories 892 function tep_get_product_path($products_id) { 893 $cPath = ''; 894 895 $category_query = tep_db_query("select p2c.categories_id from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c where p.products_id = '" . (int)$products_id . "' and p.products_status = '1' and p.products_id = p2c.products_id limit 1"); 896 if (tep_db_num_rows($category_query)) { 897 $category = tep_db_fetch_array($category_query); 898 899 $categories = array(); 900 tep_get_parent_categories($categories, $category['categories_id']); 901 902 $categories = array_reverse($categories); 903 904 $cPath = implode('_', $categories); 905 906 if (tep_not_null($cPath)) $cPath .= '_'; 907 $cPath .= $category['categories_id']; 908 } 909 910 return $cPath; 911 } 912 913 //// 914 // Return a product ID with attributes 915 function tep_get_uprid($prid, $params) { 916 if (is_numeric($prid)) { 917 $uprid = $prid; 918 919 if (is_array($params) && (sizeof($params) > 0)) { 920 $attributes_check = true; 921 $attributes_ids = ''; 922 923 reset($params); 924 while (list($option, $value) = each($params)) { 925 if (is_numeric($option) && is_numeric($value)) { 926 $attributes_ids .= '{' . (int)$option . '}' . (int)$value; 927 } else { 928 $attributes_check = false; 929 break; 930 } 931 } 932 933 if ($attributes_check == true) { 934 $uprid .= $attributes_ids; 935 } 936 } 937 } else { 938 $uprid = tep_get_prid($prid); 939 940 if (is_numeric($uprid)) { 941 if (strpos($prid, '{') !== false) { 942 $attributes_check = true; 943 $attributes_ids = ''; 944 945 // strpos()+1 to remove up to and including the first { which would create an empty array element in explode() 946 $attributes = explode('{', substr($prid, strpos($prid, '{')+1)); 947 948 for ($i=0, $n=sizeof($attributes); $i<$n; $i++) { 949 $pair = explode('}', $attributes[$i]); 950 951 if (is_numeric($pair[0]) && is_numeric($pair[1])) { 952 $attributes_ids .= '{' . (int)$pair[0] . '}' . (int)$pair[1]; 953 } else { 954 $attributes_check = false; 955 break; 956 } 957 } 958 959 if ($attributes_check == true) { 960 $uprid .= $attributes_ids; 961 } 962 } 963 } else { 964 return false; 965 } 966 } 967 968 return $uprid; 969 } 970 971 //// 972 // Return a product ID from a product ID with attributes 973 function tep_get_prid($uprid) { 974 $pieces = explode('{', $uprid); 975 976 if (is_numeric($pieces[0])) { 977 return $pieces[0]; 978 } else { 979 return false; 980 } 981 } 982 983 //// 984 // Return a customer greeting 985 function tep_customer_greeting() { 986 global $customer_id, $customer_first_name; 987 988 if (tep_session_is_registered('customer_first_name') && tep_session_is_registered('customer_id')) { 989 $greeting_string = sprintf(TEXT_GREETING_PERSONAL, tep_output_string_protected($customer_first_name), tep_href_link(FILENAME_PRODUCTS_NEW)); 990 } else { 991 $greeting_string = sprintf(TEXT_GREETING_GUEST, tep_href_link(FILENAME_LOGIN, '', 'SSL'), tep_href_link(FILENAME_CREATE_ACCOUNT, '', 'SSL')); 992 } 993 994 return $greeting_string; 995 } 996 997 //// 998 //! Send email (text/html) using MIME 999 // This is the central mail function. The SMTP Server should be configured 1000 // correct in php.ini 1001 // Parameters: 1002 // $to_name The name of the recipient, e.g. "Jan Wildeboer" 1003 // $to_email_address The eMail address of the recipient, 1004 // e.g. jan.wildeboer@gmx.de 1005 // $email_subject The subject of the eMail 1006 // $email_text The text of the eMail, may contain HTML entities 1007 // $from_email_name The name of the sender, e.g. Shop Administration 1008 // $from_email_adress The eMail address of the sender, 1009 // e.g. info@mytepshop.com 1010 1011 function tep_mail($to_name, $to_email_address, $email_subject, $email_text, $from_email_name, $from_email_address) { 1012 if (SEND_EMAILS != 'true') return false; 1013 1014 // Instantiate a new mail object 1015 $message = new email(array('X-Mailer: osCommerce Mailer')); 1016 1017 // Build the text version 1018 $text = strip_tags($email_text); 1019 if (EMAIL_USE_HTML == 'true') { 1020 $message->add_html($email_text, $text); 1021 } else { 1022 $message->add_text($text); 1023 } 1024 1025 // Send message 1026 $message->build_message(); 1027 $message->send($to_name, $to_email_address, $from_email_name, $from_email_address, $email_subject); 1028 } 1029 1030 //// 1031 // Check if product has attributes 1032 function tep_has_product_attributes($products_id) { 1033 $attributes_query = tep_db_query("select count(*) as count from " . TABLE_PRODUCTS_ATTRIBUTES . " where products_id = '" . (int)$products_id . "'"); 1034 $attributes = tep_db_fetch_array($attributes_query); 1035 1036 if ($attributes['count'] > 0) { 1037 return true; 1038 } else { 1039 return false; 1040 } 1041 } 1042 1043 //// 1044 // Get the number of times a word/character is present in a string 1045 function tep_word_count($string, $needle) { 1046 $temp_array = split($needle, $string); 1047 1048 return sizeof($temp_array); 1049 } 1050 1051 function tep_count_modules($modules = '') { 1052 $count = 0; 1053 1054 if (empty($modules)) return $count; 1055 1056 $modules_array = split(';', $modules); 1057 1058 for ($i=0, $n=sizeof($modules_array); $i<$n; $i++) { 1059 $class = substr($modules_array[$i], 0, strrpos($modules_array[$i], '.')); 1060 1061 if (is_object($GLOBALS[$class])) { 1062 if ($GLOBALS[$class]->enabled) { 1063 $count++; 1064 } 1065 } 1066 } 1067 1068 return $count; 1069 } 1070 1071 function tep_count_payment_modules() { 1072 return tep_count_modules(MODULE_PAYMENT_INSTALLED); 1073 } 1074 1075 function tep_count_shipping_modules() { 1076 return tep_count_modules(MODULE_SHIPPING_INSTALLED); 1077 } 1078 1079 function tep_create_random_value($length, $type = 'mixed') { 1080 if ( ($type != 'mixed') && ($type != 'chars') && ($type != 'digits')) return false; 1081 1082 $rand_value = ''; 1083 while (strlen($rand_value) < $length) { 1084 if ($type == 'digits') { 1085 $char = tep_rand(0,9); 1086 } else { 1087 $char = chr(tep_rand(0,255)); 1088 } 1089 if ($type == 'mixed') { 1090 if (eregi('^[a-z0-9]$', $char)) $rand_value .= $char; 1091 } elseif ($type == 'chars') { 1092 if (eregi('^[a-z]$', $char)) $rand_value .= $char; 1093 } elseif ($type == 'digits') { 1094 if (ereg('^[0-9]$', $char)) $rand_value .= $char; 1095 } 1096 } 1097 1098 return $rand_value; 1099 } 1100 1101 function tep_array_to_string($array, $exclude = '', $equals = '=', $separator = '&') { 1102 if (!is_array($exclude)) $exclude = array(); 1103 1104 $get_string = ''; 1105 if (sizeof($array) > 0) { 1106 while (list($key, $value) = each($array)) { 1107 if ( (!in_array($key, $exclude)) && ($key != 'x') && ($key != 'y') ) { 1108 $get_string .= $key . $equals . $value . $separator; 1109 } 1110 } 1111 $remove_chars = strlen($separator); 1112 $get_string = substr($get_string, 0, -$remove_chars); 1113 } 1114 1115 return $get_string; 1116 } 1117 1118 function tep_not_null($value) { 1119 if (is_array($value)) { 1120 if (sizeof($value) > 0) { 1121 return true; 1122 } else { 1123 return false; 1124 } 1125 } else { 1126 if (($value != '') && (strtolower($value) != 'null') && (strlen(trim($value)) > 0)) { 1127 return true; 1128 } else { 1129 return false; 1130 } 1131 } 1132 } 1133 1134 //// 1135 // Output the tax percentage with optional padded decimals 1136 function tep_display_tax_value($value, $padding = TAX_DECIMAL_PLACES) { 1137 if (strpos($value, '.')) { 1138 $loop = true; 1139 while ($loop) { 1140 if (substr($value, -1) == '0') { 1141 $value = substr($value, 0, -1); 1142 } else { 1143 $loop = false; 1144 if (substr($value, -1) == '.') { 1145 $value = substr($value, 0, -1); 1146 } 1147 } 1148 } 1149 } 1150 1151 if ($padding > 0) { 1152 if ($decimal_pos = strpos($value, '.')) { 1153 $decimals = strlen(substr($value, ($decimal_pos+1))); 1154 for ($i=$decimals; $i<$padding; $i++) { 1155 $value .= '0'; 1156 } 1157 } else { 1158 $value .= '.'; 1159 for ($i=0; $i<$padding; $i++) { 1160 $value .= '0'; 1161 } 1162 } 1163 } 1164 1165 return $value; 1166 } 1167 1168 //// 1169 // Checks to see if the currency code exists as a currency 1170 // TABLES: currencies 1171 function tep_currency_exists($code) { 1172 $code = tep_db_prepare_input($code); 1173 1174 $currency_code = tep_db_query("select currencies_id from " . TABLE_CURRENCIES . " where code = '" . tep_db_input($code) . "'"); 1175 if (tep_db_num_rows($currency_code)) { 1176 return $code; 1177 } else { 1178 return false; 1179 } 1180 } 1181 1182 function tep_string_to_int($string) { 1183 return (int)$string; 1184 } 1185 1186 //// 1187 // Parse and secure the cPath parameter values 1188 function tep_parse_category_path($cPath) { 1189 // make sure the category IDs are integers 1190 $cPath_array = array_map('tep_string_to_int', explode('_', $cPath)); 1191 1192 // make sure no duplicate category IDs exist which could lock the server in a loop 1193 $tmp_array = array(); 1194 $n = sizeof($cPath_array); 1195 for ($i=0; $i<$n; $i++) { 1196 if (!in_array($cPath_array[$i], $tmp_array)) { 1197 $tmp_array[] = $cPath_array[$i]; 1198 } 1199 } 1200 1201 return $tmp_array; 1202 } 1203 1204 //// 1205 // Return a random value 1206 function tep_rand($min = null, $max = null) { 1207 static $seeded; 1208 1209 if (!isset($seeded)) { 1210 mt_srand((double)microtime()*1000000); 1211 $seeded = true; 1212 } 1213 1214 if (isset($min) && isset($max)) { 1215 if ($min >= $max) { 1216 return $min; 1217 } else { 1218 return mt_rand($min, $max); 1219 } 1220 } else { 1221 return mt_rand(); 1222 } 1223 } 1224 1225 function tep_setcookie($name, $value = '', $expire = 0, $path = '/', $domain = '', $secure = 0) { 1226 setcookie($name, $value, $expire, $path, (tep_not_null($domain) ? $domain : ''), $secure); 1227 } 1228 1229 function tep_get_ip_address() { 1230 if (isset($_SERVER)) { 1231 if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { 1232 $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; 1233 } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) { 1234 $ip = $_SERVER['HTTP_CLIENT_IP']; 1235 } else { 1236 $ip = $_SERVER['REMOTE_ADDR']; 1237 } 1238 } else { 1239 if (getenv('HTTP_X_FORWARDED_FOR')) { 1240 $ip = getenv('HTTP_X_FORWARDED_FOR'); 1241 } elseif (getenv('HTTP_CLIENT_IP')) { 1242 $ip = getenv('HTTP_CLIENT_IP'); 1243 } else { 1244 $ip = getenv('REMOTE_ADDR'); 1245 } 1246 } 1247 1248 return $ip; 1249 } 1250 1251 function tep_count_customer_orders($id = '', $check_session = true) { 1252 global $customer_id; 1253 1254 if (is_numeric($id) == false) { 1255 if (tep_session_is_registered('customer_id')) { 1256 $id = $customer_id; 1257 } else { 1258 return 0; 1259 } 1260 } 1261 1262 if ($check_session == true) { 1263 if ( (tep_session_is_registered('customer_id') == false) || ($id != $customer_id) ) { 1264 return 0; 1265 } 1266 } 1267 1268 $orders_check_query = tep_db_query("select count(*) as total from " . TABLE_ORDERS . " where customers_id = '" . (int)$id . "'"); 1269 $orders_check = tep_db_fetch_array($orders_check_query); 1270 1271 return $orders_check['total']; 1272 } 1273 1274 function tep_count_customer_address_book_entries($id = '', $check_session = true) { 1275 global $customer_id; 1276 1277 if (is_numeric($id) == false) { 1278 if (tep_session_is_registered('customer_id')) { 1279 $id = $customer_id; 1280 } else { 1281 return 0; 1282 } 1283 } 1284 1285 if ($check_session == true) { 1286 if ( (tep_session_is_registered('customer_id') == false) || ($id != $customer_id) ) { 1287 return 0; 1288 } 1289 } 1290 1291 $addresses_query = tep_db_query("select count(*) as total from " . TABLE_ADDRESS_BOOK . " where customers_id = '" . (int)$id . "'"); 1292 $addresses = tep_db_fetch_array($addresses_query); 1293 1294 return $addresses['total']; 1295 } 1296 1297 // nl2br() prior PHP 4.2.0 did not convert linefeeds on all OSs (it only converted \n) 1298 function tep_convert_linefeeds($from, $to, $string) { 1299 if ((PHP_VERSION < "4.0.5") && is_array($from)) { 1300 return ereg_replace('(' . implode('|', $from) . ')', $to, $string); 1301 } else { 1302 return str_replace($from, $to, $string); 1303 } 1304 } 1305 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Mon Nov 26 19:48:25 2007 | par Balluche grâce à PHPXref 0.7 |
|