PageRenderTime 42ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/web herbalspa.net php/web herbalspa.net/plugins/system/plugin_googlemap3/plugin_googlemap3_twitter_kml.php

https://gitlab.com/phamngsinh/baitaplon_sinhvien
PHP | 347 lines | 222 code | 68 blank | 57 comment | 21 complexity | 58e98ecc318a6f07042619c2771aeb67 MD5 | raw file
  1. <?php
  2. /*------------------------------------------------------------------------
  3. # plugin_googlemap3_twitter.php - Google Maps plugin
  4. # ------------------------------------------------------------------------
  5. # author Mike Reumer
  6. # copyright Copyright (C) 2012 tech.reumer.net. All Rights Reserved.
  7. # @license - http://www.gnu.org/copyleft/gpl.html GNU/GPL
  8. # Websites: http://tech.reumer.net
  9. # Technical Support: http://tech.reumer.net/Contact-Us/Mike-Reumer.html
  10. # Documentation: http://tech.reumer.net/Google-Maps/Documentation-of-plugin-Googlemap/
  11. --------------------------------------------------------------------------*/
  12. @define('_JEXEC', 1);
  13. if (!defined('DS'))
  14. @define( 'DS', DIRECTORY_SEPARATOR );
  15. // Fix magic quotes.
  16. @ini_set('magic_quotes_runtime', 0);
  17. // Maximise error reporting.
  18. //@ini_set('zend.ze1_compatibility_mode', '0');
  19. //error_reporting(E_ALL);
  20. //@ini_set('display_errors', 1);
  21. /*
  22. * Ensure that required path constants are defined.
  23. */
  24. if (!defined('JPATH_BASE'))
  25. {
  26. $path = dirname(__FILE__);
  27. // Joomla 1.6.x/1.7.x/2.5.x
  28. $path = str_replace('/plugins/system/plugin_googlemap3', '', $path);
  29. $path = str_replace('\plugins\system\plugin_googlemap3', '', $path);
  30. // Joomla 1.5.x
  31. $path = str_replace('/plugins/system', '', $path);
  32. $path = str_replace('\plugins\system', '', $path);
  33. define('JPATH_BASE', $path);
  34. }
  35. require_once ( JPATH_BASE.'/includes/defines.php' );
  36. if (!file_exists(JPATH_LIBRARIES . '/import.legacy.php')) {
  37. // Joomla 1.5
  38. require_once ( JPATH_BASE.'/includes/framework.php' );
  39. /* To use Joomla's Database Class */
  40. require_once ( JPATH_BASE.'/libraries/joomla/factory.php' );
  41. $mainframe = JFactory::getApplication('site');
  42. $mainframe->initialise();
  43. $user = JFactory::getUser();
  44. $session = JFactory::getSession();
  45. } else {
  46. // Joomla 1.6.x/1.7.x/2.5.x
  47. /**
  48. * Import the platform. This file is usually in JPATH_LIBRARIES
  49. */
  50. require_once JPATH_BASE . '/configuration.php';
  51. require_once JPATH_LIBRARIES . '/import.legacy.php';
  52. }
  53. class Twitter {
  54. private $user = null;
  55. private $tweets = null;
  56. function __construct($user, $twitterconsumerkey, $twitterconsumersecret, $twitteraccesstoken, $twitteraccesstokensecret) {
  57. $this->user = $user;
  58. require_once 'plugin_googlemap3_twitteroauth.php';
  59. $this->twitterConnection = new TwitterOAuth(
  60. $twitterconsumerkey, // Consumer Key
  61. $twitterconsumersecret, // Consumer secret
  62. $twitteraccesstoken, // Access token
  63. $twitteraccesstokensecret // Access token secret
  64. );
  65. }
  66. function getUserTimeLine($count = 19, $retweets=0) {
  67. $ch = curl_init();
  68. $this->tweets = $this->twitterConnection->get(
  69. 'statuses/user_timeline',
  70. array(
  71. 'screen_name' => $this->user,
  72. 'count' => $count,
  73. 'include_rts' => $retweets
  74. )
  75. );
  76. if (is_object($this->tweets)&&isset($this->tweets->errors))
  77. $this->tweets = array();
  78. if (count($this->tweets)==0)
  79. $this->tweets = array();
  80. return $this->tweets;
  81. }
  82. function getProfile() {
  83. $profile = array();
  84. if(!empty($this->tweets)&&!isset($this->tweets->errors)) {
  85. $profile = $this->tweets[0]->user;
  86. }
  87. return $profile;
  88. }
  89. function timeSince($date) {
  90. $datetime = strtotime($date);
  91. $offset = time() - $datetime;
  92. $units = array(
  93. 'second' => 1,
  94. 'minute' => 60,
  95. 'hour' => 3600,
  96. 'day' => 86400,
  97. 'month' => 2629743,
  98. 'year' => 31556926);
  99. foreach($units as $unit => $value) {
  100. if($offset >= $value) {
  101. $result = floor($offset / $value);
  102. if(!in_array($unit, array('month','year'))) {
  103. if($result > 1) {
  104. $unit .= 's';
  105. }
  106. $timeAgo = 'About'.' '.$result.' '.$unit.' '.'Ago';
  107. } else {
  108. return date('j M Y', $datetime);
  109. }
  110. }
  111. }
  112. return $timeAgo;
  113. }
  114. function parseText($text) {
  115. // url
  116. $text = preg_replace( "/(([[:alnum:]]+:\/\/)|www\.)([^[:space:]]*)([[:alnum:]#?\/&=])/i", "<a href=\"\\1\\3\\4\" target=\"_blank\">\\1\\3\\4</a>", ' '.$text);
  117. $text = str_replace('href="www.', 'href="http://www.', $text);
  118. // mailto
  119. $text = preg_replace( "/(([a-z0-9_]|\\-|\\.)+@([^[:space:]]*)([[:alnum:]-]))/i", "<a href=\"mailto:\\1\">\\1</a>", $text);
  120. // user
  121. $text = preg_replace( "/ +@([a-z0-9_]*) ?/i", " <a href=\"http://twitter.com/\\1\" target=\"_blank\">@\\1</a> ", $text);
  122. // argument
  123. $text = preg_replace( "/ +#([a-z0-9_]*) ?/i", " <a href=\"http://twitter.com/search?q=%23\\1\" target=\"_blank\">#\\1</a> ", $text);
  124. // truncates long url
  125. $text = preg_replace("/>(([[:alnum:]]+:\/\/)|www\.)([^[:space:]]{30,40})([^[:space:]]*)([^[:space:]]{10,20})([[:alnum:]#?\/&=])</", ">\\3...\\5\\6<", $text);
  126. return trim($text);
  127. }
  128. }
  129. class plugin_googlemap3_twitter_kml
  130. {
  131. /**
  132. * Display the application.
  133. */
  134. function doExecute(){
  135. // Get config
  136. $plugin = JPluginHelper::getPlugin('system', 'plugin_googlemap3');
  137. $jversion = JVERSION;
  138. // In Joomla 1.5 get the parameters in Joomla 1.6 and higher the plugin already has them, but need to be rendered with JRegistry
  139. if (substr($jversion,0,3)=="1.5")
  140. $params = new JParameter($plugin->params);
  141. else {
  142. $params = new JRegistry();
  143. $params->loadString($plugin->params);
  144. }
  145. // Get params
  146. $twitterconsumerkey = $params->get('twitterconsumerkey', '');
  147. $twitterconsumersecret = $params->get('twitterconsumersecret', '');
  148. $twitteraccesstoken = $params->get('twitteraccesstoken', '');
  149. $twitteraccesstokensecret = $params->get('twitteraccesstokensecret', '');
  150. $twittername = urldecode(JRequest::getVar('twittername', ''));
  151. if ($twittername=="")
  152. $twittername = $params->get('twittername', '');
  153. $twittertweets = urldecode(JRequest::getVar('twittertweets', ''));
  154. if ($twittertweets=="")
  155. $twittertweets = $params->get('twittertweets', '15');
  156. $line = urldecode(JRequest::getVar('twitterline', ''));
  157. if ($line=="")
  158. $line = $params->get('twitterline', '');
  159. $twitterlinewidth = urldecode(JRequest::getVar('twitterlinewidth', ''));
  160. if ($twitterlinewidth=="")
  161. $twitterlinewidth = $params->get('twitterlinewidth', '5');
  162. $twitterstartloc = urldecode(JRequest::getVar('twitterstartloc', ''));
  163. if ($twitterstartloc=="")
  164. $twitterstartloc = $params->get('twitterstartloc', '5');
  165. $twitter = new Twitter(ltrim(rtrim($twittername)), $twitterconsumerkey, $twitterconsumersecret, $twitteraccesstoken, $twitteraccesstokensecret);
  166. $tweets = $twitter->getUserTimeLine(ltrim(rtrim($twittertweets)), 1);
  167. $profile = $twitter->getProfile();
  168. // Start KML file, create parent node
  169. $dom = new DOMDocument('1.0','UTF-8');
  170. //Create the root KML element and append it to the Document
  171. $node = $dom->createElementNS('http://earth.google.com/kml/2.1','kml');
  172. $parNode = $dom->appendChild($node);
  173. //Create a Folder element and append it to the KML element
  174. $docnode = $dom->createElement('Document');
  175. $parNode = $parNode->appendChild($docnode);
  176. $twitterStyleNode = $dom->createElement('Style');
  177. $twitterStyleNode->setAttribute('id', 'tweetStyle');
  178. $twitterIconstyleNode = $dom->createElement('IconStyle');
  179. $twitterIconstyleNode->setAttribute('id', 'tweetIcon');
  180. $twitterIconNode = $dom->createElement('Icon');
  181. $twitterHref = $dom->createElement('href', $params->get('twittericon', ''));
  182. $twitterIconNode->appendChild($twitterHref);
  183. $twitterIconstyleNode->appendChild($twitterIconNode);
  184. $twitterStyleNode->appendChild($twitterIconstyleNode);
  185. $docnode->appendChild($twitterStyleNode);
  186. if ($line!='') {
  187. // Create a line of travelling
  188. $twitterStyleNode = $dom->createElement('Style');
  189. $twitterStyleNode->setAttribute('id', 'lineStyle');
  190. $twitterLinestyleNode = $dom->createElement('LineStyle');
  191. $twitterColorNode = $dom->createElement('color', ltrim(rtrim($line)));
  192. $twitterLinestyleNode->appendChild($twitterColorNode);
  193. $twitterWidthNode = $dom->createElement('width', ltrim(rtrim($twitterlinewidth)));
  194. $twitterLinestyleNode->appendChild($twitterWidthNode);
  195. $twitterStyleNode->appendChild($twitterLinestyleNode);
  196. $docnode->appendChild($twitterStyleNode);
  197. }
  198. //Create a Folder element and append it to the KML element
  199. $fnode = $dom->createElement('Folder');
  200. $folderNode = $parNode->appendChild($fnode);
  201. $nameNode = $dom->createElement('name', 'Tweets '.$twittername);
  202. $folderNode->appendChild($nameNode);
  203. $tweets = array_reverse($tweets);
  204. $prev_location = explode(',', $twitterstartloc);
  205. // swap lat and long values. In kml is it different first long then lat
  206. $lat = $prev_location[0];
  207. $prev_location[0] = $prev_location[1];
  208. $prev_location[1] = $lat;
  209. foreach($tweets as $tweet) {
  210. if ($tweet->coordinates=="")
  211. $tweet->coordinates->coordinates = $prev_location;
  212. else
  213. $prev_location = $tweet->coordinates->coordinates;
  214. }
  215. $tweets = array_reverse($tweets);
  216. foreach($tweets as $tweet) {
  217. //Create a Placemark and append it to the document
  218. $node = $dom->createElement('Placemark');
  219. $placeNode = $folderNode->appendChild($node);
  220. //Create an id attribute and assign it the value of id column
  221. $placeNode->setAttribute('id','tweet_'.$tweet->id_str);
  222. //Create name, description, and address elements and assign them the values of
  223. //the name, type, and address columns from the results
  224. $nameNode = $dom->createElement('name', date('d m Y g:i:s', strtotime($tweet->created_at)));
  225. $placeNode->appendChild($nameNode);
  226. $styleUrl = $dom->createElement('styleUrl', '#tweetStyle');
  227. $placeNode->appendChild($styleUrl);
  228. $descText = "";
  229. $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>";
  230. $descText .= "<span class='tw_text'>".$twitter->parseText($tweet->text)."</span>";
  231. $descText .="<br/><span class='tw_date'>".$twitter->timeSince($tweet->created_at)."</span>";
  232. $descNode = $dom->createElement('description', '');
  233. $cdataNode = $dom->createCDATASection($descText);
  234. $descNode->appendChild($cdataNode);
  235. $placeNode->appendChild($descNode);
  236. $pointNode = $dom->createElement('Point');
  237. $placeNode->appendChild($pointNode);
  238. $coor_pointNode = $dom->createElement('coordinates',implode(",",$tweet->coordinates->coordinates));
  239. $pointNode->appendChild($coor_pointNode);
  240. }
  241. if ($line!=''&&count($tweets)>0) {
  242. // Create a line of travelling
  243. //Create a Placemark and append it to the document
  244. $node = $dom->createElement('Placemark');
  245. $placeNode = $folderNode->appendChild($node);
  246. //Create an id attribute and assign it the value of id column
  247. $placeNode->setAttribute('id','tweetline');
  248. //Create name, description, and address elements and assign them the values of
  249. //the name, type, and address columns from the results
  250. $nameNode = $dom->createElement('name','');
  251. $placeNode->appendChild($nameNode);
  252. $styleUrl = $dom->createElement('styleUrl', '#lineStyle');
  253. $placeNode->appendChild($styleUrl);
  254. //Create a LineString element
  255. $lineNode = $dom->createElement('LineString');
  256. $placeNode->appendChild($lineNode);
  257. $exnode = $dom->createElement('extrude', '1');
  258. $lineNode->appendChild($exnode);
  259. $almodenode =$dom->createElement('altitudeMode','relativeToGround');
  260. $lineNode->appendChild($almodenode);
  261. $coordinates = "";
  262. foreach($tweets as $tweet) {
  263. $coordinates .= " ".implode(",",$tweet->coordinates->coordinates);
  264. }
  265. //Create a coordinates element and give it the value of the lng and lat columns from the results
  266. $coorNode = $dom->createElement('coordinates',$coordinates);
  267. $lineNode->appendChild($coorNode);
  268. }
  269. $kmlOutput = $dom->saveXML();
  270. //assign the KML headers.
  271. header('Content-type: application/vnd.google-earth.kml+xml');
  272. echo $kmlOutput;
  273. }
  274. }
  275. // Instantiate the application.
  276. $web = new plugin_googlemap3_twitter_kml;
  277. // Run the application
  278. $web->doExecute();
  279. ?>