forked from jmosmap/plugin_googlemaps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin_googlemap3_twitter_kml.php
375 lines (304 loc) · 12.6 KB
/
plugin_googlemap3_twitter_kml.php
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
<?php
/*------------------------------------------------------------------------
# plugin_googlemap3_twitter.php - Google Maps plugin
# ------------------------------------------------------------------------
# author Mike Reumer
# copyright Copyright (C) 2012 tech.reumer.net. All Rights Reserved.
# @license - http://www.gnu.org/copyleft/gpl.html GNU/GPL
# Websites: http://tech.reumer.net
# Technical Support: http://tech.reumer.net/Contact-Us/Mike-Reumer.html
# Documentation: http://tech.reumer.net/Google-Maps/Documentation-of-plugin-Googlemap/
--------------------------------------------------------------------------*/
// Restrict access to own domain
if ($_SERVER['SERVER_PORT'] == 443)
$protocol = "https://";
else
$protocol = "http://";
// No check if the referer is the website, because it can be used by Google servers too.
//header('Access-Control-Allow-Origin: '.$protocol.$_SERVER['SERVER_NAME']);
//header('Access-Control-Allow-Credentials: true');
//header('Access-Control-Allow-Methods: GET');
//
//if (array_key_exists('HTTP_ORIGIN', $_SERVER))
// $origin = $_SERVER['HTTP_ORIGIN'];
//else
// $origin = "";
//$pattern = "/(www.)?".$_SERVER['SERVER_NAME']."/i";
//if ($origin!=""&&preg_match($pattern, $origin)==0)
// die( 'Restricted access' );
//
//$refering=parse_url($_SERVER['HTTP_REFERER']);
//if($refering['host']!=$_SERVER['SERVER_NAME'])
// die( 'Restricted access' );
@define('_JEXEC', 1);
if (!defined('DS'))
@define( 'DS', DIRECTORY_SEPARATOR );
// Fix magic quotes.
@ini_set('magic_quotes_runtime', 0);
// Maximise error reporting.
//@ini_set('zend.ze1_compatibility_mode', '0');
//error_reporting(E_ALL);
//@ini_set('display_errors', 1);
/*
* Ensure that required path constants are defined.
*/
if (!defined('JPATH_BASE'))
{
$path = dirname(__FILE__);
// Joomla 1.6.x/1.7.x/2.5.x
$path = str_replace('/plugins/system/plugin_googlemap3', '', $path);
$path = str_replace('\plugins\system\plugin_googlemap3', '', $path);
define('JPATH_BASE', $path);
}
require_once ( JPATH_BASE.'/includes/defines.php' );
require_once ( JPATH_BASE.'/includes/framework.php' );
/* To use Joomla's Database Class */
require_once ( JPATH_BASE.'/libraries/joomla/factory.php' );
$mainframe = JFactory::getApplication('site');
$mainframe->initialise();
$user = JFactory::getUser();
$session = JFactory::getSession();
// No check if the referer is the website, because it can be used by Google servers too.
//JSession::checkToken( 'get' ) or die( 'Invalid Token' );
class Twitter {
private $user = null;
private $tweets = null;
function __construct($user, $twitterconsumerkey, $twitterconsumersecret, $twitteraccesstoken, $twitteraccesstokensecret) {
$this->user = $user;
require_once 'plugin_googlemap3_twitteroauth.php';
$this->twitterConnection = new TwitterOAuth(
$twitterconsumerkey, // Consumer Key
$twitterconsumersecret, // Consumer secret
$twitteraccesstoken, // Access token
$twitteraccesstokensecret // Access token secret
);
}
function getUserTimeLine($count = 19, $retweets=0) {
$ch = curl_init();
$this->tweets = $this->twitterConnection->get(
'statuses/user_timeline',
array(
'screen_name' => $this->user,
'count' => $count,
'include_rts' => $retweets
)
);
if (is_object($this->tweets)&&isset($this->tweets->errors))
$this->tweets = array();
if (count($this->tweets)==0)
$this->tweets = array();
return $this->tweets;
}
function getProfile() {
$profile = array();
if(!empty($this->tweets)&&!isset($this->tweets->errors)) {
$profile = $this->tweets[0]->user;
}
return $profile;
}
function timeSince($date) {
$datetime = strtotime($date);
$offset = time() - $datetime;
$units = array(
'second' => 1,
'minute' => 60,
'hour' => 3600,
'day' => 86400,
'month' => 2629743,
'year' => 31556926);
foreach($units as $unit => $value) {
if($offset >= $value) {
$result = floor($offset / $value);
if(!in_array($unit, array('month','year'))) {
if($result > 1) {
$unit .= 's';
}
$timeAgo = 'About'.' '.$result.' '.$unit.' '.'Ago';
} else {
return date('j M Y', $datetime);
}
}
}
return $timeAgo;
}
function parseText($text) {
// url
$text = preg_replace( "/(([[:alnum:]]+:\/\/)|www\.)([^[:space:]]*)([[:alnum:]#?\/&=])/i", "<a href=\"\\1\\3\\4\" target=\"_blank\">\\1\\3\\4</a>", ' '.$text);
$text = str_replace('href="www.', 'href="http://www.', $text);
// mailto
$text = preg_replace( "/(([a-z0-9_]|\\-|\\.)+@([^[:space:]]*)([[:alnum:]-]))/i", "<a href=\"mailto:\\1\">\\1</a>", $text);
// user
$text = preg_replace( "/ +@([a-z0-9_]*) ?/i", " <a href=\"http://twitter.com/\\1\" target=\"_blank\">@\\1</a> ", $text);
// argument
$text = preg_replace( "/ +#([a-z0-9_]*) ?/i", " <a href=\"http://twitter.com/search?q=%23\\1\" target=\"_blank\">#\\1</a> ", $text);
// truncates long url
$text = preg_replace("/>(([[:alnum:]]+:\/\/)|www\.)([^[:space:]]{30,40})([^[:space:]]*)([^[:space:]]{10,20})([[:alnum:]#?\/&=])</", ">\\3...\\5\\6<", $text);
return trim($text);
}
}
class plugin_googlemap3_twitter_kml
{
/**
* Display the application.
*/
function doExecute(){
// Get config
$plugin = JPluginHelper::getPlugin('system', 'plugin_googlemap3');
$jversion = JVERSION;
$params = new JRegistry();
$params->loadString($plugin->params);
// Get params
$twitterconsumerkey = $params->get('twitterconsumerkey', '');
$twitterconsumersecret = $params->get('twitterconsumersecret', '');
$twitteraccesstoken = $params->get('twitteraccesstoken', '');
$twitteraccesstokensecret = $params->get('twitteraccesstokensecret', '');
$twittername = JRequest::getVar('twittername', '');
if (!is_string($twittername))
$twittername ="";
else
$twittername = urldecode($twittername);
if ($twittername=="")
$twittername = $params->get('twittername', '');
$twittertweets = JRequest::getVar('twittertweets', '');
if (!is_string($twittertweets))
$twittertweets ="";
else
$twittertweets = urldecode($twittertweets);
if ($twittertweets=="")
$twittertweets = $params->get('twittertweets', '15');
$line = JRequest::getVar('twitterline', '');
if (!is_string($line))
$line ="";
else
$line = urldecode($line);
if ($line=="")
$line = $params->get('twitterline', '');
$twitterlinewidth = JRequest::getVar('twitterlinewidth', '');
if (!is_string($twitterlinewidth))
$twitterlinewidth ="";
else
$twitterlinewidth = urldecode($twitterlinewidth);
if ($twitterlinewidth=="")
$twitterlinewidth = $params->get('twitterlinewidth', '5');
$twitterstartloc = JRequest::getVar('twitterstartloc', '');
if (!is_string($twitterstartloc))
$twitterstartloc ="";
else
$twitterstartloc = urldecode($twitterstartloc);
if ($twitterstartloc=="")
$twitterstartloc = $params->get('twitterstartloc', '5');
$twitter = new Twitter(ltrim(rtrim($twittername)), $twitterconsumerkey, $twitterconsumersecret, $twitteraccesstoken, $twitteraccesstokensecret);
$tweets = $twitter->getUserTimeLine(ltrim(rtrim($twittertweets)), 1);
$profile = $twitter->getProfile();
// Start KML file, create parent node
$dom = new DOMDocument('1.0','UTF-8');
//Create the root KML element and append it to the Document
$node = $dom->createElementNS('http://earth.google.com/kml/2.1','kml');
$parNode = $dom->appendChild($node);
//Create a Folder element and append it to the KML element
$docnode = $dom->createElement('Document');
$parNode = $parNode->appendChild($docnode);
$twitterStyleNode = $dom->createElement('Style');
$twitterStyleNode->setAttribute('id', 'tweetStyle');
$twitterIconstyleNode = $dom->createElement('IconStyle');
$twitterIconstyleNode->setAttribute('id', 'tweetIcon');
$twitterIconNode = $dom->createElement('Icon');
$twitterHref = $dom->createElement('href', $params->get('twittericon', ''));
$twitterIconNode->appendChild($twitterHref);
$twitterIconstyleNode->appendChild($twitterIconNode);
$twitterStyleNode->appendChild($twitterIconstyleNode);
$docnode->appendChild($twitterStyleNode);
if ($line!='') {
// Create a line of travelling
$twitterStyleNode = $dom->createElement('Style');
$twitterStyleNode->setAttribute('id', 'lineStyle');
$twitterLinestyleNode = $dom->createElement('LineStyle');
$twitterColorNode = $dom->createElement('color', ltrim(rtrim($line)));
$twitterLinestyleNode->appendChild($twitterColorNode);
$twitterWidthNode = $dom->createElement('width', ltrim(rtrim($twitterlinewidth)));
$twitterLinestyleNode->appendChild($twitterWidthNode);
$twitterStyleNode->appendChild($twitterLinestyleNode);
$docnode->appendChild($twitterStyleNode);
}
//Create a Folder element and append it to the KML element
$fnode = $dom->createElement('Folder');
$folderNode = $parNode->appendChild($fnode);
$nameNode = $dom->createElement('name', 'Tweets '.$twittername);
$folderNode->appendChild($nameNode);
$tweets = array_reverse($tweets);
$prev_location = explode(',', $twitterstartloc);
// swap lat and long values. In kml is it different first long then lat
$lat = $prev_location[0];
$prev_location[0] = $prev_location[1];
$prev_location[1] = $lat;
foreach($tweets as $tweet) {
if ($tweet->coordinates=="")
$tweet->coordinates->coordinates = $prev_location;
else
$prev_location = $tweet->coordinates->coordinates;
}
$tweets = array_reverse($tweets);
foreach($tweets as $tweet) {
//Create a Placemark and append it to the document
$node = $dom->createElement('Placemark');
$placeNode = $folderNode->appendChild($node);
//Create an id attribute and assign it the value of id column
$placeNode->setAttribute('id','tweet_'.$tweet->id_str);
//Create name, description, and address elements and assign them the values of
//the name, type, and address columns from the results
$nameNode = $dom->createElement('name', date('d m Y g:i:s', strtotime($tweet->created_at)));
$placeNode->appendChild($nameNode);
$styleUrl = $dom->createElement('styleUrl', '#tweetStyle');
$placeNode->appendChild($styleUrl);
$descText = "";
$descText .="<a href='http://www.twitter.com/".$profile->screen_name."' target='_blank' title='Follow us'><h4 class='tw_user'><img src='".$profile->profile_image_url."' alt='".$profile->name."' />".$profile->name."</h4></a>";
$descText .= "<span class='tw_text'>".$twitter->parseText($tweet->text)."</span>";
$descText .="<br/><span class='tw_date'>".$twitter->timeSince($tweet->created_at)."</span>";
$descNode = $dom->createElement('description', '');
$cdataNode = $dom->createCDATASection($descText);
$descNode->appendChild($cdataNode);
$placeNode->appendChild($descNode);
$pointNode = $dom->createElement('Point');
$placeNode->appendChild($pointNode);
$coor_pointNode = $dom->createElement('coordinates',implode(",",$tweet->coordinates->coordinates));
$pointNode->appendChild($coor_pointNode);
}
if ($line!=''&&count($tweets)>0) {
// Create a line of travelling
//Create a Placemark and append it to the document
$node = $dom->createElement('Placemark');
$placeNode = $folderNode->appendChild($node);
//Create an id attribute and assign it the value of id column
$placeNode->setAttribute('id','tweetline');
//Create name, description, and address elements and assign them the values of
//the name, type, and address columns from the results
$nameNode = $dom->createElement('name','');
$placeNode->appendChild($nameNode);
$styleUrl = $dom->createElement('styleUrl', '#lineStyle');
$placeNode->appendChild($styleUrl);
//Create a LineString element
$lineNode = $dom->createElement('LineString');
$placeNode->appendChild($lineNode);
$exnode = $dom->createElement('extrude', '1');
$lineNode->appendChild($exnode);
$almodenode =$dom->createElement('altitudeMode','relativeToGround');
$lineNode->appendChild($almodenode);
$coordinates = "";
foreach($tweets as $tweet) {
$coordinates .= " ".implode(",",$tweet->coordinates->coordinates);
}
//Create a coordinates element and give it the value of the lng and lat columns from the results
$coorNode = $dom->createElement('coordinates',$coordinates);
$lineNode->appendChild($coorNode);
}
$kmlOutput = $dom->saveXML();
//assign the KML headers.
header('Content-type: application/vnd.google-earth.kml+xml');
echo $kmlOutput;
}
}
// Instantiate the application.
$web = new plugin_googlemap3_twitter_kml;
// Run the application
$web->doExecute();
?>