forked from vanilla/porter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.exportcontroller.php
executable file
·166 lines (143 loc) · 5.22 KB
/
class.exportcontroller.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
<?php
/**
* @copyright Vanilla Forums Inc. 2010-2015
* @license http://opensource.org/licenses/gpl-2.0.php GNU GPL2
* @package VanillaPorter
*/
/**
* Generic controller implemented by forum-specific ones.
*/
abstract class ExportController {
/** @var array Database connection info */
protected $DbInfo = array();
/** @var array Required tables, columns set per exporter */
protected $SourceTables = array();
/** @var ExportModel */
protected $Ex = null;
/** Forum-specific export routine */
abstract protected function ForumExport($Ex);
/**
* Construct and set the controller's properties from the posted form.
*/
public function __construct() {
$this->HandleInfoForm();
$this->Ex = new ExportModel;
$this->Ex->Controller = $this;
$this->Ex->SetConnection($this->DbInfo['dbhost'], $this->DbInfo['dbuser'], $this->DbInfo['dbpass'],
$this->DbInfo['dbname']);
$this->Ex->Prefix = $this->DbInfo['prefix'];
$this->Ex->Destination = $this->Param('dest', 'file');
$this->Ex->DestDb = $this->Param('destdb', null);
$this->Ex->TestMode = $this->Param('test', false);
/**
* Selective exports
* 1. Get the comma-separated list of tables and turn it into an array
* 2. Trim off the whitespace
* 3. Normalize case to lower
* 4. Save to the ExportModel instance
*/
$RestrictedTables = $this->Param('tables', false);
if (!empty($RestrictedTables)) {
$RestrictedTables = explode(',', $RestrictedTables);
if (is_array($RestrictedTables) && !empty($RestrictedTables)) {
$RestrictedTables = array_map('trim', $RestrictedTables);
$RestrictedTables = array_map('strtolower', $RestrictedTables);
$this->Ex->RestrictedTables = $RestrictedTables;
}
}
}
/**
* Set CDN file prefix if one is given.
*
* @return string
*/
public function CdnPrefix() {
$Cdn = rtrim($this->Param('cdn', ''), '/');
if ($Cdn) {
$Cdn .= '/';
}
return $Cdn;
}
/**
* Logic for export process.
*/
public function DoExport() {
global $Supported;
// Test connection
$Msg = $this->TestDatabase();
if ($Msg === true) {
// Test src tables' existence structure
$Msg = $this->Ex->VerifySource($this->SourceTables);
if ($Msg === true) {
// Good src tables - Start dump
$this->Ex->UseCompression(true);
$this->Ex->FilenamePrefix = $this->DbInfo['dbname'];
set_time_limit(60 * 60);
// ob_start();
$this->ForumExport($this->Ex);
// $Errors = ob_get_clean();
$Msg = $this->Ex->Comments;
// Write the results. Send no path if we don't know where it went.
$RelativePath = ($this->Param('destpath', false)) ? false : $this->Ex->Path;
ViewExportResult($Msg, 'Info', $RelativePath);
} else {
ViewForm(array('Supported' => $Supported, 'Msg' => $Msg, 'Info' => $this->DbInfo));
} // Back to form with error
} else {
ViewForm(array('Supported' => $Supported, 'Msg' => $Msg, 'Info' => $this->DbInfo));
} // Back to form with error
}
/**
* User submitted db connection info.
*/
public function HandleInfoForm() {
$this->DbInfo = array(
'dbhost' => $_POST['dbhost'],
'dbuser' => $_POST['dbuser'],
'dbpass' => $_POST['dbpass'],
'dbname' => $_POST['dbname'],
'type' => $_POST['type'],
'prefix' => preg_replace('/[^A-Za-z0-9_-]/', '', $_POST['prefix'])
);
}
/**
* Retrieve a parameter passed to the export process.
*
* @param string $Name
* @param mixed $Default Fallback value.
* @return mixed Value of the parameter.
*/
public function Param($Name, $Default = false) {
if (isset($_POST[$Name])) {
return $_POST[$Name];
} elseif (isset($_GET[$Name])) {
return $_GET[$Name];
} else {
return $Default;
}
}
/**
* Test database connection info.
*
* @return string|bool True on success, message on failure.
*/
public function TestDatabase() {
// Connection
if (!function_exists('mysql_connect')) {
$Result = 'mysql_connect is an undefined function. Verify MySQL extension is installed and enabled.';
} elseif ($C = @mysql_connect($this->DbInfo['dbhost'], $this->DbInfo['dbuser'], $this->DbInfo['dbpass'])) {
// Database
if (mysql_select_db($this->DbInfo['dbname'], $C)) {
mysql_close($C);
$Result = true;
} else {
mysql_close($C);
$Result = "Could not find database '{$this->DbInfo['dbname']}'.";
}
} else {
$Result = 'Could not connect to ' . $this->DbInfo['dbhost'] . ' as ' . $this->DbInfo['dbuser'] . ' with given password.';
}
return $Result;
}
}
?>