Merge branch 'devel' of dev.sourcefabric.org:airtime into devel
This commit is contained in:
commit
3485b5a39f
17 changed files with 220 additions and 553 deletions
|
@ -1,214 +0,0 @@
|
|||
<?php
|
||||
require_once (dirname(__FILE__).'/Crontab.php');
|
||||
require_once (dirname(__FILE__).'/../../conf.php');
|
||||
/**
|
||||
* This class can call a PHP function from crontab.
|
||||
* Example:
|
||||
* <pre>
|
||||
* $cron = new Cron();
|
||||
* $access = $cron->openCrontab('write');
|
||||
* if ($access != 'write') {
|
||||
* do {
|
||||
* $access = $cron->forceWriteable();
|
||||
* } while ($access != 'write');
|
||||
* }
|
||||
* $cron->addCronJob('*','*','*','*','*',
|
||||
* 'ClassName',
|
||||
* array('first','secound','third')
|
||||
* );
|
||||
* $cron->closeCrontab();
|
||||
* </pre>
|
||||
* @package Airtime
|
||||
* @subpackage StorageServer.Cron
|
||||
*/
|
||||
class Cron {
|
||||
/**
|
||||
* @var Crontab
|
||||
*/
|
||||
public $ct;
|
||||
|
||||
/**
|
||||
* @var array This array created with getCommand() function
|
||||
*/
|
||||
private $params;
|
||||
|
||||
/**
|
||||
* @var string available values: read | write
|
||||
*/
|
||||
private $ctAccess = 'read';
|
||||
|
||||
private $lockfile;
|
||||
private $cronfile;
|
||||
private $paramdir;
|
||||
private $cronUserName;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
function Cron() {
|
||||
global $CC_CONFIG;
|
||||
$this->lockfile = $CC_CONFIG['lockfile'];
|
||||
$this->cronfile = $CC_CONFIG['cronfile'];
|
||||
$this->paramdir = $CC_CONFIG['paramdir'];
|
||||
$this->cronUserName = $CC_CONFIG['cronUserName'];
|
||||
}
|
||||
|
||||
|
||||
/* ==================================================== Cronjob functions */
|
||||
/**
|
||||
* Add a cronjob to the crontab
|
||||
*
|
||||
* @access public
|
||||
* @param string $m minute
|
||||
* @param string $h hour
|
||||
* @param string $dom day of month
|
||||
* @param string $mo month
|
||||
* @param string $dow day of week
|
||||
* @param string $className name of class, which's execute() is called by croncall.php
|
||||
* @param string $params the parameter(s)
|
||||
* @return bool true if success else PEAR error.
|
||||
*/
|
||||
function addCronJob($m, $h, $dom, $mo, $dow, $className, $params)
|
||||
{
|
||||
if ($this->ctAccess == 'write') {
|
||||
$this->ct->addCron($m, $h, $dom, $mo, $dow,
|
||||
$this->getCommand($className, $params));
|
||||
return true;
|
||||
} else {
|
||||
return PEAR::raiseError('CronJob::addCronJob : '.
|
||||
'The crontab is not writable');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This function return with the active cronjobs
|
||||
*
|
||||
* @access public
|
||||
* @return array array of cronjob struct
|
||||
*/
|
||||
function listCronJob()
|
||||
{
|
||||
return $this->ct->getByType(CRON_CMD);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a cronjob.
|
||||
*
|
||||
* @access public
|
||||
* @param int $index index of the cronjobs' array.
|
||||
* @return bool true if success else PEAR error.
|
||||
*/
|
||||
function removeCronJob($index)
|
||||
{
|
||||
if ($this->ctAccess == 'write') {
|
||||
$this->crontab->delEntry($index);
|
||||
return true;
|
||||
} else {
|
||||
return PEAR::raiseError('CronJob::removeCronJob : '.
|
||||
'The crontab is not writable');
|
||||
}
|
||||
}
|
||||
|
||||
/* ==================================================== Crontab functions */
|
||||
/**
|
||||
* Open the crontab
|
||||
*
|
||||
* @access public
|
||||
* @param string $access only for listing 'read', for add and delete 'write'
|
||||
* @return string sucessed access - available values read | write
|
||||
*/
|
||||
function openCrontab($access = 'read')
|
||||
{
|
||||
$access = strtolower($access);
|
||||
$this->ct = new Crontab($this->cronUserName);
|
||||
if ($access == 'write' &&
|
||||
$this->isCrontabWritable() &&
|
||||
$this->lockCrontab()) {
|
||||
$this->ctAccess = $access;
|
||||
} else {
|
||||
$this->ctAccess = 'read';
|
||||
}
|
||||
return $this->ctAccess;
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the crontab
|
||||
*
|
||||
* @access public
|
||||
* @return bool true if everything is ok, false is the lock file can't delete
|
||||
*/
|
||||
function closeCrontab()
|
||||
{
|
||||
if ($this->ctAccess == 'write') {
|
||||
$this->ct->writeCrontab();
|
||||
}
|
||||
return $this->ctAccess == 'write' ? $this->unlockCrontab() : true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check the crontab is writable
|
||||
*
|
||||
* @access private
|
||||
* @return bool
|
||||
*/
|
||||
function isCrontabWritable()
|
||||
{
|
||||
return !is_file($this->lockfile);
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to lock the crontab
|
||||
*
|
||||
* @access private
|
||||
* @return bool true if the locking is success
|
||||
*/
|
||||
function lockCrontab()
|
||||
{
|
||||
return @touch($this->lockfile);
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to unlock the crontab
|
||||
*
|
||||
* @access private
|
||||
* @return bool true if the unlocking is success
|
||||
*/
|
||||
function unlockCrontab()
|
||||
{
|
||||
return unlink($this->lockfile);
|
||||
}
|
||||
|
||||
/**
|
||||
* If the crontab opened with read access. This function force set
|
||||
* the access to write.
|
||||
*
|
||||
* @access public
|
||||
* @return bool true if the setting is success
|
||||
*/
|
||||
function forceWriteable()
|
||||
{
|
||||
if ($this->isCrontabWritable() && $this->lockCrontab()) {
|
||||
$this->ctAccess = 'write';
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/* ======================================================= Misc functions */
|
||||
/**
|
||||
* Get the shell command for the cronjob
|
||||
*
|
||||
* @param string $className name of the class what is called by croncall.php
|
||||
* @param mixed $params with this parameter could be called the execute() of class
|
||||
* @return string shell command
|
||||
*/
|
||||
function getCommand($className, $params)
|
||||
{
|
||||
$this->params = array (
|
||||
'class' => $className,
|
||||
'params' => $params
|
||||
);
|
||||
return $this->cronfile.' "'.str_replace('"','\"',serialize($this->params)).'"';
|
||||
}
|
||||
}
|
||||
|
|
@ -1,17 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* Cron jobs handling abstract class
|
||||
*
|
||||
* @package Airtime
|
||||
* @subpackage StorageServer.Cron
|
||||
*/
|
||||
class CronJob
|
||||
{
|
||||
/**
|
||||
* The croncall.php call this function after the objectcreation.
|
||||
*/
|
||||
function execute()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
@ -1,284 +0,0 @@
|
|||
<?php
|
||||
define('CRON_COMMENT', 0);
|
||||
define('CRON_ASSIGN', 1);
|
||||
define('CRON_CMD', 2);
|
||||
define('CRON_SPECIAL', 3);
|
||||
define('CRON_EMPTY', 4);
|
||||
|
||||
/**
|
||||
* A class that interfaces with the crontab. (cjpa@audiophile.com)
|
||||
*
|
||||
* This class lets you manipulate a user's crontab.
|
||||
* It lets you add delete update entries easily.
|
||||
*
|
||||
* @package Airtime
|
||||
* @subpackage StorageServer.Cron
|
||||
*/
|
||||
class Crontab
|
||||
{
|
||||
// {{{ properties
|
||||
/**
|
||||
* Holds all the different lines.
|
||||
* Lines are associative arrays with the following fields:
|
||||
* "minute" : holds the minutes (0-59)
|
||||
* "hour" : holds the hour (0-23)
|
||||
* "dayofmonth" : holds the day of the month (1-31)
|
||||
* "month" : the month (1-12 or the names)
|
||||
* "dayofweek" : 0-7 (or the names)
|
||||
*
|
||||
* or a line can be a 2-value array that represents an assignment:
|
||||
* "name" => "value"
|
||||
* or a line can be a comment (string beginning with #)
|
||||
* or it can be a special command (beginning with an @)
|
||||
* @var array
|
||||
*/
|
||||
private $crontabs;
|
||||
|
||||
/**
|
||||
* The user for whom the crontab will be manipulated
|
||||
* @var string
|
||||
*/
|
||||
private $user;
|
||||
|
||||
/**
|
||||
* Lists the type of line of each line in $crontabs.
|
||||
* can be: any of the CRON_* constants.
|
||||
* so $linetype[5] is the type of $crontabs[5].
|
||||
* @var string
|
||||
*/
|
||||
private $linetypes;
|
||||
|
||||
// }}}
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* Initialises $this->crontabs
|
||||
*
|
||||
* @param string $user the user for whom the crontab will be manipulated
|
||||
*/
|
||||
function Crontab($user)
|
||||
{
|
||||
$this->user = $user;
|
||||
$this->readCrontab();
|
||||
}
|
||||
|
||||
/**
|
||||
* This reads the crontab of $this->user and parses it in $this->crontabs
|
||||
*
|
||||
*/
|
||||
function readCrontab()
|
||||
{
|
||||
// return code is 0 or 1 if crontab was empty, elsewhere stop here
|
||||
$cmd = "crontab -u {$this->user} -l";
|
||||
@exec("crontab -u {$this->user} -l", $crons, $return);
|
||||
if ($return > 1) {
|
||||
return PEAR::raiseError("*** Can't read crontab ***\n".
|
||||
" Set crontab manually!\n");
|
||||
}
|
||||
|
||||
foreach ($crons as $line)
|
||||
{
|
||||
$line = trim($line); // discarding all prepending spaces and tabs
|
||||
|
||||
// empty lines..
|
||||
if (!$line) {
|
||||
$this->crontabs[] = "empty line";
|
||||
$this->linetypes[] = CRON_EMPTY;
|
||||
continue;
|
||||
}
|
||||
|
||||
// checking if this is a comment
|
||||
if ($line[0] == "#") {
|
||||
$this->crontabs[] = trim($line);
|
||||
$this->linetypes[] = CRON_COMMENT;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Checking if this is an assignment
|
||||
if (ereg("(.*)=(.*)", $line, $assign)) {
|
||||
$this->crontabs[] = array ("name" => $assign[1], "value" => $assign[2]);
|
||||
$this->linetypes[] = CRON_ASSIGN;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Checking if this is a special @-entry. check man 5 crontab for more info
|
||||
if ($line[0] == '@') {
|
||||
$this->crontabs[] = split("[ \t]", $line, 2);
|
||||
$this->linetypes[] = CRON_SPECIAL;
|
||||
continue;
|
||||
}
|
||||
|
||||
// It's a regular crontab-entry
|
||||
$ct = split("[ \t]", $line, 6);
|
||||
$this->addCron($ct[0], $ct[1], $ct[2], $ct[3], $ct[4], $ct[5]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the current crontab
|
||||
*/
|
||||
function writeCrontab()
|
||||
{
|
||||
global $DEBUG, $PATH;
|
||||
|
||||
if (empty($this->linetypes)) {
|
||||
return;
|
||||
}
|
||||
$filename = ($DEBUG ? tempnam("$PATH/crons", "cron") : tempnam("/tmp", "cron"));
|
||||
$file = fopen($filename, "w");
|
||||
|
||||
foreach($this->linetypes as $i => $line) {
|
||||
switch ($this->linetypes[$i]) {
|
||||
case CRON_COMMENT:
|
||||
$line = $this->crontabs[$i];
|
||||
break;
|
||||
case CRON_ASSIGN:
|
||||
$line = $this->crontabs[$i][name]." = ".$this->crontabs[$i][value];
|
||||
break;
|
||||
case CRON_CMD:
|
||||
$line = implode(" ", $this->crontabs[$i]);
|
||||
break;
|
||||
case CRON_SPECIAL:
|
||||
$line = implode(" ", $this->crontabs[$i]);
|
||||
break;
|
||||
case CRON_EMPTY:
|
||||
$line = "\n"; // an empty line in the crontab-file
|
||||
break;
|
||||
default:
|
||||
unset($line);
|
||||
echo "Something very weird is going on. This line ($i) has an unknown type.\n";
|
||||
break;
|
||||
}
|
||||
|
||||
// echo "line $i : $line\n";
|
||||
|
||||
if ($line) {
|
||||
$r = @fwrite($file, $line."\n");
|
||||
if($r === FALSE) {
|
||||
return PEAR::raiseError("*** Can't write crontab ***\n".
|
||||
" Set crontab manually!\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
fclose($file);
|
||||
|
||||
if ($DEBUG) {
|
||||
echo "DEBUGMODE: not updating crontab. writing to $filename instead.\n";
|
||||
} else {
|
||||
exec("crontab -u {$this->user} $filename", $returnar, $return);
|
||||
if ($return != 0) {
|
||||
echo "Error running crontab ($return). $filename not deleted\n";
|
||||
} else {
|
||||
unlink($filename);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add a item of type CRON_CMD to the end of $this->crontabs
|
||||
*
|
||||
* @param string $m
|
||||
* minute
|
||||
* @param string $h
|
||||
* hour
|
||||
* @param string $dom
|
||||
* day of month
|
||||
* @param string $mo
|
||||
* month
|
||||
* @param string $dow
|
||||
* day of week
|
||||
* @param string $cmd
|
||||
* command
|
||||
*
|
||||
*/
|
||||
function addCron($m, $h, $dom, $mo, $dow, $cmd)
|
||||
{
|
||||
$this->crontabs[] = array ("minute" => $m, "hour" => $h, "dayofmonth" => $dom, "month" => $mo, "dayofweek" => $dow, "command" => $cmd);
|
||||
$this->linetypes[] = CRON_CMD;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add a comment to the cron to the end of $this->crontabs
|
||||
*
|
||||
* @param string $comment
|
||||
*/
|
||||
function addComment($comment)
|
||||
{
|
||||
$this->crontabs[] = "# $comment\n";
|
||||
$this->linetypes[] = CRON_COMMENT;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add a special command (check man 5 crontab for more information)
|
||||
*
|
||||
* @param string $sdate special date
|
||||
* string meaning
|
||||
* ------ -------
|
||||
* @reboot Run once, at startup.
|
||||
* @yearly Run once a year, "0 0 1 1 *".
|
||||
* @annually (same as @yearly)
|
||||
* @monthly Run once a month, "0 0 1 * *".
|
||||
* @weekly Run once a week, "0 0 * * 0".
|
||||
* @daily Run once a day, "0 0 * * *".
|
||||
* @midnight (same as @daily)
|
||||
* @hourly Run once an hour, "0 * * * *".
|
||||
* @param string $cmd command
|
||||
*/
|
||||
function addSpecial($sdate, $cmd)
|
||||
{
|
||||
$this->crontabs[] = array ("special" => $sdate, "command" => $cmd);
|
||||
$this->linetypes[] = CRON_SPECIAL;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add an assignment (name = value)
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $value
|
||||
*/
|
||||
function addAssign($name, $value)
|
||||
{
|
||||
$this->crontabs[] = array ("name" => $name, "value" => $value);
|
||||
$this->linetypes[] = CRON_ASSIGN;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Delete a line from the arrays.
|
||||
*
|
||||
* @param int $index the index in $this->crontabs
|
||||
*/
|
||||
function delEntry($index)
|
||||
{
|
||||
unset ($this->crontabs[$index]);
|
||||
unset ($this->linetypes[$index]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get all the lines of a certain type in an array
|
||||
*
|
||||
* @param string $type
|
||||
*/
|
||||
function getByType($type)
|
||||
{
|
||||
if ($type < CRON_COMMENT || $type > CRON_EMPTY)
|
||||
{
|
||||
trigger_error("Wrong type: $type", E_USER_WARNING);
|
||||
return 0;
|
||||
}
|
||||
|
||||
$returnar = array ();
|
||||
for ($i = 0; $i < count($this->linetypes); $i ++)
|
||||
if ($this->linetypes[$i] == $type)
|
||||
$returnar[] = $this->crontabs[$i];
|
||||
|
||||
return $returnar;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,8 +0,0 @@
|
|||
#!/usr/bin/php
|
||||
<?php
|
||||
chdir(dirname(__FILE__));
|
||||
$p = unserialize($argv[1]);
|
||||
require_once (dirname(__FILE__).'/'.$p['class'].'.php');
|
||||
$cronjob = new $p['class']();
|
||||
$ret = $cronjob->execute($p['params']);
|
||||
exit(0);
|
Loading…
Add table
Add a link
Reference in a new issue