class.cron.php
<?php
/**
*This class is here to help you create cron jobs with ease.
*
*/
class crontab{
var $minute=NULL;
var $hour=NULL;
var $day=NULL;
var $month=NULL;
var $dayofweek=NULL;
var $command=NULL;
var $directory=NULL;
var $filename="crons";
var $crontabPath=NULL;
var $handle=NULL;
var $errors=array();
var $prefix;
public $title;
public $server = "";
public $user = "";
public $pass = "";
/**
*Constructor. Attempts to create directory for
*holding cron jobs
*
*@authorRichard Sumilang < richard@richard-sumilang.com>
*@paramstring$dir Directory to hold cron job files
*@paramstring$filename Filename to write to
*@paramstring$crontabPath Path to cron program
*@accesspublic
*/
function crontab($dir=NULL, $filename=NULL, $modewrite='a', $crontabPath=NULL ){
$result=(!$dir) ? $this->setDirectory("~/my_crontabs") : $this->setDirectory($dir);
if(!$result)
exit('Directory error');
$result=(!$filename) ? $this->createCronFile("crons",$modewrite) : $this->createCronFile($filename,$modewrite);
if(!$result)
exit('File error');
$this->pathToCrontab=($crontabPath) ? NULL : $crontabPath;
}
/**
*Set date parameters
*
*If any parameters are left NULL then they default to *
*
*A hyphen (-) between integers specifies a range of integers. For
*example, 1-4 means the integers 1, 2, 3, and 4.
*
*A list of values separated by commas (,) specifies a list. For
*example, 3, 4, 6, 8 indicates those four specific integers.
*
*The forward slash (/) can be used to specify step values. The value
*of an integer can be skipped within a range by following the range
*with /<integer>. For example, 0-59/2 can be used to define every other
*minute in the minute field. Step values can also be used with an asterisk.
*For instance, the value * /3 (no space) can be used in the month field to run the
*task every third month...
*
*@authorRichard Sumilang< richard@richard-sumilang.com>
*@parammixed$minMinute(s)... 0 to 59
*@parammixed$hourHour(s)... 0 to 23
*@parammixed$dayDay(s)... 1 to 31
*@parammixed$monthMonth(s)... 1 to 12 or short name
*@parammixed$dayofweekDay(s) of week... 0 to 7 or short name. 0 and 7 = sunday
*$accesspublic
*/
function setDateParams($min=NULL, $hour=NULL, $day=NULL, $month=NULL, $dayofweek=NULL){
if($min=="0")
$this->minute=0;
elseif($min)
$this->minute=$min;
else
$this->minute="*";
if($hour=="0")
$this->hour=0;
elseif($hour)
$this->hour=$hour;
else
$this->hour="*";
$this->month=($month) ? $month : "*";
$this->day=($day) ? $day : "*";
$this->dayofweek=($dayofweek) ? $dayofweek : "*";
}
/**
*Set the directory path. Will check it if it exists then
*try to open it. Also if it doesn't exist then it will try to
*create it, makes it with mode 0700
*
*@authorRichard Sumilang< richard@richard-sumilang.com>
*@paramstring$directoryDirectory, relative or full path
*@accesspublic
*@returnboolean
*/
function setDirectory($directory){
if(!$directory) return false;
if(is_dir($directory)){
if($dh=opendir($directory)){
$this->directory=$directory;
return true;
}else
return false;
}else{
if(mkdir($directory, 0700)){
$this->directory=$directory;
return true;
}
}
return false;
}
/**
*Create cron file
*
*This will create a cron job file for you and set the filename
*of this class to use it. Make sure you have already set the directory
*path variable with the consructor. If the file exists and we can write
*it then return true esle false. Also sets $handle with the resource handle
*to the file
*
*@authorRichard Sumilang< richard@richard-sumilang.com>
*@paramstring$filenameName of file you want to create
*@accesspublic
*@returnboolean
*/
function createCronFile($filename=NULL,$modewrite){
if(!$filename)
return false;
if(file_exists($this->directory.$filename)){
if($handle=fopen($this->directory.$filename, $modewrite)){
$this->handle=&$handle;
$this->filename=$filename;
return true;
}else
return false;
}
if(!$handle=fopen($this->directory.$filename, $modewrite))
return false;
else{
$this->handle=&$handle;
$this->filename=$filename;
return true;
}
}
/**
*Set command to execute
*
*@authorRichard Sumilang< richard@richard-sumilang.com>
*@paramstring$commandComand to set
*@accesspublic
*@returnstring$command
*/
function setCommand($command){
if($command){
$this->command=$command;
return false;
}else
return false;
}
/**
*Write cron command to file. Make sure you used createCronFile
*before using this function of it will return false
*
*@authorRichard Sumilang< richard@richard-sumilang.com>
*@accesspublic
*@returnvoid
*/
function saveCronFile(){
$command=$this->minute." ".$this->hour." ".$this->day." ".$this->month." ".$this->dayofweek." ".$this->command."n";
if(!fwrite($this->handle, $command))
return true;
else
return false;
}
/**
*Save cron in system
*
*@authorRichard Sumilang< richard@richard-sumilang.com>
*@accesspublic
*@return booleantrue if successful else false
*/
function addToCrontab(){
if(!$this->filename)
exit('No name specified for cron file');
if(exec($this->pathToCrontab."crontab ".$this->directory.$this->filename)){
return true;
}else{
$this->errors[] = mysql_error();
return false;}
}
/**
* Show error from the addtocrontab function.
*
*/
public function showerror(){
print_r($this->errors) or var_dump($this->errors);
}
/**
*Destroy file pointer
*
*@authorRichard Sumilang< richard@richard-sumilang.com>
*@accesspublic
*@return void
*/
function destroyFilePoint(){
fclose($this->handle);
return true;
}
/**
* Clear all variables...
*
*
*/
function clearParameters(){
$this->command = "";
$this->minute=0;
$this->hour=0;
$this->month="*";
$this->day="*";
$this->dayofweek="*";
return true;
}
/**
* save cron instruction to db
*
*/
public function savetodb(){
$result = mysql_query("INSERT INTO {$this->prefix}_crontab (command,title,minute,hour,day,month,dayofweek) VALUES ('{$this->command}','{$this->title}','{$this->minute}','{$this->hour}','{$this->day}','{$this->month}','{$this->dayofweek}')") or die("error ".mysql_error());
if($result){
return true;
}else{
return false;
}
}
/**
* get cron instruction from db
*
*/
public function retrievedb(){
$result = mysql_query('select * from '.$this->prefix.'_crontab') or die("error ".mysql_error());
while($row = mysql_fetch_array($result)){
$a++;
$daterd[$a]["minute"]=$row[minute];
$daterd[$a]["hour"]=$row[hour];
$daterd[$a]["day"]=$row[day];
$daterd[$a]["month"]=$row[month];
$daterd[$a]["dayofweek"]=$row[dayofweek];
$daterd[$a]["command"]=$row[command];
$daterd[$a]["title"]=$row[title];
}
return $daterd;
}
/**
* Conect to db
*
* @param mixed $server
* @param mixed $user
* @param mixed $password
*/
public function connectDatabase( $server ='localhost' , $user, $password, $dbName ){
mysql_connect( $server ,$user,$password) or $this->errors[] = 'Couldnot connect mysql '.mysql_error();
if ( mysql_select_db( $dbName ) ) {
return true;
} else {
return false;
}
}
/**
* create file to proces from db.
*
*
*/
function saveCronFilebydb(){
$commandw = "";
$result = mysql_query('select * from '.$this->prefix.'_crontab') or die("error ".mysql_error());
while($row = mysql_fetch_array($result)){
$commandw.=$row["minute"]." ".$row["hour"]." ".$row["day"]." ".$row["month"]." ".$row["dayofweek"]." ".$row["command"]."n";
}
if(!fwrite($this->handle, $commandw))
return true;
else
return false;
}
/**
* To close the connection to db.
*
*/
public function closeDB(){
return mysql_close() or mysqli_close();
}
}
?>
db.sql
--
-- Estructura de tabla para la tabla `prefix_crontab`
--
CREATE TABLE IF NOT EXISTS `prefix_crontab` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL DEFAULT '',
`command` longtext NOT NULL,
`minute` varchar(250) NOT NULL DEFAULT '0',
`hour` varchar(250) NOT NULL DEFAULT '0',
`day` varchar(250) NOT NULL DEFAULT '0',
`month` varchar(250) NOT NULL DEFAULT '0',
`dayofweek` varchar(250) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
examples.php
<?php
//EXAMPLE without data base.
require_once('class.cron.php');
$crontab=new crontab("/cron/", "filename", 'a');//if u want to delete the old pass the 'w' parameter instead of 'a'
$crontab->setDateParams($min, $hour, $day, $month, "*"); //min , hour ,day , month , dayofweek
$crontab->setCommand("curl http://somedomain.com/index.php?mod=sendcron");
$crontab->savetodb();
$crontab->saveCronFile();
$crontab->addToCrontab();
$crontab->destroyFilePoint(); // OPTIONAL
/********************************************************************************************/
//ANOTHER EXAMPLE using connection to db
require_once('class.cron.php');
$crontab=new crontab("/cron/", "filename", 'w');
$crontab->prefix = $DBprefix;
$crontab->connectDatabase('localhost','user','password','database');
$crontab->title = $_POST["title"];
$crontab->setDateParams($min, $hour, $day, $month, "*");
$crontab->setCommand("wget http://somedomain.com/index.php?mod=sendcron");
$crontab->savetodb();
/*
If you want to add more croncommand use:
$crontab->clearParameters();
$crontab->setDateParams(5, 10, 5, 5, "*");
$crontab->setCommand("curl http://somedomain.com/index.php?mod=sendcron");
$crontab->savetodb();
*/
$crontab->saveCronFilebydb();
$crontab->addToCrontab();
//$crontab->showerror();
$crontab->destroyFilePoint();
$crontab->closeDB();
/**********************************************************************************************/
//ANOTHER EXAMPLE TO RETRIEVE THE DATA FROM THE DB
require_once('class.cron.php');
$crontab=new crontab("/cron/", "filename", 'w');
$crontab->prefix = $DBprefix;
$crontab->connectDatabase('localhost','user','password','database');
$result = $crontab->retrievedb();
foreach($result as $key => $item) {
echo $item["title"]."n";
}
$crontab->closeDB();
?>
說明
主要在examples.php這支程式上設定測試即可.
The documentation and description of the functions and of the class is in the class file.
any function have their explication of use.
for examples use the examples.php file.
|