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