2012-04-06 10:10:36 +00:00
|
|
|
<?php
|
|
|
|
/*
|
2012-08-26 10:30:38 +00:00
|
|
|
----------------------------------------------------------------------
|
|
|
|
AlternC - Web Hosting System
|
|
|
|
Copyright (C) 2000-2012 by the AlternC Development Team.
|
|
|
|
https://alternc.org/
|
|
|
|
----------------------------------------------------------------------
|
2012-04-06 10:10:36 +00:00
|
|
|
LICENSE
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License (GPL)
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
To read the license please visit http://www.gnu.org/copyleft/gpl.html
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
Purpose of file: Manage hook system.
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
*/
|
2012-08-26 10:30:38 +00:00
|
|
|
|
2012-04-06 10:10:36 +00:00
|
|
|
/**
|
2012-08-26 10:30:38 +00:00
|
|
|
* This class manage web-cron tasks
|
2012-04-06 10:10:36 +00:00
|
|
|
*/
|
|
|
|
class m_cron {
|
|
|
|
|
2012-08-26 10:30:38 +00:00
|
|
|
|
2012-04-06 10:10:36 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/** Constructor
|
|
|
|
*/
|
|
|
|
function m_cron() {
|
|
|
|
}
|
2012-09-13 14:23:18 +00:00
|
|
|
|
2012-04-06 10:10:36 +00:00
|
|
|
function schedule() {
|
2012-08-26 10:30:38 +00:00
|
|
|
return Array(
|
|
|
|
Array('unit'=>1440, 'name'=>_("Daily")),
|
|
|
|
Array('unit'=>60, 'name'=>_("Hour")),
|
|
|
|
Array('unit'=>30, 'name'=>_("Half Hour")),
|
|
|
|
);
|
2012-04-06 10:10:36 +00:00
|
|
|
}
|
|
|
|
|
2012-08-26 10:30:38 +00:00
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/** List the crontab for the current user.
|
|
|
|
* @return array an hash for each crontab.
|
|
|
|
*/
|
2012-04-06 10:10:36 +00:00
|
|
|
function lst_cron() {
|
|
|
|
global $cuid,$db,$err;
|
|
|
|
$err->log("cron","lst_cron");
|
2012-08-26 10:30:38 +00:00
|
|
|
$db->query("SELECT * FROM cron WHERE uid = $cuid ORDER BY url;");
|
2012-04-06 10:10:36 +00:00
|
|
|
$r=Array();
|
|
|
|
while ($db->next_record()) {
|
|
|
|
$tmp=Array();
|
|
|
|
$tmp['id']=$db->f('id');
|
|
|
|
$tmp['url']=urldecode($db->f('url'));
|
|
|
|
$tmp['user']=urldecode($db->f('user'));
|
|
|
|
$tmp['password']=urldecode($db->f('password'));
|
|
|
|
$tmp['schedule']=$db->f('schedule');
|
|
|
|
$tmp['email']=urldecode($db->f('email'));
|
2012-10-16 14:31:56 +00:00
|
|
|
$tmp['next_execution']=$db->f('next_execution');
|
2012-04-06 10:10:36 +00:00
|
|
|
$r[]=$tmp;
|
|
|
|
}
|
|
|
|
return $r;
|
|
|
|
}
|
|
|
|
|
2013-02-18 10:01:28 +00:00
|
|
|
function hook_menu() {
|
|
|
|
$obj = array(
|
|
|
|
'title' => _("Scheduled tasks"),
|
|
|
|
'ico' => 'images/schedule.png',
|
|
|
|
'link' => 'cron.php',
|
|
|
|
'pos' => 90,
|
|
|
|
) ;
|
|
|
|
|
|
|
|
return $obj;
|
|
|
|
}
|
2012-08-26 10:30:38 +00:00
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/** update the crontab
|
|
|
|
* @param $arr array the crontab information, including its ID
|
|
|
|
* @return boolean TRUE if the crontab has been edited
|
|
|
|
*/
|
2012-04-06 10:10:36 +00:00
|
|
|
function update($arr) {
|
|
|
|
$ok=true;
|
|
|
|
foreach ($arr as $a) {
|
|
|
|
if (! isset($a['id'])) $a['id']=null;
|
|
|
|
if (empty($a['url']) && is_null($a['id'])) continue;
|
2012-08-26 10:30:38 +00:00
|
|
|
if (! $this->_update_one($a['url'], $a['user'], $a['password'], $a['email'], $a['schedule'], $a['id']) ) {
|
2012-04-06 10:10:36 +00:00
|
|
|
$ok=false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $ok;
|
|
|
|
}
|
2012-08-26 10:30:38 +00:00
|
|
|
|
2012-04-06 10:10:36 +00:00
|
|
|
|
2012-08-26 10:30:38 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/** delete a crontab
|
|
|
|
* @param $id the id of the crontab to delete
|
|
|
|
* @return boolean TRUE if the crontab has been deleted
|
|
|
|
*/
|
2012-04-06 10:10:36 +00:00
|
|
|
function delete_one($id) {
|
|
|
|
global $db,$err,$cuid;
|
|
|
|
$err->log("cron","delete_one");
|
2012-08-26 10:30:38 +00:00
|
|
|
return $db->query("DELETE FROM cron WHERE id=".intval($id)." AND uid=$cuid LIMIT 1;");
|
2012-04-06 10:10:36 +00:00
|
|
|
}
|
2012-08-26 10:30:38 +00:00
|
|
|
|
2012-04-06 10:10:36 +00:00
|
|
|
|
2012-08-26 10:30:38 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/** update a crontab,
|
|
|
|
* @return boolean TRUE if the crontab has been edited
|
|
|
|
*/
|
|
|
|
private function _update_one($url, $user, $password, $email, $schedule, $id=null) {
|
2012-04-06 10:10:36 +00:00
|
|
|
global $db,$err,$quota,$cuid;
|
|
|
|
$err->log("cron","update_one");
|
|
|
|
|
|
|
|
if (empty($url) && !is_null($id)) {
|
|
|
|
return $this->delete_one($id);
|
|
|
|
}
|
|
|
|
|
2012-09-13 14:23:18 +00:00
|
|
|
|
|
|
|
if(filter_var($url,FILTER_VALIDATE_URL)===false){
|
|
|
|
$err->raise("cron",_("URL not valid"));
|
|
|
|
return false;
|
|
|
|
}
|
2012-10-16 14:31:56 +00:00
|
|
|
$url=urlencode($url);
|
|
|
|
$user=urlencode($user);
|
2012-04-06 10:10:36 +00:00
|
|
|
if (empty($user)) $password='';
|
2012-10-16 14:31:56 +00:00
|
|
|
$password=urlencode($password);
|
2014-03-26 13:29:00 +00:00
|
|
|
|
|
|
|
//@todo remove checkmail cf functions.php
|
2012-10-17 11:54:42 +00:00
|
|
|
if (!empty($email) && ! checkmail($email) == 0 ){
|
2012-09-13 14:23:18 +00:00
|
|
|
$err->raise("cron",_("Email address is not valid"));
|
|
|
|
return false;
|
|
|
|
}
|
2012-10-16 14:31:56 +00:00
|
|
|
$email=urlencode($email);
|
2012-04-06 10:10:36 +00:00
|
|
|
if (! $this->valid_schedule($schedule)) return false;
|
|
|
|
|
|
|
|
if (is_null($id)) { // if a new insert, quotacheck
|
|
|
|
$q = $quota->getquota("cron");
|
|
|
|
if ( $q["u"] >= $q["t"] ) {
|
2012-10-18 07:52:54 +00:00
|
|
|
$err->raise("cron",_("You quota of cron entries is over. You cannot create more cron entries"));
|
2012-04-06 10:10:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else { // if not a new insert, check the $cuid
|
2012-08-26 10:30:38 +00:00
|
|
|
$db->query("SELECT uid FROM cron WHERE id = $id;");
|
2012-09-13 14:23:18 +00:00
|
|
|
if (! $db->next_record()) {
|
|
|
|
return "false";
|
|
|
|
} // return false if pb
|
2012-04-06 10:10:36 +00:00
|
|
|
if ( $db->f('uid') != $cuid ) {
|
2012-08-26 13:31:38 +00:00
|
|
|
$err->raise("cron",_("Identity problem"));
|
2012-04-06 10:10:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2012-08-26 10:44:34 +00:00
|
|
|
$query = "REPLACE INTO cron (id, uid, url, user, password, schedule, email) VALUES ('$id', '$cuid', '$url', '$user', '$password', '$schedule', '$email') ;";
|
2012-04-06 10:10:36 +00:00
|
|
|
return $db->query("$query");
|
|
|
|
}
|
|
|
|
|
2012-08-26 10:30:38 +00:00
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/** validate a crontab schedule
|
|
|
|
* @param $s array schedule paramters
|
|
|
|
* @return boolean TRUE if the schedule is valid
|
|
|
|
*/
|
2012-04-06 10:10:36 +00:00
|
|
|
function valid_schedule($s) {
|
|
|
|
$s2 = intval($s);
|
|
|
|
if ($s2 != $s) return false;
|
|
|
|
$r=false;
|
|
|
|
foreach ($this->schedule() as $cs ) {
|
|
|
|
if ($cs['unit'] == $s) return true;
|
|
|
|
}
|
|
|
|
return $r;
|
|
|
|
}
|
|
|
|
|
2012-08-26 10:30:38 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/** hook for quota computation
|
|
|
|
*/
|
|
|
|
function hook_quota_get() {
|
2012-04-06 10:10:36 +00:00
|
|
|
global $cuid,$db,$err;
|
|
|
|
$err->log("cron","alternc_get_quota");
|
2012-08-26 16:11:53 +00:00
|
|
|
$q=Array("name"=>"cron", "description"=>_("Scheduled tasks"), "used"=>0);
|
2012-04-06 10:10:36 +00:00
|
|
|
$db->query("select count(*) as cnt from cron where uid = $cuid;");
|
|
|
|
if ($db->next_record()) {
|
2012-08-26 16:11:53 +00:00
|
|
|
$q['used']=$db->f('cnt');
|
2012-04-06 10:10:36 +00:00
|
|
|
}
|
2012-08-26 16:11:53 +00:00
|
|
|
return $q;
|
2012-04-06 10:10:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-26 10:30:38 +00:00
|
|
|
} /* Class cron */
|