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 $enable_checked = (intval(get_pconfig(local_user(),'fromgplus','enable')) ? ' checked="checked"' : '');
34 $account = get_pconfig(local_user(),'fromgplus','account');
36 $s .= '<span id="settings_fromgplus_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_fromgplus_expanded\'); openClose(\'settings_fromgplus_inflated\');">';
37 $s .= '<h3>' . t('Google+ Mirroring').'</h3>';
39 $s .= '<div id="settings_fromgplus_expanded" class="settings-block" style="display: none;">';
40 $s .= '<span class="fakelink" onclick="openClose(\'settings_fromgplus_expanded\'); openClose(\'settings_fromgplus_inflated\');">';
41 $s .= '<h3>' . t('Google+ Mirroring').'</h3>';
44 $s .= '<div id="fromgplus-wrapper">';
46 $s .= '<label id="fromgplus-enable-label" for="fromgplus-enable">'.t('Enable Google+ Import').'</label>';
47 $s .= '<input id="fromgplus-enable" type="checkbox" name="fromgplus-enable" value="1"'.$enable_checked.' />';
48 $s .= '<div class="clear"></div>';
49 $s .= '<label id="fromgplus-label" for="fromgplus-account">'.t('Google Account ID').' </label>';
50 $s .= '<input id="fromgplus-account" type="text" name="fromgplus-account" value="'.$account.'" />';
51 $s .= '</div><div class="clear"></div>';
53 $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="fromgplus-submit" name="fromgplus-submit"
54 class="settings-submit" value="' . t('Save Settings') . '" /></div>';
60 function fromgplus_addon_settings_post(&$a,&$b) {
65 if($_POST['fromgplus-submit']) {
66 set_pconfig(local_user(),'fromgplus','account',trim($_POST['fromgplus-account']));
67 $enable = ((x($_POST,'fromgplus-enable')) ? intval($_POST['fromgplus-enable']) : 0);
68 set_pconfig(local_user(),'fromgplus','enable', $enable);
69 info( t('Google+ Import Settings saved.') . EOL);
73 function fromgplus_cron($a,$b) {
74 $last = get_config('fromgplus','last_poll');
76 $poll_interval = intval(get_config('fromgplus','poll_interval'));
78 $poll_interval = FROMGPLUS_DEFAULT_POLL_INTERVAL;
81 $next = $last + ($poll_interval * 60);
83 logger('fromgplus: poll intervall not reached');
88 logger('fromgplus: cron_start');
90 $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'fromgplus' AND `k` = 'enable' AND `v` = '1' ORDER BY RAND() ");
93 $account = get_pconfig($rr['uid'],'fromgplus','account');
95 logger('fromgplus: fetching for user '.$rr['uid']);
96 fromgplus_fetch($a, $rr['uid']);
101 logger('fromgplus: cron_end');
103 set_config('fromgplus','last_poll', time());
106 function fromgplus_post($a, $uid, $source, $body, $location) {
110 // Don't know what it is. Maybe some trash from the mobile client
111 $trash = html_entity_decode("", ENT_QUOTES, 'UTF-8');
112 $body = str_replace($trash, "", $body);
116 if (substr($body, 0, 3) == "[b]") {
117 $pos = strpos($body, "[/b]");
118 $title = substr($body, 3, $pos-3);
119 $body = trim(substr($body, $pos+4));
123 $_SESSION['authenticated'] = true;
124 $_SESSION['uid'] = $uid;
127 $_REQUEST['type'] = 'wall';
128 $_REQUEST['api_source'] = true;
130 $_REQUEST['profile_uid'] = $uid;
131 $_REQUEST['source'] = $source;
134 // $_REQUEST['parent']
135 // $_REQUEST['parent_uri']
137 $_REQUEST['title'] = $title;
138 $_REQUEST['body'] = $body;
139 $_REQUEST['location'] = $location;
141 if (($_REQUEST['title'] == "") AND ($_REQUEST['body'] == "")) {
142 logger('fromgplus: empty post for user '.$uid." ".print_r($_REQUEST, true));
146 require_once('mod/item.php');
147 //print_r($_REQUEST);
148 logger('fromgplus: posting for user '.$uid." ".print_r($_REQUEST, true));
150 logger('fromgplus: done for user '.$uid);
153 function fromgplus_html2bbcode($html) {
155 $bbcode = html_entity_decode($html, ENT_QUOTES, 'UTF-8');
157 $bbcode = str_ireplace(array("\n"), array(""), $bbcode);
158 $bbcode = str_ireplace(array("<b>", "</b>"), array("[b]", "[/b]"), $bbcode);
159 $bbcode = str_ireplace(array("<i>", "</i>"), array("[i]", "[/i]"), $bbcode);
160 $bbcode = str_ireplace(array("<s>", "</s>"), array("[s]", "[/s]"), $bbcode);
161 $bbcode = str_ireplace(array("<br />"), array("\n"), $bbcode);
162 $bbcode = str_ireplace(array("<br/>"), array("\n"), $bbcode);
163 $bbcode = str_ireplace(array("<br>"), array("\n"), $bbcode);
165 $bbcode = trim(strip_tags($bbcode));
169 function fromgplus_parse_query($var)
172 * Use this function to parse out the query array element from
173 * the output of parse_url().
175 $var = parse_url($var, PHP_URL_QUERY);
176 $var = html_entity_decode($var);
177 $var = explode('&', $var);
180 foreach($var as $val) {
181 $x = explode('=', $val);
184 unset($val, $x, $var);
188 function fromgplus_cleanupgoogleproxy($fullImage, $image) {
190 $preview = "/w".$fullImage->width."-h".$fullImage->height."/";
191 $preview2 = "/w".$fullImage->width."-h".$fullImage->height."-p/";
192 $fullImage = str_replace(array($preview, $preview2), array("/", "/"), $fullImage->url);
194 $preview = "/w".$image->width."-h".$image->height."/";
195 $preview2 = "/w".$image->width."-h".$image->height."-p/";
196 $image = str_replace(array($preview, $preview2), array("/", "/"), $image->url);
200 $queryvar = fromgplus_parse_query($fullImage);
201 if ($queryvar['url'] != "")
202 $cleaned["full"] = urldecode($queryvar['url']);
204 $cleaned["full"] = $fullImage;
205 if (@exif_imagetype($cleaned["full"]) == 0)
206 $cleaned["full"] = "";
208 $queryvar = fromgplus_parse_query($image);
209 if ($queryvar['url'] != "")
210 $cleaned["preview"] = urldecode($queryvar['url']);
212 $cleaned["preview"] = $image;
213 if (@exif_imagetype($cleaned["preview"]) == 0)
214 $cleaned["preview"] = "";
216 if ($cleaned["full"] == "") {
217 $cleaned["full"] = $cleaned["preview"];
218 $cleaned["preview"] = "";
221 if ($cleaned["full"] == $cleaned["preview"])
222 $cleaned["preview"] = "";
224 if ($cleaned["full"] == "")
225 if (@exif_imagetype($fullImage) != 0)
226 $cleaned["full"] = $fullImage;
228 if ($cleaned["full"] == "")
229 if (@exif_imagetype($image) != 0)
230 $cleaned["full"] = $fullImage;
235 function fromgplus_cleantext($text) {
237 // Don't know what it is. But it is added to the text.
238 $trash = html_entity_decode("", ENT_QUOTES, 'UTF-8');
240 $text = strip_tags($text);
241 $text = html_entity_decode($text);
243 $text = str_replace(array("\n", "\r", " ", $trash), array("", "", "", ""), $text);
247 function fromgplus_handleattachments($item, $displaytext) {
252 foreach ($item->object->attachments as $attachment) {
253 switch($attachment->objectType) {
255 $post .= "\n\n[bookmark=".$attachment->url."]".fromgplus_html2bbcode($attachment->displayName)."[/bookmark]\n";
257 /*$images = cleanupgoogleproxy($attachment->fullImage, $attachment->image);
258 if ($images["preview"] != "")
259 $post .= "\n[url=".$images["full"]."][img]".$images["preview"]."[/img][/url]\n";
260 elseif ($images["full"] != "")
261 $post .= "\n[img]".$images["full"]."[/img]\n";*/
266 $post .= "\n\n[class=type-link][bookmark=".$attachment->url."]".fromgplus_html2bbcode($attachment->displayName)."[/bookmark]\n";
268 $images = fromgplus_cleanupgoogleproxy($attachment->fullImage, $attachment->image);
269 //if ($images["preview"] != "")
270 // $post .= "\n[url=".$images["full"]."][img]".$images["preview"]."[/img][/url]\n";
271 //elseif ($images["full"] != "")
272 // $post .= "\n[img]".$images["full"]."[/img]\n";
273 if ($images["full"] != "")
274 $post .= "\n[img]".$images["full"]."[/img]";
276 //$post .= "[quote]".trim(fromgplus_html2bbcode($attachment->content))."[/quote]";
277 $quote = trim(fromgplus_html2bbcode($attachment->content));
279 $quote = "\n[quote]".$quote."[/quote]";
281 $quote .= "[/class]";
285 $images = fromgplus_cleanupgoogleproxy($attachment->fullImage, $attachment->image);
286 if ($images["preview"] != "")
287 $post .= "\n[url=".$images["full"]."][img]".$images["preview"]."[/img][/url]\n";
288 elseif ($images["full"] != "")
289 $post .= "\n[img]".$images["full"]."[/img]\n";
291 if (($attachment->displayName != "") AND (fromgplus_cleantext($attachment->displayName) != fromgplus_cleantext($displaytext)))
292 $post .= fromgplus_html2bbcode($attachment->displayName)."\n";
296 $post .= "\n\n[bookmark=".$attachment->url."]".fromgplus_html2bbcode($attachment->displayName)."[/bookmark]\n";
298 $images = fromgplus_cleanupgoogleproxy($attachment->fullImage, $attachment->image);
299 if ($images["preview"] != "")
300 $post .= "\n[url=".$images["full"]."][img]".$images["preview"]."[/img][/url]\n";
301 elseif ($images["full"] != "")
302 $post .= "\n[img]".$images["full"]."[/img]\n";
307 foreach($attachment->thumbnails as $thumb) {
308 $preview = "/w".$thumb->image->width."-h".$thumb->image->height."/";
309 $preview2 = "/w".$thumb->image->width."-h".$thumb->image->height."-p/";
310 $image = str_replace(array($preview, $preview2), array("/", "/"), $thumb->image->url);
312 $post .= "\n[url=".$thumb->url."][img]".$image."[/img][/url]\n";
316 $post .= "\n\n[bookmark=".$attachment->url."]".fromgplus_html2bbcode($attachment->displayName)."[/bookmark]\n";
319 // die($attachment->objectType);
322 return($post.$quote);
325 function fromgplus_fetch($a, $uid) {
328 // Special blank to identify postings from the googleplus connector
329 $blank = html_entity_decode(" ", ENT_QUOTES, 'UTF-8');
331 $account = get_pconfig($uid,'fromgplus','account');
332 $key = get_config('fromgplus','key');
334 $result = fetch_url("https://www.googleapis.com/plus/v1/people/".$account."/activities/public?alt=json&pp=1&key=".$key."&maxResults=".$maxfetch);
335 //$result = file_get_contents("google.txt");
336 //file_put_contents("google.txt", $result);
338 $activities = json_decode($result);
340 $initiallastdate = get_pconfig($uid,'fromgplus','lastdate');
342 $first_time = ($initiallastdate == "");
346 if (!is_array($activities->items))
349 $reversed = array_reverse($activities->items);
351 foreach($reversed as $item) {
352 if (strtotime($item->published) <= $initiallastdate)
355 if ($lastdate < strtotime($item->published))
356 $lastdate = strtotime($item->published);
361 if ($item->access->description == "Public")
363 // Loop prevention - ignore postings from HootSuite
364 if ($item->provider->title == "HootSuite")
367 // Loop prevention through the special blank from the googleplus connector
368 if (strstr($item->object->content, $blank))
371 switch($item->object->objectType) {
373 $post = fromgplus_html2bbcode($item->object->content);
375 if (is_array($item->object->attachments))
376 $post .= fromgplus_handleattachments($item, $item->object->content);
378 // geocode, placeName
379 if (isset($item->address))
380 $location = $item->address;
384 fromgplus_post($a, $uid, "Google+", $post, $location);
385 //fromgplus_post($a, $uid, $item->provider->title, $post, $location);
390 $post = fromgplus_html2bbcode($item->annotation)."\n";
392 if (!intval(get_config('system','old_share'))) {
393 $post .= "[share author='".str_replace("'", "'",$item->object->actor->displayName).
394 "' profile='".$item->object->actor->url.
395 "' avatar='".$item->object->actor->image->url.
396 "' link='".$item->object->url."']";
398 $post .= fromgplus_html2bbcode($item->object->content);
400 if (is_array($item->object->attachments))
401 $post .= "\n".trim(fromgplus_handleattachments($item, $item->object->content));
405 $post .= fromgplus_html2bbcode("♲");
406 $post .= " [url=".$item->object->actor->url."]".$item->object->actor->displayName."[/url] \n";
407 $post .= fromgplus_html2bbcode($item->object->content);
409 if (is_array($item->object->attachments))
410 $post .= "\n".trim(fromgplus_handleattachments($item, $item->object->content));
413 if (isset($item->address))
414 $location = $item->address;
418 fromgplus_post($a, $uid, "Google+", $post, $location);
419 //fromgplus_post($a, $uid, $item->provider->title, $post, $location);
424 set_pconfig($uid,'fromgplus','lastdate', $lastdate);
429 require_once("boot.php");
436 @include(".htconfig.php");
437 require_once("include/dba.php");
438 $db = new dba($db_host, $db_user, $db_pass, $db_data);
439 unset($db_host, $db_user, $db_pass, $db_data);
443 fromgplus_cron($a, $test);