/** * generate key for cache save * * @param string $mode * @param Coords $coords * @param int $zoom * @param array[] $params * @return string */ public static function generateCacheKey($mode, $coords, $zoom, $params = array()) { $key = $mode . ':' . $coords->getLatStart() . ':' . $coords->getLatEnd() . ':' . $coords->getLonStart() . ':' . $coords->getLonEnd() . ':' . $zoom; if (count($params) > 0) { $key .= "::"; foreach ($params as $k => $v) { $key .= "~" . $k . "=" . $v; } } return $key; }
/** * @param Wifi $wifi * @return Wifi */ public function getClosestWifiToWifi(Wifi $wifi) { $coords = Coords::createCoordsRangeByLatLng($wifi->getLatitude(), $wifi->getLongitude(), 0.03); return Wifi::createWifiFromDBRow($this->getNetsRangeQuery($coords)->where("id != ?", $wifi->getId())->where('mac != ?', $wifi->getMac())->order("SQRT(POW(latitude-?,2)+POW(longitude-?,2))", $wifi->getLatitude(), $wifi->getLongitude())->limit(1)->fetch()); }
/** * @param Coords $coords * @param int $first * @return string JSON with Wigle data */ private function getDataFromWigle($coords, $first = 0) { $arr = array("longrange1" => $coords->getLonStart(), "longrange2" => $coords->getLonEnd(), "latrange1" => $coords->getLatStart(), "latrange2" => $coords->getLatEnd()); if ($first != 0) { $arr["first"] = $first; $arr["last"] = $first + 99; } return $this->sendCurlRequest(self::WIGLE_GETDATA_URL, $arr); }
/** * create X,Y array MAP * index to lat or lng * * @param array|\Nette\Database\Table\IRow[] $data * @param Coords $coords * @return array */ private function createMappingXY($data, $coords) { $mappingX = array($coords->getLatStart(), $coords->getLatEnd()); $mappingY = array($coords->getLonStart(), $coords->getLonEnd()); foreach ($data as $d) { $d_coords = new Coords($d['lat_start'], $d['lat_end'], $d['lon_start'], $d['lon_end']); if (!in_array($d_coords->getLatStart(), $mappingX) && $d_coords->getLatStart() >= $coords->getLatStart() && $d_coords->getLatStart() <= $coords->getLatEnd()) { $mappingX[] = $d_coords->getLatStart(); } if (!in_array($d_coords->getLatEnd(), $mappingX) && $d_coords->getLatEnd() <= $coords->getLatEnd() && $d_coords->getLatEnd() >= $coords->getLatStart()) { $mappingX[] = $d_coords->getLatEnd(); } if (!in_array($d_coords->getLonStart(), $mappingY) && $d_coords->getLonStart() >= $coords->getLonStart() && $d_coords->getLonStart() <= $coords->getLonEnd()) { $mappingY[] = $d_coords->getLonStart(); } if (!in_array($d_coords->getLonEnd(), $mappingY) && $d_coords->getLonEnd() <= $coords->getLonEnd() && $d_coords->getLonEnd() >= $coords->getLonStart()) { $mappingY[] = $d_coords->getLonEnd(); } } sort($mappingX); sort($mappingY); return array('xMap' => $mappingX, 'yMap' => $mappingY); }
/** * render one image for overlay * * @param string $mode * @param float $lat1 * @param float $lat2 * @param float $lon1 * @param float $lon2 */ public function renderImage($mode, $lat1, $lat2, $lon1, $lon2) { header("Content-type: image/png"); MyUtils::setIni(180, '1024M'); $request = $this->getHttpRequest(); $zoom = $request->getQuery("zoom"); $this->overlayRenderer = new OverlayRenderer($zoom); // not allowed mode -> set do default mode if (!$this->allowedMode($mode)) { $mode = self::DEFAULT_MODE; } // zoom is too small if ($zoom < self::MIN_OVERLAY_ZOOM) { echo MyUtils::image2string($this->overlayRenderer->drawNone()); exit; } // increase coords range, due to future crop $coords = new Coords($lat1, $lat2, $lon1, $lon2); $coords->increaseLatLngRange(self::INCREASE_LATLNG_RANGE_ABOUT); // params for image creation $params = array(); switch ($mode) { case self::MODE_SEARCH: $ssidmac = $request->getQuery("ssidmac"); if ($ssidmac) { if (MyUtils::isMacAddress($ssidmac)) { $params['mac'] = urldecode($ssidmac); } else { $params['ssid'] = $ssidmac; } } $channel = $request->getQuery('channel'); if ($channel != null && $channel != "") { $params['channel'] = intval($channel); } $security = $request->getQuery('security'); if ($security != null && $security != '') { $params['sec'] = intval($security); } $source = $request->getQuery('source'); if ($source != null && $source != "") { $params['id_source'] = intval($source); } break; case self::MODE_HIGHLIGHT: $by = $request->getQuery("by"); if (in_array($by, self::$MODE_HIGHLIGHT_ALLOWED_BY)) { $params['by'] = $by; $val = $request->getQuery("val"); $params['val'] = $val; } break; case self::MODE_ONE: $ssidmac = $this->getHttpRequest()->getQuery('ssid'); if (MyUtils::isMacAddress($ssidmac)) { $params['mac'] = urldecode($ssidmac); } else { $params['ssid'] = $ssidmac; } break; default: break; } // generating cache key $key = MyUtils::generateCacheKey($mode, $coords, $zoom, $params); // try to find in cache if (self::CACHE_ON && $mode != self::MODE_CALCULATED) { $img = $this->cache->load($key); if ($img != null) { echo $img; return; } } // get data and generate tile of overlay switch ($mode) { case self::MODE_SEARCH: $params['coords'] = $coords; $nets = $this->oWifiManager->getNetsByParams($params, array('ssid,mac,latitude,longitude,id_source')); $img = $this->overlayRenderer->drawModeAll($coords, $nets); break; case self::MODE_HIGHLIGHT: if (!empty($params)) { $params['coords'] = $coords; $params[$params['by']] = $params['val']; unset($params['by']); unset($params['val']); $highlightedIds = $this->oWifiManager->getNetsByParams($params, array('id')); $allNets = $this->oWifiManager->getNetsByParams(array('coords' => $params['coords']), array('id,ssid,mac,latitude,longitude,id_source')); $img = $this->overlayRenderer->drawModeHighlight($coords, $allNets, $highlightedIds); } else { $nets = $this->wifiManager->getAllNetsInLatLngRange($coords, array('latitude', 'longitude', 'ssid', 'mac', 'id_source'), true); $img = $this->overlayRenderer->drawModeAll($coords, $nets); } break; case self::MODE_ONE: $nets = $this->wifiManager->getNetsModeSearch($coords, $params); $img = $this->overlayRenderer->drawModeOne($coords, $nets); break; case self::MODE_CALCULATED: $net = $this->wifiManager->getWifiById($this->getHttpRequest()->getQuery('a')); $lat = $net->getLatitude(); $lon = $net->getLongitude(); $lat1 = doubleval($lat) - 0.003; $lat2 = doubleval($lat) + 0.003; $lon1 = doubleval($lon) - 0.003 / 2; $lon2 = doubleval($lon) + 0.003 / 2; $coordsNew = new Coords($lat1, $lat2, $lon1, $lon2); $nets = $this->wifiManager->getNetsModeSearch($coordsNew, array('mac' => $net->getMac())); $nets2 = $this->wifiManager->getNetsModeSearch($coords, array('mac' => $net->getMac())); $latt = 0; $lont = 0; foreach ($nets as $net) { $latt += $net->getLatitude(); $lont += $net->getLongitude(); } $lat_avg = $latt / (double) count($nets); $lon_avg = $lont / (double) count($nets); $net = new Wifi(); $net->setLatitude($lat_avg); $net->setLongitude($lon_avg); $img = $this->overlayRenderer->drawCalculated($coords, $nets2, $net); break; default: $nets = $this->wifiManager->getAllNetsInLatLngRange($coords, array('latitude', 'longitude', 'ssid', 'mac', 'id_source'), true); $img = $this->overlayRenderer->drawModeAll($coords, $nets); break; } $image = MyUtils::image2string($img); $img = null; // save generated image to cache if (self::CACHE_ON && $mode != self::MODE_CALCULATED) { $this->cache->save($key, $image, array(Cache::EXPIRE => time() + self::$cacheExpire[$zoom])); } echo $image; return; }
/** * counts lat lng range shown in one pixel * onepxlat -> latitude range in one pixel * onepxlon -> longitude range in one pixel * * @param Coords $coords * @uses OverlayRenderer::IMAGE_BIGGER as size of image * @return object */ private function getConversionRatio($coords) { $one_pixel_lat = abs($coords->getLatEnd() - $coords->getLatStart()) / self::IMAGE_BIGGER; $one_pixel_lon = abs($coords->getLonEnd() - $coords->getLonStart()) / self::IMAGE_BIGGER; return (object) array("onepxlat" => $one_pixel_lat, "onepxlon" => $one_pixel_lon); }
/** * return number of nets on Wigle overlay Image * * @param Coords $coords * @return int */ private function analyzeImage($coords) { // TODO: ziskavat i jinak celky obrazek nez ctverec pokud mam jiny rozsah souradnic nez ctverec? $params = array("lat1" => $coords->getLatStart(), "long1" => $coords->getLonStart(), "lat2" => $coords->getLatEnd(), "long2" => $coords->getLonEnd(), "redir" => "Y", "networksOnly" => "Y", "sizeX" => 256, "sizeY" => 256); $url = self::WIGLE_IMAGE_OVERLAY_URL . "?" . http_build_query($params); $headers = get_headers($url, 1); $url = self::WIGLE_URL . trim($headers["Location"]); $image = imagecreatefrompng($url); $points = 0; for ($x = 0; $x < 256; $x++) { for ($y = 0; $y < 256; $y++) { if (in_array(dechex(imagecolorat($image, $x, $y)), $this->wigleNetColors)) { $points++; } } } return $points; }
/** * accuracy page */ public function renderAccuracy() { if ($this->getHttpRequest()->getQuery("mac") != "" && $this->getHttpRequest()->getQuery("r_latitude") != "" && $this->getHttpRequest()->getQuery("r_longitude") != "" && MyUtils::isMacAddress($this->getHttpRequest()->getQuery("mac"))) { $mac = MyUtils::macSeparator2Colon($this->getHttpRequest()->getQuery("mac")); $tableData = $this->wifiManager->getDistanceFromOriginalGPS($mac, doubleval($this->getHttpRequest()->getQuery("r_latitude")), doubleval($this->getHttpRequest()->getQuery("r_longitude"))); $data = array(); foreach ($tableData as $td) { $coords = new Coords($td["latitude"], $this->getHttpRequest()->getQuery("r_latitude"), $td["longitude"], $this->getHttpRequest()->getQuery("r_longitude")); $inM = $coords->getDistanceInMetres(); $arr = $td; $arr["inM"] = $inM; $data[] = $arr; } usort($data, "self::Sort"); $chWigleMin = PHP_INT_MAX; $chWigleMax = 0; $chWigleTotal = 0; $chGoogleMin = PHP_INT_MAX; $chGoogleMax = 0; $chGoogleTotal = 0; $chWigleAvg = 0; $wigleCount = 0; $wifileaksCount = 0; $wifileaksTotal = 0; $googleCount = 0; foreach ($data as $d) { if ($d["id_source"] == WifileaksDownload::ID_SOURCE) { $wifileaksCount++; $wifileaksTotal += $d["inM"]; } if ($d["id_source"] == WigleDownload::ID_SOURCE) { $wigleCount++; if ($chWigleMin > $d["inM"]) { $chWigleMin = $d["inM"]; } if ($chWigleMax < $d["inM"]) { $chWigleMax = $d["inM"]; } $chWigleTotal += $d["inM"]; if ($d["calculated"] == 1) { $chWigleAvg = $d["inM"]; } } if ($d["id_source"] == GoogleDownload::ID_SOURCE) { $googleCount++; if ($chGoogleMin > $d["inM"]) { $chGoogleMin = $d["inM"]; } if ($chGoogleMax < $d["inM"]) { $chGoogleMax = $d["inM"]; } $chGoogleTotal += $d["inM"]; } } if ($chWigleAvg == 0) { $chWigleAvg = $chWigleTotal / $wigleCount; } $chGoogleAvg = $chGoogleTotal / $googleCount; $chWifileaks = $wifileaksTotal / $wifileaksCount; $this->template->chWifileaks = $chWifileaks; $this->template->chWigleMin = $chWigleMin; $this->template->chWigleMax = $chWigleMax; $this->template->chWigleAvg = $chWigleAvg; $this->template->chGoogleMin = $chGoogleMin; $this->template->chGoogleMax = $chGoogleMax; $this->template->chGoogleAvg = $chGoogleAvg; $this->template->table = $data; } }