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-08-26 10:30:38 +00:00
|
|
|
//FIXME add a NOT NULL constraint on uid in the DB
|
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'));
|
|
|
|
$r[]=$tmp;
|
|
|
|
}
|
|
|
|
return $r;
|
|
|
|
}
|
|
|
|
|
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-08-26 10:30:38 +00:00
|
|
|
// FIXME check the url property
|
2012-04-06 10:10:36 +00:00
|
|
|
$url=mysql_real_escape_string(urlencode($url));
|
|
|
|
$user=mysql_real_escape_string(urlencode($user));
|
|
|
|
if (empty($user)) $password='';
|
|
|
|
$password=mysql_real_escape_string(urlencode($password));
|
|
|
|
if (! checkmail($email) == 0 ) return false;
|
|
|
|
$email=mysql_real_escape_string(urlencode($email));
|
|
|
|
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-08-26 10:44:34 +00:00
|
|
|
$err->raise("cron",_("You seems to be over-quota."));
|
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-04-06 10:10:36 +00:00
|
|
|
if (! $db->next_record()) { return "false"; } // return false if pb
|
|
|
|
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 */
|