m_action.php 10.5 KB
Newer Older
1
<?php
alban's avatar
alban committed
2

3
/*
alban's avatar
alban committed
4
5
6
7
8
9
  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.
10

alban's avatar
alban committed
11
12
13
14
  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.
15

alban's avatar
alban committed
16
17
18
19
20
21
  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.
  ----------------------------------------------------------------------
 */
22
23

/**
alban's avatar
alban committed
24
25
26
27
28
29
30
31
32
 * 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/
 * 
 */
33
class m_action {
alban's avatar
alban committed
34
    /* --------------------------------------------------------------------------- */
Steven Mondji-Lerider's avatar
Steven Mondji-Lerider committed
35

36
37
    /** 
     * Constructor
alban's avatar
alban committed
38
39
40
41
     */
    function m_action() {
        
    }
Steven Mondji-Lerider's avatar
Steven Mondji-Lerider committed
42

alban's avatar
alban committed
43
44
45
    /**
     * Plans the cration of a file 
     * 
46
47
     * @global \m_err $err
     * @global string $L_INOTIFY_DO_ACTION
alban's avatar
alban committed
48
49
50
51
52
53
54
55
56
57
     * @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;
    }
Steven Mondji-Lerider's avatar
Steven Mondji-Lerider committed
58

alban's avatar
alban committed
59
60
61
    /**
     * Plans a file creation
     * 
62
     * @param string  $file
alban's avatar
alban committed
63
64
65
66
67
68
69
     * @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));
    }
Steven Mondji-Lerider's avatar
Steven Mondji-Lerider committed
70

alban's avatar
alban committed
71
72
73
74
75
76
77
78
79
80
    /**
     * 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));
    }
81

alban's avatar
alban committed
82
83
84
85
86
87
88
89
90
    /**
     * 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));
    }
Steven Mondji-Lerider's avatar
Steven Mondji-Lerider committed
91

alban's avatar
alban committed
92
93
94
    /**
     * Plans a dir fix
     * 
95
96
     * @param string $dir
     * @param m_user $user
fufroma's avatar
fufroma committed
97
     * @return boolean
alban's avatar
alban committed
98
99
100
101
     */
    function fix_dir($dir, $user = "root") {
        return $this->set('fix_dir', $user, array('dir' => $dir));
    }
Steven Mondji-Lerider's avatar
Steven Mondji-Lerider committed
102

alban's avatar
alban committed
103
104
105
    /**
     * Plans a file fix
     * 
106
107
     * @param string $file
     * @param m_user $user
fufroma's avatar
fufroma committed
108
     * @return boolean
alban's avatar
alban committed
109
110
111
112
     */
    function fix_file($file, $user = "root") {
        return $this->set('fix_file', $user, array('file' => $file));
    }
113

alban's avatar
alban committed
114
115
116
    /**
     * function to delete file / folder
     * 
117
118
     * @param string $dir
     * @param m_user $user
fufroma's avatar
fufroma committed
119
     * @return boolean
alban's avatar
alban committed
120
121
122
     */
    function del($dir, $user = "root") {
        return $this->set('delete', $user, array('dir' => $dir));
123
    }
alban's avatar
alban committed
124
125
126
127

    /**
     * function returning the first not locked line of the action table 
     * 
128
129
130
     * @param string $src
     * @param string $dst
     * @param m_user $user
fufroma's avatar
fufroma committed
131
     * @return boolean
alban's avatar
alban committed
132
133
134
     */
    function move($src, $dst, $user = "root") {
        return $this->set('move', $user, array('src' => $src, 'dst' => $dst));
135
136
    }

alban's avatar
alban committed
137
138
139
140
    /**
     * 
     * function archiving a directory ( upon account deletion )
     * 
141
142
143
144
     * @global int $cuid
     * @global m_mysql $db
     * @global m_err $err
     * @param string $archive Directory to archive within the archive_del_data folder if set in variable sql table
alban's avatar
alban committed
145
     *                      If archive_del_data is not set we delete the folder
146
     * @param string $dir  sub_directory of the archive directory
alban's avatar
alban committed
147
148
149
150
     * @return boolean
     */
    function archive($archive, $dir = "html") {
        global $cuid, $db, $err;
Steven Mondji-Lerider's avatar
Steven Mondji-Lerider committed
151

alban's avatar
alban committed
152
153
154
155
156
157
158
159
160
161
162
163
164
        $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"];
Steven Mondji-Lerider's avatar
Steven Mondji-Lerider committed
165

alban's avatar
alban committed
166
167
168
169
170
        //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;
Steven Mondji-Lerider's avatar
Steven Mondji-Lerider committed
171
172
    }

alban's avatar
alban committed
173
174
175
    /**
     * function inserting the action in the sql table
     * 
176
177
178
179
180
     * @global m_mysql $db
     * @global m_err $err
     * @param string $type
     * @param m_user $user
     * @param mixed $parameters
alban's avatar
alban committed
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
     * @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;
219
    }
220

alban's avatar
alban committed
221
222
223
    /**
     * This seems to be unused ?
     * 
224
225
     * @global m_err $err
     * @global m_mysql $db
alban's avatar
alban committed
226
227
228
229
230
     * @param type $all
     * @return boolean
     */
    function get_old($all = null) {
        global $err, $db;
231

alban's avatar
alban committed
232
233
234
235
236
237
238
        $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)   ;
239
    }
Steven Mondji-Lerider's avatar
Steven Mondji-Lerider committed
240

alban's avatar
alban committed
241
242
    /**
     * 
243
244
     * @global m_err $err
     * @global m_mysql $db
alban's avatar
alban committed
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
     * @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)   ;
        
262
    }
Steven Mondji-Lerider's avatar
Steven Mondji-Lerider committed
263

alban's avatar
alban committed
264
265
266
    /**
     *  function returning the first not locked line of the action table 
     * 
267
268
     * @global m_mysql $db
     * @global m_err $err
alban's avatar
alban committed
269
270
271
272
273
274
275
276
277
278
279
280
281
     * @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;
        }
282
    }
283

alban's avatar
alban committed
284
285
286
    /**
     * function locking an entry while it is being executed by the action script
     * 
287
288
289
     * @global m_mysql $db
     * @global m_err $err
     * @param int $id
alban's avatar
alban committed
290
291
292
293
294
295
296
297
298
     * @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;
299
300
    }

alban's avatar
alban committed
301
302
303
    /**
     *  function locking an entry while it is being executed by the action script
     * 
304
305
306
     * @global m_mysql $db
     * @global m_err $err
     * @param int $id
fufroma's avatar
fufroma committed
307
     * @param integer $return
alban's avatar
alban committed
308
309
310
311
312
313
314
315
316
317
318
319
320
     * @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;
    }
    
    /**
     * 
321
322
323
     * @global m_mysql $db
     * @global m_err $err
     * @param int $id
alban's avatar
alban committed
324
325
326
327
328
329
330
331
332
333
334
335
336
337
     * @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
     * 
338
339
     * @global m_mysql $db
     * @global m_err $err
alban's avatar
alban committed
340
341
342
343
344
345
346
347
348
349
350
351
     * @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;
        }
352
    }
Steven Mondji-Lerider's avatar
Steven Mondji-Lerider committed
353

alban's avatar
alban committed
354
355
356
    /**
     *  function locking an entry while it is being executed by the action script
     * 
357
358
     * @global m_mysql $db
     * @param int $id
alban's avatar
alban committed
359
360
361
362
363
364
365
     * @return boolean
     */
    function cancel($id) {
        global $db;
        $this->finish($id, 666);
        return true;
    }
366

alban's avatar
alban committed
367
}
368

alban's avatar
alban committed
369
/* Class action */