[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,258 +1,368 @@
<?php <?php
/* /*
LICENSE LICENSE
This program is free software; you can redistribute it and/or This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License (GPL) modify it under the terms of the GNU General Public License (GPL)
as published by the Free Software Foundation; either version 2 as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version. of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
To read the license please visit http://www.gnu.org/copyleft/gpl.html
----------------------------------------------------------------------
Original Author of file: Lerider Steven
Purpose of file: Manage generic actions.
----------------------------------------------------------------------
*/
To read the license please visit http://www.gnu.org/copyleft/gpl.html
----------------------------------------------------------------------
Original Author of file: Lerider Steven
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
*/
function m_action() {
}
/* /** Constructor
* function to set the cration of a file */
*/ function m_action() {
function do_action(){
global $err, $L_INOTIFY_DO_ACTION;
$err->log("action","do_action");
touch($L_INOTIFY_DO_ACTION);
}
function create_file($file,$content="",$user="root") {
return $this->set('create_file',$user, array('file'=>$file,'content'=>$content));
}
/*
* function to set the cration of a file
*/
function create_dir($dir,$user="root") {
return $this->set('create_dir',$user, array('dir'=>$dir));
}
/*
* function to set the perms upon user creation
*/
function fix_user($uid,$user="root") {
return $this->set('fix_user',$user, array('uid'=>$uid));
}
/*
* 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
*/
function fix_file($file,$user="root") {
return $this->set('fix_file',$user, array('file'=>$file));
}
/*
* function to delete file / folder
*/
function del($dir,$user="root") {
return $this->set('delete',$user, array('dir'=>$dir));
}
/*
* function returning the first not locked line of the action table
*/
function move($src,$dst,$user="root") {
return $this->set('move',$user, array('src'=>$src, 'dst'=>$dst));
}
/*
* 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.
* @param: $dir : sub_directory of the archive directory
*/
function archive($archive,$dir="html") {
global $cuid,$db,$err;
$arch=variable_get('archive_del_data');
if(empty($arch)) {
$this->del($archive);
return true;
} }
$BACKUP_DIR=$arch;
$db->query("select login from membres where uid=$cuid;"); /**
$db->next_record(); * Plans the cration of a file
if (!$db->Record["login"]) { *
$err->raise("action",_("Login corresponding to $cuid not found")); * @global type $err
return false; * @global type $L_INOTIFY_DO_ACTION
* @return boolean
*/
function do_action() {
global $err, $L_INOTIFY_DO_ACTION;
$err->log("action", "do_action");
if( ! touch($L_INOTIFY_DO_ACTION) ){
return FALSE;
}
return TRUE;
} }
$uidlogin=$cuid."-".$db->Record["login"];
//The path will look like /<archive_del_data>/YYYY-MM/<uid>-<login>/<folder> /**
$today=getdate(); * Plans a file creation
$dest=$BACKUP_DIR.'/'.$today["year"].'-'.$today["mon"].'/'.$uidlogin.'/'.$dir; *
$this->move($archive,$dest); * @param string $file
return true; * @param string $content
} * @param int $user
* @return boolean
/* */
*function inserting the action in the sql table function create_file($file, $content = "", $user = "root") {
*/ return $this->set('create_file', $user, array('file' => $file, 'content' => $content));
function set($type,$user,$parameters) {
global $db,$err;
$err->log("action","set",$type);
$serialized=serialize($parameters);
switch($type){
case 'create_file':
$query="insert into actions values ('','CREATE_FILE','$serialized',now(),'','','$user','');";
break;
case 'create_dir':
$query="insert into actions values ('','CREATE_DIR','$serialized',now(),'','','$user','');";
break;
case 'move':
$query="insert into actions values ('','MOVE','$serialized',now(),'','','$user','');";
break;
case 'fix_user':
$query="insert into actions values ('','FIX_USER','$serialized',now(),'','','$user','');";
break;
case 'fix_file':
$query="insert into actions values ('','FIX_FILE','$serialized',now(),'','','$user','');";
break;
case 'fix_dir':
$query="insert into actions values ('','FIX_DIR','$serialized',now(),'','','$user','');";
break;
case 'delete':
$query="insert into actions values ('','DELETE','$serialized',now(),'','','$user','');";
break;
default:
return false;
} }
if(!$db->query($query)){
$err->raise("action",_("Error setting actions")); /**
return false; * Plans the cration of a dir
*
* @param string $dir
* @param int $user
* @return boolean
*/
function create_dir($dir, $user = "root") {
return $this->set('create_dir', $user, array('dir' => $dir));
} }
$this->do_action();
return true;
}
function get_old($all= null){ /**
global $err,$db; * Plans a perms fix upon user creation
* @param int $uid
$purge="select * from actions where TO_DAYS(curdate()) - TO_DAYS(creation) > 2;"; * @param string $user
if(!$db->query($purge)){ * @return boolean
$err->raise("action",_("Error selecting old actions")); */
return false; function fix_user($uid, $user = "root") {
return $this->set('fix_user', $user, array('uid' => $uid));
} }
}
function purge($all=null){ /**
global $err,$db; * Plans a dir fix
if(is_null($all)){ *
$purge="delete from actions where TO_DAYS(curdate()) - TO_DAYS(creation) > 2 and status = 0;"; * @param type $dir
}else{ * @param type $user
$purge="delete from actions where TO_DAYS(curdate()) - TO_DAYS(creation) > 2;"; * @return type
} */
if(!$db->query($purge)){ function fix_dir($dir, $user = "root") {
$err->raise("action",_("Error purging old actions")); return $this->set('fix_dir', $user, array('dir' => $dir));
return false;
} }
}
/* /**
* function returning the first not locked line of the action table * Plans a file fix
*/ *
function get_action() { * @param type $file
global $db,$err; * @param type $user
* @return type
$tab=array(); */
$db->query('select * from actions where end =0 and begin = 0 order by id limit 1;'); function fix_file($file, $user = "root") {
if ($db->next_record()){ return $this->set('fix_file', $user, array('file' => $file));
$tab[]=$db->Record;
return $tab;
}else{
return false;
} }
}
/* /**
* function locking an entry while it is being executed by the action script * function to delete file / folder
*/ *
function begin($id) { * @param type $dir
global $db,$err; * @param type $user
if(!$db->query("update actions set begin=now() where id=$id ;")){ * @return type
$err->raise("action",_("Error locking the action : $id")); */
return false; function del($dir, $user = "root") {
return $this->set('delete', $user, array('dir' => $dir));
} }
return true;
}
/* /**
* function locking an entry while it is being executed by the action script * function returning the first not locked line of the action table
*/ *
function finish($id,$return=0) { * @param type $src
global $db,$err; * @param type $dst
if(!$db->query("update actions set end=now(),status='$return' where id=$id ;")){ * @param type $user
$err->raise("action",_("Error unlocking the action : $id")); * @return type
return false; */
function move($src, $dst, $user = "root") {
return $this->set('move', $user, array('src' => $src, 'dst' => $dst));
} }
return true;
}
function reset_job($id) { /**
global $db,$err; *
if(!$db->query("update actions set end=0,begin=0,status='' where id=$id ;")){ * function archiving a directory ( upon account deletion )
$err->raise("action",_("Error unlocking the action : $id")); *
return false; * @global type $cuid
* @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") {
global $cuid, $db, $err;
$arch = variable_get('archive_del_data');
if (empty($arch)) {
$this->del($archive);
return true;
}
$BACKUP_DIR = $arch;
$db->query("select login from membres where uid=$cuid;");
$db->next_record();
if (!$db->Record["login"]) {
$err->raise("action", _("Login corresponding to $cuid not found"));
return false;
}
$uidlogin = $cuid . "-" . $db->Record["login"];
//The path will look like /<archive_del_data>/YYYY-MM/<uid>-<login>/<folder>
$today = getdate();
$dest = $BACKUP_DIR . '/' . $today["year"] . '-' . $today["mon"] . '/' . $uidlogin . '/' . $dir;
$this->move($archive, $dest);
return true;
} }
return true;
}
function get_job() { /**
global $db,$err; * function inserting the action in the sql table
$tab=array(); *
$db->query("Select * from actions where begin !=0 and end = 0 ;"); * @global type $db
if ($db->next_record()){ * @global type $err
$tab[]=$db->Record; * @param type $type
return $tab; * @param type $user
}else{ * @param type $parameters
return false; * @return boolean
*/
function set($type, $user, $parameters) {
global $db, $err;
$err->log("action", "set", $type);
$serialized = serialize($parameters);
switch ($type) {
case 'create_file':
$query = "insert into actions values ('','CREATE_FILE','$serialized',now(),'','','$user','');";
break;
case 'create_dir':
$query = "insert into actions values ('','CREATE_DIR','$serialized',now(),'','','$user','');";
break;
case 'move':
$query = "insert into actions values ('','MOVE','$serialized',now(),'','','$user','');";
break;
case 'fix_user':
$query = "insert into actions values ('','FIX_USER','$serialized',now(),'','','$user','');";
break;
case 'fix_file':
$query = "insert into actions values ('','FIX_FILE','$serialized',now(),'','','$user','');";
break;
case 'fix_dir':
$query = "insert into actions values ('','FIX_DIR','$serialized',now(),'','','$user','');";
break;
case 'delete':
$query = "insert into actions values ('','DELETE','$serialized',now(),'','','$user','');";
break;
default:
return false;
}
if (!$db->query($query)) {
$err->raise("action", _("Error setting actions"));
return false;
}
$this->do_action();
return true;
} }
}
/* /**
* function locking an entry while it is being executed by the action script * This seems to be unused ?
*/ *
function cancel($id) { * @global type $err
global $db; * @global type $db
$this->finish($id, 666); * @param type $all
return true; * @return boolean
} */
function get_old($all = null) {
global $err, $db;
} /* Class action */ $purge = "select * from actions where TO_DAYS(curdate()) - TO_DAYS(creation) > 2;";
$result = $db->query($purge);
if (! $result) {
$err->raise("action", _("Error selecting old actions"));
return false;
}
return $db->num_rows($result) ;
}
?> /**
*
* @global type $err
* @global type $db
* @param type $all
* @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;";
}
$result = $db->query($purge);
if (! $result) {
$err->raise("action", _("Error purging old actions"));
return false;
}
return $db->num_rows($result) ;
}
/**
* function returning the first not locked line of the action table
*
* @global type $db
* @global type $err
* @return boolean or array
*/
function get_action() {
global $db, $err;
$tab = array();
$db->query('select * from actions where end = 0 and begin = 0 order by id limit 1;');
if ($db->next_record()) {
$tab[] = $db->Record;
return $tab;
} else {
return false;
}
}
/**
* 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) {
global $db, $err;
if (!$db->query("update actions set begin=now() where id=$id ;")) {
$err->raise("action", _("Error locking the action : $id"));
return false;
}
return true;
}
/**
* 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) {
global $db, $err;
if (!$db->query("update actions set end=now(),status='$return' where id=$id ;")) {
$err->raise("action", _("Error unlocking the action : $id"));
return false;
}
return true;
}
/**
*
* @global type $db
* @global type $err
* @param type $id
* @return boolean
*/
function reset_job($id) {
global $db, $err;
if (!$db->query("update actions set end=0,begin=0,status='' where id=$id ;")) {
$err->raise("action", _("Error unlocking the action : $id"));
return false;
}
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() {
global $db, $err;
$tab = array();
$db->query("Select * from actions where begin !=0 and end = 0 ;");
if ($db->next_record()) {
$tab[] = $db->Record;
return $tab;
} else {
return false;
}
}
/**
* function locking an entry while it is being executed by the action script
*
* @global type $db
* @param type $id
* @return boolean
*/
function cancel($id) {
global $db;
$this->finish($id, 666);
return true;
}
}
/* Class action */