<?php

namespace FOP;

class Cron {
  
  

  
  

    // --- Statische Methoden ---
  
  public static function indexAction() { 
    
    
    // --- BLOCK: a-logout ---
    
		$userid = \FOP\User::getID();
		\FOP\User::logout(false);
		try {
	
    // --- BLOCK: access ---
    
    // --- BLOCK: main ---
    
		$updateTimes = true;
		set_time_limit(60 * 4); // 4min, da nach 5min die gefailten Crons restartet werden
		ignore_user_abort(true); // abort ignorieren
		global $argc, $argv;
		if(isset($argc) && $argc > 1 && $argv[1] == 'notimes') {
			$updateTimes = false;
			error_log('Cron time updates disabled by command line');
			 // tarife
			error_log('tarife');
			    \Tables\Mitglied::applyTarif();
			 // Tables:Aufgabe:repeatCron
			error_log('Tables:Aufgabe:repeatCron');
			    \Tables\Aufgabe::repeatCron();
			 // FOP:Notification:instantMail
			error_log('FOP:Notification:instantMail');
			    \FOP\Notification::instantMail();
			 // FOP:Notification:hourlyMail
			error_log('FOP:Notification:hourlyMail');
			    \FOP\Notification::hourlyMail();
			 // FOP:Notification:dailyMail
			error_log('FOP:Notification:dailyMail');
			    \FOP\Notification::dailyMail();
			 // FOP:Notification:weeklyMail
			error_log('FOP:Notification:weeklyMail');
			    \FOP\Notification::weeklyMail();
			 // FOP:Notification:monthlyMail
			error_log('FOP:Notification:monthlyMail');
			    \FOP\Notification::monthlyMail();
			 // FOP:Session:cron
			error_log('FOP:Session:cron');
			    \FOP\Session::cron();
			 // delayedmethods
			error_log('delayedmethods');
			    \FOP\ClassStaticMethodDelayed::worker();
			 // ImapListenerCron
			error_log('ImapListenerCron');
			    \FOP\Imap::getImapListenerCron();
			 // Umsatzdownload
			error_log('Umsatzdownload');
			    \Tables\Fop_fints_konto::syncAll();
			 // mailingcron
			error_log('mailingcron');
			    \Tables\Campaign::mailingCron();
			 // filesgc
			error_log('filesgc');
			    \Tables\CampaignTarget::cronTimeouts();
			 // clearchatsubscriptionsgruppe
			error_log('clearchatsubscriptionsgruppe');
			    \Chat\Gruppe::clearSubscriptions();
			 // filesgc
			error_log('filesgc');
			    \FOP\Files::removeOld();
			 // fail2ban
			error_log('fail2ban');
			    \FOP\Login::fail2banCron();
			
			return;
		}
		 // tarife
		if (\FOP\Database::getDriver() == 'mysql') {
			\FOP\Database::query('LOCK TABLES cron WRITE');
		} else {
			\FOP\Database::beginTransaction();
			\FOP\Database::query('LOCK TABLE cron');
		}
		$lastrun = \FOP\Database::query('SELECT lastrun FROM cron WHERE name = \'tarife\'')->fetchColumn();
		if ((self::willRunNow('1d', $lastrun))) {
			$cronstart = microtime(true);
			try {
				// Prüfzeitstempel auf +5min setzen, damit CronJob nicht mehrfach gestartet wird
				if ($lastrun === false) {
					\FOP\Database::query('INSERT '.(\FOP\Database::getDriver() == 'mysql' ? 'IGNORE ' : '') . 'INTO cron(name, lastrun) VALUES(' . \FOP\Database::quote('tarife') . ', '.time().'+5*60)'.(\FOP\Database::getDriver() != 'mysql' ? ' ON CONFLICT (name) DO NOTHING' : ''));
				} else {
					\FOP\Database::query('UPDATE cron SET lastrun='.time().'+5*60 WHERE name=' . \FOP\Database::quote('tarife'));
				}
				if (\FOP\Database::getDriver() == 'mysql') {
					\FOP\Database::query('UNLOCK TABLES');
				} else {
					\FOP\Database::commitTransaction();
				}
				\FOP\User::updateColumnsAdmin(); // crons are executed as admin by default (crons can change this by ::login(id) and ::updateColumns())
				\Tables\Mitglied::applyTarif();
			} finally {
				if (\FOP\Database::getDriver() == 'mysql') {
					\FOP\Database::query('UNLOCK TABLES');
				} else if (\FOP\Database::inTransaction()) {
					// roll back unfinished tx
					\FOP\Database::rollbackTransaction();
				}
				// braucht keinen Lock, da single expression
				$crondurationSQL = json_encode(microtime(true) - $cronstart);
				\FOP\Database::query('UPDATE cron SET medianruntime = COALESCE(0.9 * medianruntime + 0.1 * '.$crondurationSQL.', '.$crondurationSQL.'), lastrun = '.time().' WHERE name=' . \FOP\Database::quote('tarife'));
			}
		} else {
			if (\FOP\Database::getDriver() == 'mysql') {
				\FOP\Database::query('UNLOCK TABLES');
			} else {
				\FOP\Database::rollbackTransaction();
			}
		}
 // Tables:Aufgabe:repeatCron
		if (\FOP\Database::getDriver() == 'mysql') {
			\FOP\Database::query('LOCK TABLES cron WRITE');
		} else {
			\FOP\Database::beginTransaction();
			\FOP\Database::query('LOCK TABLE cron');
		}
		$lastrun = \FOP\Database::query('SELECT lastrun FROM cron WHERE name = \'Tables:Aufgabe:repeatCron\'')->fetchColumn();
		if ((self::willRunNow('24h', $lastrun))) {
			$cronstart = microtime(true);
			try {
				// Prüfzeitstempel auf +5min setzen, damit CronJob nicht mehrfach gestartet wird
				if ($lastrun === false) {
					\FOP\Database::query('INSERT '.(\FOP\Database::getDriver() == 'mysql' ? 'IGNORE ' : '') . 'INTO cron(name, lastrun) VALUES(' . \FOP\Database::quote('Tables:Aufgabe:repeatCron') . ', '.time().'+5*60)'.(\FOP\Database::getDriver() != 'mysql' ? ' ON CONFLICT (name) DO NOTHING' : ''));
				} else {
					\FOP\Database::query('UPDATE cron SET lastrun='.time().'+5*60 WHERE name=' . \FOP\Database::quote('Tables:Aufgabe:repeatCron'));
				}
				if (\FOP\Database::getDriver() == 'mysql') {
					\FOP\Database::query('UNLOCK TABLES');
				} else {
					\FOP\Database::commitTransaction();
				}
				\FOP\User::updateColumnsAdmin(); // crons are executed as admin by default (crons can change this by ::login(id) and ::updateColumns())
				\Tables\Aufgabe::repeatCron();
			} finally {
				if (\FOP\Database::getDriver() == 'mysql') {
					\FOP\Database::query('UNLOCK TABLES');
				} else if (\FOP\Database::inTransaction()) {
					// roll back unfinished tx
					\FOP\Database::rollbackTransaction();
				}
				// braucht keinen Lock, da single expression
				$crondurationSQL = json_encode(microtime(true) - $cronstart);
				\FOP\Database::query('UPDATE cron SET medianruntime = COALESCE(0.9 * medianruntime + 0.1 * '.$crondurationSQL.', '.$crondurationSQL.'), lastrun = '.time().' WHERE name=' . \FOP\Database::quote('Tables:Aufgabe:repeatCron'));
			}
		} else {
			if (\FOP\Database::getDriver() == 'mysql') {
				\FOP\Database::query('UNLOCK TABLES');
			} else {
				\FOP\Database::rollbackTransaction();
			}
		}
 // FOP:Notification:instantMail
		if (\FOP\Database::getDriver() == 'mysql') {
			\FOP\Database::query('LOCK TABLES cron WRITE');
		} else {
			\FOP\Database::beginTransaction();
			\FOP\Database::query('LOCK TABLE cron');
		}
		$lastrun = \FOP\Database::query('SELECT lastrun FROM cron WHERE name = \'FOP:Notification:instantMail\'')->fetchColumn();
		if ((self::willRunNow('1m', $lastrun))) {
			$cronstart = microtime(true);
			try {
				// Prüfzeitstempel auf +5min setzen, damit CronJob nicht mehrfach gestartet wird
				if ($lastrun === false) {
					\FOP\Database::query('INSERT '.(\FOP\Database::getDriver() == 'mysql' ? 'IGNORE ' : '') . 'INTO cron(name, lastrun) VALUES(' . \FOP\Database::quote('FOP:Notification:instantMail') . ', '.time().'+5*60)'.(\FOP\Database::getDriver() != 'mysql' ? ' ON CONFLICT (name) DO NOTHING' : ''));
				} else {
					\FOP\Database::query('UPDATE cron SET lastrun='.time().'+5*60 WHERE name=' . \FOP\Database::quote('FOP:Notification:instantMail'));
				}
				if (\FOP\Database::getDriver() == 'mysql') {
					\FOP\Database::query('UNLOCK TABLES');
				} else {
					\FOP\Database::commitTransaction();
				}
				\FOP\User::updateColumnsAdmin(); // crons are executed as admin by default (crons can change this by ::login(id) and ::updateColumns())
				\FOP\Notification::instantMail();
			} finally {
				if (\FOP\Database::getDriver() == 'mysql') {
					\FOP\Database::query('UNLOCK TABLES');
				} else if (\FOP\Database::inTransaction()) {
					// roll back unfinished tx
					\FOP\Database::rollbackTransaction();
				}
				// braucht keinen Lock, da single expression
				$crondurationSQL = json_encode(microtime(true) - $cronstart);
				\FOP\Database::query('UPDATE cron SET medianruntime = COALESCE(0.9 * medianruntime + 0.1 * '.$crondurationSQL.', '.$crondurationSQL.'), lastrun = '.time().' WHERE name=' . \FOP\Database::quote('FOP:Notification:instantMail'));
			}
		} else {
			if (\FOP\Database::getDriver() == 'mysql') {
				\FOP\Database::query('UNLOCK TABLES');
			} else {
				\FOP\Database::rollbackTransaction();
			}
		}
 // FOP:Notification:hourlyMail
		if (\FOP\Database::getDriver() == 'mysql') {
			\FOP\Database::query('LOCK TABLES cron WRITE');
		} else {
			\FOP\Database::beginTransaction();
			\FOP\Database::query('LOCK TABLE cron');
		}
		$lastrun = \FOP\Database::query('SELECT lastrun FROM cron WHERE name = \'FOP:Notification:hourlyMail\'')->fetchColumn();
		if ((self::willRunNow('1h', $lastrun))) {
			$cronstart = microtime(true);
			try {
				// Prüfzeitstempel auf +5min setzen, damit CronJob nicht mehrfach gestartet wird
				if ($lastrun === false) {
					\FOP\Database::query('INSERT '.(\FOP\Database::getDriver() == 'mysql' ? 'IGNORE ' : '') . 'INTO cron(name, lastrun) VALUES(' . \FOP\Database::quote('FOP:Notification:hourlyMail') . ', '.time().'+5*60)'.(\FOP\Database::getDriver() != 'mysql' ? ' ON CONFLICT (name) DO NOTHING' : ''));
				} else {
					\FOP\Database::query('UPDATE cron SET lastrun='.time().'+5*60 WHERE name=' . \FOP\Database::quote('FOP:Notification:hourlyMail'));
				}
				if (\FOP\Database::getDriver() == 'mysql') {
					\FOP\Database::query('UNLOCK TABLES');
				} else {
					\FOP\Database::commitTransaction();
				}
				\FOP\User::updateColumnsAdmin(); // crons are executed as admin by default (crons can change this by ::login(id) and ::updateColumns())
				\FOP\Notification::hourlyMail();
			} finally {
				if (\FOP\Database::getDriver() == 'mysql') {
					\FOP\Database::query('UNLOCK TABLES');
				} else if (\FOP\Database::inTransaction()) {
					// roll back unfinished tx
					\FOP\Database::rollbackTransaction();
				}
				// braucht keinen Lock, da single expression
				$crondurationSQL = json_encode(microtime(true) - $cronstart);
				\FOP\Database::query('UPDATE cron SET medianruntime = COALESCE(0.9 * medianruntime + 0.1 * '.$crondurationSQL.', '.$crondurationSQL.'), lastrun = '.time().' WHERE name=' . \FOP\Database::quote('FOP:Notification:hourlyMail'));
			}
		} else {
			if (\FOP\Database::getDriver() == 'mysql') {
				\FOP\Database::query('UNLOCK TABLES');
			} else {
				\FOP\Database::rollbackTransaction();
			}
		}
 // FOP:Notification:dailyMail
		if (\FOP\Database::getDriver() == 'mysql') {
			\FOP\Database::query('LOCK TABLES cron WRITE');
		} else {
			\FOP\Database::beginTransaction();
			\FOP\Database::query('LOCK TABLE cron');
		}
		$lastrun = \FOP\Database::query('SELECT lastrun FROM cron WHERE name = \'FOP:Notification:dailyMail\'')->fetchColumn();
		if ((self::willRunNow('24h', $lastrun))) {
			$cronstart = microtime(true);
			try {
				// Prüfzeitstempel auf +5min setzen, damit CronJob nicht mehrfach gestartet wird
				if ($lastrun === false) {
					\FOP\Database::query('INSERT '.(\FOP\Database::getDriver() == 'mysql' ? 'IGNORE ' : '') . 'INTO cron(name, lastrun) VALUES(' . \FOP\Database::quote('FOP:Notification:dailyMail') . ', '.time().'+5*60)'.(\FOP\Database::getDriver() != 'mysql' ? ' ON CONFLICT (name) DO NOTHING' : ''));
				} else {
					\FOP\Database::query('UPDATE cron SET lastrun='.time().'+5*60 WHERE name=' . \FOP\Database::quote('FOP:Notification:dailyMail'));
				}
				if (\FOP\Database::getDriver() == 'mysql') {
					\FOP\Database::query('UNLOCK TABLES');
				} else {
					\FOP\Database::commitTransaction();
				}
				\FOP\User::updateColumnsAdmin(); // crons are executed as admin by default (crons can change this by ::login(id) and ::updateColumns())
				\FOP\Notification::dailyMail();
			} finally {
				if (\FOP\Database::getDriver() == 'mysql') {
					\FOP\Database::query('UNLOCK TABLES');
				} else if (\FOP\Database::inTransaction()) {
					// roll back unfinished tx
					\FOP\Database::rollbackTransaction();
				}
				// braucht keinen Lock, da single expression
				$crondurationSQL = json_encode(microtime(true) - $cronstart);
				\FOP\Database::query('UPDATE cron SET medianruntime = COALESCE(0.9 * medianruntime + 0.1 * '.$crondurationSQL.', '.$crondurationSQL.'), lastrun = '.time().' WHERE name=' . \FOP\Database::quote('FOP:Notification:dailyMail'));
			}
		} else {
			if (\FOP\Database::getDriver() == 'mysql') {
				\FOP\Database::query('UNLOCK TABLES');
			} else {
				\FOP\Database::rollbackTransaction();
			}
		}
 // FOP:Notification:weeklyMail
		if (\FOP\Database::getDriver() == 'mysql') {
			\FOP\Database::query('LOCK TABLES cron WRITE');
		} else {
			\FOP\Database::beginTransaction();
			\FOP\Database::query('LOCK TABLE cron');
		}
		$lastrun = \FOP\Database::query('SELECT lastrun FROM cron WHERE name = \'FOP:Notification:weeklyMail\'')->fetchColumn();
		if ((self::willRunNow('168h', $lastrun))) {
			$cronstart = microtime(true);
			try {
				// Prüfzeitstempel auf +5min setzen, damit CronJob nicht mehrfach gestartet wird
				if ($lastrun === false) {
					\FOP\Database::query('INSERT '.(\FOP\Database::getDriver() == 'mysql' ? 'IGNORE ' : '') . 'INTO cron(name, lastrun) VALUES(' . \FOP\Database::quote('FOP:Notification:weeklyMail') . ', '.time().'+5*60)'.(\FOP\Database::getDriver() != 'mysql' ? ' ON CONFLICT (name) DO NOTHING' : ''));
				} else {
					\FOP\Database::query('UPDATE cron SET lastrun='.time().'+5*60 WHERE name=' . \FOP\Database::quote('FOP:Notification:weeklyMail'));
				}
				if (\FOP\Database::getDriver() == 'mysql') {
					\FOP\Database::query('UNLOCK TABLES');
				} else {
					\FOP\Database::commitTransaction();
				}
				\FOP\User::updateColumnsAdmin(); // crons are executed as admin by default (crons can change this by ::login(id) and ::updateColumns())
				\FOP\Notification::weeklyMail();
			} finally {
				if (\FOP\Database::getDriver() == 'mysql') {
					\FOP\Database::query('UNLOCK TABLES');
				} else if (\FOP\Database::inTransaction()) {
					// roll back unfinished tx
					\FOP\Database::rollbackTransaction();
				}
				// braucht keinen Lock, da single expression
				$crondurationSQL = json_encode(microtime(true) - $cronstart);
				\FOP\Database::query('UPDATE cron SET medianruntime = COALESCE(0.9 * medianruntime + 0.1 * '.$crondurationSQL.', '.$crondurationSQL.'), lastrun = '.time().' WHERE name=' . \FOP\Database::quote('FOP:Notification:weeklyMail'));
			}
		} else {
			if (\FOP\Database::getDriver() == 'mysql') {
				\FOP\Database::query('UNLOCK TABLES');
			} else {
				\FOP\Database::rollbackTransaction();
			}
		}
 // FOP:Notification:monthlyMail
		if (\FOP\Database::getDriver() == 'mysql') {
			\FOP\Database::query('LOCK TABLES cron WRITE');
		} else {
			\FOP\Database::beginTransaction();
			\FOP\Database::query('LOCK TABLE cron');
		}
		$lastrun = \FOP\Database::query('SELECT lastrun FROM cron WHERE name = \'FOP:Notification:monthlyMail\'')->fetchColumn();
		if ((self::willRunNow('@month', $lastrun))) {
			$cronstart = microtime(true);
			try {
				// Prüfzeitstempel auf +5min setzen, damit CronJob nicht mehrfach gestartet wird
				if ($lastrun === false) {
					\FOP\Database::query('INSERT '.(\FOP\Database::getDriver() == 'mysql' ? 'IGNORE ' : '') . 'INTO cron(name, lastrun) VALUES(' . \FOP\Database::quote('FOP:Notification:monthlyMail') . ', '.time().'+5*60)'.(\FOP\Database::getDriver() != 'mysql' ? ' ON CONFLICT (name) DO NOTHING' : ''));
				} else {
					\FOP\Database::query('UPDATE cron SET lastrun='.time().'+5*60 WHERE name=' . \FOP\Database::quote('FOP:Notification:monthlyMail'));
				}
				if (\FOP\Database::getDriver() == 'mysql') {
					\FOP\Database::query('UNLOCK TABLES');
				} else {
					\FOP\Database::commitTransaction();
				}
				\FOP\User::updateColumnsAdmin(); // crons are executed as admin by default (crons can change this by ::login(id) and ::updateColumns())
				\FOP\Notification::monthlyMail();
			} finally {
				if (\FOP\Database::getDriver() == 'mysql') {
					\FOP\Database::query('UNLOCK TABLES');
				} else if (\FOP\Database::inTransaction()) {
					// roll back unfinished tx
					\FOP\Database::rollbackTransaction();
				}
				// braucht keinen Lock, da single expression
				$crondurationSQL = json_encode(microtime(true) - $cronstart);
				\FOP\Database::query('UPDATE cron SET medianruntime = COALESCE(0.9 * medianruntime + 0.1 * '.$crondurationSQL.', '.$crondurationSQL.'), lastrun = '.time().' WHERE name=' . \FOP\Database::quote('FOP:Notification:monthlyMail'));
			}
		} else {
			if (\FOP\Database::getDriver() == 'mysql') {
				\FOP\Database::query('UNLOCK TABLES');
			} else {
				\FOP\Database::rollbackTransaction();
			}
		}
 // FOP:Session:cron
		if (\FOP\Database::getDriver() == 'mysql') {
			\FOP\Database::query('LOCK TABLES cron WRITE');
		} else {
			\FOP\Database::beginTransaction();
			\FOP\Database::query('LOCK TABLE cron');
		}
		$lastrun = \FOP\Database::query('SELECT lastrun FROM cron WHERE name = \'FOP:Session:cron\'')->fetchColumn();
		if ((self::willRunNow('1h', $lastrun))) {
			$cronstart = microtime(true);
			try {
				// Prüfzeitstempel auf +5min setzen, damit CronJob nicht mehrfach gestartet wird
				if ($lastrun === false) {
					\FOP\Database::query('INSERT '.(\FOP\Database::getDriver() == 'mysql' ? 'IGNORE ' : '') . 'INTO cron(name, lastrun) VALUES(' . \FOP\Database::quote('FOP:Session:cron') . ', '.time().'+5*60)'.(\FOP\Database::getDriver() != 'mysql' ? ' ON CONFLICT (name) DO NOTHING' : ''));
				} else {
					\FOP\Database::query('UPDATE cron SET lastrun='.time().'+5*60 WHERE name=' . \FOP\Database::quote('FOP:Session:cron'));
				}
				if (\FOP\Database::getDriver() == 'mysql') {
					\FOP\Database::query('UNLOCK TABLES');
				} else {
					\FOP\Database::commitTransaction();
				}
				\FOP\User::updateColumnsAdmin(); // crons are executed as admin by default (crons can change this by ::login(id) and ::updateColumns())
				\FOP\Session::cron();
			} finally {
				if (\FOP\Database::getDriver() == 'mysql') {
					\FOP\Database::query('UNLOCK TABLES');
				} else if (\FOP\Database::inTransaction()) {
					// roll back unfinished tx
					\FOP\Database::rollbackTransaction();
				}
				// braucht keinen Lock, da single expression
				$crondurationSQL = json_encode(microtime(true) - $cronstart);
				\FOP\Database::query('UPDATE cron SET medianruntime = COALESCE(0.9 * medianruntime + 0.1 * '.$crondurationSQL.', '.$crondurationSQL.'), lastrun = '.time().' WHERE name=' . \FOP\Database::quote('FOP:Session:cron'));
			}
		} else {
			if (\FOP\Database::getDriver() == 'mysql') {
				\FOP\Database::query('UNLOCK TABLES');
			} else {
				\FOP\Database::rollbackTransaction();
			}
		}
 // delayedmethods
		if (\FOP\Database::getDriver() == 'mysql') {
			\FOP\Database::query('LOCK TABLES cron WRITE');
		} else {
			\FOP\Database::beginTransaction();
			\FOP\Database::query('LOCK TABLE cron');
		}
		$lastrun = \FOP\Database::query('SELECT lastrun FROM cron WHERE name = \'delayedmethods\'')->fetchColumn();
		if ((self::willRunNow('1m', $lastrun))) {
			$cronstart = microtime(true);
			try {
				// Prüfzeitstempel auf +5min setzen, damit CronJob nicht mehrfach gestartet wird
				if ($lastrun === false) {
					\FOP\Database::query('INSERT '.(\FOP\Database::getDriver() == 'mysql' ? 'IGNORE ' : '') . 'INTO cron(name, lastrun) VALUES(' . \FOP\Database::quote('delayedmethods') . ', '.time().'+5*60)'.(\FOP\Database::getDriver() != 'mysql' ? ' ON CONFLICT (name) DO NOTHING' : ''));
				} else {
					\FOP\Database::query('UPDATE cron SET lastrun='.time().'+5*60 WHERE name=' . \FOP\Database::quote('delayedmethods'));
				}
				if (\FOP\Database::getDriver() == 'mysql') {
					\FOP\Database::query('UNLOCK TABLES');
				} else {
					\FOP\Database::commitTransaction();
				}
				\FOP\User::updateColumnsAdmin(); // crons are executed as admin by default (crons can change this by ::login(id) and ::updateColumns())
				\FOP\ClassStaticMethodDelayed::worker();
			} finally {
				if (\FOP\Database::getDriver() == 'mysql') {
					\FOP\Database::query('UNLOCK TABLES');
				} else if (\FOP\Database::inTransaction()) {
					// roll back unfinished tx
					\FOP\Database::rollbackTransaction();
				}
				// braucht keinen Lock, da single expression
				$crondurationSQL = json_encode(microtime(true) - $cronstart);
				\FOP\Database::query('UPDATE cron SET medianruntime = COALESCE(0.9 * medianruntime + 0.1 * '.$crondurationSQL.', '.$crondurationSQL.'), lastrun = '.time().' WHERE name=' . \FOP\Database::quote('delayedmethods'));
			}
		} else {
			if (\FOP\Database::getDriver() == 'mysql') {
				\FOP\Database::query('UNLOCK TABLES');
			} else {
				\FOP\Database::rollbackTransaction();
			}
		}
 // ImapListenerCron
		if (\FOP\Database::getDriver() == 'mysql') {
			\FOP\Database::query('LOCK TABLES cron WRITE');
		} else {
			\FOP\Database::beginTransaction();
			\FOP\Database::query('LOCK TABLE cron');
		}
		$lastrun = \FOP\Database::query('SELECT lastrun FROM cron WHERE name = \'ImapListenerCron\'')->fetchColumn();
		if ((self::willRunNow('2m', $lastrun))) {
			$cronstart = microtime(true);
			try {
				// Prüfzeitstempel auf +5min setzen, damit CronJob nicht mehrfach gestartet wird
				if ($lastrun === false) {
					\FOP\Database::query('INSERT '.(\FOP\Database::getDriver() == 'mysql' ? 'IGNORE ' : '') . 'INTO cron(name, lastrun) VALUES(' . \FOP\Database::quote('ImapListenerCron') . ', '.time().'+5*60)'.(\FOP\Database::getDriver() != 'mysql' ? ' ON CONFLICT (name) DO NOTHING' : ''));
				} else {
					\FOP\Database::query('UPDATE cron SET lastrun='.time().'+5*60 WHERE name=' . \FOP\Database::quote('ImapListenerCron'));
				}
				if (\FOP\Database::getDriver() == 'mysql') {
					\FOP\Database::query('UNLOCK TABLES');
				} else {
					\FOP\Database::commitTransaction();
				}
				\FOP\User::updateColumnsAdmin(); // crons are executed as admin by default (crons can change this by ::login(id) and ::updateColumns())
				\FOP\Imap::getImapListenerCron();
			} finally {
				if (\FOP\Database::getDriver() == 'mysql') {
					\FOP\Database::query('UNLOCK TABLES');
				} else if (\FOP\Database::inTransaction()) {
					// roll back unfinished tx
					\FOP\Database::rollbackTransaction();
				}
				// braucht keinen Lock, da single expression
				$crondurationSQL = json_encode(microtime(true) - $cronstart);
				\FOP\Database::query('UPDATE cron SET medianruntime = COALESCE(0.9 * medianruntime + 0.1 * '.$crondurationSQL.', '.$crondurationSQL.'), lastrun = '.time().' WHERE name=' . \FOP\Database::quote('ImapListenerCron'));
			}
		} else {
			if (\FOP\Database::getDriver() == 'mysql') {
				\FOP\Database::query('UNLOCK TABLES');
			} else {
				\FOP\Database::rollbackTransaction();
			}
		}
 // Umsatzdownload
		if (\FOP\Database::getDriver() == 'mysql') {
			\FOP\Database::query('LOCK TABLES cron WRITE');
		} else {
			\FOP\Database::beginTransaction();
			\FOP\Database::query('LOCK TABLE cron');
		}
		$lastrun = \FOP\Database::query('SELECT lastrun FROM cron WHERE name = \'Umsatzdownload\'')->fetchColumn();
		if ((self::willRunNow('1h', $lastrun))) {
			$cronstart = microtime(true);
			try {
				// Prüfzeitstempel auf +5min setzen, damit CronJob nicht mehrfach gestartet wird
				if ($lastrun === false) {
					\FOP\Database::query('INSERT '.(\FOP\Database::getDriver() == 'mysql' ? 'IGNORE ' : '') . 'INTO cron(name, lastrun) VALUES(' . \FOP\Database::quote('Umsatzdownload') . ', '.time().'+5*60)'.(\FOP\Database::getDriver() != 'mysql' ? ' ON CONFLICT (name) DO NOTHING' : ''));
				} else {
					\FOP\Database::query('UPDATE cron SET lastrun='.time().'+5*60 WHERE name=' . \FOP\Database::quote('Umsatzdownload'));
				}
				if (\FOP\Database::getDriver() == 'mysql') {
					\FOP\Database::query('UNLOCK TABLES');
				} else {
					\FOP\Database::commitTransaction();
				}
				\FOP\User::updateColumnsAdmin(); // crons are executed as admin by default (crons can change this by ::login(id) and ::updateColumns())
				\Tables\Fop_fints_konto::syncAll();
			} finally {
				if (\FOP\Database::getDriver() == 'mysql') {
					\FOP\Database::query('UNLOCK TABLES');
				} else if (\FOP\Database::inTransaction()) {
					// roll back unfinished tx
					\FOP\Database::rollbackTransaction();
				}
				// braucht keinen Lock, da single expression
				$crondurationSQL = json_encode(microtime(true) - $cronstart);
				\FOP\Database::query('UPDATE cron SET medianruntime = COALESCE(0.9 * medianruntime + 0.1 * '.$crondurationSQL.', '.$crondurationSQL.'), lastrun = '.time().' WHERE name=' . \FOP\Database::quote('Umsatzdownload'));
			}
		} else {
			if (\FOP\Database::getDriver() == 'mysql') {
				\FOP\Database::query('UNLOCK TABLES');
			} else {
				\FOP\Database::rollbackTransaction();
			}
		}
 // mailingcron
		if (\FOP\Database::getDriver() == 'mysql') {
			\FOP\Database::query('LOCK TABLES cron WRITE');
		} else {
			\FOP\Database::beginTransaction();
			\FOP\Database::query('LOCK TABLE cron');
		}
		$lastrun = \FOP\Database::query('SELECT lastrun FROM cron WHERE name = \'mailingcron\'')->fetchColumn();
		if ((self::willRunNow('5m', $lastrun))) {
			$cronstart = microtime(true);
			try {
				// Prüfzeitstempel auf +5min setzen, damit CronJob nicht mehrfach gestartet wird
				if ($lastrun === false) {
					\FOP\Database::query('INSERT '.(\FOP\Database::getDriver() == 'mysql' ? 'IGNORE ' : '') . 'INTO cron(name, lastrun) VALUES(' . \FOP\Database::quote('mailingcron') . ', '.time().'+5*60)'.(\FOP\Database::getDriver() != 'mysql' ? ' ON CONFLICT (name) DO NOTHING' : ''));
				} else {
					\FOP\Database::query('UPDATE cron SET lastrun='.time().'+5*60 WHERE name=' . \FOP\Database::quote('mailingcron'));
				}
				if (\FOP\Database::getDriver() == 'mysql') {
					\FOP\Database::query('UNLOCK TABLES');
				} else {
					\FOP\Database::commitTransaction();
				}
				\FOP\User::updateColumnsAdmin(); // crons are executed as admin by default (crons can change this by ::login(id) and ::updateColumns())
				\Tables\Campaign::mailingCron();
			} finally {
				if (\FOP\Database::getDriver() == 'mysql') {
					\FOP\Database::query('UNLOCK TABLES');
				} else if (\FOP\Database::inTransaction()) {
					// roll back unfinished tx
					\FOP\Database::rollbackTransaction();
				}
				// braucht keinen Lock, da single expression
				$crondurationSQL = json_encode(microtime(true) - $cronstart);
				\FOP\Database::query('UPDATE cron SET medianruntime = COALESCE(0.9 * medianruntime + 0.1 * '.$crondurationSQL.', '.$crondurationSQL.'), lastrun = '.time().' WHERE name=' . \FOP\Database::quote('mailingcron'));
			}
		} else {
			if (\FOP\Database::getDriver() == 'mysql') {
				\FOP\Database::query('UNLOCK TABLES');
			} else {
				\FOP\Database::rollbackTransaction();
			}
		}
 // filesgc
		if (\FOP\Database::getDriver() == 'mysql') {
			\FOP\Database::query('LOCK TABLES cron WRITE');
		} else {
			\FOP\Database::beginTransaction();
			\FOP\Database::query('LOCK TABLE cron');
		}
		$lastrun = \FOP\Database::query('SELECT lastrun FROM cron WHERE name = \'filesgc\'')->fetchColumn();
		if ((self::willRunNow('4h', $lastrun))) {
			$cronstart = microtime(true);
			try {
				// Prüfzeitstempel auf +5min setzen, damit CronJob nicht mehrfach gestartet wird
				if ($lastrun === false) {
					\FOP\Database::query('INSERT '.(\FOP\Database::getDriver() == 'mysql' ? 'IGNORE ' : '') . 'INTO cron(name, lastrun) VALUES(' . \FOP\Database::quote('filesgc') . ', '.time().'+5*60)'.(\FOP\Database::getDriver() != 'mysql' ? ' ON CONFLICT (name) DO NOTHING' : ''));
				} else {
					\FOP\Database::query('UPDATE cron SET lastrun='.time().'+5*60 WHERE name=' . \FOP\Database::quote('filesgc'));
				}
				if (\FOP\Database::getDriver() == 'mysql') {
					\FOP\Database::query('UNLOCK TABLES');
				} else {
					\FOP\Database::commitTransaction();
				}
				\FOP\User::updateColumnsAdmin(); // crons are executed as admin by default (crons can change this by ::login(id) and ::updateColumns())
				\Tables\CampaignTarget::cronTimeouts();
			} finally {
				if (\FOP\Database::getDriver() == 'mysql') {
					\FOP\Database::query('UNLOCK TABLES');
				} else if (\FOP\Database::inTransaction()) {
					// roll back unfinished tx
					\FOP\Database::rollbackTransaction();
				}
				// braucht keinen Lock, da single expression
				$crondurationSQL = json_encode(microtime(true) - $cronstart);
				\FOP\Database::query('UPDATE cron SET medianruntime = COALESCE(0.9 * medianruntime + 0.1 * '.$crondurationSQL.', '.$crondurationSQL.'), lastrun = '.time().' WHERE name=' . \FOP\Database::quote('filesgc'));
			}
		} else {
			if (\FOP\Database::getDriver() == 'mysql') {
				\FOP\Database::query('UNLOCK TABLES');
			} else {
				\FOP\Database::rollbackTransaction();
			}
		}
 // clearchatsubscriptionsgruppe
		if (\FOP\Database::getDriver() == 'mysql') {
			\FOP\Database::query('LOCK TABLES cron WRITE');
		} else {
			\FOP\Database::beginTransaction();
			\FOP\Database::query('LOCK TABLE cron');
		}
		$lastrun = \FOP\Database::query('SELECT lastrun FROM cron WHERE name = \'clearchatsubscriptionsgruppe\'')->fetchColumn();
		if ((self::willRunNow('1h', $lastrun))) {
			$cronstart = microtime(true);
			try {
				// Prüfzeitstempel auf +5min setzen, damit CronJob nicht mehrfach gestartet wird
				if ($lastrun === false) {
					\FOP\Database::query('INSERT '.(\FOP\Database::getDriver() == 'mysql' ? 'IGNORE ' : '') . 'INTO cron(name, lastrun) VALUES(' . \FOP\Database::quote('clearchatsubscriptionsgruppe') . ', '.time().'+5*60)'.(\FOP\Database::getDriver() != 'mysql' ? ' ON CONFLICT (name) DO NOTHING' : ''));
				} else {
					\FOP\Database::query('UPDATE cron SET lastrun='.time().'+5*60 WHERE name=' . \FOP\Database::quote('clearchatsubscriptionsgruppe'));
				}
				if (\FOP\Database::getDriver() == 'mysql') {
					\FOP\Database::query('UNLOCK TABLES');
				} else {
					\FOP\Database::commitTransaction();
				}
				\FOP\User::updateColumnsAdmin(); // crons are executed as admin by default (crons can change this by ::login(id) and ::updateColumns())
				\Chat\Gruppe::clearSubscriptions();
			} finally {
				if (\FOP\Database::getDriver() == 'mysql') {
					\FOP\Database::query('UNLOCK TABLES');
				} else if (\FOP\Database::inTransaction()) {
					// roll back unfinished tx
					\FOP\Database::rollbackTransaction();
				}
				// braucht keinen Lock, da single expression
				$crondurationSQL = json_encode(microtime(true) - $cronstart);
				\FOP\Database::query('UPDATE cron SET medianruntime = COALESCE(0.9 * medianruntime + 0.1 * '.$crondurationSQL.', '.$crondurationSQL.'), lastrun = '.time().' WHERE name=' . \FOP\Database::quote('clearchatsubscriptionsgruppe'));
			}
		} else {
			if (\FOP\Database::getDriver() == 'mysql') {
				\FOP\Database::query('UNLOCK TABLES');
			} else {
				\FOP\Database::rollbackTransaction();
			}
		}
 // filesgc
		if (\FOP\Database::getDriver() == 'mysql') {
			\FOP\Database::query('LOCK TABLES cron WRITE');
		} else {
			\FOP\Database::beginTransaction();
			\FOP\Database::query('LOCK TABLE cron');
		}
		$lastrun = \FOP\Database::query('SELECT lastrun FROM cron WHERE name = \'filesgc\'')->fetchColumn();
		if ((self::willRunNow('1d', $lastrun))) {
			$cronstart = microtime(true);
			try {
				// Prüfzeitstempel auf +5min setzen, damit CronJob nicht mehrfach gestartet wird
				if ($lastrun === false) {
					\FOP\Database::query('INSERT '.(\FOP\Database::getDriver() == 'mysql' ? 'IGNORE ' : '') . 'INTO cron(name, lastrun) VALUES(' . \FOP\Database::quote('filesgc') . ', '.time().'+5*60)'.(\FOP\Database::getDriver() != 'mysql' ? ' ON CONFLICT (name) DO NOTHING' : ''));
				} else {
					\FOP\Database::query('UPDATE cron SET lastrun='.time().'+5*60 WHERE name=' . \FOP\Database::quote('filesgc'));
				}
				if (\FOP\Database::getDriver() == 'mysql') {
					\FOP\Database::query('UNLOCK TABLES');
				} else {
					\FOP\Database::commitTransaction();
				}
				\FOP\User::updateColumnsAdmin(); // crons are executed as admin by default (crons can change this by ::login(id) and ::updateColumns())
				\FOP\Files::removeOld();
			} finally {
				if (\FOP\Database::getDriver() == 'mysql') {
					\FOP\Database::query('UNLOCK TABLES');
				} else if (\FOP\Database::inTransaction()) {
					// roll back unfinished tx
					\FOP\Database::rollbackTransaction();
				}
				// braucht keinen Lock, da single expression
				$crondurationSQL = json_encode(microtime(true) - $cronstart);
				\FOP\Database::query('UPDATE cron SET medianruntime = COALESCE(0.9 * medianruntime + 0.1 * '.$crondurationSQL.', '.$crondurationSQL.'), lastrun = '.time().' WHERE name=' . \FOP\Database::quote('filesgc'));
			}
		} else {
			if (\FOP\Database::getDriver() == 'mysql') {
				\FOP\Database::query('UNLOCK TABLES');
			} else {
				\FOP\Database::rollbackTransaction();
			}
		}
 // fail2ban
		if (\FOP\Database::getDriver() == 'mysql') {
			\FOP\Database::query('LOCK TABLES cron WRITE');
		} else {
			\FOP\Database::beginTransaction();
			\FOP\Database::query('LOCK TABLE cron');
		}
		$lastrun = \FOP\Database::query('SELECT lastrun FROM cron WHERE name = \'fail2ban\'')->fetchColumn();
		if ((self::willRunNow('24h', $lastrun))) {
			$cronstart = microtime(true);
			try {
				// Prüfzeitstempel auf +5min setzen, damit CronJob nicht mehrfach gestartet wird
				if ($lastrun === false) {
					\FOP\Database::query('INSERT '.(\FOP\Database::getDriver() == 'mysql' ? 'IGNORE ' : '') . 'INTO cron(name, lastrun) VALUES(' . \FOP\Database::quote('fail2ban') . ', '.time().'+5*60)'.(\FOP\Database::getDriver() != 'mysql' ? ' ON CONFLICT (name) DO NOTHING' : ''));
				} else {
					\FOP\Database::query('UPDATE cron SET lastrun='.time().'+5*60 WHERE name=' . \FOP\Database::quote('fail2ban'));
				}
				if (\FOP\Database::getDriver() == 'mysql') {
					\FOP\Database::query('UNLOCK TABLES');
				} else {
					\FOP\Database::commitTransaction();
				}
				\FOP\User::updateColumnsAdmin(); // crons are executed as admin by default (crons can change this by ::login(id) and ::updateColumns())
				\FOP\Login::fail2banCron();
			} finally {
				if (\FOP\Database::getDriver() == 'mysql') {
					\FOP\Database::query('UNLOCK TABLES');
				} else if (\FOP\Database::inTransaction()) {
					// roll back unfinished tx
					\FOP\Database::rollbackTransaction();
				}
				// braucht keinen Lock, da single expression
				$crondurationSQL = json_encode(microtime(true) - $cronstart);
				\FOP\Database::query('UPDATE cron SET medianruntime = COALESCE(0.9 * medianruntime + 0.1 * '.$crondurationSQL.', '.$crondurationSQL.'), lastrun = '.time().' WHERE name=' . \FOP\Database::quote('fail2ban'));
			}
		} else {
			if (\FOP\Database::getDriver() == 'mysql') {
				\FOP\Database::query('UNLOCK TABLES');
			} else {
				\FOP\Database::rollbackTransaction();
			}
		}

    
    // --- BLOCK: ~-logout ---
    
		} catch(\Throwable $error) {
			error_log($error->getMessage());
		} finally {
			\FOP\User::login($userid, false);
			\FOP\User::updateColumns();
		}
	
    
  }

  public static function canShellExec() { 
    
    
    // --- BLOCK: main ---
    
        return is_callable('shell_exec') && false === stripos(ini_get('disable_functions'), 'shell_exec');
    
    
  }

  public static function ensureIsLoaded() { 
    
    
    // --- BLOCK: main ---
    
  // Can be called to ensure class is loaded and initialized

    
  }

  public static function getCronmode() { 
    
    
    // --- BLOCK: main ---
    return \FOP\Configuration::getFOPCronCronmode();
    
  }

  public static function willRunNow($i, $lastrun) { 
    
    
    // --- BLOCK: main ---
    
		if ($i == '@month') {
			return !$lastrun || date('m', $lastrun) <> date('m', time());
		} else if ($i[0] == '@') {
			// Uhrzeit-Cron
			if (!$lastrun) $lastrun = time() - 2*24*60*60; // not run yet: run now
			$nextrun = strtotime(substr($i, 1), $lastrun);
			if ($nextrun < $lastrun) {
				$nextrun = strtotime('+1 day', $nextrun); // move to next day if lastrun time > time
			}
			return time() >= $nextrun;
		} else {
			// Interval-Cron
			if(substr($i, -1) == 'd') $i = intval($i) * 60 * 60 * 24;
			else if(substr($i, -1) == 'h') $i = intval($i) * 60 * 60;
			else if(substr($i, -1) == 'm') $i = intval($i) * 60;
			else $i = intval($i);
			return !$lastrun || $lastrun < time() - $i;
		}
	
    
  }

  public static function initialize() { 
    
    
    // --- BLOCK: process ---
    
	if (\FOP\Cron::getCronmode() == 'http' && !\FOP\Configuration::isActive() && basename($_SERVER['SCRIPT_NAME']) != "dbcheck.php") {
		self::indexAction();
	}
    
  }


  
  

  
  
}

\FOP\Cron::initialize(); 

