GeoURLPlugin.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Plugin to add ICBM metadata to HTML pages and report data to GeoURL.org
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Action
  23. * @package StatusNet
  24. * @author Evan Prodromou <evan@status.net>
  25. * @copyright 2009 StatusNet Inc.
  26. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  27. * @link http://status.net/
  28. */
  29. if (!defined('STATUSNET')) {
  30. exit(1);
  31. }
  32. /**
  33. * Plugin to add ICBM metadata to HTML pages and report data to GeoURL.org
  34. *
  35. * Adds metadata to notice and profile pages that geourl.org and others
  36. * understand. Also, pings geourl.org when a new notice is saved or
  37. * a profile is changed.
  38. *
  39. * @category Plugin
  40. * @package StatusNet
  41. * @author Evan Prodromou <evan@status.net>
  42. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  43. * @link http://status.net/
  44. *
  45. * @seeAlso Location
  46. */
  47. class GeoURLPlugin extends Plugin
  48. {
  49. const PLUGIN_VERSION = '2.0.0';
  50. public $ping = 'http://geourl.org/ping/';
  51. /**
  52. * Add extra <meta> headers for certain pages that geourl.org understands
  53. *
  54. * @param Action $action page being shown
  55. *
  56. * @return boolean event handler flag
  57. */
  58. function onEndShowHeadElements(Action $action)
  59. {
  60. $name = $action->trimmed('action');
  61. $location = null;
  62. if ($action instanceof ShowstreamAction) {
  63. $profile = $action->getTarget();
  64. if (!empty($profile->lat) && !empty($profile->lon)) {
  65. $location = $profile->lat . ', ' . $profile->lon;
  66. }
  67. } elseif ($action instanceof ShownoticeAction) {
  68. // FIXME: getNotice in ShownoticeAction will do a new lookup, we should fix those classes
  69. // so they can reliably just get a pre-stored notice object which was fetched in Shownotice prepare()...
  70. $notice = $action->notice;
  71. if ($notice instanceof Notice && !empty($notice->lat) && !empty($notice->lon)) {
  72. $location = $notice->lat . ', ' . $notice->lon;
  73. }
  74. }
  75. if (!is_null($location)) {
  76. $action->element('meta', array('name' => 'ICBM',
  77. 'content' => $location));
  78. $action->element('meta', array('name' => 'DC.title',
  79. 'content' => $action->title()));
  80. }
  81. return true;
  82. }
  83. /**
  84. * Report local notices to GeoURL.org when they're created
  85. *
  86. * @param Notice &$notice queued notice
  87. *
  88. * @return boolean event handler flag
  89. */
  90. function onHandleQueuedNotice(&$notice)
  91. {
  92. if (intval($notice->is_local) === Notice::LOCAL_PUBLIC) {
  93. $request = HTTPClient::start();
  94. $url = common_local_url('shownotice',
  95. array('notice' => $notice->id));
  96. try {
  97. $request->post($this->ping,
  98. null,
  99. array('p' => $url));
  100. } catch (Exception $e) {
  101. common_log(LOG_WARNING,
  102. "GeoURL.org ping failed for '$url' ($this->ping)");
  103. }
  104. }
  105. return true;
  106. }
  107. function onPluginVersion(array &$versions)
  108. {
  109. $versions[] = array('name' => 'GeoURL',
  110. 'version' => self::PLUGIN_VERSION,
  111. 'author' => 'Evan Prodromou',
  112. 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/GeoURL',
  113. 'rawdescription' =>
  114. // TRANS: Plugin description.
  115. _m('Ping <a href="http://geourl.org/">GeoURL</a> when '.
  116. 'new geolocation-enhanced notices are posted.'));
  117. return true;
  118. }
  119. }