1
0
mirror of https://github.com/e107inc/e107.git synced 2025-08-01 04:10:38 +02:00

cron administration clean up

This commit is contained in:
secretr
2010-11-15 16:32:33 +00:00
parent aa0d2e8da4
commit ecfebe6284

View File

@@ -1,31 +1,25 @@
<?php
/*
+ ----------------------------------------------------------------------------+
| e107 website system
|
| Copyright (C) 2008-2010 e107 Inc
| http://e107.org/
|
|
| Released under the terms and conditions of the
| GNU General Public License (http://gnu.org/).
|
| $Source: /cvs_backup/e107_0.8/e107_admin/cron.php,v $
| $Revision$
| $Date$
| $Author$
+----------------------------------------------------------------------------+
*/
* e107 website system
*
* Copyright (C) 2008-2010 e107 Inc (e107.org)
* Released under the terms and conditions of the
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
*
* Cron Administration
*
* $URL$
* $Id$
*
*/
/**
*
* @package e107
* @subpackage admin
* @version $Revision$
* @author $Author$
* @version $Id$
* Admin-related functions for cron (Scheduler) management
*/
*/
require_once('../class2.php');
if (!getperms('U'))
@@ -39,24 +33,22 @@ include_lan(e_LANGUAGEDIR.e_LANGUAGE.'/admin/lan_'.e_PAGE);
$e_sub_cat = 'cron';
require_once('auth.php');
require_once (e_HANDLER.'message_handler.php');
require_once(e_HANDLER.'form_handler.php');
$frm = new e_form(true);
$cron = new cron;
$frm = e107::getForm();
$cron = new cron();
require_once(e_ADMIN.'footer.php');
exit;
class cron
{
var $coreCrons = array();
var $cronAction;
var $e_cron = array();
protected $coreCrons = array();
protected $cronAction;
protected $e_cron = array();
function cron()
public function __construct()
{
global $pref;
$mes = $mes = e107::getMessage();
$pref = e107::getPref();
$mes = e107::getMessage();
$this->cronAction = e_QUERY;
// The 'available' flag only gives the option to configure the cron if the underlying feature is enabled
@@ -69,15 +61,12 @@ class cron
// 3 => array('name'=>'News Sticky', 'function' => 'newsPurge', 'description'=>'Remove Sticky News Items')
);
if(!vartrue($pref['e_cron_pwd']))
if (!vartrue($pref['e_cron_pwd']))
{
$pwd = $this->setCronPwd();
}
if(isset($_POST['submit']))
if (isset($_POST['submit']))
{
$this->cronSave();
}
@@ -85,68 +74,61 @@ class cron
$this->lastRefresh();
$this->cronLoad();
if(isset($_POST['save_prefs']))
if (isset($_POST['save_prefs']))
{
$this -> cronSavePrefs();
$this->cronSavePrefs();
}
if(isset($_POST['execute']))
if (isset($_POST['execute']))
{
$class_func = key($_POST['execute']);
$this -> cronExecute($class_func);
$this->cronExecute($class_func);
}
// Set Core Cron Options.
// These core functions need to be put into e_BASE/cron.php ie. news_purge()
if($this->cronAction == "" || $this->cronAction == "main")
if ($this->cronAction == "" || $this->cronAction == "main")
{
$this -> cronRenderPage();
$this->cronRenderPage();
}
if($this->cronAction == "pref")
if ($this->cronAction == "pref")
{
$this -> cronRenderPrefs();
$this->cronRenderPrefs();
}
}
function lastRefresh()
{
global $pref;
$pref = e107::getPref();
e107::getCache()->CachePageMD5 = '_';
$lastload = e107::getCache()->retrieve('cronLastLoad',FALSE,TRUE,TRUE);
$lastload = e107::getCache()->retrieve('cronLastLoad', FALSE, TRUE, TRUE);
$mes = e107::getMessage();
$ago = (time() - $lastload);
$active = ($ago < 901) ? TRUE : FALSE;
$status = ($active) ? LAN_ENABLED : LAN_DISABLED; // "Enabled" : "Offline";
$mins = floor ($ago / 60);
$mins = floor($ago / 60);
$secs = $ago % 60;
$lastRun = ($mins) ? $mins." minutes and ".$secs." seconds ago." : $secs." seconds ago.";
$lastRefresh = ($ago < 10000) ? $lastRun : 'Never';
$mes->add("Status: <b>".$status."</b>", E_MESSAGE_INFO);
// print_a($pref['e_cron_pref']);
if($pref['e_cron_pref']) // grab cron
if ($pref['e_cron_pref']) // grab cron
{
foreach($pref['e_cron_pref'] as $func=>$cron)
foreach ($pref['e_cron_pref'] as $func => $cron)
{
if($cron['active']==1)
if ($cron['active'] == 1)
{
$list[$func] = $cron;
}
@@ -159,51 +141,47 @@ class cron
//FIXME: for Windows, the is_executable() function only checks the file
// extensions of exe, com, bat and cmd.
if(!is_executable(e_BASE."cron.php"))
if (!is_executable(e_BASE."cron.php"))
{
$mes->add("Please CHMOD /cron.php to 755" , E_MESSAGE_WARNING);
$mes->add("Please CHMOD /cron.php to 755", E_MESSAGE_WARNING);
}
elseif(!$active)
elseif (!$active)
{
$setpwd_message = "Use the following Cron Command: <b style='color:black'>".$_SERVER['DOCUMENT_ROOT'].e_HTTP."cron.php ".$pref['e_cron_pwd']."</b><br />
Using your server control panel (eg. cPanel,Plesk etc.) please create a crontab to run this command on your server every minute.";
$mes->add($setpwd_message, E_MESSAGE_INFO);
}
}
function cronName($classname,$method)
{
function cronName($classname, $method)
{
$tp = e107::getParser();
foreach($this->e_cron as $class=>$val)
foreach ($this->e_cron as $class => $val)
{
if($class == $classname)
if ($class == $classname)
{
foreach($val as $func)
foreach ($val as $func)
{
if($func['function'] == $method)
if ($func['function'] == $method)
{
return $tp->toHtml($func['name']);
}
}
}
}
}
}
function cronExecute($class_func)
{
//TODO LANs
list($class_name,$method_name) = explode("__",$class_func);
list($class_name, $method_name) = explode("__", $class_func);
$mes = e107::getMessage();
$taskName = $class_name;
if($class_name =='_system')
if ($class_name == '_system')
{
require_once(e_HANDLER.'cron_class.php');
}
@@ -212,8 +190,8 @@ function cronName($classname,$method)
require_once(e_PLUGIN.$class_name.'/e_cron.php');
}
$class_name .= '_cron';
$status = $this->cronExecuteMethod($class_name,$method_name) ? E_MESSAGE_SUCCESS : E_MESSAGE_ERROR;
$mes->add("Running <b>".$this->cronName($taskName,$method_name)."</b>", $status);
$status = $this->cronExecuteMethod($class_name, $method_name) ? E_MESSAGE_SUCCESS : E_MESSAGE_ERROR;
$mes->add("Running <b>".$this->cronName($taskName, $method_name)."</b>", $status);
}
@@ -231,9 +209,9 @@ function cronName($classname,$method)
$mes = e107::getMessage();
$activeCount = 0;
foreach($_POST['cron'] as $key=>$val)
foreach ($_POST['cron'] as $key => $val)
{
if(!$val['active'])
if (!$val['active'])
{
$val['active'] = 0;
}
@@ -242,16 +220,16 @@ function cronName($classname,$method)
$activeCount++;
}
$t['minute'] = implode(",",$_POST['tab'][$key]['minute']);
$t['hour'] = implode(",",$_POST['tab'][$key]['hour']);
$t['day'] = implode(",",$_POST['tab'][$key]['day']);
$t['month'] = implode(",",$_POST['tab'][$key]['month']);
$t['weekday'] = implode(",",$_POST['tab'][$key]['weekday']);
$t['minute'] = implode(",", $_POST['tab'][$key]['minute']);
$t['hour'] = implode(",", $_POST['tab'][$key]['hour']);
$t['day'] = implode(",", $_POST['tab'][$key]['day']);
$t['month'] = implode(",", $_POST['tab'][$key]['month']);
$t['weekday'] = implode(",", $_POST['tab'][$key]['weekday']);
$val['tab'] = implode(" ",$t);
$val['tab'] = implode(" ", $t);
$tabs .= $val['tab']."<br />";
list($class,$func) = explode("__",$key);
list($class, $func) = explode("__", $key);
$val['function'] = $func;
$val['class'] = $class;
@@ -262,7 +240,7 @@ function cronName($classname,$method)
$pref['e_cron_pref'] = $cron;
if(!vartrue($pref['e_cron_pwd']) || varset($_POST['generate_pwd']))
if (!vartrue($pref['e_cron_pwd']) || varset($_POST['generate_pwd']))
{
$pwd = $this->setCronPwd();
@@ -271,10 +249,9 @@ function cronName($classname,$method)
$mes->add($setpwd_message, E_MESSAGE_WARNING);
}
// print_a($pref['e_cron_pref']);
if(save_prefs())
if (save_prefs())
{
$mes->add(LAN_SETSAVED, E_MESSAGE_SUCCESS);
$mes->add($activeCount." Cron(s) Active", E_MESSAGE_SUCCESS);
@@ -286,33 +263,33 @@ function cronName($classname,$method)
}
function setCronPwd()
{
global $pref;
function setCronPwd()
{
//global $pref;
$userMethods = e107::getUserSession();
$newpwd = $userMethods->generateRandomString('*^*#.**^*');
$newpwd = sha1($newpwd.time());
$pref['e_cron_pwd'] = $newpwd;
//$pref['e_cron_pwd'] = $newpwd;
e107::getConfig()->set('e_cron_pwd', $newpwd)->save(false);
return true;
return save_prefs();
}
}
// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
function cronRenderPrefs()
{
global $frm,$ns;
//global $frm,$ns;
$frm = e107::getForm();
$text = "<div style='text-align:center'>
<form method='post' action='".e_SELF."' id='linkform'>
<form method='post' action='"
.e_SELF."' id='linkform'>
<table class='adminlist'>
<tr>
<td style='width:30%'>Cron Password</td>
<td style='width:70%'>
".$frm->password('cron_password',100)."
"
.$frm->password('cron_password', 100)."
</td>
</tr>
@@ -320,7 +297,7 @@ function setCronPwd()
<tr style='vertical-align:top'>
<td colspan='2' class='center buttons-bar'>";
$text .= $frm->admin_button('save_prefs',LAN_SAVE, 'update');
$text .= $frm->admin_button('save_prefs', LAN_SAVE, 'update');
$text .= "</td>
</tr>
@@ -328,28 +305,27 @@ function setCronPwd()
</form>
</div>";
$ns -> tablerender(LAN_PREFS, $text);
e107::getRender()->tablerender(LAN_PREFS, $text);
}
function cronLoad() //TODO Make a generic function to work with e_cron, e_sitelink, e_url etc.
{
global $pref;
$pref = e107::getPref();
$core_cron = $this->coreCrons; // May need to check 'available' flag here
$new_cron = e107::getAddonConfig('e_cron');
$this->e_cron = array_merge($core_cron,$new_cron);
$this->e_cron = array_merge($core_cron, $new_cron);
return;
}
// ----------- Grab All e_cron parameters -----------------------------------
// ----------- Grab All e_cron parameters -----------------------------------
function cronRenderPage()
{
global $pref;
$pref = e107::getPref();
$cronpref = $pref['e_cron_pref'];
$ns = e107::getRender();
$frm = e107::getForm();
@@ -357,11 +333,11 @@ function setCronPwd()
$e_cron = $this->e_cron;
// ---------------------- List All Functions -----------------------------
$text = "<div style='text-align:center'>
<form method='post' action='".e_SELF."' id='cronform'>
<form method='post' action='"
.e_SELF."' id='cronform'>
<table class='adminlist'>
<colgroup span='8'>
<col></col>
@@ -375,43 +351,52 @@ function setCronPwd()
</colgroup>
<thead>
<tr>
<th>".LAN_CRON_1."</th>
<th>".LAN_CRON_2."</th>
<th>".LAN_CRON_3."</th>
<th>".LAN_CRON_4."</th>
<th>".LAN_CRON_5."</th>
<th>".LAN_CRON_6."</th>
<th>".LAN_CRON_7."</th>
<th>".LAN_CRON_8."</th>
<th>"
.LAN_CRON_1."</th>
<th>"
.LAN_CRON_2."</th>
<th>"
.LAN_CRON_3."</th>
<th>"
.LAN_CRON_4."</th>
<th>"
.LAN_CRON_5."</th>
<th>"
.LAN_CRON_6."</th>
<th>"
.LAN_CRON_7."</th>
<th>"
.LAN_CRON_8."</th>
<th>Run Now</th>
</tr>
</thead>
<tbody>";
foreach($e_cron as $plug=>$cfg)
foreach ($e_cron as $plug => $cfg)
{
foreach($cfg as $class=>$cron)
foreach ($cfg as $class => $cron)
{
if (!isset($cron['available']) || $cron['available']) // Only display cron functions which are available
{
$c = $plug.'__'. $cron['function']; // class and function.
$c = $plug.'__'.$cron['function']; // class and function.
$sep = array();
list($sep['minute'],$sep['hour'],$sep['day'],$sep['month'],$sep['weekday']) = explode(" ",$cronpref[$c]['tab']);
list($sep['minute'], $sep['hour'], $sep['day'], $sep['month'], $sep['weekday']) = explode(" ", $cronpref[$c]['tab']);
foreach($sep as $key=>$value)
foreach ($sep as $key => $value)
{
if($value=="")
if ($value == "")
{
$sep[$key] = "*";
}
}
$minute = explode(",",$sep['minute']);
$hour = explode(",",$sep['hour']);
$day = explode(",",$sep['day']);
$month = explode(",",$sep['month']);
$weekday = explode(",",$sep['weekday']);
$minute = explode(",", $sep['minute']);
$hour = explode(",", $sep['hour']);
$day = explode(",", $sep['day']);
$month = explode(",", $sep['month']);
$weekday = explode(",", $sep['weekday']);
$min_options = array(
"*" => LAN_CRON_11,
@@ -430,15 +415,17 @@ function setCronPwd()
);
$text .= "<tr>
<td>".$cron['name']."</td>
<td>".$cron['description']."</td>
<td>"
.$cron['name']."</td>
<td>"
.$cron['description']."</td>
<td>
<input type='hidden' name='cron[$c][path]' value='".$cron['path']."' />
<select class='tbox' style='height:70px' multiple='multiple' name='tab[$c][minute][]'>\n";
foreach($min_options as $key=>$val)
foreach ($min_options as $key => $val)
{
if($sep['minute'] == $key)
if ($sep['minute'] == $key)
{
$sel = "selected='selected'";
$minute = array();
@@ -450,10 +437,9 @@ function setCronPwd()
$text .= "<option value='$key' $sel>".$val."</option>\n";
}
for ($i=0; $i<=59; $i++)
for ($i = 0; $i <= 59; $i++)
{
$sel = (in_array(strval($i),$minute)) ? "selected='selected'" : "";
$sel = (in_array(strval($i), $minute)) ? "selected='selected'" : "";
$text .= "<option value='$i' $sel>".$i."</option>\n";
}
$text .= "</select>
@@ -462,9 +448,9 @@ function setCronPwd()
<select class='tbox' style='height:70px' multiple='multiple' name='tab[$c][hour][]'>
\n";
foreach($hour_options as $key=>$val)
foreach ($hour_options as $key => $val)
{
if($sep['hour'] == $key)
if ($sep['hour'] == $key)
{
$sel = "selected='selected'";
$hour = array();
@@ -476,11 +462,11 @@ function setCronPwd()
$text .= "<option value='$key' $sel>".$val."</option>\n";
}
for ($i=0; $i<=23; $i++)
for ($i = 0; $i <= 23; $i++)
{
$sel = (in_array(strval($i),$hour)) ? "selected='selected'" : "";
$diz = mktime($i,00,00,1,1,2000);
$text .= "<option value='$i' $sel>".$i." - ".date("g A",$diz)."</option>\n";
$sel = (in_array(strval($i), $hour)) ? "selected='selected'" : "";
$diz = mktime($i, 00, 00, 1, 1, 2000);
$text .= "<option value='$i' $sel>".$i." - ".date("g A", $diz)."</option>\n";
}
$text .= "</select>
</td>
@@ -490,9 +476,9 @@ function setCronPwd()
$sel_day = ($day[0] == "*") ? "selected='selected'" : "";
$text .= "<option value='*' {$sel_day}>".LAN_CRON_20."</option>\n"; // Every Day
for ($i=1; $i<=31; $i++)
for ($i = 1; $i <= 31; $i++)
{
$sel = (in_array($i,$day)) ? "selected='selected'" : "";
$sel = (in_array($i, $day)) ? "selected='selected'" : "";
$text .= "<option value='$i' $sel>".$i."</option>\n";
}
$text .= "</select>
@@ -503,11 +489,11 @@ function setCronPwd()
$sel_month = ($month[0] == "*") ? "selected='selected'" : "";
$text .= "<option value='*' $sel_month>".LAN_CRON_21."</option>\n"; // Every Month
for ($i=1; $i<=12; $i++)
for ($i = 1; $i <= 12; $i++)
{
$sel = (in_array($i,$month)) ? "selected='selected'" : "";
$diz = mktime(00,00,00,$i,1,2000);
$text .= "<option value='$i' $sel>".strftime("%B",$diz)."</option>\n";
$sel = (in_array($i, $month)) ? "selected='selected'" : "";
$diz = mktime(00, 00, 00, $i, 1, 2000);
$text .= "<option value='$i' $sel>".strftime("%B", $diz)."</option>\n";
}
$text .= "</select>
</td>
@@ -516,11 +502,11 @@ function setCronPwd()
$sel_weekday = ($weekday[0] == "*") ? "selected='selected'" : "";
$text .= "<option value='*' $sel_weekday>".LAN_CRON_22."</option>\n"; // Every Week Day.
$days = array(LAN_SUN,LAN_MON,LAN_TUE,LAN_WED,LAN_THU,LAN_FRI,LAN_SAT);
$days = array(LAN_SUN, LAN_MON, LAN_TUE, LAN_WED, LAN_THU, LAN_FRI, LAN_SAT);
for ($i=0; $i<=6; $i++)
for ($i = 0; $i <= 6; $i++)
{
$sel = (in_array(strval($i),$weekday)) ? "selected='selected'" : "";
$sel = (in_array(strval($i), $weekday)) ? "selected='selected'" : "";
$text .= "<option value='$i' $sel>".$days[$i]."</option>\n";
}
$text .= "</select>
@@ -542,7 +528,7 @@ function setCronPwd()
<div class='center buttons-bar'>";
// $text .= "<input class='button' type='submit' name='submit' value='".LAN_SAVE."' />";
$text .= $frm->admin_button('submit', LAN_SAVE, $action = 'update');
$text .= $frm->checkbox_switch('generate_pwd',1,'','Generate new cron command');
$text .= $frm->checkbox_switch('generate_pwd', 1, '', 'Generate new cron command');
$text .= "</div></td>
</tr>
</tbody>
@@ -550,16 +536,16 @@ function setCronPwd()
</form>
</div>";
$ns -> tablerender(PAGE_NAME, $mes->render() . $text);
$ns->tablerender(PAGE_NAME, $mes->render().$text);
}
function cronOptions()
{
$e107 = &e107::getInstance();
$e107 = e107::getInstance();
$var['main']['text'] = PAGE_NAME;
$var['main']['link'] = e_SELF;
/*
/*
$var['pref']['text'] = LAN_PREFS;
$var['pref']['link'] = e_SELF."?pref";
$var['pref']['perm'] = "N";
@@ -569,25 +555,24 @@ function setCronPwd()
e_admin_menu(PAGE_NAME, $action, $var);
}
function cronExecuteMethod($class_name,$method_name,$return='boolean')
function cronExecuteMethod($class_name, $method_name, $return = 'boolean')
{
$mes = e107::getMessage();
if(class_exists($class_name))
if (class_exists($class_name))
{
$obj = new $class_name;
if(method_exists($obj,$method_name))
if (method_exists($obj, $method_name))
{
$mes->add("Executing config function <b>".$key." : ".$method_name."()</b>", E_MESSAGE_DEBUG);
if($return == 'boolean')
$mes->add("Executing config function <b>".$class_name." : ".$method_name."()</b>", E_MESSAGE_DEBUG);
if ($return == 'boolean')
{
call_user_func(array($obj,$method_name));
call_user_func(array($obj, $method_name));
return TRUE;
}
else
{
return call_user_func(array($obj,$method_name));
return call_user_func(array($obj, $method_name));
}
}
else
@@ -605,5 +590,4 @@ function cron_adminmenu()
$cron->cronOptions();
}
?>