1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8require_once('tiki-setup.php');
9$structlib = TikiLib::lib('struct');
10
11//get_strings tra("Send Pages");
12$access->check_feature('feature_comm');
13$access->check_permission_either(['tiki_p_send_pages', 'tiki_p_send_articles']);
14
15if ($tiki_p_send_pages != 'y' && $tiki_p_send_articles != 'y') {
16	$smarty->assign('errortype', 401);
17	$smarty->assign('msg', tra('You do not have the permission that is needed to use this feature'));
18	$smarty->display('error.tpl');
19	die;
20}
21
22if (! isset($_REQUEST['username'])) {
23	$_REQUEST['username'] = $user;
24}
25
26if (! isset($_REQUEST['path'])) {
27	$_REQUEST['path'] = '/tiki/commxmlrpc.php';
28}
29
30if (! isset($_REQUEST['site'])) {
31	$_REQUEST['site'] = '';
32}
33
34if (! isset($_REQUEST['password'])) {
35	$_REQUEST['password'] = '';
36}
37
38if (! isset($_REQUEST['sendpages'])) {
39	$sendpages = [];
40} else {
41	$sendpages = json_decode(urldecode($_REQUEST['sendpages']));
42}
43
44if (! isset($_REQUEST['sendstructures'])) {
45	$sendstructures = [];
46} else {
47	$sendstructures = json_decode(urldecode($_REQUEST['sendstructures']));
48}
49
50if (! isset($_REQUEST['sendarticles'])) {
51	$sendarticles = [];
52} else {
53	$sendarticles = json_decode(urldecode($_REQUEST['sendarticles']));
54}
55
56$smarty->assign('username', $_REQUEST['username']);
57$smarty->assign('site', $_REQUEST['site']);
58$smarty->assign('path', $_REQUEST['path']);
59$smarty->assign('password', $_REQUEST['password']);
60
61if (isset($_REQUEST['dbg'])) {
62	$smarty->assign('dbg', $_REQUEST['dbg']);
63}
64
65if (isset($_REQUEST['find'])) {
66	$find = $_REQUEST['find'];
67} else {
68	$find = '';
69}
70
71$smarty->assign('find', $find);
72
73if (isset($_REQUEST['addpage'])) {
74	if (! in_array($_REQUEST['pageName'], $sendpages)) {
75		$sendpages[] = $_REQUEST['pageName'];
76	}
77}
78
79if (isset($_REQUEST['clearpages'])) {
80	$sendpages = [];
81}
82
83if (isset($_REQUEST['addstructure'])) {
84	if (! in_array($_REQUEST['structure'], $sendstructures)) {
85		$sendstructures[] = $_REQUEST['structure'];
86	}
87}
88
89if (isset($_REQUEST['clearstructures'])) {
90	$sendstructures = [];
91}
92
93if (isset($_REQUEST['addarticle'])) {
94	if (! in_array($_REQUEST['articleId'], $sendarticles)) {
95		$sendarticles[] = $_REQUEST['articleId'];
96	}
97}
98
99if (isset($_REQUEST['cleararticles'])) {
100	$sendarticles = [];
101}
102$structures = $structlib->list_structures(0, -1, 'pageName_asc', $find);
103$smarty->assign_by_ref('structures', $structures['data']);
104$msg = '';
105
106if (isset($_REQUEST['send'])) {
107	check_ticket('send-objects');
108	// Create XMLRPC object
109	$client = new XML_RPC_Client($_REQUEST['path'], $_REQUEST['site'], 80);
110	$client->setDebug((isset($_REQUEST['dbg']) && $_REQUEST['dbg'] == 'on') ? true : false);
111	foreach ($sendstructures as $structure) {
112		$spages = $structlib->s_get_structure_pages($structure);
113		$pos = 0;
114		foreach ($spages as $spage) {
115			$listPageNames[$spage['page_ref_id']] = $spage['pageName'];
116			$page_info = $tikilib->get_page_info($spage['pageName']);
117			$pos++;
118			$searchMsg = new XML_RPC_Message(
119				'sendStructurePage',
120				[
121					new XML_RPC_Value($_SERVER['SERVER_NAME'], 'string'),
122					new XML_RPC_Value($_REQUEST['username'], 'string'),
123					new XML_RPC_Value($_REQUEST['password'], 'string'),
124					new XML_RPC_Value($spages[0]['pageName'], 'string'),
125					new XML_RPC_Value($spage['parent_id'] ? $listPageNames[$spage['parent_id']] : $spage['pageName'], 'string'),
126					new XML_RPC_Value($spage['pageName'], 'string'),
127					new XML_RPC_Value(base64_encode($page_info['data']), 'string'),
128					new XML_RPC_Value($page_info['comment'], 'string'),
129					new XML_RPC_Value($page_info['description'], 'string'),
130					new XML_RPC_Value($pos, 'string'),
131					new XML_RPC_Value($spage['page_alias'], 'string')
132				]
133			);
134			$result = $client->send($searchMsg);
135			if (! $result) {
136				$errorMsg = tra('Cannot login to server maybe the server is down');
137				$msg .= $errorMsg;
138			} else {
139				if (! $result->faultCode()) {
140					$msg .= tra('Page') . ': ' . $spage['pageName'] . ' ' . tra('successfully sent') . '<br />';
141				} else {
142					$errorMsg = $result->faultString();
143					$msg .= tra('Page') . ': ' . $spage['pageName'] . ' ' . tra('not sent') . '!' . '<br />';
144					$msg .= tra('Error: ') . $result->faultCode() . '-' . tra($errorMsg) . '<br />';
145				}
146			}
147		}
148	}
149
150	foreach ($sendpages as $page) {
151		$page_info = $tikilib->get_page_info($page);
152		if ($page_info) {
153			$searchMsg = new XML_RPC_Message(
154				'sendPage',
155				[
156					new XML_RPC_Value($_SERVER['SERVER_NAME'], 'string'),
157					new XML_RPC_Value($_REQUEST['username'], 'string'),
158					new XML_RPC_Value($_REQUEST['password'], 'string'),
159					new XML_RPC_Value($page, 'string'),
160					new XML_RPC_Value(base64_encode($page_info['data']), 'string'),
161					new XML_RPC_Value($page_info['comment'], 'string'),
162					new XML_RPC_Value($page_info['description'], 'string'),
163				]
164			);
165			$result = $client->send($searchMsg);
166			if (! $result) {
167				$errorMsg = tra('Cannot login to server maybe the server is down');
168				$msg .= $errorMsg;
169			} else {
170				if (! $result->faultCode()) {
171					$msg .= tra('Page') . ': ' . $page . ' ' . tra('successfully sent') . '<br />';
172				} else {
173					$errorMsg = $result->faultString();
174					$msg .= tra('Page') . ': ' . $page . ' ' . tra('not sent') . '!' . '<br />';
175					$msg .= tra('Error: ') . $result->faultCode() . '-' . tra($errorMsg) . '<br />';
176				}
177			}
178		}
179	}
180	$artlib = TikiLib::lib('art');
181	foreach ($sendarticles as $article) {
182		$page_info = $artlib->get_article($article);
183		if ($page_info) {
184			$searchMsg = new XML_RPC_Message(
185				'sendArticle',
186				[
187					new XML_RPC_Value($_SERVER['SERVER_NAME'], 'string'),
188					new XML_RPC_Value($_REQUEST['username'], 'string'),
189					new XML_RPC_Value($_REQUEST['password'], 'string'),
190					new XML_RPC_Value(base64_encode($page_info['title']), 'string'),
191					new XML_RPC_Value(base64_encode($page_info['authorName']), 'string'),
192					new XML_RPC_Value($page_info['size'], 'int'),
193					new XML_RPC_Value($page_info['useImage'], 'string'),
194					new XML_RPC_Value($page_info['image_name'], 'string'),
195					new XML_RPC_Value($page_info['image_type'], 'string'),
196					new XML_RPC_Value($page_info['image_size'], 'int'),
197					new XML_RPC_Value($page_info['image_x'], 'int'),
198					new XML_RPC_Value($page_info['image_y'], 'int'),
199					new XML_RPC_Value(base64_encode($page_info['image_data']), 'string'),
200					new XML_RPC_Value($page_info['publishDate'], 'int'),
201					new XML_RPC_Value($page_info['expireDate'], 'int'),
202					new XML_RPC_Value($page_info['created'], 'int'),
203					new XML_RPC_Value(base64_encode($page_info['heading']), 'string'),
204					new XML_RPC_Value(base64_encode($page_info['body']), 'string'),
205					new XML_RPC_Value($page_info['hash'], 'string'),
206					new XML_RPC_Value($page_info['author'], 'string'),
207					new XML_RPC_Value($page_info['type'], 'string'),
208					new XML_RPC_Value($page_info['rating'], 'string')
209				]
210			);
211			$result = $client->send($searchMsg);
212			if (! $result) {
213				$errorMsg = tra('Cannot login to server maybe the server is down');
214				$msg .= $errorMsg;
215			} else {
216				if (! $result->faultCode()) {
217					$msg .= tra('Article:') . ' ' . $page_info['title'] . ' ' . tra('successfully sent') . '<br />';
218				} else {
219					$errorMsg = $result->faultString();
220					$msg .= tra('Article:') . ' ' . $page_info['title'] . ' ' . tra('not sent') . '!' . '<br />';
221					$msg .= tra('Error: ') . $result->faultCode() . '-' . tra($errorMsg) . '<br />';
222				}
223			}
224		}
225	}
226}
227$smarty->assign('msg', $msg);
228$smarty->assign('sendpages', $sendpages);
229$sendstructures_names = [];
230foreach ($sendstructures as $key => $id) {
231	foreach ($structures['data'] as $structure) {
232		if ($structure['page_ref_id'] == $id) {
233			$sendstructures_names[$key] = $structure['pageName'];
234		}
235	}
236}
237$smarty->assign('sendstructures_names', $sendstructures_names);
238$smarty->assign('sendarticles', $sendarticles);
239$form_sendpages = urlencode(json_encode($sendpages));
240$form_sendstructures = urlencode(json_encode($sendstructures));
241$form_sendarticles = urlencode(json_encode($sendarticles));
242$smarty->assign('form_sendpages', $form_sendpages);
243$smarty->assign('form_sendstructures', $form_sendstructures);
244$smarty->assign('form_sendarticles', $form_sendarticles);
245$pages = $tikilib->list_pageNames(0, -1, 'pageName_asc', $find);
246$smarty->assign('pages', $pages['data']);
247
248if ($prefs['feature_articles'] == 'y') {
249	$artlib = TikiLib::lib('art');
250	$articles = $artlib->list_articles(0, -1, 'publishDate_desc', $find, 0, $tikilib->now, $user);
251	$smarty->assign('articles', $articles['data']);
252}
253
254ask_ticket('send-objects');
255// disallow robots to index page:
256$smarty->assign('metatag_robots', 'NOINDEX, NOFOLLOW');
257
258$smarty->assign('mid', 'tiki-send_objects.tpl');
259$smarty->display('tiki.tpl');
260