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('connector_settings', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings');
14 register_hook('connector_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('connector_settings', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings');
20 unregister_hook('connector_settings_post', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings_post');
21 unregister_hook('cron', 'addon/fromgplus/fromgplus.php', 'fromgplus_cron');
24 unregister_hook('plugin_settings', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings');
25 unregister_hook('plugin_settings_post', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings_post');
28 function fromgplus_addon_settings(&$a,&$s) {
33 // If "gpluspost" is installed as well, then the settings are displayed there
34 $result = q("SELECT `installed` FROM `addon` WHERE `name` = 'gpluspost' AND `installed`");
35 if (count($result) > 0)
38 $enable_checked = (intval(get_pconfig(local_user(),'fromgplus','enable')) ? ' checked="checked"' : '');
39 $account = get_pconfig(local_user(),'fromgplus','account');
41 $s .= '<span id="settings_fromgplus_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_fromgplus_expanded\'); openClose(\'settings_fromgplus_inflated\');">';
42 $s .= '<img class="connector" src="images/googleplus.png" /><h3 class="connector">'. t('Google+ Mirror').'</h3>';
44 $s .= '<div id="settings_fromgplus_expanded" class="settings-block" style="display: none;">';
45 $s .= '<span class="fakelink" onclick="openClose(\'settings_fromgplus_expanded\'); openClose(\'settings_fromgplus_inflated\');">';
46 $s .= '<img class="connector" src="images/googleplus.png" /><h3 class="connector">'. t('Google+ Mirror').'</h3>';
49 $s .= '<div id="fromgplus-wrapper">';
51 $s .= '<label id="fromgplus-enable-label" for="fromgplus-enable">'.t('Enable Google+ Import').'</label>';
52 $s .= '<input id="fromgplus-enable" type="checkbox" name="fromgplus-enable" value="1"'.$enable_checked.' />';
53 $s .= '<div class="clear"></div>';
54 $s .= '<label id="fromgplus-label" for="fromgplus-account">'.t('Google Account ID').' </label>';
55 $s .= '<input id="fromgplus-account" type="text" name="fromgplus-account" value="'.$account.'" />';
56 $s .= '</div><div class="clear"></div>';
58 $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="fromgplus-submit" name="fromgplus-submit"
59 class="settings-submit" value="' . t('Save Settings') . '" /></div>';
65 function fromgplus_addon_settings_post(&$a,&$b) {
70 if($_POST['fromgplus-submit']) {
71 set_pconfig(local_user(),'fromgplus','account',trim($_POST['fromgplus-account']));
72 $enable = ((x($_POST,'fromgplus-enable')) ? intval($_POST['fromgplus-enable']) : 0);
73 set_pconfig(local_user(),'fromgplus','enable', $enable);
76 del_pconfig(local_user(),'fromgplus','lastdate');
78 info( t('Google+ Import Settings saved.') . EOL);
82 function fromgplus_cron($a,$b) {
83 $last = get_config('fromgplus','last_poll');
85 $poll_interval = intval(get_config('fromgplus','poll_interval'));
87 $poll_interval = FROMGPLUS_DEFAULT_POLL_INTERVAL;
90 $next = $last + ($poll_interval * 60);
92 logger('fromgplus: poll intervall not reached');
97 logger('fromgplus: cron_start');
99 $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'fromgplus' AND `k` = 'enable' AND `v` = '1' ORDER BY RAND() ");
102 $account = get_pconfig($rr['uid'],'fromgplus','account');
104 logger('fromgplus: fetching for user '.$rr['uid']);
105 fromgplus_fetch($a, $rr['uid']);
110 logger('fromgplus: cron_end');
112 set_config('fromgplus','last_poll', time());
115 function fromgplus_post($a, $uid, $source, $body, $location) {
119 // Don't know what it is. Maybe some trash from the mobile client
120 $trash = html_entity_decode("", ENT_QUOTES, 'UTF-8');
121 $body = str_replace($trash, "", $body);
125 if (substr($body, 0, 3) == "[b]") {
126 $pos = strpos($body, "[/b]");
127 $title = substr($body, 3, $pos-3);
128 $body = trim(substr($body, $pos+4));
132 $_SESSION['authenticated'] = true;
133 $_SESSION['uid'] = $uid;
136 $_REQUEST['type'] = 'wall';
137 $_REQUEST['api_source'] = true;
139 $_REQUEST['profile_uid'] = $uid;
140 $_REQUEST['source'] = $source;
143 // $_REQUEST['parent']
144 // $_REQUEST['parent_uri']
146 $_REQUEST['title'] = $title;
147 $_REQUEST['body'] = $body;
148 $_REQUEST['location'] = $location;
150 if (($_REQUEST['title'] == "") AND ($_REQUEST['body'] == "")) {
151 logger('fromgplus: empty post for user '.$uid." ".print_r($_REQUEST, true));
155 require_once('mod/item.php');
156 //print_r($_REQUEST);
157 logger('fromgplus: posting for user '.$uid." ".print_r($_REQUEST, true));
159 logger('fromgplus: done for user '.$uid);
162 function fromgplus_html2bbcode($html) {
164 $bbcode = html_entity_decode($html, ENT_QUOTES, 'UTF-8');
166 $bbcode = str_ireplace(array("\n"), array(""), $bbcode);
167 $bbcode = str_ireplace(array("<b>", "</b>"), array("[b]", "[/b]"), $bbcode);
168 $bbcode = str_ireplace(array("<i>", "</i>"), array("[i]", "[/i]"), $bbcode);
169 $bbcode = str_ireplace(array("<s>", "</s>"), array("[s]", "[/s]"), $bbcode);
170 $bbcode = str_ireplace(array("<br />"), array("\n"), $bbcode);
171 $bbcode = str_ireplace(array("<br/>"), array("\n"), $bbcode);
172 $bbcode = str_ireplace(array("<br>"), array("\n"), $bbcode);
174 $bbcode = trim(strip_tags($bbcode));
178 function fromgplus_parse_query($var)
181 * Use this function to parse out the query array element from
182 * the output of parse_url().
184 $var = parse_url($var, PHP_URL_QUERY);
185 $var = html_entity_decode($var);
186 $var = explode('&', $var);
189 foreach($var as $val) {
190 $x = explode('=', $val);
193 unset($val, $x, $var);
197 function fromgplus_cleanupgoogleproxy($fullImage, $image) {
198 //$preview = "/w".$fullImage->width."-h".$fullImage->height."/";
199 //$preview2 = "/w".$fullImage->width."-h".$fullImage->height."-p/";
200 //$fullImage = str_replace(array($preview, $preview2), array("/", "/"), $fullImage->url);
201 $fullImage = $fullImage->url;
203 //$preview = "/w".$image->width."-h".$image->height."/";
204 //$preview2 = "/w".$image->width."-h".$image->height."-p/";
205 //$image = str_replace(array($preview, $preview2), array("/", "/"), $image->url);
206 $image = $image->url;
210 $queryvar = fromgplus_parse_query($fullImage);
211 if ($queryvar['url'] != "")
212 $cleaned["full"] = urldecode($queryvar['url']);
214 $cleaned["full"] = $fullImage;
215 if (@exif_imagetype($cleaned["full"]) == 0)
216 $cleaned["full"] = "";
218 $queryvar = fromgplus_parse_query($image);
219 if ($queryvar['url'] != "")
220 $cleaned["preview"] = urldecode($queryvar['url']);
222 $cleaned["preview"] = $image;
223 if (@exif_imagetype($cleaned["preview"]) == 0)
224 $cleaned["preview"] = "";
226 if ($cleaned["full"] == "") {
227 $cleaned["full"] = $cleaned["preview"];
228 $cleaned["preview"] = "";
231 if ($cleaned["full"] != "")
232 $infoFull = get_photo_info($cleaned["full"]);
234 $infoFull = array("0" => 0, "1" => 0);
236 if ($cleaned["preview"] != "")
237 $infoPreview = get_photo_info($cleaned["preview"]);
239 $infoFull = array("0" => 0, "1" => 0);
241 if (($infoPreview[0] >= $infoFull[0]) AND ($infoPreview[1] >= $infoFull[1])) {
242 $temp = $cleaned["full"];
243 $cleaned["full"] = $cleaned["preview"];
244 $cleaned["preview"] = $temp;
247 if (($cleaned["full"] == $cleaned["preview"]) OR (($infoPreview[0] == $infoFull[0]) AND ($infoPreview[1] == $infoFull[1])))
248 $cleaned["preview"] = "";
250 if ($cleaned["full"] == "")
251 if (@exif_imagetype($fullImage) != 0)
252 $cleaned["full"] = $fullImage;
254 if ($cleaned["full"] == "")
255 if (@exif_imagetype($image) != 0)
256 $cleaned["full"] = $image;
258 // Could be changed in the future to a link to the album
259 $cleaned["page"] = $cleaned["full"];
264 function fromgplus_cleantext($text) {
266 // Don't know what it is. But it is added to the text.
267 $trash = html_entity_decode("", ENT_QUOTES, 'UTF-8');
269 $text = strip_tags($text);
270 $text = html_entity_decode($text, ENT_QUOTES);
272 $text = str_replace(array("\n", "\r", " ", $trash), array("", "", "", ""), $text);
276 function fromgplus_handleattachments($a, $uid, $item, $displaytext, $shared) {
277 require_once("include/Photo.php");
278 require_once("include/items.php");
283 $pagedata["type"] = "";
285 foreach ($item->object->attachments as $attachment) {
286 switch($attachment->objectType) {
288 $pagedata["type"] = "video";
289 $pagedata["url"] = $attachment->url;
290 $pagedata["title"] = fromgplus_html2bbcode($attachment->displayName);
294 $pagedata["type"] = "link";
295 $pagedata["url"] = $attachment->url;
296 $pagedata["title"] = fromgplus_html2bbcode($attachment->displayName);
298 $images = fromgplus_cleanupgoogleproxy($attachment->fullImage, $attachment->image);
299 if ($images["full"] != "")
300 $pagedata["images"][0]["src"] = $images["full"];
302 $quote = trim(fromgplus_html2bbcode($attachment->content));
305 $pagedata["text"] = $quote;
310 // Don't store shared pictures in your wall photos (to prevent a possible violating of licenses)
312 $images = fromgplus_cleanupgoogleproxy($attachment->fullImage, $attachment->image);
314 if ($attachment->fullImage->url != "")
315 $images = store_photo($a, $uid, "", $attachment->fullImage->url);
316 elseif ($attachment->image->url != "")
317 $images = store_photo($a, $uid, "", $attachment->image->url);
320 if ($images["preview"] != "") {
321 $post .= "\n[url=".$images["page"]."][img]".$images["preview"]."[/img][/url]\n";
322 $pagedata["images"][0]["src"] = $images["preview"];
323 $pagedata["url"] = $images["page"];
324 } elseif ($images["full"] != "") {
325 $post .= "\n[img]".$images["full"]."[/img]\n";
326 $pagedata["images"][0]["src"] = $images["full"];
328 if ($images["preview"] != "")
329 $pagedata["images"][1]["src"] = $images["preview"];
332 if (($attachment->displayName != "") AND (fromgplus_cleantext($attachment->displayName) != fromgplus_cleantext($displaytext))) {
333 $post .= fromgplus_html2bbcode($attachment->displayName)."\n";
334 $pagedata["title"] = fromgplus_html2bbcode($attachment->displayName);
339 $pagedata["url"] = $attachment->url;
340 $pagedata["title"] = fromgplus_html2bbcode($attachment->displayName);
341 $post .= "\n\n[bookmark=".$attachment->url."]".fromgplus_html2bbcode($attachment->displayName)."[/bookmark]\n";
343 $images = fromgplus_cleanupgoogleproxy($attachment->fullImage, $attachment->image);
345 if ($images["preview"] != "") {
346 $post .= "\n[url=".$images["full"]."][img]".$images["preview"]."[/img][/url]\n";
347 $pagedata["images"][0]["src"] = $images["preview"];
348 $pagedata["url"] = $images["full"];
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"];
359 $pagedata["type"] = "link";
360 $pagedata["url"] = $attachment->url;
361 $pagedata["title"] = fromgplus_html2bbcode($attachment->displayName);
363 $thumb = $attachment->thumbnails[0];
364 $pagedata["images"][0]["src"] = $thumb->image->url;
366 $quote = trim(fromgplus_html2bbcode($thumb->description));
368 $pagedata["text"] = $quote;
373 $pagedata["url"] = $attachment->url;
374 $pagedata["title"] = fromgplus_html2bbcode($attachment->displayName);
375 $post .= "\n\n[bookmark=".$pagedata["url"]."]".$pagedata["title"]."[/bookmark]\n";
379 // die($attachment->objectType);
383 if ($pagedata["type"] != "")
384 return(add_page_info_data($pagedata));
386 return($post.$quote);
389 function fromgplus_fetch($a, $uid) {
392 // Special blank to identify postings from the googleplus connector
393 $blank = html_entity_decode(" ", ENT_QUOTES, 'UTF-8');
395 $account = get_pconfig($uid,'fromgplus','account');
396 $key = get_config('fromgplus','key');
398 $result = fetch_url("https://www.googleapis.com/plus/v1/people/".$account."/activities/public?alt=json&pp=1&key=".$key."&maxResults=".$maxfetch);
399 //$result = file_get_contents("google.txt");
400 //$result = file_get_contents("addon/fromgplus/album.txt");
401 //file_put_contents("google.txt", $result);
403 $activities = json_decode($result);
405 $initiallastdate = get_pconfig($uid,'fromgplus','lastdate');
407 $first_time = ($initiallastdate == "");
411 if (!is_array($activities->items))
414 $reversed = array_reverse($activities->items);
416 foreach($reversed as $item) {
417 if (strtotime($item->published) <= $initiallastdate)
420 // Don't publish items that are too young
421 if (strtotime($item->published) > (time() - 3*60)) {
422 logger('fromgplus_fetch: item too new '.$item->published);
426 if ($lastdate < strtotime($item->published))
427 $lastdate = strtotime($item->published);
432 if ($item->access->description == "Public")
434 // Loop prevention - ignore postings from HootSuite
435 if ($item->provider->title == "HootSuite")
438 // Loop prevention through the special blank from the googleplus connector
439 if (strstr($item->object->content, $blank))
442 switch($item->object->objectType) {
444 $post = fromgplus_html2bbcode($item->object->content);
446 if (is_array($item->object->attachments))
447 $post .= fromgplus_handleattachments($a, $uid, $item, $item->object->content, false);
449 // geocode, placeName
450 if (isset($item->address))
451 $location = $item->address;
455 fromgplus_post($a, $uid, "Google+", $post, $location);
456 //fromgplus_post($a, $uid, $item->provider->title, $post, $location);
461 $post = fromgplus_html2bbcode($item->annotation)."\n";
463 if (!intval(get_config('system','old_share'))) {
464 $post .= "[share author='".str_replace("'", "'",$item->object->actor->displayName).
465 "' profile='".$item->object->actor->url.
466 "' avatar='".$item->object->actor->image->url.
467 "' link='".$item->object->url."']";
469 $post .= fromgplus_html2bbcode($item->object->content);
471 if (is_array($item->object->attachments))
472 $post .= "\n".trim(fromgplus_handleattachments($a, $uid, $item, $item->object->content, true));
476 $post .= fromgplus_html2bbcode("♲");
477 $post .= " [url=".$item->object->actor->url."]".$item->object->actor->displayName."[/url] \n";
478 $post .= fromgplus_html2bbcode($item->object->content);
480 if (is_array($item->object->attachments))
481 $post .= "\n".trim(fromgplus_handleattachments($a, $uid, $item, $item->object->content, true));
484 if (isset($item->address))
485 $location = $item->address;
489 fromgplus_post($a, $uid, "Google+", $post, $location);
490 //fromgplus_post($a, $uid, $item->provider->title, $post, $location);
495 set_pconfig($uid,'fromgplus','lastdate', $lastdate);