db->query('SELECT * FROM `ntp_list` WHERE id=' . $listId . ';'); return $res->fetchArray(SQLITE3_ASSOC)['name']; } public function getList() { // 获取所有NTP服务器地址 $this->db = new SqliteDB($this->ntpDB); $res = $this->db->query('SELECT * FROM `ntp_host`;'); while ($row = $res->fetchArray(SQLITE3_ASSOC)) { $index = $row['list_id']; unset($row['list_id']); $list[$this->getListName($index)][] = $row; } return $list; } } class ntpCheck { // NTP服务器检查 private function formatOffset($str) { // 格式化偏移时间 $num = number_format($str, 6) * 1000; // s -> ms $str = sprintf("%1\$.3f", $num); // 补零到小数点后3位 if ($num > 0) { $str = '+' . $str; // 正数前加+ } return $str . 'ms'; } private function sortByIp($servers) { // 排序算法 $temp = array(); foreach ($servers as $val){ $temp[] = $val['Server']; } sort($temp); $temp = array_flip($temp); $sort = array(); foreach ($servers as $val) { $temp_1 = $val['Server']; $temp_2 = $temp[$temp_1]; $sort[$temp_2] = $val; } asort($sort); return $sort; } private function sortServer($servers) { // 按顺序排列服务器 foreach ($servers as $server) { if(filter_var($server['Server'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { $ipv4[] = $server; // 提取IPv4服务器 } if(filter_var($server['Server'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { $ipv6[] = $server; // 提取IPv6服务器 } } if (isset($ipv4)) { // 存在IPv4服务器 foreach ($ipv4 as $index => $ip) { $ipv4[$index]['Server'] = ip2long($ip['Server']); // IPv4预处理 } $ipv4 = $this->sortByIp($ipv4); // 排序IPv4服务器 foreach ($ipv4 as $index => $ip) { $ip['Server'] = long2ip($ip['Server']); // IPv4恢复 $result[] = $ip; } } if (isset($ipv6)) { // 存在IPv6服务器 foreach ($ipv6 as $index => $ip) { $ipv6[$index]['Server'] = (new DNS)->ip2long6($ip['Server']); // IPv6预处理 } $ipv6 = $this->sortByIp($ipv6); // 排序IPv6服务器 foreach ($ipv6 as $index => $ip) { $ip['Server'] = (new DNS)->long2ip6($ip['Server']); // IPv6恢复 $result[] = $ip; } } return (!isset($result)) ? array() : $result; // 无结果 返回空数组 } private function getNtpStatus($host) { // 获取NTP服务器状态 $html = (new Curl)->post('https://servertest.online/ntp', array( 'a' => $host, 'c' => 'Query+both' )); if ($html == '') { return null; } // 服务错误 preg_match('/<\/form>[\s\S]+