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 logger('fromgplus: posting for user '.$uid);
129 require_once('mod/item.php');
130 //print_r($_REQUEST);
134 function fromgplus_html2bbcode($html) {
136 $bbcode = html_entity_decode($html, ENT_QUOTES, 'UTF-8');
138 $bbcode = str_ireplace(array("\n"), array(""), $bbcode);
139 $bbcode = str_ireplace(array("<b>", "</b>"), array("[b]", "[/b]"), $bbcode);
140 $bbcode = str_ireplace(array("<i>", "</i>"), array("[i]", "[/i]"), $bbcode);
141 $bbcode = str_ireplace(array("<s>", "</s>"), array("[s]", "[/s]"), $bbcode);
142 $bbcode = str_ireplace(array("<br />"), array("\n"), $bbcode);
143 $bbcode = str_ireplace(array("<br/>"), array("\n"), $bbcode);
144 $bbcode = str_ireplace(array("<br>"), array("\n"), $bbcode);
146 $bbcode = trim(strip_tags($bbcode));
150 function fromgplus_parse_query($var)
153 * Use this function to parse out the query array element from
154 * the output of parse_url().
156 $var = parse_url($var, PHP_URL_QUERY);
157 $var = html_entity_decode($var);
158 $var = explode('&', $var);
161 foreach($var as $val) {
162 $x = explode('=', $val);
165 unset($val, $x, $var);
169 function fromgplus_cleanupgoogleproxy($fullImage, $image) {
171 $preview = "/w".$fullImage->width."-h".$fullImage->height."/";
172 $preview2 = "/w".$fullImage->width."-h".$fullImage->height."-p/";
173 $fullImage = str_replace(array($preview, $preview2), array("/", "/"), $fullImage->url);
175 $preview = "/w".$image->width."-h".$image->height."/";
176 $preview2 = "/w".$image->width."-h".$image->height."-p/";
177 $image = str_replace(array($preview, $preview2), array("/", "/"), $image->url);
181 $queryvar = fromgplus_parse_query($fullImage);
182 if ($queryvar['url'] != "")
183 $cleaned["full"] = urldecode($queryvar['url']);
185 $cleaned["full"] = $fullImage;
186 if (@exif_imagetype($cleaned["full"]) == 0)
187 $cleaned["full"] = "";
189 $queryvar = fromgplus_parse_query($image);
190 if ($queryvar['url'] != "")
191 $cleaned["preview"] = urldecode($queryvar['url']);
193 $cleaned["preview"] = $image;
194 if (@exif_imagetype($cleaned["preview"]) == 0)
195 $cleaned["preview"] = "";
197 if ($cleaned["full"] == "") {
198 $cleaned["full"] = $cleaned["preview"];
199 $cleaned["preview"] = "";
202 if ($cleaned["full"] == $cleaned["preview"])
203 $cleaned["preview"] = "";
205 if ($cleaned["full"] == "")
206 if (@exif_imagetype($fullImage) != 0)
207 $cleaned["full"] = $fullImage;
209 if ($cleaned["full"] == "")
210 if (@exif_imagetype($image) != 0)
211 $cleaned["full"] = $fullImage;
216 function fromgplus_handleattachments($item, $displaytext) {
220 foreach ($item->object->attachments as $attachment) {
221 switch($attachment->objectType) {
223 $post .= "\n\n[bookmark=".$attachment->url."]".fromgplus_html2bbcode($attachment->displayName)."[/bookmark]\n";
225 /*$images = cleanupgoogleproxy($attachment->fullImage, $attachment->image);
226 if ($images["preview"] != "")
227 $post .= "\n[url=".$images["full"]."][img]".$images["preview"]."[/img][/url]\n";
228 elseif ($images["full"] != "")
229 $post .= "\n[img]".$images["full"]."[/img]\n";*/
234 $post .= "\n\n[bookmark=".$attachment->url."]".fromgplus_html2bbcode($attachment->displayName)."[/bookmark]\n";
236 $images = fromgplus_cleanupgoogleproxy($attachment->fullImage, $attachment->image);
237 if ($images["preview"] != "")
238 $post .= "\n[url=".$images["full"]."][img]".$images["preview"]."[/img][/url]\n";
239 elseif ($images["full"] != "")
240 $post .= "\n[img]".$images["full"]."[/img]\n";
242 //$post .= "[quote]".trim(fromgplus_html2bbcode($attachment->content))."[/quote]";
243 $quote = trim(fromgplus_html2bbcode($attachment->content));
245 $quote = "\n[quote]".$quote."[/quote]";
249 $images = fromgplus_cleanupgoogleproxy($attachment->fullImage, $attachment->image);
250 if ($images["preview"] != "")
251 $post .= "\n[url=".$images["full"]."][img]".$images["preview"]."[/img][/url]\n";
252 elseif ($images["full"] != "")
253 $post .= "\n[img]".$images["full"]."[/img]\n";
255 if (($attachment->displayName != "") AND ($attachment->displayName != $displaytext))
256 $post .= fromgplus_html2bbcode($attachment->displayName)."\n";
260 $post .= "\n\n[bookmark=".$attachment->url."]".fromgplus_html2bbcode($attachment->displayName)."[/bookmark]\n";
262 $images = fromgplus_cleanupgoogleproxy($attachment->fullImage, $attachment->image);
263 if ($images["preview"] != "")
264 $post .= "\n[url=".$images["full"]."][img]".$images["preview"]."[/img][/url]\n";
265 elseif ($images["full"] != "")
266 $post .= "\n[img]".$images["full"]."[/img]\n";
271 foreach($attachment->thumbnails as $thumb) {
272 $preview = "/w".$thumb->image->width."-h".$thumb->image->height."/";
273 $preview2 = "/w".$thumb->image->width."-h".$thumb->image->height."-p/";
274 $image = str_replace(array($preview, $preview2), array("/", "/"), $thumb->image->url);
276 $post .= "\n[url=".$thumb->url."][img]".$image."[/img][/url]\n";
280 $post .= "\n\n[bookmark=".$attachment->url."]".fromgplus_html2bbcode($attachment->displayName)."[/bookmark]\n";
283 // die($attachment->objectType);
286 return($post.$quote);
289 function fromgplus_fetch($a, $uid) {
292 $account = get_pconfig($uid,'fromgplus','account');
293 $key = get_config('fromgplus','key');
295 $result = fetch_url("https://www.googleapis.com/plus/v1/people/".$account."/activities/public?alt=json&pp=1&key=".$key."&maxResults=".$maxfetch);
296 //$result = file_get_contents("google.txt");
297 //file_put_contents("google.txt", $result);
299 $activities = json_decode($result);
301 $initiallastdate = get_pconfig($uid,'fromgplus','lastdate');
305 if (!is_array($activities->items))
308 $reversed = array_reverse($activities->items);
310 foreach($reversed as $item) {
311 if (strtotime($item->published) <= $initiallastdate)
314 if ($lastdate < strtotime($item->published))
315 $lastdate = strtotime($item->published);
317 if ($item->access->description == "Public")
318 switch($item->object->objectType) {
320 $post = fromgplus_html2bbcode($item->object->content);
322 if (is_array($item->object->attachments))
323 $post .= fromgplus_handleattachments($item, $item->object->content);
325 // geocode, placeName
326 if (isset($item->address))
327 $location = $item->address;
331 // Loop prevention - should be made better
332 if ($item->provider->title != "HootSuite")
333 fromgplus_post($a, $uid, "Google+", $post, $location);
334 //fromgplus_post($a, $uid, $item->provider->title, $post, $location);
339 $post = fromgplus_html2bbcode($item->annotation)."\n";
341 if (intval(get_config('system','new_share'))) {
342 $post .= "[share author='".str_replace("'", "'",$item->object->actor->displayName).
343 "' profile='".$item->object->actor->url.
344 "' avatar='".$item->object->actor->image->url.
345 "' link='".$item->object->url."']";
347 $post .= fromgplus_html2bbcode($item->object->content);
349 if (is_array($item->object->attachments))
350 $post .= "\n".trim(fromgplus_handleattachments($item, $item->object->content));
354 $post .= fromgplus_html2bbcode("♲");
355 $post .= " [url=".$item->object->actor->url."]".$item->object->actor->displayName."[/url] \n";
356 $post .= fromgplus_html2bbcode($item->object->content);
358 if (is_array($item->object->attachments))
359 $post .= "\n".trim(fromgplus_handleattachments($item, $item->object->content));
362 if (isset($item->address))
363 $location = $item->address;
367 // Loop prevention - should be made better
368 if ($item->provider->title != "HootSuite")
369 fromgplus_post($a, $uid, "Google+", $post, $location);
370 //fromgplus_post($a, $uid, $item->provider->title, $post, $location);
375 set_pconfig($uid,'fromgplus','lastdate', $lastdate);
380 require_once("boot.php");
387 @include(".htconfig.php");
388 require_once("include/dba.php");
389 $db = new dba($db_host, $db_user, $db_pass, $db_data);
390 unset($db_host, $db_user, $db_pass, $db_data);
394 fromgplus_cron($a, $test);