diff --git a/bureau/class/m_action.php b/bureau/class/m_action.php index a1bec46d..cbbba4cd 100644 --- a/bureau/class/m_action.php +++ b/bureau/class/m_action.php @@ -69,7 +69,19 @@ class m_action { } /** - * Plans the cration of a dir + * Plans the a chmod on file or dir + * + * @param string $filename + * @param int $perms + * @param string $user + * @return boolean + */ + function chmod($filename, $perms, $user = "root") { + return $this->set('chmod', $user, array('filename' => $filename,"perms"=>$perms)); + } + + /** + * Plans the creation of a dir * * @param string $dir * @param int $user @@ -186,6 +198,9 @@ class m_action { $serialized = serialize($parameters); switch ($type) { + case 'chmod': + $query = "insert into actions values ('','CHMOD','$serialized',now(),'','','$user','');"; + break; case 'create_file': $query = "insert into actions values ('','CREATE_FILE','$serialized',now(),'','','$user','');"; break; diff --git a/bureau/class/m_bro.php b/bureau/class/m_bro.php index 57820ca6..4b5246ac 100644 --- a/bureau/class/m_bro.php +++ b/bureau/class/m_bro.php @@ -546,6 +546,7 @@ class m_bro { * Change les droits d'acces aux fichier de $d du dossier $R en $p * * @global m_err $err + * @global m_action $action * @param string $R Dossier dans lequel se trouve les fichiers renommer. * @param boolean $verbose Shall we 'echo' what we did ? * @return boolean TRUE Si les fichiers ont t renomms, FALSE si une erreur s'est produite. @@ -569,7 +570,7 @@ class m_bro { } else { $m=$m ^ 0222; // ugo-w } - chmod($absolute."/".$d[$i], $m); + $action->chmod($absolute."/".$d[$i], $m); if ($verbose) { echo "chmod ".sprintf('%o', $m)." file, was ".sprintf('%o', fileperms($absolute."/". $d[$i])). " -- ".$perm[$i]['w']; } diff --git a/phpunit/tests/bureau/class/m_actionTest.php b/phpunit/tests/bureau/class/m_actionTest.php index 1de07cdf..6aebe7d1 100644 --- a/phpunit/tests/bureau/class/m_actionTest.php +++ b/phpunit/tests/bureau/class/m_actionTest.php @@ -69,6 +69,15 @@ class m_actionTest extends AlterncTest { $this->assertFileExists($L_INOTIFY_DO_ACTION); } + /** + * @covers m_action::chmod + */ + public function testChmod() { + $result = $this->object->chmod(self::TEST_FILE, 0777,"phpunit"); + $this->assertEquals(1, $this->getConnection()->getRowCount('actions')); + + } + /** * @covers m_action::create_file */ diff --git a/src/do_actions.php b/src/do_actions.php index bd2a3d93..dc01d302 100644 --- a/src/do_actions.php +++ b/src/do_actions.php @@ -39,34 +39,69 @@ /* Fixme - - handle error correctly - check all those cases - */ /////////////////////////////////// // Put this var to 1 if you want to enable debug prints -$debug=1; -$error_raise=''; +$debug=0; -// Debug function that print infos +// Collects errors along execution. If length > 1, an email is sent. +$errorsList=array(); + +// Bootstraps +require_once("/usr/share/alternc/panel/class/config_nochk.php"); + +// Script lock through filesystem +$admin->stop_if_jobs_locked(); + +if( !defined("ALTERNC_DO_ACTION_LOCK")){ + define("ALTERNC_DO_ACTION_LOCK",'/var/run/alternc/do_actions_cron.lock'); +} + +$SCRIPT='/usr/bin/php do_actions.php'; +$MY_PID=getmypid(); +$FIXPERM='/usr/lib/alternc/fixperms.sh'; + + +/** + * + * Debug function that print infos + * + * @global int $debug + * @param type $mess + */ function d($mess){ global $debug; if ($debug == 1) echo "$mess\n"; } -// Function to mail the panel's administrator if something failed +/** + * Function to mail the panel's administrator if something failed + * @global array $errorsList + * @global type $L_FQDN + */ function mail_it(){ - global $error_raise,$L_FQDN; - mail("alterncpanel@$L_FQDN",'Script do_actions.php issues',"\n Errors reporting mail:\n\n$error_raise"); + global $errorsList,$L_FQDN; + // Forces array + if( !is_array($errorsList)){ + $errorsList = array($errorsList); + } + // Builds message from array + $msg = implode("\n", $errorsList); + // Attempts to send email + // @todo log if fails + mail("alterncpanel@$L_FQDN",'Script do_actions.php issues',"\n Errors reporting mail:\n\n$msg"); } -/* - * $command: the command - * $parameters: of the command (they are going to be protected) - * return array('output'=>'output of exec', 'return_var'=>'returned integer of exec') +/** + * Common routine for system calls + * + * @param type $command the command + * @param type $parameters of the command (they are going to be protected) + * @return array('output'=>'output of exec', 'return_var'=>'returned integer of exec') */ function execute_cmd($command, $parameters=array()) { $cmd_line = "$command "; @@ -84,20 +119,12 @@ function execute_cmd($command, $parameters=array()) { return array('executed' => $cmd_line, 'output'=>$output, 'return_var'=>$code); } -require_once("/usr/share/alternc/panel/class/config_nochk.php"); - -$admin->stop_if_jobs_locked(); - -$LOCK_FILE='/var/run/alternc/do_actions_cron.lock'; -$SCRIPT='/usr/bin/php do_actions.php'; -$MY_PID=getmypid(); -$FIXPERM='/usr/lib/alternc/fixperms.sh'; // Check if script isn't already running -if (file_exists($LOCK_FILE) !== false){ +if (file_exists(ALTERNC_DO_ACTION_LOCK) !== false){ d("Lock file already exists. "); // Check if file is in process list - $PID=file_get_contents($LOCK_FILE); + $PID=file_get_contents(ALTERNC_DO_ACTION_LOCK); d("My PID is $MY_PID, PID in the lock file is $PID"); if ($PID == exec("pidof $SCRIPT | tr ' ' '\n' | grep -v $MY_PID")){ // Previous cron is not finished yet, just exit @@ -105,16 +132,16 @@ if (file_exists($LOCK_FILE) !== false){ exit(0); }else{ // Previous cron failed! - $error_raise.="Lock file already exists. No process with PID $PID found! Previous cron failed...\n"; + $errorsList[]="Lock file already exists. No process with PID $PID found! Previous cron failed...\n"; // No need to remove anything, we're going to recreate it //d("Removing lock file and trying to process the failed action..."); // Delete the lock and continue to the next action - //unlink($LOCK_FILE); + //unlink(ALTERNC_DO_ACTION_LOCK); // Lock with the current script's PID - if (file_put_contents($LOCK_FILE,$MY_PID) === false){ - $error_raise.="Cannot open/write $LOCK_FILE\n"; + if (file_put_contents(ALTERNC_DO_ACTION_LOCK,$MY_PID) === false){ + $errorsList[]="Cannot open/write ALTERNC_DO_ACTION_LOCK\n"; mail_it(); exit(1); } @@ -131,9 +158,9 @@ if (file_exists($LOCK_FILE) !== false){ $action->reset_job($c["id"]); }else{ // We can't resume the others types, notify the fail and finish this action - $error_raise.="Can't resume the job n°".$c["id"]." action '".$c["type"]."', finishing it with a fail status.\n"; + $errorsList[]="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.")){ - $error_raise.="Cannot finish the action! Error while inserting the error value in the DB for action n°".$c["id"]." : action '".$c["type"]."'\n"; + $errorsList[]="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 } } @@ -141,8 +168,8 @@ if (file_exists($LOCK_FILE) !== false){ } }else{ // Lock with the current script's PID - if (file_put_contents($LOCK_FILE,$MY_PID) === false){ - $error_raise.="Cannot open/write $LOCK_FILE\n"; + if (file_put_contents(ALTERNC_DO_ACTION_LOCK,$MY_PID) === false){ + $errorsList[]="Cannot open/write ALTERNC_DO_ACTION_LOCK\n"; mail_it(); exit(1); } @@ -157,7 +184,6 @@ while ($rr=$action->get_action()){ $SU="su ".$r["user"]." 2>&1 ;"; else $SU=""; - unset($output); // We lock the action d("-----------\nBeginning action n°".$r["id"]); $action->begin($r["id"]); @@ -170,19 +196,36 @@ while ($rr=$action->get_action()){ // Create the directory and make parent directories as needed #@exec("$FIXPERM -u ".$params["uid"]." 2>&1", $trash, $code); $returned = execute_cmd("$FIXPERM -u", $params["uid"]); + break; + case "CHMOD" : + $filename=$params["file"]; + $perms=$params["perms"]; + // Checks the file or directory exists + if( !is_dir($filename) && ! is_file($filename)){ + $errorsList=array("Fail: cannot create ".$params["dst"]); + } + // Checks the perms are correct + else if ( !is_int( $perms)){ + $errorsList=array("Fail: cannot create ".$params["dst"]); + } + // Attempts to change the rights on the file or directory + else if( !chmod($filename, $perms)) { + $errorsList=array("Fail: cannot create ".$params["dst"]); + } + break; case "CREATE_FILE" : if(!file_exists($params["file"])) { #@exec("$SU touch ".$params["file"]." 2>&1 ; echo '".$params["content"]."' > '".$params["file"]."' 2>&1", $output); if ( file_put_contents($params["file"], $params["content"]) === false ) { - $log_error=array("Fail: can't write into file ".$params["file"]); + $errorsList=array("Fail: can't write into file ".$params["file"]); } else { if (!chown($params["file"], $r["user"])) { - $log_error=array("Fail: cannot chown ".$params["file"]); + $errorsList=array("Fail: cannot chown ".$params["file"]); } } } else { - $log_error=array("Fail: file already exists ".$params["file"]); + $errorsList=array("Fail: file already exists ".$params["file"]); } break; case "CREATE_DIR" : @@ -203,18 +246,22 @@ while ($rr=$action->get_action()){ $returned = execute_cmd("$SU mv -f", array($params["src"], $params["dst"])); } } else { //is_dir false - $log_error=array("Fail: cannot create ".$params["dst"]); + $errorsList=array("Fail: cannot create ".$params["dst"]); } // is_dir break; case "FIX_DIR" : $returned = execute_cmd($FIXPERM, array('-d', $params["dir"])); - if($returned['return_val'] != 0) $log_error=array("Fixperms.sh failed, returned error code : ".$returned['return_val']); + if($returned['return_val'] != 0) { + $errorsList=array("Fixperms.sh failed, returned error code : ".$returned['return_val']); + } break; case "FIX_FILE" : #@exec("$FIXPERM -f ".$params["file"]." 2>&1", $trash, $code); $returned = execute_cmd($FIXPERM, array('-f', $params["file"])); - if($returned['return_val'] != 0) $log_error=array("Fixperms.sh failed, returned error code : ".$returned['return_val']); + if($returned['return_val'] != 0){ + $errorsList=array("Fixperms.sh failed, returned error code : ".$returned['return_val']); + } break; default : $output=array("Fail: Sorry dude, i do not know this type of action"); @@ -223,24 +270,24 @@ while ($rr=$action->get_action()){ // Get the error (if exists). if(isset($output[0])){ $return=$output[0]; - $error_raise.="\nAction n°".$r["id"]." '".$r["type"]."' failed! With user: ".$r["user"]."\nHere is the complete output:\n".print_r($output); + $errorsList[]="\nAction n°".$r["id"]." '".$r["type"]."' failed! With user: ".$r["user"]."\nHere is the complete output:\n".print_r($output); } // We finished the action, notify the DB. d("Finishing... return value is : $return\n"); if(!$action->finish($r["id"],addslashes($return))){ - $error_raise.="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"; + $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"; break; // Else we go into an infinite loop... AAAAHHHHHH } } -// If something have failed, notify it to the admin -if($error_raise !== '') { +// If an error occured, notify it to the admin +if(count($errorsList)) { mail_it(); } // Unlock the script -unlink($LOCK_FILE); +// @todo This could be handled by m_admin +unlink(ALTERNC_DO_ACTION_LOCK); // Exit this script -exit(0); -?> +exit(0); \ No newline at end of file