GeoURLPlugin.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. public $ping = 'http://geourl.org/ping/';
  50. /**
  51. * Add extra <meta> headers for certain pages that geourl.org understands
  52. *
  53. * @param Action $action page being shown
  54. *
  55. * @return boolean event handler flag
  56. */
  57. function onEndShowHeadElements(Action $action)
  58. {
  59. $name = $action->trimmed('action');
  60. $location = null;
  61. if ($action instanceof ShowstreamAction) {
  62. $profile = $action->getTarget();
  63. if (!empty($profile->lat) && !empty($profile->lon)) {
  64. $location = $profile->lat . ', ' . $profile->lon;
  65. }
  66. } elseif ($action instanceof ShownoticeAction) {
  67. // FIXME: getNotice in ShownoticeAction will do a new lookup, we should fix those classes
  68. // so they can reliably just get a pre-stored notice object which was fetched in Shownotice prepare()...
  69. $notice = $action->notice;
  70. if ($notice instanceof Notice && !empty($notice->lat) && !empty($notice->lon)) {
  71. $location = $notice->lat . ', ' . $notice->lon;
  72. }
  73. }
  74. if (!is_null($location)) {
  75. $action->element('meta', array('name' => 'ICBM',
  76. 'content' => $location));
  77. $action->element('meta', array('name' => 'DC.title',
  78. 'content' => $action->title()));
  79. }
  80. return true;
  81. }
  82. /**
  83. * Report local notices to GeoURL.org when they're created
  84. *
  85. * @param Notice &$notice queued notice
  86. *
  87. * @return boolean event handler flag
  88. */
  89. function onHandleQueuedNotice(&$notice)
  90. {
  91. if (intval($notice->is_local) === Notice::LOCAL_PUBLIC) {
  92. $request = HTTPClient::start();
  93. $url = common_local_url('shownotice',
  94. array('notice' => $notice->id));
  95. try {
  96. $request->post($this->ping,
  97. null,
  98. array('p' => $url));
  99. } catch (Exception $e) {
  100. common_log(LOG_WARNING,
  101. "GeoURL.org ping failed for '$url' ($this->ping)");
  102. }
  103. }
  104. return true;
  105. }
  106. function onPluginVersion(array &$versions)
  107. {
  108. $versions[] = array('name' => 'GeoURL',
  109. 'version' => GNUSOCIAL_VERSION,
  110. 'author' => 'Evan Prodromou',
  111. 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/GeoURL',
  112. 'rawdescription' =>
  113. // TRANS: Plugin description.
  114. _m('Ping <a href="http://geourl.org/">GeoURL</a> when '.
  115. 'new geolocation-enhanced notices are posted.'));
  116. return true;
  117. }
  118. }