-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaction.php
232 lines (178 loc) · 6.64 KB
/
action.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
<?php
/**
* DokuWiki Plugin autobackup (Action Component)
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Robert McLeod <[email protected]>
*/
// must be run within Dokuwiki
if (!defined('DOKU_INC')) die();
if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
// Custom constants
if (!defined('DOKU_PLUGIN_IMAGES')) define('DOKU_PLUGIN_IMAGES',DOKU_BASE.'lib/plugins/autobackup/images/');
if (!defined('DOKU_DATA')) define('DOKU_DATA', "/var/lib/dokuwiki/data/");
if (!defined('AUTOBACKUP_PLUGIN')) define('AUTOBACKUP_PLUGIN', DOKU_PLUGIN.'autobackup/');
require_once DOKU_PLUGIN.'action.php';
require_once AUTOBACKUP_PLUGIN.'lib/Dropbox.php';
class action_plugin_autobackup extends DokuWiki_Action_Plugin {
private $user;
/**
* Register hooks from dokuwiki
*/
public function register(Doku_Event_Handler &$controller) {
$controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_action_act_preprocess');
$controller->register_hook('TPL_CONTENT_DISPLAY', 'BEFORE', $this, 'handle_tpl_content_display');
$controller->register_hook('TPL_ACT_UNKNOWN', 'BEFORE', $this, 'handle_tpl_act_unknown');
$controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax_call_unknown');
}
public function handle_action_act_preprocess(Doku_Event &$event, $param) {
$this->_set_user();
global $JSINFO;
$JSINFO['user'] = $this->user;
switch ( $event->data ) {
case "memories":
if ( $this->user == "unknown" ) {
send_redirect("/doku.php?do=login");
$event->preventDefault();
$event->stopPropagation();
return;
}
$event->preventDefault();
break;
case "restore":
if ( $this->user == "unknown" ) {
send_redirect("/doku.php?do=login");
$event->preventDefault();
$event->stopPropagation();
return;
}
$event->preventDefault();
break;
default:
return;
break;
}
}
public function handle_ajax_call_unknown(Doku_Event &$event, $param) {
$this->_set_user();
$event->preventDefault();
$event->stopPropagation();
$json = new StdClass;
switch ( $event->data ) {
case "dropbox.enable":
$result = Dropbox::enable_for( $this->user );
$json->message = ( $result === true )
? "You have been queued to have Dropbox installed on your account, you should get an email detailing the next steps within 10 minutes"
: "$result. Please contact your deployment manager";
break;
case "dropbox.disable":
$json->message = Dropbox::disable_for( $this->user );
break;
case "restore.memory":
$json = $this->_restore_memory();
break;
default:
$json->message = "Unsupported request";
break;
}
echo json_encode($json);
}
public function handle_tpl_content_display(Doku_Event &$event, $param) {
}
public function handle_tpl_act_unknown(Doku_Event &$event, $param) {
global $INPUT;
$this->_set_user();
try {
switch ( $event->data ) {
case "memories":
echo "<h2>Memories</h2>";
$this->_show_backup_options();
$this->_show_memories();
$event->preventDefault();
break;
case "restore":
$this->_do_restore();
$event->preventDefault();
break;
default:
return;
break;
}
} catch ( Exception $e ) {
echo $e->getMessage();
}
}
private function _set_user() {
$session = reset( $_SESSION ); // gives the first element of the array
$user = $session["auth"]["user"];
if ( is_null( $user ) )
return $this->user = "unknown";
return $this->user = $user;
}
/**
* Prints out the backup options
*/
private function _show_backup_options() {
$dropbox_status = Dropbox::status_for( $this->user );
$dropbox_button = Dropbox::generate_button( $this->user );
include AUTOBACKUP_PLUGIN."inc/backup_options.php"; # TODO: not this
}
private function _show_memories() {
$memory_list = "/home/{$this->user}/memories.list";
$backups = array();
if ( file_exists($memory_list) )
$backups = json_decode( file_get_contents( $memory_list ) );
$current = new StdClass;
$current->date = "Current";
$current->source = "Dokuwiki";
array_unshift( $backups, $current );
include AUTOBACKUP_PLUGIN."inc/memories.php"; # TODO: not this
}
private function _do_restore() {
$username = $this->user;
include AUTOBACKUP_PLUGIN."inc/restore.php"; # TODO: not this
}
private function _restore_memory() {
$username = $this->user;
$source = $_POST['source'];
$timestamp = stripslashes(trim($_POST['timestamp']));
$json = new StdClass;
// Try to extract and link the wiki
try {
// get current timestamp
$cmd = "braincase-wiki-switcher $username";
exec($cmd, $out, $ret);
$current_timestamp = trim($out[0]);
// Check if we need to do a restore
if ( $current_timestamp != $timestamp
&& !file_exists("/home/$username/.dokuwiki/data.$timestamp") ) {
// Restore the backup requested
$cmd = "braincase-restore $username $source $timestamp dokuwiki";
exec($cmd, $out, $ret);
if ( $ret != 0 ) {
$out = implode("\n", $out);
throw new Exception("Failed to restore the timestamp.\n$ $cmd\n$out\nReturned $ret");
}
}
// Check if we need to do a switch
if ( $current_timestamp != $timestamp ) {
// Setup the dokuwiki links
$cmd = "braincase-wiki-switcher $username $timestamp";
exec($cmd, $out, $ret);
if ( $ret != 0 ) {
$out = implode("\n", $out);
throw new Exception("Failed to switch timestamps.\n$ $cmd\n$out\nReturned $ret");
}
}
$json->error = 0;
$json->message = "Successfully restored the Dokuwiki contents from $timestamp";
} catch ( Exception $e ) {
$json->error = 1;
$json->error_output = $e->getMessage();
}
return $json;
}
}
// vim:ts=4:sw=4:et: