MentionURLPlugin.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. function onEndFindMentions(Profile $sender, $text, &$mentions)
  11. {
  12. $matches = array();
  13. preg_match_all('/(?:^|\s+)@([A-Za-z0-9_:\-\.\/%]+)\b/',
  14. $text,
  15. $atmatches,
  16. PREG_OFFSET_CAPTURE);
  17. foreach ($atmatches[1] as $match) {
  18. $url = $match[0];
  19. if(!common_valid_http_url($url)) { $url = 'http://' . $url; }
  20. if(common_valid_http_url($url)) {
  21. $mentioned = Mention_url_profile::fromUrl($url);
  22. $text = mb_strlen($mentioned->nickname) <= mb_strlen($match[0]) ? $mentioned->nickname : $match[0];
  23. }
  24. if($mentioned instanceof Profile) {
  25. $matches[$match[1]] = array('mentioned' => array($mentioned),
  26. 'type' => 'mention',
  27. 'text' => $text,
  28. 'position' => $match[1],
  29. 'length' => mb_strlen($match[0]),
  30. 'url' => $mentioned->profileurl);
  31. }
  32. }
  33. foreach ($mentions as $i => $other) {
  34. // If we share a common prefix with a local user, override it!
  35. $pos = $other['position'];
  36. if (isset($matches[$pos])) {
  37. $mentions[$i] = $matches[$pos];
  38. unset($matches[$pos]);
  39. }
  40. }
  41. foreach ($matches as $mention) {
  42. $mentions[] = $mention;
  43. }
  44. return true;
  45. }
  46. public function onStartGetProfileFromURI($uri, &$profile)
  47. {
  48. $mention_profile = Mention_url_profile::getKV('profileurl', $uri);
  49. if($mention_profile instanceof Mention_url_profile) {
  50. $profile = $mention_profile->getProfile();
  51. return !($profile instanceof Profile);
  52. }
  53. return true;
  54. }
  55. public function onCheckSchema()
  56. {
  57. $schema = Schema::get();
  58. $schema->ensureTable('mention_url_profile', Mention_url_profile::schemaDef());
  59. return true;
  60. }
  61. public function onPluginVersion(array &$versions)
  62. {
  63. $versions[] = array('name' => 'MentionURL',
  64. 'version' => GNUSOCIAL_VERSION,
  65. 'author' => 'Stephen Paul Weber',
  66. 'homepage' => 'http://gnu.io/',
  67. 'description' =>
  68. // TRANS: Plugin description.
  69. _m('Plugin to allow mentioning arbitrary URLs.'));
  70. return true;
  71. }
  72. }