MentionURLPlugin.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. if (!defined('GNUSOCIAL')) { exit(1); }
  3. require_once __DIR__ . '/lib/util.php';
  4. /*
  5. * This plugin lets you type @twitter.com/singpolyma
  6. * so that you can be specific instead of relying on heuristics.
  7. */
  8. class MentionURLPlugin extends Plugin
  9. {
  10. const PLUGIN_VERSION = '2.0.0';
  11. function onEndFindMentions(Profile $sender, $text, &$mentions)
  12. {
  13. $matches = array();
  14. preg_match_all('/(?:^|\s+)@([A-Za-z0-9_:\-\.\/%]+)\b/',
  15. $text,
  16. $atmatches,
  17. PREG_OFFSET_CAPTURE);
  18. foreach ($atmatches[1] as $match) {
  19. $url = $match[0];
  20. if(!common_valid_http_url($url)) { $url = 'http://' . $url; }
  21. if(common_valid_http_url($url)) {
  22. $mentioned = Mention_url_profile::fromUrl($url);
  23. $text = mb_strlen($mentioned->nickname) <= mb_strlen($match[0]) ? $mentioned->nickname : $match[0];
  24. }
  25. if($mentioned instanceof Profile) {
  26. $matches[$match[1]] = array('mentioned' => array($mentioned),
  27. 'type' => 'mention',
  28. 'text' => $text,
  29. 'position' => $match[1],
  30. 'length' => mb_strlen($match[0]),
  31. 'url' => $mentioned->profileurl);
  32. }
  33. }
  34. foreach ($mentions as $i => $other) {
  35. // If we share a common prefix with a local user, override it!
  36. $pos = $other['position'];
  37. if (isset($matches[$pos])) {
  38. $mentions[$i] = $matches[$pos];
  39. unset($matches[$pos]);
  40. }
  41. }
  42. foreach ($matches as $mention) {
  43. $mentions[] = $mention;
  44. }
  45. return true;
  46. }
  47. public function onStartGetProfileFromURI($uri, &$profile)
  48. {
  49. $mention_profile = Mention_url_profile::getKV('profileurl', $uri);
  50. if($mention_profile instanceof Mention_url_profile) {
  51. $profile = $mention_profile->getProfile();
  52. return !($profile instanceof Profile);
  53. }
  54. return true;
  55. }
  56. public function onCheckSchema()
  57. {
  58. $schema = Schema::get();
  59. $schema->ensureTable('mention_url_profile', Mention_url_profile::schemaDef());
  60. return true;
  61. }
  62. public function onPluginVersion(array &$versions)
  63. {
  64. $versions[] = array('name' => 'MentionURL',
  65. 'version' => self::PLUGIN_VERSION,
  66. 'author' => 'Stephen Paul Weber',
  67. 'homepage' => 'http://gnu.io/',
  68. 'description' =>
  69. // TRANS: Plugin description.
  70. _m('Plugin to allow mentioning arbitrary URLs.'));
  71. return true;
  72. }
  73. }