/
DataExchangeController.php
104 lines (91 loc) · 2.96 KB
/
DataExchangeController.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
<?php
/**
* Copyright 2017 Christoph M. Becker
*
* This file is part of Realblog_XH.
*
* Realblog_XH 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.
*
* Realblog_XH 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 Realblog_XH. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Realblog;
class DataExchangeController extends AbstractController
{
/**
* @return string
*/
public function defaultAction()
{
global $sn, $plugin_tx;
$view = new View('data-exchange');
$view->csrfToken = $this->getCsrfToken();
$view->url = "$sn?realblog";
$view->articleCount = Finder::countArticlesWithStatus(array(0, 1, 2));
$filename = $this->getCsvFilename();
if (file_exists($filename)) {
$view->filename = $filename;
$view->filemtime = date('c', filemtime($filename));
}
$view->confirmImport = json_encode($plugin_tx['realblog']['exchange_confirm_import']);
return $view->render();
}
public function exportToCsvAction()
{
$this->checkCsrfToken();
if (DB::exportToCsv($this->getCsvFilename())) {
$this->redirectToDefault();
} else {
return "<h1>Realblog – {$this->text['exchange_heading']}</h1>"
. XH_message('fail', $this->text['exchange_export_failure'], $this->getCsvFilename());
}
}
public function importFromCsvAction()
{
$this->checkCsrfToken();
if (DB::importFromCsv($this->getCsvFilename())) {
$this->redirectToDefault();
} else {
return "<h1>Realblog – {$this->text['exchange_heading']}</h1>"
. XH_message('fail', $this->text['exchange_import_failure'], $this->getCsvFilename());
}
}
/**
* @return string
*/
private function getCsvFilename()
{
global $pth;
return "{$pth['folder']['content']}realblog/realblog.csv";
}
/**
* @return string
*/
private function getCsrfToken()
{
global $_XH_csrfProtection;
$html = $_XH_csrfProtection->tokenInput();
if (preg_match('/value="([0-9a-f]+)"/', $html, $matches)) {
return $matches[1];
}
}
private function checkCsrfToken()
{
global $_XH_csrfProtection;
$_XH_csrfProtection->check();
}
private function redirectToDefault()
{
$url = CMSIMPLE_URL . "?&realblog&admin=data_exchange";
header("Location: $url", true, 303);
exit;
}
}