<?php
error_reporting(E_ALL & ~E_WARNING & ~E_NOTICE & ~E_DEPRECATED);
ob_start();
session_start();
require_once(__DIR__."/../db/connection.php");
require_once(__DIR__ . "/../config/config.inc.php");
$path = __DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR;
$array_marker_image_files = array();
$array_geo_image_files = array();
$array_geo_data_files = array();
$array_logo_files = array();
$array_icon_files = array();
$array_content_files = array();
$array_media_library_files = array();

$query = "SELECT logo,logo_mobile,song,background_image,background_image_mobile,intro_image,meta_image FROM sml_maps;";
$result = $mysqli->query($query);
if($result) {
    if($result->num_rows>0) {
        while($row=$result->fetch_array(MYSQLI_ASSOC)) {
            $logo = $row['logo'];
            if($logo!='') {
                if(!in_array($logo,$array_logo_files)) {
                    array_push($array_logo_files,$logo);
                }
            }
            $logo_mobile = $row['logo_mobile'];
            if($logo_mobile!='') {
                if(!in_array($logo_mobile,$array_logo_files)) {
                    array_push($array_logo_files,$logo_mobile);
                }
            }
            $song = $row['song'];
            if($song!='') {
                if(!in_array($song,$array_content_files)) {
                    array_push($array_content_files,$song);
                }
            }
            $background_image = $row['background_image'];
            if($background_image!='') {
                if(!in_array($background_image,$array_content_files)) {
                    array_push($array_content_files,$background_image);
                }
            }
            $background_image_mobile = $row['background_image_mobile'];
            if($background_image_mobile!='') {
                if(!in_array($background_image_mobile,$array_content_files)) {
                    array_push($array_content_files,$background_image_mobile);
                }
            }
            $intro_image = $row['intro_image'];
            if($intro_image!='') {
                if(!in_array($intro_image,$array_content_files)) {
                    array_push($array_content_files,$intro_image);
                }
            }
            $meta_image = $row['meta_image'];
            if($meta_image!='') {
                if(!in_array($meta_image,$array_content_files)) {
                    array_push($array_content_files,$meta_image);
                }
            }
        }
    }
}
$query = "SELECT song FROM sml_markers;";
$result = $mysqli->query($query);
if($result) {
    if($result->num_rows>0) {
        while($row=$result->fetch_array(MYSQLI_ASSOC)) {
            $song = $row['song'];
            if($song!='') {
                if(!in_array($song,$array_content_files)) {
                    array_push($array_content_files,$song);
                }
            }
        }
    }
}
$query = "SELECT logo,banner FROM sml_showcases;";
$result = $mysqli->query($query);
if($result) {
    if($result->num_rows>0) {
        while($row=$result->fetch_array(MYSQLI_ASSOC)) {
            $logo = $row['logo'];
            if($logo!='') {
                if(!in_array($logo,$array_content_files)) {
                    array_push($array_content_files,$logo);
                }
            }
            $banner = $row['banner'];
            if($banner!='') {
                if(!in_array($banner,$array_content_files)) {
                    array_push($array_content_files,$banner);
                }
            }
        }
    }
}
$query = "SELECT image FROM sml_icons;";
$result = $mysqli->query($query);
if($result) {
    if($result->num_rows>0) {
        while($row=$result->fetch_array(MYSQLI_ASSOC)) {
            $image = $row['image'];
            if($image!='') {
                if(!in_array($image,$array_icon_files)) {
                    array_push($array_icon_files,$image);
                }
            }
        }
    }
}
$query = "SELECT file FROM sml_media_library;";
$result = $mysqli->query($query);
if($result) {
    if($result->num_rows>0) {
        while($row=$result->fetch_array(MYSQLI_ASSOC)) {
            $file = $row['file'];
            if($file!='') {
                if(!in_array($file,$array_media_library_files)) {
                    array_push($array_media_library_files,$file);
                }
            }
        }
    }
}
$query = "SELECT image FROM sml_images;";
$result = $mysqli->query($query);
if($result) {
    if($result->num_rows>0) {
        while($row=$result->fetch_array(MYSQLI_ASSOC)) {
            $image = $row['image'];
            if (strpos($image, 'http') !== 0) {
                if($image!='') {
                    if(!in_array($image,$array_marker_image_files)) {
                        array_push($array_marker_image_files,$image);
                    }
                }
                $image_orig = "o_".$row['image'];
                if($image_orig!='') {
                    if(!in_array($image_orig,$array_marker_image_files)) {
                        array_push($array_marker_image_files,$image_orig);
                    }
                }
            }
        }
    }
}
$query = "SELECT image FROM sml_geoimages;";
$result = $mysqli->query($query);
if($result) {
    if($result->num_rows>0) {
        while($row=$result->fetch_array(MYSQLI_ASSOC)) {
            $image = $row['image'];
            if($image!='') {
                if(!in_array($image,$array_geo_image_files)) {
                    array_push($array_geo_image_files,$image);
                }
            }
        }
    }
}
$query = "SELECT file FROM sml_geodata;";
$result = $mysqli->query($query);
if($result) {
    if($result->num_rows>0) {
        while($row=$result->fetch_array(MYSQLI_ASSOC)) {
            $file = $row['file'];
            if($file!='') {
                if(!in_array($file,$array_geo_data_files)) {
                    array_push($array_geo_data_files,$file);
                }
            }
        }
    }
}
check_files($path."viewer".DIRECTORY_SEPARATOR."content".DIRECTORY_SEPARATOR,$array_content_files);
check_files($path."viewer".DIRECTORY_SEPARATOR."marker_images".DIRECTORY_SEPARATOR,$array_marker_image_files);
check_files($path."viewer".DIRECTORY_SEPARATOR."marker_images".DIRECTORY_SEPARATOR.'thumb'.DIRECTORY_SEPARATOR,$array_marker_image_files);
check_files($path."viewer".DIRECTORY_SEPARATOR."geoimages".DIRECTORY_SEPARATOR,$array_geo_image_files);
check_files($path."viewer".DIRECTORY_SEPARATOR."geojson".DIRECTORY_SEPARATOR,$array_geo_data_files);
check_files($path."viewer".DIRECTORY_SEPARATOR."logos".DIRECTORY_SEPARATOR,$array_logo_files);
check_files($path."viewer".DIRECTORY_SEPARATOR."icons".DIRECTORY_SEPARATOR,$array_icon_files);
check_files($path."viewer".DIRECTORY_SEPARATOR."media".DIRECTORY_SEPARATOR,$array_media_library_files);
check_files($path."viewer".DIRECTORY_SEPARATOR."media".DIRECTORY_SEPARATOR.'thumb'.DIRECTORY_SEPARATOR,$array_media_library_files);
ob_end_clean();

function check_files($path_file,$array) {
    if ($dh = opendir($path_file)) {
        while (($filename = readdir($dh)) !== false) {
            if ($filename != "." && $filename != "..") {
                if(is_file($path_file.$filename)) {
                    if(!in_array($filename,$array)) {
                        $fileModTime = filemtime($path_file.$filename);
                        $fileAgeMinutes = (time() - $fileModTime) / 60;
                        if ($fileAgeMinutes >= 5) {
                            unlink($path_file.$filename);
                        }
                    }
                }
            }
        }
        closedir($dh);
    }
}