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');
13 require_once('mod/parse_url.php');
15 function fromgplus_install() {
16 register_hook('connector_settings', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings');
17 register_hook('connector_settings_post', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings_post');
18 register_hook('cron', 'addon/fromgplus/fromgplus.php', 'fromgplus_cron');
21 function fromgplus_uninstall() {
22 unregister_hook('connector_settings', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings');
23 unregister_hook('connector_settings_post', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings_post');
24 unregister_hook('cron', 'addon/fromgplus/fromgplus.php', 'fromgplus_cron');
27 unregister_hook('plugin_settings', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings');
28 unregister_hook('plugin_settings_post', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings_post');
31 function fromgplus_addon_settings(&$a,&$s) {
36 // If "gpluspost" is installed as well, then the settings are displayed there
37 $result = q("SELECT `installed` FROM `addon` WHERE `name` = 'gpluspost' AND `installed`");
38 if (count($result) > 0)
41 $enable_checked = (intval(get_pconfig(local_user(),'fromgplus','enable')) ? ' checked="checked"' : '');
42 $account = get_pconfig(local_user(),'fromgplus','account');
44 $s .= '<span id="settings_fromgplus_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_fromgplus_expanded\'); openClose(\'settings_fromgplus_inflated\');">';
45 $s .= '<img class="connector" src="images/googleplus.png" /><h3 class="connector">'. t('Google+ Mirror').'</h3>';
47 $s .= '<div id="settings_fromgplus_expanded" class="settings-block" style="display: none;">';
48 $s .= '<span class="fakelink" onclick="openClose(\'settings_fromgplus_expanded\'); openClose(\'settings_fromgplus_inflated\');">';
49 $s .= '<img class="connector" src="images/googleplus.png" /><h3 class="connector">'. t('Google+ Mirror').'</h3>';
52 $s .= '<div id="fromgplus-wrapper">';
54 $s .= '<label id="fromgplus-enable-label" for="fromgplus-enable">'.t('Enable Google+ Import').'</label>';
55 $s .= '<input id="fromgplus-enable" type="checkbox" name="fromgplus-enable" value="1"'.$enable_checked.' />';
56 $s .= '<div class="clear"></div>';
57 $s .= '<label id="fromgplus-label" for="fromgplus-account">'.t('Google Account ID').' </label>';
58 $s .= '<input id="fromgplus-account" type="text" name="fromgplus-account" value="'.$account.'" />';
59 $s .= '</div><div class="clear"></div>';
61 $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="fromgplus-submit" name="fromgplus-submit"
62 class="settings-submit" value="' . t('Save Settings') . '" /></div>';
68 function fromgplus_addon_settings_post(&$a,&$b) {
73 if($_POST['fromgplus-submit']) {
74 set_pconfig(local_user(),'fromgplus','account',trim($_POST['fromgplus-account']));
75 $enable = ((x($_POST,'fromgplus-enable')) ? intval($_POST['fromgplus-enable']) : 0);
76 set_pconfig(local_user(),'fromgplus','enable', $enable);
79 del_pconfig(local_user(),'fromgplus','lastdate');
81 info( t('Google+ Import Settings saved.') . EOL);
85 function fromgplus_plugin_admin(&$a, &$o){
86 $t = get_markup_template("admin.tpl", "addon/fromgplus/");
88 $o = replace_macros($t, array(
89 '$submit' => t('Save Settings'),
90 '$key' => array('key', t('Key'), trim(get_config('fromgplus', 'key')), t('')),
94 function fromgplus_plugin_admin_post(&$a){
95 $key = ((x($_POST,'key')) ? trim($_POST['key']) : '');
96 set_config('fromgplus','key',$key);
97 info( t('Settings updated.'). EOL );
100 function fromgplus_cron($a,$b) {
101 $last = get_config('fromgplus','last_poll');
103 $poll_interval = intval(get_config('fromgplus','poll_interval'));
105 $poll_interval = FROMGPLUS_DEFAULT_POLL_INTERVAL;
108 $next = $last + ($poll_interval * 60);
110 logger('fromgplus: poll intervall not reached');
115 logger('fromgplus: cron_start');
117 $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'fromgplus' AND `k` = 'enable' AND `v` = '1' ORDER BY RAND() ");
120 $account = get_pconfig($rr['uid'],'fromgplus','account');
122 logger('fromgplus: fetching for user '.$rr['uid']);
123 fromgplus_fetch($a, $rr['uid']);
128 logger('fromgplus: cron_end');
130 set_config('fromgplus','last_poll', time());
133 function fromgplus_post($a, $uid, $source, $body, $location) {
137 // Don't know what it is. Maybe some trash from the mobile client
138 $trash = html_entity_decode("", ENT_QUOTES, 'UTF-8');
139 $body = str_replace($trash, "", $body);
143 if (substr($body, 0, 3) == "[b]") {
144 $pos = strpos($body, "[/b]");
145 $title = substr($body, 3, $pos-3);
146 $body = trim(substr($body, $pos+4));
150 $_SESSION['authenticated'] = true;
151 $_SESSION['uid'] = $uid;
154 $_REQUEST['type'] = 'wall';
155 $_REQUEST['api_source'] = true;
157 $_REQUEST['profile_uid'] = $uid;
158 $_REQUEST['source'] = $source;
159 $_REQUEST['extid'] = NETWORK_GPLUS;
162 // $_REQUEST['parent']
163 // $_REQUEST['parent_uri']
165 $_REQUEST['title'] = $title;
166 $_REQUEST['body'] = $body;
167 $_REQUEST['location'] = $location;
169 if (($_REQUEST['title'] == "") AND ($_REQUEST['body'] == "")) {
170 logger('fromgplus: empty post for user '.$uid." ".print_r($_REQUEST, true));
174 require_once('mod/item.php');
175 //print_r($_REQUEST);
176 logger('fromgplus: posting for user '.$uid." ".print_r($_REQUEST, true));
178 logger('fromgplus: done for user '.$uid);
181 function fromgplus_html2bbcode($html) {
183 $bbcode = html_entity_decode($html, ENT_QUOTES, 'UTF-8');
185 $bbcode = str_ireplace(array("\n"), array(""), $bbcode);
186 $bbcode = str_ireplace(array("<b>", "</b>"), array("[b]", "[/b]"), $bbcode);
187 $bbcode = str_ireplace(array("<i>", "</i>"), array("[i]", "[/i]"), $bbcode);
188 $bbcode = str_ireplace(array("<s>", "</s>"), array("[s]", "[/s]"), $bbcode);
189 $bbcode = str_ireplace(array("<br />"), array("\n"), $bbcode);
190 $bbcode = str_ireplace(array("<br/>"), array("\n"), $bbcode);
191 $bbcode = str_ireplace(array("<br>"), array("\n"), $bbcode);
193 $bbcode = trim(strip_tags($bbcode));
197 function fromgplus_parse_query($var)
200 * Use this function to parse out the query array element from
201 * the output of parse_url().
203 $var = parse_url($var, PHP_URL_QUERY);
204 $var = html_entity_decode($var);
205 $var = explode('&', $var);
208 foreach($var as $val) {
209 $x = explode('=', $val);
212 unset($val, $x, $var);
216 function fromgplus_cleanupgoogleproxy($fullImage, $image) {
217 //$preview = "/w".$fullImage->width."-h".$fullImage->height."/";
218 //$preview2 = "/w".$fullImage->width."-h".$fullImage->height."-p/";
219 //$fullImage = str_replace(array($preview, $preview2), array("/", "/"), $fullImage->url);
220 $fullImage = $fullImage->url;
222 //$preview = "/w".$image->width."-h".$image->height."/";
223 //$preview2 = "/w".$image->width."-h".$image->height."-p/";
224 //$image = str_replace(array($preview, $preview2), array("/", "/"), $image->url);
225 $image = $image->url;
229 $queryvar = fromgplus_parse_query($fullImage);
230 if ($queryvar['url'] != "")
231 $cleaned["full"] = urldecode($queryvar['url']);
233 $cleaned["full"] = $fullImage;
234 if (@exif_imagetype($cleaned["full"]) == 0)
235 $cleaned["full"] = "";
237 $queryvar = fromgplus_parse_query($image);
238 if ($queryvar['url'] != "")
239 $cleaned["preview"] = urldecode($queryvar['url']);
241 $cleaned["preview"] = $image;
242 if (@exif_imagetype($cleaned["preview"]) == 0)
243 $cleaned["preview"] = "";
245 if ($cleaned["full"] == "") {
246 $cleaned["full"] = $cleaned["preview"];
247 $cleaned["preview"] = "";
250 if ($cleaned["full"] != "")
251 $infoFull = get_photo_info($cleaned["full"]);
253 $infoFull = array("0" => 0, "1" => 0);
255 if ($cleaned["preview"] != "")
256 $infoPreview = get_photo_info($cleaned["preview"]);
258 $infoFull = array("0" => 0, "1" => 0);
260 if (($infoPreview[0] >= $infoFull[0]) AND ($infoPreview[1] >= $infoFull[1])) {
261 $temp = $cleaned["full"];
262 $cleaned["full"] = $cleaned["preview"];
263 $cleaned["preview"] = $temp;
266 if (($cleaned["full"] == $cleaned["preview"]) OR (($infoPreview[0] == $infoFull[0]) AND ($infoPreview[1] == $infoFull[1])))
267 $cleaned["preview"] = "";
269 if ($cleaned["full"] == "")
270 if (@exif_imagetype($fullImage) != 0)
271 $cleaned["full"] = $fullImage;
273 if ($cleaned["full"] == "")
274 if (@exif_imagetype($image) != 0)
275 $cleaned["full"] = $image;
277 // Could be changed in the future to a link to the album
278 $cleaned["page"] = $cleaned["full"];
283 function fromgplus_cleantext($text) {
285 // Don't know what it is. But it is added to the text.
286 $trash = html_entity_decode("", ENT_QUOTES, 'UTF-8');
288 $text = strip_tags($text);
289 $text = html_entity_decode($text, ENT_QUOTES);
291 $text = str_replace(array("\n", "\r", " ", $trash), array("", "", "", ""), $text);
295 function fromgplus_handleattachments($a, $uid, $item, $displaytext, $shared) {
296 require_once("include/Photo.php");
297 require_once("include/items.php");
298 require_once("include/network.php");
303 $pagedata["type"] = "";
305 foreach ($item->object->attachments as $attachment) {
306 switch($attachment->objectType) {
308 $pagedata["type"] = "video";
309 $pagedata["url"] = original_url($attachment->url);
310 $pagedata["title"] = fromgplus_html2bbcode($attachment->displayName);
314 $pagedata["type"] = "link";
315 $pagedata["url"] = original_url($attachment->url);
316 $pagedata["title"] = fromgplus_html2bbcode($attachment->displayName);
318 $images = fromgplus_cleanupgoogleproxy($attachment->fullImage, $attachment->image);
319 if ($images["full"] != "")
320 $pagedata["images"][0]["src"] = $images["full"];
322 $quote = trim(fromgplus_html2bbcode($attachment->content));
325 $pagedata["text"] = $quote;
327 // Add Keywords to page link
328 $data = parseurl_getsiteinfo_cached($pagedata["url"], true);
329 if (isset($data["keywords"]))
330 $pagedata["keywords"] = $data["keywords"];
335 // Don't store shared pictures in your wall photos (to prevent a possible violating of licenses)
337 $images = fromgplus_cleanupgoogleproxy($attachment->fullImage, $attachment->image);
339 if ($attachment->fullImage->url != "")
340 $images = store_photo($a, $uid, "", $attachment->fullImage->url);
341 elseif ($attachment->image->url != "")
342 $images = store_photo($a, $uid, "", $attachment->image->url);
345 if ($images["preview"] != "") {
346 $post .= "\n[url=".$images["page"]."][img]".$images["preview"]."[/img][/url]\n";
347 $pagedata["images"][0]["src"] = $images["preview"];
348 $pagedata["url"] = $images["page"];
349 } elseif ($images["full"] != "") {
350 $post .= "\n[img]".$images["full"]."[/img]\n";
351 $pagedata["images"][0]["src"] = $images["full"];
353 if ($images["preview"] != "")
354 $pagedata["images"][1]["src"] = $images["preview"];
357 if (($attachment->displayName != "") AND (fromgplus_cleantext($attachment->displayName) != fromgplus_cleantext($displaytext))) {
358 $post .= fromgplus_html2bbcode($attachment->displayName)."\n";
359 $pagedata["title"] = fromgplus_html2bbcode($attachment->displayName);
364 $pagedata["url"] = original_url($attachment->url);
365 $pagedata["title"] = fromgplus_html2bbcode($attachment->displayName);
366 $post .= "\n\n[bookmark=".$pagedata["url"]."]".$pagedata["title"]."[/bookmark]\n";
368 $images = fromgplus_cleanupgoogleproxy($attachment->fullImage, $attachment->image);
370 if ($images["preview"] != "") {
371 $post .= "\n[url=".$images["full"]."][img]".$images["preview"]."[/img][/url]\n";
372 $pagedata["images"][0]["src"] = $images["preview"];
373 $pagedata["url"] = $images["full"];
374 } elseif ($images["full"] != "") {
375 $post .= "\n[img]".$images["full"]."[/img]\n";
376 $pagedata["images"][0]["src"] = $images["full"];
378 if ($images["preview"] != "")
379 $pagedata["images"][1]["src"] = $images["preview"];
384 $pagedata["type"] = "link";
385 $pagedata["url"] = original_url($attachment->url);
386 $pagedata["title"] = fromgplus_html2bbcode($attachment->displayName);
388 $thumb = $attachment->thumbnails[0];
389 $pagedata["images"][0]["src"] = $thumb->image->url;
391 $quote = trim(fromgplus_html2bbcode($thumb->description));
393 $pagedata["text"] = $quote;
398 $pagedata["url"] = original_url($attachment->url);
399 $pagedata["title"] = fromgplus_html2bbcode($attachment->displayName);
400 $post .= "\n\n[bookmark=".$pagedata["url"]."]".$pagedata["title"]."[/bookmark]\n";
404 // die($attachment->objectType);
408 if ($pagedata["type"] != "")
409 return(add_page_info_data($pagedata));
411 return($post.$quote);
414 function fromgplus_fetch($a, $uid) {
417 // Special blank to identify postings from the googleplus connector
418 $blank = html_entity_decode(" ", ENT_QUOTES, 'UTF-8');
420 $account = get_pconfig($uid,'fromgplus','account');
421 $key = get_config('fromgplus','key');
423 $result = fetch_url("https://www.googleapis.com/plus/v1/people/".$account."/activities/public?alt=json&pp=1&key=".$key."&maxResults=".$maxfetch);
424 //$result = file_get_contents("google.txt");
425 //$result = file_get_contents("addon/fromgplus/album.txt");
426 //file_put_contents("google.txt", $result);
428 $activities = json_decode($result);
430 $initiallastdate = get_pconfig($uid,'fromgplus','lastdate');
432 $first_time = ($initiallastdate == "");
436 if (!is_array($activities->items))
439 $reversed = array_reverse($activities->items);
441 foreach($reversed as $item) {
443 if (strtotime($item->published) <= $initiallastdate)
446 // Don't publish items that are too young
447 if (strtotime($item->published) > (time() - 3*60)) {
448 logger('fromgplus_fetch: item too new '.$item->published);
452 if ($lastdate < strtotime($item->published))
453 $lastdate = strtotime($item->published);
458 if ($item->access->description == "Public") {
460 // Loop prevention through the special blank from the googleplus connector
461 //if (strstr($item->object->content, $blank))
462 if (strrpos($item->object->content, $blank) >= strlen($item->object->content) - 5)
465 switch($item->object->objectType) {
467 $post = fromgplus_html2bbcode($item->object->content);
469 if (is_array($item->object->attachments))
470 $post .= fromgplus_handleattachments($a, $uid, $item, $item->object->content, false);
472 // geocode, placeName
473 if (isset($item->address))
474 $location = $item->address;
478 //fromgplus_post($a, $uid, "Google+", $post, $location);
479 fromgplus_post($a, $uid, $item->provider->title, $post, $location);
484 $post = fromgplus_html2bbcode($item->annotation)."\n";
486 if (!intval(get_config('system','old_share'))) {
488 if (function_exists("share_header"))
489 $post .= share_header($item->object->actor->displayName, $item->object->actor->url,
490 $item->object->actor->image->url, "",
491 datetime_convert('UTC','UTC',$item->object->published),$item->object->url);
493 $post .= "[share author='".str_replace("'", "'",$item->object->actor->displayName).
494 "' profile='".$item->object->actor->url.
495 "' avatar='".$item->object->actor->image->url.
496 "' posted='".datetime_convert('UTC','UTC',$item->object->published).
497 "' link='".$item->object->url."']";
499 $post .= fromgplus_html2bbcode($item->object->content);
501 if (is_array($item->object->attachments))
502 $post .= "\n".trim(fromgplus_handleattachments($a, $uid, $item, $item->object->content, true));
506 $post .= fromgplus_html2bbcode("♲");
507 $post .= " [url=".$item->object->actor->url."]".$item->object->actor->displayName."[/url] \n";
508 $post .= fromgplus_html2bbcode($item->object->content);
510 if (is_array($item->object->attachments))
511 $post .= "\n".trim(fromgplus_handleattachments($a, $uid, $item, $item->object->content, true));
514 if (isset($item->address))
515 $location = $item->address;
519 //fromgplus_post($a, $uid, "Google+", $post, $location);
520 fromgplus_post($a, $uid, $item->provider->title, $post, $location);
526 set_pconfig($uid,'fromgplus','lastdate', $lastdate);