4 * Description: Imports posts from a Google+ account and repeats them
6 * Author: Michael Vogel <ike@piratenpartei.de>
10 define('FROMGPLUS_DEFAULT_POLL_INTERVAL', 30); // given in minutes
12 function fromgplus_install() {
13 register_hook('plugin_settings', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings');
14 register_hook('plugin_settings_post', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings_post');
15 register_hook('cron', 'addon/fromgplus/fromgplus.php', 'fromgplus_cron');
18 function fromgplus_uninstall() {
19 unregister_hook('plugin_settings', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings');
20 unregister_hook('plugin_settings_post', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings_post');
21 unregister_hook('cron', 'addon/fromgplus/fromgplus.php', 'fromgplus_cron');
24 function fromgplus_addon_settings(&$a,&$s) {
29 $enable_checked = (intval(get_pconfig(local_user(),'fromgplus','enable')) ? ' checked="checked"' : '');
30 $account = get_pconfig(local_user(),'fromgplus','account');
32 $s .= '<div class="settings-block">';
33 $s .= '<h3>' . t('Google+ Import Settings').'</h3>';
34 $s .= '<div id="fromgplus-wrapper">';
36 $s .= '<label id="fromgplus-enable-label" for="fromgplus-enable">'.t('Enable Google+ Import').'</label>';
37 $s .= '<input id="fromgplus-enable" type="checkbox" name="fromgplus-enable" value="1"'.$enable_checked.' />';
38 $s .= '<div class="clear"></div>';
39 $s .= '<label id="fromgplus-label" for="fromgplus-account">'.t('Google Account ID').' </label>';
40 $s .= '<input id="fromgplus-account" type="text" name="fromgplus-account" value="'.$account.'" />';
41 $s .= '</div><div class="clear"></div>';
43 $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="fromgplus-submit" name="fromgplus-submit"
44 class="settings-submit" value="' . t('Submit') . '" /></div>';
50 function fromgplus_addon_settings_post(&$a,&$b) {
55 if($_POST['fromgplus-submit']) {
56 set_pconfig(local_user(),'fromgplus','account',trim($_POST['fromgplus-account']));
57 $enable = ((x($_POST,'fromgplus-enable')) ? intval($_POST['fromgplus-enable']) : 0);
58 set_pconfig(local_user(),'fromgplus','enable', $enable);
59 info( t('Google+ Import Settings saved.') . EOL);
63 function fromgplus_cron($a,$b) {
64 $last = get_config('fromgplus','last_poll');
66 $poll_interval = intval(get_config('fromgplus','poll_interval'));
68 $poll_interval = FROMGPLUS_DEFAULT_POLL_INTERVAL;
71 $next = $last + ($poll_interval * 60);
73 logger('fromgplus: poll intervall not reached');
78 logger('fromgplus: cron_start');
80 $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'fromgplus' AND `k` = 'enable' AND `v` = '1' ORDER BY RAND() ");
83 $account = get_pconfig($rr['uid'],'fromgplus','account');
85 logger('fromgplus: fetching for user '.$rr['uid']);
86 fromgplus_fetch($a, $rr['uid']);
91 logger('fromgplus: cron_end');
93 set_config('fromgplus','last_poll', time());
96 function fromgplus_post($a, $uid, $source, $body, $location) {
102 if (substr($body, 0, 3) == "[b]") {
103 $pos = strpos($body, "[/b]");
104 $title = substr($body, 3, $pos-3);
105 $body = trim(substr($body, $pos+4));
109 $_SESSION['authenticated'] = true;
110 $_SESSION['uid'] = $uid;
112 $_REQUEST['type'] = 'wall';
113 $_REQUEST['api_source'] = true;
115 $_REQUEST['profile_uid'] = $uid;
116 $_REQUEST['source'] = $source;
119 // $_REQUEST['parent']
120 // $_REQUEST['parent_uri']
122 $_REQUEST['title'] = $title;
123 $_REQUEST['body'] = $body;
124 $_REQUEST['location'] = $location;
126 logger('fromgplus: posting for user '.$uid);
128 require_once('mod/item.php');
129 //print_r($_REQUEST);
133 function fromgplus_html2bbcode($html) {
135 $bbcode = html_entity_decode($html, ENT_QUOTES, 'UTF-8');
137 $bbcode = str_ireplace(array("\n"), array(""), $bbcode);
138 $bbcode = str_ireplace(array("<b>", "</b>"), array("[b]", "[/b]"), $bbcode);
139 $bbcode = str_ireplace(array("<i>", "</i>"), array("[i]", "[/i]"), $bbcode);
140 $bbcode = str_ireplace(array("<s>", "</s>"), array("[s]", "[/s]"), $bbcode);
141 $bbcode = str_ireplace(array("<br />"), array("\n"), $bbcode);
142 $bbcode = str_ireplace(array("<br/>"), array("\n"), $bbcode);
143 $bbcode = str_ireplace(array("<br>"), array("\n"), $bbcode);
145 $bbcode = trim(strip_tags($bbcode));
149 function fromgplus_parse_query($var)
152 * Use this function to parse out the query array element from
153 * the output of parse_url().
155 $var = parse_url($var, PHP_URL_QUERY);
156 $var = html_entity_decode($var);
157 $var = explode('&', $var);
160 foreach($var as $val) {
161 $x = explode('=', $val);
164 unset($val, $x, $var);
168 function fromgplus_cleanupgoogleproxy($fullImage, $image) {
170 $preview = "/w".$fullImage->width."-h".$fullImage->height."/";
171 $preview2 = "/w".$fullImage->width."-h".$fullImage->height."-p/";
172 $fullImage = str_replace(array($preview, $preview2), array("/", "/"), $fullImage->url);
174 $preview = "/w".$image->width."-h".$image->height."/";
175 $preview2 = "/w".$image->width."-h".$image->height."-p/";
176 $image = str_replace(array($preview, $preview2), array("/", "/"), $image->url);
180 $queryvar = fromgplus_parse_query($fullImage);
181 if ($queryvar['url'] != "")
182 $cleaned["full"] = urldecode($queryvar['url']);
184 $cleaned["full"] = $fullImage;
185 if (@exif_imagetype($cleaned["full"]) == 0)
186 $cleaned["full"] = "";
188 $queryvar = fromgplus_parse_query($image);
189 if ($queryvar['url'] != "")
190 $cleaned["preview"] = urldecode($queryvar['url']);
192 $cleaned["preview"] = $image;
193 if (@exif_imagetype($cleaned["preview"]) == 0)
194 $cleaned["preview"] = "";
196 if ($cleaned["full"] == "") {
197 $cleaned["full"] = $cleaned["preview"];
198 $cleaned["preview"] = "";
201 if ($cleaned["full"] == $cleaned["preview"])
202 $cleaned["preview"] = "";
204 if ($cleaned["full"] == "")
205 if (@exif_imagetype($fullImage) != 0)
206 $cleaned["full"] = $fullImage;
208 if ($cleaned["full"] == "")
209 if (@exif_imagetype($image) != 0)
210 $cleaned["full"] = $fullImage;
215 function fromgplus_handleattachments($item) {
219 foreach ($item->object->attachments as $attachment) {
220 switch($attachment->objectType) {
222 $post .= "\n\n[bookmark=".$attachment->url."]".fromgplus_html2bbcode($attachment->displayName)."[/bookmark]\n";
224 /*$images = cleanupgoogleproxy($attachment->fullImage, $attachment->image);
225 if ($images["preview"] != "")
226 $post .= "\n[url=".$images["full"]."][img]".$images["preview"]."[/img][/url]\n";
227 elseif ($images["full"] != "")
228 $post .= "\n[img]".$images["full"]."[/img]\n";*/
233 $post .= "\n\n[bookmark=".$attachment->url."]".fromgplus_html2bbcode($attachment->displayName)."[/bookmark]\n";
235 $images = fromgplus_cleanupgoogleproxy($attachment->fullImage, $attachment->image);
236 if ($images["preview"] != "")
237 $post .= "\n[url=".$images["full"]."][img]".$images["preview"]."[/img][/url]\n";
238 elseif ($images["full"] != "")
239 $post .= "\n[img]".$images["full"]."[/img]\n";
241 //$post .= "[quote]".trim(fromgplus_html2bbcode($attachment->content))."[/quote]";
242 $quote = trim(fromgplus_html2bbcode($attachment->content));
244 $quote = "\n[quote]".$quote."[/quote]";
248 $images = fromgplus_cleanupgoogleproxy($attachment->fullImage, $attachment->image);
249 if ($images["preview"] != "")
250 $post .= "\n[url=".$images["full"]."][img]".$images["preview"]."[/img][/url]\n";
251 elseif ($images["full"] != "")
252 $post .= "\n[img]".$images["full"]."[/img]\n";
254 if ($attachment->displayName != "")
255 $post .= fromgplus_html2bbcode($attachment->displayName)."\n";
259 $post .= "\n\n[bookmark=".$attachment->url."]".fromgplus_html2bbcode($attachment->displayName)."[/bookmark]\n";
261 $images = fromgplus_cleanupgoogleproxy($attachment->fullImage, $attachment->image);
262 if ($images["preview"] != "")
263 $post .= "\n[url=".$images["full"]."][img]".$images["preview"]."[/img][/url]\n";
264 elseif ($images["full"] != "")
265 $post .= "\n[img]".$images["full"]."[/img]\n";
270 foreach($attachment->thumbnails as $thumb) {
271 $preview = "/w".$thumb->image->width."-h".$thumb->image->height."/";
272 $preview2 = "/w".$thumb->image->width."-h".$thumb->image->height."-p/";
273 $image = str_replace(array($preview, $preview2), array("/", "/"), $thumb->image->url);
275 $post .= "\n[url=".$thumb->url."][img]".$image."[/img][/url]\n";
279 // die($attachment->objectType);
282 return($post.$quote);
285 function fromgplus_fetch($a, $uid) {
288 $account = get_pconfig($uid,'fromgplus','account');
289 $key = get_config('fromgplus','key');
291 $result = fetch_url("https://www.googleapis.com/plus/v1/people/".$account."/activities/public?alt=json&pp=1&key=".$key."&maxResults=".$maxfetch);
292 //$result = file_get_contents("google.txt");
293 //file_put_contents("google.txt", $result);
295 $activities = json_decode($result);
297 $initiallastdate = get_pconfig($uid,'fromgplus','lastdate');
301 $reversed = array_reverse($activities->items);
303 foreach($reversed as $item) {
304 if (strtotime($item->published) <= $initiallastdate)
307 if ($lastdate < strtotime($item->published))
308 $lastdate = strtotime($item->published);
310 if ($item->access->description == "Public")
311 switch($item->object->objectType) {
313 $post = fromgplus_html2bbcode($item->object->content);
315 if (is_array($item->object->attachments))
316 $post .= fromgplus_handleattachments($item);
318 // geocode, placeName
319 if (isset($item->address))
320 $location = $item->address;
324 fromgplus_post($a, $uid, $item->provider->title, $post, $location);
329 $post = fromgplus_html2bbcode($item->annotation)."\n";
330 $post .= fromgplus_html2bbcode("♲");
331 //$post .= html2bbcode("♻");
332 //$post .= fromgplus_html2bbcode("◌");
333 $post .= " [url=".$item->object->actor->url."]".$item->object->actor->displayName."[/url] \n";
335 /*$post .= "[share author='".$item->object->actor->displayName.
336 "' profile='".$item->object->actor->url.
337 "' avatar='".$item->object->actor->image->url.
338 "' link='".$item->object->url."']\n";*/
340 $post .= fromgplus_html2bbcode($item->object->content);
342 if (is_array($item->object->attachments))
343 $post .= "\n".trim(fromgplus_handleattachments($item));
345 //$post .= "[/share]";
347 if (isset($item->address))
348 $location = $item->address;
352 fromgplus_post($a, $uid, $item->provider->title, $post, $location);
357 set_pconfig($uid,'fromgplus','lastdate', $lastdate);
362 require_once("boot.php");
369 @include(".htconfig.php");
370 require_once("include/dba.php");
371 $db = new dba($db_host, $db_user, $db_pass, $db_data);
372 unset($db_host, $db_user, $db_pass, $db_data);
376 fromgplus_cron($a, $test);