2013-04-19 15:09:34 +00:00
|
|
|
#!/usr/bin/php -q
|
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
$Id: do_actions.php,v 1.0 2013/04/19 13:40:32 axel Exp $
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
AlternC - Web Hosting System
|
|
|
|
Copyright (C) 2002 by the AlternC Development Team.
|
|
|
|
http://alternc.org/
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
Based on:
|
|
|
|
Valentin Lacambre's web hosting softwares: http://altern.org/
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
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
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
Original Author of file: Axel Roger
|
|
|
|
Purpose of file: Do planed actions on files/directories.
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* This script check the MySQL DB for actions to do, and do them one by one.
|
|
|
|
*
|
|
|
|
* @copyright AlternC-Team 2002-2013 http://alternc.org/
|
|
|
|
*/
|
|
|
|
|
2013-04-23 13:32:37 +00:00
|
|
|
// Put this var to 1 if you want to enable debug prints
|
|
|
|
$debug=1;
|
|
|
|
|
|
|
|
// Debug function that print infos
|
|
|
|
function d($mess){
|
|
|
|
global $debug;
|
|
|
|
if ($debug == 1)
|
|
|
|
echo "$mess\n";
|
|
|
|
}
|
|
|
|
|
2013-04-19 15:09:34 +00:00
|
|
|
require_once("/usr/share/alternc/panel/class/config_nochk.php");
|
|
|
|
|
|
|
|
$LOCK_FILE='/var/run/alternc/do_actions_cron.lock';
|
2013-04-23 08:06:44 +00:00
|
|
|
$SCRIPT='/usr/bin/php do_actions.php';
|
2013-04-19 15:09:34 +00:00
|
|
|
$MY_PID=getmypid();
|
|
|
|
|
|
|
|
// Check if script isn't already running
|
2013-04-23 08:06:44 +00:00
|
|
|
if (file_exists($LOCK_FILE) !== false){
|
2013-04-23 13:32:37 +00:00
|
|
|
d("Lock file already exists. ");
|
2013-04-19 15:09:34 +00:00
|
|
|
// Check if file is in process list
|
2013-04-23 08:06:44 +00:00
|
|
|
$PID=file_get_contents($LOCK_FILE);
|
2013-04-23 13:32:37 +00:00
|
|
|
d("My PID is $MY_PID, PID in the lock file is $PID");
|
|
|
|
if ($PID == exec("pidof $SCRIPT | tr ' ' '\n' | grep -v $MY_PID")){
|
2013-04-19 15:09:34 +00:00
|
|
|
// Previous cron is not finished yet, just exit
|
2013-04-23 13:32:37 +00:00
|
|
|
d("Previous cron is already running, we just exit and let it finish :-)");
|
2013-04-23 08:06:44 +00:00
|
|
|
exit(0);
|
2013-04-19 15:09:34 +00:00
|
|
|
}else{
|
|
|
|
// Previous cron failed!
|
2013-04-23 13:32:37 +00:00
|
|
|
d("No process with PID $PID found! Previous cron failed...\nRemoving lock file and trying to process the failed action...");
|
2013-04-19 15:09:34 +00:00
|
|
|
// Delete the lock and continue to the next action
|
|
|
|
unlink($LOCK_FILE);
|
|
|
|
|
2013-04-23 13:32:37 +00:00
|
|
|
// Lock with the current script's PID
|
|
|
|
d("Lock the script...");
|
|
|
|
if (file_put_contents($LOCK_FILE,$MY_PID) === false){
|
|
|
|
die("Cannot open/write $LOCK_FILE");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the action that was processing when previous script failed
|
|
|
|
while($cc=$action->get_job()){
|
|
|
|
$c=$cc[0];
|
|
|
|
// We can resume these types of action, so we reset the job to process it later
|
|
|
|
d("Previous job was the n°".$c["id"]." : '".$c["type"]."'");
|
|
|
|
if($c["type"] == "CREATE_FILE" || $c["type"] == "CREATE_DIR" || $c["type"] == "DELETE"){
|
|
|
|
d("Reset of the job! So it will be resumed...");
|
|
|
|
$action->reset_job($c["id"]);
|
|
|
|
}else{
|
|
|
|
// We can't resume the others types, notify the fail and finish this action
|
|
|
|
d("Can't resume the job, finishing it with a fail status.");
|
|
|
|
if(!$action->finish($c["id"],"Fail: Previous script crashed while processing this action, cannot resume it.")){
|
|
|
|
d("FINISH FAILED!");
|
|
|
|
break; // Else we go into an infinite loop... AAAAHHHHHH
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
// Lock with the current script's PID
|
|
|
|
d("Lock the script...");
|
|
|
|
if (file_put_contents($LOCK_FILE,$MY_PID) === false){
|
|
|
|
die("Cannot open/write $LOCK_FILE");
|
|
|
|
}
|
2013-04-19 15:09:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//We get the next action to do
|
2013-04-23 08:06:44 +00:00
|
|
|
while ($rr=$action->get_action()){
|
|
|
|
$r=$rr[0];
|
|
|
|
$return="OK";
|
2013-04-19 15:09:34 +00:00
|
|
|
// We lock the action
|
2013-04-23 13:32:37 +00:00
|
|
|
d("-----------\nBeginning action n°".$r["id"]);
|
2013-04-19 15:14:34 +00:00
|
|
|
$action->begin($r["id"]);
|
2013-04-19 15:09:34 +00:00
|
|
|
// We process it
|
2013-04-23 08:06:44 +00:00
|
|
|
$params=unserialize($r["parameters"]);
|
|
|
|
// Remove all previous error message...
|
|
|
|
@trigger_error("");
|
2013-04-19 15:09:34 +00:00
|
|
|
// We exec with the specified user
|
2013-04-23 13:32:37 +00:00
|
|
|
d("Executing action '".$r["type"]."' with user '".$r["user"]."'");
|
2013-04-23 08:06:44 +00:00
|
|
|
// For now, this script only work for user 'root'
|
|
|
|
if($r["user"] != "root"){
|
|
|
|
if(exec("su ".$r["user"])){ // TODO
|
2013-04-23 13:32:37 +00:00
|
|
|
d("Login successfull, now processing the action...");
|
2013-04-23 08:06:44 +00:00
|
|
|
}else{
|
2013-04-23 13:32:37 +00:00
|
|
|
d("Error: cannot login as ".$r["user"]);
|
|
|
|
if(!$action->finish($r["id"],"Fail: Cannot login as user ".$r["user"])){
|
|
|
|
d("FINISH FAILED!");
|
|
|
|
break; // Else we go into an infinite loop... AAAAHHHHHH
|
|
|
|
}
|
2013-04-23 08:06:44 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2013-04-19 15:09:34 +00:00
|
|
|
switch ($r["type"]){
|
|
|
|
case "CREATE_FILE" :
|
2013-04-23 08:06:44 +00:00
|
|
|
@file_put_contents($params["file"],$params["content"]);
|
2013-04-19 15:09:34 +00:00
|
|
|
break;
|
|
|
|
case "CREATE_DIR" :
|
2013-04-23 13:32:37 +00:00
|
|
|
// Create the directory and make parent directories as needed
|
2013-04-23 08:06:44 +00:00
|
|
|
@mkdir($params["dir"],0777,true);
|
2013-04-19 15:09:34 +00:00
|
|
|
break;
|
|
|
|
case "DELETE" :
|
2013-04-23 13:32:37 +00:00
|
|
|
// Delete file/directory and its contents recursively
|
2013-04-23 08:06:44 +00:00
|
|
|
@exec("rm -rf ".$params["dir"]." 2>&1", $output);
|
2013-04-19 15:09:34 +00:00
|
|
|
break;
|
|
|
|
case "MOVE" :
|
2013-04-23 08:06:44 +00:00
|
|
|
// If destination dir does not exists, create it
|
|
|
|
if(!is_dir($params["dst"]))
|
|
|
|
@mkdir($params["dst"],0777,true);
|
|
|
|
@exec("mv -f ".$params["src"]." ".$params["dst"]." 2>&1", $output);
|
2013-04-19 15:09:34 +00:00
|
|
|
break;
|
2013-04-22 08:06:48 +00:00
|
|
|
case "PERMFIX" :
|
|
|
|
// TODO
|
|
|
|
break;
|
2013-04-19 15:09:34 +00:00
|
|
|
default :
|
2013-04-23 13:32:37 +00:00
|
|
|
$output=array("Fail: Sorry dude, i don't know this type of action");
|
2013-04-19 15:09:34 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-04-23 08:06:44 +00:00
|
|
|
// Get the last error if exists.
|
|
|
|
if(isset($output[0]))
|
|
|
|
$return=$output[0];
|
|
|
|
else
|
|
|
|
if($error=error_get_last())
|
|
|
|
if($error["message"]!="")
|
|
|
|
$return=$error["message"];
|
|
|
|
// We finished the action, notify the DB.
|
2013-04-23 13:32:37 +00:00
|
|
|
d("Finishing... return value is : $return\n");
|
|
|
|
if(!$action->finish($r["id"],addslashes($return))){
|
|
|
|
d("FINISH FAILED!");
|
|
|
|
break; // Else we go into an infinite loop... AAAAHHHHHH
|
|
|
|
}
|
2013-04-19 15:09:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unlock the script
|
2013-04-23 13:32:37 +00:00
|
|
|
d("Unlock the script...");
|
2013-04-19 15:09:34 +00:00
|
|
|
unlink($LOCK_FILE);
|
2013-04-23 08:06:44 +00:00
|
|
|
|
|
|
|
// Exit this script
|
|
|
|
exit(0);
|
2013-04-19 15:09:34 +00:00
|
|
|
?>
|