]> git.mxchange.org Git - friendica-addons.git/blob - gpluspost/gpluspost.php
c8918e1cc73eb7632f6345407f938b98195a132a
[friendica-addons.git] / gpluspost / gpluspost.php
1 <?php
2
3 /**
4  * Name: G+ Post
5  * Description: Posts to a Google+ page with the help of Hootsuite
6  * Version: 0.1
7  * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
8  * Status: Unsupported
9  */
10
11 function gpluspost_install() {
12         register_hook('post_local',           'addon/gpluspost/gpluspost.php', 'gpluspost_post_local');
13         register_hook('notifier_normal',      'addon/gpluspost/gpluspost.php', 'gpluspost_send');
14         register_hook('jot_networks',         'addon/gpluspost/gpluspost.php', 'gpluspost_jot_nets');
15         register_hook('connector_settings',      'addon/gpluspost/gpluspost.php', 'gpluspost_settings');
16         register_hook('connector_settings_post', 'addon/gpluspost/gpluspost.php', 'gpluspost_settings_post');
17         register_hook('queue_predeliver', 'addon/gpluspost/gpluspost.php', 'gpluspost_queue_hook');
18 }
19
20
21 function gpluspost_uninstall() {
22         unregister_hook('post_local',       'addon/gpluspost/gpluspost.php', 'gpluspost_post_local');
23         unregister_hook('notifier_normal',  'addon/gpluspost/gpluspost.php', 'gpluspost_send');
24         unregister_hook('jot_networks',     'addon/gpluspost/gpluspost.php', 'gpluspost_jot_nets');
25         unregister_hook('connector_settings',      'addon/gpluspost/gpluspost.php', 'gpluspost_settings');
26         unregister_hook('connector_settings_post', 'addon/gpluspost/gpluspost.php', 'gpluspost_settings_post');
27         unregister_hook('queue_predeliver', 'addon/gpluspost/gpluspost.php', 'gpluspost_queue_hook');
28 }
29
30 function gpluspost_jot_nets(&$a,&$b) {
31         if(! local_user())
32                 return;
33
34         $post = get_pconfig(local_user(),'gpluspost','post');
35         if(intval($post) == 1) {
36                 $defpost = get_pconfig(local_user(),'gpluspost','post_by_default');
37                 $selected = ((intval($defpost) == 1) ? ' checked="checked" ' : '');
38                 $b .= '<div class="profile-jot-net"><input type="checkbox" name="gpluspost_enable"' . $selected . ' value="1" /> '
39                         . t('Post to Google+') . '</div>';
40     }
41 }
42
43 function gpluspost_nextscripts() {
44         $a = get_app();
45         return file_exists($a->get_basepath()."/addon/gpluspost/postToGooglePlus.php");
46 }
47
48 function gpluspost_settings(&$a,&$s) {
49
50         if(! local_user())
51                 return;
52
53         $result = q("SELECT `installed` FROM `addon` WHERE `name` = 'fromgplus' AND `installed`");
54         $fromgplus_enabled = count($result) > 0;
55
56         /* Add our stylesheet to the page so we can make our settings look nice */
57
58         $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->get_baseurl() . '/addon/gpluspost/gpluspost.css' . '" media="all" />' . "\r\n";
59
60         $enabled = get_pconfig(local_user(),'gpluspost','post');
61         $checked = (($enabled) ? ' checked="checked" ' : '');
62         $css = (($enabled) ? '' : '-disabled');
63
64         $def_enabled = get_pconfig(local_user(),'gpluspost','post_by_default');
65         $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
66
67         $noloop_enabled = get_pconfig(local_user(),'gpluspost','no_loop_prevention');
68         $noloop_checked = (($noloop_enabled) ? ' checked="checked" ' : '');
69
70         $skip_enabled = get_pconfig(local_user(),'gpluspost','skip_without_link');
71         $skip_checked = (($skip_enabled) ? ' checked="checked" ' : '');
72
73         $mirror_enable_checked = (intval(get_pconfig(local_user(),'fromgplus','enable')) ? ' checked="checked"' : '');
74         $mirror_account = get_pconfig(local_user(),'fromgplus','account');
75
76         $username = get_pconfig(local_user(), 'gpluspost', 'username');
77         $password = get_pconfig(local_user(), 'gpluspost', 'password');
78         $page = get_pconfig(local_user(), 'gpluspost', 'page');
79
80         if ($fromgplus_enabled)
81                 $title = "Google+ Export/Mirror";
82         else
83                 $title = "Google+ Export";
84
85         $s .= '<span id="settings_gpluspost_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_gpluspost_expanded\'); openClose(\'settings_gpluspost_inflated\');">';
86         $s .= '<img class="connector'.$css.'" src="images/googleplus.png" /><h3 class="connector">'. t($title).'</h3>';
87         $s .= '</span>';
88         $s .= '<div id="settings_gpluspost_expanded" class="settings-block" style="display: none;">';
89         $s .= '<span class="fakelink" onclick="openClose(\'settings_gpluspost_expanded\'); openClose(\'settings_gpluspost_inflated\');">';
90         $s .= '<img class="connector'.$css.'" src="images/googleplus.png" /><h3 class="connector">'. t($title).'</h3>';
91         $s .= '</span>';
92         $s .= '<div id="gpluspost-enable-wrapper">';
93
94         $s .= '<label id="gpluspost-enable-label" for="gpluspost-checkbox">' . t('Enable Google+ Post Plugin') . '</label>';
95         $s .= '<input id="gpluspost-checkbox" type="checkbox" name="gpluspost" value="1" ' . $checked . '/>';
96         $s .= '</div><div class="clear"></div>';
97
98         if (gpluspost_nextscripts()) {
99                 /*
100                 // To-Do: Option to check the credentials if requested
101                 if (($username != "") && ($password != "")) {
102                         require_once("addon/gpluspost/postToGooglePlus.php");
103                         $loginError = doConnectToGooglePlus2($username, $password);
104                         if ($loginError)
105                                 $s .= '<p>Login Error. Please enter the correct credentials.</p>';
106                 }*/
107
108                 $s .= '<div id="gpluspost-username-wrapper">';
109                 $s .= '<label id="gpluspost-username-label" for="gpluspost-username">' . t('Google+ username') . '</label>';
110                 $s .= '<input id="gpluspost-username" type="text" name="username" value="' . $username . '" />';
111                 $s .= '</div><div class="clear"></div>';
112
113                 $s .= '<div id="gpluspost-password-wrapper">';
114                 $s .= '<label id="gpluspost-password-label" for="gpluspost-password">' . t('Google+ password') . '</label>';
115                 $s .= '<input id="gpluspost-password" type="password" name="password" value="' . $password . '" />';
116                 $s .= '</div><div class="clear"></div>';
117
118                 $s .= '<div id="gpluspost-page-wrapper">';
119                 $s .= '<label id="gpluspost-page-label" for="gpluspost-page">' . t('Google+ page number') . '</label>';
120                 $s .= '<input id="gpluspost-page" type="text" name="page" value="' . $page . '" />';
121                 $s .= '</div><div class="clear"></div>';
122         }
123
124         $s .= '<div id="gpluspost-bydefault-wrapper">';
125         $s .= '<label id="gpluspost-bydefault-label" for="gpluspost-bydefault">' . t('Post to Google+ by default') . '</label>';
126         $s .= '<input id="gpluspost-bydefault" type="checkbox" name="gpluspost_bydefault" value="1" ' . $def_checked . '/>';
127         $s .= '</div><div class="clear"></div>';
128
129         $s .= '<div id="gpluspost-noloopprevention-wrapper">';
130         $s .= '<label id="gpluspost-noloopprevention-label" for="gpluspost-noloopprevention">' . t('Do not prevent posting loops') . '</label>';
131         $s .= '<input id="gpluspost-noloopprevention" type="checkbox" name="gpluspost_noloopprevention" value="1" ' . $noloop_checked . '/>';
132         $s .= '</div><div class="clear"></div>';
133
134         if (!gpluspost_nextscripts()) {
135                 $s .= '<div id="gpluspost-skipwithoutlink-wrapper">';
136                 $s .= '<label id="gpluspost-skipwithoutlink-label" for="gpluspost-skipwithoutlink">' . t('Skip messages without links') . '</label>';
137                 $s .= '<input id="gpluspost-skipwithoutlink" type="checkbox" name="gpluspost_skipwithoutlink" value="1" ' . $skip_checked . '/>';
138                 $s .= '</div><div class="clear"></div>';
139         }
140
141         if ($fromgplus_enabled) {
142                 $s .= '<div id="gpluspost-mirror-wrapper">';
143                 $s .= '<label id="gpluspost-mirror-label" for="gpluspost-mirror">'.t('Mirror all public posts').'</label>';
144                 $s .= '<input id="gpluspost-mirror" type="checkbox" name="fromgplus-enable" value="1"'.$mirror_enable_checked.' />';
145                 $s .= '</div><div class="clear"></div>';
146                 $s .= '<div id="gpluspost-mirroraccount-wrapper">';
147                 $s .= '<label id="gpluspost-account-label" for="gpluspost-account">'.t('Mirror Google Account ID').' </label>';
148                 $s .= '<input id="gpluspost-account" type="text" name="fromgplus-account" value="'.$mirror_account.'" />';
149                 $s .= '</div><div class="clear"></div>';
150         }
151
152         /* provide a submit button */
153
154         $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="gpluspost-submit" name="gpluspost-submit" class="settings-submit" value="' . t('Save Settings') . '" /></div>';
155
156         if (gpluspost_nextscripts()) {
157                 $s .= "<p>If the plugin doesn't work or if it stopped, please login to Google+, then unlock your account by following this <a href='https://www.google.com/accounts/UnlockCaptcha'>link</a>. ";
158                 $s .= 'At this page please click on "Continue". Then your posts should arrive in several minutes.</p>';
159         } else {
160                 $s .= '<p>Register an account at <a href="https://hootsuite.com">Hootsuite</a>, add your G+ page and add the feed-url there.<br />';
161                 $s .= 'Feed-url: '.$a->get_baseurl().'/gpluspost/'.urlencode($a->user["nickname"]).'</p>';
162         }
163         $s .= '</div>';
164 }
165
166 function gpluspost_settings_post(&$a,&$b) {
167
168         if(x($_POST,'gpluspost-submit')) {
169                 set_pconfig(local_user(),'gpluspost','post',intval($_POST['gpluspost']));
170                 set_pconfig(local_user(),'gpluspost','post_by_default',intval($_POST['gpluspost_bydefault']));
171                 set_pconfig(local_user(),'gpluspost','no_loop_prevention',intval($_POST['gpluspost_noloopprevention']));
172
173                 if (!gpluspost_nextscripts()) {
174                         set_pconfig(local_user(),'gpluspost','skip_without_link',intval($_POST['gpluspost_skipwithoutlink']));
175                 } else {
176                         set_pconfig(local_user(),'gpluspost','username',trim($_POST['username']));
177                         set_pconfig(local_user(),'gpluspost','password',trim($_POST['password']));
178                         set_pconfig(local_user(),'gpluspost','page',trim($_POST['page']));
179                 }
180
181                 $result = q("SELECT `installed` FROM `addon` WHERE `name` = 'fromgplus' AND `installed`");
182                 if (count($result) > 0) {
183                         set_pconfig(local_user(),'fromgplus','account',trim($_POST['fromgplus-account']));
184                         $enable = ((x($_POST,'fromgplus-enable')) ? intval($_POST['fromgplus-enable']) : 0);
185                         set_pconfig(local_user(),'fromgplus','enable', $enable);
186
187                         if (!$enable)
188                                 del_pconfig(local_user(),'fromgplus','lastdate');
189                 }
190         }
191 }
192
193 function gpluspost_post_local(&$a,&$b) {
194
195         if($b['edit'])
196                 return;
197
198         if((! local_user()) || (local_user() != $b['uid']))
199                 return;
200
201         if($b['private'] || $b['parent'])
202                 return;
203
204         $post   = intval(get_pconfig(local_user(),'gpluspost','post'));
205
206         $enable = (($post && x($_REQUEST,'gpluspost_enable')) ? intval($_REQUEST['gpluspost_enable']) : 0);
207
208         if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'gpluspost','post_by_default')))
209                 $enable = 1;
210
211         if(!$enable)
212                 return;
213
214         if(strlen($b['postopts']))
215                 $b['postopts'] .= ',';
216
217         $b['postopts'] .= 'gplus';
218 }
219
220 function gpluspost_send(&$a,&$b) {
221
222         logger('gpluspost_send: invoked for post '.$b['id']." ".$b['app']);
223
224         if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
225                 return;
226
227         if(! strstr($b['postopts'],'gplus'))
228                 return;
229
230         if($b['parent'] != $b['id'])
231                 return;
232
233         // if post comes from Google+ don't send it back
234         if (!get_pconfig($b["uid"],'gpluspost','no_loop_prevention') && (($b['app'] == "Google+") || ($b["extid"] == NETWORK_GPLUS)))
235                 return;
236
237         if (!gpluspost_nextscripts()) {
238                 // Posting via RSS-Feed and Hootsuite
239                 $itemlist = get_pconfig($b["uid"],'gpluspost','itemlist');
240                 $items = explode(",", $itemlist);
241
242                 $i = 0;
243                 $newitems = array($b['id']);
244                 foreach ($items AS $item)
245                         if ($i++ < 9)
246                                 $newitems[] = $item;
247
248                 $itemlist = implode(",", $newitems);
249
250                 logger('gpluspost_send: new itemlist: '.$itemlist." for uid ".$b["uid"]);
251
252                 set_pconfig($b["uid"],'gpluspost','itemlist', $itemlist);
253
254         } else {
255                 // Posting via NextScripts
256                 $username = get_pconfig($b['uid'],'gpluspost','username');
257                 $password = get_pconfig($b['uid'],'gpluspost','password');
258                 $page = get_pconfig($b['uid'],'gpluspost','page');
259
260                 $success = false;
261
262                 if($username && $password) {
263                         require_once("addon/gpluspost/postToGooglePlus.php");
264                         require_once("include/plaintext.php");
265
266                         $item = $b;
267
268                         // Markup for Google+
269                         if  ($item["title"] != "")
270                                 $item["title"] = "*".$item["title"]."*";
271
272                         $item["body"] = preg_replace("(\[b\](.*?)\[\/b\])ism",'*$1*',$item["body"]);
273                         $item["body"] = preg_replace("(\[i\](.*?)\[\/i\])ism",'_$1_',$item["body"]);
274                         $item["body"] = preg_replace("(\[s\](.*?)\[\/s\])ism",'-$1-',$item["body"]);
275
276                         $data = plaintext($a, $item, 0, false, 9);
277
278                         logger('gpluspost_send: data: '.print_r($data, true), LOGGER_DEBUG);
279
280                         $loginError = doConnectToGooglePlus2($username, $password);
281                         if (!$loginError) {
282                                 if ($data["url"] != "")
283                                         $lnk = doGetGoogleUrlInfo2($data["url"]);
284                                 elseif ($data["image"] != "")
285                                         $lnk = array('img'=>$data["image"]);
286                                 else
287                                         $lnk = "";
288
289                                 // Send a special blank to identify the post through the "fromgplus" addon
290                                 $blank = html_entity_decode("&#x00A0;", ENT_QUOTES, 'UTF-8');
291
292                                 doPostToGooglePlus2($data["text"].$blank, $lnk, $page);
293
294                                 $success = true;
295
296                                 logger('gpluspost_send: '.$b['uid'].' success', LOGGER_DEBUG);
297                         } else
298                                 logger('gpluspost_send: '.$b['uid'].' failed '.$loginError, LOGGER_DEBUG);
299
300                         if (!$success) {
301                                 logger('gpluspost_send: requeueing '.$b['uid'], LOGGER_DEBUG);
302
303                                 $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `self`", $b['uid']);
304                                 if (count($r))
305                                         $a->contact = $r[0]["id"];
306
307                                 $s = serialize(array('url' => $url, 'item' => $b['id'], 'post' => $data));
308                                 require_once('include/queue_fn.php');
309                                 add_to_queue($a->contact,NETWORK_GPLUS,$s);
310                                 notice(t('Google+ post failed. Queued for retry.').EOL);
311                         }
312                 } else
313                                 logger('gpluspost_send: '.$b['uid'].' missing username or password', LOGGER_DEBUG);
314         }
315
316 }
317
318 function gpluspost_queue_hook(&$a,&$b) {
319
320         $qi = q("SELECT * FROM `queue` WHERE `network` = '%s'",
321                 dbesc(NETWORK_GPLUS)
322         );
323         if(! count($qi))
324                 return;
325
326         require_once('include/queue_fn.php');
327
328         foreach($qi as $x) {
329                 if($x['network'] !== NETWORK_GPLUS)
330                         continue;
331
332                 logger('gpluspost_queue: run');
333
334                 $r = q("SELECT `user`.* FROM `user` LEFT JOIN `contact` on `contact`.`uid` = `user`.`uid`
335                         WHERE `contact`.`self` = 1 AND `contact`.`id` = %d LIMIT 1",
336                         intval($x['cid'])
337                 );
338                 if(! count($r))
339                         continue;
340
341                 $userdata = $r[0];
342
343                 //logger('gpluspost_queue: fetching userdata '.print_r($userdata, true));
344
345                 $username = get_pconfig($userdata['uid'],'gpluspost','username');
346                 $password = get_pconfig($userdata['uid'],'gpluspost','password');
347                 $page = get_pconfig($userdata['uid'],'gpluspost','page');
348
349                 $success = false;
350
351                 if($username && $password) {
352                         require_once("addon/gpluspost/postToGooglePlus.php");
353
354                         logger('gpluspost_queue: able to post for user '.$username);
355
356                         $z = unserialize($x['content']);
357
358                         $data = $z['post'];
359                         // $z['url']
360
361                         logger('gpluspost_send: data: '.print_r($data, true), LOGGER_DATA);
362
363                         $loginError = doConnectToGooglePlus2($username, $password);
364                         if (!$loginError) {
365                                 if ($data["url"] != "")
366                                         $lnk = doGetGoogleUrlInfo2($data["url"]);
367                                 elseif ($data["image"] != "")
368                                         $lnk = array('img'=>$data["image"]);
369                                 else
370                                         $lnk = "";
371
372                                 // Send a special blank to identify the post through the "fromgplus" addon
373                                 $blank = html_entity_decode("&#x00A0;", ENT_QUOTES, 'UTF-8');
374
375                                 doPostToGooglePlus2($data["text"].$blank, $lnk, $page);
376
377                                 logger('gpluspost_queue: send '.$userdata['uid'].' success', LOGGER_DEBUG);
378
379                                 $success = true;
380
381                                 remove_queue_item($x['id']);
382                         } else
383                                 logger('gpluspost_queue: send '.$userdata['uid'].' failed '.$loginError, LOGGER_DEBUG);
384                 } else
385                         logger('gpluspost_queue: send '.$userdata['uid'].' missing username or password', LOGGER_DEBUG);
386
387                 if (!$success) {
388                         logger('gpluspost_queue: delayed');
389                         update_queue_time($x['id']);
390                 }
391         }
392 }
393
394 function gpluspost_module() {}
395
396 function gpluspost_init() {
397         global $a, $_SERVER;
398
399         $uid = 0;
400
401         if (isset($a->argv[1])) {
402                 $uid = (int)$a->argv[1];
403                 if ($uid == 0) {
404                         $contacts = q("SELECT `username`, `uid` FROM `user` WHERE `nickname` = '%s' LIMIT 1", dbesc($a->argv[1]));
405                         if ($contacts) {
406                                 $uid = $contacts[0]["uid"];
407                                 $nick = $a->argv[1];
408                         }
409                 } else {
410                         $contacts = q("SELECT `username` FROM `user` WHERE `uid`=%d LIMIT 1", intval($uid));
411                         $nick = $uid;
412                 }
413         }
414
415         header("content-type: application/atom+xml");
416         echo '<?xml version="1.0" encoding="UTF-8"?>'."\n";
417         echo '<feed xmlns="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">'."\n";
418         echo "\t".'<title type="html"><![CDATA['.$a->config['sitename'].']]></title>'."\n";
419         if ($uid != 0) {
420                 echo "\t".'<subtitle type="html"><![CDATA['.$contacts[0]["username"]."]]></subtitle>\n";
421                 echo "\t".'<link rel="self" href="'.$a->get_baseurl().'/gpluspost/'.$nick.'"/>'."\n";
422         } else
423                 echo "\t".'<link rel="self" href="'.$a->get_baseurl().'/gpluspost"/>'."\n";
424         echo "\t<id>".$a->get_baseurl()."/</id>\n";
425         echo "\t".'<link rel="alternate" type="text/html" href="'.$a->get_baseurl().'"/>'."\n";
426         echo "\t<updated>".date("c")."</updated>\n"; // To-Do
427         // <rights>Copyright ... </rights>
428         echo "\t".'<generator uri="'.$a->get_baseurl().'">'.$a->config['sitename'].'</generator>'."\n";
429
430         if ($uid != 0) {
431                 $itemlist = get_pconfig($uid,'gpluspost','itemlist');
432                 $items = explode(",", $itemlist);
433
434                 foreach ($items AS $item)
435                         gpluspost_feeditem($item, $uid);
436         } else {
437                 $items = q("SELECT `id` FROM `item` WHERE `item`.`visible` = 1 AND `item`.`deleted` = 0 and `item`.`moderated` = 0 AND `item`.`allow_cid` = ''  AND `item`.`allow_gid` = '' AND `item`.`deny_cid`  = '' AND `item`.`deny_gid`  = '' AND `item`.`private` = 0 AND `item`.`wall` = 1 AND `item`.`id` = `item`.`parent` ORDER BY `received` DESC LIMIT 10");
438                 foreach ($items AS $item)
439                         gpluspost_feeditem($item["id"], $uid);
440         }
441         echo "</feed>\n";
442         killme();
443 }
444
445 function gpluspost_feeditem($pid, $uid) {
446         global $a;
447
448         require_once('include/api.php');
449         require_once('include/bbcode.php');
450         require_once("include/html2plain.php");
451         require_once("include/network.php");
452
453         $skipwithoutlink = get_pconfig($uid,'gpluspost','skip_without_link');
454
455         $items = q("SELECT `uri`, `plink`, `author-link`, `author-name`, `created`, `edited`, `id`, `title`, `body` from `item` WHERE id=%d", intval($pid));
456         foreach ($items AS $item) {
457
458                 $item['body'] = bb_CleanPictureLinks($item['body']);
459
460                 $item['body'] = bb_remove_share_information($item['body'], true);
461
462                 if ($item["title"] != "")
463                         $item['body'] = "*".$item["title"]."*\n\n".$item['body'];
464
465                 // Looking for the first image
466                 $image = '';
467                 if(preg_match("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/is",$item['body'],$matches))
468                         $image = $matches[3];
469
470                 if ($image == '')
471                         if(preg_match("/\[img\](.*?)\[\/img\]/is",$item['body'],$matches))
472                                 $image = $matches[1];
473
474                 $multipleimages = (strpos($item['body'], "[img") != strrpos($item['body'], "[img"));
475
476                 // When saved into the database the content is sent through htmlspecialchars
477                 // That means that we have to decode all image-urls
478                 $image = htmlspecialchars_decode($image);
479
480                 $link = '';
481                 // look for bookmark-bbcode and handle it with priority
482                 if(preg_match("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/is",$item['body'],$matches))
483                         $link = $matches[1];
484
485                 $multiplelinks = (strpos($item['body'], "[bookmark") != strrpos($item['body'], "[bookmark"));
486
487                 $body = $item['body'];
488
489                 $body = preg_replace("(\[b\](.*?)\[\/b\])ism",'*$1*',$body);
490                 $body = preg_replace("(\[i\](.*?)\[\/i\])ism",'_$1_',$body);
491                 $body = preg_replace("(\[s\](.*?)\[\/s\])ism",'-$1-',$body);
492
493                 // At first convert the text to html
494                 $html = bbcode(api_clean_plain_items($body), false, false, 2);
495
496                 // Then convert it to plain text
497                 $msg = trim(html2plain($html, 0, true));
498                 $msg = html_entity_decode($msg,ENT_QUOTES,'UTF-8');
499
500                 // If there is no bookmark element then take the first link
501                 if ($link == '') {
502                         $links = collecturls($html);
503                         if (sizeof($links) > 0) {
504                                 reset($links);
505                                 $link = current($links);
506                         }
507                         $multiplelinks = (sizeof($links) > 1);
508
509                         if ($multiplelinks) {
510                                 $html2 = bbcode($msg, false, false);
511                                 $links2 = collecturls($html2);
512                                 if (sizeof($links2) > 0) {
513                                         reset($links2);
514                                         $link = current($links2);
515                                         $multiplelinks = (sizeof($links2) > 1);
516                                 }
517                         }
518                 }
519
520                 $msglink = "";
521                 if ($multiplelinks)
522                         $msglink = $item["plink"];
523                 else if ($link != "")
524                         $msglink = $link;
525                 else if ($multipleimages)
526                         $msglink = $item["plink"];
527                 else if ($image != "")
528                         $msglink = $image;
529
530                 if (($msglink == "") && $skipwithoutlink)
531                         continue;
532                 else if ($msglink == "")
533                         $msglink = $item["plink"];
534
535                 // Fetching the title - or the first line
536                 if ($item["title"] != "")
537                         $title = $item["title"];
538                 else {
539                         $lines = explode("\n", $msg);
540                         $title = $lines[0];
541                 }
542
543                 //if ($image != $msglink)
544                 //      $html = trim(str_replace($msglink, "", $html));
545
546                 $title = trim(str_replace($msglink, "", $title));
547
548                 $msglink = original_url($msglink);
549
550                 if ($uid == 0)
551                         $title = $item["author-name"].": ".$title;
552
553                 $msglink = htmlspecialchars(html_entity_decode($msglink));
554
555                 if (strpos($msg, $msglink) == 0)
556                         $msg .= "\n".$msglink;
557
558                 $msg = nl2br($msg);
559
560                 $title = str_replace("&", "&amp;", $title);
561                 //$html = str_replace("&", "&amp;", $html);
562
563                 echo "\t".'<entry xmlns="http://www.w3.org/2005/Atom">'."\n";
564                 echo "\t\t".'<title type="html" xml:space="preserve"><![CDATA['.$title."]]></title>\n";
565                 echo "\t\t".'<link rel="alternate" type="text/html" href="'.$msglink.'" />'."\n";
566                 // <link rel="enclosure" type="audio/mpeg" length="1337" href="http://example.org/audio/ph34r_my_podcast.mp3"/>
567                 echo "\t\t<id>".$item["uri"]."</id>\n";
568                 echo "\t\t<updated>".date("c", strtotime($item["edited"]))."</updated>\n";
569                 echo "\t\t<published>".date("c", strtotime($item["created"]))."</published>\n";
570                 echo "\t\t<author>\n\t\t\t<name><![CDATA[".$item["author-name"]."]]></name>\n";
571                 echo "\t\t\t<uri>".$item["author-link"]."</uri>\n\t\t</author>\n";
572                 //echo '<content type="image/png" src="http://media.example.org/the_beach.png"/>';
573                 echo "\t\t".'<content type="html" xml:space="preserve" xml:base="'.$item["plink"].'"><![CDATA['.$msg."]]></content>\n";
574                 echo "\t</entry>\n";
575         }
576 }