| 1 |
<?php
|
| 2 |
// $Id: yshout.module,v 1.4 2009/01/05 23:54:36 michael9 Exp $
|
| 3 |
$yshout_js_done = false;
|
| 4 |
|
| 5 |
/*
|
| 6 |
* implementation of hook_help()
|
| 7 |
*/
|
| 8 |
function yshout_help($path, $arg) {
|
| 9 |
$output = '';
|
| 10 |
switch($path) {
|
| 11 |
case 'admin/help#yshout':
|
| 12 |
$output .= '<p>'.t('YShout is a shoutbox purchasable from').' <a href="http://yurivish.com/yshout">here</a><br/>'.t('Once you have purchased it, you should install it in a directory named \'yshout\' relative to your drupal installation.').'</p>';
|
| 13 |
break;
|
| 14 |
}
|
| 15 |
return $output;
|
| 16 |
} // function yshout_help
|
| 17 |
|
| 18 |
/*
|
| 19 |
* implementation of hook_perm()
|
| 20 |
*/
|
| 21 |
function yshout_perm() {
|
| 22 |
return array('access shoutbox', 'view or change yshout admin password', 'view yshout bans');
|
| 23 |
} // function yshout_perm
|
| 24 |
|
| 25 |
/*
|
| 26 |
* implementation of hook_link()
|
| 27 |
*/
|
| 28 |
function yshout_link($type, $object, $teaser = FALSE) {
|
| 29 |
$links = array();
|
| 30 |
if(user_access('access shoutbox') && !isset($object->nid)) {
|
| 31 |
$links['yshout_shoutbox'] = array(
|
| 32 |
'title' => t('Shoutbox'),
|
| 33 |
'href' => check_url(url('shoutbox'))
|
| 34 |
);
|
| 35 |
}
|
| 36 |
return $links;
|
| 37 |
} // function yshout_link
|
| 38 |
|
| 39 |
/*
|
| 40 |
* implementation of hook_cron()
|
| 41 |
*/
|
| 42 |
function yshout_cron() {
|
| 43 |
$file = file_get_contents(drupal_get_path('module', 'yshout').'/yshout/logs/log.'.variable_get('yshout_logfile', 1).'.txt');
|
| 44 |
$logs = unserialize($file);
|
| 45 |
$highesttimestamp = 0;
|
| 46 |
foreach($logs['posts'] as $key => $data) {
|
| 47 |
if($data['nickname'] != "Guest" && user_load(array('name' => $data['nickname'])) === FALSE) unset($logs['posts'][$key]);
|
| 48 |
elseif($data['timestamp'] > $highesttimestamp) $highesttimestamp = $data['timestamp'];
|
| 49 |
}
|
| 50 |
$logs['info']['latestTimestamp'] = $highesttimestamp;
|
| 51 |
$logs = serialize($logs);
|
| 52 |
$file = file_put_contents(drupal_get_path('module', 'yshout').'/yshout/logs/log.'.variable_get('yshout_logfile', 1).'.txt', $logs);
|
| 53 |
}
|
| 54 |
|
| 55 |
/*
|
| 56 |
* implementation of hook_block()
|
| 57 |
*/
|
| 58 |
function yshout_block($op = 'list', $delta = 0) {
|
| 59 |
$block = array();
|
| 60 |
if($op == 'list') {
|
| 61 |
$block[0] = array();
|
| 62 |
$block[0]['info'] = t('Shoutbox');
|
| 63 |
return $block;
|
| 64 |
} elseif($op == 'view' && user_access('access shoutbox')) {
|
| 65 |
$block['subject'] = t('Shoutbox');
|
| 66 |
$block['content'] .= yshout_js().'<a id="ylink" href="#">Open the shoutbox</a>';
|
| 67 |
return $block;
|
| 68 |
}
|
| 69 |
} // function yshout_block
|
| 70 |
|
| 71 |
function yshout_admin() {
|
| 72 |
$file = file_get_contents(drupal_get_path('module', 'yshout').'/yshout/logs/yshout.prefs.txt');
|
| 73 |
$prefs = unserialize($file);
|
| 74 |
$form['yshout_refreshtime'] = array(
|
| 75 |
'#type' => 'textfield',
|
| 76 |
'#title' => t('Refresh Time (in seconds)'),
|
| 77 |
'#default_value' => ($prefs['refresh']/1000),
|
| 78 |
'#size' => 2,
|
| 79 |
'#description' => 'Refresh time in seconds (you can use decimals)',
|
| 80 |
'#required' => TRUE
|
| 81 |
);
|
| 82 |
if(user_access('view or change yshout admin password')) {
|
| 83 |
$form['yshout_password'] = array(
|
| 84 |
'#type' => 'textfield',
|
| 85 |
'#title' => 'YShout password',
|
| 86 |
'#default_value' => $prefs['password'],
|
| 87 |
'#description' => 'The YShout admin password',
|
| 88 |
'#required' => TRUE
|
| 89 |
);
|
| 90 |
}
|
| 91 |
$form['yshout_logfile'] = array(
|
| 92 |
'#type' => 'textfield',
|
| 93 |
'#title' => t('YShout logfile'),
|
| 94 |
'#default_value' => variable_get('yshout_logfile', 1),
|
| 95 |
'#size' => 2,
|
| 96 |
'#maxlength' => 2,
|
| 97 |
'#description' => t('What logfile would you like Drupal to use?'),
|
| 98 |
'#required' => TRUE
|
| 99 |
);
|
| 100 |
$selects = yshout_getcss();
|
| 101 |
$form['yshout_css'] = array(
|
| 102 |
'#type' => 'select',
|
| 103 |
'#title' => t('YShout CSS file'),
|
| 104 |
'#default_value' => variable_get('yshout_css', $selects[0]),
|
| 105 |
'#description' => t('This is the CSS (Cascading Style Sheet) that YShout will use. It automatically reads all the files from \''.yshout_getcss(true).'\'.'),
|
| 106 |
'#options' => $selects,
|
| 107 |
'#required' => TRUE
|
| 108 |
);
|
| 109 |
return system_settings_form($form);
|
| 110 |
} // function yshout_admin
|
| 111 |
|
| 112 |
function yshout_bans() {
|
| 113 |
if(user_access('view yshout bans')) {
|
| 114 |
$rows = array();
|
| 115 |
$header = array(array("data" => t('IP')), array("data" => t('Nickname')), array("data" => t('Time')));
|
| 116 |
$file = file_get_contents(drupal_get_path('module', 'yshout').'/yshout/logs/yshout.bans.txt');
|
| 117 |
$bans = unserialize($file);
|
| 118 |
foreach($bans as $data) {
|
| 119 |
$data['ip'] .= ' ('.gethostbyaddr($data['ip']).')';
|
| 120 |
$rows[] = array('data' => array($data['ip'], $data['nickname'], format_date($data['timestamp'], 'large')));
|
| 121 |
}
|
| 122 |
if(empty($rows)) {
|
| 123 |
$rows[] = array(array('data' => t('No bans'), 'colspan' => count($header)));
|
| 124 |
}
|
| 125 |
$table = theme("table", $header, $rows);
|
| 126 |
return $table;
|
| 127 |
}
|
| 128 |
} // function yshout_bans
|
| 129 |
|
| 130 |
function yshout_getcss($returndir = false) {
|
| 131 |
$dir = drupal_get_path('module', 'yshout')."/yshout/example/css/";
|
| 132 |
if($returndir === true) {
|
| 133 |
$selects = $dir;
|
| 134 |
} else {
|
| 135 |
$selects = array();
|
| 136 |
if(is_dir($dir)) {
|
| 137 |
if($dh = opendir($dir)) {
|
| 138 |
while(($file = readdir($dh)) !== false) {
|
| 139 |
if($file != ".." && $file != "." && substr($file,-4) == ".css" && $file != "style.css") {
|
| 140 |
$selects[] = $file;
|
| 141 |
}
|
| 142 |
}
|
| 143 |
closedir($dh);
|
| 144 |
}
|
| 145 |
}
|
| 146 |
$selects = drupal_map_assoc($selects);
|
| 147 |
}
|
| 148 |
return $selects;
|
| 149 |
}
|
| 150 |
|
| 151 |
function yshout_admin_validate($form, &$form_state) {
|
| 152 |
$file = file_get_contents(drupal_get_path('module', 'yshout').'/yshout/logs/yshout.prefs.txt');
|
| 153 |
$prefs = unserialize($file);
|
| 154 |
$logfile = $form_state['values']['yshout_logfile'];
|
| 155 |
if(!is_numeric($logfile)) {
|
| 156 |
form_set_error('yshout_logfile', t('The logfile must be a number'));
|
| 157 |
} elseif($logfile < 1) {
|
| 158 |
form_set_error('yshout_logfile', t('The logfile must be a number above 0'));
|
| 159 |
}
|
| 160 |
$refreshtime = $form_state['values']['yshout_refreshtime'];
|
| 161 |
if(!is_numeric($refreshtime)) {
|
| 162 |
form_set_error('yshout_refreshtime', t('The time must be numeric'));
|
| 163 |
} elseif($logfile <= 0) {
|
| 164 |
form_set_error('yshout_refreshtime', t('The time must be above 0'));
|
| 165 |
} else {
|
| 166 |
$prefs['refresh'] = ($refreshtime*1000);
|
| 167 |
}
|
| 168 |
$password = $form_state['values']['yshout_password'];
|
| 169 |
if(strlen($password) < 5) {
|
| 170 |
form_set_error('yshout_password', t('The password must be 5 characters or longer'));
|
| 171 |
} else {
|
| 172 |
$prefs['password'] = $password;
|
| 173 |
}
|
| 174 |
$file = serialize($prefs);
|
| 175 |
file_put_contents(drupal_get_path('module', 'yshout').'/yshout/logs/yshout.prefs.txt', $file);
|
| 176 |
} // function yshout_admin_validate
|
| 177 |
|
| 178 |
function yshout_all() {
|
| 179 |
$content = '';
|
| 180 |
$content .= yshout_js();
|
| 181 |
$content .= '<div id="yshout">Chat loading...</div>';
|
| 182 |
$content .= '<br/>'.yshout_bans();
|
| 183 |
return $content;
|
| 184 |
} // function yshout_all
|
| 185 |
|
| 186 |
// yshout_js()
|
| 187 |
// Returns the JavaScript files required for YShout
|
| 188 |
function yshout_js() {
|
| 189 |
global $yshout_js_done, $user;
|
| 190 |
if(!$yshout_js_done) {
|
| 191 |
$return = '';
|
| 192 |
drupal_add_js(drupal_get_path('module', 'yshout').'/yshout/js/yshout.js', 'module');
|
| 193 |
drupal_add_js(drupal_get_path('module', 'yshout').'/yshout/js/jquery.js', 'module');
|
| 194 |
$yshoutcode .= 'new YShout({
|
| 195 |
yPath: \''.base_path().drupal_get_path('module', 'yshout').'/yshout/'.'\',
|
| 196 |
log: '.variable_get('yshout_logfile', 1).',
|
| 197 |
yLink: \'ylink\',
|
| 198 |
name: \''.(strlen($user->name) > 0 ? $user->name : 'Guest').'\'
|
| 199 |
});';
|
| 200 |
drupal_add_js($yshoutcode, 'inline');
|
| 201 |
drupal_add_css(drupal_get_path('module', 'yshout').'/yshout/example/css/'.variable_get('yshout_css', 'light.yshout.css'), 'module');
|
| 202 |
$yshout_js_done = true;
|
| 203 |
}
|
| 204 |
} // function yshout_js
|
| 205 |
|
| 206 |
/*
|
| 207 |
* implementation of hook_menu()
|
| 208 |
*/
|
| 209 |
function yshout_menu() {
|
| 210 |
$items = array();
|
| 211 |
$items['admin/settings/yshout'] = array(
|
| 212 |
'title' => 'YShout',
|
| 213 |
'page callback' => 'system_admin_menu_block_page',
|
| 214 |
'access arguments' => array('access administration pages'),
|
| 215 |
'type' => MENU_NORMAL_ITEM,
|
| 216 |
'description' => 'YShout admin menu',
|
| 217 |
'file' => 'system.admin.inc',
|
| 218 |
'file path' => drupal_get_path('module', 'system'),
|
| 219 |
);
|
| 220 |
$items['admin/settings/yshout/settings'] = array(
|
| 221 |
'title' => 'YShout settings',
|
| 222 |
'description' => 'YShout settings control',
|
| 223 |
'page callback' => 'drupal_get_form',
|
| 224 |
'page arguments' => array('yshout_admin'),
|
| 225 |
'access arguments' => array('access administration pages'),
|
| 226 |
'type' => MENU_NORMAL_ITEM,
|
| 227 |
'weight' => 2
|
| 228 |
);
|
| 229 |
$items['admin/settings/yshout/bans'] = array(
|
| 230 |
'title' => 'YShout bans',
|
| 231 |
'description' => 'YShout ban list',
|
| 232 |
'page callback' => 'yshout_bans',
|
| 233 |
'access arguments' => array('access administration pages'),
|
| 234 |
'weight' => 1,
|
| 235 |
'type' => MENU_NORMAL_ITEM
|
| 236 |
);
|
| 237 |
$items['shoutbox'] = array(
|
| 238 |
'title' => 'Shoutbox',
|
| 239 |
'page callback' => 'yshout_all',
|
| 240 |
'access arguments' => array('access shoutbox'),
|
| 241 |
'weight' => 0,
|
| 242 |
'type' => MENU_SUGGESTED_ITEM
|
| 243 |
);
|
| 244 |
return $items;
|
| 245 |
} // function yshout_menu
|