DISCUZ架构:misc.func.php文件分析一

网络整理 - 07-26

以下为引用的内容:
/**
* 这个函数看着很长,其实要实现的功能很少,就是通过ip地址返回一个对应的地理位置
* @para string $ip //给定的ip,要符合点分十进制
*
* @return string
*/
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($fd, 4);
                $DataEnd = fread($fd, 4);
                $ipbegin = implode('', unpack('L', $DataBegin));
                if($ipbegin < 0) $ipbegin += pow(2, 32);
                $ipend = implode('', unpack('L', $DataEnd));
                if($ipend < 0) $ipend += pow(2, 32);
                $ipAllNum = ($ipend - $ipbegin) / 7 + 1;

                $BeginNum = 0;
                $EndNum = $ipAllNum;

                while($ip1num > $ipNum || $ip2num < $ipNum) {
                        $Middle= intval(($EndNum + $BeginNum) / 2);

                        fseek($fd, $ipbegin + 7 * $Middle);
                        $ipData1 = fread($fd, 4);
                        if(strlen($ipData1) < 4) {
                                fclose($fd);
                                return '- System Error';
                        }
                        $ip1num = implode('', unpack('L', $ipData1));
                        if($ip1num < 0) $ip1num += pow(2, 32);

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

                        $DataSeek = fread($fd, 3);
                        if(strlen($DataSeek) < 3) {
                                fclose($fd);