5 * Description: Posts to a Google+ page with the help of Hootsuite
7 * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
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');
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');
27 function gpluspost_jot_nets(&$a,&$b) {
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>';
40 function gpluspost_settings(&$a,&$s) {
45 /* Add our stylesheet to the page so we can make our settings look nice */
47 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/gpluspost/gpluspost.css' . '" media="all" />' . "\r\n";
49 $enabled = get_pconfig(local_user(),'gpluspost','post');
50 $checked = (($enabled) ? ' checked="checked" ' : '');
52 $def_enabled = get_pconfig(local_user(),'gpluspost','post_by_default');
53 $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
55 $noloop_enabled = get_pconfig(local_user(),'gpluspost','no_loop_prevention');
56 $noloop_checked = (($noloop_enabled) ? ' checked="checked" ' : '');
58 $skip_enabled = get_pconfig(local_user(),'gpluspost','skip_without_link');
59 $skip_checked = (($skip_enabled) ? ' checked="checked" ' : '');
61 $s .= '<span id="settings_gpluspost_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_gpluspost_expanded\'); openClose(\'settings_gpluspost_inflated\');">';
62 $s .= '<h3>' . t('Google+ Export') . '</h3>';
64 $s .= '<div id="settings_gpluspost_expanded" class="settings-block" style="display: none;">';
65 $s .= '<span class="fakelink" onclick="openClose(\'settings_gpluspost_expanded\'); openClose(\'settings_gpluspost_inflated\');">';
66 $s .= '<h3>' . t('Google+ Export') . '</h3>';
69 $s .= '<div id="gpluspost-enable-wrapper">';
70 $s .= '<label id="gpluspost-enable-label" for="gpluspost-checkbox">' . t('Enable Google+ Post Plugin') . '</label>';
71 $s .= '<input id="gpluspost-checkbox" type="checkbox" name="gpluspost" value="1" ' . $checked . '/>';
72 $s .= '</div><div class="clear"></div>';
74 $s .= '<div id="gpluspost-bydefault-wrapper">';
75 $s .= '<label id="gpluspost-bydefault-label" for="gpluspost-bydefault">' . t('Post to Google+ by default') . '</label>';
76 $s .= '<input id="gpluspost-bydefault" type="checkbox" name="gpluspost_bydefault" value="1" ' . $def_checked . '/>';
77 $s .= '</div><div class="clear"></div>';
79 $s .= '<div id="gpluspost-noloopprevention-wrapper">';
80 $s .= '<label id="gpluspost-noloopprevention-label" for="gpluspost-noloopprevention">' . t('Do not prevent posting loops') . '</label>';
81 $s .= '<input id="gpluspost-noloopprevention" type="checkbox" name="gpluspost_noloopprevention" value="1" ' . $noloop_checked . '/>';
82 $s .= '</div><div class="clear"></div>';
84 $s .= '<div id="gpluspost-skipwithoutlink-wrapper">';
85 $s .= '<label id="gpluspost-skipwithoutlink-label" for="gpluspost-skipwithoutlink">' . t('Skip messages without links') . '</label>';
86 $s .= '<input id="gpluspost-skipwithoutlink" type="checkbox" name="gpluspost_skipwithoutlink" value="1" ' . $skip_checked . '/>';
87 $s .= '</div><div class="clear"></div>';
89 /* provide a submit button */
91 $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="gpluspost-submit" name="gpluspost-submit" class="settings-submit" value="' . t('Save Settings') . '" /></div>';
92 $s .= '<p>Register an account at <a href="https://hootsuite.com">Hootsuite</a>, add your G+ page and add the feed-url there.<br />';
93 $s .= 'Feed-url: '.$a->get_baseurl().'/gpluspost/'.urlencode($a->user["nickname"]).'</p></div>';
96 function gpluspost_settings_post(&$a,&$b) {
98 if(x($_POST,'gpluspost-submit')) {
99 set_pconfig(local_user(),'gpluspost','post',intval($_POST['gpluspost']));
100 set_pconfig(local_user(),'gpluspost','post_by_default',intval($_POST['gpluspost_bydefault']));
101 set_pconfig(local_user(),'gpluspost','no_loop_prevention',intval($_POST['gpluspost_noloopprevention']));
102 set_pconfig(local_user(),'gpluspost','skip_without_link',intval($_POST['gpluspost_skipwithoutlink']));
106 function gpluspost_post_local(&$a,&$b) {
111 if((! local_user()) || (local_user() != $b['uid']))
114 if($b['private'] || $b['parent'])
117 $post = intval(get_pconfig(local_user(),'gpluspost','post'));
119 $enable = (($post && x($_REQUEST,'gpluspost_enable')) ? intval($_REQUEST['gpluspost_enable']) : 0);
121 if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'gpluspost','post_by_default')))
127 if(strlen($b['postopts']))
128 $b['postopts'] .= ',';
130 $b['postopts'] .= 'gplus';
133 function gpluspost_send(&$a,&$b) {
135 logger('gpluspost_send: invoked for post '.$b['id']." ".$b['app']);
137 if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
140 if(! strstr($b['postopts'],'gplus'))
143 if($b['parent'] != $b['id'])
146 // if post comes from Google+ don't send it back
147 if (!get_pconfig($b["uid"],'gpluspost','no_loop_prevention') and ($b['app'] == "Google+"))
150 $itemlist = get_pconfig($b["uid"],'gpluspost','itemlist');
151 $items = explode(",", $itemlist);
154 $newitems = array($b['id']);
155 foreach ($items AS $item)
159 $itemlist = implode(",", $newitems);
161 logger('gpluspost_send: new itemlist: '.$itemlist." for uid ".$b["uid"]);
163 set_pconfig($b["uid"],'gpluspost','itemlist', $itemlist);
166 function gpluspost_module() {}
168 function gpluspost_init() {
173 if (isset($a->argv[1])) {
174 $uid = (int)$a->argv[1];
176 $contacts = q("SELECT `username`, `uid` FROM `user` WHERE `nickname` = '%s' LIMIT 1", dbesc($a->argv[1]));
178 $uid = $contacts[0]["uid"];
182 $contacts = q("SELECT `username` FROM `user` WHERE `uid`=%d LIMIT 1", intval($uid));
187 header("content-type: application/atom+xml");
188 echo '<?xml version="1.0" encoding="UTF-8"?>'."\n";
189 echo '<feed xmlns="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">'."\n";
190 echo "\t".'<title type="html"><![CDATA['.$a->config['sitename'].']]></title>'."\n";
192 echo "\t".'<subtitle type="html"><![CDATA['.$contacts[0]["username"]."]]></subtitle>\n";
193 echo "\t".'<link rel="self" href="'.$a->get_baseurl().'/gpluspost/'.$nick.'"/>'."\n";
195 echo "\t".'<link rel="self" href="'.$a->get_baseurl().'/gpluspost"/>'."\n";
196 echo "\t<id>".$a->get_baseurl()."/</id>\n";
197 echo "\t".'<link rel="alternate" type="text/html" href="'.$a->get_baseurl().'"/>'."\n";
198 echo "\t<updated>".date("c")."</updated>\n"; // To-Do
199 // <rights>Copyright ... </rights>
200 echo "\t".'<generator uri="'.$a->get_baseurl().'">'.$a->config['sitename'].'</generator>'."\n";
203 $itemlist = get_pconfig($uid,'gpluspost','itemlist');
204 $items = explode(",", $itemlist);
206 foreach ($items AS $item)
207 gpluspost_feeditem($item, $uid);
209 $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");
210 foreach ($items AS $item)
211 gpluspost_feeditem($item["id"], $uid);
217 function gpluspost_feeditem($pid, $uid) {
220 require_once('include/bbcode.php');
221 require_once("include/html2plain.php");
222 require_once("include/network.php");
224 $skipwithoutlink = get_pconfig($uid,'gpluspost','skip_without_link');
226 $items = q("SELECT `uri`, `plink`, `author-link`, `author-name`, `created`, `edited`, `id`, `title`, `body` from `item` WHERE id=%d", intval($pid));
227 foreach ($items AS $item) {
229 $item['body'] = bb_CleanPictureLinks($item['body']);
231 // Looking for the first image
233 if(preg_match("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/is",$item['body'],$matches))
234 $image = $matches[3];
237 if(preg_match("/\[img\](.*?)\[\/img\]/is",$item['body'],$matches))
238 $image = $matches[1];
240 $multipleimages = (strpos($item['body'], "[img") != strrpos($item['body'], "[img"));
242 // When saved into the database the content is sent through htmlspecialchars
243 // That means that we have to decode all image-urls
244 $image = htmlspecialchars_decode($image);
247 // look for bookmark-bbcode and handle it with priority
248 if(preg_match("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/is",$item['body'],$matches))
251 $multiplelinks = (strpos($item['body'], "[bookmark") != strrpos($item['body'], "[bookmark"));
253 $body = $item['body'];
255 // At first convert the text to html
256 $html = bbcode($body, false, false, 2);
258 // Then convert it to plain text
259 $msg = trim(html2plain($html, 0, true));
260 $msg = html_entity_decode($msg,ENT_QUOTES,'UTF-8');
262 // If there is no bookmark element then take the first link
264 $links = collecturls($html);
265 if (sizeof($links) > 0) {
267 $link = current($links);
269 $multiplelinks = (sizeof($links) > 1);
271 if ($multiplelinks) {
272 $html2 = bbcode($msg, false, false);
273 $links2 = collecturls($html2);
274 if (sizeof($links2) > 0) {
276 $link = current($links2);
277 $multiplelinks = (sizeof($links2) > 1);
284 $msglink = $item["plink"];
285 else if ($link != "")
287 else if ($multipleimages)
288 $msglink = $item["plink"];
289 else if ($image != "")
292 if (($msglink == "") AND $skipwithoutlink)
294 else if ($msglink == "")
295 $msglink = $item["plink"];
297 // Fetching the title - or the first line
298 if ($item["title"] != "")
299 $title = $item["title"];
301 $lines = explode("\n", $msg);
305 //if ($image != $msglink)
306 // $html = trim(str_replace($msglink, "", $html));
308 $title = trim(str_replace($msglink, "", $title));
310 $msglink = original_url($msglink);
313 $title = $item["author-name"].": ".$title;
315 $msglink = htmlspecialchars(html_entity_decode($msglink));
317 $title = str_replace("&", "&", $title);
318 //$html = str_replace("&", "&", $html);
320 echo "\t".'<entry xmlns="http://www.w3.org/2005/Atom">'."\n";
321 echo "\t\t".'<title type="html" xml:space="preserve"><![CDATA['.$title."]]></title>\n";
322 echo "\t\t".'<link rel="alternate" type="text/html" href="'.$msglink.'" />'."\n";
323 // <link rel="enclosure" type="audio/mpeg" length="1337" href="http://example.org/audio/ph34r_my_podcast.mp3"/>
324 echo "\t\t<id>".$item["uri"]."</id>\n";
325 echo "\t\t<updated>".date("c", strtotime($item["edited"]))."</updated>\n";
326 echo "\t\t<published>".date("c", strtotime($item["created"]))."</published>\n";
327 echo "\t\t<author>\n\t\t\t<name><![CDATA[".$item["author-name"]."]]></name>\n";
328 echo "\t\t\t<uri>".$item["author-link"]."</uri>\n\t\t</author>\n";
329 //echo '<content type="image/png" src="http://media.example.org/the_beach.png"/>';
330 echo "\t\t".'<content type="html" xml:space="preserve" xml:base="'.$item["plink"].'"><![CDATA['.$html."]]></content>\n";