urlshortenerplugin.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 Plugin
  35. * @package StatusNet
  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 $shortenerName;
  43. public $freeService = false;
  44. // Url Shortener plugins should implement some (or all)
  45. // of these methods
  46. /**
  47. * Make an URL shorter.
  48. *
  49. * @param string $url URL to shorten
  50. *
  51. * @return string shortened version of the url, or null on failure
  52. */
  53. protected abstract function shorten($url);
  54. /**
  55. * Utility to get the data at an URL
  56. *
  57. * @param string $url URL to fetch
  58. *
  59. * @return string response body
  60. *
  61. * @todo rename to code-standard camelCase httpGet()
  62. */
  63. protected function http_get($url)
  64. {
  65. $request = HTTPClient::start();
  66. $response = $request->get($url);
  67. return $response->getBody();
  68. }
  69. /**
  70. * Utility to post a request and get a response URL
  71. *
  72. * @param string $url URL to fetch
  73. * @param array $data post parameters
  74. *
  75. * @return string response body
  76. *
  77. * @todo rename to code-standard httpPost()
  78. */
  79. protected function http_post($url, $data)
  80. {
  81. $request = HTTPClient::start();
  82. $response = $request->post($url, null, $data);
  83. return $response->getBody();
  84. }
  85. // Hook handlers
  86. /**
  87. * Called when all plugins have been initialized
  88. *
  89. * @return boolean hook value
  90. */
  91. function onInitializePlugin()
  92. {
  93. if (!isset($this->shortenerName)) {
  94. throw new Exception("must specify a shortenerName");
  95. }
  96. return true;
  97. }
  98. /**
  99. * Called when a showing the URL shortener drop-down box
  100. *
  101. * Properties of the shortening service currently only
  102. * include whether it's a free service.
  103. *
  104. * @param array &$shorteners array mapping shortener name to properties
  105. *
  106. * @return boolean hook value
  107. */
  108. function onGetUrlShorteners(&$shorteners)
  109. {
  110. $shorteners[$this->shortenerName] =
  111. array('freeService' => $this->freeService);
  112. return true;
  113. }
  114. /**
  115. * Called to shorten an URL
  116. *
  117. * @param string $url URL to shorten
  118. * @param string $shortenerName Shortening service. Don't handle if it's
  119. * not you!
  120. * @param string &$shortenedUrl URL after shortening; out param.
  121. *
  122. * @return boolean hook value
  123. */
  124. function onStartShortenUrl($url, $shortenerName, &$shortenedUrl)
  125. {
  126. if ($shortenerName == $this->shortenerName) {
  127. $result = $this->shorten($url);
  128. if (isset($result) && $result != null && $result !== false) {
  129. $shortenedUrl = $result;
  130. common_log(LOG_INFO,
  131. __CLASS__ . ": $this->shortenerName ".
  132. "shortened $url to $shortenedUrl");
  133. return false;
  134. }
  135. }
  136. return true;
  137. }
  138. }