<?php
die();
require_once("/home/fundbridge/crons.ballng.com/db.php");

$ASSET_URL = "/home/fundbridge/assets.ballng.com/";
function checkDelete($folder,$to_remove){
    global $ASSET_URL;
    $to_delete = array_diff(scandir($ASSET_URL.$folder), $to_remove);
    foreach($to_delete as $f){
        unlink($ASSET_URL."$folder/$f");
    }
}

// check admins photos
$users = mysqli_fetch_assoc(mysqli_query($conn, "select group_concat(photo separator ',') as photos  from admins  where photo != ''"))["photos"];
if($users){
    $to_remove = array_merge(explode(",", $users), array('.', '..'));
    checkDelete("admins",$to_remove);
}

// check users photos
$users = mysqli_fetch_assoc(mysqli_query($conn, "select group_concat(photo separator ',') as photos  from users  where photo != ''"))['photos'];
if($users){
    $to_remove = array_merge(explode(",", $users), array('.', '..'));
    checkDelete("users",$to_remove);
}

// check ids
$ids = mysqli_fetch_assoc(mysqli_query($conn, "select group_concat(photo separator ',') as photos  from verifications  where photo != ''"))['photos'];
if($ids){
    $to_remove = array_merge(explode(",", $ids), array('.', '..'));
    checkDelete("identifications",$to_remove);
}

//check selfie
$selfie = mysqli_fetch_assoc(mysqli_query($conn, "select group_concat(selfie separator ',') as selfies  from verifications  where selfie != ''"))['selfies'];
if($selfie){
    $to_remove = array_merge(explode(",", $selfie), array('.', '..'));
    checkDelete("selfie",$to_remove);
}

//check promos
$promos = mysqli_fetch_assoc(mysqli_query($conn, "select group_concat(image separator ',') as images  from promo  where image != ''"))['images'];
if($promos){
    $to_remove = array_merge(explode(",", $promos), array('.', '..'));
    checkDelete("promos",$to_remove);
}
$conn->close();
?>