A modest collection of PHP libraries used at SparkFun.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
3.4 KiB

  1. <?php
  2. namespace SparkLib\SocialNoise;
  3. use \SparkLib\Fail;
  4. /**
  5. * This is based on:
  6. *
  7. * Plugin Name: PlanetTwitter
  8. * Description: Pulls in a feed based on a search term passed to it using
  9. * this format: [planettwitter:searchterm]
  10. * Version: 1.1
  11. * Author: Eric Sipple & Brennen Bearnes
  12. * Author URI: http://saalonmuyo.com
  13. *
  14. * Copyright 2009 Eric Sipple (email : saalon@gmail.com)
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 2 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  29. */
  30. class Twitter extends \SparkLib\SocialNoise {
  31. public static $tableClass = '';
  32. /**
  33. * Return search results formatted as HTML.
  34. */
  35. public static function searchHTML ($text, $qty)
  36. {
  37. $url = 'https://search.twitter.com/search.json?q=' . rawurlencode($text);
  38. $searchResults = static::getSearchFromJson($url);
  39. if (! $searchResults)
  40. return '<p>Search failure.</p>';
  41. // Set up our string format templates
  42. $img = '<img src="%s" height=48 width=48/>';
  43. $bold = '<b>%s</b>';
  44. $fromUser = '<a href="http://www.twitter.com/%s"><b>%s</b></a> ';
  45. $feed = '';
  46. // Extrude some markup for each result
  47. $foundQty = 0;
  48. foreach ($searchResults->results as $result)
  49. {
  50. $foundQty++;
  51. // Replace any @replies or links with hrefs:
  52. $twitterText = $result->text;
  53. $twitterText = preg_replace("#(^|[\n ])@([^ \'\"\t\n\r<]*)#ise", "'\\1@<a href=\"http://www.twitter.com/\\2\" >\\2</a>'", $twitterText);
  54. $twitterText = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t<]*)#ise", "'\\1<a href=\"\\2\" >\\2</a>'", $twitterText);
  55. $twitterText = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r<]*)#ise", "'\\1<a href=\"http://\\2\" >\\2</a>'", $twitterText);
  56. // Basically undocumented behavior, but for now you can get at profile images
  57. // securely using https://si[n] instead of http://a[n] addresses. See:
  58. // http://code.google.com/p/twitter-api/issues/detail?id=2175
  59. // http://code.google.com/p/twitter-api/issues/detail?id=2006
  60. $image_url = str_replace('http://a', 'https://si', $result->profile_image_url);
  61. // Take the profile image, the from user and the created date,
  62. // throw some HTML blobs around them:
  63. $feed .= '<tr>'
  64. . '<td>' . sprintf($img, $image_url) . '</td>'
  65. . '<td>' . sprintf($fromUser, $result->from_user, $result->from_user)
  66. . $twitterText
  67. . '<br><small><i><a href="http://twitter.com/#!/' . $result->from_user . '/status/' . $result->id_str . '">' . $result->created_at . '</a></i></small>'
  68. . '</td>'
  69. . '</tr>';
  70. if ($foundQty >= $qty) {
  71. break;
  72. }
  73. }
  74. $class = htmlspecialchars(static::$tableClass);
  75. return "<table class=\"{$class}\">$feed</table>";
  76. }
  77. }