📄 functions_general.php
字号:
<?php
/**
* functions_general.php
* General functions used throughout Zen Cart
*
* @package functions
* @copyright Copyright 2003-2005 Zen Cart Development Team
* @copyright Portions Copyright 2003 osCommerce
* @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
* @version $Id: functions_general.php 3670 2006-05-29 20:24:28Z wilt $
*/
if (!defined('IS_ADMIN_FLAG')) {
die('Illegal Access');
}
/**
* Stop from parsing any further PHP code
*/
function zen_exit() {
zen_session_close();
exit();
}
/**
* Redirect to another page or site
* @param string The url to redirect to
*/
function zen_redirect($url) {
global $request_type;
if ( (ENABLE_SSL == true) && ($request_type == 'SSL') ) { // We are loading an SSL page
if (substr($url, 0, strlen(HTTP_SERVER)) == HTTP_SERVER) { // NONSSL url
$url = HTTPS_SERVER . substr($url, strlen(HTTP_SERVER)); // Change it to SSL
}
}
// clean up URL before executing it
while (strstr($url, '&&')) $url = str_replace('&&', '&', $url);
while (strstr($url, '&&')) $url = str_replace('&&', '&', $url);
// header locates should not have the & in the address it breaks things
while (strstr($url, '&')) $url = str_replace('&', '&', $url);
header('Location: ' . $url);
zen_exit();
}
/**
* Parse the data used in the html tags to ensure the tags will not break.
* Basically just an extension to the php strstr function
* @param string The string to be parsed
* @param string The needle to find
*/
// Parse the data used in the html tags to ensure the tags will not break
function zen_parse_input_field_data($data, $parse) {
return strtr(trim($data), $parse);
}
/**
* Returns a string with conversions for security.
* @param string The string to be parsed
* @param string contains a string to be translated, otherwise just quote is translated
* @param boolean Do we run htmlspecialchars over the string
*/
function zen_output_string($string, $translate = false, $protected = false) {
if ($protected == true) {
return htmlspecialchars($string);
} else {
if ($translate == false) {
return zen_parse_input_field_data($string, array('"' => '"'));
} else {
return zen_parse_input_field_data($string, $translate);
}
}
}
/**
* Returns a string with conversions for security.
*
* Simply calls the zen_ouput_string function
* with parameters that run htmlspecialchars over the string
* and converts quotes to html entities
*
* @param string The string to be parsed
*/
function zen_output_string_protected($string) {
return zen_output_string($string, false, true);
}
/**
* Returns a string with conversions for security.
*
* @param string The string to be parsed
*/
function zen_sanitize_string($string) {
$string = ereg_replace(' +', ' ', $string);
return preg_replace("/[<>]/", '_', $string);
}
/**
* Break a word in a string if it is longer than a specified length ($len)
*
* @param string The string to be broken up
* @param int The maximum length allowed
* @param string The character to use at the end of the broken line
*/
function zen_break_string($string, $len, $break_char = '-') {
$l = 0;
$output = '';
for ($i=0, $n=strlen($string); $i<$n; $i++) {
$char = substr($string, $i, 1);
if ($char != ' ') {
$l++;
} else {
$l = 0;
}
if ($l > $len) {
$l = 1;
$output .= $break_char;
}
$output .= $char;
}
return $output;
}
/**
* Return all HTTP GET variables, except those passed as a parameter
*
* The return is a urlencoded string
*
* @param mixed either a single or array of parameter names to be excluded from output
*/
// Return all HTTP GET variables, except those passed as a parameter
function zen_get_all_get_params($exclude_array = '', $search_engine_safe = true) {
if (!is_array($exclude_array)) $exclude_array = array();
$get_url = '';
if (is_array($_GET) && (sizeof($_GET) > 0)) {
reset($_GET);
while (list($key, $value) = each($_GET)) {
if ( (strlen($value) > 0) && ($key != 'main_page') && ($key != zen_session_name()) && ($key != 'error') && (!in_array($key, $exclude_array)) && ($key != 'x') && ($key != 'y') ) {
if ( (SEARCH_ENGINE_FRIENDLY_URLS == 'true') && ($search_engine_safe == true) ) {
// die ('here');
$get_url .= $key . '/' . rawurlencode(stripslashes($value)) . '/';
} else {
$get_url .= $key . '=' . rawurlencode(stripslashes($value)) . '&';
}
}
}
}
while (strstr($get_url, '&&')) $get_url = str_replace('&&', '&', $get_url);
while (strstr($get_url, '&&')) $get_url = str_replace('&&', '&', $get_url);
return $get_url;
}
////
// Returns the clients browser
function zen_browser_detect($component) {
global $HTTP_USER_AGENT;
return stristr($HTTP_USER_AGENT, $component);
}
////
// Wrapper function for round()
function zen_round($number, $precision) {
/// fix rounding error on GVs etc.
$number = round($number, $precision);
return $number;
}
////
// default filler is a 0 or pass filler to be used
function zen_row_number_format($number, $filler='0') {
if ( ($number < 10) && (substr($number, 0, 1) != '0') ) $number = $filler . $number;
return $number;
}
// Output a raw date string in the selected locale date format
// $raw_date needs to be in this format: YYYY-MM-DD HH:MM:SS
function zen_date_long($raw_date) {
if ( ($raw_date == '0001-01-01 00:00:00') || ($raw_date == '') ) return false;
$year = (int)substr($raw_date, 0, 4);
$month = (int)substr($raw_date, 5, 2);
$day = (int)substr($raw_date, 8, 2);
$hour = (int)substr($raw_date, 11, 2);
$minute = (int)substr($raw_date, 14, 2);
$second = (int)substr($raw_date, 17, 2);
return strftime(DATE_FORMAT_LONG, mktime($hour,$minute,$second,$month,$day,$year));
}
////
// Output a raw date string in the selected locale date format
// $raw_date needs to be in this format: YYYY-MM-DD HH:MM:SS
// NOTE: Includes a workaround for dates before 01/01/1970 that fail on windows servers
function zen_date_short($raw_date) {
if ( ($raw_date == '0001-01-01 00:00:00') || empty($raw_date) ) return false;
$year = substr($raw_date, 0, 4);
$month = (int)substr($raw_date, 5, 2);
$day = (int)substr($raw_date, 8, 2);
$hour = (int)substr($raw_date, 11, 2);
$minute = (int)substr($raw_date, 14, 2);
$second = (int)substr($raw_date, 17, 2);
// error on 1969 only allows for leap year
if ($year != 1969 && @date('Y', mktime($hour, $minute, $second, $month, $day, $year)) == $year) {
return date(DATE_FORMAT, mktime($hour, $minute, $second, $month, $day, $year));
} else {
return ereg_replace('2037' . '$', $year, date(DATE_FORMAT, mktime($hour, $minute, $second, $month, $day, 2037)));
}
}
////
// Parse search string into indivual objects
function zen_parse_search_string($search_str = '', &$objects) {
$search_str = trim(strtolower($search_str));
// Break up $search_str on whitespace; quoted string will be reconstructed later
$pieces = split('[[:space:]]+', $search_str);
$objects = array();
$tmpstring = '';
$flag = '';
for ($k=0; $k<count($pieces); $k++) {
while (substr($pieces[$k], 0, 1) == '(') {
$objects[] = '(';
if (strlen($pieces[$k]) > 1) {
$pieces[$k] = substr($pieces[$k], 1);
} else {
$pieces[$k] = '';
}
}
$post_objects = array();
while (substr($pieces[$k], -1) == ')') {
$post_objects[] = ')';
if (strlen($pieces[$k]) > 1) {
$pieces[$k] = substr($pieces[$k], 0, -1);
} else {
$pieces[$k] = '';
}
}
// Check individual words
if ( (substr($pieces[$k], -1) != '"') && (substr($pieces[$k], 0, 1) != '"') ) {
$objects[] = trim($pieces[$k]);
for ($j=0; $j<count($post_objects); $j++) {
$objects[] = $post_objects[$j];
}
} else {
/* This means that the $piece is either the beginning or the end of a string.
So, we'll slurp up the $pieces and stick them together until we get to the
end of the string or run out of pieces.
*/
// Add this word to the $tmpstring, starting the $tmpstring
$tmpstring = trim(ereg_replace('"', ' ', $pieces[$k]));
// Check for one possible exception to the rule. That there is a single quoted word.
if (substr($pieces[$k], -1 ) == '"') {
// Turn the flag off for future iterations
$flag = 'off';
$objects[] = trim($pieces[$k]);
for ($j=0; $j<count($post_objects); $j++) {
$objects[] = $post_objects[$j];
}
unset($tmpstring);
// Stop looking for the end of the string and move onto the next word.
continue;
}
// Otherwise, turn on the flag to indicate no quotes have been found attached to this word in the string.
$flag = 'on';
// Move on to the next word
$k++;
// Keep reading until the end of the string as long as the $flag is on
while ( ($flag == 'on') && ($k < count($pieces)) ) {
while (substr($pieces[$k], -1) == ')') {
$post_objects[] = ')';
if (strlen($pieces[$k]) > 1) {
$pieces[$k] = substr($pieces[$k], 0, -1);
} else {
$pieces[$k] = '';
}
}
// If the word doesn't end in double quotes, append it to the $tmpstring.
if (substr($pieces[$k], -1) != '"') {
// Tack this word onto the current string entity
$tmpstring .= ' ' . $pieces[$k];
// Move on to the next word
$k++;
continue;
} else {
/* If the $piece ends in double quotes, strip the double quotes, tack the
$piece onto the tail of the string, push the $tmpstring onto the $haves,
kill the $tmpstring, turn the $flag "off", and return.
*/
$tmpstring .= ' ' . trim(ereg_replace('"', ' ', $pieces[$k]));
// Push the $tmpstring onto the array of stuff to search for
$objects[] = trim($tmpstring);
for ($j=0; $j<count($post_objects); $j++) {
$objects[] = $post_objects[$j];
}
unset($tmpstring);
// Turn off the flag to exit the loop
$flag = 'off';
}
}
}
}
// add default logical operators if needed
$temp = array();
for($i=0; $i<(count($objects)-1); $i++) {
$temp[] = $objects[$i];
if ( ($objects[$i] != 'and') &&
($objects[$i] != 'or') &&
($objects[$i] != '(') &&
($objects[$i+1] != 'and') &&
($objects[$i+1] != 'or') &&
($objects[$i+1] != ')') ) {
$temp[] = ADVANCED_SEARCH_DEFAULT_OPERATOR;
}
}
$temp[] = $objects[$i];
$objects = $temp;
$keyword_count = 0;
$operator_count = 0;
$balance = 0;
for($i=0; $i<count($objects); $i++) {
if ($objects[$i] == '(') $balance --;
if ($objects[$i] == ')') $balance ++;
if ( ($objects[$i] == 'and') || ($objects[$i] == 'or') ) {
$operator_count ++;
} elseif ( ($objects[$i]) && ($objects[$i] != '(') && ($objects[$i] != ')') ) {
$keyword_count ++;
}
}
if ( ($operator_count < $keyword_count) && ($balance == 0) ) {
return true;
} else {
return false;
}
}
////
// Check date
function zen_checkdate($date_to_check, $format_string, &$date_array) {
$separator_idx = -1;
$separators = array('-', ' ', '/', '.');
$month_abbr = array('jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec');
$no_of_days = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
$format_string = strtolower($format_string);
if (strlen($date_to_check) != strlen($format_string)) {
return false;
}
$size = sizeof($separators);
for ($i=0; $i<$size; $i++) {
$pos_separator = strpos($date_to_check, $separators[$i]);
if ($pos_separator != false) {
$date_separator_idx = $i;
break;
}
}
for ($i=0; $i<$size; $i++) {
$pos_separator = strpos($format_string, $separators[$i]);
if ($pos_separator != false) {
$format_separator_idx = $i;
break;
}
}
if ($date_separator_idx != $format_separator_idx) {
return false;
}
if ($date_separator_idx != -1) {
$format_string_array = explode( $separators[$date_separator_idx], $format_string );
if (sizeof($format_string_array) != 3) {
return false;
}
$date_to_check_array = explode( $separators[$date_separator_idx], $date_to_check );
if (sizeof($date_to_check_array) != 3) {
return false;
}
$size = sizeof($format_string_array);
for ($i=0; $i<$size; $i++) {
if ($format_string_array[$i] == 'mm' || $format_string_array[$i] == 'mmm') $month = $date_to_check_array[$i];
if ($format_string_array[$i] == 'dd') $day = $date_to_check_array[$i];
if ( ($format_string_array[$i] == 'yyyy') || ($format_string_array[$i] == 'aaaa') ) $year = $date_to_check_array[$i];
}
} else {
if (strlen($format_string) == 8 || strlen($format_string) == 9) {
$pos_month = strpos($format_string, 'mmm');
if ($pos_month != false) {
$month = substr( $date_to_check, $pos_month, 3 );
$size = sizeof($month_abbr);
for ($i=0; $i<$size; $i++) {
if ($month == $month_abbr[$i]) {
$month = $i;
break;
}
}
} else {
$month = substr($date_to_check, strpos($format_string, 'mm'), 2);
}
} else {
return false;
}
$day = substr($date_to_check, strpos($format_string, 'dd'), 2);
$year = substr($date_to_check, strpos($format_string, 'yyyy'), 4);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -