1
|
|
|
<?php namespace EvolutionCMS\Legacy; |
2
|
|
|
|
3
|
|
|
use EvolutionCMS\Interfaces\ExportSiteInerface; |
4
|
|
|
|
5
|
|
|
class ExportSite implements ExportSiteInerface |
|
|
|
|
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var string |
9
|
|
|
*/ |
10
|
|
|
public $targetDir; |
11
|
|
|
/** |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
public $generate_mode; |
15
|
|
|
/** |
16
|
|
|
* @var |
17
|
|
|
*/ |
18
|
|
|
public $total; |
19
|
|
|
/** |
20
|
|
|
* @var int |
21
|
|
|
*/ |
22
|
|
|
public $count; |
23
|
|
|
/** |
24
|
|
|
* @var |
25
|
|
|
*/ |
26
|
|
|
public $ignore_ids; |
27
|
|
|
/** |
28
|
|
|
* @var array|mixed |
29
|
|
|
*/ |
30
|
|
|
public $exportstart; |
31
|
|
|
/** |
32
|
|
|
* @var |
33
|
|
|
*/ |
34
|
|
|
public $repl_before; |
35
|
|
|
/** |
36
|
|
|
* @var |
37
|
|
|
*/ |
38
|
|
|
public $repl_after; |
39
|
|
|
/** |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
public $output = array(); |
43
|
|
|
/** |
44
|
|
|
* @var int |
45
|
|
|
*/ |
46
|
|
|
public $dirCheckCount = 0; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* EXPORT_SITE constructor. |
50
|
|
|
*/ |
51
|
|
|
public function __construct() |
52
|
|
|
{ |
53
|
|
|
$modx = evolutionCMS(); |
54
|
|
|
|
55
|
|
|
if (!defined('MODX_BASE_PATH')) { |
56
|
|
|
return false; |
|
|
|
|
57
|
|
|
} |
58
|
|
|
$this->exportstart = $this->get_mtime(); |
59
|
|
|
$this->count = 0; |
60
|
|
|
$this->setUrlMode(); |
61
|
|
|
$this->generate_mode = 'crawl'; |
62
|
|
|
$this->targetDir = $modx->config['base_path'] . 'temp/export'; |
63
|
|
|
if (!isset($this->total)) { |
64
|
|
|
$this->getTotal(); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string $dir |
70
|
|
|
*/ |
71
|
|
|
public function setExportDir($dir) |
72
|
|
|
{ |
73
|
|
|
$dir = str_replace('\\', '/', $dir); |
74
|
|
|
$dir = rtrim($dir, '/'); |
75
|
|
|
$this->targetDir = $dir; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return int |
80
|
|
|
*/ |
81
|
|
|
public function get_mtime() |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
$mtime = microtime(); |
84
|
|
|
$mtime = explode(' ', $mtime); |
85
|
|
|
$mtime = $mtime[1] + $mtime[0]; |
86
|
|
|
|
87
|
|
|
return $mtime; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return void |
92
|
|
|
*/ |
93
|
|
|
public function setUrlMode() |
94
|
|
|
{ |
95
|
|
|
$modx = evolutionCMS(); |
96
|
|
|
|
97
|
|
|
if ($modx->config['friendly_urls'] == 0) { |
98
|
|
|
$modx->config['friendly_urls'] = 1; |
99
|
|
|
$modx->config['use_alias_path'] = 1; |
100
|
|
|
$modx->clearCache('full'); |
101
|
|
|
} |
102
|
|
|
$modx->config['make_folders'] = '1'; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param string|array $ignore_ids |
107
|
|
|
* @param string|int|bool $noncache |
108
|
|
|
* @return int |
109
|
|
|
*/ |
110
|
|
|
public function getTotal($ignore_ids = '', $noncache = '0') |
|
|
|
|
111
|
|
|
{ |
112
|
|
|
$modx = evolutionCMS(); |
113
|
|
|
$tbl_site_content = $modx->getFullTableName('site_content'); |
114
|
|
|
|
115
|
|
|
$ignore_ids = array_filter(array_map('intval', explode(',', $ignore_ids))); |
116
|
|
|
if (count($ignore_ids) > 0) { |
117
|
|
|
$ignore_ids = "AND NOT id IN ('" . implode("','", $ignore_ids) . "')"; |
118
|
|
|
} else { |
119
|
|
|
$ignore_ids = ''; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$this->ignore_ids = $ignore_ids; |
123
|
|
|
|
124
|
|
|
$noncache = ($noncache == 1) ? '' : 'AND cacheable=1'; |
125
|
|
|
$where = "deleted=0 AND ((published=1 AND type='document') OR (isfolder=1)) {$noncache} {$ignore_ids}"; |
126
|
|
|
$rs = $modx->getDatabase()->select('count(id)', $tbl_site_content, $where); |
|
|
|
|
127
|
|
|
$this->total = (int)$modx->getDatabase()->getValue($rs); |
|
|
|
|
128
|
|
|
|
129
|
|
|
return $this->total; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param string $directory |
134
|
|
|
* @return bool |
135
|
|
|
*/ |
136
|
|
|
public function removeDirectoryAll($directory = '') |
137
|
|
|
{ |
138
|
|
|
$rs = false; |
|
|
|
|
139
|
|
|
if (empty($directory)) { |
140
|
|
|
$directory = $this->targetDir; |
141
|
|
|
} |
142
|
|
|
$directory = rtrim($directory, '/'); |
143
|
|
|
// if the path is not valid or is not a directory ... |
144
|
|
|
if (empty($directory)) { |
145
|
|
|
return false; |
146
|
|
|
} |
147
|
|
|
if (strpos($directory, MODX_BASE_PATH) === false) { |
148
|
|
|
return $rs; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
if (!is_dir($directory)) { |
152
|
|
|
return $rs; |
153
|
|
|
} elseif (!is_readable($directory)) { |
154
|
|
|
return $rs; |
155
|
|
|
} else { |
156
|
|
|
$files = glob($directory . '/*'); |
157
|
|
|
if (!empty($files)) { |
158
|
|
|
foreach ($files as $path) { |
159
|
|
|
$rs = is_dir($path) ? $this->removeDirectoryAll($path) : unlink($path); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
if ($directory !== $this->targetDir) { |
164
|
|
|
$rs = rmdir($directory); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return $rs; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param int $docid |
172
|
|
|
* @param string $filepath |
173
|
|
|
* @return string |
174
|
|
|
*/ |
175
|
|
|
public function makeFile($docid, $filepath) |
176
|
|
|
{ |
177
|
|
|
$modx = evolutionCMS(); global $_lang; |
|
|
|
|
178
|
|
|
$file_permission = octdec($modx->config['new_file_permissions']); |
179
|
|
|
if ($this->generate_mode === 'direct') { |
180
|
|
|
$back_lang = $_lang; |
181
|
|
|
$src = $modx->executeParser($docid); |
|
|
|
|
182
|
|
|
|
183
|
|
|
$_lang = $back_lang; |
184
|
|
|
} else { |
185
|
|
|
$src = $this->curl_get_contents(MODX_SITE_URL . "index.php?id={$docid}"); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
|
189
|
|
|
if ($src !== false) { |
190
|
|
|
if ($this->repl_before !== $this->repl_after) { |
191
|
|
|
$src = str_replace($this->repl_before, $this->repl_after, $src); |
192
|
|
|
} |
193
|
|
|
$result = file_put_contents($filepath, $src); |
194
|
|
|
if ($result !== false) { |
195
|
|
|
@chmod($filepath, $file_permission); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
if ($result !== false) { |
199
|
|
|
return 'success'; |
200
|
|
|
} else { |
201
|
|
|
return 'failed_no_write'; |
202
|
|
|
} |
203
|
|
|
} else { |
204
|
|
|
return 'failed_no_retrieve'; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @param int $docid |
210
|
|
|
* @param string $alias |
211
|
|
|
* @param string $prefix |
212
|
|
|
* @param string $suffix |
213
|
|
|
* @return string |
214
|
|
|
*/ |
215
|
|
|
public function getFileName($docid, $alias = '', $prefix, $suffix) |
|
|
|
|
216
|
|
|
{ |
217
|
|
|
$modx = evolutionCMS(); |
218
|
|
|
|
219
|
|
|
if ($alias === '') { |
220
|
|
|
$filename = $prefix . $docid . $suffix; |
221
|
|
|
} else { |
222
|
|
|
if ($modx->config['suffix_mode'] === '1' && strpos($alias, '.') !== false) { |
223
|
|
|
$suffix = ''; |
224
|
|
|
} |
225
|
|
|
$filename = $prefix . $alias . $suffix; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
return $filename; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @param int $parent |
233
|
|
|
* @return string |
|
|
|
|
234
|
|
|
*/ |
235
|
|
|
public function run($parent = 0) |
|
|
|
|
236
|
|
|
{ |
237
|
|
|
global $_lang; |
238
|
|
|
$modx = evolutionCMS(); |
239
|
|
|
|
240
|
|
|
$tbl_site_content = $modx->getFullTableName('site_content'); |
241
|
|
|
|
242
|
|
|
$ignore_ids = $this->ignore_ids; |
243
|
|
|
$dirpath = $this->targetDir . '/'; |
244
|
|
|
|
245
|
|
|
$prefix = $modx->config['friendly_url_prefix']; |
246
|
|
|
$suffix = $modx->config['friendly_url_suffix']; |
247
|
|
|
|
248
|
|
|
$tpl = ' <span class="[+status+]">[+msg1+]</span> [+msg2+]</span>'; |
249
|
|
|
$ph = array(); |
|
|
|
|
250
|
|
|
|
251
|
|
|
$ph['status'] = 'fail'; |
252
|
|
|
$ph['msg1'] = $_lang['export_site_failed']; |
253
|
|
|
$ph['msg2'] = $_lang["export_site_failed_no_write"] . ' - ' . $dirpath; |
254
|
|
|
$msg_failed_no_write = $this->parsePlaceholder($tpl, $ph); |
255
|
|
|
|
256
|
|
|
$ph['msg2'] = $_lang["export_site_failed_no_retrieve"]; |
257
|
|
|
$msg_failed_no_retrieve = $this->parsePlaceholder($tpl, $ph); |
258
|
|
|
|
259
|
|
|
$ph['status'] = 'success'; |
260
|
|
|
$ph['msg1'] = $_lang['export_site_success']; |
261
|
|
|
$ph['msg2'] = ''; |
262
|
|
|
$msg_success = $this->parsePlaceholder($tpl, $ph); |
263
|
|
|
|
264
|
|
|
$ph['msg2'] = $_lang['export_site_success_skip_doc']; |
265
|
|
|
$msg_success_skip_doc = $this->parsePlaceholder($tpl, $ph); |
266
|
|
|
|
267
|
|
|
$ph['msg2'] = $_lang['export_site_success_skip_dir']; |
268
|
|
|
$msg_success_skip_dir = $this->parsePlaceholder($tpl, $ph); |
269
|
|
|
|
270
|
|
|
$fields = "id, alias, pagetitle, isfolder, (content = '' AND template = 0) AS wasNull, published"; |
271
|
|
|
$noncache = $_POST['includenoncache'] == 1 ? '' : 'AND cacheable=1'; |
272
|
|
|
$where = "parent = '{$parent}' AND deleted=0 AND ((published=1 AND type='document') OR (isfolder=1)) {$noncache} {$ignore_ids}"; |
273
|
|
|
$rs = $modx->getDatabase()->select($fields, $tbl_site_content, $where); |
|
|
|
|
274
|
|
|
|
275
|
|
|
$ph = array(); |
276
|
|
|
$ph['total'] = $this->total; |
277
|
|
|
$folder_permission = octdec($modx->config['new_folder_permissions']); |
278
|
|
|
while ($row = $modx->getDatabase()->getRow($rs)) { |
|
|
|
|
279
|
|
|
$this->count++; |
280
|
|
|
$filename = ''; |
281
|
|
|
$row['count'] = $this->count; |
282
|
|
|
$row['url'] = $modx->makeUrl($row['id']); |
283
|
|
|
|
284
|
|
|
if (!$row['wasNull']) { // needs writing a document |
285
|
|
|
$docname = $this->getFileName($row['id'], $row['alias'], $prefix, $suffix); |
286
|
|
|
$filename = $dirpath . $docname; |
287
|
|
|
if (!is_file($filename)) { |
288
|
|
|
if ($row['published'] === '1') { |
289
|
|
|
$status = $this->makeFile($row['id'], $filename); |
290
|
|
|
switch ($status) { |
291
|
|
|
case 'failed_no_write' : |
|
|
|
|
292
|
|
|
$row['status'] = $msg_failed_no_write; |
293
|
|
|
break; |
294
|
|
|
case 'failed_no_retrieve': |
295
|
|
|
$row['status'] = $msg_failed_no_retrieve; |
296
|
|
|
break; |
297
|
|
|
default: |
298
|
|
|
$row['status'] = $msg_success; |
299
|
|
|
} |
300
|
|
|
} else { |
301
|
|
|
$row['status'] = $msg_failed_no_retrieve; |
302
|
|
|
} |
303
|
|
|
} else { |
304
|
|
|
$row['status'] = $msg_success_skip_doc; |
305
|
|
|
} |
306
|
|
|
$this->output[] = $this->parsePlaceholder($_lang['export_site_exporting_document'], $row); |
307
|
|
|
} else { |
308
|
|
|
$row['status'] = $msg_success_skip_dir; |
309
|
|
|
$this->output[] = $this->parsePlaceholder($_lang['export_site_exporting_document'], $row); |
310
|
|
|
} |
311
|
|
|
if ($row['isfolder'] === '1' && ($modx->config['suffix_mode'] !== '1' || strpos($row['alias'], |
312
|
|
|
'.') === false)) { // needs making a folder |
313
|
|
|
$end_dir = ($row['alias'] !== '') ? $row['alias'] : $row['id']; |
314
|
|
|
$dir_path = $dirpath . $end_dir; |
315
|
|
|
if (strpos($dir_path, MODX_BASE_PATH) === false) { |
316
|
|
|
return false; |
317
|
|
|
} |
318
|
|
|
if (!is_dir($dir_path)) { |
319
|
|
|
if (is_file($dir_path)) { |
320
|
|
|
@unlink($dir_path); |
321
|
|
|
} |
322
|
|
|
mkdir($dir_path); |
323
|
|
|
@chmod($dir_path, $folder_permission); |
324
|
|
|
|
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
|
328
|
|
|
if ($modx->config['make_folders'] === '1' && $row['published'] === '1') { |
329
|
|
|
if (!empty($filename) && is_file($filename)) { |
330
|
|
|
rename($filename, $dir_path . '/index.html'); |
331
|
|
|
} |
332
|
|
|
} |
333
|
|
|
$this->targetDir = $dir_path; |
334
|
|
|
$this->run($row['id']); |
335
|
|
|
} |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
return implode("\n", $this->output); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* @param string $url |
343
|
|
|
* @param int $timeout |
344
|
|
|
* @return string |
345
|
|
|
*/ |
346
|
|
|
public function curl_get_contents($url, $timeout = 30) |
|
|
|
|
347
|
|
|
{ |
348
|
|
|
if (!function_exists('curl_init')) { |
349
|
|
|
return @file_get_contents($url); |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
$ch = curl_init(); |
|
|
|
|
353
|
|
|
curl_setopt($ch, CURLOPT_URL, $url); |
354
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // 0 = DO NOT VERIFY AUTHENTICITY OF SSL-CERT |
355
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // 2 = CERT MUST INDICATE BEING CONNECTED TO RIGHT SERVER |
356
|
|
|
curl_setopt($ch, CURLOPT_HEADER, false); |
357
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
358
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); |
359
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); |
360
|
|
|
$result = curl_exec($ch); |
361
|
|
|
curl_close($ch); |
362
|
|
|
|
363
|
|
|
return $result; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
public function parsePlaceholder($tpl, $ph = array()) |
|
|
|
|
367
|
|
|
{ |
368
|
|
|
foreach ($ph as $k => $v) { |
369
|
|
|
$k = "[+{$k}+]"; |
370
|
|
|
$tpl = str_replace($k, $v, $tpl); |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
return $tpl; |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
} |
377
|
|
|
|
This check marks property names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString
.