5 * Description: Posts to a Google+ page with the help of Seesmic
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 $enabled = get_pconfig(local_user(),'gpluspost','post');
47 $checked = (($enabled) ? ' checked="checked" ' : '');
49 $def_enabled = get_pconfig(local_user(),'gpluspost','post_by_default');
51 $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
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>';
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>';
65 /* provide a submit button */
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>';
73 function gpluspost_settings_post(&$a,&$b) {
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']));
81 function gpluspost_post_local(&$a,&$b) {
86 if((! local_user()) || (local_user() != $b['uid']))
89 if($b['private'] || $b['parent'])
92 $post = intval(get_pconfig(local_user(),'gpluspost','post'));
94 $enable = (($post && x($_REQUEST,'gpluspost_enable')) ? intval($_REQUEST['gpluspost_enable']) : 0);
96 if(intval(get_pconfig(local_user(),'gpluspost','post_by_default')))
102 if(strlen($b['postopts']))
103 $b['postopts'] .= ',';
105 $b['postopts'] .= 'gplus';
108 function gpluspost_send(&$a,&$b) {
110 logger('gpluspost_send: invoked for post '.$b['id']." ".$b['app']);
112 if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
115 if(! strstr($b['postopts'],'gplus'))
118 if($b['parent'] != $b['id'])
121 // if post comes from Google+ don't send it back
122 //if($b['app'] == "Google+")
125 $itemlist = get_pconfig($b["uid"],'gpluspost','itemlist');
126 $items = explode(",", $itemlist);
129 $newitems = array($b['id']);
130 foreach ($items AS $item)
134 $itemlist = implode(",", $newitems);
136 logger('gpluspost_send: new itemlist: '.$itemlist." for uid ".$b["uid"]);
138 set_pconfig($b["uid"],'gpluspost','itemlist', $itemlist);
141 function gpluspost_module() {}
143 function gpluspost_init() {
148 if (isset($a->argv[1])) {
149 $uid = (int)$a->argv[1];
151 $contacts = q("SELECT `name`, `id` FROM contact WHERE `nick` = '%s' LIMIT 1", dbesc($a->argv[1]));
153 $uid = $contacts[0]["id"];
157 $contacts = q("SELECT `name` FROM contact WHERE ID=%d LIMIT 1", intval($uid));
162 header("content-type: application/atom+xml");
163 echo '<?xml version="1.0" encoding="UTF-8"?>'."\n";
164 echo '<feed xmlns="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">'."\n";
165 echo "\t".'<title type="html"><![CDATA['.$a->config['sitename'].']]></title>'."\n";
167 echo "\t".'<subtitle type="html"><![CDATA['.$contacts[0]["name"]."]]></subtitle>\n";
168 echo "\t".'<link rel="self" href="'.$a->get_baseurl().'/gpluspost/'.$nick.'"/>'."\n";
170 echo "\t".'<link rel="self" href="'.$a->get_baseurl().'/gpluspost"/>'."\n";
171 echo "\t<id>".$a->get_baseurl()."/</id>\n";
172 echo "\t".'<link rel="alternate" type="text/html" href="'.$a->get_baseurl().'"/>'."\n";
173 echo "\t<updated>".date("c")."</updated>\n"; // To-Do
174 // <rights>Copyright ... </rights>
175 echo "\t".'<generator uri="'.$a->get_baseurl().'">'.$a->config['sitename'].'</generator>'."\n";
178 $itemlist = get_pconfig($uid,'gpluspost','itemlist');
179 $items = explode(",", $itemlist);
181 foreach ($items AS $item)
182 gpluspost_feeditem($item, $uid);
184 $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");
185 foreach ($items AS $item)
186 gpluspost_feeditem($item["id"], $uid);
192 function gpluspost_ShareAttributes($match) {
194 $attributes = $match[1];
197 preg_match("/author='(.*?)'/ism", $attributes, $matches);
198 if ($matches[1] != "")
199 $author = $matches[1];
201 preg_match('/author="(.*?)"/ism', $attributes, $matches);
202 if ($matches[1] != "")
203 $author = $matches[1];
205 $headline = '<div class="shared_header">';
207 $headline .= sprintf(t('%s:'), $author);
209 $headline .= "</div>";
211 //$text = "<br />".$headline."</strong><blockquote>".$match[2]."</blockquote>";
212 //$text = "\n\t".$match[2].":\t";
213 $text = $author.": ".$match[2];
218 function gpluspost_feeditem($pid, $uid) {
221 require_once('include/bbcode.php');
222 require_once("include/html2plain.php");
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 // Looking for the first image
231 if(preg_match("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/is",$item['body'],$matches))
232 $image = $matches[3];
235 if(preg_match("/\[img\](.*?)\[\/img\]/is",$item['body'],$matches))
236 $image = $matches[1];
238 $multipleimages = (strpos($item['body'], "[img") != strrpos($item['body'], "[img"));
240 // When saved into the database the content is sent through htmlspecialchars
241 // That means that we have to decode all image-urls
242 $image = htmlspecialchars_decode($image);
245 // look for bookmark-bbcode and handle it with priority
246 if(preg_match("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/is",$item['body'],$matches))
249 $multiplelinks = (strpos($item['body'], "[bookmark") != strrpos($item['body'], "[bookmark"));
251 $body = $item['body'];
252 $body = preg_replace_callback("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]/ism","gpluspost_ShareAttributes", $body);
254 $html = bbcode($body, false, false);
255 $msg = trim(html2plain($html, 0, true));
257 // If there is no bookmark element then take the first link
259 $links = collecturls($html);
260 if (sizeof($links) > 0) {
262 $link = current($links);
264 $multiplelinks = (sizeof($links) > 1);
269 $msglink = $item["plink"];
270 else if ($link != "")
272 else if ($multipleimages)
273 $msglink = $item["plink"];
274 else if ($image != "")
278 $msglink = $item["plink"];
280 if ($image != $msglink)
281 $html = trim(str_replace($msglink, "", $html));
283 // Fetching the title - or the first line
284 if ($item["title"] != "")
285 $title = $item["title"];
287 $lines = explode("\n", $msg);
292 $title = $item["author-name"].": ".$title;
294 $title = str_replace("&", "&", $title);
295 //$html = str_replace("&", "&", $html);
297 echo "\t".'<entry xmlns="http://www.w3.org/2005/Atom">'."\n";
298 echo "\t\t".'<title type="html" xml:space="preserve"><![CDATA['.$title."]]></title>\n";
299 echo "\t\t".'<link rel="alternate" type="text/html" href="'.$msglink.'" />'."\n";
300 // <link rel="enclosure" type="audio/mpeg" length="1337" href="http://example.org/audio/ph34r_my_podcast.mp3"/>
301 echo "\t\t<id>".$item["uri"]."</id>\n";
302 echo "\t\t<updated>".date("c", strtotime($item["edited"]))."</updated>\n";
303 echo "\t\t<published>".date("c", strtotime($item["created"]))."</published>\n";
304 echo "\t\t<author>\n\t\t\t<name><![CDATA[".$item["author-name"]."]]></name>\n";
305 echo "\t\t\t<uri>".$item["author-link"]."</uri>\n\t\t</author>\n";
306 //echo '<content type="image/png" src="http://media.example.org/the_beach.png"/>';
307 echo "\t\t".'<content type="html" xml:space="preserve" xml:base="'.$item["plink"].'"><![CDATA['.$html."]]></content>\n";