]> git.mxchange.org Git - friendica-addons.git/blob - gpluspost/gpluspost.php
gpluspost: Posting to Google+ using an RSS feed that is read via hootsuite
[friendica-addons.git] / gpluspost / gpluspost.php
1 <?php
2
3 /**
4  * Name: G+ Post
5  * Version: 0.1
6  * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
7  */
8
9 function gpluspost_install() {
10 }
11
12
13 function gpluspost_uninstall() {
14 }
15
16 function gpluspost_module() {}
17
18 function gpluspost_init() {
19         global $a, $_SERVER;
20
21         $uid = 1;
22
23         if (isset($a->argv[1])) {
24                 $uid = (int)$a->argv[1];
25         }
26         $pid = 317976;
27
28         $contacts = q("SELECT `name` from contact where ID=%d LIMIT 1", intval($uid));
29
30         header("content-type: application/atom+xml");
31         echo '<?xml version="1.0" encoding="UTF-8"?>'."\n";
32         echo '<feed xmlns="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">'."\n";
33         echo "\t".'<title type="html"><![CDATA['.$a->config['sitename'].']]></title>'."\n";
34         echo "\t".'<subtitle type="html"><![CDATA['.$contacts[0]["name"]."]]></subtitle>\n";
35         echo "\t".'<link rel="self" href="'.$a->get_baseurl().'/gpluspost"/>'."\n";
36         echo "\t<id>".$a->get_baseurl()."/</id>\n";
37         echo "\t".'<link rel="alternate" type="text/html" href="'.$a->get_baseurl().'"/>'."\n";
38         echo "\t<updated>".date("c")."</updated>\n"; // To-Do
39         // <rights>Copyright ... </rights>
40         echo "\t".'<generator uri="'.$a->get_baseurl().'">'.$a->config['sitename'].'</generator>'."\n";
41
42         $pidlist = "262568,262567,269154,271508,270121,273721,314735,312616,311570,308771,308247,306100,295372,291096,290390,290389,283242,283060,280465,273725";
43         $pids = explode(",", $pidlist);
44
45         $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 50");
46         //foreach ($items AS $item)
47         //      gpluspost_feeditem($item["id"]);
48         foreach ($pids AS $pid)
49                 gpluspost_feeditem($pid);
50
51         echo "</feed>\n";
52         killme();
53 }
54
55 function gpluspost_feeditem($pid) {
56         global $a;
57
58         require_once('include/bbcode.php');
59         require_once("include/html2plain.php");
60
61         $max_char = 140;
62
63         $items = q("SELECT `uri`, `plink`, `author-link`, `author-name`, `created`, `edited`, `id`, `title`, `body` from `item` WHERE id=%d", intval($pid));
64         foreach ($items AS $item) {
65                 // To-Do:
66                 // extract the link from the body if there is exactly one link
67
68                 // Looking for the first image
69                 $image = '';
70                 if(preg_match("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/is",$item['body'],$matches))
71                         $image = $matches[3];
72
73                 if ($image == '')
74                         if(preg_match("/\[img\](.*?)\[\/img\]/is",$item['body'],$matches))
75                                 $image = $matches[1];
76
77                 $multipleimages = (strpos($item['body'], "[img") != strrpos($item['body'], "[img"));
78
79                 // When saved into the database the content is sent through htmlspecialchars
80                 // That means that we have to decode all image-urls
81                 $image = htmlspecialchars_decode($image);
82
83                 $link = '';
84                 // look for bookmark-bbcode and handle it with priority
85                 if(preg_match("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/is",$item['body'],$matches))
86                         $link = $matches[1];
87
88                 $multiplelinks = (strpos($item['body'], "[bookmark") != strrpos($item['body'], "[bookmark"));
89
90                 $html = bbcode($item["body"], false, false);
91                 $msg = trim(html2plain($html, 0, true));
92
93                 // If there is no bookmark element then take the first link
94                 if ($link == '') {
95                         $links = collecturls($html);
96                         if (sizeof($links) > 0) {
97                                 reset($links);
98                                 $link = current($links);
99                         }
100                         $multiplelinks = (sizeof($links) > 1);
101                 }
102
103                 $msglink = "";
104                 if ($multiplelinks)
105                         $msglink = $item["plink"];
106                 else if ($link != "")
107                         $msglink = $link;
108                 else if ($multipleimages)
109                         $msglink = $item["plink"];
110                 else if ($image != "")
111                         $msglink = $image;
112
113                 //if (($msglink == "")  and strlen($msg) > $max_char)
114                 if ($msglink == "")
115                         $msglink = $item["plink"];
116
117                 $html = trim(str_replace($msglink, "", $html));
118
119                 // Fetching the title - or the first line
120                 if ($item["title"] != "")
121                         $title = $item["title"];
122                 else {
123                         $lines = explode("\n", $msg);
124                         $title = $lines[0];
125                 }
126                 $title = str_replace("&", "&amp;", $title);
127                 //$html = str_replace("&", "&amp;", $html);
128
129                 echo "\t".'<entry xmlns="http://www.w3.org/2005/Atom">'."\n";
130                 echo "\t\t".'<title type="html" xml:space="preserve"><![CDATA['.$title."]]></title>\n";
131                 echo "\t\t".'<link rel="alternate" type="text/html" href="'.$msglink.'" />'."\n";
132                 // <link rel="enclosure" type="audio/mpeg" length="1337" href="http://example.org/audio/ph34r_my_podcast.mp3"/>
133                 echo "\t\t<id>".$item["uri"]."</id>\n";
134                 //echo "\t\t<updated>".date("c", strtotime($item["edited"]))."</updated>\n";
135                 //echo "\t\t<published>".date("c", strtotime($item["created"]))."</published>\n";
136                 echo "\t\t<updated>".date("c")."</updated>\n";
137                 echo "\t\t<published>".date("c")."</published>\n";
138                 echo "\t\t<author>\n\t\t\t<name><![CDATA[".$item["author-name"]."]]></name>\n";
139                 echo "\t\t\t<uri>".$item["author-link"]."</uri>\n\t\t</author>\n";
140                 //echo '<content type="image/png" src="http://media.example.org/the_beach.png"/>';
141                 echo "\t\t".'<content type="html" xml:space="preserve" xml:base="'.$item["plink"].'"><![CDATA['.$html."]]></content>\n";
142                 echo "\t</entry>\n";
143         }
144 }