54 require_once(
"/usr/share/alternc/panel/class/config_nochk.php");
57 $admin->stop_if_jobs_locked();
59 if( !defined(
"ALTERNC_DO_ACTION_LOCK")){
60 define(
"ALTERNC_DO_ACTION_LOCK",
'/run/alternc/do_actions_cron.lock');
96 mail(
"alterncpanel@$L_FQDN",
'Script do_actions.php issues',
"\n Errors reporting mail:\n\n$msg");
107 $cmd_line =
"$command ";
108 if (!empty($parameters)) {
109 if (is_array($parameters)) {
110 foreach($parameters as $pp) {
111 $cmd_line.=
" ".escapeshellarg($pp).
" ";
114 $cmd_line.=
" ".escapeshellarg($parameters).
" " ;
118 exec($cmd_line, $output, $code);
119 return array(
'executed' => $cmd_line,
'output'=>$output,
'return_val'=>$code);
128 global $L_ALTERNC_HTML, $L_ALTERNC_MAIL;
130 $allowed=array(realpath($L_ALTERNC_HTML).
"/", realpath($L_ALTERNC_MAIL).
"/");
131 $path=realpath($path);
132 foreach($allowed as $one) {
134 if (strlen($path)>strlen($one) && substr($path,0,strlen($one))==$one) {
142 if (file_exists(ALTERNC_DO_ACTION_LOCK) !==
false){
143 d(
"Lock file already exists. ");
145 $PID=file_get_contents(ALTERNC_DO_ACTION_LOCK);
146 d(
"My PID is $MY_PID, PID in the lock file is $PID");
147 if ($PID == exec(
"pidof $SCRIPT | tr ' ' '\n' | grep -v $MY_PID")){
149 d(
"Previous cron is already running, we just exit and let it finish :-)");
153 $errorsList[]=
"Lock file already exists. No process with PID $PID found! Previous cron failed...\n";
161 if (file_put_contents(ALTERNC_DO_ACTION_LOCK,
$MY_PID) ===
false){
162 $errorsList[]=
"Cannot open/write ALTERNC_DO_ACTION_LOCK\n";
169 while($cc=$action->get_job()){
171 $params=unserialize(
$c[
"parameters"]);
173 d(
"Previous job was the n°".
$c[
"id"].
" : '".
$c[
"type"].
"'");
174 if(
$c[
"type"] ==
"CREATE_FILE" && is_dir(dirname($params[
"file"])) ||
$c[
"type"] ==
"CREATE_DIR" ||
$c[
"type"] ==
"DELETE" ||
$c[
"type"] ==
"FIX_DIR" ||
$c[
"type"] ==
"FIX_FILE"){
175 d(
"Reset of the job! So it will be resumed...");
176 $action->reset_job(
$c[
"id"]);
179 $errorsList[]=
"Can't resume the job n°".$c[
"id"].
" action '".
$c[
"type"].
"', finishing it with a fail status.\n";
180 if(!$action->finish(
$c[
"id"],
"Fail: Previous script crashed while processing this action, cannot resume it.")){
181 $errorsList[]=
"Cannot finish the action! Error while inserting the error value in the DB for action n°".$c[
"id"].
" : action '".
$c[
"type"].
"'\n";
189 if (file_put_contents(ALTERNC_DO_ACTION_LOCK,
$MY_PID) ===
false){
190 $errorsList[]=
"Cannot open/write ALTERNC_DO_ACTION_LOCK\n";
197 while (
$rr=$action->get_action()){
201 if(
$r[
"user"] !=
"root")
202 $SU=
"su ".$r[
"user"].
" 2>&1 ;";
206 d(
"-----------\nBeginning action n°".
$r[
"id"]);
207 $action->begin(
$r[
"id"]);
209 $params=@unserialize(
$r[
"parameters"]);
211 d(
"Executing action '".
$r[
"type"].
"' with user '".
$r[
"user"].
"'");
215 $returned =
execute_cmd(
"$FIXPERM -u", $params[
"uid"]);
219 if ($filename===
false) {
223 $perms=$params[
"perms"];
225 if( !is_dir($filename) && ! is_file($filename)){
226 $errorsList=array(
"Fail: cannot retrieve CHMOD filename" );
229 else if ( !is_int( $perms)){
230 $errorsList=array(
"Fail: Incorrect perms : $perms");
233 else if( !chmod($filename, $perms)) {
234 $errorsList=array(
"Fail: cannot change perms ($perms) on filename ($filename)");
239 $dirname=
my_realpath(dirname($params[
"filename"]));
240 $filename=basename($params[
"filename"]);
241 if ($dirname===
false) {
245 $params[
"file"]=$dirname.DIRECTORY_SEPARATOR.$filename;
246 if(!file_exists($params[
"file"])) {
247 if ( file_put_contents($params[
"file"], $params[
"content"]) ===
false ) {
248 $errorsList=array(
"Fail: can't write into file ".$params[
"file"]);
250 if (!chown($params[
"file"],
$r[
"user"])) {
251 $errorsList=array(
"Fail: cannot chown ".$params[
"file"]);
255 $errorsList=array(
"Fail: file already exists ".$params[
"file"]);
260 $filename=basename($params[
"dir"]);
261 if ($dirname===
false) {
265 $params[
"dir"]=$dirname.DIRECTORY_SEPARATOR.$filename;
267 $returned =
execute_cmd(
"$SU mkdir", array(
'-p', $params[
"dir"]));
271 if ($dirname===
false) {
276 $returned =
execute_cmd(
"$SU rm", array(
'-rf', $dirname));
281 $filename=basename($params[
"dst"]);
282 if ($dirname===
false) {
286 $params[
"dst"]=$dirname.DIRECTORY_SEPARATOR.$filename;
288 if ($params[
"src"]===
false) {
293 if (!is_dir($params[
"dst"]))
294 if ( @mkdir($params[
"dst"], 0777,
true)) {
295 if ( @chown($params[
"dst"],
$r[
"user"]) ) {
296 $returned =
execute_cmd(
"$SU mv -f", array($params[
"src"], $params[
"dst"]));
299 $errorsList=array(
"Fail: cannot create ".$params[
"dst"]);
305 if ($params[
"dir"]===
false) {
310 if($returned[
'return_val'] != 0) {
311 $errorsList=array(
"Fixperms.sh failed, returned error code : ".$returned[
'return_val']);
316 if ($params[
"file"]===
false) {
321 if($returned[
'return_val'] != 0){
322 $errorsList=array(
"Fixperms.sh failed, returned error code : ".$returned[
'return_val']);
326 $output=array(
"Fail: Sorry dude, i do not know this type of action");
330 if(isset($output[0])){
332 $errorsList[]=
"\nAction n°".$r[
"id"].
" '".
$r[
"type"].
"' failed! With user: ".
$r[
"user"].
"\nHere is the complete output:\n".print_r($output);
335 d(
"Finishing... return value is : $return\n");
336 if(!$action->finish(
$r[
"id"],addslashes($return))){
337 $errorsList[]=
"Cannot finish the action! Error while inserting the error value in the DB for action n°".$r[
"id"].
" : action '".
$r[
"type"].
"'\nReturn value: ".addslashes($return).
"\n";
345 if( (php_sapi_name() ===
'cli') ){
346 echo _(
"errors were met");
354 unlink(ALTERNC_DO_ACTION_LOCK);
execute_cmd($command, $parameters=array())
Common routine for system calls.
d($mess)
Debug function that print infos.
my_realpath($path)
Check if a file or folder is in the list of allowed path (after dereferencing all ....
$debug
This script check the MySQL DB for actions to do, and do them one by one.
if(!defined("ALTERNC_DO_ACTION_LOCK")) $SCRIPT
mail_it()
Function to mail the panel's administrator if something failed.