<?php
/*
* ###########
* #__________#
* __________#
* ________#
* _____###_____²xiT development
* _________#
* ___________#
* #__________#
* _#________#
* __#______#
* ____####
*
* @version 1.20
* @author Joris Berthelot <hide@address.com>
* @copyright Copyright (c) 2009, Joris Berthelot
* @license http://www.opensource.org/licenses/mit-license.php MIT Licence
*/
class JabbimStatus
{
// Jabber ID
const JID = '@jabbim.com';
// Status provider URL (%uid% will be replaced by JID constant)
const SURL = 'http://netlab.cz/status/?jid=%uid%&ib=bulb&type=text';
// Your Website URL
const URL = 'http://(www.)foobar.tld/';
// Paragraphe DOM ID (don't forget spaces around)
const DOMID = ' id="jabber" ';
// Images config
const IMG_PATH = 'img/';
const IMG_EXT = '.png';
const IMG_WIDTH = 120;
const IMG_HEIGHT = 20;
const IMG_FONT = './Trebuchet_MS.ttf';
// Default AWAY array key
// Needed for all custom AWAY statuses
// Also depending of your Jabber client language
const AWAY_KEY = 'parti(e)';
// Statuses will go here
private $_curl_info;
private $_result;
private $_status;
private $_extraStatus;
// Sets image names
public $statusIcons = array(
'online' => 'status_online',
'chatty' => 'status_online',
'dnd' => 'status_busy',
'away' => 'status_away',
'xa' => 'status_away',
'offline' => 'status_offline',
'timeout' => 'status_offline'
);
// Sets translations from French to English + manages custom status
public $statusTexts = array(
'online' => array(null => 'Online'),
'chatty' => array(null => 'Free to chat'),
'away' => array(
null => 'Invisible',
'parti(e)' => 'Away',
'au téléphone' => 'On the phone',
'parti(e) manger' => 'Out to lunch',
'de retour de suite' => 'Be right back',
'inactif' => 'Inactive'
),
'xa' => array(null => 'Away for a while'),
'dnd' => array(
null => 'Do not disturb',
'code' => 'Coding...'
),
'offline' => array(null => 'Offline'),
'timeout' => array(null => '(timeout)')
);
public function __construct()
{
$this->_getStatus();
}
public function render() {
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
switch (array_shift(@array_keys($_GET))) {
case 'img' :
header('Content-Type: image/png');
echo $this->showIconStatus();
break;
case 'full' :
header('Content-Type: image/png');
$this->showFullImgStatus();
break;
case 'text' :
header('Content-Type: text/plain');
echo $this->showTextStatus();
break;
case 'debug' :
header('Content-Type: text/plain');
$this->showDebugStatus();
break;
default :
header('Content-Type: text/html');
echo $this->showFullHtml();
}
}
// Returns information about request + request result
public function showDebugStatus()
{
echo 'Request connexion information:' . "\n";
var_dump($this->_curl_info);
echo "\n" . 'Request original result:' . "\n";
var_dump($this->_result);
}
// Returns the status icon source code
public function showIconStatus()
{
return @file_get_contents($this->_getIconPath());
}
// Outputs the status text
public function showTextStatus()
{
return $this->_getTextStatus();
}
// Generates and outputs status + text as an image
public function showFullImgStatus()
{
$img = imagecreate(self::IMG_WIDTH, self::IMG_HEIGHT) or exit;
$status = $this->_getTextStatus(false);
$icon = imagecreatefrompng($this->_getIconPath());
// Enables transparency
imagealphablending($icon, false);
imagesavealpha($icon, true);
imagecolortransparent($img, imagecolorallocate($img, 255, 255, 255));
// Pastes status icon on the final image
imagecopy($img, $icon, 2, 2, 0, 0, 16, 16);
// Writes the status text
imagefttext($img, 10, 0, 20, 16,
imagecolorallocate($img, 0, 0, 0),
self::IMG_FONT,
$status
);
// Generates the final image
imagepng($img);
imagedestroy($img);
}
public function showFullHtml()
{
return '<p'
. self::DOMID
. '><img src="'
. self::URL
. 'jabber.png" '
. 'alt="status" width="16" height="16" title="'
. $this->_getTextStatus()
. '" /> '
. $this->_getTextStatus()
. '</p>';
}
private function _getStatus()
{
// Opens a connection to get official status
$con = curl_init();
// Sets some connection parameters
curl_setopt_array($con, array(
CURLOPT_HEADER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 3,
CURLOPT_URL => str_replace(
'%uid%', self::JID, self::SURL)
));
// Unable to connect
if (!$this->_result = curl_exec($con)) {
$this->_errorMod();
return;
}
// Saves connexion information for debug output
$this->_curl_info = curl_getinfo($con);
// Closes the connection
curl_close($con);
// Gets the status
$state = @explode(
'title="',
$this->_result
);
// Unexcepted result
if (count($state) < 2) {
$this->_errorMod();
return;
}
// Gets official status and extra status
$finalState = @explode(':', substr(
$state[1],
0,
stripos($state[1], '"')
));
// Sets status values
$this->status = strtolower(trim($finalState[0]));
$this->extraStatus = htmlentities(trim(strtr($finalState[1],
'?',
' '
)), ENT_COMPAT, 'UTF-8');
}
// Sets the status to timeout for the rest of the script
private function _errorMod()
{
$this->_status = 'timeout';
}
private function _getIconPath()
{
// Fixes a tips because server doesn't make the
// difference between offine and invisible
// For the server, invisible = away
if ($this->status == 'away' && !$this->extraStatus) {
$this->status = 'offline';
}
return self::IMG_PATH
. $this->statusIcons[$this->status]
. self::IMG_EXT;
}
// Gets the status as text
private function _getTextStatus($extra = true)
{
// The extra status exists
if (array_key_exists(
strtolower($this->extraStatus),
$this->statusTexts[$this->status]
)) {
return $this->statusTexts[$this->status]
[strtolower($this->extraStatus)];
}
// The extra status doesn't exist
if ($extra) {
// If the status is a custom one, it's assigned to AWAY by default
if ($this->status == 'away') {
// Returns the status with the AWAY text instead of null value
// which is actually INVISIBLE
return
ucfirst($this->statusTexts[$this->status][self::AWAY_KEY])
. ' ('
. $this->extraStatus
. ')';
}
// The status is not AWAY then it returns the value to the current
// value
return ucfirst($this->statusTexts[$this->status][null])
. ' ('
. $this->extraStatus
. ')';
}
// These lines are used for the full img format (only status title)
// Returns the status with the AWAY text instead of null value which
// is actually INVISIBLE
if ($this->status == 'away') {
return $this->statusTexts[$this->status][self::AWAY_KEY];
}
// The status is not AWAY then it returns the value to the current value
return $this->statusTexts[$this->status][null];
}
}
$s = new JabbimStatus();
$s->render();
?>