<?php
require_once('_common.php');
require_once(ROOT.W.F_FUNC.W.'class._JSON.php');
require_once(ROOT.W.F_CONFIG.W.DB.PHP);
require_once(ROOT.W.F_CONFIG.W.SETTINGS.PHP);
require_once(ROOT.W.F_CONFIG.W.SESSION.PHP);

$rs = $DB->Select('GRADE', 'MEMBER', "NO='{$SESSION->NoMember()}'");
$member = $DB->Get($rs);
if($member['GRADE'] < 6) {
    $response['result'] = 'FAIL';
    $response['msg'] = '운영자 권한이 없습니다.';
    sleep(1);
    echo json_encode($response);
}


$targetTime = strtotime("-1 month");

$picDir = ROOT.W.D.'pic';

$usedImg = [];

$imgResult = $DB->Select('PICTURE', 'MEMBER', 'IMGSVR=1');
foreach($DB->GetAll($imgResult) as $row){
    $imgname = $row['PICTURE'];
    if(strlen($imgname)<(1+1+3+2+8)){ //*.jpg?=YYYYMMDD
        continue;
    }
    $imgname = substr($imgname, 0, strlen($imgname)-10);
    $usedImg[$imgname] = true;
}

foreach(glob($picDir."/*.{jpg,png,gif}", GLOB_BRACE) as $filepath){
    $filename = basename($filepath);
    
    if (array_key_exists($filename, $usedImg)) {
        continue;
    }

    $mtime = filemtime($filepath);
    if($mtime > $targetTime){
        continue;
    }

    @unlink($filepath);
}

sleep(1);
echo json_encode(['result'=>'SUCCESS']);
