GeoURLPlugin.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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)
  58. {
  59. $name = $action->trimmed('action');
  60. $location = null;
  61. if ($name == 'showstream') {
  62. $profile = $action->profile;
  63. if (!empty($profile) && !empty($profile->lat) && !empty($profile->lon)) {
  64. $location = $profile->lat . ', ' . $profile->lon;
  65. }
  66. } else if ($name == 'shownotice') {
  67. $notice = $action->profile;
  68. if (!empty($notice) && !empty($notice->lat) && !empty($notice->lon)) {
  69. $location = $notice->lat . ', ' . $notice->lon;
  70. }
  71. }
  72. if (!empty($location)) {
  73. $action->element('meta', array('name' => 'ICBM',
  74. 'content' => $location));
  75. $action->element('meta', array('name' => 'DC.title',
  76. 'content' => $action->title()));
  77. }
  78. return true;
  79. }
  80. /**
  81. * Report local notices to GeoURL.org when they're created
  82. *
  83. * @param Notice &$notice queued notice
  84. *
  85. * @return boolean event handler flag
  86. */
  87. function onHandleQueuedNotice(&$notice)
  88. {
  89. if ($notice->is_local == 1) {
  90. $request = HTTPClient::start();
  91. $url = common_local_url('shownotice',
  92. array('notice' => $notice->id));
  93. try {
  94. $request->post($this->ping,
  95. null,
  96. array('p' => $url));
  97. } catch (HTTP_Request2_Exception $e) {
  98. common_log(LOG_WARNING,
  99. "GeoURL.org ping failed for '$url' ($this->ping)");
  100. }
  101. }
  102. return true;
  103. }
  104. function onPluginVersion(&$versions)
  105. {
  106. $versions[] = array('name' => 'GeoURL',
  107. 'version' => GNUSOCIAL_VERSION,
  108. 'author' => 'Evan Prodromou',
  109. 'homepage' => 'http://status.net/wiki/Plugin:GeoURL',
  110. 'rawdescription' =>
  111. // TRANS: Plugin description.
  112. _m('Ping <a href="http://geourl.org/">GeoURL</a> when '.
  113. 'new geolocation-enhanced notices are posted.'));
  114. return true;
  115. }
  116. }