<?
########## Confixx(R) 3.0 Professional ############
####### Copyright SWsoft, Inc. 2004-2005 ##########
#### http://www.sw-soft.com - info@sw-soft.com ####

include_once("class.Crontab.php");

function lmsg($v, $a = false)
{
	return ltext($v, $a);
}

function read_cron($user)
{
	global $PHP_AUTH_USER;
	$id = db_query("select count(*) as c from cronjobs where kunde='$PHP_AUTH_USER'");
	$werte = db_fetch_array($id);
	if($werte["c"]) {
		db_query("update cronjobs set neu = 2 where kunde='$PHP_AUTH_USER'");
	} else 	{
		db_query("insert into cronjobs (kunde, neu) values ('$PHP_AUTH_USER', 2)");
	}
	db_query("update allgemein set cronupdate=1");
}

function load_cron( $user = false )
{
	global $PHP_AUTH_USER, $bindir;
	if(!$user)
	{
		$user = $PHP_AUTH_USER;
	}
	$ret = "";
	$id = db_query("select cronfile, neu, lastchange from cronjobs where kunde='$user'");
	if (db_num_rows($id)) {
		$ret = $werte = db_fetch_array($id);
//		$ret = explode("\n", $werte["cronfile"]);
	} else {
		$ret = false;	// no crontab detected
	}
	//echo $comm."<br>";
	//print_r($ret);
	//echo "<br>";
	//print_r($err);
	return $ret;
}

function save_cron($f)
{
	global $PHP_AUTH_USER;
	$f = str_replace("'", "\\'", $f);
	$id = db_query("select count(*) as c from cronjobs where kunde='$PHP_AUTH_USER'");
	$werte = db_fetch_array($id);
	if($werte["c"])
	{
		db_query("update cronjobs set cronfile='$f', neu = 1 where kunde='$PHP_AUTH_USER'");
	}
	else
	{
		db_query("insert into cronjobs (kunde, cronfile, neu) values ('$PHP_AUTH_USER', '$f', 1)");
	}
	db_query("update allgemein set cronupdate=1");

}

function count_cron_jobs_user($user)
{

	$cron_rec = load_cron($user);
	$cron_content = ($cron_rec == false) ? array('') : explode("\n", $cron_rec['cronfile']);
	$crn = new Crontab( $cron_content );
	return $crn->count_jobs();
}

function count_cron_jobs_reseller( $res )
{
	$c = 0;
	$id = db_query("select kunde from kunden where anbieter='$res'");
	if($id)
	{
		while($w = db_fetch_array($id))
		{
			$c += count_cron_jobs_user($w["kunde"]);
		}
	}
	return $c;
}


?>
