forked from asmecher/backup
/
BackupPlugin.inc.php
125 lines (116 loc) · 4.09 KB
/
BackupPlugin.inc.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
<?php
/**
* @file BackupPlugin.inc.php
*
* Copyright (c) 2014-2021 Simon Fraser University
* Copyright (c) 2000-2021 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file LICENSE.
*
* @class BackupPlugin
* @brief Plugin to allow generation of a backup extract
*/
import('lib.pkp.classes.plugins.GenericPlugin');
class BackupPlugin extends GenericPlugin {
/**
* @copydoc Plugin::register()
*/
public function register($category, $path, $mainContextId = null) {
if (parent::register($category, $path, $mainContextId)) {
$this->addLocaleData();
return true;
}
return false;
}
/**
* Get the display name of this plugin
* @return string
*/
public function getDisplayName() {
return __('plugins.generic.backup.name');
}
/**
* Get the description of this plugin
* @return string
*/
public function getDescription() {
return __('plugins.generic.backup.description');
}
/**
* Designate this plugin as a site plugin
*/
public function isSitePlugin() {
return true;
}
/**
* @copydoc Plugin::getActions()
*/
public function getActions($request, $verb) {
$router = $request->getRouter();
import('lib.pkp.classes.linkAction.request.AjaxModal');
return array_merge(
$this->getEnabled()?array(
new LinkAction(
'backup',
new AjaxModal(
$router->url($request, null, null, 'manage', null, array('verb' => 'backup', 'plugin' => $this->getName(), 'category' => 'generic')),
__('plugins.generic.backup.link')
),
__('plugins.generic.backup.link'),
null
),
):array(),
parent::getActions($request, $verb)
);
}
/**
* @copydoc PKPPlugin::manage()
*/
public function manage($args, $request) {
$router = $request->getRouter();
switch ($request->getUserVar('verb')) {
case 'backup':
$templateMgr = TemplateManager::getManager($request);
$templateMgr->assign(array(
'pluginName' => $this->getName(),
'isDumpConfigured' => Config::getVar('cli', 'dump')!='',
'isTarConfigured' => Config::getVar('cli', 'tar')!='',
'errorMessage' => __('plugins.generic.backup.failure')
));
$output = $templateMgr->fetch($this->getTemplateResource('index.tpl'));
return new JSONMessage(true, $output);
case 'db':
$dumpTool = Config::getVar('cli', 'dump');
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename=db-' . strftime('%Y-%m-%d') . '.sql');
header('Content-Type: application/sql');
header('Content-Transfer-Encoding: binary');
passthru(strtr($dumpTool, array(
'{$hostname}' => escapeshellarg(Config::getVar('database', 'host')),
'{$username}' => escapeshellarg(Config::getVar('database', 'username')),
'{$password}' => escapeshellarg(Config::getVar('database', 'password')),
'{$databasename}' => escapeshellarg(Config::getVar('database', 'name')),
)), $returnValue);
if ($returnValue !== 0) header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
exit();
case 'files':
$tarTool = Config::getVar('cli', 'tar');
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename=files-' . strftime('%Y-%m-%d') . '.tar.gz');
header('Content-Type: application/gzip');
header('Content-Transfer-Encoding: binary');
passthru($tarTool . ' -c -f - -z ' . escapeshellarg(Config::getVar('files', 'files_dir')), $returnValue);
if ($returnValue !== 0) header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
exit();
case 'code':
$tarTool = Config::getVar('cli', 'tar');
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename=code-' . strftime('%Y-%m-%d') . '.tar.gz');
header('Content-Type: application/gzip');
header('Content-Transfer-Encoding: binary');
passthru($tarTool . ' -c -f - -z ' . escapeshellarg(dirname(dirname(dirname(dirname(__FILE__))))), $returnValue);
if ($returnValue !== 0) header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
exit();
}
return parent::manage($args, $request);
}
}