require_once ("../cms/lib/database.inc.php");
require_once ("config.inc.php");
if (!empty($_GET['id'])) {
include("../map/add_location.php");
}
$satteliten = array(
"S0.8W" => "Intelsat",
"S1W" => "Thor",
"S4.8E" => "Astra",
"S5E" => "SES 5",
"S9E" => "Eutelsat,Ka-Sat",
"S13E" => "Eutelsat Hot Bird",
"S19.2E" => "Astra",
"S23.5E" => "Astra",
"S28.2E" => "Astra",
"S30W" => "Hispasat",
"S36E" => "Eutelsat",
"S42E" => "Turksat",
"S78.5E" => "Thaicom",
);
if (count($_GET) == 0) {
// Adressen aller Kanallisten als Linkliste ausgeben
header("Content-Type: text/html; charset=UTF8");
$db = new Database ("data", $GLOBALS['config']['db']);
if ($db->query ("SELECT DISTINCT type, address FROM channellist ORDER BY SUBSTRING(type,1,1), CONVERT(SUBSTRING(type,2),SIGNED INTEGER), address")) {
foreach ($db as $row) {
echo "{$row['type']} {$row['address']}
\n";
}
}
exit;
}
header("Content-Type: text/plain; charset=UTF8");
if (isset($_GET['channellists']) && !isset($_GET['id'])) {
// Adressen aller Kanallisten ausgeben
$db = new Database ("data", $GLOBALS['config']['db']);
foreach (array('S', 'T2', 'C', 'I') as $type) {
if ($db->query ("SELECT DISTINCT type, address FROM channellist WHERE type LIKE '$type%' ORDER BY CONVERT(SUBSTRING(type,2),SIGNED INTEGER), address")) {
foreach ($db as $row) {
echo "{$row['type']} {$row['address']}\n";
}
}
}
}
if (isset($_GET['channellists']) && !isset($_GET['type']) && !empty($_GET['id']) && !empty($lat) && !empty($lng)) {
// Zur Region passende Kanallisten abrufen
$db = new Database ("data", $GLOBALS['config']['db']);
$lat = Database::escape(round($lat, 6));
$lng = Database::escape(round($lng, 6));
echo "S19.2E {$satteliten['S19.2E']}\n";
echo "S36E {$satteliten['S36E']}\n";
//if ($db->query ("SELECT DISTINCT type, address FROM channellist WHERE type LIKE 'S%' ORDER BY LPAD(lower(type), 10,0)")) {
// foreach ($db as $row) {
// echo "{$row['type']} {$row['address']}\n";
// }
//}
foreach (array('T2', 'C') as $type) {
if ($db->query ("SELECT type, address, SQRT(POW(lat-$lat,2)+POW(lng-$lng,2))*100 AS distance FROM channellist WHERE type='$type' HAVING distance<500 ORDER BY distance LIMIT 5")) {
foreach ($db as $key => $row) {
if ($key==0 || $row['distance'] < 100) {
echo "{$row['type']} {$row['address']}\n";
}
}
}
}
}
if (isset($_GET['channellists']) && !empty($_GET['type']) && !empty($_GET['id']) && !empty($lat) && !empty($lng)) {
// Zur Region passende Kanallisten für einen DVB Typ abrufen
$db = new Database ("data", $GLOBALS['config']['db']);
$type = Database::escape(strtoupper($_GET['type']));
$lat = Database::escape(round($lat, 6));
$lng = Database::escape(round($lng, 6));
if ($type == "S") {
if ($db->query ("SELECT DISTINCT type FROM channellist WHERE type LIKE 'S%'")) {
foreach ($db as $row) {
echo "{$row['type']}\n";
}
}
} else {
if ($db->query ("SELECT id, SQRT(POW(lat-$lat,2)+POW(lng-$lng,2))*100 AS distance FROM channellist WHERE type='$type' HAVING distance<500 ORDER BY distance LIMIT 5")) {
foreach ($db as $key => $row) {
if ($key==0 || $row['distance'] < 100) {
echo "{$row['id']}\n";
}
}
}
}
}
if (isset($_GET['channellist']) && (!empty($_GET['channellist_id']) || !empty($_GET['type']))) {
// Eine Kanalliste ausgeben
$db = new Database ("data", $GLOBALS['config']['db']);
$channellist_id = isset ($_GET['channellist_id']) ? Database::escape($_GET['channellist_id']) : "";
$type = isset ($_GET['type']) ? Database::escape($_GET['type']) : "";
$address = isset ($_GET['address']) ? Database::escape($_GET['address']) : "";
if ($channellist_id) {
if ($db->query ("SELECT channels FROM channellist WHERE id='$channellist_id'")) {
echo $db['channels'];
}
} else {
if ($db->query ("SELECT channels FROM channellist WHERE type='$type' AND address='$address' ORDER BY ROUND(POW(`rows`,0.3)) DESC, DATE_ADD(time, INTERVAL ROUND(POW(updates, 0.5)) DAY) DESC LIMIT 1")) {
echo $db['channels'];
}
}
}
/*
function getAddressFromGoogle($lat, $lng, &$cc) {
$place = json_decode(file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?key={$GLOBALS['config']['geocode_key']}&latlng=$lat,$lng&language=$cc"));
if (!empty($place->results[0])) {
foreach($place->results[0]->address_components as $location) {
if (in_array("country", $location->types) && $location->short_name!=$cc) {
// Noch mal in Landessprache abrufen
$cc = $location->short_name;
$place = json_decode(file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?key={$GLOBALS['config']['geocode_key']}&latlng=$lat,$lng&language=$cc"));
break;
}
}
$lastname = "";
$address = array();
foreach($place->results[0]->address_components as $location) {
if (in_array("political", $location->types) && $location->long_name != $lastname) {
array_unshift($address, $location->long_name);
$lastname = $location->long_name;
}
}
if (count($address)) {
return implode(",", preg_replace("/\//", ",", $address));
}
}
return "";
}
*/
function getAddress($lat, $lng, &$cc) {
$place = json_decode(file_get_contents("https://api.mapbox.com/geocoding/v5/mapbox.places/$lng,$lat.json?access_token={$GLOBALS['config']['mapbox_token']}&language=$cc"));
if (!empty($place->features)) {
$address = array();
foreach($place->features as $feature) {
if (in_array($feature->place_type[0], array('locality', 'place', 'region', 'country'))) {
array_unshift($address, $feature->text);
}
}
if (count($address)) {
return implode(",", $address);
}
}
return "";
}
if (isset($_GET['address'])) {
$cc='DE';
echo getAddress($lat, $lng, $cc);
}
if (isset($_GET['channels']) && !empty($_GET['id']) && !empty($lat) && !empty($lng)) {
// Neue Kanalliste hinzufügen oder Kanalliste aktualisieren
$db = new Database ("data", $GLOBALS['config']['db']);
$user_id = Database::escape($_GET['id']);
$lat = Database::escape(round($lat, 6));
$lng = Database::escape(round($lng, 6));
if ($if = fopen('php://input', 'r')) {
$channels = "";
$type = "";
$t2 = false;
$rows = 0;
while (($line = fgets($if, 1000)) !== false) {
if (!$type && preg_match("/.*?:.*?:(.*?):(.*?):/", $line, $erg)) {
$t2 = $erg[2] == "T" && preg_match("/S1/", $erg[1]);
$type = preg_replace("/\.0/", "", $erg[2]);
}
if ($type && preg_match("/.*?:.*?:.*?:$type:/", $line)) {
$channels .= $line;
$rows++;
}
}
if ($t2) {
$type = "T2";
}
if (mb_detect_encoding($channels, 'UTF-8, ISO-8859-1') == 'ISO-8859-1') {
$channels = utf8_encode($channels);
}
if ($channels && $type) {
$channels = Database::escape($channels);
if ($db->query ("SELECT id, lat, lng, updates FROM channellist WHERE user_id='$user_id' && type='$type'")) {
$id = $db['id'];
$updates = $db['updates'] + 1;
if (($db['lat'] != $lat || $db['lng'] != $lng) && !preg_match ("/^S/", $type)) {
$cc = "DE";
$address = Database::escape(getAddress($lat, $lng, $cc));
$cc = Database::escape($cc);
$db->query ("UPDATE channellist SET cc='$cc', address='$address' WHERE id=$id");
}
$db->query ("UPDATE channellist SET updates='$updates', channels='$channels' WHERE id=$id");
} else {
if (preg_match ("/^S/", $type)) {
$address = isset($satteliten[$type]) ? $satteliten[$type] : "";
$cc = "";
} else {
$cc = "DE";
$address = Database::escape(getAddress($lat, $lng, $cc));
$cc = Database::escape($cc);
}
$type = Database::escape($type);
$db->query ("INSERT INTO channellist (user_id, lat, lng, cc, address, type, `rows`, channels) VALUES ('$user_id', '$lat', '$lng', '$cc', '$address', '$type', '$rows', '$channels')");
}
}
fclose($if);
}
}
if (isset($_GET['import'])) {
// Alte Kanallisten aus Filesystem importieren
echo "Import:\n";
$db = new Database ("data", $GLOBALS['config']['db']);
if ($d = dir("{$config['data']}channels/")) {
while ($f = $d->read ()) {
if (preg_match ("/^DVB-(.+)-([0-9.]+)-([0-9.]+).conf/i", $f, $erg)) {
$type = Database::escape(strtoupper($erg[1]));
$lat = Database::escape($erg[2]);
$lng = Database::escape($erg[3]);
if (!$db->query ("SELECT id FROM channellist WHERE type='$type' AND lat='$lat' AND lng='$lng'")) {
if (preg_match ("/^S/", $type)) {
$type = preg_replace("/\.0/", "", $type);
$address = isset($satteliten[$type]) ? $satteliten[$type] : "";
$cc = "";
} else {
$cc = "DE";
$address = Database::escape(getAddress($lat, $lng, $cc));
$cc = Database::escape($cc);
}
$user_id = "";
if ($db->query ("SELECT user_id FROM geo WHERE ROUND(lat,6)='$lat' AND ROUND(lng,6)='$lng'")) {
$user_id = $db['user_id'];
}
$channels = file_get_contents($d->path.$f);
$rows = substr_count($channels, "\n");
$channels = Database::escape($channels);
$time = filemtime($d->path.$f);
echo "$type - $address\n";
$db->query ("INSERT INTO channellist (time, user_id, lat, lng, cc, address, type, `rows`, channels) VALUES (FROM_UNIXTIME('$time'), '$user_id', '$lat', '$lng', '$cc', '$address', '$type', '$rows', '$channels')");
//break;
}
}
}
}
}
if (isset($_GET['update'])) {
// Update sat address
echo "Update:\n";
$db = new Database ("data", $GLOBALS['config']['db']);
foreach ($satteliten as $type => $name) {
$db->query ("UPDATE channellist SET address='$name' WHERE type='$type'");
}
}
/*
if (isset($_GET['update_address'])) {
// update place address
echo "Update:\n";
$db = new Database ("data", $GLOBALS['config']['db']);
$db2 = new Database ("data", $GLOBALS['config']['db']);
if ($db->query ("SELECT id, address, lat, lng, type FROM channellist WHERE address!='' AND (type='T' OR type='T2' OR type='C' OR type='I') ORDER BY id")) {
foreach ($db as $key => $row) {
$cc = 'DE';
$address = getAddress($row['lat'], $row['lng'], $cc);
$db2->query ("UPDATE channellist SET address='$address' WHERE id={$row['id']}");
echo "{$row['id']}\n{$row['address']}\n$address\n\n";
flush();
}
}
}
*/