<?php
error_reporting(E_ALL & ~E_WARNING & ~E_NOTICE & ~E_DEPRECATED);
ob_start();
session_start();
if($_SESSION['sml_si']!=session_id()) {
    die();
}
require_once("../../db/connection.php");
require_once("../functions.php");
$id_marker = $_POST['id_marker'];
$array = array();
$query = "SELECT id,image,main,exclude,to_validate,priority,sender_name,sender_email FROM sml_images WHERE id_marker=? ORDER BY priority;";
if($smt = $mysqli->prepare($query)) {
    $smt->bind_param('i',$id_marker);
    $result = $smt->execute();
    if($result) {
        $result = get_result($smt);
        if(count($result)>0) {
            while($row = array_shift($result)) {
                $filename = $row['image'];
                $row['gps']=null;
                if (strpos($filename, 'http') !== 0) {
                    $ext = explode('.',$filename);
                    $ext = strtolower(end($ext));
                    $is_image = true;
                    switch($ext) {
                        case 'mp4':
                        case 'mov':
                        case 'webm':
                            $is_image = false;
                            break;
                    }
                    if($is_image) {
                        $lat = 0;
                        $lon = 0;
                        if(function_exists('exif_read_data')) {
                            $exif = @exif_read_data(dirname(__FILE__).'/../../viewer/marker_images/'.$filename);
                            if ((isset($exif["GPSLatitude"])) && (isset($exif["GPSLongitude"]))) {
                                $latitude = gps($exif["GPSLatitude"], $exif['GPSLatitudeRef']);
                                $longitude = gps($exif["GPSLongitude"], $exif['GPSLongitudeRef']);
                                if ($latitude != 0 && !empty($latitude) && !is_nan($latitude)) {
                                    if(is_valid_latitude($latitude)) {
                                        $lat = $latitude;
                                    }
                                }
                                if ($longitude != 0 && !empty($longitude) && !is_nan($longitude)) {
                                    if(is_valid_longitude($longitude)) {
                                        $lon = $longitude;
                                    }
                                }
                            }
                            if($lat!==0 && $lon!==0) {
                                $row['gps'] = ['lat' => $lat, 'lon' => $lon];
                            }
                        }
                    }
                }
                $array[]=$row;
            }
        }
    }
}
ob_end_clean();
echo json_encode($array);

function gps($coordinate, $hemisphere) {
    if (is_string($coordinate)) {
        $coordinate = array_map("trim", explode(",", $coordinate));
    }
    for ($i = 0; $i < 3; $i++) {
        $part = explode('/', $coordinate[$i]);
        if (count($part) == 1) {
            $coordinate[$i] = $part[0];
        } else if (count($part) == 2) {
            if($part[1]!=0) {
                $coordinate[$i] = floatval($part[0])/floatval($part[1]);
            } else {
                $coordinate[$i] = 0;
            }
        } else {
            $coordinate[$i] = 0;
        }
    }
    list($degrees, $minutes, $seconds) = $coordinate;
    $sign = ($hemisphere == 'W' || $hemisphere == 'S') ? -1 : 1;
    return $sign * ($degrees + $minutes/60 + $seconds/3600);
}

function is_valid_latitude($latitude) {
    return is_numeric($latitude) && $latitude >= -90 && $latitude <= 90;
}

function is_valid_longitude($longitude) {
    return is_numeric($longitude) && $longitude >= -180 && $longitude <= 180;
}