Adding Presence to Your Website

You might have noticed the small green, yellow or gray icon next to my name in the about section on the right. It shows my XMPP status and my status message. This is done by including a small JavaScript snippet in the template of my blog:

<script type="text/javascript" 
  src="/js/presence.js?uid=stefan.reuter&nick=Stefan%20Reuter">
</script>

The presence.js script is in fact a PHP script that retrieves the XMPP presence from the presence plugin of my Openfire server:

<?php
ini_set('display_errors', false);

$uid = $_GET['uid'];
if (! preg_match('/^[A-Za-z0-9_\.-]+$/', $uid))
{
        echo "document.write('Invalid uid parameter.');";
        die;
}
if (isset($_GET['nick']))
{
        $nick = $_GET['nick'];
}
else
{
        $nick = $uid;
}
if (! preg_match('/^[A-Za-z0-9_\. -]+$/', $nick))
{
        echo "document.write('Invalid nick parameter.');";
        die;
}
$imgtag = "<img src=\"/status/".$uid."\"/>";
$url = "http://openfire:9090/plugins/presence/status?jid="
        .$uid."@reucon.com&type=text";
$text = rtrim(implode(file($url, FILE_SKIP_EMPTY_LINES)));
$text = str_replace("\n","",nl2br(htmlspecialchars($text)));
?>
document.write('<table class="xmpp-status"><tbody>');
document.write('<tr valign="center">');
document.write('<td><?php print $imgtag; ?></td>');
document.write('<td><?php print $nick; ?></td>');
document.write('</tr>');
<?php if ($text != 'null') { ?>
document.write('<tr>');
document.write('<td> </td>');
document.write('<td><?php print $text; ?></td>');
document.write('</tr>');
<?php } ?>
document.write('</tbody></table>');

References

Leave a Reply

Your email address will not be published. Required fields are marked *