#!/usr/bin/php -q get_job()){ $c=$cc[0]; $params=unserialize($c["parameters"]); // 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" && is_dir(dirname($params["file"])) || $c["type"] == "CREATE_DIR" || $c["type"] == "DELETE" || $c["type"] == "FIXDIR"){ 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 echo "Can't resume the job n°".$c["id"]." action '".$c["type"]."', finishing it with a fail status.\n"; if(!$action->finish($c["id"],"Fail: Previous script crashed while processing this action, cannot resume it.")){ echo "Cannot finish the action! Error while inserting the error value in the DB for action n°".$c["id"]." : action '".$c["type"]."'\n"; 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"); } } //We get the next action to do while ($rr=$action->get_action()){ $r=$rr[0]; $return="OK"; unset($output); // We lock the action d("-----------\nBeginning action n°".$r["id"]); $action->begin($r["id"]); // We process it $params=unserialize($r["parameters"]); // Remove all previous error message... @trigger_error(""); // We exec with the specified user d("Executing action '".$r["type"]."' with user '".$r["user"]."'"); // For now, this script only work for user 'root' if($r["user"] != "root"){ // TODO } switch ($r["type"]){ case "CREATE_FILE" : if(!file_exists($params["file"])) @file_put_contents($params["file"],$params["content"]); else $output=array("Fail: file already exists"); break; case "CREATE_DIR" : // Create the directory and make parent directories as needed @mkdir($params["dir"],0777,true); break; case "DELETE" : // Delete file/directory and its contents recursively @exec("rm -rf ".$params["dir"]." 2>&1", $output); break; case "MOVE" : // 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); // If MOVE failed, we have to notify the cron if(isset($output[0])) echo "Action n°".$r["id"]." 'MOVE' failed!\nuser: ".$r["user"]."\nsource: ".$params["src"]."\ndestination: ".$params["dst"]."\n"; break; case "FIXDIR" : @exec("$FIXPERM -f ".$params["dir"]." 2>&1", $trash, $code); if($code!=0) $output[0]=$code; break; default : $output=array("Fail: Sorry dude, i do not know this type of action"); break; } // 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. d("Finishing... return value is : $return\n"); if(!$action->finish($r["id"],addslashes($return))){ echo "Cannot finish the action! Error while inserting the error value in the DB for action n°".$c["id"]." : action '".$c["type"]."'\nReturn value: ".addslashes($return)."\n"; break; // Else we go into an infinite loop... AAAAHHHHHH } } // Unlock the script d("Unlock the script..."); unlink($LOCK_FILE); // Exit this script exit(0); ?>