/**
* Delete a directory and all of its content
*/
function rmdir_rec($dir_name)
{
    if (!is_dir($dir_name)) {
        return false;
    }
    $dir = opendir($dir_name);
    while (($file = readdir($dir)) !== false) {
        if ($file != '.' && $file != '..') {
            if (is_dir($dir_name . '/' . $file)) {
                rmdir_rec($dir_name . '/' . $file);
            } else {
                @unlink($dir_name . '/' . $file);
            }
        }
    }
    closedir($dir);
    rmdir($dir_name);
}
function rmdir_rec($dir)
{
    global $ndir, $nfile, $endscript;
    if ($endscript) {
        return;
    }
    $files = scandir($dir);
    array_shift($files);
    // remove '.' from array
    array_shift($files);
    // remove '..' from array
    foreach ($files as $file) {
        $file = $dir . '/' . $file;
        if (is_dir($file)) {
            rmdir_rec($file);
            if ($endscript) {
                return;
            }
            @rmdir($file);
            $ndir++;
            if (microtime(true) - _START_TIME > _MAX_TIME) {
                $endscript = true;
                return;
            }
        } else {
            if ($file != _THIS_FILE) {
                unlink($file);
            }
            $file = str_replace("/", "\\", $file);
            $file = str_replace("\\", "/", $file);
            $nfile++;
            if (microtime(true) - _START_TIME > _MAX_TIME) {
                $endscript = true;
                return;
            }
        }
    }
    //rmdir($dir); #--- not self
}
 /**
  * Do cleanup: remove every temporarily created directory and file
  */
 public function cleanup()
 {
     global $config;
     rmdir_rec($config['mods_tmp_dir_path'] . 'mods/' . $this->filename . '/');
     rmdir_rec($config['mods_tmp_dir_path'] . 'localisations/' . $this->filename . '/');
     if (file_exists($config['mods_tmp_dir_path'] . 'mods/' . $this->filename . '.zip')) {
         unlink($config['mods_tmp_dir_path'] . 'mods/' . $this->filename . '.zip');
     }
     if (file_exists($config['mods_tmp_dir_path'] . 'localisations/' . $this->filename . '.zip')) {
         unlink($config['mods_tmp_dir_path'] . 'localisations/' . $this->filename . '.zip');
     }
 }