Board logo

标题: 代码学习 [打印本页]

作者: Nothing    时间: 2007-7-10 09:52     标题: 代码学习

<?php
// 常位于函数文件的开头,禁止在url中直接访问该文件

if (eregi("function.php",$_SERVER['PHP_SELF'])) {
    Header("Location: ../index.php");
    die();
}


//返回安全字符串

function getSafeValue($value)
{
    if (! get_magic_quotes_gpc()) {
        return strip_tags(addslashes($value));
    } else {
        return strip_tags($value);
    }
}
/*
get_magic_quotes_gpc 函数说明

取得 PHP 环境变量 magic_quotes_gpc 的值。

本函数取得 PHP 环境配置的变量 magic_quotes_gpc (GPC, Get/Post/Cookie) 值。返回 0 表示关闭本功能;返回 1 表示本功能打开。当 magic_quotes_gpc 打开时,所有的 ' (单引号), " (双引号), \ (反斜线) and 空字符会自动转为含有反斜线的溢出字符。

参考资料: http://www.cycoo.net/study/php/function.php-get_magic_quotes_gpc.htm

*/



// 将prefix分成2部分,最后一位数字代表高度使用的CSS样式,其余的部分代表正则Pattern

// 常用于关键字高亮显示

function highlight(& $item, $key, $prefix) {   
    $prefix1 = preg_quote(substr($prefix, 0, strlen($prefix) -1), '/');
    $prefix2 = intval(substr($prefix, -1));
      $item = preg_replace("/$prefix1/is", "<span class='regSearch$prefix2'>\$0</span>", $item);
}


// 用于URL改写,把url中的问号'?'和与符号'&'改写成斜杆'/'、'+'、','、'-'等字符

function parse_url_path()
{
  /* 此方法仅用于apache服务器 */
  if(isset($_SERVER[PATH_INFO])) {//若PATH_INFO可用,注意apache2默认不可用

    $query_string = $_SERVER[PATH_INFO];
  } else {
    Return '';
  }
  //strip '.html','.htm'

  $query_string = str_replace('.html', '', $query_string);
  $query_string = str_replace('.htm', '', $query_string);  
  if(empty($query_string)) {
    Return '';
  } else {
    $args = split("/", substr($query_string,1)); //分裂成数组

    Return $args;
  }
}



//专用函数,只用于将brand按字母排序

function GroupLetter($products,$group,$join, $categories_name = '') {
  $out = array();
  if(! $products) {
        Return array();
  }
  foreach($products as $v) {
    $key   = $v[$group];
    $value = $v[$join];
    $out[$key][$join][] = "$value";
    $out[$key]['models_id'][] = $v['models_id'];
  }
  $tmp_category_path = getCategoryPath($categories_name);
  $filename = $tmp_category_path['modelsFile'];   
  $result = array();
  foreach($out as $k=>$v) {
    //定义brand的链接

    $numBrand = count($v[$join]);
    for ($i=0; $i<$numBrand; $i++) {
        $href[$k][] = "<a href=\"" . $filename . "/" . convertFileName($v['models_brand'][$i]) . '/' . $v['models_id'][$i] . REWRITE_FILE_EXT2 . "\" class=\"brand\">" . $v[$join][$i] . "</a>";
    }
    $result[] = array($group => $k, $join => $href[$k]);
   
  }
  return $result;
}


// 获得字符串的Ascii值

function AsciiCode($name) {
    if(empty($name)) {
        Return '';
    }
    $name = strval($name);
    $convert_name = '';
    $length = strlen($name);
    for($i=0; $i<$length; $i++) {
        $convert_name[] = ord($name[$i]);
    }
    $name = implode("", $convert_name);
    Return $name;
}


// 判断用户是否已经登录

function checkUser($keywords)
{
  $key  = $_SERVER['SERVER_NAME'] . dirname($_SERVER["REQUEST_URI"]);
  $pass = crc32(md5($key));
  $keywords = base64_decode($keywords);
  if($keywords == $pass)
  {
    Return true;
  } else
  {
      Return false;
  }

}


/**
*此函数对指定的一个二维数组$products中$group进行分类筛选,将相同$group的$join值连接在一起
*@Parameters:$products,$group,$join,其中$products包含了要操作的数据
*@Return:返回一个二维数组$result,数组包括两个键名:$group,$join
*/
function GroupBrand($products,$group,$join) {
  $out = array();
  if(! $products) {
        Return array();
  }
  foreach($products as $v) {
    $key   = strtoupper($v[$group]);
    $value = $v[$join];
    /*
    if(isset($out[$key]))
      $out[$key] .= ", $value";
    else
      $out[$key] = "$value";
   
    */
    $out[$key][] = "$value";
  }
  $result = array();
  foreach($out as $k=>$v)
    $result[] = array($group => $k, $join => $v);

  return $result;
}

/**
* 同上,但以,分隔
*/
function GroupBrand2($products,$group,$join) {
  $out = array();
  if(! $products) {
        Return array();
  }
  foreach($products as $v) {
    $key   = strtoupper($v[$group]);
    $value = $v[$join];
    if(isset($out[$key]))
      $out[$key] .= ",$value";
    else
      $out[$key] = "$value";
   
  }
  $result = array();
  foreach($out as $k=>$v)
    $result[] = array($group => $k, $join => $v);

  return $result;
}

/**
*此函数对指定的一个二维数组$products中$group进行筛选,将相同$group的$join值只留一个
*@Parameters:$products,$group,$join,其中$products包含了要操作的数据
*@Return:返回一个二维数组$result,数组包括两个键名:$group,$join
*/

function StripRepeated($products,$group,$join) {
  $out = array();
  if(!$products) {
        Return array();
  }
    foreach($products as $v) {
        $key   = $v[$group];
        $value = $v[$join];
        if(! isset($out[$key]))
        {
            $out[$key] = "$value";
        }
    }
    $result = array();
    foreach($out as $k=>$v)
      $result[] = array($group => $k, $join => $v);

    return $result;
}


/**
  * send a mail
  * this function only can send a mail without attachment file
  * it must be support by mimie class
  * @sendtype  => (smtp,mail);
  */
  function SendMail($to = SUPPORTER_EMAIL, $from = 'unnamed', $subject, $message, $sendtype = 'smtp')
  {
      global $class_mail;
    $domain = $_SERVER['REMOTE_ADDR'];
    $send_date = strftime(DATE_FORMAT_LONG);
    $send_time = strftime(DATE_TIME_FORMAT);
    if(empty($subject) || !isset($subject)) {
        $subject = "a email  from : $domain at " . $send_date . " " . $send_time;
    }
    $mailmessage  = $message . "<hr>this email is from :";
    $mailmessage .= $domain . " at " . $send_date . " " . $send_time;
    $text = $mailmessage;
    $html = $mailmessage;
    $class_mail->setHtml($html, $text);
    $class_mail->setReturnPath($to);
    $class_mail->setFrom($from);
    $class_mail->setBcc(CC_EMAIL);
    $class_mail->setSubject($subject);
    $result = $class_mail->send(array($to), $sendtype);
    if (! $result) {
      Show_Error_Message("Mail send error", $class_mail->errors);
    }
  }

// Redirect page

function Redirect_Page($url = "/") {
   $location="<script language=\"javascript\">window.location.href='" .$url. "';</script>";
    echo $location;
    return;
}


// Alert Window Message

function Alert_Message($message = "") {
   echo "<script language=javascript> alert('" .$message. "');</script>";
   return;
}


//Show user define information in window

function Show_Message($title, $message)
{
   $title = htmlspecialchars($title);
   $message = htmlspecialchars($message);
   $messageTable  = "<table border=\"0\" width=\"80%\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\">";
   $messageTable .= "<tr>";
   $messageTable .= "<td width=\"100%\" height=\"14\" class=\"infoBoxHeading\">";
   $messageTable .= $title;
   $messageTable .= "</td></tr></table>";
   $messageTable .= "<table border=\"0\" width=\"80%\" cellspacing=\"0\" cellpadding=\"1\" class=\"infoBox\" align=\"center\">";
   $messageTable .= "<tr>";
   $messageTable .= "<td><table border=\"0\" width=\"100%\" cellspacing=\"0\" cellpadding=\"3\" class=\"infoBoxContents\">";
   $messageTable .= "<tr><td><img src=\"/images/pixel_trans.gif\" border=\"0\" width=\"100%\" height=\"1\"></td></tr>";
   $messageTable .= "<tr><td align=\"center\" class=\"boxText\">";
   $messageTable .= $message;
   $messageTable .= "</td></tr>";
   $messageTable .= "<tr><td><img src=\"/images/pixel_trans.gif\" border=\"0\" width=\"100%\" height=\"1\"></td></tr></table></td></tr></table>";
   echo($messageTable);
   return;
}


/**
  * Show Error Message and Send a email to the supporter of the website.
  */

function Show_Error_Message($errorTitle, $errorMessage, $errorFile = __FILE__, $errorLine = __LINE__)
{
   $errorFile    = basename($errorFile);
   $errorTitle   = htmlspecialchars($errorTitle);
   $errorMessage = htmlspecialchars($errorMessage);
   $errorTable  = "<table border=\"0\" width=\"600\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\">";
   $errorTable .= "<tr>";
   $errorTable .= "<td width=\"100%\" height=\"14\" style=\"font-family:Verdana, Arial, sans-serif;font-size: 10px;  font-weight: bold;  background: #EBF3E6; color: #000000;\">  ";
   $errorTable .= $errorTitle;
   $errorTable .= "</td></tr></table>";
   $errorTable .= "<table border=\"0\" width=\"600\" cellspacing=\"0\" cellpadding=\"1\" style=\"background: #FFFFFF;  border: 1px solid #EBF3E6;  border-spacing: 0px;\" align=\"center\">";
   $errorTable .= "<tr>";
   $errorTable .= "<td><table border=\"0\" width=\"100%\" cellspacing=\"0\" cellpadding=\"3\" class=\"infoBoxContents\">";
   $errorTable .= "<tr><td><img src=\"/images/pixel_trans.gif\" border=\"0\" width=\"100%\" height=\"1\"></td></tr>";
   $errorTable .= "<tr><td align=\"center\" style=\"font: 11px Verdana, Arial, Helvetica, sans-serif;    color: #000000;\">";
   $errorTable .= "<strong>ERROR:</strong> $errorMessage";
   $errorTable .= "<p>Error occurred on line $errorLine of file $errorFile</p>";
   $errorTable .= "<P>An email had sent to the web supporter.</p>";
   $errorTable .= "<P><a href=\"/\"><img src=\"/images/cart/button_continue.gif\" border='0'></a></p>";
   $errorTable .= "</td></tr>";
   $errorTable .= "<tr><td><img src=\"/images/pixel_trans.gif\" border=\"0\" width=\"100%\" height=\"1\"></td></tr></table></td></tr></table>";
   echo($errorTable);
   $errorTable .= sprintf('<p>%s</p>', $_SERVER["REQUEST_URI"]);
   //send mail to supporter

   SendMail(WEBMASTER_EMAIL, $_SERVER['REMOTE_ADDR'], $errorTitle, $errorTable);
   //die();      

}

?>





欢迎光临 编程开发论坛 (http://bbs.lihuasoft.net/) Powered by Discuz! 6.0.0