<?php
class
FilterTools {
public
static
$keyword
=
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
;
}
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
)));
}
}
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();
}
function
init(
$isReset
= false){
$this
->list =
$this
->memcache->get(
$this
->key)?
$this
->memcache->get(
$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'
);
}
}
function
getList(){
return
$this
->list;
}
}