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
8function wikiplugin_includeurl_info()
9{
10	return [
11		'name' => tra('Include URL'),
12		'documentation' => 'PluginIncludeURL',
13		'description' => tra('Include the body content from a URL'),
14		'prefs' => ['wikiplugin_includeurl'],
15		'iconname' => 'link-external',
16		'introduced' => 15,
17		'tags' => [ 'basic' ],
18		'format' => 'html',
19		'params' => [
20			'url' => [
21				'required' => true,
22				'name' => tra('URL'),
23				'description' => tra('URL to external file to include.'),
24				'since' => '15.0',
25				'filter' => 'url',
26				'default' => '',
27			],
28		],
29	];
30}
31
32function wikiplugin_includeurl($data, $params)
33{
34	// Validate that "url" is set.
35	if (empty($params['url'])) {
36		return tr('Missing parameter url for plugin %0', 'includeurl') . '<br>';
37	} else {
38		$url = $params['url'];
39		$html = file_get_contents($url);
40
41		// Only include the body part of the html file
42		$matches = [];
43		if (preg_match("/<body.*\/body>/s", $html, $matches)) {
44			// Find and strip the body
45			$taggedBody = $matches[0];
46			$bodyEndIdx = strpos($taggedBody, '>');
47			if ($bodyEndIdx > 0) {
48				$taggedBody = substr($taggedBody, $bodyEndIdx + 1);
49			}
50			$body = substr($taggedBody, 0, -7);
51		} else {
52			// No body tag. Return whole html
53			$body = $html;
54		}
55		return $body;
56	}
57}
58