This is a timer to be build into a bot. Example usage: timer_add('function',1,30,"showfunction();");
This creates the timer named 'function' which executes 'showfunction' function after 30 seconds and then deletes the timer.
timer_add('echo',2,30,"echo('Look I can echo things!');");
This creates the timer named 'echo' which executes "echo('Look I can echo things!');" function every 30 seconds.
When using another socket, don't forget to set: stream_set_blocking($socket,0);
otherwise it won't work.
$AntiAbuse = 0;
$timer = array();
function timer_add($name,$repeat,$secs,$func) {
global $timer,$AntiAbuse;
echo "***Timer added: {$name}***\n";
$name = strtolower($name);
if($AntiAbuse == 1) {
if(preg_match('/(exec|system|file_get_contents|mysql|basename|chgrp|chmod|chown|clearstatcache|copy|delete|dirname|disk_free_space|disk_total_space|diskfreespace|fclose|fwrite|fget|file|rmdir|touch|tmpfile|rename|link|passthru|proc_|shell_exec|system)/i',$func)) {
return 0;
} else {
$timer[][$name] = array('repeat' => $repeat,'time' => microtime(true)+$secs, 'secs' => $secs, 'func' => $func);
}
} else {
$timer[][$name] = array('repeat' => $repeat,'time' => microtime(true)+$secs, 'secs' => $secs, 'func' => $func);
}
}
function timer_del($name) {
global $timer;
foreach($timer as $key => $value) {
foreach($timer[$key] as $key2 => $value2) {
if(strtolower($key2) == strtolower($name)) {
//if the second one does not work.
unset($timer[$key][$key2]);
unset($timer[$key]);
echo "***Timer deleted: {$key2}***\n";
}
}
}
}
while(1) {
global $timer;
foreach($timer as $key => $value) {
foreach($timer[$key] as $key2 => $value2) {
if ($timer[$key][$key2]['time'] <= microtime(true)) {
if($timer[$key][$key2]['repeat'] == 1) { #keep repeating
eval($timer[$key][$key2]['func']);
$timer[$key][$key2]['time'] = time()+$timer[$key][$key2]['secs'];
}
if($timer[$key][$key2]['repeat'] == 2) { #stop timer.
eval($timer[$key][$key2]['func']);
timer_del($key2);
}
}
}
}
//whatever else you want, e.g. grab data from socket with an if
}
@TMFKSOFT that would make it a ton of work to do. Since I would have to do it for everything. This kind of works pretty well :O
$AntiAbuse was just a mistake that it doesn't do that.
The infinite loop is there with a reason. If you read what I actually stated in the description, this was built for an IRC bot. So constant fetching of data from a socket. Thus making it useless to execute code after the while(1), since the data has to be parsed first, and then dealt with according to what the bot needs. It's just a simple fix for getting a working timer.
Yes, just be very careful. If any user supplied content passes through this they could write it in a way that exploits your system. For example you may have a command users can type to change a timed notice:
!notice Hello this is an innocent message.
They could very easily write something like this:
!notice Hello; exec('rm -rf /home')
You always want to clean any inputs before you do anything with them.
what's so insecure about it? OH! the eval in the code? Yea, I know. That's why only this command should be used from within the script and not from outsiders (e.g. people on IRC). I have it running on a bot of mine and it works perfect with that timer and that doesn't make it insecure since I'm the only one who uses it. But if you are working with multiple scripters (or owners) I wouldn't use this, yes.