]> git.mxchange.org Git - friendica-addons.git/blob - fromgplus/tofriendica.php
Merge pull request #45 from annando/master
[friendica-addons.git] / fromgplus / tofriendica.php
1 <?php
2 require_once("statusnet.lib.php");
3 include("config.php");
4
5 function html2bbcode($html) {
6
7         $bbcode = html_entity_decode($html, ENT_QUOTES, 'UTF-8');
8
9         $bbcode = str_replace(array("\n"), array(""), $bbcode);
10         $bbcode = str_replace(array("<b>", "</b>"), array("[b]", "[/b]"), $bbcode);
11         $bbcode = str_replace(array("<i>", "</i>"), array("[i]", "[/i]"), $bbcode);
12         $bbcode = str_replace(array("<s>", "</s>"), array("[s]", "[/s]"), $bbcode);
13         $bbcode = str_replace(array("<br />"), array("\n"), $bbcode);
14
15         $bbcode = trim(strip_tags($bbcode));
16         return($bbcode);
17 }
18
19 function friendicapost($post) {
20         global $friendica;
21
22         $api = new Statusnet($friendica["user"], $friendica["pw"], "GooglePlus", $friendica["server"]);
23         $ret = $api->updateStatus($post);
24         $api->endSession();
25 }
26
27 function handleattachments($item) {
28         $post = "";
29
30         foreach ($item->object->attachments as $attachment) {
31                 switch($attachment->objectType) {
32                         case "video":
33                                 //$post .= "\n\n[url=".$attachment->url."]".
34                                 //              "[size=large][b]".html2bbcode($attachment->displayName)."[/b][/size][/url]\n";
35                                 $post .= "\n\n[bookmark=".$attachment->url."]".html2bbcode($attachment->displayName)."[/bookmark]\n";
36
37                                 //if (strpos($attachment->embed->url, "youtube.com"))
38                                 //      $post .= "[youtube]".$attachment->url."[/youtube]\n";
39                                 //else
40                                 ///     $post .= "[url=".$attachment->url."][img]".$attachment->image->url."[/img][/url]\n";
41
42                                 ///$post .= "[quote]".trim(html2bbcode($attachment->content))."[/quote]";
43                                 break;
44
45                         case "article":
46                                 //$post .= "\n\n[url=".$attachment->url."]".
47                                 //              "[size=large][b]".html2bbcode($attachment->displayName)."[/b][/size][/url]\n";
48                                 $post .= "\n\n[bookmark=".$attachment->url."]".html2bbcode($attachment->displayName)."[/bookmark]\n";
49                                 $post .= "[quote]".trim(html2bbcode($attachment->content))."[/quote]";
50                                 break;
51
52                         case "photo":
53                                 //$post .= "\n\n[url=".$attachment->fullImage->url."]".
54                                 //              "[img]".$attachment->fullImage->url."[/img][/url]\n";
55                                 $post .= "\n\n[img]".$attachment->fullImage->url."[/img]\n";
56                                 if ($attachment->displayName != "")
57                                         $post .= html2bbcode($attachment->displayName)."\n";
58                                 break;
59
60                         case "photo-album":
61                                 $post .= "\n\n[url=".$attachment->url."]".
62                                                 "[size=large][b]".html2bbcode($attachment->displayName)."[/b][/size][/url]\n";
63                                 break;
64
65                         default:
66                                 print_r($attachment);
67                                 die();
68                                 break;
69                 }
70         }
71         return($post);
72 }
73
74 $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"]);
75 $activities = json_decode($result);
76
77 $state = array("lastid"=>'');
78 if (file_exists($statefile))
79         $state = unserialize(file_get_contents($statefile));
80
81 $lastid = "";
82
83 foreach($activities->items as $item) {
84         if ($item->id == $state["lastid"])
85                 break;
86
87         if ($lastid == "")
88                 $lastid = $item->id;
89
90         switch($item->object->objectType) {
91                 case "note":
92                         $post = html2bbcode($item->object->content);
93
94                         if (is_array($item->object->attachments))
95                                 $post .= handleattachments($item);
96                         friendicapost($post);
97                         break;
98
99                 case "activity":
100                         $post = html2bbcode($item->annotation)."\n";
101                         //$post .= html2bbcode("&#x2672; ");
102                         $post .= html2bbcode("&#x267B; ");
103                         $post .= "[url=".$item->object->actor->url."]".$item->object->actor->displayName."[/url]";
104                         $post .= " \n";
105                         //$post .= "[quote]";
106
107                         $post .= html2bbcode($item->object->content);
108
109                         if (is_array($item->object->attachments))
110                                 $post .= "\n".trim(handleattachments($item));
111
112                         //$post .= "[/quote]";
113
114                         friendicapost($post);
115                         break;
116
117                 default:
118                         print_r($item);
119                         die();
120                         break;
121         }
122 }
123
124 if ($lastid != "") {
125         $state['lastid'] = $lastid;
126         file_put_contents($statefile, serialize($state));
127 }
128 ?>