/
lib.php
398 lines (362 loc) · 14.4 KB
/
lib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
<?php
/**
* Mahara: Electronic portfolio, weblog, resume builder and social networking
* Copyright (C) 2006-2009 Catalyst IT Ltd and others; see:
* http://wiki.mahara.org/Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package mahara
* @subpackage export
* @author Catalyst IT Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL
* @copyright (C) 2006-2009 Catalyst IT Ltd http://catalyst.net.nz
*
*/
defined('INTERNAL') || die();
require_once('view.php');
require_once(get_config('docroot') . '/artefact/lib.php');
/**
* Base class for all Export plugins.
*
* This class does some basic setup for export plugins, as well as interfacing
* with the Mahara Plugin API. Mostly, the work of generating exports is
* delegated to the plugins themselves.
*
* TODO: split generation of an archive file from the export() method,
* implement zipping the export in a method in this class to reduce
* duplication.
*/
abstract class PluginExport extends Plugin {
/**
* Export all views owned by this user
*/
const EXPORT_ALL_VIEWS = -1;
/**
* Export only certain views - used internally when a list of views is
* passed to the constructor
*/
const EXPORT_LIST_OF_VIEWS = -2;
/**
* Export all artefacts owned by this user
*/
const EXPORT_ALL_ARTEFACTS = -3;
/**
* Export artefacts that are part of the views to be exported
*/
const EXPORT_ARTEFACTS_FOR_VIEWS = -4;
/**
* Export only certain artefacts - used internally when a list of artefacts
* is passed to the constructor
*/
const EXPORT_LIST_OF_ARTEFACTS = -5;
/**
* A human-readable title for the export
*/
abstract public static function get_title();
/**
* A human-readable description for the export
*/
abstract public static function get_description();
/**
* Perform the export and return the path to the resulting file.
*
* @return string path to the resulting file (relative to dataroot)
*/
abstract public function export();
// MAIN CLASS DEFINITION
/**
* List of artefacts to export. Set up by constructor.
*/
public $artefacts = array();
/**
* List of views to export. Set up by constructor.
*/
public $views = array();
/**
* User object for the user being exported.
*/
protected $user;
/**
* Represents the mode for exporting views - one of the class consts
* defined above
*/
protected $viewexportmode;
/**
* Represents the mode for exporting artefacts - one of the class consts
* defined above
*/
protected $artefactexportmode;
/**
* The time the export was generated.
*
* Technically, this is the time at which the export object was created,
* not the time at which export() was called.
*/
protected $exporttime;
/**
* Callback to notify when progress is made
*/
private $progresscallback = null;
/**
* Establishes exactly what views and artefacts are to be exported, and
* sets up temporary export directories
*
* Subclasses can override this if they need to do anything else, but
* they must call parent::__construct.
*
* @param User $user The user to export data for
* @param mixed $views can be:
* - PluginExport::EXPORT_ALL_VIEWS
* - array, containing:
* - int - view ids
* - stdclass objects - db rows
* - View objects
* @param mixed $artefacts can be:
* - PluginExport::EXPORT_ALL_ARTEFACTS
* - PluginExport::EXPORT_ARTEFACTS_FOR_VIEWS
* - array, containing:
* - int - artefact ids
* - stdclass objects - db rows
* - ArtefactType subclasses
*/
public function __construct(User $user, $views, $artefacts, $progresscallback=null) {
if (!is_null($progresscallback)) {
if (is_callable($progresscallback)) {
$this->progresscallback = $progresscallback;
}
else {
throw new SystemException("The specified progress callback isn't callable");
}
}
$this->notify_progress_callback(0, 'Starting');
$this->exporttime = time();
$this->user = $user;
$userid = $this->user->get('id');
$tmpviews = array();
$tmpartefacts = array();
// Get the list of views to export
if ($views == self::EXPORT_ALL_VIEWS) {
$tmpviews = get_column_sql('SELECT id FROM {view} WHERE owner = ? ORDER BY id', array($userid));
$this->viewexportmode = $views;
}
else if (is_array($views)) {
$tmpviews = $views;
$this->viewexportmode = self::EXPORT_LIST_OF_VIEWS;
}
foreach ($tmpviews as $v) {
$view = null;
if ($v instanceof View) {
$view = $v;
}
else if (is_object($v)) {
$view = new View($v->id, $v);
}
else if (is_numeric($v)) {
$view = new View($v);
}
if (is_null($view)) {
throw new ParamOutOfRangeException("Invalid view $v");
}
if ($view->get('owner') != $userid) {
throw new UserException("User $userid does not own view " . $view->get('id'));
}
$this->views[$view->get('id')] = $view;
}
// Get the list of artefacts to export
if ($artefacts == self::EXPORT_ALL_ARTEFACTS) {
$tmpartefacts = get_column_sql('SELECT id
FROM {artefact}
WHERE "owner" = ?
UNION
SELECT artefact
FROM {view_artefact}
WHERE "view" IN (SELECT id FROM {view} WHERE "owner" = ?)
ORDER BY id', array($userid, $userid));
$this->artefactexportmode = $artefacts;
}
else {
if ($tmpviews) {
$sql = "SELECT DISTINCT va.artefact
FROM {view_artefact} va
LEFT JOIN {view} v ON v.id = va.view
WHERE v.owner = ?
AND va.view IN ( " . implode(',', array_keys($this->views)) . ")
ORDER BY va.artefact";
$tmpartefacts = (array)get_column_sql($sql, array($userid));
// Some artefacts are not inside the view, but still need to be exported with it
$tmpartefacts = array_unique(array_merge($tmpartefacts, $this->get_view_extra_artefacts()));
$tmpartefacts = artefact_get_descendants($tmpartefacts);
$tmpartefacts = array_unique(array_merge($tmpartefacts, $this->get_artefact_extra_artefacts($tmpartefacts)));
}
if ($artefacts == self::EXPORT_ARTEFACTS_FOR_VIEWS) {
$this->artefactexportmode = $artefacts;
}
else {
$tmpartefacts = array_unique(array_merge($tmpartefacts, $artefacts));
$this->artefactexportmode = self::EXPORT_LIST_OF_ARTEFACTS;
}
}
$typestoplugins = get_records_assoc('artefact_installed_type');
foreach ($tmpartefacts as $a) {
$artefact = null;
if ($a instanceof ArefactType) {
$artefact = $a;
}
else if (is_object($a) && isset($a->id)) {
$artefact = artefact_instance_from_id($a->id);
}
else if (is_numeric($a)) {
$artefact = artefact_instance_from_id($a);
}
if (is_null($artefact)) {
throw new ParamOutOfRangeException("Invalid artefact $a");
}
// This check won't work, at the _least_ because at the time of
// writing, can_view_artefact does not support normal users viewing
// site files. This check is also pretty damn slow. So think twice
// before uncommenting it. I presume if you _are_ uncommenting it,
// it's because you're trying to isloate a security vulnerability
// where a user can export another user's files or something. In
// which case you'll be being careful anyway, I hope.
//if (!$this->user->can_view_artefact($artefact)) {
// throw new SystemException("User $userid does not own artefact " . $artefact->get('id'));
//}
if ($artefact->exportable()) {
$this->artefacts[$artefact->get('id')] = $artefact;
}
}
$this->collections = array();
$collections = get_records_sql_assoc('
SELECT * FROM {collection} WHERE id IN (
SELECT collection
FROM {collection_view}
WHERE view IN (' . join(',', array_keys($this->views)) . ')
)',
array()
);
if ($collections) {
require_once('collection.php');
foreach ($collections as &$c) {
$this->collections[$c->id] = new Collection(0, $c);
}
}
// Now set up the temporary export directories
$this->exportdir = get_config('dataroot')
. 'export/'
. $this->user->get('id') . '/'
. $this->exporttime . '/';
if (!check_dir_exists($this->exportdir)) {
throw new SystemException("Couldn't create the temporary export directory $this->exportdir");
}
$this->notify_progress_callback(10, 'Setup');
}
/**
* Accessor
*
* @param string $field The field to get (see the class definition to find
* which fields are available)
*/
public function get($field) {
if (!property_exists($this, $field)) {
throw new ParamOutOfRangeException("Field $field wasn't found in class " . get_class($this));
}
return $this->{$field};
}
/**
* Notifies the registered progress callback about the progress in generating the export.
*
* This is provided as exports can take a long time to generate. Export
* plugins are encouraged to call this at least after performing some major
* operation, and should always call it saying when the execution of
* export() is done. However, it is unnecessary to call it too often.
*
* For testing purposes, you may find it useful to register a progress
* callback that simply log_debug()s the data, so you can check that the
* percentage is always increasing, for example.
*
* @param int $percent The total percentage of the way through generating
* the export. The base class constructor hands over
* control claiming 10% of the work is done.
* @param string $status A string describing the current status of the
* export - e.g. 'Exporting Artefact (20/75)'
*/
protected function notify_progress_callback($percent, $status) {
if ($this->progresscallback) {
call_user_func_array($this->progresscallback, array(
$percent, $status
));
}
}
/**
* Artefact plugins can specify additional artefacts required for view export
*/
protected function get_view_extra_artefacts() {
$extra = array();
$plugins = plugins_installed('artefact');
foreach ($plugins as &$plugin) {
safe_require('artefact', $plugin->name);
$classname = generate_class_name('artefact', $plugin->name);
if (is_callable($classname . '::view_export_extra_artefacts')) {
if ($artefacts = call_static_method($classname, 'view_export_extra_artefacts', array_keys($this->views))) {
$extra = array_unique(array_merge($extra, $artefacts));
}
}
}
return $extra;
}
protected function get_artefact_extra_artefacts(&$artefactids) {
if (empty($artefactids)) {
return array();
}
$extra = array();
$plugins = plugins_installed('artefact');
foreach ($plugins as &$plugin) {
safe_require('artefact', $plugin->name);
$classname = generate_class_name('artefact', $plugin->name);
if (is_callable($classname . '::artefact_export_extra_artefacts')) {
if ($artefacts = call_static_method($classname, 'artefact_export_extra_artefacts', $artefactids)) {
$extra = array_unique(array_merge($extra, $artefacts));
}
}
}
return $extra;
}
}
/**
* Looks in the export staging area in dataroot and deletes old, unneeded
* exports.
*/
function export_cleanup_old_exports() {
require_once('file.php');
$basedir = get_config('dataroot') . 'export/';
$exportdir = new DirectoryIterator($basedir);
$mintime = time() - (12 * 60 * 60); // delete exports older than 12 hours
// The export dir contains one directory for each user who has created
// an export, named after their UID
foreach ($exportdir as $userdir) {
if ($userdir->isDot()) continue;
// Each user's directory contains one directory for each export
// they made, named as the unix timestamp of the time they
// generated it
$udir = new DirectoryIterator($basedir . $userdir->getFilename());
foreach ($udir as $dir) {
if ($dir->isDot()) continue;
if ($dir->getCTime() < $mintime) {
rmdirr($basedir . $userdir->getFilename() . '/' . $dir->getFilename());
}
}
}
}
?>