setHeaders(['content-type' => 'text/plain']); $sites = SiteFactory::normalizeArray($this->request->getQueryParameters()['site'] ?? []); $data = $this->request->getQueryParameter('data') ?? ''; if ($data == '') { return "# Error: The 'data' GET parameter is required in the URL to access this page"; } if (!in_array($data, ['ip4', 'cidr4'])) { return "# Error: The 'data' GET parameter must be 'ip4' or 'cidr4'"; } $response = []; if (count($sites)) { foreach ($sites as $site) { $response = array_merge($response, $this->getSites()[$site]->$data); } } else { foreach ($this->getSites() as $siteEntity) { $response = array_merge($response, $siteEntity->$data); } } $response = SiteFactory::normalizeArray($response, true); return implode( "\n", array_map(function (string $item) { $parts = explode('/', $item); $mask = $this->formatShortIpMask($parts[1] ?? ''); return 'route add ' . $parts[0] . ' mask ' . $mask . ' 0.0.0.0'; }, $response) ); } /** * @param string $mask * @return string */ private function formatShortIpMask(string $mask): string { if ($mask == '') { $mask = '32'; } $binaryMask = str_repeat('1', $mask) . str_repeat('0', 32 - $mask); $octets = []; for ($i = 0; $i < 4; $i++) { $octets[] = bindec(substr($binaryMask, $i * 8, 8)); } return implode('.', $octets); } }