功能:一个censor审核提交字符串的类(附加在DEDECMS里面)
两份不同的作品,同样的一种功能,大家评比一下~认为他们的水平如何?
如果你是主考官,你会如何选择?为什么?
涉及MySQL数据表:gk_info_censormysql> desc gk_info_censor;
+-------------+----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+----------------------+------+-----+---------+----------------+
| id | smallint(6) unsigned | NO | PRI | NULL | auto_increment |
| admin | varchar(15) | NO | | | |
| type | smallint(6) | NO | | 1 | |
| find | varchar(255) | NO | UNI | | |
| replacement | varchar(255) | NO | | | |
| extra | varchar(255) | NO | | | |
| count | int(11) | NO | | 0 | |
| updatetime | int(11) | NO | | NULL | |
| tids | mediumtext | NO | | NULL | |
| enable | tinyint(1) | NO | | -1 | |
+-------------+----------------------+------+-----+---------+----------------+
10 rows in set (0.01 sec)1.censor.class1.php<?php/* censorStatus: {banned} {censor} {replace} */class Censor
{
public $censorCount;
public $censorData;
public $censorLimit; function __construct()
{
$this->censorCount = NULL;
$this->censorCount->replace = 0;
$this->censorCount->censor = 0;
$this->censorCount->Banned = 0;
$this->censorLimit = 120;
$this->censorData = $this->GetCensorData();
} /*
select * from
*/
function GetCensorData()
{
static $censorData = NULL;
if($censorData !== NULL) return $censorData; $censorData->banned = null;
$censorData->censor = null;
$censorData->replace= null;
$censorData->repalce->from = $censorData->replace->to = null; global $dsql;
$sql = "SELECT * FROM gk_info_censor WHERE replacement!='' AND `enable`=1 ";
$dsql->Execute('s',$sql);
while ($row = $dsql->GetArray('s'))
{
switch ($row['replacement'])
{
case '{banned}': $censorData->banned[] = $row['find']; break;//banned
case '{censor}': $censorData->censor[] = $row['find']; break;//censor
default: $censorData->replace->from[] = $row['find'];//replace
$censorData->replace->to[] = $row['replacement'];
break;
}
}
return $censorData;
} function check( &$string )
{
if($this->banned( $string) < 1)
{
if($this->censor( $string ) < 1)
{
$this->replace( $string );
}
}
} //替换级别 返回替换匹配个数
function replace(&$subject)
{
if(empty($this->censorData->replace->from) || empty($this->censorData->replace->to)) return 0; $i = 0;
while ($arrFROM = array_slice($this->censorData->replace->find,$i,$this->censorLimit))
{
$i += $this->censorLimit;
if(empty($arrFrom)) continue;
$arrTo = array_slice($this->censorData->replace->to,$i,$this->censorLimit);
#$subject = preg_replace($arrFrom, $arrTo, $subject);
array_map('str_replace',$arrFrom,$arrTo,$subject);
}
} //审核级别 返回审核匹配个数
function censor(&$subject)
{
if(empty($this->censorData->censor)) return 0; $i = $count = 0;
while ($arr = array_slice($this->censorData->censor,$i,$this->censorLimit))
{
preg_match_all('#'.split('|',$arr).'#', $subject, $matches);
if($matches) $count += count($matches[0]);
$i += $this->censorLimit;
}
$this->censorCount->banned += $count;
return $count;
} //彻底禁止 返回禁止匹配个数
function banned(&$subject)
{
if(empty($this->censorData->banned)) return 0; $i = $count = 0;
while($arr = array_slice($this->censorData->banned,$i,$this->censorLimit))
{
preg_match_all('#'.split('|',$arr).'#', $subject, $matches);
if($matches) $count += count($matches[0]);
$i+= $this->censorLimit;
}
$this->censorCount->banned += $count;
return $count;
} //审核级别 banned,censor,pass
function censorLevel()
{
if($this->censorCount->banned > 0 ) return 'banned';
if($this->censorCount->censor > 0 ) return 'censor';
return 'pass';
}
}2.censor.class2.php
<?php/* censorStatus: {banned} {censor} {replace} */class Censor
{
public $censorCount_banned;
public $censorCount_censor;
public $censorCount_replace;
public $censorData;
public $censorLimit;
public $cachePath; function __construct()
{
$this->censorCount_replace = 0;
$this->censorCount_censor = 0;
$this->censorCount_banned = 0;
$this->censorLimit = 120;
$this->cachePath = DEDEDATA.'/cache_censor.php';
$this->censorData = $this->GetCensorData();
}
function GetCensorData()
{
if(defined('CENSORDATA_LOADED'))
{
if(empty($censorData)) ShowMsg("ERROR:CENSOR数据不存在!");
return $censorData;
} elseif( file_exists($this->cachePath) && filesize($this->cachePath) > 1024) {
include $this->cachePath;
if(empty($this->cachePath)) ShowMsg('Error: censor cache file size error!');
return $censorData;
} $censorData = array();
$censorData['banned'] = array();
$censorData['censor'] = array();
$censorData['repalce']= array(); global $dsql;
$sql = "SELECT * FROM gk_info_censor WHERE replacement!='' AND `enable`=1 ";
$dsql->Execute('s',$sql);
while ($row = $dsql->GetArray('s'))
{
switch ($row['replacement'])
{
case '{banned}': $censorData[banned][] = $row['find']; break;//banned
case '{censor}': $censorData[censor][] = $row['find']; break;//censor
default: $censorData[replace][$row['find']]=$row['replacement'];break;//replace
}
} #$cachePath = DEDEDATA.'/cache_censor.php';
$cacheCode = "<"."?php\n";
$cacheCode .= "\n#Make Time:".date("Y-m-d H:i:s");
$cacheCode .= "\n#Make File:".__FILE__."(Line:".__LINE__.")";
$cacheCode .= "\n#notice! do not modify me!\n\n"; $cacheCode .= "\ndefine('CENSORDATA_LOADED',TRUE);";
$cacheCode .= "\n\$censorData=array();";
$cacheCode .= "\n\$censorData[banned] = null;";
$cacheCode .= "\n\$censorData[censor] = null;";
$cacheCode .= "\n\$censorData[replace]= null;"; $cacheCode .= $this->makecode($censorData[banned],'censorData[banned]');
$cacheCode .= $this->makecode($censorData[censor],'censorData[censor]');
foreach ($censorData[replace] as $k => $v) {
$cacheCode .= "\n\$censorData[replace]['".addslashes($k)."']= '".addslashes ($v)."';";
}
$cacheCode .= "\n\n?".">"; $fp = @fopen($this->cachePath,"w") or die(" Tag Engine Create File FALSE! ");
fwrite($fp,$cacheCode);
fclose($fp);
return $censorData;
} function makecode($data,$varname)
{
$i=0;
$codestring = '';
while($arr = array_slice($data,$i,$this->censorLimit,true))
{
$codestring .= "\n\$".$varname."[] = '/". addcslashes( implode('|',$arr),"{}[]/.()'\"*" )."/i';";
$i += $this->censorLimit;
}
return $codestring;
} function check( &$string )
{
$this->banned($string);
$this->censor($string);
$this->replace($string);
/*
if($this->banned( $string) < 1)
{
if($this->censor( $string ) < 1)
{
$this->replace( $string );
}
}
*/
} //替换级别 返回替换匹配个数
function replace(&$subject)
{
if(empty($this->censorData[replace])) return 0; foreach($this->censorData[replace] as $from => $to)
{
$subject = str_replace($from,$to,$subject);
}
} //审核级别 返回审核匹配个数
function censor(&$subject)
{
if(empty($this->censorData[censor])) return 0; $count = 0;
foreach($this->censorData[censor] as $pattern)
{
preg_match_all($pattern, $subject, $matches);
if($matches) $count += count($matches[0]);
}
$this->censorCount_censor += $count;
return $count;
} //彻底禁止 返回禁止匹配个数
function banned(&$subject)
{
if(empty($this->censorData[banned])) return 0; $count = 0;
foreach($this->censorData[banned] as $pattern)
{
preg_match_all($pattern, $subject, $matches);
if($matches) $count += count( $matches[0] );
//print_r($matches);
}
$this->censorCount_banned += $count;
return $count;
} //审核级别 banned,censor,pass
function censorLevel()
{
if($this->censorCount_banned > 0 ) return 'banned';
if($this->censorCount_censor > 0 ) return 'censor';
return 'pass';
}
}
两份不同的作品,同样的一种功能,大家评比一下~认为他们的水平如何?
如果你是主考官,你会如何选择?为什么?
涉及MySQL数据表:gk_info_censormysql> desc gk_info_censor;
+-------------+----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+----------------------+------+-----+---------+----------------+
| id | smallint(6) unsigned | NO | PRI | NULL | auto_increment |
| admin | varchar(15) | NO | | | |
| type | smallint(6) | NO | | 1 | |
| find | varchar(255) | NO | UNI | | |
| replacement | varchar(255) | NO | | | |
| extra | varchar(255) | NO | | | |
| count | int(11) | NO | | 0 | |
| updatetime | int(11) | NO | | NULL | |
| tids | mediumtext | NO | | NULL | |
| enable | tinyint(1) | NO | | -1 | |
+-------------+----------------------+------+-----+---------+----------------+
10 rows in set (0.01 sec)1.censor.class1.php<?php/* censorStatus: {banned} {censor} {replace} */class Censor
{
public $censorCount;
public $censorData;
public $censorLimit; function __construct()
{
$this->censorCount = NULL;
$this->censorCount->replace = 0;
$this->censorCount->censor = 0;
$this->censorCount->Banned = 0;
$this->censorLimit = 120;
$this->censorData = $this->GetCensorData();
} /*
select * from
*/
function GetCensorData()
{
static $censorData = NULL;
if($censorData !== NULL) return $censorData; $censorData->banned = null;
$censorData->censor = null;
$censorData->replace= null;
$censorData->repalce->from = $censorData->replace->to = null; global $dsql;
$sql = "SELECT * FROM gk_info_censor WHERE replacement!='' AND `enable`=1 ";
$dsql->Execute('s',$sql);
while ($row = $dsql->GetArray('s'))
{
switch ($row['replacement'])
{
case '{banned}': $censorData->banned[] = $row['find']; break;//banned
case '{censor}': $censorData->censor[] = $row['find']; break;//censor
default: $censorData->replace->from[] = $row['find'];//replace
$censorData->replace->to[] = $row['replacement'];
break;
}
}
return $censorData;
} function check( &$string )
{
if($this->banned( $string) < 1)
{
if($this->censor( $string ) < 1)
{
$this->replace( $string );
}
}
} //替换级别 返回替换匹配个数
function replace(&$subject)
{
if(empty($this->censorData->replace->from) || empty($this->censorData->replace->to)) return 0; $i = 0;
while ($arrFROM = array_slice($this->censorData->replace->find,$i,$this->censorLimit))
{
$i += $this->censorLimit;
if(empty($arrFrom)) continue;
$arrTo = array_slice($this->censorData->replace->to,$i,$this->censorLimit);
#$subject = preg_replace($arrFrom, $arrTo, $subject);
array_map('str_replace',$arrFrom,$arrTo,$subject);
}
} //审核级别 返回审核匹配个数
function censor(&$subject)
{
if(empty($this->censorData->censor)) return 0; $i = $count = 0;
while ($arr = array_slice($this->censorData->censor,$i,$this->censorLimit))
{
preg_match_all('#'.split('|',$arr).'#', $subject, $matches);
if($matches) $count += count($matches[0]);
$i += $this->censorLimit;
}
$this->censorCount->banned += $count;
return $count;
} //彻底禁止 返回禁止匹配个数
function banned(&$subject)
{
if(empty($this->censorData->banned)) return 0; $i = $count = 0;
while($arr = array_slice($this->censorData->banned,$i,$this->censorLimit))
{
preg_match_all('#'.split('|',$arr).'#', $subject, $matches);
if($matches) $count += count($matches[0]);
$i+= $this->censorLimit;
}
$this->censorCount->banned += $count;
return $count;
} //审核级别 banned,censor,pass
function censorLevel()
{
if($this->censorCount->banned > 0 ) return 'banned';
if($this->censorCount->censor > 0 ) return 'censor';
return 'pass';
}
}2.censor.class2.php
<?php/* censorStatus: {banned} {censor} {replace} */class Censor
{
public $censorCount_banned;
public $censorCount_censor;
public $censorCount_replace;
public $censorData;
public $censorLimit;
public $cachePath; function __construct()
{
$this->censorCount_replace = 0;
$this->censorCount_censor = 0;
$this->censorCount_banned = 0;
$this->censorLimit = 120;
$this->cachePath = DEDEDATA.'/cache_censor.php';
$this->censorData = $this->GetCensorData();
}
function GetCensorData()
{
if(defined('CENSORDATA_LOADED'))
{
if(empty($censorData)) ShowMsg("ERROR:CENSOR数据不存在!");
return $censorData;
} elseif( file_exists($this->cachePath) && filesize($this->cachePath) > 1024) {
include $this->cachePath;
if(empty($this->cachePath)) ShowMsg('Error: censor cache file size error!');
return $censorData;
} $censorData = array();
$censorData['banned'] = array();
$censorData['censor'] = array();
$censorData['repalce']= array(); global $dsql;
$sql = "SELECT * FROM gk_info_censor WHERE replacement!='' AND `enable`=1 ";
$dsql->Execute('s',$sql);
while ($row = $dsql->GetArray('s'))
{
switch ($row['replacement'])
{
case '{banned}': $censorData[banned][] = $row['find']; break;//banned
case '{censor}': $censorData[censor][] = $row['find']; break;//censor
default: $censorData[replace][$row['find']]=$row['replacement'];break;//replace
}
} #$cachePath = DEDEDATA.'/cache_censor.php';
$cacheCode = "<"."?php\n";
$cacheCode .= "\n#Make Time:".date("Y-m-d H:i:s");
$cacheCode .= "\n#Make File:".__FILE__."(Line:".__LINE__.")";
$cacheCode .= "\n#notice! do not modify me!\n\n"; $cacheCode .= "\ndefine('CENSORDATA_LOADED',TRUE);";
$cacheCode .= "\n\$censorData=array();";
$cacheCode .= "\n\$censorData[banned] = null;";
$cacheCode .= "\n\$censorData[censor] = null;";
$cacheCode .= "\n\$censorData[replace]= null;"; $cacheCode .= $this->makecode($censorData[banned],'censorData[banned]');
$cacheCode .= $this->makecode($censorData[censor],'censorData[censor]');
foreach ($censorData[replace] as $k => $v) {
$cacheCode .= "\n\$censorData[replace]['".addslashes($k)."']= '".addslashes ($v)."';";
}
$cacheCode .= "\n\n?".">"; $fp = @fopen($this->cachePath,"w") or die(" Tag Engine Create File FALSE! ");
fwrite($fp,$cacheCode);
fclose($fp);
return $censorData;
} function makecode($data,$varname)
{
$i=0;
$codestring = '';
while($arr = array_slice($data,$i,$this->censorLimit,true))
{
$codestring .= "\n\$".$varname."[] = '/". addcslashes( implode('|',$arr),"{}[]/.()'\"*" )."/i';";
$i += $this->censorLimit;
}
return $codestring;
} function check( &$string )
{
$this->banned($string);
$this->censor($string);
$this->replace($string);
/*
if($this->banned( $string) < 1)
{
if($this->censor( $string ) < 1)
{
$this->replace( $string );
}
}
*/
} //替换级别 返回替换匹配个数
function replace(&$subject)
{
if(empty($this->censorData[replace])) return 0; foreach($this->censorData[replace] as $from => $to)
{
$subject = str_replace($from,$to,$subject);
}
} //审核级别 返回审核匹配个数
function censor(&$subject)
{
if(empty($this->censorData[censor])) return 0; $count = 0;
foreach($this->censorData[censor] as $pattern)
{
preg_match_all($pattern, $subject, $matches);
if($matches) $count += count($matches[0]);
}
$this->censorCount_censor += $count;
return $count;
} //彻底禁止 返回禁止匹配个数
function banned(&$subject)
{
if(empty($this->censorData[banned])) return 0; $count = 0;
foreach($this->censorData[banned] as $pattern)
{
preg_match_all($pattern, $subject, $matches);
if($matches) $count += count( $matches[0] );
//print_r($matches);
}
$this->censorCount_banned += $count;
return $count;
} //审核级别 banned,censor,pass
function censorLevel()
{
if($this->censorCount_banned > 0 ) return 'banned';
if($this->censorCount_censor > 0 ) return 'censor';
return 'pass';
}
}
至少第二段代码 29 行处的
if(empty($censorData)) ShowMsg("ERROR:CENSOR数据不存在!");
return $censorData;
是错误的
require_once DEDEINC.'/plugin/censor.class.php';
$str = '提交的字符串';
$censor = new Censor();
$censor->check($str);
switch($censor->censorLevel())
{
case 'banned': /*禁止发布*/ break;
case 'censor': /*发布待审核*/ break;
default: /*直接发布*/ break;
}
是的,呵呵,这里报错。
<?php/* censorStatus: {banned} {censor} {replace} */class Censor
{
public $censorCount_banned;
public $censorCount_censor;
public $censorCount_replace;
public $censorData;
public $censorLimit;
public $cachePath; function __construct()
{
$this->censorCount_replace = 0;
$this->censorCount_censor = 0;
$this->censorCount_banned = 0;
$this->censorLimit = 120;
$this->cachePath = DEDEDATA.'/cache_censor.php';
$this->censorData = $this->GetCensorData();
}
function GetCensorData()
{
if(defined('CENSORDATA_LOADED') && !empty($GLOBALS['censorData']))
{
return $GLOBALS['censorData'];
} elseif( file_exists($this->cachePath) && filesize($this->cachePath) > 1024) {
include $this->cachePath;
if(!empty($censorData)) return $censorData;
} $censorData = array();
$censorData['banned'] = array();
$censorData['censor'] = array();
$censorData['repalce']= array(); global $dsql;
$sql = "SELECT * FROM gk_info_censor WHERE replacement!='' AND `enable`=1 ";
$dsql->Execute('s',$sql);
while ($row = $dsql->GetArray('s'))
{
switch ($row['replacement'])
{
case '{banned}': $censorData['banned'][] = $row['find']; break;//banned
case '{censor}': $censorData['censor'][] = $row['find']; break;//censor
default: $censorData['replace'][$row['find']]=$row['replacement'];break;//replace
}
} #$cachePath = DEDEDATA.'/cache_censor.php';
$cacheCode = "<"."?php\n";
$cacheCode .= "\n#Make Time:".date("Y-m-d H:i:s");
$cacheCode .= "\n#Make File:".__FILE__."(Line:".__LINE__.")";
$cacheCode .= "\n#notice! do not modify me!\n\n"; $cacheCode .= "\ndefine('CENSORDATA_LOADED',TRUE);";
$cacheCode .= "\n\$censorData=array();";
$cacheCode .= "\n\$censorData['banned'] = null;";
$cacheCode .= "\n\$censorData['censor'] = null;";
$cacheCode .= "\n\$censorData['replace']= null;"; $cacheCode .= $this->makecode($censorData['banned'],"censorData['banned']");
$cacheCode .= $this->makecode($censorData['censor'],"censorData['censor']");
foreach ($censorData['replace'] as $k => $v) {
$cacheCode .= "\n\$censorData['replace']['".addslashes($k)."']= '".addcslashes ($v,"{}[]/.()'\"*")."';";
}
$cacheCode .= "\n\n?".">"; $fp = @fopen($this->cachePath,"w") or die(" Tag Engine Create File FALSE! ");
fwrite($fp,$cacheCode);
fclose($fp);
return $censorData;
} function makecode($data,$varname)
{
$i=0;
$codestring = '';
while($arr = array_slice($data,$i,$this->censorLimit,true))
{
$codestring .= "\n\$".$varname."[] = '/". addcslashes( implode('|',$arr),"{}[]/.()'\"*" )."/i';";
$i += $this->censorLimit;
}
return $codestring;
} function check( &$string )
{
$this->banned($string);
$this->censor($string);
$this->replace($string);
/*
if($this->banned( $string) < 1)
{
if($this->censor( $string ) < 1)
{
$this->replace( $string );
}
}
*/
} //替换级别 返回替换匹配个数
function replace(&$subject)
{
if(empty($this->censorData['replace'])) return 0; foreach($this->censorData['replace'] as $from => $to)
{
$subject = str_replace($from,$to,$subject);
}
} //审核级别 返回审核匹配个数
function censor(&$subject)
{
if(empty($this->censorData['censor'])) return 0; $count = 0;
foreach($this->censorData['censor'] as $pattern)
{
preg_match_all($pattern, $subject, $matches);
if($matches) $count += count($matches[0]);
}
$this->censorCount_censor += $count;
return $count;
} //彻底禁止 返回禁止匹配个数
function banned(&$subject)
{
if(empty($this->censorData['banned'])) return 0; $count = 0;
foreach($this->censorData['banned'] as $pattern)
{
preg_match_all($pattern, $subject, $matches);
if($matches) $count += count( $matches[0] );
//print_r($matches);
}
$this->censorCount_banned += $count;
return $count;
} //审核级别 banned,censor,pass
function censorLevel()
{
if($this->censorCount_banned > 0 ) return 'banned';
if($this->censorCount_censor > 0 ) return 'censor';
return 'pass';
}
}
$this->censorCount = NULL;
$this->censorCount->replace = 0;
$this->censorCount->censor = 0;
$this->censorCount->Banned = 0;
第一段代码 这种用法是自创的吧
1. 第一个人滥用类,没申明的东西都开始加属性,这个习惯很不好,不知道他从哪学来的。
2. if语句能写成这样 function check( &$string )
{
if($this->banned( $string) < 1)
{
if($this->censor( $string ) < 1)
{
$this->replace( $string );
}
}
}
3. 第二个人想到了将有用的经常调用的东西缓存起来总结一下:
两人的代码看上去很牛X,实际上问题多多。楼主看一下哪个人态度好些吧,我觉的这个很重要
谢谢点评。
2. if语句能写成这样 function check( &$string )
{
if($this->banned( $string) < 1)
{
if($this->censor( $string ) < 1)
{
$this->replace( $string );
}
}
}这个这里是指?
/* #database gk_info_censor,gk_info_censorlog #
mysql> desc gk_info_censor;
+-------------+----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+----------------------+------+-----+---------+----------------+
| id | smallint(6) unsigned | NO | PRI | NULL | auto_increment |
| admin | varchar(15) | NO | | | |
| type | smallint(6) | NO | | 1 | |
| find | varchar(255) | NO | UNI | | |
| replacement | varchar(255) | NO | | | |
| extra | varchar(255) | NO | | | |
| count | int(11) | NO | | 0 | |
| updatetime | int(11) | NO | | NULL | |
| tids | mediumtext | NO | | NULL | |
| enable | tinyint(1) | NO | | -1 | |
+-------------+----------------------+------+-----+---------+----------------+
10 rows in set (0.02 sec)mysql> desc gk_info_censorlog;
+-------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| tableid | varchar(20) | NO | | NULL | |
| dataid | int(11) | NO | | NULL | |
| matchcensor | text | NO | | NULL | |
| matchbanned | text | NO | | NULL | |
| isdeal | tinyint(1) | NO | | -1 | |
+-------------+-------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)
#how to use ?
$str1 = 'post title';
$str2 = 'post content';
include DEDEINC.'/plugin/censor.class.php';
$censor = new Censor();
$censor->check($str1);
$censor->check($str2);
if($censor->censorLevel() == 'banned')
{
//your code
} elseif ($censor->censorLevel() == 'censor') {
//your code
} else {
//your code
}
*/
class Censor
{
public $censorMatch_banned;
public $censorMatch_censor;
public $censorData;
public $censorLimit;
public $cachePath; function __construct()
{
$this->censorMatch_censor = array();
$this->censorMatch_banned = array(); $this->censorLimit = 120;
$this->cachePath = DEDEDATA.'/cache_censor.php';
$this->censorData = $this->GetCensorData();
}
function GetCensorData()
{
if(defined('CENSORDATA_LOADED') && !empty($GLOBALS['censorData']))
{
return $GLOBALS['censorData'];
} elseif( file_exists($this->cachePath) && filesize($this->cachePath) > 1024) {
include $this->cachePath;
if(!empty($censorData)) return $censorData;
} $censorData = array();
$censorData['banned'] = array();
$censorData['censor'] = array();
$censorData['repalce']= array(); global $dsql;
$sql = "SELECT * FROM gk_info_censor WHERE replacement!='' AND `enable`=1 ";
$dsql->Execute('s',$sql);
while ($row = $dsql->GetArray('s'))
{
switch ($row['replacement'])
{
case '{banned}': $censorData['banned'][] = $row['find']; break;//banned
case '{censor}': $censorData['censor'][] = $row['find']; break;//censor
default: $censorData['replace'][$row['find']]=$row['replacement'];break;//replace
}
} #$cachePath = DEDEDATA.'/cache_censor.php';
$cacheCode = "<"."?php\n";
$cacheCode .= "\n#Make Time:".date("Y-m-d H:i:s");
$cacheCode .= "\n#Make File:".__FILE__."(Line:".__LINE__.")";
$cacheCode .= "\n#notice! do not modify me!\n\n"; $cacheCode .= "\ndefine('CENSORDATA_LOADED',TRUE);";
$cacheCode .= "\n\$censorData=array();";
$cacheCode .= "\n\$censorData['banned'] = null;";
$cacheCode .= "\n\$censorData['censor'] = null;";
$cacheCode .= "\n\$censorData['replace']= null;"; $cacheCode .= $this->makecode($censorData['banned'],"censorData['banned']");
$cacheCode .= $this->makecode($censorData['censor'],"censorData['censor']");
foreach ($censorData['replace'] as $k => $v) {
$cacheCode .= "\n\$censorData['replace']['".addslashes($k)."']= '".addcslashes ($v,"{}[]/.()'\"*")."';";
}
$cacheCode .= "\n\n?".">"; $fp = @fopen($this->cachePath,"w") or die(" Tag Engine Create File FALSE! ");
fwrite($fp,$cacheCode);
fclose($fp);
return $censorData;
} function makecode($data,$varname)
{
$i=0;
$codestring = '';
while($arr = array_slice($data,$i,$this->censorLimit,true))
{
$codestring .= "\n\$".$varname."[] = '/". addcslashes( implode('|',$arr),"{}[]/.()'\"*" )."/i';";
$i += $this->censorLimit;
}
return $codestring;
} function check( &$string )
{
$this->banned($string);
$this->censor($string);
$this->replace($string);
} //替换级别 返回替换匹配个数
function replace(&$subject)
{
if(empty($this->censorData['replace'])) return 0; foreach($this->censorData['replace'] as $from => $to)
{
$subject = str_replace($from,$to,$subject);
}
} //审核级别 返回审核匹配个数
function censor(&$subject)
{
if(empty($this->censorData['censor'])) return 0; foreach($this->censorData['censor'] as $pattern)
{
preg_match_all($pattern, $subject, $matches);
if($matches) $this->censorMatch_censor = array_merge($this->censorMatch_censor,$matches[0]);
}
} //彻底禁止 返回禁止匹配个数
function banned(&$subject)
{
if(empty($this->censorData['banned'])) return 0; foreach($this->censorData['banned'] as $pattern)
{
preg_match_all($pattern, $subject, $matches);
if($matches) $this->censorMatch_banned = array_merge($this->censorMatch_banned,$matches[0]);
}
} //审核级别 banned,censor,pass
function censorLevel()
{
if(count($this->censorMatch_banned) > 0 ) return 'banned';
if(count($this->censorMatch_censor) > 0 ) return 'censor';
return 'pass';
} //add sql log
function insertLog($tableid,$dataid,$isdeal=-1)
{
if(empty($this->censorMatch_banned) && empty($this->censorMatch_censor)) return FALSE;
global $dsql;
$sql = "INSERT INTO gk_info_censorlog SET `tableid`='$tableid',`dataid`='$dataid',`isdeal`='$isdeal',";
$sql .="`matchcensor`='". implode(',',$this->censorMatch_censor)."',";
$sql .="`matchbanned`='". implode(',',$this->censorMatch_banned)."' ";
return $dsql->ExecuteNoneQuery2( $sql );
}
}
结构很乱,看的很吃力。在不知功能的情况下可能还看不懂。
代码不优美
//这一段建议包装一下。 如果你喜欢使用缓存策略,这块代码需要封装起来,便于重用!~
//读和取,都封起来~
if(defined('CENSORDATA_LOADED') && !empty($GLOBALS['censorData']))
{
return $GLOBALS['censorData'];
} elseif( file_exists($this->cachePath) && filesize($this->cachePath) > 1024) {
include $this->cachePath;
if(!empty($censorData)) return $censorData;
} #$cachePath = DEDEDATA.'/cache_censor.php';
$cacheCode = "<"."?php\n";
$cacheCode .= "\n#Make Time:".date("Y-m-d H:i:s");
$cacheCode .= "\n#Make File:".__FILE__."(Line:".__LINE__.")";
$cacheCode .= "\n#notice! do not modify me!\n\n";
$cacheCode .= "\ndefine('CENSORDATA_LOADED',TRUE);";
$cacheCode .= "\n\$censorData=array();";
$cacheCode .= "\n\$censorData['banned'] = null;";
$cacheCode .= "\n\$censorData['censor'] = null;";
$cacheCode .= "\n\$censorData['replace']= null;";
$cacheCode .= $this->makecode($censorData['banned'],"censorData['banned']");
$cacheCode .= $this->makecode($censorData['censor'],"censorData['censor']");
function check( &$string ) {
if($this->banned( $string) < 1)
{
if($this->censor( $string ) < 1)
{
$this->replace( $string );
}
}
}function banned(&$subject)
对于这种引用传递的。可以使用链式语法,这些方法都不需要再返回那个数量,而是在方法内部做数量检查,然后都返回$this。
然后这里就可以用链式语法了。function banned(&$subject){
...
return $this;
}function check( &$string ) {
$this->banned( $string)
->censor( $string )
->replace( $string )
->
}总结,对oop的理解很肤浅。除非用的是php4,否则这样使用class还不如直接写一堆函数。明显是为了用class而用的class,根本没有理解oop的精髓。有些方法可以拆开几个方法。虽然有些啰嗦,但那样才符合抽象,松耦合的概念。包括你改过之后的代码,仅仅是实现了功能而已,从oop的角度讲,还欠缺很多。