<?php
/**
* Created by JetBrains phpstorm.
* USER:LSL
* date:13-8-28
* Time: Afternoon 2:58
* Sensitive word Filter tool class
* Use method
* echo filtertools::filtercontent (" your mother i fuck color wolf hybrid two mountain food ","*",dir." ; config/word.txt" $GLOBALS ["p_memcache"] ["bad_words"]);
*/
class Filtertools {
Public static $keyword = Array ();
/**
* Load sensitive words from a file
* @param $filename
* @return Array
*/
static function Getbadwords ($filename) {
$file _handle = fopen ($filename, "r");
while (!feof ($file _handle)) {
$line = Trim (fgets ($file _handle));
Array_push (self:: $keyword, $line);
}
fclose ($file _handle);
return self:: $keyword;
}
/**
* @param $content to be processed string
* @param $target replaced character
* @param $filename Sensitive word configuration file
* @param $memconfig cache configuration file
* @return The processed string
*/
static function Filtercontent ($content, $target, $filename, $memconfig) {
$mem = new Badwordsmemcache ($filename, $memconfig);
$keyword = $mem->getlist ();
if (count ($keyword) = 0) {
$keyword = self::getbadwords ($filename);
}
return strtr ($content, Array_combine ($keyword, Array_fill (0,count ($keyword), $target ));
}
}
/**
* Sensitive word Cache processing class
* Class badwordsmemcache
* USER:LSL
*/
class badwordsmemcache{
var $memcache;
var $key;
var $list;
var $filename;
function __construct ($filename, $memconfig) {
$this->filename = $filename;
if (!class_exists " p_memcache")) {
require_once dir."lib/memcache.class.php";
}
$this->key = "bad_words";
$this->memcache = new P_memcache ();
$this->memcache->config = $memconfig;
$this->memcache->connect ();
Print_r ($this->memcache);
$this->init ();
}
function __destruct () {
$this->memcache->close ();
}
/**
* Initialization
* @param bool $isReset
*/
function init ($isReset = False) {
$this->list = $this->memcache->get ($this->key)? $this->memcache->ge T ($this->key): Array ();
if (count ($this->list) ==0 | | $isReset) {
$this->list = filtertools::getbadwords ($this->filename);
$this->memcache->set ($this->key, $this->list);
$log _data = log::formatdata ($this->list);
Log::logwrite ($log _data, ' bad.words ', ' init ');
}
}
/**
* Get list
* @return Mixed
*/
function GetList () {
return $this->list;
}
}