<?php
//error_reporting(E_ALL | E_NOTICE);
//ini_set("display_errors","On");
include "../config.php";
require_once "../inc/functions.php";
//echo memory_get_usage(); 

####################################################################################
#  UPDATE/MAINTENANCE PERFORMED
####################################################################################
//function update_cron () {
//global $domain, $adminemail, $adminname, $sitename;
//$today = date('Y-m-d',strtotime("now"));
//remove_expiredads ();
//monthy_cron ();


//}

//optimize_database();

//$to = $adminemail;
//$subject = "".$sitename." update cron performed";
//$body = "update successful";
//sendoutmail ($to, $adminname, $subject, $body);

//echo "update performed";


//echo "test0<br>";
####################################################################################
#  SEND MEMBER CREDIT MAIL
####################################################################################
//function send_creditmail () {
global $domain, $sitename;
$querylist = mysql_query("SELECT id, userid, subject, message, url, sent, sendto FROM membermail WHERE sent = 0 ORDER BY id ASC LIMIT 1");
$adid = @mysql_result($querylist, 0, "id");
$mid = @mysql_result($querylist, 0, "userid");
$sendsubject = @mysql_result($querylist, 0, "subject");
$sendmessage = @mysql_result($querylist, 0, "message");
$sendurl = @mysql_result($querylist, 0, "url");
$sendto = @mysql_result($querylist, 0, "sendto");
$tid=fetchuid($mid);
if ($tid) {
$string = random_string(10);
mysql_query("UPDATE membermail SET sent=1, num='".$string."' WHERE id='".$adid."'");
//echo "test1<br>";
$sql3 = mysql_query("SELECT * FROM members WHERE status=1 and verified=1 and vacation=0 ORDER BY RAND() LIMIT $sendto");
//original code below. I updated members/mailer.php with new code 11/29/24
//$sql3 = mysql_query("SELECT * FROM members WHERE verified=1 ORDER BY RAND() LIMIT $sendto");
while($each = mysql_fetch_array($sql3)) {
$users[$each['userid']]= $each;
}
//echo "test2<br>";
foreach($users as $each) {
$each['name'] = trim($each['name']);
if(strpos($each['name'], " ")) $firstname = substr($each['name'] , 0, strpos($each['name'], " "));
else $firstname = $each['name'];
$earn=adearn(22,$each['userid'],$each['memtype'],"");
$message = "=============================================================<br>This mailing is from a $sitename member. Do not reply as the sender will not<br>receive your reply and it will cause your email to bounce and your account will be<br>at risk. If you no longer want our messages, follow the instructions at the bottom.<br>=============================================================<br><br>";
$subject = str_replace("~userid~", $each['userid'], str_replace("~fname~", $firstname, stripslashes($sendsubject)));
$message .= "<b>";  
$message .= str_replace("~userid~", $each['userid'], str_replace("~fname~", $firstname, stripslashes($sendsubject)));
$message .= "</b><br><br>";
$message .= str_replace("~userid~", $each['userid'], str_replace("~fname~", $firstname, stripslashes($sendmessage)));
$message .= "<br><br>";
$message .= "<a href=\"".$domain."/earn.php?t=22&u=".$each['id']."&adid=".$adid."&num=".$string."\"><b><font color=RED>Click Here To View The Link And Earn ".$earn." Credits</font></b></a><br><br>";
$message .= "=============================================================<br><br>";
$message .= mailfooter ($each['userid']);
//echo "test3<br>";
sendoutmail ($each['contact_email'], $each['name'], $subject, $message);
$counter=$counter+1;
mysql_query("INSERT INTO membermail_process VALUES ('".$adid."','".$each['userid']."','".time()."')");
//echo "test4<br>";
}
mysql_query("UPDATE membermail set sentto='".$counter."' WHERE id = '".$adid."' ");
creditmail_sent ($counter);
}  
//echo "test5<br>";
//}
####################################################################################

function is_time_cron($time , $cron)
{
$cron_parts = explode(' ' , $cron);
if(count($cron_parts) != 5)
{
return false;
}

list($min , $hour , $day , $mon , $week) = explode(' ' , $cron);

$to_check = array('min' => 'i' , 'hour' => 'G' , 'day' => 'j' , 'mon' => 'n' , 'week' => 'w');

$ranges = array(
'min' => '0-59' ,
'hour' => '0-23' ,
'day' => '1-31' ,
'mon' => '1-12' ,
'week' => '0-6' ,
);

foreach($to_check as $part => $c)
{
$val = $$part;
$values = array();

/*
For patters like 0-23/2
*/
if(strpos($val , '/') !== false)
{
//Get the range and step
list($range , $steps) = explode('/' , $val);

//Now get the start and stop
if($range == '*')
{
$range = $ranges[$part];
}
list($start , $stop) = explode('-' , $range);

for($i = $start ; $i <= $stop ; $i = $i + $steps)
{
$values[] = $i;
}
}
/*
For patters like :
2
2,5,8
2-23
*/
else
{
$k = explode(',' , $val);

foreach($k as $v)
{
if(strpos($v , '-') !== false)
{
list($start , $stop) = explode('-' , $v);

for($i = $start ; $i <= $stop ; $i++)
{
$values[] = $i;
}
}
else
{
$values[] = $v;
}
}
}

if ( !in_array( date($c , $time) , $values ) and (strval($val) != '*') )
{
return false;
}
}

return true;
}

$cronjobs = array();

$query = "select * from cron_jobs";
$result = mysql_query($query);
$my_array = array();
$line=0;      				

while ($row = mysql_fetch_assoc($result))
{
$line++;
$my_title = $row['title'];
$my_command = $row['command']; 
$cronjobs[$my_title] = $my_command;		
}

foreach($cronjobs as $method => $cron)
{
$time = time();
if( is_time_cron($time , $cron) )
{
$result = $method();
echo $result;
//echo "test22";
}
}
?>