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