Browse Source

Merge branch 'senstive_word' into sess_0306

stanley-king 9 years ago
parent
commit
587997383e

+ 1 - 0
data/model/member.model.php

@@ -99,6 +99,7 @@ class memberModel extends Model
         $_SESSION['member_email'] = $member_info['member_email'];
         $_SESSION['is_buy'] = isset($member_info['is_buy']) ? $member_info['is_buy'] : 1;
         $_SESSION['avatar'] = $member_info['member_avatar'];
+        $_SESSION['member_avatar'] = $member_info['member_avatar'];
         $_SESSION['member_truename'] = $member_info['member_truename'];
         $_SESSION['member_nickname'] = $member_info['member_nickname'];
         $_SESSION['member_signname'] = $member_info['member_signname'];

File diff suppressed because it is too large
+ 14594 - 0
data/resource/sensitive_word/dictionary.txt


+ 6 - 4
fooder.php

@@ -8,6 +8,7 @@ define('BASE_MOBILE_PATH',BASE_ROOT_PATH.'/mobile');
 define('BASE_CRONTAB_PATH',BASE_ROOT_PATH.'/crontab');
 define('BASE_UPLOAD_PATH',BASE_DATA_PATH.'/upload');
 define('BASE_UTIL_PATH',BASE_ROOT_PATH.'/util');
+define('BASE_AVATAR_PATH',BASE_UPLOAD_PATH.'/shop/avatar');
 
 require_once(BASE_ROOT_PATH . '/global.php');
 require_once(BASE_DATA_PATH . '/config/config.ini.php');
@@ -32,7 +33,8 @@ require_once(BASE_CORE_PATH . '/framework/function/goods.php');
 require_once(BASE_CORE_PATH . '/framework/libraries/validate.php');
 require_once(BASE_CORE_PATH . '/framework/libraries/resizeimage.php');
 
-require_once (BASE_ROOT_PATH . '/helper/performance_helper.php');
-require_once (BASE_ROOT_PATH . '/helper/request_helper.php');
-require_once (BASE_ROOT_PATH . '/helper/configure.php');
-?>
+require_once(BASE_ROOT_PATH . '/helper/performance_helper.php');
+require_once(BASE_ROOT_PATH . '/helper/sensitive_word/dfa.php');
+require_once(BASE_ROOT_PATH . '/helper/request_helper.php');
+require_once(BASE_ROOT_PATH . '/helper/configure.php');
+?>

+ 3 - 0
helper/fcgi_server.php

@@ -50,6 +50,9 @@ class fcgi_server
 
     public function run_looper()
     {
+        //载入敏感词词库
+        DFAFilter::instance();
+
         require_once(BASE_ROOT_PATH.'/mobile/index.php');
         Base::mobile_init();
 

+ 68 - 0
helper/file_upload.php

@@ -0,0 +1,68 @@
+<?php
+
+class FileUpload
+{
+    static public function recursive_makedir($path)
+    {
+        return is_dir($path) or (self::recursive_makedir(dirname($path)) and @mkdir($path));
+    }
+
+    static public function upload_normal($src_file,$dest_dir,&$file_path)
+    {
+        if(!file_exists($src_file)) {
+            return false;
+        }
+
+        $file_name = strtolower(pathinfo($src_file)['basename']);
+        $sha1 = sha1_file($src_file);
+
+        return self::do_upload($src_file,$sha1,$file_name,$dest_dir,$file_path);
+    }
+
+    static public function upload_nginx($dest_dir,&$file_path)
+    {
+        $tmppath = $_POST["file_path"];
+        $sha1 = $_POST["file_sha1"];
+        $file_name = $_POST["file_name"];
+
+        return self::do_upload($tmppath,$sha1,$file_name,$dest_dir,$file_path);
+    }
+
+    static private function do_upload($src_path,$file_sig,$file_name,$dest_dir,&$file_path)
+    {
+        if (!isset($src_path) || empty($src_path)) {
+            return false;
+        }
+        if(!file_exists($src_path)) {
+            return false;
+        }
+        if (!isset($file_sig) || empty($file_sig)) {
+            @unlink($src_path);
+            return false;
+        }
+
+        if (!isset($dest_dir) || empty($dest_dir)) {
+            @unlink($src_path);
+            return false;
+        }
+
+        $ext_name = strtolower(pathinfo($file_name)['extension']);
+        $timestamp = date("YmdHis", time());
+        $dest_file = $dest_dir . "/" . substr($timestamp, 0, 4) . "/" . substr($file_sig, 0, 2) . "/" . $file_sig . '.' . $ext_name;
+
+        if (!self::recursive_makedir(dirname($dest_file))) {
+            @unlink($src_path);
+            return false;
+        }
+
+        if (!copy($src_path, $dest_file)) {
+            @unlink($src_path);
+            return false;
+        } else {
+            $file_path = $dest_file;
+            return true;
+        }
+    }
+}
+
+?>

+ 0 - 15
helper/sensitive/DFAFilter.php

@@ -1,15 +0,0 @@
-<?php
-
-/**
- * Created by PhpStorm.
- * User: stanley-king
- * Date: 16/5/8
- * Time: 下午1:49
- */
-
-namespace sensitive_word;
-
-class DFAFilter
-{
-
-}

+ 0 - 95
helper/sensitive/DFAItem.php

@@ -1,95 +0,0 @@
-<?php
-/**
- * Created by PhpStorm.
- * User: stanley-king
- * Date: 16/5/8
- * Time: 下午1:50
- */
-
-namespace sensitive_word;
-
-class DFAItem
-{
-    private $word = null;
-    private $sub_items = array();
-    private $is_end = 0;
-
-    public function __set($name, $value)
-    {
-        $this->$name = $value;
-    }
-
-    public function __get($name)
-    {
-        return $this->$name;
-    }
-
-    // 初始化
-    public function init($word)
-    {
-        $cnt = count($word);
-        if ($cnt <= 0) {
-            return null;
-        } else if ($cnt === 1) {
-            $this->is_end = 1;
-            $this->word = $word[0];
-            $this->sub_items = null;
-        } else {
-            $this->is_end = 0;
-            $this->word = $word[0];
-            $this->add_item(array_slice($word, 1));
-        }
-        return $this;
-    }
-
-    // 添加子节点内容
-    public function add_item($word)
-    {
-        $subitem = new DFAItem();
-        $ret = $subitem->init($word);
-        if (!is_null($ret)) {
-            array_push($this->sub_items, $subitem);
-        }
-    }
-
-    // 向链表中添加内容
-    public function addwords($word)
-    {
-        $found = false;
-        foreach ($this->sub_items as $item) {
-            if (0 == strcmp($word[0], $item->word)) {
-                $item->addwords(array_slice($word, 1));
-                $found = true;
-            }
-        }
-
-        if (!$found) {
-            $subitem = new DFAItem();
-            $subitem->init($word);
-            array_push($this->sub_items, $subitem);
-        }
-    }
-
-    // 判断关键字是否在属于此item
-    public function checkword($txt)
-    {
-        if (is_null($txt)) {
-            return false;
-        }
-        $head = mb_substr($txt, 0, 1);
-        $found = false;
-        foreach ($this->sub_items as $item) {
-
-            if (0 == strcmp($item->word, $head)) {
-                if ($item->is_end == 1) {
-                    return true;
-                } else {
-                    return $item->checkword(mb_substr($txt, 1));
-                }
-            }
-        }
-        if (!$found) {
-            return false;
-        }
-    }
-}

+ 0 - 64
helper/sensitive/SensitiveWordInit.php

@@ -1,64 +0,0 @@
-<?php
-
-/**
- * 初始化敏感词库<br>
- * 将敏感词加入到HashMap中<br>
- * 构建DFA算法模型
- *
- * @author dxm
- *
- */
-
-namespace sensitive_word;
-
-require_once(BASE_ROOT_PATH . "/helper/sensitive/DFAItem.php");
-
-class SensitiveWordInit
-{
-    // 字符编码
-    const ENCODING = "UTF-8";
-
-    /**
-     * 初始化敏感字库
-     *
-     * @return
-     */
-    public function initKeyWord()
-    {
-        $word_array = $this->readSensitiveWordFile();
-        return $this->addSensitiveWordToHashMap($word_array);
-    }
-
-    /**
-     * 读取敏感词库,将敏感词放入HashSet中,构建一个DFA算法模型:<br>
-     * 中 = { isEnd = 0 国 = {<br>
-     * isEnd = 1 人 = {isEnd = 0 民 = {isEnd = 1} } 男 = { isEnd = 0 人 = { isEnd =
-     * 1 } } } } 五 = { isEnd = 0 星 = { isEnd = 0 红 = { isEnd = 0 旗 = { isEnd = 1
-     * } } } }
-     *
-     */
-    public function addSensitiveWordToHashMap($words)
-    {
-        $dfa = new DFAItem();
-        foreach ($words as $word) {
-            $dfa->addwords(explode(" ",$word));
-        }
-        return $dfa;
-    }
-
-    /**
-     * 读取敏感词库中的内容,将内容添加到array中
-     *
-     * @return
-     * @throws Exception
-     */
-    private function readSensitiveWordFile()
-    {
-        $word_array = array();
-        array_push($word_array, '中 国');
-        array_push($word_array, '中 央');
-        array_push($word_array, '国 家');
-        array_push($word_array, '他 妈 的');
-        return $word_array;
-    }
-}

+ 0 - 112
helper/sensitive/SensitivewordFilter.php

@@ -1,112 +0,0 @@
-<?php
-
-/**
- * 敏感词过滤
- *
- * @author dxm
- *
- */
-
-namespace sensitive_word;
-
-require_once(BASE_ROOT_PATH . "/helper/sensitive/DFAItem.php");
-
-class SensitivewordFilter
-{
-
-    private $dfa = null;
-
-    // 最小匹配规则
-    public static $minMatchTYpe = 1;
-
-    // 最大匹配规则
-    public static $maxMatchType = 2;
-
-    // 单例
-    private static $inst = null;
-
-    /**
-     * 构造函数,初始化敏感词库
-     */
-    function __construct()
-    {
-    }
-
-
-    /**
-     * 获取单例
-     *
-     * @return
-     */
-    public static function getInstance()
-    {
-        if (null == self::$inst) {
-            self::$inst = new SensitivewordFilter();
-            self::$inst->dfa = (new \sensitive_word\SensitiveWordInit())->initKeyWord();
-        }
-        return self::$inst;
-    }
-
-    /**
-     * 判断文字是否包含敏感字符
-     *
-     * @param txt
-     * @param matchType
-     * @return
-     */
-    public function isContaintSensitiveWord($txt)
-    {
-        return $this->dfa->checkword($txt);
-    }
-
-    /**
-     * 获取文字中的敏感词
-     *
-     * @param txt
-     * @param matchType
-     * @return
-     */
-    public function getSensitiveWord($txt, $matchType)
-    {
-
-    }
-
-    /**
-     * 替换敏感字字符
-     *
-     * @param txt
-     * @param matchType
-     * @param replaceChar
-     * @return
-     */
-    public function  replaceSensitiveWord($txt, $matchType, $replaceChar)
-    {
-
-    }
-
-    /**
-     * 获取替换字符串
-     *
-     * @param replaceChar
-     * @param length
-     * @return
-     */
-    private function getReplaceChars($replaceChar, $length)
-    {
-    }
-
-    /**
-     * 检查文字中是否包含敏感字符,检查规则如下:<br>
-     * 如果存在,则返回敏感词字符的长度,不存在返回0
-     *
-     * @param txt
-     * @param beginIndex
-     * @param matchType
-     * @return
-     */
-    public function CheckSensitiveWord($txt, $beginIndex, $matchType)
-    {
-
-    }
-
-}

+ 134 - 0
helper/sensitive_word/dfa.php

@@ -0,0 +1,134 @@
+<?php
+
+class DFAFilter
+{
+    static private $stInstance = NULL;
+    private $dict;
+    const dictionary_path = BASE_RESOURCE_PATH . '/sensitive_word/dictionary.txt';
+	
+    static public function instance()
+    {
+        if(self::$stInstance == NULL) {
+            self::$stInstance = new DFAFilter();
+        }
+        return self::$stInstance;
+    }
+	
+    private function __construct()
+    {
+        $this->loadFile();
+    }
+
+    private function loadFile()
+    {
+        $this->dict = array();
+        $this->initDict();
+    }
+
+    private function initDict()
+    {
+        $handle = fopen(self::dictionary_path, 'r');
+        if (!$handle) {
+            $path = self::dictionary_path;
+            throw new RuntimeException("cannot open sensitive_word dictionary file ={$path}.");
+        }
+
+        while (!feof($handle)) {
+            $word = trim(fgets($handle, 128));
+
+            if (empty($word)) {
+                continue;
+            }
+
+            $uWord = $this->unicodeSplit($word);
+            $pdict = &$this->dict;
+
+            $count = count($uWord);
+            for ($i = 0; $i < $count; $i++) {
+                if (!isset($pdict[$uWord[$i]])) {
+                    $pdict[$uWord[$i]] = array();
+                }
+                $pdict = &$pdict[$uWord[$i]];
+            }
+
+            $pdict['end'] = true;
+        }
+
+        fclose($handle);
+    }
+
+    public function filter($str, $maxDistance = 10)
+    {
+        if ($maxDistance < 1) {
+            $maxDistance = 1;
+        }
+
+        $uStr = $this->unicodeSplit($str);
+        $count = count($uStr);
+
+        for ($i = 0; $i < $count; $i++) {
+            if (isset($this->dict[$uStr[$i]])) {
+                $pdict = &$this->dict[$uStr[$i]];
+
+                $matchIndexes = array();
+                for ($j = $i + 1, $d = 0; $d < $maxDistance && $j < $count; $j++, $d++) {
+                    if (isset($pdict[$uStr[$j]])) {
+                        $matchIndexes[] = $j;
+                        $pdict = &$pdict[$uStr[$j]];
+                        $d = -1;
+                    }
+                }
+
+                if (isset($pdict['end'])) {
+                    $uStr[$i] = '*';
+                    foreach ($matchIndexes as $k) {
+                        if ($k - $i == 1) {
+                            $i = $k;
+                        }
+                        $uStr[$k] = '*';
+                    }
+                }
+            }
+        }
+
+        return implode('',$uStr);
+    }
+
+    public function unicodeSplit($str)
+    {
+        $str = strtolower($str);
+        $ret = array();
+        $len = strlen($str);
+        for ($i = 0; $i < $len; $i++) {
+            $c = ord($str[$i]);
+
+            if ($c & 0x80) {
+                if (($c & 0xf8) == 0xf0 && $len - $i >= 4) {
+                    if ((ord($str[$i + 1]) & 0xc0) == 0x80 && (ord($str[$i + 2]) & 0xc0) == 0x80 && (ord($str[$i + 3]) & 0xc0) == 0x80) {
+                        $uc = substr($str, $i, 4);
+                        $ret[] = $uc;
+                        $i += 3;
+                    }
+                } else if (($c & 0xf0) == 0xe0 && $len - $i >= 3) {
+                    if ((ord($str[$i + 1]) & 0xc0) == 0x80 && (ord($str[$i + 2]) & 0xc0) == 0x80) {
+                        $uc = substr($str, $i, 3);
+                        $ret[] = $uc;
+                        $i += 2;
+                    }
+                } else if (($c & 0xe0) == 0xc0 && $len - $i >= 2) {
+                    if ((ord($str[$i + 1])  & 0xc0) == 0x80) {
+                        $uc = substr($str, $i, 2);
+                        $ret[] = $uc;
+                        $i += 1;
+                    }
+                }
+            } else {
+                $ret[] = $str[$i];
+            }
+        }
+
+        return $ret;
+    }
+}
+
+

+ 9 - 1
helper/text_filter.php

@@ -21,9 +21,17 @@ class text_filter
 
         return $input;
     }
+
     static public function filter_input($input)
     {
         $input = self::filter_html($input);
         return $input;
     }
-}
+
+    //过滤敏感词
+    static public function filter_sensitive_word($input)
+    {
+        $input = self::filter_html($input);
+        return DFAFilter::instance()->filter($input);
+    }
+}

+ 45 - 2
mobile/control/member_info.php

@@ -4,9 +4,11 @@
  ***/
 defined('InShopNC') or exit('Access Invalid!');
 require_once (BASE_ROOT_PATH . '/helper/text_filter.php');
+require_once (BASE_ROOT_PATH . '/helper/file_upload.php');
 
 class member_infoControl extends mbMemberControl
 {
+
     public function __construct()
     {
         parent::__construct();
@@ -19,7 +21,7 @@ class member_infoControl extends mbMemberControl
 
     public function getOp()
     {
-        $fields = array('member_sex','member_nickname','member_truename','member_signname','member_birthday','member_mobile');
+        $fields = array('member_sex','member_nickname','member_truename','member_signname','member_birthday','member_mobile','member_avatar');
 
         $ret = array();
         foreach($fields as $val)
@@ -30,7 +32,24 @@ class member_infoControl extends mbMemberControl
                     $time = $_SESSION[$val];
                     $birthday = strtotime($time);
                     $ret[$val] = ($birthday == false) ? '' : $birthday;
-                } else {
+                }
+                elseif($val == 'member_avatar')
+                {
+                    $path = $_SESSION[$val];
+                    if(empty($path)) {
+                        $ret['member_avatar'] = "";
+                    }
+                    else
+                    {
+                        if(strncasecmp($path,"http://",strlen("http://")) == 0) {
+                            $ret['member_avatar'] = $path;
+                        } else {
+                            $url = UPLOAD_SITE_URL . "/shop/avatar/{$path}";
+                            $ret['member_avatar'] = $url;
+                        }
+                    }
+                }
+                else {
                     $ret[$val] = $_SESSION[$val];
                 }
             }
@@ -39,6 +58,30 @@ class member_infoControl extends mbMemberControl
         self::outsuccess($ret);
     }
 
+    public function upavatarOp()
+    {
+        $result = FileUpload::upload_nginx(BASE_AVATAR_PATH,$file_path);
+        if($result == false) {
+            return self::outerr(errcode::ErrUploadFileFailed);
+        }
+
+        $file_path = str_replace(BASE_AVATAR_PATH, '', $file_path);
+        $member_id = $_SESSION['member_id'];
+        if (isset($member_id))
+        {
+            $ret = Model("member")->editMember(array('member_id' => $member_id),array('member_avatar' => $file_path));
+            if ($ret) {
+                $_SESSION['member_avatar'] = $file_path;
+                return self::outsuccess(NULL);
+            } else {
+                return self::outerr(errcode::ErrDB);
+            }
+        }
+        else {
+            return self::outerr(errcode::ErrUploadFileFailed);
+        }
+    }
+
     public function updateinfoOp()
     {
         $this->editOp();

+ 2 - 0
mobile/util/errcode.php

@@ -28,6 +28,8 @@ class errcode extends SplEnum
 
     const ErrFrequentlyRequest = 10017;//请求频繁
 
+    const ErrUploadFileFailed = 10018; //上传文件失败
+
     const ErrSpecial = 10100;
 
     // 购物车模块(10200-10299)