functions_general.php
来自「Zen Cart是真正的电子商务艺术」· PHP 代码 · 共 1,529 行 · 第 1/4 页
PHP
1,529 行
}
} else {
if (getenv('HTTP_X_FORWARDED_FOR')) {
$ip = getenv('HTTP_X_FORWARDED_FOR');
} elseif (getenv('HTTP_CLIENT_IP')) {
$ip = getenv('HTTP_CLIENT_IP');
} else {
$ip = getenv('REMOTE_ADDR');
}
}
return $ip;
}
// nl2br() prior PHP 4.2.0 did not convert linefeeds on all OSs (it only converted \n)
function zen_convert_linefeeds($from, $to, $string) {
if ((PHP_VERSION < "4.0.5") && is_array($from)) {
return ereg_replace('(' . implode('|', $from) . ')', $to, $string);
} else {
return str_replace($from, $to, $string);
}
}
////
function is_product_valid($product_id, $coupon_id) {
global $db;
$coupons_query = "SELECT * FROM " . TABLE_COUPON_RESTRICT . "
WHERE coupon_id = '" . (int)$coupon_id . "'
ORDER BY coupon_restrict ASC";
$coupons = $db->Execute($coupons_query);
$product_query = "SELECT products_model FROM " . TABLE_PRODUCTS . "
WHERE products_id = '" . (int)$product_id . "'";
$product = $db->Execute($product_query);
if (ereg('^GIFT', $product->fields['products_model'])) {
return false;
}
// modified to manage restrictions better - leave commented for now
if ($coupons->RecordCount() == 0) return true;
if ($coupons->RecordCount() == 1) {
// If product is restricted(deny) and is same as tested prodcut deny
if (($coupons->fields['product_id'] != 0) && $coupons->fields['product_id'] == (int)$product_id && $coupons->fields['coupon_restrict']=='Y') return false;
// If product is not restricted(allow) and is not same as tested prodcut deny
if (($coupons->fields['product_id'] != 0) && $coupons->fields['product_id'] != (int)$product_id && $coupons->fields['coupon_restrict']=='N') return false;
// if category is restricted(deny) and product in category deny
if (($coupons->fields['category_id'] !=0) && (zen_product_in_category($product_id, $coupons->fields['category_id'])) && ($coupons->fields['coupon_restrict']=='Y')) return false;
// if category is not restricted(allow) and product not in category deny
if (($coupons->fields['category_id'] !=0) && (!zen_product_in_category($product_id, $coupons->fields['category_id'])) && ($coupons->fields['coupon_restrict']=='N')) return false;
return true;
}
$allow_for_category = validate_for_category($product_id, $coupon_id);
$allow_for_product = validate_for_product($product_id, $coupon_id);
// echo '#'.$product_id . '#' . $allow_for_category;
// echo '#'.$product_id . '#' . $allow_for_product;
if ($allow_for_category == 'none') {
if ($allow_for_product === 'none') return true;
if ($allow_for_product === true) return true;
if ($allow_for_product === false) return false;
}
if ($allow_for_category === true) {
if ($allow_for_product === 'none') return true;
if ($allow_for_product === true) return true;
if ($allow_for_product === false) return false;
}
if ($allow_for_category === false) {
if ($allow_for_product === 'none') return false;
if ($allow_for_product === true) return true;
if ($allow_for_product === false) return false;
}
return false; //should never get here
}
function validate_for_category($product_id, $coupon_id) {
global $db;
$retVal = 'none';
$productCatPath = zen_get_product_path($product_id);
$catPathArray = array_reverse(explode('_', $productCatPath));
$sql = "SELECT count(*) AS total
FROM " . TABLE_COUPON_RESTRICT . "
WHERE category_id = -1
AND coupon_restrict = 'Y'";
$checkQuery = $db->execute($sql);
foreach ($catPathArray as $catPath) {
$sql = "SELECT * FROM " . TABLE_COUPON_RESTRICT . "
WHERE category_id = " . (int)$catPath . "
AND coupon_id = " . (int)$coupon_id;
$result = $db->execute($sql);
if ($result->recordCount() > 0 && $result->fields['coupon_restrict'] == 'N') return true;
if ($result->recordCount() > 0 && $result->fields['coupon_restrict'] == 'Y') return false;
}
if ($checkQuery->fields['total'] > 0) {
return false;
} else {
return 'none';
}
}
function validate_for_product($product_id, $coupon_id) {
global $db;
$sql = "SELECT * FROM " . TABLE_COUPON_RESTRICT . "
WHERE product_id = " . (int)$product_id . "
AND coupon_id = " . (int)$coupon_id . " LIMIT 1";
$result = $db->execute($sql);
if ($result->recordCount() > 0) {
if ($result->fields['coupon_restrict'] == 'N') return true;
if ($result->fields['coupon_restrict'] == 'Y') return false;
} else {
return 'none';
}
}
////
function zen_db_input($string) {
return addslashes($string);
}
////
function zen_db_prepare_input($string) {
if (is_string($string)) {
return trim(zen_sanitize_string(stripslashes($string)));
} elseif (is_array($string)) {
reset($string);
while (list($key, $value) = each($string)) {
$string[$key] = zen_db_prepare_input($value);
}
return $string;
} else {
return $string;
}
}
////
function zen_db_perform($table, $data, $action = 'insert', $parameters = '', $link = 'db_link') {
global $db;
reset($data);
if (strtolower($action) == 'insert') {
$query = 'INSERT INTO ' . $table . ' (';
while (list($columns, ) = each($data)) {
$query .= $columns . ', ';
}
$query = substr($query, 0, -2) . ') VALUES (';
reset($data);
while (list(, $value) = each($data)) {
switch ((string)$value) {
case 'now()':
$query .= 'now(), ';
break;
case 'null':
$query .= 'null, ';
break;
default:
$query .= '\'' . zen_db_input($value) . '\', ';
break;
}
}
$query = substr($query, 0, -2) . ')';
} elseif (strtolower($action) == 'update') {
$query = 'UPDATE ' . $table . ' SET ';
while (list($columns, $value) = each($data)) {
switch ((string)$value) {
case 'now()':
$query .= $columns . ' = now(), ';
break;
case 'null':
$query .= $columns .= ' = null, ';
break;
default:
$query .= $columns . ' = \'' . zen_db_input($value) . '\', ';
break;
}
}
$query = substr($query, 0, -2) . ' WHERE ' . $parameters;
}
return $db->Execute($query);
}
////
function zen_db_output($string) {
return htmlspecialchars($string);
}
// function to return field type
// uses $tbl = table name, $fld = field name
function zen_field_type($tbl, $fld) {
global $db;
$rs = $db->MetaColumns($tbl);
// modified by zen-cart.cn
$type = $rs[GBcase($fld,"upper")]->type;
return $type;
}
// function to return field length
// uses $tbl = table name, $fld = field name
function zen_field_length($tbl, $fld) {
global $db;
$rs = $db->MetaColumns($tbl);
$length = $rs[strtoupper($fld)]->max_length;
return $length;
}
////
// return the size and maxlength settings in the form size="blah" maxlength="blah" based on maximum size being 70
// uses $tbl = table name, $fld = field name
// example: zen_set_field_length(TABLE_CATEGORIES_DESCRIPTION, 'categories_name')
function zen_set_field_length($tbl, $fld, $max=70) {
$field_length= zen_field_length($tbl, $fld);
switch (true) {
case ($field_length > $max):
$length= 'size = "' . ($max+1) . '" maxlength= "' . $field_length . '"';
break;
default:
$length= 'size = "' . ($field_length+1) . '" maxlength = "' . $field_length . '"';
break;
}
return $length;
}
////
// Set back button
function zen_back_link() {
if (sizeof($_SESSION['navigation']->path)-2 > 0) {
$back = sizeof($_SESSION['navigation']->path)-2;
$link = '<a href="' . zen_href_link($_SESSION['navigation']->path[$back]['page'], zen_array_to_string($_SESSION['navigation']->path[$back]['get'], array('action')), $_SESSION['navigation']->path[$back]['mode']) . '">';
} else {
if (isset($_SERVER['HTTP_REFERER']) && strstr(HTTP_SERVER, $_SERVER['HTTP_REFERER'])) {
$link= $_SERVER['HTTP_REFERER'];
} else {
$link = '<a href="' . zen_href_link(FILENAME_DEFAULT) . '">';
}
$_SESSION['navigation'] = new navigationHistory;
}
return $link;
}
////
// Set back link only
function zen_back_link_only($link_only = false) {
if (sizeof($_SESSION['navigation']->path)-2 > 0) {
$back = sizeof($_SESSION['navigation']->path)-2;
$link = zen_href_link($_SESSION['navigation']->path[$back]['page'], zen_array_to_string($_SESSION['navigation']->path[$back]['get'], array('action')), $_SESSION['navigation']->path[$back]['mode']);
} else {
if (strstr(HTTP_SERVER, $_SERVER['HTTP_REFERER'])) {
$link= $_SERVER['HTTP_REFERER'];
} else {
$link = zen_href_link(FILENAME_DEFAULT);
}
$_SESSION['navigation'] = new navigationHistory;
}
if ($link_only == true) {
return $link;
} else {
return '<a href="' . $link . '">';
}
}
////
// Return a random row from a database query
function zen_random_select($query) {
global $db;
$random_product = '';
$random_query = $db->Execute($query);
$num_rows = $random_query->RecordCount();
if ($num_rows > 1) {
$random_row = zen_rand(0, ($num_rows - 1));
$random_query->Move($random_row);
}
return $random_query;
}
////
// Truncate a string
function zen_trunc_string($str = "", $len = 150, $more = 'true') {
if ($str == "") return $str;
if (is_array($str)) return $str;
$str = trim($str);
// if it's les than the size given, then return it
if (strlen($str) <= $len) return $str;
// else get that size of text
$str = substr($str, 0, $len);
// backtrack to the end of a word
if ($str != "") {
// check to see if there are any spaces left
if (!substr_count($str , " ")) {
if ($more == 'true') $str .= "...";
return $str;
}
// backtrack
while(strlen($str) && ($str[strlen($str)-1] != " ")) {
$str = substr($str, 0, -1);
}
$str = substr($str, 0, -1);
if ($more == 'true') $str .= "...";
if ($more != 'true' and $more != 'false') $str .= $more;
}
return $str;
}
////
// set current box id
function zen_get_box_id($box_id) {
while (strstr($box_id, '_')) $box_id = str_replace('_', '', $box_id);
$box_id = str_replace('.php', '', $box_id);
return $box_id;
}
////
// Switch buy now button based on call for price sold out etc.
function zen_get_buy_now_button($product_id, $link, $additional_link = false) {
global $db;
// show case only superceeds all other settings
if (STORE_STATUS != '0') {
return '<a href="' . zen_href_link(FILENAME_CONTACT_US) . '">' . TEXT_SHOWCASE_ONLY . '</a>';
}
// 0 = normal shopping
// 1 = Login to shop
// 2 = Can browse but no prices
// verify display of prices
switch (true) {
case (CUSTOMERS_APPROVAL == '1' and $_SESSION['customer_id'] == ''):
// customer must be logged in to browse
$login_for_price = '<a href="' . zen_href_link(FILENAME_LOGIN, '', 'SSL') . '">' . TEXT_LOGIN_FOR_PRICE_BUTTON_REPLACE . '</a>';
return $login_for_price;
break;
case (CUSTOMERS_APPROVAL == '2' and $_SESSION['customer_id'] == ''):
if (TEXT_LOGIN_FOR_PRICE_PRICE == '') {
// show room only
return TEXT_LOGIN_FOR_PRICE_BUTTON_REPLACE;
} else {
// customer may browse but no prices
$login_for_price = '<a href="' . zen_href_link(FILENAME_LOGIN, '', 'SSL') . '">' . TEXT_LOGIN_FOR_PRICE_BUTTON_REPLACE . '</a>';
}
return $login_for_price;
break;
// show room only
case (CUSTOMERS_APPROVAL == '3'):
$login_for_price = TEXT_LOGIN_FOR_PRICE_BUTTON_REPLACE_SHOWROOM;
return $login_for_price;
break;
case ((CUSTOMERS_APPROVAL_AUTHORIZATION != '0' and CUSTOMERS_APPROVAL_AUTHORIZATION != '3') and $_SESSION['customer_id'] == ''):
// customer must be logged in to browse
$login_for_price = TEXT_AUTHORIZATION_PENDING_BUTTON_REPLACE;
return $login_for_price;
break;
case ((CUSTOMERS_APPROVAL_AUTHORIZATION == '3') and $_SESSION['customer_id'] == ''):
// customer must be logged in and approved to add to cart
$login_for_price = '<a href="' . zen_href_link(FILENAME_LOGIN, '', 'SSL') . '">' . TEXT_LOGIN_TO_SHOP_BUTTON_REPLACE . '</a>';
return $login_for_price;
break;
case (CUSTOMERS_APPROVAL_AUTHORIZATION != '0' and $_SESSION['customers_authorization'] > '0'):
// customer must be logged in to browse
$login_for_price = TEXT_AUTHORIZATION_PENDING_BUTTON_REPLACE;
return $login_for_price;
break;
default:
// proceed normally
break;
}
$button_check = $db->Execute("select product_is_call, products_quantity from " . TABLE_PRODUCTS . " where products_id = '" . (int)$product_id . "'");
switch (true) {
// cannot be added to the cart
case (zen_get_products_allow_add_to_cart($product_id) == 'N') :
return $additional_link;
break;
case ($button_check->fields['product_is_call'] == '1'):
$return_button = '<a href="' . zen_href_link(FILENAME_CONTACT_US) . '">' . TEXT_CALL_FOR_PRICE . '</a>';
break;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?