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