AlternC/bureau/class/m_action.php

370 lines
9.9 KiB
PHP
Raw Permalink Normal View History

2013-04-19 12:24:00 +00:00
<?php
2014-03-23 14:15:33 +00:00
2013-04-19 12:24:00 +00:00
/*
----------------------------------------------------------------------
2014-03-23 14:15:33 +00:00
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.
2013-04-19 12:24:00 +00:00
2014-03-23 14:15:33 +00:00
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.
2013-04-19 12:24:00 +00:00
2014-03-23 14:15:33 +00:00
To read the license please visit http://www.gnu.org/copyleft/gpl.html
----------------------------------------------------------------------
*/
2013-04-19 12:24:00 +00:00
/**
2014-03-23 14:15:33 +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.
*
* @copyright AlternC-Team 2000-2017 https://alternc.com/
2014-03-23 14:15:33 +00:00
*/
2013-04-19 12:24:00 +00:00
class m_action {
2013-04-24 12:43:05 +00:00
2014-03-23 14:15:33 +00:00
/**
* Tell the incron that an action should be performed
2014-03-23 14:15:33 +00:00
*
2017-08-17 01:32:18 +00:00
* @global m_messages $msg
* @global string $L_INOTIFY_DO_ACTION
2014-03-23 14:15:33 +00:00
* @return boolean
*/
function do_action() {
2017-08-17 01:32:18 +00:00
global $msg, $L_INOTIFY_DO_ACTION;
$msg->log("action", "do_action");
if (!@touch($L_INOTIFY_DO_ACTION)) {
2014-03-23 14:15:33 +00:00
return FALSE;
}
return TRUE;
}
2013-04-24 12:43:05 +00:00
2014-03-23 14:15:33 +00:00
/**
* Plans a file creation
*
* @param string $file
2014-03-23 14:15:33 +00:00
* @param string $content
* @param int $user
* @return boolean
*/
function create_file($file, $content = "", $user = "root") {
return $this->set('create_file', $user, array('file' => $file, 'content' => $content));
}
2013-04-24 12:43:05 +00:00
2014-03-23 14:15:33 +00:00
/**
2014-09-09 12:52:56 +00:00
* 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));
2014-09-09 12:52:56 +00:00
}
2014-09-09 12:52:56 +00:00
/**
* Plans the creation of a dir
2014-03-23 14:15:33 +00:00
*
* @param string $dir
* @param int $user
* @return boolean
*/
function create_dir($dir, $user = "root") {
return $this->set('create_dir', $user, array('dir' => $dir));
}
2013-05-14 08:24:17 +00:00
2014-03-23 14:15:33 +00:00
/**
* Plans a perms fix upon user creation
* @param int $uid
* @param string $user
* @return boolean
*/
function fix_user($uid, $user = "root") {
return $this->set('fix_user', $user, array('uid' => $uid));
}
2013-04-24 12:43:05 +00:00
2014-03-23 14:15:33 +00:00
/**
* Plans a dir permission fix
2014-03-23 14:15:33 +00:00
*
* @param string $dir
* @param m_user $user
2014-03-26 13:59:41 +00:00
* @return boolean
2014-03-23 14:15:33 +00:00
*/
function fix_dir($dir, $user = "root") {
return $this->set('fix_dir', $user, array('dir' => $dir));
}
2013-04-24 12:43:05 +00:00
2014-03-23 14:15:33 +00:00
/**
* Plans a file permission fix
2014-03-23 14:15:33 +00:00
*
* @param string $file
* @param m_user $user
2014-03-26 13:59:41 +00:00
* @return boolean
2014-03-23 14:15:33 +00:00
*/
function fix_file($file, $user = "root") {
return $this->set('fix_file', $user, array('file' => $file));
}
2014-03-23 14:15:33 +00:00
/**
* function to delete file / folder
*
* @param string $dir
* @param m_user $user
2014-03-26 13:59:41 +00:00
* @return boolean
2014-03-23 14:15:33 +00:00
*/
function del($dir, $user = "root") {
return $this->set('delete', $user, array('dir' => $dir));
}
2014-03-23 14:15:33 +00:00
2014-03-23 14:15:33 +00:00
/**
* function returning the first not locked line of the action table
*
* @param string $src
* @param string $dst
* @param m_user $user
2014-03-26 13:59:41 +00:00
* @return boolean
2014-03-23 14:15:33 +00:00
*/
function move($src, $dst, $user = "root") {
return $this->set('move', $user, array('src' => $src, 'dst' => $dst));
2013-04-22 08:23:28 +00:00
}
2014-03-23 14:15:33 +00:00
/**
*
* function archiving a directory ( upon account deletion )
*
* @global int $cuid
* @global m_mysql $db
2017-08-17 01:32:18 +00:00
* @global m_messages $msg
* @param string $archive Directory to archive within the archive_del_data folder if set in variable sql table
2014-03-23 14:15:33 +00:00
* If archive_del_data is not set we delete the folder
* @param string $dir sub_directory of the archive directory
2014-03-23 14:15:33 +00:00
* @return boolean
*/
function archive($archive, $dir = "html") {
2017-08-17 01:32:18 +00:00
global $cuid, $db, $msg;
2013-04-24 12:43:05 +00:00
2014-03-23 14:15:33 +00:00
$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= ?;", array($cuid));
2014-03-23 14:15:33 +00:00
$db->next_record();
if (!$db->Record["login"]) {
$msg->raise("ERROR", "action", _("Login corresponding to $cuid not found"));
2014-03-23 14:15:33 +00:00
return false;
}
$uidlogin = $cuid . "-" . $db->Record["login"];
2013-04-24 12:43:05 +00:00
2014-03-23 14:15:33 +00:00
//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;
2013-04-24 12:43:05 +00:00
}
2014-03-23 14:15:33 +00:00
/**
* function inserting the action in the sql table
*
* @global m_mysql $db
2017-08-17 01:32:18 +00:00
* @global m_messages $msg
* @param string $type
2014-03-26 15:40:43 +00:00
* @param string|integer $user wich user do we impersonate?
* @param mixed $parameters
2014-03-23 14:15:33 +00:00
* @return boolean
*/
function set($type, $user, $parameters) {
2017-08-17 01:32:18 +00:00
global $db, $msg;
$msg->log("action", "set", $type);
2014-03-23 14:15:33 +00:00
$serialized = serialize($parameters);
$type = strtoupper($type);
if (in_array($type, array('CHMOD',
'CREATE_FILE',
'CREATE_DIR',
'MOVE',
'FIX_USER',
'FIX_FILE',
'FIX_DIR',
'DELETE'))) {
$query = "INSERT INTO `actions` (type, parameters, creation, user) VALUES('$type', '$serialized', now(), '$user');";
} else {
return False;
}
2014-03-23 14:15:33 +00:00
if (!$db->query($query)) {
$msg->raise("ERROR", "action", _("Error setting actions"));
2014-03-23 14:15:33 +00:00
return false;
}
2014-09-29 15:06:52 +00:00
return $this->do_action();
}
2013-04-19 14:19:50 +00:00
2014-03-23 14:15:33 +00:00
/**
* This seems to be unused ?
*
2017-08-17 01:32:18 +00:00
* @global m_messages $msg
* @global m_mysql $db
2014-03-23 14:15:33 +00:00
* @return boolean
*/
2014-03-26 15:40:43 +00:00
function get_old() {
2017-08-17 01:32:18 +00:00
global $msg, $db;
2013-04-19 15:23:26 +00:00
2014-03-23 14:15:33 +00:00
$purge = "select * from actions where TO_DAYS(curdate()) - TO_DAYS(creation) > 2;";
$result = $db->query($purge);
if (!$result) {
$msg->raise("ERROR", "action", _("Error selecting old actions"));
2014-03-23 14:15:33 +00:00
return false;
}
return $db->num_rows($result);
}
2013-04-24 12:43:05 +00:00
2014-03-23 14:15:33 +00:00
/**
*
2017-08-17 01:32:18 +00:00
* @global m_messages $msg
* @global m_mysql $db
2014-03-23 14:15:33 +00:00
* @param type $all
* @return boolean|int The number of rows purged; False is there was an error
2014-03-23 14:15:33 +00:00
*/
function purge($all = null) {
2017-08-17 01:32:18 +00:00
global $msg, $db;
2014-03-23 14:15:33 +00:00
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) {
$msg->raise("ERROR", "action", _("Error purging old actions"));
2014-03-23 14:15:33 +00:00
return false;
}
return $db->num_rows($result);
}
2013-04-24 12:43:05 +00:00
2014-03-23 14:15:33 +00:00
/**
* function returning the first not locked line of the action table
*
* @global m_mysql $db
2014-03-23 14:15:33 +00:00
* @return boolean or array
*/
function get_action() {
2017-08-17 01:32:18 +00:00
global $db;
2014-03-23 14:15:33 +00:00
$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;
}
}
2014-03-23 14:15:33 +00:00
/**
* function locking an entry while it is being executed by the action script
*
* @global m_mysql $db
2017-08-17 01:32:18 +00:00
* @global m_messages $msg
* @param int $id
2014-03-23 14:15:33 +00:00
* @return boolean
*/
function begin($id) {
2017-08-17 01:32:18 +00:00
global $db, $msg;
if (!$db->query("update actions set begin=now() where id= ? ;", array($id))) {
$msg->raise("ERROR", "action", _("Error locking the action : $id"));
2014-03-23 14:15:33 +00:00
return false;
}
return true;
}
2014-03-23 14:15:33 +00:00
/**
* function locking an entry while it is being executed by the action script
*
* @global m_mysql $db
2017-08-17 01:32:18 +00:00
* @global m_messages $msg
* @param int $id
2014-03-26 13:59:41 +00:00
* @param integer $return
2014-03-23 14:15:33 +00:00
* @return boolean
*/
function finish($id, $return = 0) {
2017-08-17 01:32:18 +00:00
global $db, $msg;
if (!$db->query("update actions set end=now(),status=? where id= ?;", array($return, $id))) {
$msg->raise("ERROR", "action", _("Error unlocking the action : $id"));
2014-03-23 14:15:33 +00:00
return false;
}
return true;
}
2014-03-23 14:15:33 +00:00
/**
*
* @global m_mysql $db
2017-08-17 01:32:18 +00:00
* @global m_messages $msg
* @param int $id
2014-03-23 14:15:33 +00:00
* @return boolean
*/
function reset_job($id) {
2017-08-17 01:32:18 +00:00
global $db, $msg;
if (!$db->query("update actions set end=0,begin=0,status='' where id= ?;", array($id))) {
$msg->raise("ERROR", "action", _("Error unlocking the action : $id"));
2014-03-23 14:15:33 +00:00
return false;
}
return true;
}
2014-03-23 14:15:33 +00:00
/**
* Returns a list of actions marked as executable and ready for execution
*
* @global m_mysql $db
2017-08-17 01:32:18 +00:00
* @global m_messages $msg
2014-03-23 14:15:33 +00:00
* @return boolean
*/
function get_job() {
global $db;
2014-03-23 14:15:33 +00:00
$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;
}
}
2013-04-24 12:43:05 +00:00
2014-03-23 14:15:33 +00:00
/**
* function locking an entry while it is being executed by the action script
*
* @global m_mysql $db
* @param int $id
2014-03-23 14:15:33 +00:00
* @return boolean
*/
function cancel($id) {
$this->finish($id, 666);
return true;
}
2013-04-19 12:24:00 +00:00
} /* Class action */
2013-04-19 12:24:00 +00:00