| 1 |
<?php
|
| 2 |
// $Id: onlinestatus.module,v 1.14 2006/11/06 00:38:35 xeniac Exp $
|
| 3 |
/**
|
| 4 |
* @file
|
| 5 |
* The .module File of the Onlinestatus Indicator.
|
| 6 |
*
|
| 7 |
* This file will be loaded by Drupal on demand. An enables an Onlinestatus
|
| 8 |
* Indicators for the most commonly used Instant Messenagers today.
|
| 9 |
*
|
| 10 |
* This file is part of Onlinestatus Indicator.
|
| 11 |
*
|
| 12 |
* Onlinestatus Indicator is free software; you can redistribute it and/or modify
|
| 13 |
* it under the terms of the GNU General Public License as published by
|
| 14 |
* the Free Software Foundation; either version 2 of the License, or
|
| 15 |
* any later version.
|
| 16 |
*
|
| 17 |
* Onlinestatus Indicator is distributed in the hope that it will be useful,
|
| 18 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 19 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 20 |
* GNU General Public License for more details.
|
| 21 |
*
|
| 22 |
* You should have received a copy of the GNU General Public License
|
| 23 |
* along with Onlinestatus Indicator; if not, write to the Free Software
|
| 24 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
| 25 |
*
|
| 26 |
* @author Christian Edelmann <xeniac@gmx.at>
|
| 27 |
* @version $Revision: 1.14 $
|
| 28 |
*/
|
| 29 |
|
| 30 |
/**
|
| 31 |
* Returns an associative array with all Supported Messengers with title.
|
| 32 |
*
|
| 33 |
* @param boolean $only_activated If set to true only this function only
|
| 34 |
* returns the activated Messengers.
|
| 35 |
* @return array an associative array 'messenger' => 'Messenger Title'
|
| 36 |
*/
|
| 37 |
function onlinestatus_get_messengers($only_activated = true) {
|
| 38 |
$supportet_messengers = array (
|
| 39 |
'aim' => t('AOL Instant messenger'),
|
| 40 |
'icq' => t('ICQ'),
|
| 41 |
'gtalk' => t('Google Talk'),
|
| 42 |
'jabber' => t('Jabber'),
|
| 43 |
'msn' => t('MSN messenger'),
|
| 44 |
'skype' => t('Skype'),
|
| 45 |
'yahoo' => t('Yahoo! messenger'),
|
| 46 |
'xfire' => t('XFire'),
|
| 47 |
);
|
| 48 |
if ($only_activated) {
|
| 49 |
$return = array ();
|
| 50 |
foreach (variable_get('onlinestatus_messengers', array ()) as $messenger) {
|
| 51 |
$return[$messenger] = $supportet_messengers[$messenger];
|
| 52 |
}
|
| 53 |
return $return;
|
| 54 |
}
|
| 55 |
else {
|
| 56 |
return $supportet_messengers;
|
| 57 |
}
|
| 58 |
}
|
| 59 |
|
| 60 |
/**
|
| 61 |
* Implementation of hook_help(), generates the Helpmessage for this Module.
|
| 62 |
*
|
| 63 |
* @param string $section the Drupal Path.
|
| 64 |
* @return string
|
| 65 |
*/
|
| 66 |
function onlinestatus_help($section) {
|
| 67 |
global $user;
|
| 68 |
switch ($section) {
|
| 69 |
case 'admin/help#onlinestatus' :
|
| 70 |
return t('
|
| 71 |
<p>This Module adds Onlinestatus Indicator support for the most common Instant Messengers into Drupal.
|
| 72 |
The User could fill in his messengerIDs in the user account form and the onlinestatus for each messenger will be promoted on the users profile page.</p>
|
| 73 |
<p>You can</p>
|
| 74 |
<ul>
|
| 75 |
<li>control which messenger should be supportet by your website in <a href="%settingsurl">administer >> settings > > onlinestatus</a>.</li>
|
| 76 |
<li><a href="%messengerurl">fill in your messenger account data</a>.</li>
|
| 77 |
<li><a href="%profileurl">see your onlinestatus</a>.</li>
|
| 78 |
</ul>
|
| 79 |
',array('%settingsurl' => url('admin/settings/onlinestatus'), '%messengerurl' => url('user/'.$user->uid.'/edit'), '%profileurl' => url('user/'.$user->uid)));
|
| 80 |
case 'admin/help#onlinestatus':
|
| 81 |
return 'reservated for some wise words';
|
| 82 |
case 'admin/settings/onlinestatus' :
|
| 83 |
return t('<p>These are the Basic Settings for the Online Status Indicator.</p>');
|
| 84 |
}
|
| 85 |
}
|
| 86 |
|
| 87 |
/**
|
| 88 |
* A 'Meta Function' for every messenger.
|
| 89 |
*
|
| 90 |
* onlinestatus_messenger contains the code that is common to every messenger.
|
| 91 |
* For the different part this function it includes messengers.inc.php
|
| 92 |
* and calls onlinestatus_messenger_$messenger
|
| 93 |
*
|
| 94 |
* @param string $op possible: description, update, url, settings, validate
|
| 95 |
* @param string $messenger the desired messenger 'jabber' for example.
|
| 96 |
* @param string $object a drupal object that contains user information, this could be a user or author object.
|
| 97 |
* @return string
|
| 98 |
*/
|
| 99 |
function onlinestatus_messenger($op, $messenger, $object = null) {
|
| 100 |
include_once (drupal_get_path('module','onlinestatus').'/messengers.inc.php');
|
| 101 |
switch ($op) {
|
| 102 |
case 'description' :
|
| 103 |
//returns the description text for the inputfield in the userform.
|
| 104 |
return call_user_func('onlinestatus_messenger_'.$messenger, 'description', $object-> $messenger);
|
| 105 |
case 'status' :
|
| 106 |
if($object->onlinestatus_expose == 0 ) {
|
| 107 |
return 'unknown';
|
| 108 |
}
|
| 109 |
$status = db_fetch_array(db_query("SELECT status,changed FROM {onlinestatus} WHERE uid=%d AND messenger='%s'", $object->uid, $messenger));
|
| 110 |
//Check if the Online Information in the Database is older then the cachetime.
|
| 111 |
if ((time() - $status['changed']) > variable_get('onlinestatus_cachetime', 0)) {
|
| 112 |
$status = call_user_func('onlinestatus_messenger_'.$messenger, 'update', $object->$messenger);
|
| 113 |
db_query("DELETE FROM {onlinestatus} WHERE uid=%d AND messenger='%s'", $object->uid, $messenger);
|
| 114 |
db_query("INSERT INTO {onlinestatus} (uid,messenger,status,changed) VALUES (%d,'%s','%s',%d)", $object->uid, $messenger, $status, time());
|
| 115 |
}
|
| 116 |
else {
|
| 117 |
$status = $status['status'];
|
| 118 |
}
|
| 119 |
return $status;
|
| 120 |
case 'url' :
|
| 121 |
$url = call_user_func('onlinestatus_messenger_'.$messenger, 'url', $object-> $messenger);
|
| 122 |
if ($url == false) {
|
| 123 |
$url = url('user/'.$object->uid);
|
| 124 |
}
|
| 125 |
return $url;
|
| 126 |
default :
|
| 127 |
return call_user_func('onlinestatus_messenger_'.$messenger, $op, $object);
|
| 128 |
}
|
| 129 |
return false;
|
| 130 |
}
|
| 131 |
|
| 132 |
function onlinestatus_menu($may_cache) {
|
| 133 |
global $user;
|
| 134 |
$items = array();
|
| 135 |
|
| 136 |
if ($may_cache) {
|
| 137 |
$items[] = array(
|
| 138 |
'path' => 'admin/settings/onlinestatus',
|
| 139 |
'title' => t('Onlinestatus Indicator'),
|
| 140 |
'description' => t('Configure the Status Settings'),
|
| 141 |
'callback' => 'drupal_get_form',
|
| 142 |
'callback arguments' => array('onlinestatus_admin_settings'),
|
| 143 |
'access' => 'administer site configuration',
|
| 144 |
);
|
| 145 |
}
|
| 146 |
return $items;
|
| 147 |
}
|
| 148 |
/**
|
| 149 |
* Implementation of hook_perm().
|
| 150 |
*/
|
| 151 |
function onlinestatus_perm() {
|
| 152 |
return array ('access onlinestatus');
|
| 153 |
}
|
| 154 |
|
| 155 |
/**
|
| 156 |
* Shows the General Setup for the Module
|
| 157 |
*/
|
| 158 |
function onlinestatus_admin_settings() {
|
| 159 |
//Only a real Site Administrator should be able to change this.
|
| 160 |
if (!user_access("administer site configuration")) {
|
| 161 |
return message_access();
|
| 162 |
}
|
| 163 |
//Check if PHP has CURL Support, else drop a message.
|
| 164 |
if (!extension_loaded('curl')) {
|
| 165 |
drupal_set_message(t('Your PHP installation has no CURL Support, Onlinestatus will not work.'), 'error');
|
| 166 |
}
|
| 167 |
//General Module Settings
|
| 168 |
$form['general'] = array (
|
| 169 |
'#type' => 'fieldset',
|
| 170 |
'#title' => t('General Settings'),
|
| 171 |
'#collapsible' => true,
|
| 172 |
'#collapsed' => false,);
|
| 173 |
|
| 174 |
$form['general']['onlinestatus_messengers'] = array (
|
| 175 |
'#type' => 'select',
|
| 176 |
'#multiple' => true,
|
| 177 |
'#title' => t('Use this messengers'),
|
| 178 |
'#options' => onlinestatus_get_messengers(false),
|
| 179 |
'#default_value' => variable_get('onlinestatus_messengers', array ('aim','icq','skype','yahoo')),
|
| 180 |
'#description' => t('Select all messengers that you want to support.'));
|
| 181 |
|
| 182 |
$form['general']['onlinestatus_osidescription'] = array(
|
| 183 |
'#type' => 'markup',
|
| 184 |
'#value' => t('<strong>This Module needs an <a href="!url">Online Status Indicator Server</a> to query the MSN and Jabber Onlinestatus.</strong>', array('!url' => url('http://onlinestatus.org/')) ).
|
| 185 |
t('<p>Online Status Indicator is a service that lets you put a small image on a web page to show if you are online on AOL Instant Messenger, ICQ, IRC, Jabber, MSN Messenger, and Yahoo Messenger.</p>').
|
| 186 |
t('<p>The recommended way is to install your own Online Status Indicator Server, if you don\'t have a Server with Java support and Shell access you can use one of the servers listet on onlinestatus.org '));
|
| 187 |
|
| 188 |
$form['general']['onlinestatus_osiserver'] = array (
|
| 189 |
'#type' => 'textfield',
|
| 190 |
'#title' => t('URL to your Online Status Indicator'),
|
| 191 |
'#default_value' => variable_get('onlinestatus_osiserver', ''),
|
| 192 |
'#description' => t('Enter the complete URL to your Online Status Indicator Service like <address>http://www.example.com:8080</address>'));
|
| 193 |
|
| 194 |
$form['general']['onlinestatus_registration'] = array (
|
| 195 |
'#type' => 'checkbox',
|
| 196 |
'#title' => t('Show Onlinestatus Form on Registration'),
|
| 197 |
'#default_value' => variable_get('onlinestatus_registration', false),
|
| 198 |
'#description' => t('Activate this Checkbox to add the Onlinestatus fields to the registration Progress'));
|
| 199 |
|
| 200 |
//Image Preview and Image Settings
|
| 201 |
$form['images'] = array (
|
| 202 |
'#type' => 'fieldset',
|
| 203 |
'#title' => t('Image Settings'),
|
| 204 |
'#collapsible' => true,
|
| 205 |
'#collapsed' => true,
|
| 206 |
'#description' => t('Reload this page, to see the changes you have made.'));
|
| 207 |
|
| 208 |
$form['images']['onlinestatus_imagepath'] = array (
|
| 209 |
'#type' => 'textfield',
|
| 210 |
'#title' => t('Image Path'),
|
| 211 |
'#default_value' => variable_get('onlinestatus_imagepath', '%module/images/%im-%status.png'),
|
| 212 |
'#description' => t('Path for the inidcator Images.').'<br />'.t('%im will be substituted with aim,icq,jabber, and so on.<br/>%theme will be substituted with the path to your current Theme<br/> %status will be substituted with online, offline, or unknown<br/>%module ist the current Module Path'));
|
| 213 |
|
| 214 |
$preview = '<div>';
|
| 215 |
foreach (onlinestatus_get_messengers() as $messenger => $title) {
|
| 216 |
$preview .= "<strong>$messenger:</strong> ";
|
| 217 |
foreach (array('unknown','online','offline') as $status) {
|
| 218 |
$image = strtr(variable_get('onlinestatus_imagepath', ''),
|
| 219 |
array (
|
| 220 |
'%im' => $messenger,
|
| 221 |
'%status' => $status,
|
| 222 |
'%theme' => path_to_theme(),
|
| 223 |
'%module' => drupal_get_path('module', 'onlinestatus')
|
| 224 |
)
|
| 225 |
);
|
| 226 |
$preview .= theme('image', $image, $messenger, $status, array('class' => 'onlinestatus-indicator'), FALSE);
|
| 227 |
}
|
| 228 |
$preview .= '<br/>';
|
| 229 |
}
|
| 230 |
$output .= '</div>';
|
| 231 |
$form['images']['preview'] = array ('#value' => $preview);
|
| 232 |
|
| 233 |
$form['performance'] = array (
|
| 234 |
'#type' => 'fieldset',
|
| 235 |
'#title' => t('Performance Settings'),
|
| 236 |
'#collapsible' => true,
|
| 237 |
'#collapsed' => true);
|
| 238 |
|
| 239 |
$form['performance']['help'] = array (
|
| 240 |
'#type' => 'markup',
|
| 241 |
'#value' => t('<p>Changing this Settings will affect the performance of your website.</p>'));
|
| 242 |
|
| 243 |
$form['performance']['onlinestatus_cachetime'] = array (
|
| 244 |
'#type' => 'textfield',
|
| 245 |
'#title' => t('Cache Onlinestatus'),
|
| 246 |
'#default_value' => variable_get('onlinestatus_cachetime', '60'),
|
| 247 |
'#description' => t('Cache the Onlinestatus for x Seconds.'));
|
| 248 |
|
| 249 |
$form['performance']['onlinestatus_timeout'] = array (
|
| 250 |
'#type' => 'textfield',
|
| 251 |
'#title' => t('Query Timeout'),
|
| 252 |
'#default_value' => variable_get('onlinestatus_timeout', '3'),
|
| 253 |
'#description' => t('The Maximum Limit an Statusquery should take'));
|
| 254 |
|
| 255 |
|
| 256 |
//add messenger specific options:
|
| 257 |
foreach (onlinestatus_get_messengers(false) as $messenger => $title) {
|
| 258 |
$settings = onlinestatus_messenger('settings', $messenger);
|
| 259 |
if (is_array($settings)) {
|
| 260 |
$form[$messenger] = array (
|
| 261 |
'#type' => 'fieldset',
|
| 262 |
'#title' => $title,
|
| 263 |
'#collapsible' => true,
|
| 264 |
'#collapsed' => true,);
|
| 265 |
$form[$messenger][] = $settings;
|
| 266 |
}
|
| 267 |
}
|
| 268 |
return system_settings_form($form);
|
| 269 |
}
|
| 270 |
|
| 271 |
/**
|
| 272 |
* Implementation of hook_user().
|
| 273 |
*/
|
| 274 |
function onlinestatus_user($op, & $edit, & $user, $category = NULL) {
|
| 275 |
switch ($op) {
|
| 276 |
case 'delete' :
|
| 277 |
case 'load' :
|
| 278 |
case 'login' :
|
| 279 |
case 'logout' :
|
| 280 |
break;
|
| 281 |
case 'form' :
|
| 282 |
if ($category == 'account') {
|
| 283 |
return onlinestatus_user_form($edit, $user, $category);
|
| 284 |
}
|
| 285 |
break;
|
| 286 |
case 'insert' :
|
| 287 |
return onlinestatus_user_update($edit, $user, $category);
|
| 288 |
case 'register' :
|
| 289 |
if (variable_get('onlinestatus_registration',false)) {
|
| 290 |
return onlinestatus_user_form($edit, $user, $category);
|
| 291 |
}
|
| 292 |
case 'update' :
|
| 293 |
return onlinestatus_user_update($edit, $user, $category);
|
| 294 |
case 'validate' :
|
| 295 |
return onlinestatus_user_validate($edit, $category);
|
| 296 |
case 'view' :
|
| 297 |
return theme('onlinestatus_profile', $user);
|
| 298 |
}
|
| 299 |
}
|
| 300 |
|
| 301 |
/**
|
| 302 |
* Generates the Onlinestatus Section in the user/edit form.
|
| 303 |
*/
|
| 304 |
function onlinestatus_user_form($edit, $user, $category) {
|
| 305 |
$form['onlinestatus'] = array (
|
| 306 |
'#type' => 'fieldset',
|
| 307 |
'#title' => t('Instant Messengers'),
|
| 308 |
'#collapsible' => TRUE, '#collapsed' => false,
|
| 309 |
'#description' => t('Here you can input you Accounts of all Instant messengers that you use.'),
|
| 310 |
'#weight' => 3);
|
| 311 |
foreach (onlinestatus_get_messengers(true) as $messenger => $title) {
|
| 312 |
$form['onlinestatus'][$messenger] = array (
|
| 313 |
'#type' => 'textfield',
|
| 314 |
'#title' => "$title",
|
| 315 |
'#default_value' => $user->$messenger,
|
| 316 |
'#description' => onlinestatus_messenger('description', $messenger),
|
| 317 |
);
|
| 318 |
}
|
| 319 |
if (!isset($user->onlinestatus_expose)) {
|
| 320 |
$user->onlinestatus_expose = true;
|
| 321 |
}
|
| 322 |
$form['onlinestatus']['onlinestatus_expose'] = array (
|
| 323 |
'#type' => 'checkbox',
|
| 324 |
'#title' => t('Expose my Onlinestatus'),
|
| 325 |
'#default_value' => $user->onlinestatus_expose,
|
| 326 |
'#description' => t('Activate this Checkbox if you want to expose your online status to other users'),
|
| 327 |
);
|
| 328 |
|
| 329 |
return $form;
|
| 330 |
}
|
| 331 |
|
| 332 |
/**
|
| 333 |
* deletes the status cache on a user update.
|
| 334 |
*/
|
| 335 |
function onlinestatus_user_update($edit, $user, $category) {
|
| 336 |
db_query("DELETE FROM {onlinestatus} WHERE uid = %d", $user->uid);
|
| 337 |
}
|
| 338 |
|
| 339 |
|
| 340 |
/**
|
| 341 |
* Validates the account information for each messenger.
|
| 342 |
*/
|
| 343 |
function onlinestatus_user_validate($edit, $category) {
|
| 344 |
foreach (onlinestatus_get_messengers() as $messenger => $title) {
|
| 345 |
if (!empty ($edit[$messenger]) && !onlinestatus_messenger('validate', $messenger, $edit[$messenger])) {
|
| 346 |
form_set_error(
|
| 347 |
$messenger,
|
| 348 |
t('%value is not a valid Account for %title',
|
| 349 |
array ('%value' => $edit[$messenger], '%title' => $title))
|
| 350 |
);
|
| 351 |
}
|
| 352 |
}
|
| 353 |
}
|
| 354 |
|
| 355 |
|
| 356 |
/**
|
| 357 |
* Displays the Onlinestatus of all messengers on the user profile page.
|
| 358 |
*/
|
| 359 |
function theme_onlinestatus_profile($user) {
|
| 360 |
$items = array();
|
| 361 |
drupal_add_css(drupal_get_path('module', 'onlinestatus') .'/onlinestatus.css');
|
| 362 |
foreach (onlinestatus_get_messengers(true) as $messenger => $title) {
|
| 363 |
if (empty ($user->$messenger)) {
|
| 364 |
continue;
|
| 365 |
}
|
| 366 |
$status = onlinestatus_messenger('status', $messenger, $user);
|
| 367 |
$image = theme('onlinestatus_indicator', $user, $messenger);
|
| 368 |
$url = onlinestatus_messenger('url', $messenger, $user);
|
| 369 |
$items[] = array(
|
| 370 |
'value' => sprintf('%s <a href="%s">%s (%s)</a>', $image, $url, $user->$messenger, $status),
|
| 371 |
);
|
| 372 |
}
|
| 373 |
return array(t('Online Status') => $items);
|
| 374 |
}
|
| 375 |
|
| 376 |
|
| 377 |
/**
|
| 378 |
* Themeable Statusindicator for Drupal.
|
| 379 |
*
|
| 380 |
* Generates the indicator image.
|
| 381 |
* Call this function with theme('onlinestatus_inidicator',$user, $messenger)
|
| 382 |
*
|
| 383 |
* @see theme()
|
| 384 |
* @param object $user can be an node, comment, or user object object.
|
| 385 |
* @param string $messenger the Name of the Messenger 'aim' for example.
|
| 386 |
* @return string
|
| 387 |
*/
|
| 388 |
function theme_onlinestatus_indicator($user, $messenger) {
|
| 389 |
$status = onlinestatus_messenger('status', $messenger, $user);
|
| 390 |
$url = onlinestatus_messenger('url', $messenger, $user);
|
| 391 |
$image = strtr(
|
| 392 |
variable_get('onlinestatus_imagepath', ''),
|
| 393 |
array (
|
| 394 |
'%im' => $messenger,
|
| 395 |
'%status' => $status,
|
| 396 |
'%theme' => path_to_theme(),
|
| 397 |
'%module' => drupal_get_path('module', 'onlinestatus'),
|
| 398 |
)
|
| 399 |
);
|
| 400 |
return sprintf(
|
| 401 |
'<a href="%s">%s</a>',
|
| 402 |
onlinestatus_messenger('url',$messenger, $user),
|
| 403 |
theme('image', $image, $messenger, $status, array('class' => 'onlinestatus-indicator'), FALSE)
|
| 404 |
);
|
| 405 |
}
|
| 406 |
|
| 407 |
|
| 408 |
/**
|
| 409 |
* fetches the Data of an url via CURL
|
| 410 |
*
|
| 411 |
* @var handle $curl_connection the handle for the CURL Connection
|
| 412 |
* @param string $url the url we want to see
|
| 413 |
* @param boolean $fetch_header set to true if you need the http Header.
|
| 414 |
* @param boolean $no_content set to true if you don't need the HTML Content.
|
| 415 |
* @param boolean $foolw_location set to true if you want to CURL to follow http redirects.
|
| 416 |
* @return string
|
| 417 |
**/
|
| 418 |
function onlinestatus_fetch_url($url, $fetch_header = true, $no_content = true, $follow_location = false) {
|
| 419 |
$curl_connection = curl_init($url);
|
| 420 |
curl_setopt($curl_connection, CURLOPT_TIMEOUT, variable_get('onlinestatus_timeout', 3));
|
| 421 |
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, 1);
|
| 422 |
curl_setopt($curl_connection, CURLOPT_HEADER, $fetch_header);
|
| 423 |
curl_setopt($curl_connection, CURLOPT_NOBODY, $no_content);
|
| 424 |
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, $follow_location);
|
| 425 |
$data = curl_exec($curl_connection);
|
| 426 |
if (curl_errno($curl_connection)) {
|
| 427 |
watchdog('onlinestatus', curl_error($curl_connection), WATCHDOG_NOTICE, $url);
|
| 428 |
}
|
| 429 |
curl_close($curl_connection);
|
| 430 |
return trim($data);
|
| 431 |
}
|
| 432 |
?>
|