Alternc  3.2
Alternc logiel libre pour l'hébergement
 All Data Structures Namespaces Files Functions Variables Pages
do_actions.php
Go to the documentation of this file.
1 #!/usr/bin/php -q
2 <?php
3 /*
4  $Id: do_actions.php,v 1.0 2013/04/19 13:40:32 axel Exp $
5  ----------------------------------------------------------------------
6  AlternC - Web Hosting System
7  Copyright (C) 2002 by the AlternC Development Team.
8  http://alternc.org/
9  ----------------------------------------------------------------------
10  Based on:
11  Valentin Lacambre's web hosting softwares: http://altern.org/
12  ----------------------------------------------------------------------
13  LICENSE
14 
15  This program is free software; you can redistribute it and/or
16  modify it under the terms of the GNU General Public License (GPL)
17  as published by the Free Software Foundation; either version 2
18  of the License, or (at your option) any later version.
19 
20  This program is distributed in the hope that it will be useful,
21  but WITHOUT ANY WARRANTY; without even the implied warranty of
22  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23  GNU General Public License for more details.
24 
25  To read the license please visit http://www.gnu.org/copyleft/gpl.html
26  ----------------------------------------------------------------------
27  Original Author of file: Axel Roger
28  Purpose of file: Do planed actions on files/directories.
29  ----------------------------------------------------------------------
30  */
31 /**
32  * This script check the MySQL DB for actions to do, and do them one by one.
33  *
34  * @copyright AlternC-Team 2002-2013 http://alternc.org/
35  */
36 
37 
38 //////////////////////////////////
39 /*
40 Fixme
41 
42  - handle error correctly
43  - check all those cases
44 
45 
46 */
47 ///////////////////////////////////
48 
49 // Put this var to 1 if you want to enable debug prints
52 
53 // Debug function that print infos
54 function d($mess){
55  global $debug;
56  if ($debug == 1)
57  echo "$mess\n";
58 }
59 
60 // Function to mail the panel's administrator if something failed
61 function mail_it(){
62  global $error_raise,$L_FQDN;
63  mail("alterncpanel@$L_FQDN",'Script do_actions.php issues',"\n Errors reporting mail:\n\n$error_raise");
64 }
65 
66 /*
67  * $command: the command
68  * $parameters: of the command (they are going to be protected)
69  * return array('output'=>'output of exec', 'return_var'=>'returned integer of exec')
70  */
71 function execute_cmd($command, $parameters=array()) {
72  $cmd_line = "$command ";
73  if (!empty($parameters)) {
74  if (is_array($parameters)) {
75  foreach($parameters as $pp) {
76  $cmd_line.= " ".escapeshellarg($pp)." ";
77  }
78  } else {
79  $cmd_line.= " ".escapeshellarg($parameters)." " ;
80  }
81  }
82  $cmd_line.= " 2>&1";
83  exec($cmd_line, $output, $code);
84  return array('executed' => $cmd_line, 'output'=>$output, 'return_var'=>$code);
85 }
86 
87 require_once("/usr/share/alternc/panel/class/config_nochk.php");
88 
89 $admin->stop_if_jobs_locked();
90 
91 $LOCK_FILE='/var/run/alternc/do_actions_cron.lock';
92 $SCRIPT='/usr/bin/php do_actions.php';
93 $MY_PID=getmypid();
94 $FIXPERM='/usr/lib/alternc/fixperms.sh';
95 
96 // Check if script isn't already running
97 if (file_exists($LOCK_FILE) !== false){
98  d("Lock file already exists. ");
99  // Check if file is in process list
100  $PID=file_get_contents($LOCK_FILE);
101  d("My PID is $MY_PID, PID in the lock file is $PID");
102  if ($PID == exec("pidof $SCRIPT | tr ' ' '\n' | grep -v $MY_PID")){
103  // Previous cron is not finished yet, just exit
104  d("Previous cron is already running, we just exit and let it finish :-)");
105  exit(0);
106  }else{
107  // Previous cron failed!
108  $error_raise.="Lock file already exists. No process with PID $PID found! Previous cron failed...\n";
109 
110  // No need to remove anything, we're going to recreate it
111  //d("Removing lock file and trying to process the failed action...");
112  // Delete the lock and continue to the next action
113  //unlink($LOCK_FILE);
114 
115  // Lock with the current script's PID
116  if (file_put_contents($LOCK_FILE,$MY_PID) === false){
117  $error_raise.="Cannot open/write $LOCK_FILE\n";
118  mail_it();
119  exit(1);
120  }
121 
122  // Get the action(s) that was processing when previous script failed
123  // (Normally, there will be at most 1 job pending... but who know?)
124  while($cc=$action->get_job()){
125  $c=$cc[0];
126  $params=unserialize($c["parameters"]);
127  // We can resume these types of action, so we reset the job to process it later
128  d("Previous job was the n°".$c["id"]." : '".$c["type"]."'");
129  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"){
130  d("Reset of the job! So it will be resumed...");
131  $action->reset_job($c["id"]);
132  }else{
133  // We can't resume the others types, notify the fail and finish this action
134  $error_raise.="Can't resume the job n°".$c["id"]." action '".$c["type"]."', finishing it with a fail status.\n";
135  if(!$action->finish($c["id"],"Fail: Previous script crashed while processing this action, cannot resume it.")){
136  $error_raise.="Cannot finish the action! Error while inserting the error value in the DB for action n°".$c["id"]." : action '".$c["type"]."'\n";
137  break; // Else we go into an infinite loop... AAAAHHHHHH
138  }
139  }
140  }
141  }
142 }else{
143  // Lock with the current script's PID
144  if (file_put_contents($LOCK_FILE,$MY_PID) === false){
145  $error_raise.="Cannot open/write $LOCK_FILE\n";
146  mail_it();
147  exit(1);
148  }
149 }
150 
151 //We get the next action to do
152 while ($rr=$action->get_action()){
153  $r=$rr[0];
154  $return="OK";
155  // Do we have to do this action with a specific user?
156  if($r["user"] != "root")
157  $SU="su ".$r["user"]." 2>&1 ;";
158  else
159  $SU="";
160  unset($output);
161  // We lock the action
162  d("-----------\nBeginning action n°".$r["id"]);
163  $action->begin($r["id"]);
164  // We process it
165  $params=@unserialize($r["parameters"]);
166  // We exec with the specified user
167  d("Executing action '".$r["type"]."' with user '".$r["user"]."'");
168  switch ($r["type"]){
169  case "FIX_USER" :
170  // Create the directory and make parent directories as needed
171  #@exec("$FIXPERM -u ".$params["uid"]." 2>&1", $trash, $code);
172  $returned = execute_cmd("$FIXPERM -u", $params["uid"]);
173  break;
174  case "CREATE_FILE" :
175  if(!file_exists($params["file"])) {
176  #@exec("$SU touch ".$params["file"]." 2>&1 ; echo '".$params["content"]."' > '".$params["file"]."' 2>&1", $output);
177  if ( file_put_contents($params["file"], $params["content"]) === false ) {
178  $log_error=array("Fail: can't write into file ".$params["file"]);
179  } else {
180  if (!chown($params["file"], $r["user"])) {
181  $log_error=array("Fail: cannot chown ".$params["file"]);
182  }
183  }
184  } else {
185  $log_error=array("Fail: file already exists ".$params["file"]);
186  }
187  break;
188  case "CREATE_DIR" :
189  // Create the directory and make parent directories as needed
190  #@exec("$SU mkdir -p ".$params["dir"]." 2>&1",$output);
191  $returned = execute_cmd("$SU mkdir", array('-p', $params["dir"]));
192  break;
193  case "DELETE" :
194  // Delete file/directory and its contents recursively
195  #@exec("$SU rm -rf ".$params["dir"]." 2>&1", $output);
196  $returned = execute_cmd("$SU rm", array('-rf', $params["dir"]));
197  break;
198  case "MOVE" :
199  // If destination dir does not exists, create it
200  if(!is_dir($params["dst"]))
201  if ( @mkdir($params["dst"], 0777, true)) {
202  if ( @chown($params["dst"], $r["user"]) ) {
203  $returned = execute_cmd("$SU mv -f", array($params["src"], $params["dst"]));
204  }
205  } else { //is_dir false
206  $log_error=array("Fail: cannot create ".$params["dst"]);
207  } // is_dir
208 
209  break;
210  case "FIX_DIR" :
211  $returned = execute_cmd($FIXPERM, array('-d', $params["dir"]));
212  if($returned['return_val'] != 0) $log_error=array("Fixperms.sh failed, returned error code : ".$returned['return_val']);
213  break;
214  case "FIX_FILE" :
215  #@exec("$FIXPERM -f ".$params["file"]." 2>&1", $trash, $code);
216  $returned = execute_cmd($FIXPERM, array('-f', $params["file"]));
217  if($returned['return_val'] != 0) $log_error=array("Fixperms.sh failed, returned error code : ".$returned['return_val']);
218  break;
219  default :
220  $output=array("Fail: Sorry dude, i do not know this type of action");
221  break;
222  }
223  // Get the error (if exists).
224  if(isset($output[0])){
225  $return=$output[0];
226  $error_raise.="\nAction n°".$r["id"]." '".$r["type"]."' failed! With user: ".$r["user"]."\nHere is the complete output:\n".print_r($output);
227  }
228  // We finished the action, notify the DB.
229  d("Finishing... return value is : $return\n");
230  if(!$action->finish($r["id"],addslashes($return))){
231  $error_raise.="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";
232  break; // Else we go into an infinite loop... AAAAHHHHHH
233  }
234 }
235 
236 // If something have failed, notify it to the admin
237 if($error_raise !== '') {
238  mail_it();
239 }
240 
241 // Unlock the script
242 unlink($LOCK_FILE);
243 
244 // Exit this script
245 exit(0);
246 ?>