]> git.mxchange.org Git - friendica-addons.git/blob - gpluspost/gpluspost.php
gpluspost: Posting to G+ is now working
[friendica-addons.git] / gpluspost / gpluspost.php
1 <?php
2
3 /**
4  * Name: G+ Post
5  * Description: Posts to a Google+ page with the help of Seesmic
6  * Version: 0.1
7  * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
8  */
9
10 function gpluspost_install() {
11         register_hook('post_local',           'addon/gpluspost/gpluspost.php', 'gpluspost_post_local');
12         register_hook('notifier_normal',      'addon/gpluspost/gpluspost.php', 'gpluspost_send');
13         register_hook('jot_networks',         'addon/gpluspost/gpluspost.php', 'gpluspost_jot_nets');
14         register_hook('connector_settings',      'addon/gpluspost/gpluspost.php', 'gpluspost_settings');
15         register_hook('connector_settings_post', 'addon/gpluspost/gpluspost.php', 'gpluspost_settings_post');
16 }
17
18
19 function gpluspost_uninstall() {
20         unregister_hook('post_local',       'addon/gpluspost/gpluspost.php', 'gpluspost_post_local');
21         unregister_hook('notifier_normal',  'addon/gpluspost/gpluspost.php', 'gpluspost_send');
22         unregister_hook('jot_networks',     'addon/gpluspost/gpluspost.php', 'gpluspost_jot_nets');
23         unregister_hook('connector_settings',      'addon/gpluspost/gpluspost.php', 'gpluspost_settings');
24         unregister_hook('connector_settings_post', 'addon/gpluspost/gpluspost.php', 'gpluspost_settings_post');
25 }
26
27 function gpluspost_jot_nets(&$a,&$b) {
28         if(! local_user())
29                 return;
30
31         $post = get_pconfig(local_user(),'gpluspost','post');
32         if(intval($post) == 1) {
33                 $defpost = get_pconfig(local_user(),'gpluspost','post_by_default');
34                 $selected = ((intval($defpost) == 1) ? ' checked="checked" ' : '');
35                 $b .= '<div class="profile-jot-net"><input type="checkbox" name="gpluspost_enable"' . $selected . ' value="1" /> '
36                         . t('Post to Google+') . '</div>';
37     }
38 }
39
40 function gpluspost_settings(&$a,&$s) {
41
42         if(! local_user())
43                 return;
44
45         $enabled = get_pconfig(local_user(),'gpluspost','post');
46
47         $checked = (($enabled) ? ' checked="checked" ' : '');
48
49         $def_enabled = get_pconfig(local_user(),'gpluspost','post_by_default');
50
51         $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
52
53         $s .= '<div class="settings-block">';
54         $s .= '<h3>' . t('Google+ Post Settings') . '</h3>';
55         $s .= '<div id="gpluspost-enable-wrapper">';
56         $s .= '<label id="gpluspost-enable-label" for="gpluspost-checkbox">' . t('Enable Google+ Post Plugin') . '</label>';
57         $s .= '<input id="gpluspost-checkbox" type="checkbox" name="gpluspost" value="1" ' . $checked . '/>';
58         $s .= '</div><div class="clear"></div>';
59
60         $s .= '<div id="gpluspost-bydefault-wrapper">';
61         $s .= '<label id="gpluspost-bydefault-label" for="gpluspost-bydefault">' . t('Post to Google+ by default') . '</label>';
62         $s .= '<input id="gpluspost-bydefault" type="checkbox" name="gpluspost_bydefault" value="1" ' . $def_checked . '/>';
63         $s .= '</div><div class="clear"></div>';
64
65         /* provide a submit button */
66
67         $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="gpluspost-submit" name="gpluspost-submit" class="settings-submit" value="' . t('Submit') . '" /></div>';
68         $s .= 'Register an account at <a href="https://hootsuite.com">Hootsuite</a>, add your G+ page and add the feed-url there.<br />';
69         //$s .= 'Feed-url: '.$a->get_baseurl().'/gpluspost/'.$a->user["uid"].'</div>';
70         $s .= 'Feed-url: '.$a->get_baseurl().'/gpluspost/'.urlencode($a->user["nickname"]).'</div>';
71 }
72
73 function gpluspost_settings_post(&$a,&$b) {
74
75         if(x($_POST,'gpluspost-submit')) {
76                 set_pconfig(local_user(),'gpluspost','post',intval($_POST['gpluspost']));
77                 set_pconfig(local_user(),'gpluspost','post_by_default',intval($_POST['gpluspost_bydefault']));
78         }
79 }
80
81 function gpluspost_post_local(&$a,&$b) {
82
83         if($b['edit'])
84                 return;
85
86         if((! local_user()) || (local_user() != $b['uid']))
87                 return;
88
89         if($b['private'] || $b['parent'])
90                 return;
91
92         $post   = intval(get_pconfig(local_user(),'gpluspost','post'));
93
94         $enable = (($post && x($_REQUEST,'gpluspost_enable')) ? intval($_REQUEST['gpluspost_enable']) : 0);
95
96         if(intval(get_pconfig(local_user(),'gpluspost','post_by_default')))
97                 $enable = 1;
98
99         if(!$enable)
100                 return;
101
102         if(strlen($b['postopts']))
103                 $b['postopts'] .= ',';
104
105         $b['postopts'] .= 'gplus';
106 }
107
108 function gpluspost_send(&$a,&$b) {
109
110         logger('gpluspost_send: invoked for post '.$b['id']);
111
112         if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
113                 return;
114
115         if(! strstr($b['postopts'],'gplus'))
116                 return;
117
118         if($b['parent'] != $b['id'])
119                 return;
120
121         // if post comes from Google+ don't send it back
122         if($b['app'] == "Google+")
123                 return;
124
125         $itemlist = get_pconfig($b["uid"],'gpluspost','itemlist');
126         $items = explode(",", $itemlist);
127
128         $i = 0;
129         $newitems = array($b['id']);
130         foreach ($items AS $item)
131                 if ($i++ < 9)
132                         $newitems[] = $item;
133
134         $itemlist = implode(",", $newitems);
135
136         logger('gpluspost_send: new itemlist: '.$itemlist." for uid ".$b["uid"]);
137
138         set_pconfig($b["uid"],'gpluspost','itemlist', $itemlist);
139 }
140
141 function gpluspost_module() {}
142
143 function gpluspost_init() {
144         global $a, $_SERVER;
145
146         $uid = 0;
147
148         if (isset($a->argv[1])) {
149                 $uid = (int)$a->argv[1];
150                 if ($uid == 0) {
151                         $contacts = q("SELECT `name`, `id` FROM contact WHERE `nick` = '%s' LIMIT 1", dbesc($a->argv[1]));
152                         if ($contacts)
153                                 $uid = $contacts[0]["id"];
154                 } else
155                         $contacts = q("SELECT `name` FROM contact WHERE ID=%d LIMIT 1", intval($uid));
156         }
157
158         header("content-type: application/atom+xml");
159         echo '<?xml version="1.0" encoding="UTF-8"?>'."\n";
160         echo '<feed xmlns="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">'."\n";
161         echo "\t".'<title type="html"><![CDATA['.$a->config['sitename'].']]></title>'."\n";
162         if ($uid != 0)
163                 echo "\t".'<subtitle type="html"><![CDATA['.$contacts[0]["name"]."]]></subtitle>\n";
164         echo "\t".'<link rel="self" href="'.$a->get_baseurl().'/gpluspost"/>'."\n";
165         echo "\t<id>".$a->get_baseurl()."/</id>\n";
166         echo "\t".'<link rel="alternate" type="text/html" href="'.$a->get_baseurl().'"/>'."\n";
167         echo "\t<updated>".date("c")."</updated>\n"; // To-Do
168         // <rights>Copyright ... </rights>
169         echo "\t".'<generator uri="'.$a->get_baseurl().'">'.$a->config['sitename'].'</generator>'."\n";
170
171         if ($uid != 0) {
172                 $itemlist = get_pconfig(local_user(),'gpluspost','itemlist');
173                 //$itemlist = "262568,262567,269154,271508,270121,273721,314735,312616,311570,308771,308247,306100,295372,291096,290390,290389,283242,283060,280465,273725";
174                 $items = explode(",", $itemlist);
175
176                 foreach ($items AS $item)
177                         gpluspost_feeditem($item, $uid);
178         } else {
179                 $items = q("SELECT `id` FROM `item` FORCE INDEX (`received`) WHERE `item`.`visible` = 1 AND `item`.`deleted` = 0 and `item`.`moderated` = 0 AND `item`.`allow_cid` = ''  AND `item`.`allow_gid` = '' AND `item`.`deny_cid`  = '' AND `item`.`deny_gid`  = '' AND `item`.`private` = 0 AND `item`.`wall` = 1 AND `item`.`id` = `item`.`parent` ORDER BY `received` DESC LIMIT 10");
180                 foreach ($items AS $item)
181                         gpluspost_feeditem($item["id"], $uid);
182         }
183         echo "</feed>\n";
184         killme();
185 }
186
187 function gpluspost_ShareAttributes($match) {
188
189         $attributes = $match[1];
190
191         $author = "";
192         preg_match("/author='(.*?)'/ism", $attributes, $matches);
193         if ($matches[1] != "")
194                 $author = $matches[1];
195
196         preg_match('/author="(.*?)"/ism', $attributes, $matches);
197         if ($matches[1] != "")
198                 $author = $matches[1];
199
200         $headline = '<div class="shared_header">';
201
202         $headline .= sprintf(t('%s:'), $author);
203
204         $headline .= "</div>";
205
206         //$text = "<br />".$headline."</strong><blockquote>".$match[2]."</blockquote>";
207         //$text = "\n\t".$match[2].":\t";
208         $text = $author.": ".$match[2];
209
210         return($text);
211 }
212
213 function gpluspost_feeditem($pid, $uid) {
214         global $a;
215
216         require_once('include/bbcode.php');
217         require_once("include/html2plain.php");
218
219         $max_char = 140;
220
221         $items = q("SELECT `uri`, `plink`, `author-link`, `author-name`, `created`, `edited`, `id`, `title`, `body` from `item` WHERE id=%d", intval($pid));
222         foreach ($items AS $item) {
223
224                 // Looking for the first image
225                 $image = '';
226                 if(preg_match("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/is",$item['body'],$matches))
227                         $image = $matches[3];
228
229                 if ($image == '')
230                         if(preg_match("/\[img\](.*?)\[\/img\]/is",$item['body'],$matches))
231                                 $image = $matches[1];
232
233                 $multipleimages = (strpos($item['body'], "[img") != strrpos($item['body'], "[img"));
234
235                 // When saved into the database the content is sent through htmlspecialchars
236                 // That means that we have to decode all image-urls
237                 $image = htmlspecialchars_decode($image);
238
239                 $link = '';
240                 // look for bookmark-bbcode and handle it with priority
241                 if(preg_match("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/is",$item['body'],$matches))
242                         $link = $matches[1];
243
244                 $multiplelinks = (strpos($item['body'], "[bookmark") != strrpos($item['body'], "[bookmark"));
245
246                 $body = $item['body'];
247                 $body = preg_replace_callback("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]/ism","gpluspost_ShareAttributes", $body);
248
249                 $html = bbcode($body, false, false);
250                 $msg = trim(html2plain($html, 0, true));
251
252                 // If there is no bookmark element then take the first link
253                 if ($link == '') {
254                         $links = collecturls($html);
255                         if (sizeof($links) > 0) {
256                                 reset($links);
257                                 $link = current($links);
258                         }
259                         $multiplelinks = (sizeof($links) > 1);
260                 }
261
262                 $msglink = "";
263                 if ($multiplelinks)
264                         $msglink = $item["plink"];
265                 else if ($link != "")
266                         $msglink = $link;
267                 else if ($multipleimages)
268                         $msglink = $item["plink"];
269                 else if ($image != "")
270                         $msglink = $image;
271
272                 if ($msglink == "")
273                         $msglink = $item["plink"];
274
275                 if ($image != $msglink)
276                         $html = trim(str_replace($msglink, "", $html));
277
278                 // Fetching the title - or the first line
279                 if ($item["title"] != "")
280                         $title = $item["title"];
281                 else {
282                         $lines = explode("\n", $msg);
283                         $title = $lines[0];
284                 }
285
286                 if ($uid == 0)
287                         $title = $item["author-name"].": ".$title;
288
289                 $title = str_replace("&", "&amp;", $title);
290                 //$html = str_replace("&", "&amp;", $html);
291
292                 echo "\t".'<entry xmlns="http://www.w3.org/2005/Atom">'."\n";
293                 echo "\t\t".'<title type="html" xml:space="preserve"><![CDATA['.$title."]]></title>\n";
294                 echo "\t\t".'<link rel="alternate" type="text/html" href="'.$msglink.'" />'."\n";
295                 // <link rel="enclosure" type="audio/mpeg" length="1337" href="http://example.org/audio/ph34r_my_podcast.mp3"/>
296                 echo "\t\t<id>".$item["uri"]."</id>\n";
297                 echo "\t\t<updated>".date("c", strtotime($item["edited"]))."</updated>\n";
298                 echo "\t\t<published>".date("c", strtotime($item["created"]))."</published>\n";
299                 echo "\t\t<author>\n\t\t\t<name><![CDATA[".$item["author-name"]."]]></name>\n";
300                 echo "\t\t\t<uri>".$item["author-link"]."</uri>\n\t\t</author>\n";
301                 //echo '<content type="image/png" src="http://media.example.org/the_beach.png"/>';
302                 echo "\t\t".'<content type="html" xml:space="preserve" xml:base="'.$item["plink"].'"><![CDATA['.$html."]]></content>\n";
303                 echo "\t</entry>\n";
304         }
305 }