Alternc  latest
Alternc logiel libre pour l'hébergement
m_action.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  ----------------------------------------------------------------------
5  LICENSE
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License (GPL)
9  as published by the Free Software Foundation; either version 2
10  of the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  To read the license please visit http://www.gnu.org/copyleft/gpl.html
18  ----------------------------------------------------------------------
19 */
20 
21 /**
22  * This class manage actions to be performed on the file system on behalf of alternc Classes
23  * It primary use is to store the actions to be performed ( creating file or folder, deleting, setting permissions etc..) in the action sql table.
24  * The script /usr/lib/alternc/do_actions.php handled by cron and incron is then used to perform those actions.
25  *
26  * @copyright AlternC-Team 2000-2017 https://alternc.com/
27  */
28 class m_action {
29 
30 
31  /**
32  * Tell the incron that an action should be performed
33  *
34  * @global m_messages $msg
35  * @global string $L_INOTIFY_DO_ACTION
36  * @return boolean
37  */
38  function do_action() {
39  global $msg, $L_INOTIFY_DO_ACTION;
40  $msg->log("action", "do_action");
41  if (!@touch($L_INOTIFY_DO_ACTION)) {
42  return FALSE;
43  }
44  return TRUE;
45  }
46 
47 
48  /**
49  * Plans a file creation
50  *
51  * @param string $file
52  * @param string $content
53  * @param int $user
54  * @return boolean
55  */
56  function create_file($file, $content = "", $user = "root") {
57  return $this->set('create_file', $user, array('file' => $file, 'content' => $content));
58  }
59 
60 
61  /**
62  * Plans the a chmod on file or dir
63  *
64  * @param string $filename
65  * @param int $perms
66  * @param string $user
67  * @return boolean
68  */
69  function chmod($filename, $perms, $user = "root") {
70  return $this->set('chmod', $user, array('filename' => $filename, "perms" => $perms));
71  }
72 
73 
74  /**
75  * Plans the creation of a dir
76  *
77  * @param string $dir
78  * @param int $user
79  * @return boolean
80  */
81  function create_dir($dir, $user = "root") {
82  return $this->set('create_dir', $user, array('dir' => $dir));
83  }
84 
85 
86  /**
87  * Plans a perms fix upon user creation
88  * @param int $uid
89  * @param string $user
90  * @return boolean
91  */
92  function fix_user($uid, $user = "root") {
93  return $this->set('fix_user', $user, array('uid' => $uid));
94  }
95 
96 
97  /**
98  * Plans a dir permission fix
99  *
100  * @param string $dir
101  * @param m_user $user
102  * @return boolean
103  */
104  function fix_dir($dir, $user = "root") {
105  return $this->set('fix_dir', $user, array('dir' => $dir));
106  }
107 
108 
109  /**
110  * Plans a file permission fix
111  *
112  * @param string $file
113  * @param m_user $user
114  * @return boolean
115  */
116  function fix_file($file, $user = "root") {
117  return $this->set('fix_file', $user, array('file' => $file));
118  }
119 
120 
121  /**
122  * function to delete file / folder
123  *
124  * @param string $dir
125  * @param m_user $user
126  * @return boolean
127  */
128  function del($dir, $user = "root") {
129  return $this->set('delete', $user, array('dir' => $dir));
130  }
131 
132 
133  /**
134  * function returning the first not locked line of the action table
135  *
136  * @param string $src
137  * @param string $dst
138  * @param m_user $user
139  * @return boolean
140  */
141  function move($src, $dst, $user = "root") {
142  return $this->set('move', $user, array('src' => $src, 'dst' => $dst));
143  }
144 
145 
146  /**
147  *
148  * function archiving a directory ( upon account deletion )
149  *
150  * @global int $cuid
151  * @global m_mysql $db
152  * @global m_messages $msg
153  * @param string $archive Directory to archive within the archive_del_data folder if set in variable sql table
154  * If archive_del_data is not set we delete the folder
155  * @param string $dir sub_directory of the archive directory
156  * @return boolean
157  */
158  function archive($archive, $dir = "html") {
159  global $cuid, $db, $msg;
160 
161  $arch = variable_get('archive_del_data');
162  if (empty($arch)) {
163  $this->del($archive);
164  return true;
165  }
166  $BACKUP_DIR = $arch;
167  $db->query("select login from membres where uid= ?;", array($cuid));
168  $db->next_record();
169  if (!$db->Record["login"]) {
170  $msg->raise("ERROR", "action", _("Login corresponding to $cuid not found"));
171  return false;
172  }
173  $uidlogin = $cuid . "-" . $db->Record["login"];
174 
175  //The path will look like /<archive_del_data>/YYYY-MM/<uid>-<login>/<folder>
176  $today = getdate();
177  $dest = $BACKUP_DIR . '/' . $today["year"] . '-' . $today["mon"] . '/' . $uidlogin . '/' . $dir;
178  $this->move($archive, $dest);
179  return true;
180  }
181 
182 
183  /**
184  * function inserting the action in the sql table
185  *
186  * @global m_mysql $db
187  * @global m_messages $msg
188  * @param string $type
189  * @param string|integer $user wich user do we impersonate?
190  * @param mixed $parameters
191  * @return boolean
192  */
193  function set($type, $user, $parameters) {
194  global $db, $msg;
195  $msg->log("action", "set", $type);
196  $serialized = serialize($parameters);
197  $type = strtoupper($type);
198  if (in_array($type, array('CHMOD',
199  'CREATE_FILE',
200  'CREATE_DIR',
201  'MOVE',
202  'FIX_USER',
203  'FIX_FILE',
204  'FIX_DIR',
205  'DELETE'))) {
206  $query = "INSERT INTO `actions` (type, parameters, creation, user) VALUES('$type', '$serialized', now(), '$user');";
207  } else {
208  return False;
209  }
210 
211  if (!$db->query($query)) {
212  $msg->raise("ERROR", "action", _("Error setting actions"));
213  return false;
214  }
215  return $this->do_action();
216  }
217 
218 
219  /**
220  * This seems to be unused ?
221  *
222  * @global m_messages $msg
223  * @global m_mysql $db
224  * @return boolean
225  */
226  function get_old() {
227  global $msg, $db;
228 
229  $purge = "select * from actions where TO_DAYS(curdate()) - TO_DAYS(creation) > 2;";
230  $result = $db->query($purge);
231  if (!$result) {
232  $msg->raise("ERROR", "action", _("Error selecting old actions"));
233  return false;
234  }
235  return $db->num_rows($result);
236  }
237 
238 
239  /**
240  *
241  * @global m_messages $msg
242  * @global m_mysql $db
243  * @param type $all
244  * @return boolean|int The number of rows purged; False is there was an error
245  */
246  function purge($all = null) {
247  global $msg, $db;
248  if (is_null($all)) {
249  $purge = "delete from actions where TO_DAYS(curdate()) - TO_DAYS(creation) > 2 and status = 0;";
250  } else {
251  $purge = "delete from actions where TO_DAYS(curdate()) - TO_DAYS(creation) > 2;";
252  }
253  $result = $db->query($purge);
254  if (!$result) {
255  $msg->raise("ERROR", "action", _("Error purging old actions"));
256  return false;
257  }
258  return $db->num_rows($result);
259  }
260 
261 
262  /**
263  * function returning the first not locked line of the action table
264  *
265  * @global m_mysql $db
266  * @return boolean or array
267  */
268  function get_action() {
269  global $db;
270 
271  $tab = array();
272  $db->query('select * from actions where end = 0 and begin = 0 order by id limit 1;');
273  if ($db->next_record()) {
274  $tab[] = $db->Record;
275  return $tab;
276  } else {
277  return false;
278  }
279  }
280 
281 
282  /**
283  * function locking an entry while it is being executed by the action script
284  *
285  * @global m_mysql $db
286  * @global m_messages $msg
287  * @param int $id
288  * @return boolean
289  */
290  function begin($id) {
291  global $db, $msg;
292  if (!$db->query("update actions set begin=now() where id= ? ;", array($id))) {
293  $msg->raise("ERROR", "action", _("Error locking the action : $id"));
294  return false;
295  }
296  return true;
297  }
298 
299 
300  /**
301  * function locking an entry while it is being executed by the action script
302  *
303  * @global m_mysql $db
304  * @global m_messages $msg
305  * @param int $id
306  * @param integer $return
307  * @return boolean
308  */
309  function finish($id, $return = 0) {
310  global $db, $msg;
311  if (!$db->query("update actions set end=now(),status=? where id= ?;", array($return, $id))) {
312  $msg->raise("ERROR", "action", _("Error unlocking the action : $id"));
313  return false;
314  }
315  return true;
316  }
317 
318 
319  /**
320  *
321  * @global m_mysql $db
322  * @global m_messages $msg
323  * @param int $id
324  * @return boolean
325  */
326  function reset_job($id) {
327  global $db, $msg;
328  if (!$db->query("update actions set end=0,begin=0,status='' where id= ?;", array($id))) {
329  $msg->raise("ERROR", "action", _("Error unlocking the action : $id"));
330  return false;
331  }
332  return true;
333  }
334 
335 
336  /**
337  * Returns a list of actions marked as executable and ready for execution
338  *
339  * @global m_mysql $db
340  * @global m_messages $msg
341  * @return boolean
342  */
343  function get_job() {
344  global $db;
345  $tab = array();
346  $db->query("Select * from actions where begin !=0 and end = 0 ;");
347  if ($db->next_record()) {
348  $tab[] = $db->Record;
349  return $tab;
350  } else {
351  return false;
352  }
353  }
354 
355 
356  /**
357  * function locking an entry while it is being executed by the action script
358  *
359  * @global m_mysql $db
360  * @param int $id
361  * @return boolean
362  */
363  function cancel($id) {
364  $this->finish($id, 666);
365  return true;
366  }
367 
368 } /* Class action */
369 
$query
Definition: 3.0.0~3.php:37
global $db
Definition: bootstrap.php:26
$msg
Definition: bootstrap.php:75
$cuid
Definition: bootstrap.php:43
$content
Definition: bro_editor.php:91
variable_get($name, $default=null, $createit_comment=null)
Return a persistent variable.
Definition: variables.php:85
This class manage actions to be performed on the file system on behalf of alternc Classes It primary ...
Definition: m_action.php:28
move($src, $dst, $user="root")
function returning the first not locked line of the action table
Definition: m_action.php:141
del($dir, $user="root")
function to delete file / folder
Definition: m_action.php:128
create_dir($dir, $user="root")
Plans the creation of a dir.
Definition: m_action.php:81
get_old()
This seems to be unused ?
Definition: m_action.php:226
chmod($filename, $perms, $user="root")
Plans the a chmod on file or dir.
Definition: m_action.php:69
begin($id)
function locking an entry while it is being executed by the action script
Definition: m_action.php:290
reset_job($id)
Definition: m_action.php:326
do_action()
Tell the incron that an action should be performed.
Definition: m_action.php:38
cancel($id)
function locking an entry while it is being executed by the action script
Definition: m_action.php:363
purge($all=null)
Definition: m_action.php:246
create_file($file, $content="", $user="root")
Plans a file creation.
Definition: m_action.php:56
fix_user($uid, $user="root")
Plans a perms fix upon user creation.
Definition: m_action.php:92
archive($archive, $dir="html")
function archiving a directory ( upon account deletion )
Definition: m_action.php:158
fix_dir($dir, $user="root")
Plans a dir permission fix.
Definition: m_action.php:104
fix_file($file, $user="root")
Plans a file permission fix.
Definition: m_action.php:116
get_action()
function returning the first not locked line of the action table
Definition: m_action.php:268
get_job()
Returns a list of actions marked as executable and ready for execution.
Definition: m_action.php:343
finish($id, $return=0)
function locking an entry while it is being executed by the action script
Definition: m_action.php:309
$uid
$user
Definition: bootstrap.php:84
if(!isset($is_include)) if(! $key &&! $crt) $id