PageRenderTime 48ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/media/widgetkit/widgets/twitter/twitter.php

https://bitbucket.org/organicdevelopment/joomla-2.5
PHP | 275 lines | 143 code | 64 blank | 68 comment | 30 complexity | 2945fc6442284a71b94ab16e56506534 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-2.0, MIT, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * @package Widgetkit
  4. * @author YOOtheme http://www.yootheme.com
  5. * @copyright Copyright (C) YOOtheme GmbH
  6. * @license http://www.gnu.org/licenses/gpl.html GNU/GPL
  7. */
  8. /*
  9. Class: TwitterWidgetkitHelper
  10. Twitter helper class
  11. */
  12. class TwitterWidgetkitHelper extends WidgetkitHelper {
  13. /* type */
  14. public $type;
  15. /* options */
  16. public $options;
  17. /*
  18. Function: Constructor
  19. Class Constructor.
  20. */
  21. public function __construct($widgetkit) {
  22. parent::__construct($widgetkit);
  23. // init vars
  24. $this->type = strtolower(str_replace('WidgetkitHelper', '', get_class($this)));
  25. $this->options = $this['system']->options;
  26. // create cache
  27. $cache = $this['path']->path('cache:');
  28. if ($cache && !file_exists($cache.'/twitter')) {
  29. mkdir($cache.'/twitter', 0777, true);
  30. }
  31. // register path
  32. $this['path']->register(dirname(__FILE__), $this->type);
  33. }
  34. /*
  35. Function: site
  36. Site init actions
  37. Returns:
  38. Void
  39. */
  40. public function site() {
  41. // add translations
  42. foreach (array('LESS_THAN_A_MINUTE_AGO', 'ABOUT_A_MINUTE_AGO', 'X_MINUTES_AGO', 'ABOUT_AN_HOUR_AGO', 'X_HOURS_AGO', 'ONE_DAY_AGO', 'X_DAYS_AGO') as $key) {
  43. $translations[$key] = $this['system']->__($key);
  44. }
  45. // add stylesheets/javascripts
  46. $this['asset']->addFile('css', 'twitter:styles/style.css');
  47. $this['asset']->addFile('js', 'twitter:twitter.js');
  48. $this['asset']->addString('js', sprintf('$widgetkit.trans.addDic(%s);', json_encode($translations)));
  49. // rtl
  50. if ($this['system']->options->get('direction') == 'rtl') {
  51. $this['asset']->addFile('css', 'twitter:styles/rtl.css');
  52. }
  53. }
  54. /*
  55. Function: render
  56. Render widget on site
  57. Returns:
  58. String
  59. */
  60. public function render($options) {
  61. if ($tweets = $this->_getTweets($options)) {
  62. // get options
  63. extract($options);
  64. return $this['template']->render("twitter:styles/$style/template", compact('tweets', 'show_image', 'show_author', 'show_date', 'image_size'));
  65. }
  66. return 'No tweets found.';
  67. }
  68. /*
  69. Function: _getURL
  70. Create Twitter Query URL
  71. Returns:
  72. String
  73. */
  74. protected function _getURL($options) {
  75. // get options
  76. extract($options);
  77. // clean options
  78. foreach (array('from_user', 'to_user', 'ref_user', 'word', 'nots', 'hashtag') as $var) {
  79. $$var = preg_replace('/[@#]/', '', preg_replace('/\s+/', ' ', trim($$var)));
  80. }
  81. // build query
  82. $query = array();
  83. if ($from_user) {
  84. $query[] = 'from:'.str_replace(' ', ' OR from:', $from_user);
  85. }
  86. if ($to_user) {
  87. $query[] = 'to:'.str_replace(' ', ' OR to:', $to_user);
  88. }
  89. if ($ref_user) {
  90. $query[] = '@'.str_replace(' ', ' @', $ref_user);
  91. }
  92. if ($word) {
  93. $query[] = $word;
  94. }
  95. if ($nots) {
  96. $query[] = '-'.str_replace(' ', ' -', $nots);
  97. }
  98. if ($hashtag) {
  99. $query[] = '#'.str_replace(' ', ' #', $hashtag);
  100. }
  101. $limit = min($limit ? intval($limit) : 5, 100);
  102. // build timeline url
  103. if ($from_user && !strpos($from_user, ' ') && count($query) == 1) {
  104. $url = 'http://twitter.com/statuses/user_timeline/'.strtolower($from_user).'.json';
  105. if ($limit > 15) {
  106. $url .= '?count='.$limit;
  107. }
  108. return $url;
  109. }
  110. // build search url
  111. if (count($query)) {
  112. $url = 'http://search.twitter.com/search.json?q='.urlencode(implode(' ', $query));
  113. if ($limit > 15) {
  114. $url .= '&rpp='.$limit;
  115. }
  116. return $url;
  117. }
  118. return null;
  119. }
  120. /*
  121. Function: _getTweets
  122. Get Tweet Object Array
  123. Returns:
  124. Array
  125. */
  126. protected function _getTweets($options) {
  127. // init vars
  128. $tweets = array();
  129. // query twitter
  130. if ($url = $this->_getURL($options)) {
  131. if ($path = $this['path']->path('cache:twitter')) {
  132. $file = rtrim($path, '/').sprintf('/twitter-%s.php', md5($url));
  133. // is cached ?
  134. if (file_exists($file)) {
  135. $response = file_get_contents($file);
  136. }
  137. // refresh cache ?
  138. if (!file_exists($file) || (time() - filemtime($file)) > 300) {
  139. // send query
  140. $request = $this['http']->get($url);
  141. if (isset($request['status']['code']) && $request['status']['code'] == 200) {
  142. $response = $request['body'];
  143. file_put_contents($file, $response);
  144. }
  145. }
  146. }
  147. }
  148. // create tweets
  149. if (isset($response)) {
  150. $response = json_decode($response, true);
  151. if (is_array($response)) {
  152. if (isset($response['results'])) {
  153. foreach ($response['results'] as $res) {
  154. $tweet = new WidgetkitTweet();
  155. $tweet->id = $res['id_str'];
  156. $tweet->user = $res['from_user'];
  157. $tweet->name = $res['from_user'];
  158. $tweet->image = $res['profile_image_url'];
  159. $tweet->text = $res['text'];
  160. $tweet->created_at = $res['created_at'];
  161. $tweets[] = $tweet;
  162. }
  163. } else {
  164. foreach ($response as $res) {
  165. $tweet = new WidgetkitTweet();
  166. $tweet->id = $res['id_str'];
  167. $tweet->user = $res['user']['screen_name'];
  168. $tweet->name = $res['user']['name'];
  169. $tweet->image = $res['user']['profile_image_url'];
  170. $tweet->text = $res['text'];
  171. $tweet->created_at = $res['created_at'];
  172. $tweets[] = $tweet;
  173. }
  174. }
  175. }
  176. }
  177. return array_slice($tweets, 0, $options['limit'] ? intval($options['limit']) : 5);
  178. }
  179. }
  180. /*
  181. Class: WidgetkitTweet
  182. Widgetkit Twitter Tweet.
  183. */
  184. class WidgetkitTweet {
  185. public $id;
  186. public $user;
  187. public $name;
  188. public $image;
  189. public $text;
  190. public $created_at;
  191. public function getLink() {
  192. return 'http://twitter.com/'.$this->user;
  193. }
  194. public function getStatusLink() {
  195. return 'http://twitter.com/'.$this->user.'/statuses/'.$this->id;
  196. }
  197. public function getText() {
  198. // format text
  199. $text = preg_replace('@(https?://([-\w\.]+)+(/([\w/_\.]*(\?\S+)?(#\S+)?)?)?)@', '<a href="$1">$1</a>', $this->text);
  200. $text = preg_replace('/@(\w+)/', '<a href="http://twitter.com/$1">@$1</a>', $text);
  201. $text = preg_replace('/\s+#(\w+)/', ' <a href="http://search.twitter.com/search?q=%23$1">#$1</a>', $text);
  202. return $text;
  203. }
  204. }
  205. // bind events
  206. $widgetkit = Widgetkit::getInstance();
  207. $widgetkit['event']->bind('site', array($widgetkit['twitter'], 'site'));