<?php
defined('C5_EXECUTE') or die(_("Access Denied."));
 
/*
Install
 
    Copy this file to /jobs
    Dashboard > System & Settings > Automated Jobs
    Install "Clean Temp Sessions"
    Automate the job to your liking
 
*/
 
class CleanTempSessions extends Job {
    public function getJobName() {
        return t('Clean Temp Sessions');
    }
 
    public function getJobDescription() {
        return t('Cleans up PHP sess files from /files/tmp that are older than one day.');
    }
 
    public function run() {
        $dir = DIR_BASE . '/files/tmp';
        $unlink_count = 0;
        if ($handle = opendir($dir)) {
            while (false !== ($file = readdir($handle))) {
                if (substr($file, 0, 5) === 'sess_') {
                    $filelastmodified = filemtime($dir . '/' . $file);
                    if ((time() - $filelastmodified) > (24 * 3600)) {
                       @unlink($dir . '/' . $file);
                       $unlink_count = $unlink_count + 1;
                    }
                }
            }
            closedir($handle);
        }
        return $unlink_count . ' files cleaned up.';
    }
}