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