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;
113 $_REQUEST['type'] = 'wall';
114 $_REQUEST['api_source'] = true;
116 $_REQUEST['profile_uid'] = $uid;
117 $_REQUEST['source'] = $source;
120 // $_REQUEST['parent']
121 // $_REQUEST['parent_uri']
123 $_REQUEST['title'] = $title;
124 $_REQUEST['body'] = $body;
125 $_REQUEST['location'] = $location;
127 if (($_REQUEST['title'] == "") AND ($_REQUEST['body'] == "")) {
128 logger('fromgplus: empty post for user '.$uid." ".print_r($_REQUEST, true));
132 require_once('mod/item.php');
133 //print_r($_REQUEST);
134 logger('fromgplus: posting for user '.$uid." ".print_r($_REQUEST, true));
136 logger('fromgplus: done for user '.$uid);
139 function fromgplus_html2bbcode($html) {
141 $bbcode = html_entity_decode($html, ENT_QUOTES, 'UTF-8');
143 $bbcode = str_ireplace(array("\n"), array(""), $bbcode);
144 $bbcode = str_ireplace(array("<b>", "</b>"), array("[b]", "[/b]"), $bbcode);
145 $bbcode = str_ireplace(array("<i>", "</i>"), array("[i]", "[/i]"), $bbcode);
146 $bbcode = str_ireplace(array("<s>", "</s>"), array("[s]", "[/s]"), $bbcode);
147 $bbcode = str_ireplace(array("<br />"), array("\n"), $bbcode);
148 $bbcode = str_ireplace(array("<br/>"), array("\n"), $bbcode);
149 $bbcode = str_ireplace(array("<br>"), array("\n"), $bbcode);
151 $bbcode = trim(strip_tags($bbcode));
155 function fromgplus_parse_query($var)
158 * Use this function to parse out the query array element from
159 * the output of parse_url().
161 $var = parse_url($var, PHP_URL_QUERY);
162 $var = html_entity_decode($var);
163 $var = explode('&', $var);
166 foreach($var as $val) {
167 $x = explode('=', $val);
170 unset($val, $x, $var);
174 function fromgplus_cleanupgoogleproxy($fullImage, $image) {
176 $preview = "/w".$fullImage->width."-h".$fullImage->height."/";
177 $preview2 = "/w".$fullImage->width."-h".$fullImage->height."-p/";
178 $fullImage = str_replace(array($preview, $preview2), array("/", "/"), $fullImage->url);
180 $preview = "/w".$image->width."-h".$image->height."/";
181 $preview2 = "/w".$image->width."-h".$image->height."-p/";
182 $image = str_replace(array($preview, $preview2), array("/", "/"), $image->url);
186 $queryvar = fromgplus_parse_query($fullImage);
187 if ($queryvar['url'] != "")
188 $cleaned["full"] = urldecode($queryvar['url']);
190 $cleaned["full"] = $fullImage;
191 if (@exif_imagetype($cleaned["full"]) == 0)
192 $cleaned["full"] = "";
194 $queryvar = fromgplus_parse_query($image);
195 if ($queryvar['url'] != "")
196 $cleaned["preview"] = urldecode($queryvar['url']);
198 $cleaned["preview"] = $image;
199 if (@exif_imagetype($cleaned["preview"]) == 0)
200 $cleaned["preview"] = "";
202 if ($cleaned["full"] == "") {
203 $cleaned["full"] = $cleaned["preview"];
204 $cleaned["preview"] = "";
207 if ($cleaned["full"] == $cleaned["preview"])
208 $cleaned["preview"] = "";
210 if ($cleaned["full"] == "")
211 if (@exif_imagetype($fullImage) != 0)
212 $cleaned["full"] = $fullImage;
214 if ($cleaned["full"] == "")
215 if (@exif_imagetype($image) != 0)
216 $cleaned["full"] = $fullImage;
221 function fromgplus_handleattachments($item, $displaytext) {
225 foreach ($item->object->attachments as $attachment) {
226 switch($attachment->objectType) {
228 $post .= "\n\n[bookmark=".$attachment->url."]".fromgplus_html2bbcode($attachment->displayName)."[/bookmark]\n";
230 /*$images = cleanupgoogleproxy($attachment->fullImage, $attachment->image);
231 if ($images["preview"] != "")
232 $post .= "\n[url=".$images["full"]."][img]".$images["preview"]."[/img][/url]\n";
233 elseif ($images["full"] != "")
234 $post .= "\n[img]".$images["full"]."[/img]\n";*/
239 $post .= "\n\n[bookmark=".$attachment->url."]".fromgplus_html2bbcode($attachment->displayName)."[/bookmark]\n";
241 $images = fromgplus_cleanupgoogleproxy($attachment->fullImage, $attachment->image);
242 if ($images["preview"] != "")
243 $post .= "\n[url=".$images["full"]."][img]".$images["preview"]."[/img][/url]\n";
244 elseif ($images["full"] != "")
245 $post .= "\n[img]".$images["full"]."[/img]\n";
247 //$post .= "[quote]".trim(fromgplus_html2bbcode($attachment->content))."[/quote]";
248 $quote = trim(fromgplus_html2bbcode($attachment->content));
250 $quote = "\n[quote]".$quote."[/quote]";
254 $images = fromgplus_cleanupgoogleproxy($attachment->fullImage, $attachment->image);
255 if ($images["preview"] != "")
256 $post .= "\n[url=".$images["full"]."][img]".$images["preview"]."[/img][/url]\n";
257 elseif ($images["full"] != "")
258 $post .= "\n[img]".$images["full"]."[/img]\n";
260 if (($attachment->displayName != "") AND ($attachment->displayName != $displaytext))
261 $post .= fromgplus_html2bbcode($attachment->displayName)."\n";
265 $post .= "\n\n[bookmark=".$attachment->url."]".fromgplus_html2bbcode($attachment->displayName)."[/bookmark]\n";
267 $images = fromgplus_cleanupgoogleproxy($attachment->fullImage, $attachment->image);
268 if ($images["preview"] != "")
269 $post .= "\n[url=".$images["full"]."][img]".$images["preview"]."[/img][/url]\n";
270 elseif ($images["full"] != "")
271 $post .= "\n[img]".$images["full"]."[/img]\n";
276 foreach($attachment->thumbnails as $thumb) {
277 $preview = "/w".$thumb->image->width."-h".$thumb->image->height."/";
278 $preview2 = "/w".$thumb->image->width."-h".$thumb->image->height."-p/";
279 $image = str_replace(array($preview, $preview2), array("/", "/"), $thumb->image->url);
281 $post .= "\n[url=".$thumb->url."][img]".$image."[/img][/url]\n";
285 $post .= "\n\n[bookmark=".$attachment->url."]".fromgplus_html2bbcode($attachment->displayName)."[/bookmark]\n";
288 // die($attachment->objectType);
291 return($post.$quote);
294 function fromgplus_fetch($a, $uid) {
297 $account = get_pconfig($uid,'fromgplus','account');
298 $key = get_config('fromgplus','key');
300 $result = fetch_url("https://www.googleapis.com/plus/v1/people/".$account."/activities/public?alt=json&pp=1&key=".$key."&maxResults=".$maxfetch);
301 //$result = file_get_contents("google.txt");
302 //file_put_contents("google.txt", $result);
304 $activities = json_decode($result);
306 $initiallastdate = get_pconfig($uid,'fromgplus','lastdate');
310 if (!is_array($activities->items))
313 $reversed = array_reverse($activities->items);
315 foreach($reversed as $item) {
316 if (strtotime($item->published) <= $initiallastdate)
319 if ($lastdate < strtotime($item->published))
320 $lastdate = strtotime($item->published);
322 if ($item->access->description == "Public")
323 switch($item->object->objectType) {
325 $post = fromgplus_html2bbcode($item->object->content);
327 if (is_array($item->object->attachments))
328 $post .= fromgplus_handleattachments($item, $item->object->content);
330 // geocode, placeName
331 if (isset($item->address))
332 $location = $item->address;
336 // Loop prevention - should be made better
337 if ($item->provider->title != "HootSuite")
338 fromgplus_post($a, $uid, "Google+", $post, $location);
339 //fromgplus_post($a, $uid, $item->provider->title, $post, $location);
344 $post = fromgplus_html2bbcode($item->annotation)."\n";
346 if (intval(get_config('system','new_share'))) {
347 $post .= "[share author='".str_replace("'", "'",$item->object->actor->displayName).
348 "' profile='".$item->object->actor->url.
349 "' avatar='".$item->object->actor->image->url.
350 "' link='".$item->object->url."']";
352 $post .= fromgplus_html2bbcode($item->object->content);
354 if (is_array($item->object->attachments))
355 $post .= "\n".trim(fromgplus_handleattachments($item, $item->object->content));
359 $post .= fromgplus_html2bbcode("♲");
360 $post .= " [url=".$item->object->actor->url."]".$item->object->actor->displayName."[/url] \n";
361 $post .= fromgplus_html2bbcode($item->object->content);
363 if (is_array($item->object->attachments))
364 $post .= "\n".trim(fromgplus_handleattachments($item, $item->object->content));
367 if (isset($item->address))
368 $location = $item->address;
372 // Loop prevention - should be made better
373 if ($item->provider->title != "HootSuite")
374 fromgplus_post($a, $uid, "Google+", $post, $location);
375 //fromgplus_post($a, $uid, $item->provider->title, $post, $location);
380 set_pconfig($uid,'fromgplus','lastdate', $lastdate);
385 require_once("boot.php");
392 @include(".htconfig.php");
393 require_once("include/dba.php");
394 $db = new dba($db_host, $db_user, $db_pass, $db_data);
395 unset($db_host, $db_user, $db_pass, $db_data);
399 fromgplus_cron($a, $test);