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_twitter_info()
9{
10	return [
11		'name' => tra('Twitter'),
12		'documentation' => 'PluginTwitter',
13		'description' => tra('Display the activity for a twitter account'),
14		'prefs' => ['wikiplugin_twitter'],
15		'body' => '',
16		'iconname' => 'twitter',
17		'introduced' => 7,
18		'params' => [
19			'tweet' => [
20				'required' => true,
21				'name' => tra('Twitter Path'),
22				'description' => tr(
23					'Depends on the type of timeline (Users, Collections, Favorites or Lists). For a
24					 User, it is the Account Name (like %0twitterdev%1), for Favorites, something like
25					 %0twitterdev/favorites%1. For lists, something like %0twitterdev/lists/listname%1, etc. ',
26					'<code>',
27					'</code>'
28				),
29				'since' => '7.0',
30				'filter' => 'text',
31				'default' => ''
32			],
33			'widgetId' => [
34				'required' => true,
35				'name' => tra('Widget Id'),
36				'description' => tra('Numeric identifier of the widget'),
37				'since' => '13.1',
38				'filter' => 'digits',
39				'default' => ''
40			],
41			'theme' => [
42				'required' => false,
43				'name' => tra('Theme'),
44				'description' => tr('Embedded timelines are available in light and dark themes for customization.
45					The light theme is for pages that use a light colored background, while the dark theme is for pages
46					that use a dark colored background. Default is %0light%1.', '<code>', '</code>'),
47				'since' => '13.1',
48				'filter' => 'alpha',
49				'options' => [
50					['text' => '', 'value' => ''],
51					['text' => tra('light'), 'value' => 'light'],
52					['text' => tra('dark'), 'value' => 'dark'],
53				],
54				'default' => 'light'
55			],
56			'tweetcolor' => [
57				'required' => false,
58				'name' => tra('Link Color'),
59				'description' => tra('Text color for individual tweets. Default is theme default.'),
60				'since' => '7.0',
61				'accepted' => tra('Valid HTML color codes (with beginning #) or names.'),
62				'filter' => 'text'
63			],
64			'tweetbg' => [
65				'required' => false,
66				'name' => tra('Border Color'),
67				'description' => tra('Change the border color used by the widget. Default is theme default.'),
68				'since' => '7.0',
69				'accepted' => tra('Valid HTML color codes (with beginning #) or names.'),
70				'filter' => 'text'
71			],
72			'height' => [
73				'required' => false,
74				'name' => tra('Height'),
75				'description' => tr('Height of widget in pixels. Default is %0.', '<code>300</code>'),
76				'since' => '7.0',
77				'filter' => 'digits',
78				'default' => 300
79			],
80			'width' => [
81				'required' => false,
82				'name' => tra('Width'),
83				'description' => tr(
84					'Width of widget in pixels or \'auto\' to fit to width of page. Default is %0.',
85					'<code>auto</code>'
86				),
87				'accepted' => tra('Number of pixels or the word \'auto\'.'),
88				'since' => '7.0',
89				'filter' => 'text',
90				'default' => 'auto'
91			],
92			'noheader' => [
93				'required' => false,
94				'advanced' => true,
95				'name' => tra('No Header'),
96				'description' => tra('Default is with Header'),
97				'since' => '13.1',
98				'filter' => 'text',
99				'options' => [
100					['text' => tra('Header'), 'value' => ''],
101					['text' => tra('No Header'), 'value' => 'y'],
102				],
103			],
104			'nofooter' => [
105				'required' => false,
106				'advanced' => true,
107				'name' => tra('No Footer'),
108				'description' => tra('Default is with Footer'),
109				'since' => '13.1',
110				'filter' => 'text',
111				'options' => [
112					['text' => tra('Footer'), 'value' => ''],
113					['text' => tra('No Footer'), 'value' => 'y'],
114				],
115			],
116			'noborders' => [
117				'required' => false,
118				'advanced' => true,
119				'name' => tra('Layout Option: No Borders'),
120				'description' => tra('Default is with Borders'),
121				'since' => '13.1',
122				'filter' => 'text',
123				'options' => [
124					['text' => tra('Borders'), 'value' => ''],
125					['text' => tra('No Borders'), 'value' => 'y'],
126				],
127			],
128			'noscrollbar' => [
129				'required' => false,
130				'advanced' => true,
131				'name' => tra('No Scrollbar'),
132				'description' => tra('Default is with Scrollbar'),
133				'since' => '13.1',
134				'filter' => 'text',
135				'options' => [
136					['text' => tra('Scrollbar'), 'value' => ''],
137					['text' => tra('No Scrollbar'), 'value' => 'y'],
138				],
139			],
140			'shellbg' => [
141				'required' => false,
142				'advanced' => true,
143				'name' => tra('Transparent Background'),
144				'description' => tra('Transparent Shell Background. Default is theme default'),
145				'since' => '7.0',
146				'filter' => 'text',
147				'options' => [
148					['text' => tra('Theme default'), 'value' => ''],
149					['text' => tra('Transparent'), 'value' => 'transparent'],
150				],
151			],
152		],
153	];
154}
155
156function wikiplugin_twitter($data, $params)
157{
158	$default = ['shellbg' => '', 'shellcolor' => '', 'tweetbg' => '', 'tweetcolor' => '', 'width' => 'auto', 'height' => 300];
159	$params = array_merge($default, $params);
160	extract($params, EXTR_SKIP);
161
162	// Variables sanitizing
163	$tweetlimit = (int)$tweetlimit;
164	$tweetbg = preg_replace('/[^#0-9a-zA-Z]/', '', $tweetbg);
165	$tweetcolor = preg_replace('/[^#0-9a-zA-Z]/', '', $tweetcolor);
166	$tweet = preg_replace('/[^#0-9a-zA-Z%\/=]/', '', $tweet);
167	$widgetId = preg_replace('/[^0-9]/', '', $widgetId);
168	if ($theme != 'dark') {
169		$theme = 'light';
170	}
171	$datachrome = [];
172	if ($noheader == 'y') {
173		$datachrome[] = 'noheader';
174	}
175	if ($nofooter == 'y') {
176		$datachrome[] = 'nofooter';
177	}
178	if ($noborders == 'y') {
179		$datachrome[] = 'noborders';
180	}
181	if ($noscrollbar == 'y') {
182		$datachrome[] = 'noscrollbar';
183	}
184	if ($shellbg == 'transparent') {
185		$datachrome[] = 'transparent' ;
186	}
187	if (count($datachrome) > 0) {
188		$datachromehtml = "data-chrome=' " . implode(' ', $datachrome) . "' ";
189	}
190	if ($width != 'auto') {
191		$width = preg_replace('/[^0-9]/', '', $width);
192	}
193	$height = (int)$height;
194
195	// Inspiration: http://stackoverflow.com/questions/14303710/how-to-customize-twitter-widget-style
196	// and https://dev.twitter.com/web/embedded-timelines
197	// Note: the $widgetId is more important than the $tweet in defining what is displayed
198	$html = "<a class=\"twitter-timeline\"  href=\"https://twitter.com/$tweet\" data-widget-id=\"$widgetId\"
199$datachromehtml
200" . (empty($tweetlimit) ? '' : " data-tweet-limit='$tweetlimit'\n") .
201	(empty($tweetcolor) ? "" : " data-link-color='$tweetcolor'\n") .
202	(empty($tweetbg) ? "" : " data-border-color='$tweetbg'\n") .
203	"data-theme='$theme'
204height='$height'
205width='$width'
206" .
207	"data-show-replies='false'
208data-aria-polite='polite'>Tweets from @$tweet</a>
209<script>
210!function(d,s,id){
211	var js,fjs=d.getElementsByTagName(s)[0];
212
213	if(!d.getElementById(id)){
214		js=d.createElement(s);
215		js.id=id;
216		js.src=\"http://platform.twitter.com/widgets.js\";
217		fjs.parentNode.insertBefore(js,fjs);
218	}
219
220}(document,'script','twitter-wjs');
221</script>";
222
223	//debug return '~np~'.nl2br(htmlspecialchars($html)).'~/np~';
224	//debug return '~np~'.$html.nl2br(htmlspecialchars($html)).'~/np~';
225
226	return '~np~' . $html . '~/np~';
227}
228