aboutsummaryrefslogtreecommitdiffstats
path: root/Zotlabs/Module/Uexport.php
blob: 870c4280263c74cba5ad3bb6683cc50707859ad2 (plain) (blame)
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
<?php
namespace Zotlabs\Module;

use App;
use ZipArchive;
use Zotlabs\Lib\Apps;
use Zotlabs\Web\Controller;

class Uexport extends Controller {

	function init() {
		if(! local_channel()) {
			return;
		}

		if(! Apps::system_app_installed(local_channel(), 'Channel Export')) {
			return;
		}

		if(argc() > 1) {

			$zap_compat = (($_REQUEST['zap_compat']) ? intval($_REQUEST['zap_compat']) : false);
			$channel = App::get_channel();
			$year = null;
			$month = null;

			if(argc() > 1 && intval(argv(1)) > 1900) {
				$year = intval(argv(1));
			}

			if(argc() > 2 && intval(argv(2)) > 0 && intval(argv(2)) <= 12) {
				$month = intval(argv(2));
			}

			$sections = [];
			$section = '';
			if(argc() > 1 && ctype_lower(argv(1))) {
				$section = argv(1);
			}

			switch ($section) {
				case 'channel':
					$sections = get_default_export_sections();
					break;
				case 'chatrooms':
					$sections = ['chatrooms'];
					break;
				case 'events':
					$sections = ['events'];
					break;
				case 'webpages':
					$sections = ['webpages'];
					break;
				case 'wikis':
					$sections = ['wikis'];
					break;
				case 'custom':
				default:
					$custom_sections = ['channel', 'connections', 'config', 'apps', 'chatrooms', 'events', 'webpages', 'wikis'];
					$raw_sections = (($_REQUEST['sections']) ? explode(',', $_REQUEST['sections']) : '');
					if ($raw_sections) {
						foreach ($raw_sections as $raw_section) {
							if(in_array($raw_section, $custom_sections)) {
								$sections[] = $raw_section;
							}
						}
					}
			}

			if ($sections) {

				$export = json_encode(identity_basic_export(local_channel(), $sections, $zap_compat));

				header('Content-Type: application/json');
				header('Content-Disposition: attachment; filename="' . $channel['channel_address'] . '-' . implode('-', $sections) . '.json"');
				header('Content-Length: ' . strlen($export));

				echo $export;

				killme();
			}
			elseif ($year && !$month) {
				$zip_dir = 'store/[data]/' . $channel['channel_address'] . '/tmp';
				if (!is_dir($zip_dir))
					mkdir($zip_dir, STORAGE_DEFAULT_PERMISSIONS, true);

				$zip_file = $channel['channel_address'] . '-' . $year . '.zip';
				$zip_path = $zip_dir . '/' . $zip_file;
				$zip_content_available = false;
				$zip = new ZipArchive();

				if ($zip->open($zip_path, ZipArchive::CREATE) === true) {
					$month = 1;
					while ($month <= 12) {
						$name = $channel['channel_address'] . '-' . $year . '-' . $month . '.json';
						$content = conv_item_export_year(local_channel(), $year, $month, $zap_compat);
						if(isset($content['item'])) {
							$zip_content_available = true;
							$zip->addFromString($name, json_encode($content));
						}
						$month++;
					}
					$zip->setCompressionName($zip_path, ZipArchive::CM_STORE);
					$zip->close();
				}
				if (!$zip_content_available) {
					unlink($zip_path);
					notice(t('No content available for year') . ' ' . $year . EOL);
					goaway('/uexport');
				}

				header('Content-Type: application/zip');
				header('Content-Disposition: attachment; filename="' . $zip_file . '"');
				header('Content-Length: ' . filesize($zip_path));

				$istream = fopen($zip_path, 'rb');
				$ostream = fopen('php://output', 'wb');
				if ($istream && $ostream) {
					pipe_streams($istream, $ostream);
					fclose($istream);
					fclose($ostream);
				}

				unlink($zip_path);
				killme();
			}
			elseif ($year && $month) {
				$export = json_encode(conv_item_export_year(local_channel(), $year, $month, $zap_compat));

				header('Content-Type: application/json');
				header('Content-Disposition: attachment; filename="' . $channel['channel_address'] . '-' . $year . '-' . $month . '.json"');
				header('Content-Length: ' . strlen($export));

				echo $export;

				killme();
			}
			else {
				killme();
			}
		}
	}

	function get() {

		if(! local_channel()) {
			return;
		}

		if(! Apps::system_app_installed(local_channel(), 'Channel Export')) {
			//Do not display any associated widgets at this point
			App::$pdl = '';
			$papp = Apps::get_papp('Channel Export');
			return Apps::app_render($papp, 'module');
		}

		$account = App::get_account();
		$year_start = datetime_convert('UTC', date_default_timezone_get(), $account['account_created'], 'Y');
		$year_end = datetime_convert('UTC', date_default_timezone_get(), 'now', 'Y');
		$years = [];

		while ($year_start <= $year_end) {
			$years[] = $year_start;
			$year_start++;
		}

		$item_import_url = '/import_items';
		$channel_import_url = '/import';

		$o = replace_macros(get_markup_template('uexport.tpl'), array(
			'$title' => t('Export Channel'),

			'$channel_title' => t('Export channel'),
			'$channel_info' => t('This will export your identity and social graph into a file which can be used to import your channel to a new hub.'),

			'$years' => $years,
			'$content_title' => t('Export content'),
			'$content_info' => t('This will export your posts, direct messages, articles and cards per month stored into a zip file per year. Months with no posts will be dismissed.'),

			'$wikis_title' => t('Export wikis'),
			'$wikis_info' => t('This will export your wikis and wiki pages.'),

			'$webpages_title' => t('Export webpages'),
			'$webpages_info' => t('This will export your webpages and menus.'),

			'$events_title' => t('Export channel calendar'),
			'$events_info' => t('This will export your channel calendar events and associated items. CalDAV calendars are not included.'),

			'$chatrooms_title' => t('Export chatrooms'),
			'$chatrooms_info' => t('This will export your chatrooms. Chat history is dismissed.'),

			'$items_extra_info' => sprintf( t('This export can be imported or restored by visiting <a href="%1$s">%2$s</a> on any site containing your channel.'), $item_import_url, $item_import_url),
		));
		return $o;
	}




}