]> git.mxchange.org Git - friendica-addons.git/blob - fromgplus/fromgplus.php
Merge commit 'upstream/master'
[friendica-addons.git] / fromgplus / fromgplus.php
1 <?php
2 /**
3  * Name: From GPlus
4  * Description: Imports posts from a Google+ account and repeats them
5  * Version: 1.0
6  * Author: Michael Vogel <ike@piratenpartei.de>
7  *
8  */
9
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');
13 }
14
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');
18 }
19
20 function fromgplus_addon_settings(&$a,&$s) {
21
22         if(! local_user())
23                 return;
24
25         $enable_checked = (intval(get_pconfig(local_user(),'fromgplus','enable')) ? ' checked="checked"' : '');
26         $account = get_pconfig(local_user(),'fromgplus','account');
27
28         $s .= '<div class="settings-block">';
29         $s .= '<h3>' . t('Google+ Import Settings').'</h3>';
30         $s .= '<div id="fromgplus-wrapper">';
31
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>';
38
39         $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="fromgplus-submit" name="fromgplus-submit" class="settings-submit" value="' . t('Submit') . '" /></div>';
40         $s .= '</div>';
41
42         return;
43 }
44
45 function fromgplus_addon_settings_post(&$a,&$b) {
46
47         if(! local_user())
48                 return;
49
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);
55         }
56 }
57 /*
58 function html2bbcode($html) {
59
60         $bbcode = html_entity_decode($html, ENT_QUOTES, 'UTF-8');
61
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);
67
68         $bbcode = trim(strip_tags($bbcode));
69         return($bbcode);
70 }
71
72 function friendicapost($post) {
73         global $friendica;
74
75         $api = new Statusnet($friendica["user"], $friendica["pw"], "GooglePlus", $friendica["server"]);
76         $ret = $api->updateStatus($post);
77         $api->endSession();
78 }
79
80 function handleattachments($item) {
81         $post = "";
82
83         foreach ($item->object->attachments as $attachment) {
84                 switch($attachment->objectType) {
85                         case "video":
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";
89
90                                 //if (strpos($attachment->embed->url, "youtube.com"))
91                                 //      $post .= "[youtube]".$attachment->url."[/youtube]\n";
92                                 //else
93                                 ///     $post .= "[url=".$attachment->url."][img]".$attachment->image->url."[/img][/url]\n";
94
95                                 ///$post .= "[quote]".trim(html2bbcode($attachment->content))."[/quote]";
96                                 break;
97
98                         case "article":
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]";
103                                 break;
104
105                         case "photo":
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";
111                                 break;
112
113                         case "photo-album":
114                                 $post .= "\n\n[url=".$attachment->url."]".
115                                                 "[size=large][b]".html2bbcode($attachment->displayName)."[/b][/size][/url]\n";
116                                 break;
117
118                         default:
119                                 print_r($attachment);
120                                 die();
121                                 break;
122                 }
123         }
124         return($post);
125 }
126
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);
129
130 $state = array("lastid"=>'');
131 if (file_exists($statefile))
132         $state = unserialize(file_get_contents($statefile));
133
134 $lastid = "";
135
136 foreach($activities->items as $item) {
137         if ($item->id == $state["lastid"])
138                 break;
139
140         if ($lastid == "")
141                 $lastid = $item->id;
142
143         switch($item->object->objectType) {
144                 case "note":
145                         $post = html2bbcode($item->object->content);
146
147                         if (is_array($item->object->attachments))
148                                 $post .= handleattachments($item);
149                         friendicapost($post);
150                         break;
151
152                 case "activity":
153                         $post = html2bbcode($item->annotation)."\n";
154                         //$post .= html2bbcode("&#x2672; ");
155                         $post .= html2bbcode("&#x267B; ");
156                         $post .= "[url=".$item->object->actor->url."]".$item->object->actor->displayName."[/url]";
157                         $post .= " \n";
158                         //$post .= "[quote]";
159
160                         $post .= html2bbcode($item->object->content);
161
162                         if (is_array($item->object->attachments))
163                                 $post .= "\n".trim(handleattachments($item));
164
165                         //$post .= "[/quote]";
166
167                         friendicapost($post);
168                         break;
169
170                 default:
171                         print_r($item);
172                         die();
173                         break;
174         }
175 }
176
177 if ($lastid != "") {
178         $state['lastid'] = $lastid;
179         file_put_contents($statefile, serialize($state));
180 }
181 */