]> git.mxchange.org Git - friendica-addons.git/blob - fromgplus/fromgplus.php
Merge remote branch '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 - not working by now
5  * Version: 0.1
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" 
40 class="settings-submit" value="' . t('Submit') . '" /></div>';
41         $s .= '</div>';
42
43         return;
44 }
45
46 function fromgplus_addon_settings_post(&$a,&$b) {
47
48         if(! local_user())
49                 return;
50
51         if($_POST['fromgplus-submit']) {
52                 set_pconfig(local_user(),'fromgplus','account',trim($_POST['fromgplus-account']));
53                 $enable = ((x($_POST,'fromgplus-enable')) ? intval($_POST['fromgplus-enable']) : 0);
54                 set_pconfig(local_user(),'fromgplus','enable', $enable);
55                 info( t('Google+ Import Settings saved.') . EOL);
56         }
57 }
58 /*
59 function html2bbcode($html) {
60
61         $bbcode = html_entity_decode($html, ENT_QUOTES, 'UTF-8');
62
63         $bbcode = str_replace(array("\n"), array(""), $bbcode);
64         $bbcode = str_replace(array("<b>", "</b>"), array("[b]", "[/b]"), $bbcode);
65         $bbcode = str_replace(array("<i>", "</i>"), array("[i]", "[/i]"), $bbcode);
66         $bbcode = str_replace(array("<s>", "</s>"), array("[s]", "[/s]"), $bbcode);
67         $bbcode = str_replace(array("<br />"), array("\n"), $bbcode);
68
69         $bbcode = trim(strip_tags($bbcode));
70         return($bbcode);
71 }
72
73 function friendicapost($post) {
74         global $friendica;
75
76         $api = new Statusnet($friendica["user"], $friendica["pw"], "GooglePlus", $friendica["server"]);
77         $ret = $api->updateStatus($post);
78         $api->endSession();
79 }
80
81 function handleattachments($item) {
82         $post = "";
83
84         foreach ($item->object->attachments as $attachment) {
85                 switch($attachment->objectType) {
86                         case "video":
87                                 //$post .= "\n\n[url=".$attachment->url."]".
88                                 //              "[size=large][b]".html2bbcode($attachment->displayName)."[/b][/size][/url]\n";
89                                 $post .= "\n\n[bookmark=".$attachment->url."]".html2bbcode($attachment->displayName)."[/bookmark]\n";
90
91                                 //if (strpos($attachment->embed->url, "youtube.com"))
92                                 //      $post .= "[youtube]".$attachment->url."[/youtube]\n";
93                                 //else
94                                 ///     $post .= "[url=".$attachment->url."][img]".$attachment->image->url."[/img][/url]\n";
95
96                                 ///$post .= "[quote]".trim(html2bbcode($attachment->content))."[/quote]";
97                                 break;
98
99                         case "article":
100                                 //$post .= "\n\n[url=".$attachment->url."]".
101                                 //              "[size=large][b]".html2bbcode($attachment->displayName)."[/b][/size][/url]\n";
102                                 $post .= "\n\n[bookmark=".$attachment->url."]".html2bbcode($attachment->displayName)."[/bookmark]\n";
103                                 $post .= "[quote]".trim(html2bbcode($attachment->content))."[/quote]";
104                                 break;
105
106                         case "photo":
107                                 //$post .= "\n\n[url=".$attachment->fullImage->url."]".
108                                 //              "[img]".$attachment->fullImage->url."[/img][/url]\n";
109                                 $post .= "\n\n[img]".$attachment->fullImage->url."[/img]\n";
110                                 if ($attachment->displayName != "")
111                                         $post .= html2bbcode($attachment->displayName)."\n";
112                                 break;
113
114                         case "photo-album":
115                                 $post .= "\n\n[url=".$attachment->url."]".
116                                                 "[size=large][b]".html2bbcode($attachment->displayName)."[/b][/size][/url]\n";
117                                 break;
118
119                         default:
120                                 print_r($attachment);
121                                 die();
122                                 break;
123                 }
124         }
125         return($post);
126 }
127
128 $result = 
129 file_get_contents("https://www.googleapis.com/plus/v1/people/".$google["id"]."/activities/public?alt=json&pp=1&key=".$google["key"]."&maxResults=".$google["maxfetch"]);
130 $activities = json_decode($result);
131
132 $state = array("lastid"=>'');
133 if (file_exists($statefile))
134         $state = unserialize(file_get_contents($statefile));
135
136 $lastid = "";
137
138 foreach($activities->items as $item) {
139         if ($item->id == $state["lastid"])
140                 break;
141
142         if ($lastid == "")
143                 $lastid = $item->id;
144
145         switch($item->object->objectType) {
146                 case "note":
147                         $post = html2bbcode($item->object->content);
148
149                         if (is_array($item->object->attachments))
150                                 $post .= handleattachments($item);
151                         friendicapost($post);
152                         break;
153
154                 case "activity":
155                         $post = html2bbcode($item->annotation)."\n";
156                         //$post .= html2bbcode("&#x2672; ");
157                         $post .= html2bbcode("&#x267B; ");
158                         $post .= "[url=".$item->object->actor->url."]".$item->object->actor->displayName."[/url]";
159                         $post .= " \n";
160                         //$post .= "[quote]";
161
162                         $post .= html2bbcode($item->object->content);
163
164                         if (is_array($item->object->attachments))
165                                 $post .= "\n".trim(handleattachments($item));
166
167                         //$post .= "[/quote]";
168
169                         friendicapost($post);
170                         break;
171
172                 default:
173                         print_r($item);
174                         die();
175                         break;
176         }
177 }
178
179 if ($lastid != "") {
180         $state['lastid'] = $lastid;
181         file_put_contents($statefile, serialize($state));
182 }
183 */