Passed
Push — development ( a254f4...4fadb5 )
by Spuds
01:18 queued 34s
created

template_empty_xml()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * @package   ElkArte Forum
5
 * @copyright ElkArte Forum contributors
6
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file)
7
 *
8
 * This file contains code covered by:
9
 * copyright: 2011 Simple Machines (http://www.simplemachines.org)
10
 *
11
 * @version 2.0 Beta 1
12
 *
13
 */
14
15
use ElkArte\Helper\Util;
16
17
/**
18
 * Returns the text of a post in response to a quote request for loading into the current editing text box
19
 */
20
function template_quotefast()
21
{
22
	global $context;
23
24
	echo '<?xml version="1.0" encoding="UTF-8"?>
25
<elk>
26
	<quote>', cleanXml($context['quote']['xml']), '</quote>
27
</elk>';
28
}
29
30
/**
31
 * Returns a message text and subject for use in the quick modify screen
32
 */
33
function template_modifyfast()
34
{
35
	global $context;
36
37
	echo '<?xml version="1.0" encoding="UTF-8"?>
38
<elk>
39
	<subject><![CDATA[', cleanXml($context['message']['subject']), ']]></subject>
40
	<message id="msg_', $context['message']['id'], '"><![CDATA[', cleanXml($context['message']['body']), ']]></message>
41
</elk>';
42
}
43
44
/**
45
 * Returns updated message details so the topic display can be updated after a quick edit is completed
46
 */
47
function template_modifydone()
48
{
49
	global $context, $txt;
50
51
	echo '<?xml version="1.0" encoding="UTF-8"?>
52
<elk>
53
	<message id="msg_', $context['message']['id'], '">';
54
	if (empty($context['message']['errors']))
55
	{
56
		echo '
57
		<modified><![CDATA[', empty($context['message']['modified']['time']) ? '' : cleanXml(sprintf($txt['last_edit_by'], $context['message']['modified']['time'], $context['message']['modified']['name'])), ']]></modified>
58
		<subject is_first="', $context['message']['first_in_topic'] ? '1' : '0', '"><![CDATA[', cleanXml($context['message']['subject']), ']]></subject>
59
		<body><![CDATA[', $context['message']['body'], ']]></body>';
60
	}
61
	else
62
	{
63
		echo '
64
		<error in_subject="', $context['message']['error_in_subject'] ? '1' : '0', '" in_body="', cleanXml($context['message']['error_in_body']) ? '1' : '0', '"><![CDATA[', implode('<br />', $context['message']['errors']), ']]></error>';
65
	}
66
67
	echo '
68
	</message>
69
</elk>';
70
}
71
72
/**
73
 * When done modifying a topic title, updates the board listing
74
 */
75
function template_modifytopicdone()
76
{
77
	global $context, $txt;
78
79
	echo '<?xml version="1.0" encoding="UTF-8"?>
80
<elk>
81
	<message id="msg_', $context['message']['id'], '">';
82
	if (empty($context['message']['errors']))
83
	{
84
		echo '
85
		<modified><![CDATA[', empty($context['message']['modified']['time']) ? '' : cleanXml('&#171; <em>' . sprintf($txt['last_edit_by'], $context['message']['modified']['time'], $context['message']['modified']['name']) . '</em> &#187;'), ']]></modified>';
86
		if (!empty($context['message']['subject']))
87
		{
88
			echo '
89
		<subject><![CDATA[', cleanXml($context['message']['subject']), ']]></subject>';
90
		}
91
	}
92
	else
93
	{
94
		echo '
95
		<error in_subject="', $context['message']['error_in_subject'] ? '1' : '0', '"><![CDATA[', cleanXml(implode('<br />', $context['message']['errors'])), ']]></error>';
96
	}
97
98
	echo '
99
	</message>
100
</elk>';
101
}
102
103
/**
104
 * Used to return a post preview
105
 */
106
function template_post()
107
{
108
	global $context;
109
110
	echo '<?xml version="1.0" encoding="UTF-8"?>
111
<elk>
112
	<preview>
113
		<subject><![CDATA[', $context['preview_subject'], ']]></subject>
114
		<body><![CDATA[', $context['preview_message'], ']]></body>
115
	</preview>
116
	<errors serious="', empty($context['errors']['type']) || $context['errors']['type'] !== 'serious' ? '0' : '1', '" topic_locked="', $context['locked'] ? '1' : '0', '">';
117
118
	if (!empty($context['post_error']['errors']))
119
	{
120
		foreach ($context['post_error']['errors'] as $key => $message)
121
		{
122
			echo '
123
		<error code="', cleanXml($key), '"><![CDATA[', cleanXml($message), ']]></error>';
124
		}
125
	}
126
127
	echo '
128
		<caption name="guestname" class="', isset($context['post_error']['long_name']) || isset($context['post_error']['no_name']) || isset($context['post_error']['bad_name']) ? 'error' : '', '" />
129
		<caption name="email" class="', isset($context['post_error']['no_email']) || isset($context['post_error']['bad_email']) ? 'error' : '', '" />
130
		<caption name="evtitle" class="', isset($context['post_error']['no_event']) ? 'error' : '', '" />
131
		<caption name="subject" class="', isset($context['post_error']['no_subject']) ? 'error' : '', '" />
132
		<caption name="question" class="', isset($context['post_error']['no_question']) ? 'error' : '', '" />
133
	</errors>
134
	<last_msg>', $context['topic_last_message'] ?? '0', '</last_msg>';
135
136
	if (!empty($context['previous_posts']))
137
	{
138
		echo '
139
	<new_posts>';
140
		foreach ($context['previous_posts'] as $post)
141
		{
142
			echo '
143
		<post id="', $post['id'], '">
144
			<time><![CDATA[', $post['time'], ']]></time>
145
			<poster><![CDATA[', cleanXml($post['poster']), ']]></poster>
146
			<message><![CDATA[', cleanXml($post['body']), ']]></message>
147
			<is_ignored>', $post['is_ignored'] ? '1' : '0', '</is_ignored>
148
		</post>';
149
		}
150
151
		echo '
152
	</new_posts>';
153
	}
154
155
	echo '
156
</elk>';
157
}
158
159
/**
160
 * Returns a preview, used by personal messages, newsletters, bounce templates, etc
161
 */
162
function template_generic_preview()
163
{
164
	global $context, $txt;
165
166
	echo '<?xml version="1.0" encoding="UTF-8"?>
167
<elk>
168
	<preview>
169
		<subject><![CDATA[', empty($context['preview_subject']) ? $txt['not_applicable'] : $context['preview_subject'], ']]></subject>
170
		<body><![CDATA[', $context['preview_message'], ']]></body>
171
	</preview>
172
	<errors serious="', empty($context['error_type']) || $context['error_type'] !== 'serious' ? '0' : '1', '">';
173
174
	if (!empty($context['post_error']['errors']))
175
	{
176
		foreach ($context['post_error']['errors'] as $key => $message)
177
		{
178
			echo '
179
		<error code="', cleanXml($key), '"><![CDATA[', cleanXml($message), ']]></error>';
180
		}
181
	}
182
183
	// This is the not so generic section, mainly used by PM preview, can be used by others as well
184
	echo '
185
		<caption name="to" class="', isset($context['post_error']['no_to']) ? 'error' : '', '" />
186
		<caption name="bbc" class="', isset($context['post_error']['no_bbc']) ? 'error' : '', '" />
187
		<caption name="subject" class="', isset($context['post_error']['no_subject']) ? 'error' : '', '" />
188
		<caption name="question" class="', isset($context['post_error']['no_question']) ? 'error' : '', '" />',
189
	isset($context['post_error']['no_message']) || isset($context['post_error']['long_message']) ? '<post_error />' : '', '
190
	</errors>';
191
192
	echo '
193
</elk>';
194
}
195
196
/**
197
 * Returns additional statistics when a year/month is expanded
198
 */
199
function template_stats()
200
{
201
	global $context, $modSettings;
202
203
	echo '<?xml version="1.0" encoding="UTF-8"?>
204
<elk>';
205
	foreach ($context['yearly'] as $year)
206
	{
207
		foreach ($year['months'] as $month)
208
		{
209
			echo '
210
	<month id="', $month['date']['year'], $month['date']['month'], '">';
211
			foreach ($month['days'] as $day)
212
			{
213
				echo '
214
		<day date="', $day['year'], '-', $day['month'], '-', $day['day'], '" new_topics="', $day['new_topics'], '" new_posts="', $day['new_posts'], '" new_members="', $day['new_members'], '" most_members_online="', $day['most_members_online'], '"', empty($modSettings['hitStats']) ? '' : ' hits="' . $day['hits'] . '"', ' />';
215
			}
216
217
			echo '
218
	</month>';
219
		}
220
	}
221
222
	echo '
223
</elk>';
224
}
225
226
/**
227
 * Breaking up is not so hard to do
228
 */
229
function template_split()
230
{
231
	global $context;
232
233
	echo '<?xml version="1.0" encoding="UTF-8"?>
234
<elk>
235
	<pageIndex section="not_selected" startFrom="', $context['not_selected']['start'], '"><![CDATA[', $context['not_selected']['page_index'], ']]></pageIndex>
236
	<pageIndex section="selected" startFrom="', $context['selected']['start'], '"><![CDATA[', $context['selected']['page_index'], ']]></pageIndex>';
237
	foreach ($context['changes'] as $change)
238
	{
239
		if ($change['type'] === 'remove')
240
		{
241
			echo '
242
	<change id="', $change['id'], '" curAction="remove" section="', $change['section'], '" />';
243
		}
244
		else
245
		{
246
			echo '
247
	<change id="', $change['id'], '" curAction="insert" section="', $change['section'], '">
248
		<subject><![CDATA[', cleanXml($change['insert_value']['subject']), ']]></subject>
249
		<time><![CDATA[', cleanXml($change['insert_value']['time']), ']]></time>
250
		<body><![CDATA[', cleanXml($change['insert_value']['body']), ']]></body>
251
		<poster><![CDATA[', cleanXml($change['insert_value']['poster']), ']]></poster>
252
	</change>';
253
		}
254
	}
255
256
	echo '
257
</elk>';
258
}
259
260
/**
261
 * Return search results
262
 */
263
function template_results()
264
{
265
	global $context, $txt;
266
	echo '<?xml version="1.0" encoding="UTF-8"?>
267
<elk>';
268
269
	if (empty($context['topics']))
270
	{
271
		echo '
272
		<noresults>', $txt['find_no_results'], '</noresults>';
273
	}
274
	else
275
	{
276
		echo '
277
		<results>';
278
279
		while (($topic = $context['get_topics']()))
280
		{
281
			echo '
282
			<result>
283
				<id>', $topic['id'], '</id>
284
				<relevance>', $topic['relevance'], '</relevance>
285
				<board>
286
					<id>', $topic['board']['id'], '</id>
287
					<name>', cleanXml($topic['board']['name']), '</name>
288
					<href>', $topic['board']['href'], '</href>
289
				</board>
290
				<category>
291
					<id>', $topic['category']['id'], '</id>
292
					<name>', cleanXml($topic['category']['name']), '</name>
293
					<href>', $topic['category']['href'], '</href>
294
				</category>
295
				<messages>';
296
297
			foreach ($topic['matches'] as $message)
298
			{
299
				echo '
300
					<message>
301
						<id>', $message['id'], '</id>
302
						<subject><![CDATA[', cleanXml($message['subject_highlighted'] != '' ? $message['subject_highlighted'] : $message['subject']), ']]></subject>
303
						<body><![CDATA[', cleanXml($message['body_highlighted'] != '' ? $message['body_highlighted'] : $message['body']), ']]></body>
304
						<time>', $message['time'], '</time>
305
						<timestamp>', $message['timestamp'], '</timestamp>
306
						<start>', $message['start'], '</start>
307
308
						<author>
309
							<id>', $message['member']['id'], '</id>
310
							<name>', cleanXml($message['member']['name']), '</name>
311
							<href>', $message['member']['href'], '</href>
312
						</author>
313
					</message>';
314
			}
315
316
			echo '
317
				</messages>
318
			</result>';
319
		}
320
321
		echo '
322
		</results>';
323
	}
324
325
	echo '
326
</elk>';
327
}
328
329
/**
330
 * Build the jump to box
331
 */
332
function template_jump_to()
333
{
334
	global $context;
335
336
	echo '<?xml version="1.0" encoding="UTF-8"?>
337
<elk>';
338
339
	foreach ($context['categories'] as $category)
340
	{
341
		echo '
342
	<item type="category" id="', $category['id'], '"><![CDATA[', cleanXml($category['name']), ']]></item>';
343
		foreach ($category['boards'] as $board)
344
		{
345
			echo '
346
	<item type="board" id="', $board['id'], '" childlevel="', $board['child_level'], '"><![CDATA[', cleanXml($board['name']), ']]></item>';
347
		}
348
	}
349
350
	echo '
351
</elk>';
352
}
353
354
/**
355
 * Loads the message icons for changing them via the quick edit
356
 */
357
function template_message_icons()
358
{
359
	global $context;
360
361
	echo '<?xml version="1.0" encoding="UTF-8"?>
362
<elk>';
363
364
	foreach ($context['icons'] as $icon)
365
	{
366
		echo '
367
	<icon value="', $icon['value'], '" url="', $icon['url'], '"><![CDATA[', cleanXml($icon['name']), ']]></icon>';
368
	}
369
370
	echo '
371
</elk>';
372
}
373
374
/**
375
 * Returns if the username is valid or not, used during registration
376
 */
377
function template_check_username()
378
{
379
	global $context;
380
381
	echo '<?xml version="1.0" encoding="UTF-8"?>
382
<elk>
383
	<username valid="', $context['valid_username'] ? 1 : 0, '">', cleanXml($context['checked_username']), '</username>
384
</elk>';
385
}
386
387
/**
388
 * @todo ... maybe emanuele can explain :D
389
 */
390
function template_generic_xml_buttons()
391
{
392
	global $context;
393
394
	$tag = empty($context['xml_data']['error']) ? 'button' : 'error';
395
396
	echo '<?xml version="1.0" encoding="UTF-8"?>
397
<elk>
398
	<', $tag, '>';
399
400
	foreach ($context['xml_data'] as $key => $val)
401
	{
402
		if ($key !== 'error')
403
		{
404
			echo '
405
			<', $key, '><![CDATA[', cleanXml($val), ']]></', $key, '>';
406
		}
407
	}
408
409
	echo '
410
	</', $tag, '>
411
</elk>';
412
}
413
414
/**
415
 * This prints XML in its most generic form.
416
 */
417
function template_generic_xml()
418
{
419
	global $context;
420
421
	echo '<?xml version="1.0" encoding="UTF-8"?>';
422
423
	// Show the data.
424
	template_generic_xml_recursive($context['xml_data'], 'elk', '', -1);
425
}
426
427
/**
428
 * Recursive function for displaying generic XML data.
429
 *
430
 * @param array $xml_data
431
 * @param string $parent_ident
432
 * @param string $child_ident
433
 * @param int $level
434
 */
435
function template_generic_xml_recursive($xml_data, $parent_ident, $child_ident, $level)
436
{
437
	// This is simply for neat indentation.
438
	$level++;
439
440
	echo "\n" . str_repeat("\t", $level), '<', $parent_ident, '>';
441
442
	foreach ($xml_data as $key => $data)
443
	{
444
		// A group?
445
		if (is_array($data) && isset($data['identifier']))
446
		{
447
			template_generic_xml_recursive($data['children'], $key, $data['identifier'], $level);
448
		}
449
		// An item...
450
		elseif (is_array($data) && isset($data['value']))
451
		{
452
			echo "\n", str_repeat("\t", $level), '<', $child_ident;
453
454
			if (!empty($data['attributes']))
455
			{
456
				foreach ($data['attributes'] as $k => $v)
457
				{
458
					echo ' ' . $k . '="' . $v . '"';
459
				}
460
			}
461
462
			echo '><![CDATA[', cleanXml($data['value']), ']]></', $child_ident, '>';
463
		}
464
	}
465
466
	echo "\n", str_repeat("\t", $level), '</', $parent_ident, '>';
467
}
468
469
/**
470
 * Formats data retrieved in other functions into XML format.
471
 * Additionally, formats data based on the specific format passed.
472
 * This function is recursively called to handle subarrays of data.
473
 *
474
 * @param array $data the array to output as xml data
475
 * @param int $i the amount of indentation to use.
476
 * @param string|null $tag if specified, it will be used instead of the keys of data.
477
 * @param string $xml_format one of rss, rss2, rdf, atom
478
 */
479
function template_xml_news($data, $i, $tag = null, $xml_format = 'rss')
480
{
481
	require_once(SUBSDIR . '/News.subs.php');
482
483
	// For every array in the data...
484
	foreach ($data as $key => $val)
485
	{
486
		// Skip it, it's been set to null.
487
		if ($val === null)
488
		{
489
			continue;
490
		}
491
492
		// If a tag was passed, use it instead of the key.
493
		$key = $tag ?? $key;
494
495
		// First let's indent!
496
		echo "\n", str_repeat("\t", $i);
497
498
		// Grr, I hate kludges... almost worth doing it properly, here, but not quite.
499
		if ($xml_format === 'atom' && $key === 'link')
500
		{
501
			echo '<link rel="alternate" type="text/html" href="', fix_possible_url($val), '" />';
502
			continue;
503
		}
504
505
		// If it's empty/0/nothing simply output an empty tag.
506
		if ($val == '')
507
		{
508
			echo '<', $key, ' />';
509
		}
510
		elseif ($xml_format === 'atom' && $key === 'category')
511
		{
512
			echo '<', $key, ' term="', $val, '" />';
513
		}
514
		else
515
		{
516
			// Beginning tag.
517
			if ($xml_format === 'rdf' && $key === 'item' && isset($val['link']))
518
			{
519
				echo '<', $key, ' rdf:about="', fix_possible_url($val['link']), '">';
520
				echo "\n", str_repeat("\t", $i + 1);
521
				echo '<dc:format>text/html</dc:format>';
522
			}
523
			elseif ($xml_format === 'atom' && $key === 'summary')
524
			{
525
				echo '<', $key, ' type="html">';
526
			}
527
			else
528
			{
529
				echo '<', $key, '>';
530
			}
531
532
			if (is_array($val))
533
			{
534
				// An array.  Dump it, and then indent the tag.
535
				template_xml_news($val, $i + 1, null, $xml_format);
536
				echo "\n", str_repeat("\t", $i), '</', $key, '>';
537
			}
538
			// A string with returns in it.... show this as a multiline element.
539
			elseif (str_contains($val, "\n") || str_contains($val, '<br />'))
540
			{
541
				echo "\n", fix_possible_url($val), "\n", str_repeat("\t", $i), '</', $key, '>';
542
			}
543
			// A simple string.
544
			else
545
			{
546
				echo fix_possible_url($val), '</', $key, '>';
547
			}
548
		}
549
	}
550
}
551
552
/**
553
 * Main Atom feed template
554
 */
555
function template_rdf()
556
{
557
	global $context, $scripturl, $txt;
558
559
	echo '<?xml version="1.0" encoding="UTF-8"?' . '>
560
	<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns="http://purl.org/rss/1.0/">
561
		<channel rdf:about="', $scripturl, '">
562
			<title>', $context['feed_title'], '</title>
563
			<link>', $scripturl, '</link>
564
			<description><![CDATA[', strip_tags($txt['xml_rss_desc']), ']]></description>
565
			<items>
566
				<rdf:Seq>';
567
568
	foreach ($context['recent_posts_data'] as $item)
569
	{
570
		echo '
571
					<rdf:li rdf:resource="', $item['link'], '" />';
572
	}
573
574
	echo '
575
				</rdf:Seq>
576
			</items>
577
		</channel>
578
	';
579
580
	template_xml_news($context['recent_posts_data'], 1, 'item', $context['xml_format']);
581
582
	echo '
583
	</rdf:RDF>';
584
}
585
586
/**
587
 * Main Atom feed template
588
 */
589
function template_feedatom()
590
{
591
	global $context, $scripturl, $txt;
592
593
	echo '<?xml version="1.0" encoding="UTF-8"?>
594
	<feed xmlns="http://www.w3.org/2005/Atom">
595
		<title>', $context['feed_title'], '</title>
596
		<link rel="alternate" type="text/html" href="', $scripturl, '" />
597
		<link rel="self" type="application/rss+xml" href="', $scripturl, '?type=atom;action=.xml', $context['url_parts'], '" />
598
		<id>', $scripturl, '</id>
599
		<icon>', $context['favicon'] . '</icon>
600
		<logo>', $context['header_logo_url_html_safe'], '</logo>
601
602
		<updated>', Util::gmstrftime('%Y-%m-%dT%H:%M:%SZ'), '</updated>
603
		<subtitle><![CDATA[', strip_tags(un_htmlspecialchars($txt['xml_rss_desc'])), ']]></subtitle>
604
		<generator uri="https://www.elkarte.net" version="', strtr(FORUM_VERSION, ['ElkArte' => '']), '">ElkArte</generator>
605
		<author>
606
			<name>', strip_tags(un_htmlspecialchars($context['forum_name'])), '</name>
607
		</author>';
608
609
	template_xml_news($context['recent_posts_data'], 2, 'entry', $context['xml_format']);
610
611
	echo '
612
	</feed>';
613
}
614
615
/**
616
 * Main RSS feed template (0.92 and 2.0)
617
 */
618
function template_feedrss()
619
{
620
	global $context, $scripturl, $txt;
621
622
	echo '<?xml version="1.0" encoding="UTF-8"?>
623
	<rss version=', $context['xml_format'] === 'rss2' ? '"2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"' : '"0.92"', ' xml:lang="', strtr($txt['lang_locale'], '_', '-'), '">
624
		<channel>
625
			<title>', $context['feed_title'], '</title>
626
			<link>', $scripturl, '</link>
627
			<description><![CDATA[', un_htmlspecialchars(strip_tags($txt['xml_rss_desc'])), ']]></description>
628
			<generator>ElkArte</generator>
629
			<ttl>30</ttl>
630
			<image>
631
				<url>', $context['header_logo_url_html_safe'], '</url>
632
				<title>', $context['feed_title'], '</title>
633
				<link>', $scripturl, '</link>
634
			</image>';
635
636
	// Output all the associative array, start indenting with 2 tabs, and name everything "item".
637
	template_xml_news($context['recent_posts_data'], 2, 'item', $context['xml_format']);
638
639
	// Output the footer of the xml.
640
	echo '
641
		</channel>
642
	</rss>';
643
}
644
645
/**
646
 * Returns XML response to a draft autosave request
647
 * provides the id of the draft saved and the time it was saved in the response
648
 */
649
function template_xml_draft()
650
{
651
	global $context, $txt;
652
653
	echo '<?xml version="1.0" encoding="UTF-8"?>
654
<drafts>
655
	<draft id="', $context['id_draft'], '"><![CDATA[', $txt['draft_saved_on'], ': ', standardTime($context['draft_saved_on']), ']]></draft>
656
</drafts>';
657
}
658
659
/**
660
 * Returns XML response to a draft load request
661
 * provides the subject of the draft saved and the link to load
662
 */
663
function template_xml_load_draft()
664
{
665
	global $context;
666
667
	echo '<?xml version="1.0" encoding="UTF-8"?>
668
<drafts>';
669
	foreach ($context['drafts'] as $key => $draft)
670
	{
671
		echo '
672
	<draft id="', $key, '">
673
		<subject><![CDATA[', cleanXml($draft['subject']), ']]></subject>
674
		<time><![CDATA[', cleanXml($draft['poster_time']), ']]></time>
675
		<link><![CDATA[', cleanXml($draft['link']), ']]></link>
676
	</draft>';
677
	}
678
679
	echo '
680
</drafts>';
681
}
682
683
/**
684
 * Returns an empty XML response
685
 */
686
function template_empty_xml()
687
{
688
	echo '<?xml version="1.0" encoding="UTF-8"?>
689
<elk />';
690
}
691