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