2013-04-19 12:24:00 +00:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
LICENSE
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License (GPL)
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
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.
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
/**
|
2013-08-14 13:17:16 +00:00
|
|
|
* 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.
|
|
|
|
* 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}
|
|
|
|
*
|
|
|
|
* @copyright AlternC-Team 2013-8-13 http://alternc.org/
|
|
|
|
*
|
|
|
|
*/
|
2013-04-19 12:24:00 +00:00
|
|
|
class m_action {
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/** Constructor
|
|
|
|
*/
|
|
|
|
function m_action() {
|
|
|
|
}
|
2013-04-24 12:43:05 +00:00
|
|
|
|
2013-04-19 12:24:00 +00:00
|
|
|
/*
|
|
|
|
* function to set the cration of a file
|
|
|
|
*/
|
2013-04-24 12:43:05 +00:00
|
|
|
function do_action(){
|
|
|
|
global $err, $L_INOTIFY_DO_ACTION;
|
2013-04-25 13:05:27 +00:00
|
|
|
$err->log("action","do_action");
|
2013-04-24 12:43:05 +00:00
|
|
|
touch($L_INOTIFY_DO_ACTION);
|
|
|
|
}
|
|
|
|
|
2013-04-22 08:23:28 +00:00
|
|
|
function create_file($file,$content="",$user="root") {
|
2013-04-19 15:23:26 +00:00
|
|
|
return $this->set('create_file',$user, array('file'=>$file,'content'=>$content));
|
2013-04-19 12:24:00 +00:00
|
|
|
}
|
2013-04-24 12:43:05 +00:00
|
|
|
|
2013-04-19 12:24:00 +00:00
|
|
|
/*
|
|
|
|
* function to set the cration of a file
|
|
|
|
*/
|
|
|
|
function create_dir($dir,$user="root") {
|
2013-04-19 15:23:26 +00:00
|
|
|
return $this->set('create_dir',$user, array('dir'=>$dir));
|
2013-04-19 12:24:00 +00:00
|
|
|
}
|
2013-04-24 12:43:05 +00:00
|
|
|
|
2013-08-08 15:56:09 +00:00
|
|
|
/*
|
|
|
|
* function to set the perms upon user creation
|
|
|
|
*/
|
|
|
|
function fix_user($uid,$user="root") {
|
|
|
|
return $this->set('fix_user',$user, array('uid'=>$uid));
|
|
|
|
}
|
2013-04-19 12:24:00 +00:00
|
|
|
/*
|
2013-04-23 16:08:56 +00:00
|
|
|
* function to set the cration of a file
|
|
|
|
*/
|
|
|
|
function fix_dir($dir,$user="root") {
|
|
|
|
return $this->set('fix_dir',$user, array('dir'=>$dir));
|
|
|
|
}
|
2013-04-24 12:43:05 +00:00
|
|
|
|
2013-05-14 08:24:17 +00:00
|
|
|
/*
|
|
|
|
* function to set the cration of a file
|
|
|
|
*/
|
|
|
|
function fix_file($file,$user="root") {
|
|
|
|
return $this->set('fix_file',$user, array('file'=>$file));
|
|
|
|
}
|
|
|
|
|
2013-04-23 16:08:56 +00:00
|
|
|
/*
|
2013-04-19 12:24:00 +00:00
|
|
|
* function to delete file / folder
|
|
|
|
*/
|
2013-04-22 16:02:05 +00:00
|
|
|
function del($dir,$user="root") {
|
2013-04-19 15:23:26 +00:00
|
|
|
return $this->set('delete',$user, array('dir'=>$dir));
|
2013-04-19 12:24:00 +00:00
|
|
|
}
|
2013-04-24 12:43:05 +00:00
|
|
|
|
2013-04-19 12:24:00 +00:00
|
|
|
/*
|
|
|
|
* function returning the first not locked line of the action table
|
|
|
|
*/
|
2013-04-22 08:23:28 +00:00
|
|
|
function move($src,$dst,$user="root") {
|
2013-04-19 15:23:26 +00:00
|
|
|
return $this->set('move',$user, array('src'=>$src, 'dst'=>$dst));
|
2013-04-19 12:24:00 +00:00
|
|
|
}
|
2013-04-24 12:43:05 +00:00
|
|
|
|
2013-04-19 12:24:00 +00:00
|
|
|
/*
|
|
|
|
* function archiving a directory ( upon account deletion )
|
2013-10-18 09:04:18 +00:00
|
|
|
* @param: $archive : directory to archive within the archive_del_data folder if set in variable sql table.
|
2013-08-14 13:17:16 +00:00
|
|
|
* If archive_del_data is not set we delete the folder.
|
|
|
|
* @param: $dir : sub_directory of the archive directory
|
2013-04-19 12:24:00 +00:00
|
|
|
*/
|
2013-10-18 07:28:05 +00:00
|
|
|
function archive($archive,$dir="html") {
|
2013-04-22 13:28:59 +00:00
|
|
|
global $cuid,$db,$err;
|
2013-08-14 13:17:16 +00:00
|
|
|
|
|
|
|
$arch=variable_get('archive_del_data');
|
|
|
|
if(empty($arch)) {
|
|
|
|
$this->del($archive);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
$BACKUP_DIR=$arch;
|
2013-04-22 08:23:28 +00:00
|
|
|
$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"];
|
|
|
|
|
2013-08-14 13:17:16 +00:00
|
|
|
//The path will look like /<archive_del_data>/YYYY-MM/<uid>-<login>/<folder>
|
2013-04-19 15:23:26 +00:00
|
|
|
$today=getdate();
|
2013-04-22 08:43:03 +00:00
|
|
|
$dest=$BACKUP_DIR.'/'.$today["year"].'-'.$today["mon"].'/'.$uidlogin.'/'.$dir;
|
2013-04-19 15:23:26 +00:00
|
|
|
$this->move($archive,$dest);
|
2013-08-14 13:17:16 +00:00
|
|
|
return true;
|
2013-04-19 12:24:00 +00:00
|
|
|
}
|
2013-04-24 12:43:05 +00:00
|
|
|
|
2013-04-19 12:24:00 +00:00
|
|
|
/*
|
|
|
|
*function inserting the action in the sql table
|
|
|
|
*/
|
2013-04-19 15:23:26 +00:00
|
|
|
function set($type,$user,$parameters) {
|
2013-04-22 13:28:59 +00:00
|
|
|
global $db,$err;
|
2013-05-14 08:24:17 +00:00
|
|
|
$err->log("action","set",$type);
|
2013-04-19 14:19:50 +00:00
|
|
|
|
2013-04-19 15:23:26 +00:00
|
|
|
$serialized=serialize($parameters);
|
2013-04-19 14:19:50 +00:00
|
|
|
switch($type){
|
|
|
|
case 'create_file':
|
2013-04-22 13:28:59 +00:00
|
|
|
$query="insert into actions values ('','CREATE_FILE','$serialized',now(),'','','$user','');";
|
2013-04-22 08:23:28 +00:00
|
|
|
break;
|
2013-04-19 14:19:50 +00:00
|
|
|
case 'create_dir':
|
2013-04-22 13:28:59 +00:00
|
|
|
$query="insert into actions values ('','CREATE_DIR','$serialized',now(),'','','$user','');";
|
2013-04-22 08:23:28 +00:00
|
|
|
break;
|
2013-04-19 14:19:50 +00:00
|
|
|
case 'move':
|
2013-04-22 13:28:59 +00:00
|
|
|
$query="insert into actions values ('','MOVE','$serialized',now(),'','','$user','');";
|
2013-04-22 08:23:28 +00:00
|
|
|
break;
|
2013-08-08 15:56:09 +00:00
|
|
|
case 'fix_user':
|
2013-10-18 09:04:18 +00:00
|
|
|
$query="insert into actions values ('','FIX_USER','$serialized',now(),'','','$user','');";
|
2013-08-08 15:56:09 +00:00
|
|
|
break;
|
2013-05-14 08:24:17 +00:00
|
|
|
case 'fix_file':
|
2013-10-18 09:04:18 +00:00
|
|
|
$query="insert into actions values ('','FIX_FILE','$serialized',now(),'','','$user','');";
|
2013-05-14 08:24:17 +00:00
|
|
|
break;
|
2013-04-23 16:08:56 +00:00
|
|
|
case 'fix_dir':
|
2013-10-18 09:04:18 +00:00
|
|
|
$query="insert into actions values ('','FIX_DIR','$serialized',now(),'','','$user','');";
|
2013-04-23 16:08:56 +00:00
|
|
|
break;
|
2013-08-08 15:56:09 +00:00
|
|
|
case 'delete':
|
2013-04-22 13:28:59 +00:00
|
|
|
$query="insert into actions values ('','DELETE','$serialized',now(),'','','$user','');";
|
2013-04-22 08:23:28 +00:00
|
|
|
break;
|
2013-04-19 14:19:50 +00:00
|
|
|
default:
|
|
|
|
return false;
|
2013-04-24 12:43:05 +00:00
|
|
|
}
|
|
|
|
if(!$db->query($query)){
|
2013-04-22 13:28:59 +00:00
|
|
|
$err->raise("action",_("Error setting actions"));
|
2013-04-24 12:43:05 +00:00
|
|
|
return false;
|
2013-04-22 13:28:59 +00:00
|
|
|
}
|
2013-04-24 12:43:05 +00:00
|
|
|
$this->do_action();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_old($all= null){
|
|
|
|
global $err,$db;
|
|
|
|
|
2013-04-22 16:02:05 +00:00
|
|
|
$purge="select * from actions where TO_DAYS(curdate()) - TO_DAYS(creation) > 2;";
|
2013-04-22 13:28:59 +00:00
|
|
|
if(!$db->query($purge)){
|
2013-04-24 12:43:05 +00:00
|
|
|
$err->raise("action",_("Error selecting old actions"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)){
|
2013-04-22 13:28:59 +00:00
|
|
|
$err->raise("action",_("Error purging old actions"));
|
2013-04-22 12:31:43 +00:00
|
|
|
return false;
|
2013-04-22 13:28:59 +00:00
|
|
|
}
|
2013-04-19 12:24:00 +00:00
|
|
|
}
|
2013-04-19 14:19:50 +00:00
|
|
|
|
2013-04-19 12:24:00 +00:00
|
|
|
/*
|
|
|
|
* function returning the first not locked line of the action table
|
|
|
|
*/
|
|
|
|
function get_action() {
|
2013-04-22 13:28:59 +00:00
|
|
|
global $db,$err;
|
2013-04-19 15:23:26 +00:00
|
|
|
|
|
|
|
$tab=array();
|
2013-04-22 13:42:37 +00:00
|
|
|
$db->query('select * from actions where end =0 and begin = 0 order by id limit 1;');
|
2013-04-19 14:19:50 +00:00
|
|
|
if ($db->next_record()){
|
|
|
|
$tab[]=$db->Record;
|
|
|
|
return $tab;
|
2013-04-22 13:28:59 +00:00
|
|
|
}else{
|
2013-04-19 14:19:50 +00:00
|
|
|
return false;
|
2013-04-22 13:28:59 +00:00
|
|
|
}
|
2013-04-19 12:24:00 +00:00
|
|
|
}
|
2013-04-24 12:43:05 +00:00
|
|
|
|
2013-04-19 12:24:00 +00:00
|
|
|
/*
|
|
|
|
* function locking an entry while it is being executed by the action script
|
|
|
|
*/
|
|
|
|
function begin($id) {
|
2013-04-22 13:28:59 +00:00
|
|
|
global $db,$err;
|
2013-04-22 16:02:05 +00:00
|
|
|
if(!$db->query("update actions set begin=now() where id=$id ;")){
|
|
|
|
$err->raise("action",_("Error locking the action : $id"));
|
2013-04-22 13:28:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-04-19 12:24:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
2013-04-24 12:43:05 +00:00
|
|
|
|
2013-04-19 12:24:00 +00:00
|
|
|
/*
|
|
|
|
* function locking an entry while it is being executed by the action script
|
|
|
|
*/
|
2013-04-22 12:31:43 +00:00
|
|
|
function finish($id,$return=0) {
|
2013-04-22 13:28:59 +00:00
|
|
|
global $db,$err;
|
2013-04-22 16:02:05 +00:00
|
|
|
if(!$db->query("update actions set end=now(),status='$return' where id=$id ;")){
|
2013-04-22 13:28:59 +00:00
|
|
|
$err->raise("action",_("Error unlocking the action : $id"));
|
|
|
|
return false;
|
|
|
|
}
|
2013-04-19 12:24:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
2013-04-23 16:08:56 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_job() {
|
|
|
|
global $db,$err;
|
|
|
|
$tab=array();
|
2013-04-24 12:43:05 +00:00
|
|
|
$db->query("Select * from actions where begin !=0 and end = 0 ;");
|
2013-04-23 16:08:56 +00:00
|
|
|
if ($db->next_record()){
|
|
|
|
$tab[]=$db->Record;
|
|
|
|
return $tab;
|
|
|
|
}else{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2013-04-24 12:43:05 +00:00
|
|
|
|
2013-04-19 12:24:00 +00:00
|
|
|
/*
|
|
|
|
* function locking an entry while it is being executed by the action script
|
|
|
|
*/
|
|
|
|
function cancel($id) {
|
|
|
|
global $db;
|
2013-04-22 12:31:43 +00:00
|
|
|
$this->finish($id, 666);
|
2013-04-19 12:24:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} /* Class action */
|
|
|
|
|
|
|
|
?>
|