]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/ShareNotice/ShareNoticePlugin.php
d44e234529fe4bd64cac235623840341748fa861
[quix0rs-gnu-social.git] / plugins / ShareNotice / ShareNoticePlugin.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 /**
21  * @package ShareNoticePlugin
22  * @maintainer Brion Vibber <brion@status.net>
23  */
24
25 if (!defined('STATUSNET')) { exit(1); }
26
27 class ShareNoticePlugin extends Plugin
28 {
29     public $targets = array(
30         array('Twitter'),
31         array('Facebook'),
32         array('StatusNet', array('baseurl' => 'http://identi.ca'))
33     );
34
35     function onEndShowStatusNetStyles($action) {
36         $action->cssLink('plugins/ShareNotice/css/sharenotice.css');
37         return true;
38     }
39
40     function onStartShowNoticeItem($item)
41     {
42         $notice = $item->notice;
43         $out = $item->out;
44
45         $out->elementStart('ul', array('class' => 'notice-share'));
46         foreach ($this->targets as $data) {
47             $type = $data[0];
48             $args = (count($data) > 1) ? $data[1] : array();
49             $target = $this->getShareTarget($type, $notice, $args);
50             $this->showShareTarget($out, $target);
51         }
52         $out->elementEnd('ul');
53     }
54
55     private function getShareTarget($type, $notice, $args)
56     {
57         $class = ucfirst($type) . 'ShareTarget';
58
59         return new $class($notice, $args);
60     }
61
62     private function showShareTarget(HTMLOutputter $out, NoticeShareTarget $target)
63     {
64         $class = $target->getClass();
65         $text = $target->getText();
66         $url = $target->targetUrl();
67
68         $out->elementStart('li', array('class' => 'notice-share-' . $class));
69         $out->elementStart('a', array(
70             'href' => $url,
71             'title' => $text,
72             'target' => '_blank'
73         ));
74         $out->element('span', array(), $text);
75         $out->elementEnd('a');
76         $out->elementEnd('li');
77     }
78 }
79
80 abstract class NoticeShareTarget
81 {
82     protected $notice;
83
84     public function __construct($notice)
85     {
86         $this->notice = $notice;
87     }
88
89     public abstract function getClass();
90
91     public abstract function getText();
92
93     public abstract function targetUrl();
94 }
95
96 abstract class GenericNoticeShareTarget extends NoticeShareTarget
97 {
98     protected function maxLength()
99     {
100         return 140; // typical
101     }
102
103     protected function statusText()
104     {
105         $pattern = _m('"%s"');
106         $url = $this->notice->bestUrl();
107         $suffix = ' ' . $url;
108         $room = $this->maxLength() - mb_strlen($suffix) - (mb_strlen($pattern) - mb_strlen('%s'));
109
110         $content = $this->notice->content;
111         if (mb_strlen($content) > $room) {
112             $content = mb_substr($content, 0, $room - 1) . '…';
113         }
114
115         return sprintf($pattern, $content) . $suffix;
116     }
117 }
118
119 class TwitterShareTarget extends GenericNoticeShareTarget
120 {
121     public function getClass()
122     {
123         return 'twitter';
124     }
125
126     public function getText()
127     {
128         return _m('Share on Twitter');
129     }
130
131     public function targetUrl()
132     {
133         $args = array(
134             'status' => $this->statusText()
135         );
136         return 'http://twitter.com/home?' .
137                 http_build_query($args, null, '&');
138     }
139 }
140
141 class StatusNetShareTarget extends GenericNoticeShareTarget
142 {
143     protected $baseurl;
144
145     public function __construct($notice, $args)
146     {
147         parent::__construct($notice);
148         $this->baseurl = $args['baseurl'];
149     }
150
151     public function getClass()
152     {
153         return 'statusnet';
154     }
155
156     public function getText()
157     {
158         $host = parse_url($this->baseurl, PHP_URL_HOST);
159         return sprintf(_m('Share on %s'), $host);
160     }
161
162     public function targetUrl()
163     {
164         $args = array(
165             'status_textarea' => $this->statusText()
166         );
167         return $this->baseurl . '/notice/new?' .
168                 http_build_query($args, null, '&');
169     }
170
171 }
172
173 class FacebookShareTarget extends NoticeShareTarget
174 {
175     public function getClass()
176     {
177         return 'facebook';
178     }
179
180     public function getText()
181     {
182         return _m('Share on Facebook');
183     }
184
185     public function targetUrl()
186     {
187         $args = array(
188             'u' => $this->notice->bestUrl(),
189             't' => sprintf(_m('"%s"'), $this->notice->content),
190         );
191         return 'http://www.facebook.com/sharer.php?' .
192             http_build_query($args, null, '&');
193     }
194 }