4 * Description: Imports posts from a Google+ account and repeats them
6 * Author: Michael Vogel <ike@piratenpartei.de>
10 function fromgplus_install() {
11 register_hook('plugin_settings', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings');
12 register_hook('plugin_settings_post', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings_post');
15 function fromgplus_uninstall() {
16 unregister_hook('plugin_settings', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings');
17 unregister_hook('plugin_settings_post', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings_post');
20 function fromgplus_addon_settings(&$a,&$s) {
25 $enable_checked = (intval(get_pconfig(local_user(),'fromgplus','enable')) ? ' checked="checked"' : '');
26 $account = get_pconfig(local_user(),'fromgplus','account');
28 $s .= '<div class="settings-block">';
29 $s .= '<h3>' . t('Google+ Import Settings').'</h3>';
30 $s .= '<div id="fromgplus-wrapper">';
32 $s .= '<label id="fromgplus-enable-label" for="fromgplus-enable">'.t('Enable Google+ Import').'</label>';
33 $s .= '<input id="fromgplus-enable" type="checkbox" name="fromgplus-enable" value="1"'.$enable_checked.' />';
34 $s .= '<div class="clear"></div>';
35 $s .= '<label id="fromgplus-label" for="fromgplus-account">'.t('Google Account ID').' </label>';
36 $s .= '<input id="fromgplus-account" type="text" name="fromgplus-account" value="'.$account.'" />';
37 $s .= '</div><div class="clear"></div>';
39 $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="fromgplus-submit" name="fromgplus-submit" class="settings-submit" value="' . t('Submit') . '" /></div>';
45 function fromgplus_addon_settings_post(&$a,&$b) {
50 if($_POST['fromgplus-submit']) {
51 set_pconfig(local_user(),'fromgplus','account',trim($_POST['fromgplus-account']));
52 $enable = ((x($_POST,'fromgplus-enable')) ? intval($_POST['fromgplus-enable']) : 0);
53 set_pconfig(local_user(),'fromgplus','enable', $enable);
54 info( t('Google+ Import Settings saved.') . EOL);
58 function html2bbcode($html) {
60 $bbcode = html_entity_decode($html, ENT_QUOTES, 'UTF-8');
62 $bbcode = str_replace(array("\n"), array(""), $bbcode);
63 $bbcode = str_replace(array("<b>", "</b>"), array("[b]", "[/b]"), $bbcode);
64 $bbcode = str_replace(array("<i>", "</i>"), array("[i]", "[/i]"), $bbcode);
65 $bbcode = str_replace(array("<s>", "</s>"), array("[s]", "[/s]"), $bbcode);
66 $bbcode = str_replace(array("<br />"), array("\n"), $bbcode);
68 $bbcode = trim(strip_tags($bbcode));
72 function friendicapost($post) {
75 $api = new Statusnet($friendica["user"], $friendica["pw"], "GooglePlus", $friendica["server"]);
76 $ret = $api->updateStatus($post);
80 function handleattachments($item) {
83 foreach ($item->object->attachments as $attachment) {
84 switch($attachment->objectType) {
86 //$post .= "\n\n[url=".$attachment->url."]".
87 // "[size=large][b]".html2bbcode($attachment->displayName)."[/b][/size][/url]\n";
88 $post .= "\n\n[bookmark=".$attachment->url."]".html2bbcode($attachment->displayName)."[/bookmark]\n";
90 //if (strpos($attachment->embed->url, "youtube.com"))
91 // $post .= "[youtube]".$attachment->url."[/youtube]\n";
93 /// $post .= "[url=".$attachment->url."][img]".$attachment->image->url."[/img][/url]\n";
95 ///$post .= "[quote]".trim(html2bbcode($attachment->content))."[/quote]";
99 //$post .= "\n\n[url=".$attachment->url."]".
100 // "[size=large][b]".html2bbcode($attachment->displayName)."[/b][/size][/url]\n";
101 $post .= "\n\n[bookmark=".$attachment->url."]".html2bbcode($attachment->displayName)."[/bookmark]\n";
102 $post .= "[quote]".trim(html2bbcode($attachment->content))."[/quote]";
106 //$post .= "\n\n[url=".$attachment->fullImage->url."]".
107 // "[img]".$attachment->fullImage->url."[/img][/url]\n";
108 $post .= "\n\n[img]".$attachment->fullImage->url."[/img]\n";
109 if ($attachment->displayName != "")
110 $post .= html2bbcode($attachment->displayName)."\n";
114 $post .= "\n\n[url=".$attachment->url."]".
115 "[size=large][b]".html2bbcode($attachment->displayName)."[/b][/size][/url]\n";
119 print_r($attachment);
127 $result = file_get_contents("https://www.googleapis.com/plus/v1/people/".$google["id"]."/activities/public?alt=json&pp=1&key=".$google["key"]."&maxResults=".$google["maxfetch"]);
128 $activities = json_decode($result);
130 $state = array("lastid"=>'');
131 if (file_exists($statefile))
132 $state = unserialize(file_get_contents($statefile));
136 foreach($activities->items as $item) {
137 if ($item->id == $state["lastid"])
143 switch($item->object->objectType) {
145 $post = html2bbcode($item->object->content);
147 if (is_array($item->object->attachments))
148 $post .= handleattachments($item);
149 friendicapost($post);
153 $post = html2bbcode($item->annotation)."\n";
154 //$post .= html2bbcode("♲ ");
155 $post .= html2bbcode("♻ ");
156 $post .= "[url=".$item->object->actor->url."]".$item->object->actor->displayName."[/url]";
158 //$post .= "[quote]";
160 $post .= html2bbcode($item->object->content);
162 if (is_array($item->object->attachments))
163 $post .= "\n".trim(handleattachments($item));
165 //$post .= "[/quote]";
167 friendicapost($post);
178 $state['lastid'] = $lastid;
179 file_put_contents($statefile, serialize($state));