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 require_once('mod/share.php');
14 function fromgplus_install() {
15 register_hook('connector_settings', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings');
16 register_hook('connector_settings_post', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings_post');
17 register_hook('cron', 'addon/fromgplus/fromgplus.php', 'fromgplus_cron');
20 function fromgplus_uninstall() {
21 unregister_hook('connector_settings', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings');
22 unregister_hook('connector_settings_post', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings_post');
23 unregister_hook('cron', 'addon/fromgplus/fromgplus.php', 'fromgplus_cron');
26 unregister_hook('plugin_settings', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings');
27 unregister_hook('plugin_settings_post', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings_post');
30 function fromgplus_addon_settings(&$a,&$s) {
35 // If "gpluspost" is installed as well, then the settings are displayed there
36 $result = q("SELECT `installed` FROM `addon` WHERE `name` = 'gpluspost' AND `installed`");
37 if (count($result) > 0)
40 $enable_checked = (intval(get_pconfig(local_user(),'fromgplus','enable')) ? ' checked="checked"' : '');
41 $account = get_pconfig(local_user(),'fromgplus','account');
43 $s .= '<span id="settings_fromgplus_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_fromgplus_expanded\'); openClose(\'settings_fromgplus_inflated\');">';
44 $s .= '<img class="connector" src="images/googleplus.png" /><h3 class="connector">'. t('Google+ Mirror').'</h3>';
46 $s .= '<div id="settings_fromgplus_expanded" class="settings-block" style="display: none;">';
47 $s .= '<span class="fakelink" onclick="openClose(\'settings_fromgplus_expanded\'); openClose(\'settings_fromgplus_inflated\');">';
48 $s .= '<img class="connector" src="images/googleplus.png" /><h3 class="connector">'. t('Google+ Mirror').'</h3>';
51 $s .= '<div id="fromgplus-wrapper">';
53 $s .= '<label id="fromgplus-enable-label" for="fromgplus-enable">'.t('Enable Google+ Import').'</label>';
54 $s .= '<input id="fromgplus-enable" type="checkbox" name="fromgplus-enable" value="1"'.$enable_checked.' />';
55 $s .= '<div class="clear"></div>';
56 $s .= '<label id="fromgplus-label" for="fromgplus-account">'.t('Google Account ID').' </label>';
57 $s .= '<input id="fromgplus-account" type="text" name="fromgplus-account" value="'.$account.'" />';
58 $s .= '</div><div class="clear"></div>';
60 $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="fromgplus-submit" name="fromgplus-submit"
61 class="settings-submit" value="' . t('Save Settings') . '" /></div>';
67 function fromgplus_addon_settings_post(&$a,&$b) {
72 if($_POST['fromgplus-submit']) {
73 set_pconfig(local_user(),'fromgplus','account',trim($_POST['fromgplus-account']));
74 $enable = ((x($_POST,'fromgplus-enable')) ? intval($_POST['fromgplus-enable']) : 0);
75 set_pconfig(local_user(),'fromgplus','enable', $enable);
78 del_pconfig(local_user(),'fromgplus','lastdate');
80 info( t('Google+ Import Settings saved.') . EOL);
84 function fromgplus_plugin_admin(&$a, &$o){
85 $t = get_markup_template("admin.tpl", "addon/fromgplus/");
87 $o = replace_macros($t, array(
88 '$submit' => t('Save Settings'),
89 '$key' => array('key', t('Key'), trim(get_config('fromgplus', 'key')), t('')),
93 function fromgplus_plugin_admin_post(&$a){
94 $key = ((x($_POST,'key')) ? trim($_POST['key']) : '');
95 set_config('fromgplus','key',$key);
96 info( t('Settings updated.'). EOL );
99 function fromgplus_cron($a,$b) {
100 $last = get_config('fromgplus','last_poll');
102 $poll_interval = intval(get_config('fromgplus','poll_interval'));
104 $poll_interval = FROMGPLUS_DEFAULT_POLL_INTERVAL;
107 $next = $last + ($poll_interval * 60);
109 logger('fromgplus: poll intervall not reached');
114 logger('fromgplus: cron_start');
116 $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'fromgplus' AND `k` = 'enable' AND `v` = '1' ORDER BY RAND() ");
119 $account = get_pconfig($rr['uid'],'fromgplus','account');
121 logger('fromgplus: fetching for user '.$rr['uid']);
122 fromgplus_fetch($a, $rr['uid']);
127 logger('fromgplus: cron_end');
129 set_config('fromgplus','last_poll', time());
132 function fromgplus_post($a, $uid, $source, $body, $location) {
136 // Don't know what it is. Maybe some trash from the mobile client
137 $trash = html_entity_decode("", ENT_QUOTES, 'UTF-8');
138 $body = str_replace($trash, "", $body);
142 if (substr($body, 0, 3) == "[b]") {
143 $pos = strpos($body, "[/b]");
144 $title = substr($body, 3, $pos-3);
145 $body = trim(substr($body, $pos+4));
149 $_SESSION['authenticated'] = true;
150 $_SESSION['uid'] = $uid;
153 $_REQUEST['type'] = 'wall';
154 $_REQUEST['api_source'] = true;
156 $_REQUEST['profile_uid'] = $uid;
157 $_REQUEST['source'] = $source;
158 $_REQUEST['extid'] = NETWORK_GPLUS;
161 // $_REQUEST['parent']
162 // $_REQUEST['parent_uri']
164 $_REQUEST['title'] = $title;
165 $_REQUEST['body'] = $body;
166 $_REQUEST['location'] = $location;
168 if (($_REQUEST['title'] == "") AND ($_REQUEST['body'] == "")) {
169 logger('fromgplus: empty post for user '.$uid." ".print_r($_REQUEST, true));
173 require_once('mod/item.php');
174 //print_r($_REQUEST);
175 logger('fromgplus: posting for user '.$uid." ".print_r($_REQUEST, true));
177 logger('fromgplus: done for user '.$uid);
180 function fromgplus_html2bbcode($html) {
182 $bbcode = html_entity_decode($html, ENT_QUOTES, 'UTF-8');
184 $bbcode = str_ireplace(array("\n"), array(""), $bbcode);
185 $bbcode = str_ireplace(array("<b>", "</b>"), array("[b]", "[/b]"), $bbcode);
186 $bbcode = str_ireplace(array("<i>", "</i>"), array("[i]", "[/i]"), $bbcode);
187 $bbcode = str_ireplace(array("<s>", "</s>"), array("[s]", "[/s]"), $bbcode);
188 $bbcode = str_ireplace(array("<br />"), array("\n"), $bbcode);
189 $bbcode = str_ireplace(array("<br/>"), array("\n"), $bbcode);
190 $bbcode = str_ireplace(array("<br>"), array("\n"), $bbcode);
192 $bbcode = trim(strip_tags($bbcode));
196 function fromgplus_parse_query($var)
199 * Use this function to parse out the query array element from
200 * the output of parse_url().
202 $var = parse_url($var, PHP_URL_QUERY);
203 $var = html_entity_decode($var);
204 $var = explode('&', $var);
207 foreach($var as $val) {
208 $x = explode('=', $val);
211 unset($val, $x, $var);
215 function fromgplus_cleanupgoogleproxy($fullImage, $image) {
216 //$preview = "/w".$fullImage->width."-h".$fullImage->height."/";
217 //$preview2 = "/w".$fullImage->width."-h".$fullImage->height."-p/";
218 //$fullImage = str_replace(array($preview, $preview2), array("/", "/"), $fullImage->url);
219 $fullImage = $fullImage->url;
221 //$preview = "/w".$image->width."-h".$image->height."/";
222 //$preview2 = "/w".$image->width."-h".$image->height."-p/";
223 //$image = str_replace(array($preview, $preview2), array("/", "/"), $image->url);
224 $image = $image->url;
228 $queryvar = fromgplus_parse_query($fullImage);
229 if ($queryvar['url'] != "")
230 $cleaned["full"] = urldecode($queryvar['url']);
232 $cleaned["full"] = $fullImage;
233 if (@exif_imagetype($cleaned["full"]) == 0)
234 $cleaned["full"] = "";
236 $queryvar = fromgplus_parse_query($image);
237 if ($queryvar['url'] != "")
238 $cleaned["preview"] = urldecode($queryvar['url']);
240 $cleaned["preview"] = $image;
241 if (@exif_imagetype($cleaned["preview"]) == 0)
242 $cleaned["preview"] = "";
244 if ($cleaned["full"] == "") {
245 $cleaned["full"] = $cleaned["preview"];
246 $cleaned["preview"] = "";
249 if ($cleaned["full"] != "")
250 $infoFull = get_photo_info($cleaned["full"]);
252 $infoFull = array("0" => 0, "1" => 0);
254 if ($cleaned["preview"] != "")
255 $infoPreview = get_photo_info($cleaned["preview"]);
257 $infoFull = array("0" => 0, "1" => 0);
259 if (($infoPreview[0] >= $infoFull[0]) AND ($infoPreview[1] >= $infoFull[1])) {
260 $temp = $cleaned["full"];
261 $cleaned["full"] = $cleaned["preview"];
262 $cleaned["preview"] = $temp;
265 if (($cleaned["full"] == $cleaned["preview"]) OR (($infoPreview[0] == $infoFull[0]) AND ($infoPreview[1] == $infoFull[1])))
266 $cleaned["preview"] = "";
268 if ($cleaned["full"] == "")
269 if (@exif_imagetype($fullImage) != 0)
270 $cleaned["full"] = $fullImage;
272 if ($cleaned["full"] == "")
273 if (@exif_imagetype($image) != 0)
274 $cleaned["full"] = $image;
276 // Could be changed in the future to a link to the album
277 $cleaned["page"] = $cleaned["full"];
282 function fromgplus_cleantext($text) {
284 // Don't know what it is. But it is added to the text.
285 $trash = html_entity_decode("", ENT_QUOTES, 'UTF-8');
287 $text = strip_tags($text);
288 $text = html_entity_decode($text, ENT_QUOTES);
290 $text = str_replace(array("\n", "\r", " ", $trash), array("", "", "", ""), $text);
294 function fromgplus_handleattachments($a, $uid, $item, $displaytext, $shared) {
295 require_once("include/Photo.php");
296 require_once("include/items.php");
297 require_once("include/network.php");
302 $pagedata["type"] = "";
304 foreach ($item->object->attachments as $attachment) {
305 switch($attachment->objectType) {
307 $pagedata["type"] = "video";
308 $pagedata["url"] = original_url($attachment->url);
309 $pagedata["title"] = fromgplus_html2bbcode($attachment->displayName);
313 $pagedata["type"] = "link";
314 $pagedata["url"] = original_url($attachment->url);
315 $pagedata["title"] = fromgplus_html2bbcode($attachment->displayName);
317 $images = fromgplus_cleanupgoogleproxy($attachment->fullImage, $attachment->image);
318 if ($images["full"] != "")
319 $pagedata["images"][0]["src"] = $images["full"];
321 $quote = trim(fromgplus_html2bbcode($attachment->content));
324 $pagedata["text"] = $quote;
329 // Don't store shared pictures in your wall photos (to prevent a possible violating of licenses)
331 $images = fromgplus_cleanupgoogleproxy($attachment->fullImage, $attachment->image);
333 if ($attachment->fullImage->url != "")
334 $images = store_photo($a, $uid, "", $attachment->fullImage->url);
335 elseif ($attachment->image->url != "")
336 $images = store_photo($a, $uid, "", $attachment->image->url);
339 if ($images["preview"] != "") {
340 $post .= "\n[url=".$images["page"]."][img]".$images["preview"]."[/img][/url]\n";
341 $pagedata["images"][0]["src"] = $images["preview"];
342 $pagedata["url"] = $images["page"];
343 } elseif ($images["full"] != "") {
344 $post .= "\n[img]".$images["full"]."[/img]\n";
345 $pagedata["images"][0]["src"] = $images["full"];
347 if ($images["preview"] != "")
348 $pagedata["images"][1]["src"] = $images["preview"];
351 if (($attachment->displayName != "") AND (fromgplus_cleantext($attachment->displayName) != fromgplus_cleantext($displaytext))) {
352 $post .= fromgplus_html2bbcode($attachment->displayName)."\n";
353 $pagedata["title"] = fromgplus_html2bbcode($attachment->displayName);
358 $pagedata["url"] = original_url($attachment->url);
359 $pagedata["title"] = fromgplus_html2bbcode($attachment->displayName);
360 $post .= "\n\n[bookmark=".$pagedata["url"]."]".$pagedata["title"]."[/bookmark]\n";
362 $images = fromgplus_cleanupgoogleproxy($attachment->fullImage, $attachment->image);
364 if ($images["preview"] != "") {
365 $post .= "\n[url=".$images["full"]."][img]".$images["preview"]."[/img][/url]\n";
366 $pagedata["images"][0]["src"] = $images["preview"];
367 $pagedata["url"] = $images["full"];
368 } elseif ($images["full"] != "") {
369 $post .= "\n[img]".$images["full"]."[/img]\n";
370 $pagedata["images"][0]["src"] = $images["full"];
372 if ($images["preview"] != "")
373 $pagedata["images"][1]["src"] = $images["preview"];
378 $pagedata["type"] = "link";
379 $pagedata["url"] = original_url($attachment->url);
380 $pagedata["title"] = fromgplus_html2bbcode($attachment->displayName);
382 $thumb = $attachment->thumbnails[0];
383 $pagedata["images"][0]["src"] = $thumb->image->url;
385 $quote = trim(fromgplus_html2bbcode($thumb->description));
387 $pagedata["text"] = $quote;
392 $pagedata["url"] = original_url($attachment->url);
393 $pagedata["title"] = fromgplus_html2bbcode($attachment->displayName);
394 $post .= "\n\n[bookmark=".$pagedata["url"]."]".$pagedata["title"]."[/bookmark]\n";
398 // die($attachment->objectType);
402 if ($pagedata["type"] != "")
403 return(add_page_info_data($pagedata));
405 return($post.$quote);
408 function fromgplus_fetch($a, $uid) {
411 // Special blank to identify postings from the googleplus connector
412 $blank = html_entity_decode(" ", ENT_QUOTES, 'UTF-8');
414 $account = get_pconfig($uid,'fromgplus','account');
415 $key = get_config('fromgplus','key');
417 $result = fetch_url("https://www.googleapis.com/plus/v1/people/".$account."/activities/public?alt=json&pp=1&key=".$key."&maxResults=".$maxfetch);
418 //$result = file_get_contents("google.txt");
419 //$result = file_get_contents("addon/fromgplus/album.txt");
420 //file_put_contents("google.txt", $result);
422 $activities = json_decode($result);
424 $initiallastdate = get_pconfig($uid,'fromgplus','lastdate');
426 $first_time = ($initiallastdate == "");
430 if (!is_array($activities->items))
433 $reversed = array_reverse($activities->items);
435 foreach($reversed as $item) {
437 if (strtotime($item->published) <= $initiallastdate)
440 // Don't publish items that are too young
441 if (strtotime($item->published) > (time() - 3*60)) {
442 logger('fromgplus_fetch: item too new '.$item->published);
446 if ($lastdate < strtotime($item->published))
447 $lastdate = strtotime($item->published);
452 if ($item->access->description == "Public") {
454 // Loop prevention through the special blank from the googleplus connector
455 //if (strstr($item->object->content, $blank))
456 if (strrpos($item->object->content, $blank) >= strlen($item->object->content) - 5)
459 switch($item->object->objectType) {
461 $post = fromgplus_html2bbcode($item->object->content);
463 if (is_array($item->object->attachments))
464 $post .= fromgplus_handleattachments($a, $uid, $item, $item->object->content, false);
466 // geocode, placeName
467 if (isset($item->address))
468 $location = $item->address;
472 //fromgplus_post($a, $uid, "Google+", $post, $location);
473 fromgplus_post($a, $uid, $item->provider->title, $post, $location);
478 $post = fromgplus_html2bbcode($item->annotation)."\n";
480 if (!intval(get_config('system','old_share'))) {
482 if (function_exists("share_header"))
483 $post .= share_header($item->object->actor->displayName, $item->object->actor->url,
484 $item->object->actor->image->url, "",
485 datetime_convert('UTC','UTC',$item->object->published),$item->object->url);
487 $post .= "[share author='".str_replace("'", "'",$item->object->actor->displayName).
488 "' profile='".$item->object->actor->url.
489 "' avatar='".$item->object->actor->image->url.
490 "' posted='".datetime_convert('UTC','UTC',$item->object->published).
491 "' link='".$item->object->url."']";
493 $post .= fromgplus_html2bbcode($item->object->content);
495 if (is_array($item->object->attachments))
496 $post .= "\n".trim(fromgplus_handleattachments($a, $uid, $item, $item->object->content, true));
500 $post .= fromgplus_html2bbcode("♲");
501 $post .= " [url=".$item->object->actor->url."]".$item->object->actor->displayName."[/url] \n";
502 $post .= fromgplus_html2bbcode($item->object->content);
504 if (is_array($item->object->attachments))
505 $post .= "\n".trim(fromgplus_handleattachments($a, $uid, $item, $item->object->content, true));
508 if (isset($item->address))
509 $location = $item->address;
513 //fromgplus_post($a, $uid, "Google+", $post, $location);
514 fromgplus_post($a, $uid, $item->provider->title, $post, $location);
520 set_pconfig($uid,'fromgplus','lastdate', $lastdate);