[fix] Documents and tweaks m_action

This commit is contained in:
alban 2014-03-23 15:15:33 +01:00
parent ae490904ea
commit 6dd6094b71
1 changed files with 349 additions and 239 deletions

View File

@ -1,4 +1,5 @@
<?php <?php
/* /*
LICENSE LICENSE
@ -17,235 +18,344 @@
Original Author of file: Lerider Steven Original Author of file: Lerider Steven
Purpose of file: Manage generic actions. Purpose of file: Manage generic actions.
---------------------------------------------------------------------- ----------------------------------------------------------------------
*/ */
/** /**
* This class manage actions to be performed on the file system on behalf of alternc Classes * This class manage actions to be performed on the file system on behalf of alternc Classes
* It primary use is to store the actions to be performed ( creating file or folder, deleting, setting permissions etc..) in the action sql table. * It primary use is to store the actions to be performed ( creating file or folder, deleting, setting permissions etc..) in the action sql table.
* The script /usr/lib/alternc/do_actions.php handled by cron and incron is then used to perform those actions. * The script /usr/lib/alternc/do_actions.php handled by cron and incron is then used to perform those actions.
* *
* Copyleft {@link http://alternc.org/ AlternC Team} * Copyleft {@link http://alternc.org/ AlternC Team}
* *
* @copyright AlternC-Team 2013-8-13 http://alternc.org/ * @copyright AlternC-Team 2013-8-13 http://alternc.org/
* *
*/ */
class m_action { class m_action {
/*---------------------------------------------------------------------------*/ /* --------------------------------------------------------------------------- */
/** Constructor /** Constructor
*/ */
function m_action() { function m_action() {
} }
/* /**
* function to set the cration of a file * Plans the cration of a file
*
* @global type $err
* @global type $L_INOTIFY_DO_ACTION
* @return boolean
*/ */
function do_action(){ function do_action() {
global $err, $L_INOTIFY_DO_ACTION; global $err, $L_INOTIFY_DO_ACTION;
$err->log("action","do_action"); $err->log("action", "do_action");
touch($L_INOTIFY_DO_ACTION); if( ! touch($L_INOTIFY_DO_ACTION) ){
return FALSE;
}
return TRUE;
} }
function create_file($file,$content="",$user="root") { /**
return $this->set('create_file',$user, array('file'=>$file,'content'=>$content)); * Plans a file creation
} *
* @param string $file
/* * @param string $content
* function to set the cration of a file * @param int $user
* @return boolean
*/ */
function create_dir($dir,$user="root") { function create_file($file, $content = "", $user = "root") {
return $this->set('create_dir',$user, array('dir'=>$dir)); return $this->set('create_file', $user, array('file' => $file, 'content' => $content));
} }
/* /**
* function to set the perms upon user creation * Plans the cration of a dir
*
* @param string $dir
* @param int $user
* @return boolean
*/ */
function fix_user($uid,$user="root") { function create_dir($dir, $user = "root") {
return $this->set('fix_user',$user, array('uid'=>$uid)); return $this->set('create_dir', $user, array('dir' => $dir));
}
/*
* function to set the cration of a file
*/
function fix_dir($dir,$user="root") {
return $this->set('fix_dir',$user, array('dir'=>$dir));
} }
/* /**
* function to set the cration of a file * Plans a perms fix upon user creation
* @param int $uid
* @param string $user
* @return boolean
*/ */
function fix_file($file,$user="root") { function fix_user($uid, $user = "root") {
return $this->set('fix_file',$user, array('file'=>$file)); return $this->set('fix_user', $user, array('uid' => $uid));
} }
/* /**
* Plans a dir fix
*
* @param type $dir
* @param type $user
* @return type
*/
function fix_dir($dir, $user = "root") {
return $this->set('fix_dir', $user, array('dir' => $dir));
}
/**
* Plans a file fix
*
* @param type $file
* @param type $user
* @return type
*/
function fix_file($file, $user = "root") {
return $this->set('fix_file', $user, array('file' => $file));
}
/**
* function to delete file / folder * function to delete file / folder
*
* @param type $dir
* @param type $user
* @return type
*/ */
function del($dir,$user="root") { function del($dir, $user = "root") {
return $this->set('delete',$user, array('dir'=>$dir)); return $this->set('delete', $user, array('dir' => $dir));
} }
/* /**
* function returning the first not locked line of the action table * function returning the first not locked line of the action table
*
* @param type $src
* @param type $dst
* @param type $user
* @return type
*/ */
function move($src,$dst,$user="root") { function move($src, $dst, $user = "root") {
return $this->set('move',$user, array('src'=>$src, 'dst'=>$dst)); return $this->set('move', $user, array('src' => $src, 'dst' => $dst));
} }
/* /**
*
* function archiving a directory ( upon account deletion ) * function archiving a directory ( upon account deletion )
* @param: $archive : directory to archive within the archive_del_data folder if set in variable sql table. *
* If archive_del_data is not set we delete the folder. * @global type $cuid
* @param: $dir : sub_directory of the archive directory * @global type $db
* @global type $err
* @param type $archive Directory to archive within the archive_del_data folder if set in variable sql table
* If archive_del_data is not set we delete the folder
* @param type $dir sub_directory of the archive directory
* @return boolean
*/ */
function archive($archive,$dir="html") { function archive($archive, $dir = "html") {
global $cuid,$db,$err; global $cuid, $db, $err;
$arch=variable_get('archive_del_data'); $arch = variable_get('archive_del_data');
if(empty($arch)) { if (empty($arch)) {
$this->del($archive); $this->del($archive);
return true; return true;
} }
$BACKUP_DIR=$arch; $BACKUP_DIR = $arch;
$db->query("select login from membres where uid=$cuid;"); $db->query("select login from membres where uid=$cuid;");
$db->next_record(); $db->next_record();
if (!$db->Record["login"]) { if (!$db->Record["login"]) {
$err->raise("action",_("Login corresponding to $cuid not found")); $err->raise("action", _("Login corresponding to $cuid not found"));
return false; return false;
} }
$uidlogin=$cuid."-".$db->Record["login"]; $uidlogin = $cuid . "-" . $db->Record["login"];
//The path will look like /<archive_del_data>/YYYY-MM/<uid>-<login>/<folder> //The path will look like /<archive_del_data>/YYYY-MM/<uid>-<login>/<folder>
$today=getdate(); $today = getdate();
$dest=$BACKUP_DIR.'/'.$today["year"].'-'.$today["mon"].'/'.$uidlogin.'/'.$dir; $dest = $BACKUP_DIR . '/' . $today["year"] . '-' . $today["mon"] . '/' . $uidlogin . '/' . $dir;
$this->move($archive,$dest); $this->move($archive, $dest);
return true; return true;
} }
/* /**
*function inserting the action in the sql table * function inserting the action in the sql table
*
* @global type $db
* @global type $err
* @param type $type
* @param type $user
* @param type $parameters
* @return boolean
*/ */
function set($type,$user,$parameters) { function set($type, $user, $parameters) {
global $db,$err; global $db, $err;
$err->log("action","set",$type); $err->log("action", "set", $type);
$serialized=serialize($parameters); $serialized = serialize($parameters);
switch($type){ switch ($type) {
case 'create_file': case 'create_file':
$query="insert into actions values ('','CREATE_FILE','$serialized',now(),'','','$user','');"; $query = "insert into actions values ('','CREATE_FILE','$serialized',now(),'','','$user','');";
break; break;
case 'create_dir': case 'create_dir':
$query="insert into actions values ('','CREATE_DIR','$serialized',now(),'','','$user','');"; $query = "insert into actions values ('','CREATE_DIR','$serialized',now(),'','','$user','');";
break; break;
case 'move': case 'move':
$query="insert into actions values ('','MOVE','$serialized',now(),'','','$user','');"; $query = "insert into actions values ('','MOVE','$serialized',now(),'','','$user','');";
break; break;
case 'fix_user': case 'fix_user':
$query="insert into actions values ('','FIX_USER','$serialized',now(),'','','$user','');"; $query = "insert into actions values ('','FIX_USER','$serialized',now(),'','','$user','');";
break; break;
case 'fix_file': case 'fix_file':
$query="insert into actions values ('','FIX_FILE','$serialized',now(),'','','$user','');"; $query = "insert into actions values ('','FIX_FILE','$serialized',now(),'','','$user','');";
break; break;
case 'fix_dir': case 'fix_dir':
$query="insert into actions values ('','FIX_DIR','$serialized',now(),'','','$user','');"; $query = "insert into actions values ('','FIX_DIR','$serialized',now(),'','','$user','');";
break; break;
case 'delete': case 'delete':
$query="insert into actions values ('','DELETE','$serialized',now(),'','','$user','');"; $query = "insert into actions values ('','DELETE','$serialized',now(),'','','$user','');";
break; break;
default: default:
return false; return false;
} }
if(!$db->query($query)){ if (!$db->query($query)) {
$err->raise("action",_("Error setting actions")); $err->raise("action", _("Error setting actions"));
return false; return false;
} }
$this->do_action(); $this->do_action();
return true; return true;
} }
function get_old($all= null){ /**
global $err,$db; * This seems to be unused ?
*
* @global type $err
* @global type $db
* @param type $all
* @return boolean
*/
function get_old($all = null) {
global $err, $db;
$purge="select * from actions where TO_DAYS(curdate()) - TO_DAYS(creation) > 2;"; $purge = "select * from actions where TO_DAYS(curdate()) - TO_DAYS(creation) > 2;";
if(!$db->query($purge)){ $result = $db->query($purge);
$err->raise("action",_("Error selecting old actions")); if (! $result) {
$err->raise("action", _("Error selecting old actions"));
return false; return false;
} }
return $db->num_rows($result) ;
} }
function purge($all=null){ /**
global $err,$db; *
if(is_null($all)){ * @global type $err
$purge="delete from actions where TO_DAYS(curdate()) - TO_DAYS(creation) > 2 and status = 0;"; * @global type $db
}else{ * @param type $all
$purge="delete from actions where TO_DAYS(curdate()) - TO_DAYS(creation) > 2;"; * @return boolean
*/
function purge($all = null) {
global $err, $db;
if (is_null($all)) {
$purge = "delete from actions where TO_DAYS(curdate()) - TO_DAYS(creation) > 2 and status = 0;";
} else {
$purge = "delete from actions where TO_DAYS(curdate()) - TO_DAYS(creation) > 2;";
} }
if(!$db->query($purge)){ $result = $db->query($purge);
$err->raise("action",_("Error purging old actions")); if (! $result) {
$err->raise("action", _("Error purging old actions"));
return false; return false;
} }
return $db->num_rows($result) ;
} }
/* /**
* function returning the first not locked line of the action table * function returning the first not locked line of the action table
*
* @global type $db
* @global type $err
* @return boolean or array
*/ */
function get_action() { function get_action() {
global $db,$err; global $db, $err;
$tab=array(); $tab = array();
$db->query('select * from actions where end =0 and begin = 0 order by id limit 1;'); $db->query('select * from actions where end = 0 and begin = 0 order by id limit 1;');
if ($db->next_record()){ if ($db->next_record()) {
$tab[]=$db->Record; $tab[] = $db->Record;
return $tab; return $tab;
}else{ } else {
return false; return false;
} }
} }
/* /**
* function locking an entry while it is being executed by the action script * function locking an entry while it is being executed by the action script
*
* @global type $db
* @global type $err
* @param type $id
* @return boolean
*/ */
function begin($id) { function begin($id) {
global $db,$err; global $db, $err;
if(!$db->query("update actions set begin=now() where id=$id ;")){ if (!$db->query("update actions set begin=now() where id=$id ;")) {
$err->raise("action",_("Error locking the action : $id")); $err->raise("action", _("Error locking the action : $id"));
return false; return false;
} }
return true; return true;
} }
/* /**
* function locking an entry while it is being executed by the action script * function locking an entry while it is being executed by the action script
*
* @global type $db
* @global type $err
* @param type $id
* @param type $return
* @return boolean
*/ */
function finish($id,$return=0) { function finish($id, $return = 0) {
global $db,$err; global $db, $err;
if(!$db->query("update actions set end=now(),status='$return' where id=$id ;")){ if (!$db->query("update actions set end=now(),status='$return' where id=$id ;")) {
$err->raise("action",_("Error unlocking the action : $id")); $err->raise("action", _("Error unlocking the action : $id"));
return false; return false;
} }
return true; return true;
} }
/**
*
* @global type $db
* @global type $err
* @param type $id
* @return boolean
*/
function reset_job($id) { function reset_job($id) {
global $db,$err; global $db, $err;
if(!$db->query("update actions set end=0,begin=0,status='' where id=$id ;")){ if (!$db->query("update actions set end=0,begin=0,status='' where id=$id ;")) {
$err->raise("action",_("Error unlocking the action : $id")); $err->raise("action", _("Error unlocking the action : $id"));
return false; return false;
} }
return true; return true;
} }
/**
* Returns a list of actions marked as executable and ready for execution
*
* @global type $db
* @global type $err
* @return boolean
*/
function get_job() { function get_job() {
global $db,$err; global $db, $err;
$tab=array(); $tab = array();
$db->query("Select * from actions where begin !=0 and end = 0 ;"); $db->query("Select * from actions where begin !=0 and end = 0 ;");
if ($db->next_record()){ if ($db->next_record()) {
$tab[]=$db->Record; $tab[] = $db->Record;
return $tab; return $tab;
}else{ } else {
return false; return false;
} }
} }
/* /**
* function locking an entry while it is being executed by the action script * function locking an entry while it is being executed by the action script
*
* @global type $db
* @param type $id
* @return boolean
*/ */
function cancel($id) { function cancel($id) {
global $db; global $db;
@ -253,6 +363,6 @@ class m_action {
return true; return true;
} }
} /* Class action */ }
?> /* Class action */