UrlShortenerPlugin.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Superclass for plugins that do URL shortening
  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 Plugin
  23. * @package StatusNet
  24. * @author Craig Andrews <candrews@integralblue.com>
  25. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  26. * @link http://status.net/
  27. */
  28. if (!defined('STATUSNET') && !defined('LACONICA')) {
  29. exit(1);
  30. }
  31. /**
  32. * Superclass for plugins that do URL shortening
  33. *
  34. * @category Module
  35. * @package GNUsocial
  36. * @author Craig Andrews <candrews@integralblue.com>
  37. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  38. * @link http://status.net/
  39. */
  40. abstract class UrlShortenerPlugin extends Plugin
  41. {
  42. public $widgetOpts;
  43. public $scoped;
  44. public $shortenerName;
  45. public $freeService = false;
  46. // Url Shortener plugins should implement some (or all)
  47. // of these methods
  48. /**
  49. * Make an URL shorter.
  50. *
  51. * @param string $url URL to shorten
  52. *
  53. * @return string shortened version of the url, or null on failure
  54. */
  55. protected abstract function shorten($url);
  56. /**
  57. * Utility to get the data at an URL
  58. *
  59. * @param string $url URL to fetch
  60. *
  61. * @return string response body
  62. *
  63. * @todo rename to code-standard camelCase httpGet()
  64. */
  65. protected function http_get($url)
  66. {
  67. $request = HTTPClient::start();
  68. $response = $request->get($url);
  69. return $response->getBody();
  70. }
  71. /**
  72. * Utility to post a request and get a response URL
  73. *
  74. * @param string $url URL to fetch
  75. * @param array $data post parameters
  76. *
  77. * @return string response body
  78. *
  79. * @todo rename to code-standard httpPost()
  80. */
  81. protected function http_post($url, $data)
  82. {
  83. $request = HTTPClient::start();
  84. $response = $request->post($url, null, $data);
  85. return $response->getBody();
  86. }
  87. // Hook handlers
  88. /**
  89. * Called when all plugins have been initialized
  90. *
  91. * @return boolean hook value
  92. */
  93. function onInitializePlugin()
  94. {
  95. if (!isset($this->shortenerName)) {
  96. throw new Exception("must specify a shortenerName");
  97. }
  98. return true;
  99. }
  100. /**
  101. * Called when a showing the URL shortener drop-down box
  102. *
  103. * Properties of the shortening service currently only
  104. * include whether it's a free service.
  105. *
  106. * @param array &$shorteners array mapping shortener name to properties
  107. *
  108. * @return boolean hook value
  109. */
  110. function onGetUrlShorteners(&$shorteners)
  111. {
  112. $shorteners[$this->shortenerName] =
  113. array('freeService' => $this->freeService);
  114. return true;
  115. }
  116. /**
  117. * Called to shorten an URL
  118. *
  119. * @param string $url URL to shorten
  120. * @param string $shortenerName Shortening service. Don't handle if it's
  121. * not you!
  122. * @param string &$shortenedUrl URL after shortening; out param.
  123. *
  124. * @return boolean hook value
  125. */
  126. function onStartShortenUrl($url, $shortenerName, &$shortenedUrl)
  127. {
  128. if ($shortenerName == $this->shortenerName) {
  129. $result = $this->shorten($url);
  130. if (isset($result) && $result != null && $result !== false) {
  131. $shortenedUrl = $result;
  132. common_log(LOG_INFO,
  133. __CLASS__ . ": $this->shortenerName ".
  134. "shortened $url to $shortenedUrl");
  135. return false;
  136. }
  137. }
  138. return true;
  139. }
  140. }