Alternc  latest
Alternc logiel libre pour l'hébergement
do_actions.php File Reference

Go to the source code of this file.

Functions

 d ($mess)
 Debug function that print infos. More...
 
 mail_it ()
 Function to mail the panel's administrator if something failed. More...
 
 execute_cmd ($command, $parameters=array())
 Common routine for system calls. More...
 
 my_realpath ($path)
 Check if a file or folder is in the list of allowed path (after dereferencing all ../ and symlinks. More...
 

Variables

 $debug =0
 This script check the MySQL DB for actions to do, and do them one by one. More...
 
 $errorsList =array()
 
if(!defined("ALTERNC_DO_ACTION_LOCK")) $SCRIPT ='/usr/bin/php do_actions.php'
 
 $MY_PID =getmypid()
 
 $FIXPERM ='/usr/lib/alternc/fixperms.sh'
 
if(file_exists(ALTERNC_DO_ACTION_LOCK) !==falseelse
 

Function Documentation

◆ d()

d (   $mess)

Debug function that print infos.

Parameters
globalint $debug
type$mess

Definition at line 75 of file do_actions.php.

75  {
76  global $debug;
77  if ($debug == 1)
78  echo "$mess\n";
79 }
$debug
This script check the MySQL DB for actions to do, and do them one by one.
Definition: do_actions.php:48

References $debug.

◆ execute_cmd()

execute_cmd (   $command,
  $parameters = array() 
)

Common routine for system calls.

Parameters
type$commandthe command
type$parametersof the command (they are going to be protected)
Returns
array('output'=>'output of exec', 'return_val'=>'returned integer of exec')

Definition at line 106 of file do_actions.php.

106  {
107  $cmd_line = "$command ";
108  if (!empty($parameters)) {
109  if (is_array($parameters)) {
110  foreach($parameters as $pp) {
111  $cmd_line.= " ".escapeshellarg($pp)." ";
112  }
113  } else {
114  $cmd_line.= " ".escapeshellarg($parameters)." " ;
115  }
116  }
117  $cmd_line.= " 2>&1";
118  exec($cmd_line, $output, $code);
119  return array('executed' => $cmd_line, 'output'=>$output, 'return_val'=>$code);
120 }

◆ mail_it()

mail_it ( )

Function to mail the panel's administrator if something failed.

Parameters
globalarray $errorsList
globaltype $L_FQDN

Definition at line 86 of file do_actions.php.

86  {
87  global $errorsList,$L_FQDN;
88  // Forces array
89  if( !is_array($errorsList)){
90  $errorsList = array($errorsList);
91  }
92  // Builds message from array
93  $msg = implode("\n", $errorsList);
94  // Attempts to send email
95  // @todo log if fails
96  mail("alterncpanel@$L_FQDN",'Script do_actions.php issues',"\n Errors reporting mail:\n\n$msg");
97 }
$msg
Definition: bootstrap.php:75
$errorsList
Definition: do_actions.php:51

References $errorsList, and $msg.

◆ my_realpath()

my_realpath (   $path)

Check if a file or folder is in the list of allowed path (after dereferencing all ../ and symlinks.

Parameters
$pathstring the path to check against
Returns
string the dereferenced path, or FALSE if the path is NOT allowed (/var/www/alternc /var/mail/alternc)

Definition at line 127 of file do_actions.php.

127  {
128  global $L_ALTERNC_HTML, $L_ALTERNC_MAIL;
129  // add here any allowed path:
130  $allowed=array(realpath($L_ALTERNC_HTML)."/", realpath($L_ALTERNC_MAIL)."/");
131  $path=realpath($path);
132  foreach($allowed as $one) {
133  // the path must be BELOW each allowed folder. forbid anything
134  if (strlen($path)>strlen($one) && substr($path,0,strlen($one))==$one) {
135  return $path;
136  }
137  }
138  return false;
139 }
140 
141 // Check if script isn't already running
142 if (file_exists(ALTERNC_DO_ACTION_LOCK) !== false){
143  d("Lock file already exists. ");
144  // Check if file is in process list
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")){
148  // Previous cron is not finished yet, just exit
149  d("Previous cron is already running, we just exit and let it finish :-)");
150  exit(0);
151  }else{
152  // Previous cron failed!
153  $errorsList[]="Lock file already exists. No process with PID $PID found! Previous cron failed...\n";
154 
155  // No need to remove anything, we're going to recreate it
156  //d("Removing lock file and trying to process the failed action...");
157  // Delete the lock and continue to the next action
158  //unlink(ALTERNC_DO_ACTION_LOCK);
159 
160  // Lock with the current script's PID
161  if (file_put_contents(ALTERNC_DO_ACTION_LOCK,$MY_PID) === false){
162  $errorsList[]="Cannot open/write ALTERNC_DO_ACTION_LOCK\n";
163  mail_it();
164  exit(1);
165  }
166 
167  // Get the action(s) that was processing when previous script failed
168  // (Normally, there will be at most 1 job pending... but who know?)
169  while($cc=$action->get_job()){
170  $c=$cc[0];
171  $params=unserialize($c["parameters"]);
172  // We can resume these types of action, so we reset the job to process it later
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"]);
177  }else{
178  // We can't resume the others types, notify the fail and finish this action
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";
182  break; // Else we go into an infinite loop... AAAAHHHHHH
183  }
184  }
185  }
186  }
187 }else{
exit
Definition: adm_doadd.php:70
$c
Definition: bootstrap.php:47
$MY_PID
Definition: do_actions.php:64
d($mess)
Debug function that print infos.
Definition: do_actions.php:75
mail_it()
Function to mail the panel's administrator if something failed.
Definition: do_actions.php:86

Variable Documentation

◆ $debug

$debug =0

This script check the MySQL DB for actions to do, and do them one by one.

Definition at line 48 of file do_actions.php.

Referenced by d().

◆ $errorsList

$errorsList =array()

Definition at line 51 of file do_actions.php.

Referenced by mail_it().

◆ $FIXPERM

$FIXPERM ='/usr/lib/alternc/fixperms.sh'

Definition at line 65 of file do_actions.php.

◆ $MY_PID

$MY_PID =getmypid()

Definition at line 64 of file do_actions.php.

◆ $SCRIPT

if (!defined("ALTERNC_DO_ACTION_LOCK")) $SCRIPT ='/usr/bin/php do_actions.php'

Definition at line 63 of file do_actions.php.

◆ else

if (file_exists(ALTERNC_DO_ACTION_LOCK) !==false) else

Definition at line 187 of file do_actions.php.