两份PHP程序代码对比,大家认为其编程水平都怎么样?censor.class.php
功能:一个censor审核提交字符串的类(附加在DEDECMS里面)
两份不同的作品,同样的一种功能,大家评比一下~认为他们的水平如何?
如果你是主考官,你会如何选择?为什么?
涉及MySQL数据表:gk_info_censor
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.01 sec)
<?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';
}
}
<?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';
}
}
是错误的
[解决办法]
都很牛逼的样子
[解决办法]
都垃圾的很!
[解决办法]