从discuz里面拿来的东东[转phpx]

<?php
加解密函数

 $encode 
authocode('我要加密','ENCODE');

 
$decode authocode('我要解密','DECODE');


$auth_key 34577 //密钥
function authcode($string$operation$key '') {

    
$key md5($key $key $GLOBALS['auth_key']);
    
$key_length strlen($key);

    
$string $operation == 'DECODE' base64_decode($string) : substr(md5($string.$key), 08).$string;
    
$string_length strlen($string);

    
$rndkey $box = array();
    
$result '';

    for(
$i 0$i <= 255$i++) {
        
$rndkey[$i] = ord($key[$i $key_length]);
        
$box[$i] = $i;
    }

    for(
$j $i 0$i 256$i++) {
        
$j = ($j $box[$i] + $rndkey[$i]) % 256;
        
$tmp $box[$i];
        
$box[$i] = $box[$j];
        
$box[$j] = $tmp;
    }

    for(
$a $j $i 0$i $string_length$i++) {
        
$a = ($a 1) % 256;
        
$j = ($j $box[$a]) % 256;
        
$tmp $box[$a];
        
$box[$a] = $box[$j];
        
$box[$j] = $tmp;
        
$result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
    }

    if(
$operation == 'DECODE') {
        if(
substr($result08) == substr(md5(substr($result8).$key), 08)) {
            return 
substr($result8);
        } else {
            return 
'';
        }
    } else {
        return 
str_replace('='''base64_encode($result));
    }

}

?>
<?php
字符串截取函数

$cutstr 
=  cutstr('截取我,hahahahahaaha','3');



function 
cutstr($string$length$dot ' ...') {
    global 
$charset;

    if(
strlen($string) <= $length) {
        return 
$string;
    }

    
$string str_replace(array('&amp;''&quot;''&lt;''&gt;'), array('&''"''<''>'), $string);

    
$strcut '';
    if(
strtolower($charset) == 'utf-8') {

        
$n $tn $noc 0;
        while(
$n strlen($string)) {

            
$t ord($string[$n]);
            if(
$t == || $t == 10 || (32 <= $t && $t <= 126)) {
                
$tn 1$n++; $noc++;
            } elseif(
194 <= $t && $t <= 223) {
                
$tn 2$n += 2$noc += 2;
            } elseif(
224 <= $t && $t 239) {
                
$tn 3$n += 3$noc += 2;
            } elseif(
240 <= $t && $t <= 247) {
                
$tn 4$n += 4$noc += 2;
            } elseif(
248 <= $t && $t <= 251) {
                
$tn 5$n += 5$noc += 2;
            } elseif(
$t == 252 || $t == 253) {
                
$tn 6$n += 6$noc += 2;
            } else {
                
$n++;
            }

            if(
$noc >= $length) {
                break;
            }

        }
        if(
$noc $length) {
            
$n -= $tn;
        }

        
$strcut substr($string0$n);

    } else {
        for(
$i 0$i $length strlen($dot) - 1$i++) {
            
$strcut .= ord($string[$i]) > 127 $string[$i].$string[++$i] : $string[$i];
        }
    }

    
$strcut str_replace(array('&''"''<''>'), array('&amp;''&quot;''&lt;''&gt;'), $strcut);

    return 
$strcut.$dot;
}
function 
daddslashes($string$force 0) {
    !
defined('MAGIC_QUOTES_GPC') && define('MAGIC_QUOTES_GPC'get_magic_quotes_gpc());
    if(!
MAGIC_QUOTES_GPC || $force) {
        if(
is_array($string)) {
            foreach(
$string as $key => $val) {
                
$string[$key] = daddslashes($val$force);
            }
        } else {
            
$string addslashes($string);
        }
    }
    return 
$string;
}
?>
<?php

生成 随机数的函数
function random($length$numeric 0) {
    
PHP_VERSION '4.2.0' && mt_srand((double)microtime() * 1000000);
    if(
$numeric) {
        
$hash sprintf('%0'.$length.'d'mt_rand(0pow(10$length) - 1));
    } else {
        
$hash '';
        
$chars 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
        
$max strlen($chars) - 1;
        for(
$i 0$i $length$i++) {
            
$hash .= $chars[mt_rand(0$max)];
        }
    }
    return 
$hash;
}


?>
<?php
// discuz 一个ip转换地址的函数 wry.dat 是ip数据库 可以从纯真网下载最新的 [url]http://www.cz88.net/[/url]

function convertip($ip) {
    if(!
preg_match("/^d{1,3}.d{1,3}.d{1,3}.d{1,3}$/"$ip)) {
        return 
'';
    }

    if(
$fd = @fopen(DISCUZ_ROOT.'./ipdata/wry.dat''rb')) {

        
$ip explode('.'$ip);
        
$ipNum $ip[0] * 16777216 $ip[1] * 65536 $ip[2] * 256 $ip[3];

        
$DataBegin fread($fd4);
        
$DataEnd fread($fd4);
        
$ipbegin implode(''unpack('L'$DataBegin));
        if(
$ipbegin 0$ipbegin += pow(232);
        
$ipend implode(''unpack('L'$DataEnd));
        if(
$ipend 0$ipend += pow(232);
        
$ipAllNum = ($ipend $ipbegin) / 1;

        
$BeginNum 0;
        
$EndNum $ipAllNum;

        while(
$ip1num $ipNum || $ip2num $ipNum) {
            
$Middleintval(($EndNum $BeginNum) / 2);

            
fseek($fd$ipbegin $Middle);
            
$ipData1 fread($fd4);
            if(
strlen($ipData1) < 4) {
                
fclose($fd);
                return 
'System Error';
            }
            
$ip1num implode(''unpack('L'$ipData1));
            if(
$ip1num 0$ip1num += pow(232);

            if(
$ip1num $ipNum) {
                
$EndNum $Middle;
                continue;
            }

            
$DataSeek fread($fd3);
            if(
strlen($DataSeek) < 3) {
                
fclose($fd);
                return 
'System Error';
            }
            
$DataSeek implode(''unpack('L'$DataSeek.chr(0)));
            
fseek($fd$DataSeek);
            
$ipData2 fread($fd4);
            if(
strlen($ipData2) < 4) {
                
fclose($fd);
                return 
'System Error';
            }
            
$ip2num implode(''unpack('L'$ipData2));
            if(
$ip2num 0$ip2num += pow(232);

            if(
$ip2num $ipNum) {
                if(
$Middle == $BeginNum) {
                    
fclose($fd);
                    return 
'Unknown';
                }
                
$BeginNum $Middle;
            }
        }

        
$ipFlag fread($fd1);
        if(
$ipFlag == chr(1)) {
            
$ipSeek fread($fd3);
            if(
strlen($ipSeek) < 3) {
                
fclose($fd);
                return 
'System Error';
            }
            
$ipSeek implode(''unpack('L'$ipSeek.chr(0)));
            
fseek($fd$ipSeek);
            
$ipFlag fread($fd1);
        }

        if(
$ipFlag == chr(2)) {
            
$AddrSeek fread($fd3);
            if(
strlen($AddrSeek) < 3) {
                
fclose($fd);
                return 
'System Error';
            }
            
$ipFlag fread($fd1);
            if(
$ipFlag == chr(2)) {
                
$AddrSeek2 fread($fd3);
                if(
strlen($AddrSeek2) < 3) {
                    
fclose($fd);
                    return 
'System Error';
                }
                
$AddrSeek2 implode(''unpack('L'$AddrSeek2.chr(0)));
                
fseek($fd$AddrSeek2);
            } else {
                
fseek($fd, -1SEEK_CUR);
            }

            while((
$char fread($fd1)) != chr(0))
                
$ipAddr2 .= $char;

            
$AddrSeek implode(''unpack('L'$AddrSeek.chr(0)));
            
fseek($fd$AddrSeek);

            while((
$char fread($fd1)) != chr(0))
                
$ipAddr1 .= $char;
        } else {
            
fseek($fd, -1SEEK_CUR);
            while((
$char fread($fd1)) != chr(0))
                
$ipAddr1 .= $char;

            
$ipFlag fread($fd1);
            if(
$ipFlag == chr(2)) {
                
$AddrSeek2 fread($fd3);
                if(
strlen($AddrSeek2) < 3) {
                    
fclose($fd);
                    return 
'System Error';
                }
                
$AddrSeek2 implode(''unpack('L'$AddrSeek2.chr(0)));
                
fseek($fd$AddrSeek2);
            } else {
                
fseek($fd, -1SEEK_CUR);
            }
            while((
$char fread($fd1)) != chr(0))
                
$ipAddr2 .= $char;
        }
        
fclose($fd);

        if(
preg_match('/http/i'$ipAddr2)) {
            
$ipAddr2 '';
        }
        
$ipaddr "$ipAddr1 $ipAddr2";
        
$ipaddr preg_replace('/CZ88.NET/is'''$ipaddr);
        
$ipaddr preg_replace('/^s*/is'''$ipaddr);
        
$ipaddr preg_replace('/s*$/is'''$ipaddr);
        if(
preg_match('/http/i'$ipaddr) || $ipaddr == '') {
            
$ipaddr 'Unknown';
        }

        return 
$ipaddr;

    } else {

        
$datadir DISCUZ_ROOT.'./ipdata/';
        
$ip_detail explode('.'$ip);
        if(
file_exists($datadir.$ip_detail[0].'.txt')) {
            
$ip_fdata = @fopen($datadir.$ip_detail[0].'.txt''r');
        } else {
            if(!(
$ip_fdata = @fopen($datadir.'0.txt''r'))) {
                return 
'Invalid IP data file';
            }
        }
        for (
$i 0$i <= 3$i++) {
            
$ip_detail[$i] = sprintf('%03d'$ip_detail[$i]);
        }
        
$ip join('.'$ip_detail);
        do {
            
$ip_data fgets($ip_fdata200);
            
$ip_data_detail explode('|'$ip_data);
            if(
$ip >= $ip_data_detail[0] && $ip <= $ip_data_detail[1]) {
                
fclose($ip_fdata);
                return 
$ip_data_detail[2].$ip_data_detail[3];
            }
        } while(!
feof($ip_fdata));
        
fclose($ip_fdata);
        return 
'UNKNOWN';

    }

}
?>
posted @ 2008-02-17 22:11  二宝的博客  阅读(180)  评论(0编辑  收藏  举报