[3319] | 1 | <?php |
---|
| 2 | |
---|
| 3 | if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!'); |
---|
[3695] | 4 | global $conf, $conf_whois, $prefixeTable; |
---|
| 5 | include_once(WHOIS_ONLINE_PATH.'include/wo_functions.inc.php'); |
---|
[3319] | 6 | /* Secure Config */ |
---|
[3340] | 7 | if (!isset($conf_whois['Active'])) $conf_whois = whois_online_conf(); |
---|
[3319] | 8 | |
---|
| 9 | function whois_online_conf() |
---|
| 10 | { |
---|
| 11 | global $conf; |
---|
[3695] | 12 | $default = array( |
---|
| 13 | 'Active' => true, |
---|
| 14 | 'Delete level' => 20, |
---|
| 15 | 'Radar limit' => 25, |
---|
| 16 | 'Webmasters' => 2, // Normal |
---|
| 17 | 'Administrators' => 2, |
---|
| 18 | 'Obsolete limit' => 20, |
---|
| 19 | 'Default display' => true, |
---|
| 20 | 'Add to Plugins menu' => false, |
---|
| 21 | 'Add icon to History' => true, |
---|
| 22 | 'Keep data' => true, |
---|
| 23 | 'Search id' => 0, |
---|
| 24 | 'Users' => Array('max' => 0, 'When' => date('Y-m-d'), 'count' => 0), |
---|
| 25 | ); |
---|
| 26 | $conf_whois = array_merge($default, unserialize($conf['Whois Online'])); |
---|
[3336] | 27 | if ((!isset($conf_whois['Version'])) or $conf_whois['Version'] != WHOIS_ONLINE_VER |
---|
[3319] | 28 | or $conf['Whois Online'] != serialize($conf_whois)) { |
---|
| 29 | $conf_whois['Version'] = WHOIS_ONLINE_VER; |
---|
| 30 | $conf['Whois Online'] = serialize($conf_whois); |
---|
| 31 | pwg_query('REPLACE INTO ' . CONFIG_TABLE . " (param,value,comment) |
---|
| 32 | VALUES ('Whois Online','". $conf['Whois Online'] ."','Whois Online configuration');"); |
---|
| 33 | } |
---|
| 34 | return $conf_whois; |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | define('ONLINE_LEVEL', (100+$conf_whois['Delete level'])/100); |
---|
| 38 | define('ONLINE_LIMIT', $conf_whois['Obsolete limit']); |
---|
| 39 | |
---|
| 40 | /* Admin menus are always available */ |
---|
| 41 | add_event_handler('get_admin_plugin_menu_links', 'whois_add_icon' ); |
---|
| 42 | /* On Active */ |
---|
| 43 | if ($conf_whois['Active']) { |
---|
| 44 | $conf['Whois Online Update'] = true; |
---|
| 45 | add_event_handler('loc_begin_picture', 'whois_online_management'); |
---|
| 46 | add_event_handler('loc_begin_index', 'whois_online_management'); |
---|
| 47 | add_event_handler('register_user', 'whois_online_register'); |
---|
| 48 | if ($conf_whois['Default display'] or (defined('IN_ADMIN') and IN_ADMIN)) add_event_handler('loc_begin_page_tail', 'whois_default_display' ); |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | |
---|
[3695] | 52 | |
---|
| 53 | |
---|
| 54 | // Assume the Default display on pages |
---|
[3319] | 55 | function whois_default_display() { |
---|
| 56 | global $template; |
---|
| 57 | $template->set_filenames(array( 'Whois_display' => dirname(__FILE__).'/default.tpl')); |
---|
| 58 | $template->pparse('Whois_display'); |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | // Add admin links |
---|
| 62 | function whois_add_icon($menu) { |
---|
[3695] | 63 | global $conf_whois, $lang; |
---|
| 64 | if ($conf_whois['Add icon to History']) |
---|
[3319] | 65 | $lang['History'] .= '</a> <a class="external" href="' . get_admin_plugin_menu_link(WHOIS_ONLINE_PATH.'config.php') . '"> |
---|
| 66 | <img class="button" src="' . WHOIS_ONLINE_PATH . 'icons/Whois_tuner.gif" alt="Whois Online configuration" title="Whois Online configuration" /></a>'; |
---|
[3695] | 67 | if ($conf_whois['Add to Plugins menu']) array_push($menu, array( |
---|
[3319] | 68 | 'NAME' => 'Whois Online', |
---|
| 69 | 'URL' => get_admin_plugin_menu_link(WHOIS_ONLINE_PATH.'config.php'), |
---|
| 70 | )); |
---|
| 71 | return $menu; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | // Template function |
---|
| 75 | function Whois_most($text, $count, $when, $format) { |
---|
| 76 | return sprintf(l10n($text),$count,date(l10n($format),$when)); |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | // New member |
---|
| 80 | function whois_online_register($who) { |
---|
| 81 | global $conf, $conf_whois; |
---|
| 82 | $conf_whois['Users']['count'] = $who['id']; |
---|
| 83 | $conf['Whois Online'] = serialize($conf_whois); |
---|
| 84 | pwg_query('REPLACE INTO ' . CONFIG_TABLE . " (param,value,comment) |
---|
| 85 | VALUES ('Whois Online','". $conf['Whois Online'] ."','Whois Online configuration');"); |
---|
| 86 | return; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | function whois_country($trace, $bypass = false) { |
---|
| 90 | if (!isset($trace['country'])) { |
---|
| 91 | pwg_query('ALTER TABLE ' . WHOIS_ONLINE_TABLE . ' ADD `country` VARCHAR( 256 ) NOT NULL AFTER `lang` ;'); |
---|
| 92 | $trace['country']=''; |
---|
| 93 | } |
---|
| 94 | $c = array(); |
---|
| 95 | if ($trace['country']!='') $c = @unserialize(htmlspecialchars_decode($trace['country'])); |
---|
| 96 | if (isset($c['Code']) and $c['Code']!='' and $c['Code']!='__') return $c; |
---|
| 97 | if ($bypass and isset($c['Code']) and $c['Code']=='__') return $c; |
---|
| 98 | $f = @fopen ('http://api.hostip.info/get_html.php?ip=' . $trace['IP'], 'r'); |
---|
| 99 | if ($f === FALSE) return Array('Code' => '__', 'Name' => l10n('Unknown country'), 'City' => 'N/A',); |
---|
| 100 | $result=''; |
---|
| 101 | while ($l = fgets ($f, 1024)) $result .= $l; |
---|
| 102 | fclose ($f); |
---|
| 103 | $tokens = split ("[:\n]", $result); |
---|
| 104 | $c = array ('Name' => $tokens[1], 'City' => $tokens[3]); |
---|
| 105 | if (strpos ($c['Name'], '?') === FALSE) { |
---|
| 106 | $tokens = split ("[\(\)]", $c['Name']); |
---|
| 107 | $c['Code'] = isset($tokens[1]) ? $tokens[1]:'__'; |
---|
| 108 | $c['Name'] = ucwords ( strtolower( ereg_replace |
---|
| 109 | (' \([^\)]+)', '', $c['Name']))); |
---|
| 110 | } |
---|
| 111 | else $c = Array('Code' => '__', 'Name' => l10n('Unknown country'), 'City' => 'N/A',); |
---|
| 112 | if ($c['Code'] == 'Private Address') { |
---|
| 113 | $c['Name'] = l10n('Private Address'); |
---|
| 114 | $c['City'] = l10n('N/A'); |
---|
| 115 | $c['Code'] = '__'; |
---|
| 116 | } |
---|
[3695] | 117 | $new = htmlspecialchars(serialize($c),ENT_QUOTES,'UTF-8'); |
---|
| 118 | if ($new == $trace['country']) return $c; |
---|
[3319] | 119 | pwg_query('UPDATE ' . WHOIS_ONLINE_TABLE . ' |
---|
[3695] | 120 | SET `country` = \'' . $new . '\' |
---|
[3319] | 121 | WHERE `session_id` = \'' . $trace['session_id'] . '\';'); |
---|
| 122 | return $c; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | function whois_flag($trace, &$step, $limit = 10) { |
---|
| 126 | $flag = WHOIS_ONLINE_PATH . 'flags/' . $trace['Country']['Code'] . '.jpg'; |
---|
| 127 | if (file_exists($flag)) return $flag; |
---|
| 128 | if ( $step > $limit ) return WHOIS_ONLINE_PATH . 'flags/__.jpg'; |
---|
| 129 | $f = fopen ('http://api.hostip.info/flag.php?ip=' . $trace['IP'], 'r'); |
---|
| 130 | $result=''; |
---|
| 131 | while ($l = fgets ($f, 1024)) $result .= $l; |
---|
| 132 | fclose ($f); |
---|
| 133 | $f = fopen($flag,"w+"); |
---|
| 134 | fputs($f,$result); |
---|
| 135 | fclose($f); |
---|
| 136 | return $flag; |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | // Read all data |
---|
| 140 | function whois_online_get_data($order='') { |
---|
| 141 | $remove = "''"; // Nothing to remove ( = an empty session_id) |
---|
| 142 | $ctr = 0; $Online[0] = ''; |
---|
| 143 | $result = pwg_query('SELECT * FROM ' . WHOIS_ONLINE_TABLE . $order . ';'); |
---|
| 144 | while($row=mysql_fetch_array($result)) { |
---|
| 145 | $row['delay'] = (int) time() - $row['last_access']; |
---|
| 146 | $row['elm_ids'] = explode(' ', $row['last_elm_ids']); |
---|
| 147 | $row['cat_ids'] = explode(' ', $row['last_cat_ids']); |
---|
| 148 | $row['tag_ids'] = explode(' ', $row['last_tag_ids']); |
---|
| 149 | $row['sch_ids'] = explode(' ', $row['last_sch_ids']); |
---|
| 150 | $row['dates'] = explode(' ', $row['last_dates']); |
---|
| 151 | $ctr++; |
---|
| 152 | if ( $row['IP'] != 'global' and $row['permanent'] == 'false' |
---|
| 153 | and $row['delay'] > (float)( 60 * 60 * 24 * 3 )) $remove .= ", '" . $row['session_id'] . "'"; |
---|
| 154 | elseif ($row['IP'] == 'global') $Online[0] = $row; |
---|
| 155 | else $Online[] = $row; |
---|
| 156 | } |
---|
| 157 | // Out of 7 visits: Reduce registered visits for 3 days without access |
---|
| 158 | if ($remove != "''" and $ctr > (count($Online) * ONLINE_LEVEL) |
---|
| 159 | and ($ctr-count($Online)) > ONLINE_LIMIT and ($Online[0]['pag_hits']%7)==0) { |
---|
| 160 | pwg_query('DELETE FROM ' . WHOIS_ONLINE_TABLE . ' WHERE `session_id` IN ('. $remove .') |
---|
| 161 | AND `permanent` = \'false\' AND `IP` <> \'global\';'); |
---|
| 162 | if (($Online[0]['pag_hits']%13)==0) @pwg_query('OPTIMIZE TABLE ' . WHOIS_ONLINE_TABLE . ';'); |
---|
| 163 | } |
---|
| 164 | return $Online; |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | // Update global and update/create current session_id |
---|
| 168 | function whois_online_update(&$global, &$dedicated, &$gtrace) |
---|
| 169 | { |
---|
| 170 | global $lang_info; |
---|
| 171 | // Rewrite the global record |
---|
| 172 | if ( $gtrace == 2 ) { |
---|
| 173 | $query = 'REPLACE INTO ' . WHOIS_ONLINE_TABLE . ' (`IP`, `hidden_IP`, `session_id`,`user_id`,`username`,`lang`, |
---|
| 174 | `permanent`,`last_access`,`last_elm_ids`, `last_cat_ids`, `last_tag_ids`, `last_sch_ids`, |
---|
| 175 | `first_access_date`, `last_dates`, `elm_hits`, `pag_hits`) |
---|
| 176 | VALUES (\'global\', \'true\',\'global\', 0, \''. $global['username'] .'\', \'--\', \'true\', \'' |
---|
| 177 | . time() .'\', \'' |
---|
| 178 | . implode(' ',$global['elm_ids']) . '\', \'' |
---|
| 179 | . implode(' ',$global['cat_ids']) . '\', \'' |
---|
| 180 | . implode(' ',$global['tag_ids']) . '\', \'' |
---|
| 181 | . implode(' ',$global['sch_ids']) . '\', \'' |
---|
| 182 | . $global['first_access_date'] . '\', \'\', \'' |
---|
| 183 | . $global['elm_hits'] . '\', \'' . $global['pag_hits'] . '\');'; |
---|
| 184 | pwg_query($query); |
---|
| 185 | } |
---|
| 186 | // Write or Rewrite the dedicated record |
---|
[3695] | 187 | $query = 'REPLACE INTO ' . WHOIS_ONLINE_TABLE . ' (`IP`, `hidden_IP`, `session_id`,`user_id`,`username`,`lang`, `user_agent`, |
---|
| 188 | `any_previous`, `same_previous`, `permanent`,`last_access`,`last_elm_ids`, `last_cat_ids`, `last_tag_ids`, `last_sch_ids`, |
---|
[3319] | 189 | `first_access_date`, `last_dates`, `elm_hits`, `pag_hits`) |
---|
| 190 | VALUES (\''. $dedicated['IP'] .'\', \'' |
---|
| 191 | . $dedicated['hidden_IP'] .'\', \''. $dedicated['session_id'] .'\', \'' |
---|
| 192 | . $dedicated['user_id'] .'\', \''. $dedicated['username'] .'\', \'' |
---|
| 193 | . substr($lang_info['code'],0,2) .'\', \'' |
---|
[3695] | 194 | . $dedicated['user_agent'] .'\', \'' |
---|
| 195 | . $dedicated['any_previous'] .'\', \'' |
---|
| 196 | . $dedicated['same_previous'] .'\', \'' |
---|
[3319] | 197 | . $dedicated['permanent'] . '\', \''. time() .'\', \'' |
---|
| 198 | . implode(' ',$dedicated['elm_ids']) . '\', \'' |
---|
| 199 | . implode(' ',$dedicated['cat_ids']) . '\', \'' |
---|
| 200 | . implode(' ',$dedicated['tag_ids']) . '\', \'' |
---|
| 201 | . implode(' ',$dedicated['sch_ids']) . '\', \'' |
---|
| 202 | . $dedicated['first_access_date'] . '\', \'' |
---|
| 203 | . implode(' ',$dedicated['dates']) . '\', \'' |
---|
| 204 | . $dedicated['elm_hits'] . '\', \'' |
---|
| 205 | . $dedicated['pag_hits'] . '\');'; |
---|
| 206 | pwg_query($query); |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | // Data tracking |
---|
| 210 | // Parameters: |
---|
| 211 | // - Array of Ids |
---|
| 212 | // - New ID |
---|
| 213 | // - Array of dates |
---|
| 214 | // => Add the ID if needed, Add Today if needed |
---|
| 215 | function whois_online_track(&$key, $id, &$date) { |
---|
| 216 | if ($id != '') array_unshift($key, $id); |
---|
| 217 | $key = array_unique($key); |
---|
| 218 | if (count($key)>10) array_pop($key); |
---|
| 219 | array_unshift($date, date('Y-m-d')); |
---|
| 220 | $date = array_unique($date); |
---|
| 221 | if (count($date)>5) array_pop($date); |
---|
| 222 | return; |
---|
| 223 | } |
---|
| 224 | |
---|
[3695] | 225 | // Antiaspi delay conversion in seconds |
---|
| 226 | // delay in "HH:ii:ss" or "d :HH:ii:ss" |
---|
| 227 | // return delay in seconds |
---|
| 228 | function whois_online_duration($date_string) |
---|
| 229 | { |
---|
| 230 | list($s, $i, $H, $d, $more) = |
---|
| 231 | array_merge(array_reverse( |
---|
| 232 | explode(" ",str_ireplace(':',' ', $date_string))), |
---|
| 233 | array(0,0,0,0,0)); |
---|
| 234 | $t = time(); |
---|
| 235 | return strtotime(sprintf("+%s days %s hours %s minutes %s seconds", |
---|
| 236 | $d, $H, $i, $s), $t) - $t; |
---|
| 237 | } |
---|
| 238 | |
---|
[3319] | 239 | /* |
---|
| 240 | Main process: Analyze, set new values and prepare displayed values. |
---|
| 241 | |
---|
| 242 | Update on parameter... |
---|
| 243 | */ |
---|
| 244 | function whois_online_management() |
---|
| 245 | { |
---|
| 246 | global $user, $conf, $conf_whois, $template, $page, $lang, $lang_info; |
---|
| 247 | load_language('plugin.lang', WHOIS_ONLINE_PATH); |
---|
| 248 | srand((float)time()); |
---|
| 249 | |
---|
| 250 | pwg_debug('*********** start Online_management ***********'); |
---|
| 251 | if (!isset($conf_whois['Active'])) $conf_whois = whois_online_conf(); |
---|
| 252 | |
---|
| 253 | $online = whois_online_get_data(); |
---|
| 254 | |
---|
| 255 | $global = $online[0]; |
---|
| 256 | unset($online[0]); |
---|
| 257 | $sid = session_id(); |
---|
[3695] | 258 | |
---|
[3319] | 259 | // Step 1 - Find the User and/or IP/session_id |
---|
| 260 | foreach ($online as $key => $record) { |
---|
| 261 | // 1st case: Same IP and same member (Proxy guests are viewed as one) |
---|
| 262 | if ($record['IP'] == $_SERVER['REMOTE_ADDR'] |
---|
| 263 | and $record['username'] == $user['username'] ) { |
---|
| 264 | $visit = $record; |
---|
| 265 | $v = $key; |
---|
| 266 | break; |
---|
| 267 | } |
---|
| 268 | // 2nd case: Same session and user (No doubt) |
---|
| 269 | if ($record['session_id'] == $sid |
---|
| 270 | and $record['username'] == $user['username'] ) { |
---|
| 271 | $visit = $record; // Maybe a guest |
---|
| 272 | if (!is_a_guest()) $visit['hidden_IP'] = 'true'; // Maybe Proxy or hidden IP |
---|
| 273 | //$visit['IP'] = $_SERVER['REMOTE_ADDR']; |
---|
| 274 | $v = $key; |
---|
| 275 | break; |
---|
| 276 | } |
---|
| 277 | // 3rd and last case: Same user_id |
---|
| 278 | if ($record['user_id'] == $user['id'] and !is_a_guest()) { // new IP and new session |
---|
| 279 | $visit = $record; |
---|
| 280 | $visit['hidden_IP'] = 'true'; /* Or Generic user or the user is using several connections */ |
---|
| 281 | //$visit['IP'] = $_SERVER['REMOTE_ADDR']; |
---|
| 282 | $v = $key; |
---|
| 283 | break; |
---|
| 284 | } |
---|
| 285 | } |
---|
| 286 | |
---|
| 287 | // Step 2 - Assume a new comer |
---|
| 288 | if ( !isset($visit) ) { |
---|
| 289 | $visit = Array( |
---|
| 290 | 'IP' => $_SERVER['REMOTE_ADDR'], // First known IP (Is that one true?) |
---|
| 291 | 'hidden_IP' => 'false', // supposed a fixed IP |
---|
| 292 | 'session_id' => $sid, |
---|
| 293 | 'user_id' => $user['id'], |
---|
| 294 | 'username' => $user['username'], |
---|
| 295 | 'delay' => 0, |
---|
| 296 | 'lang' => substr($lang_info['code'],0,2), |
---|
| 297 | 'permanent' => 'false', // False: delete after 72 Hours / True: delete after 90 days |
---|
| 298 | 'last_access' => time(), // Last access by this user |
---|
| 299 | 'elm_ids' => array_fill(0, 10, 0), // 10 last minutes + Last reference minute |
---|
| 300 | 'cat_ids' => array_fill(0, 12, 0), // 12 ranges (of 5 minutes) + ref |
---|
| 301 | 'tag_ids' => array_fill(0, 24, 0), // 24 hours + ref |
---|
| 302 | 'sch_ids' => array_fill(0, 14, 0), // 14 days + ref |
---|
| 303 | 'date' => date('Y-m-d'), // Futur usage |
---|
| 304 | 'elm_hits' => 0, 'pag_hits' => 0, // elements hits and pages hits by this user |
---|
| 305 | 'first_access_date' => date('Y-m-d'), // First access by this user |
---|
| 306 | 'dates' => Array(), // 5 last access dates |
---|
| 307 | ); |
---|
| 308 | $online[] = $visit; |
---|
| 309 | $v = count($online); |
---|
| 310 | } |
---|
| 311 | |
---|
| 312 | // Step 3 - Monitor this access |
---|
| 313 | $base = script_basename(); |
---|
| 314 | // Picture page |
---|
| 315 | if (isset($page['image_id']) and $base == 'picture') { |
---|
| 316 | whois_online_track($visit['elm_ids'], $page['image_id'],$visit['dates']); |
---|
| 317 | if (isset($page['tags'])) |
---|
| 318 | whois_online_track($visit['tag_ids'], $page['image_id'],$visit['dates']); |
---|
| 319 | if (isset($page['search'])) |
---|
| 320 | whois_online_track($visit['sch_ids'], $page['image_id'],$visit['dates']); |
---|
| 321 | $visit['elm_hits']++; |
---|
| 322 | $global['elm_hits']++; |
---|
| 323 | } |
---|
| 324 | // Category page |
---|
| 325 | if (isset($page['category']['id']) and $base == 'index') |
---|
| 326 | whois_online_track($visit['cat_ids'], $page['category']['id'],$visit['dates']); |
---|
| 327 | // Page index |
---|
| 328 | if (!isset($page['category']['id']) and !isset($page['image_id'])) |
---|
| 329 | whois_online_track($visit['cat_ids'], '' ,$visit['dates']); |
---|
| 330 | $visit['pag_hits']++; |
---|
| 331 | $global['pag_hits']++; |
---|
| 332 | |
---|
| 333 | // Step 4 - Identify current range |
---|
| 334 | $current = floor(time() / 60); // minutes for Unix time origin |
---|
| 335 | $minute = $current % 10; // (0-9) current minute |
---|
| 336 | $five = floor($current / 5); // (0-11) last 5 minutes range |
---|
| 337 | $hour = floor($current / 60); // (0-11) last hours |
---|
| 338 | $day = floor($current / 1440); // (0-13) last days |
---|
| 339 | if (isset($global['elm_ids'][10])) $old = $global['elm_ids'][10]; // previous minute (last one, or maybe 60 minutes ago or more). |
---|
| 340 | else $old = $current; /* Only the first time or prevent wrong changes */ |
---|
| 341 | |
---|
| 342 | // Step 5 - Hits by range |
---|
| 343 | if ($current != $old) { |
---|
| 344 | $global['elm_ids'][11] = $global['elm_ids'][$minute]; |
---|
| 345 | $raz = min($current-$old, 10); |
---|
| 346 | // 5.1 - $global['elm_ids'] ( hits by minute range ) |
---|
| 347 | for ($i=0;$i<$raz;$i++) { |
---|
| 348 | $global['elm_ids'][($minute+10-$i)%10] = 0; |
---|
| 349 | } |
---|
| 350 | // 5.2 - $global['cat_ids'] ( hits by 5 minutes range ) |
---|
| 351 | if (isset($global['cat_ids'][12])) $oldfive = $global['cat_ids'][12]; |
---|
| 352 | else $oldfive = floor($old / 5); |
---|
| 353 | $raz = min($five - $oldfive, 12); |
---|
| 354 | for ($i=0;$i<$raz;$i++) { |
---|
| 355 | $global['cat_ids'][($five+12-$i)%12] = 0; // Reset in backside |
---|
| 356 | } |
---|
| 357 | // 5.3 - $global['tag_ids'] (hits by hours ) |
---|
| 358 | if (count($global['tag_ids'])<25) $global['tag_ids'] = array_fill(0, 24, 0); |
---|
| 359 | if (isset($global['tag_ids'][24])) $oldhour = $global['tag_ids'][24]; |
---|
| 360 | else $oldhour = floor($old / 60); |
---|
| 361 | $raz = min($hour - $oldhour, 24); |
---|
| 362 | for ($i=0;$i<$raz;$i++) { |
---|
| 363 | $global['tag_ids'][($hour+24-$i)%24] = 0; // Reset in backside |
---|
| 364 | } |
---|
| 365 | // 5.4 - $global['sch_ids'] ( hits by days ) |
---|
| 366 | if (isset($global['sch_ids'][14])) $oldday = $global['sch_ids'][14]; |
---|
| 367 | else $oldday = floor($old / 1440); |
---|
| 368 | $raz = min($day - $oldday, 14); |
---|
| 369 | for ($i=0;$i<$raz;$i++) { |
---|
| 370 | $global['sch_ids'][($day+14-$i)%14] = 0; // Reset in backside |
---|
| 371 | } |
---|
| 372 | } |
---|
| 373 | $global['elm_ids'][$minute]++; |
---|
| 374 | $global['cat_ids'][$five%12]++; |
---|
| 375 | $global['tag_ids'][$hour%12]++; |
---|
| 376 | $global['sch_ids'][$day%14]++; |
---|
[3695] | 377 | // !!! WARNING !!! WARNING !!! WARNING !!! WARNING !!! |
---|
[3319] | 378 | $global['elm_ids'][10] = $current; // reference minute has changed |
---|
| 379 | $global['cat_ids'][12] = $five; |
---|
| 380 | $global['tag_ids'][24] = $hour; |
---|
| 381 | $global['sch_ids'][14] = $day; |
---|
[3695] | 382 | // !!! WARNING !!! WARNING !!! WARNING !!! WARNING !!! |
---|
[3319] | 383 | |
---|
[3695] | 384 | // 5.5 - Add in previous |
---|
| 385 | if (!isset($global['any_previous'])) { |
---|
| 386 | pwg_query('ALTER TABLE ' . WHOIS_ONLINE_TABLE . |
---|
| 387 | ' ADD `same_previous` VARCHAR( 256 ) NOT NULL DEFAULT \'\' AFTER `country`;'); |
---|
| 388 | pwg_query('ALTER TABLE ' . WHOIS_ONLINE_TABLE . |
---|
| 389 | ' ADD `any_previous` VARCHAR( 256 ) NOT NULL DEFAULT \'\' AFTER `country`;'); |
---|
| 390 | pwg_query('ALTER TABLE ' . WHOIS_ONLINE_TABLE . |
---|
| 391 | ' ADD `user_agent` VARCHAR( 160 ) NOT NULL DEFAULT \'\' AFTER `country`;'); |
---|
| 392 | } |
---|
| 393 | |
---|
| 394 | $antiaspi = array( |
---|
| 395 | 'diff' => '20 pages in 00:00:10' , // Banned for 20 access in 10 seconds or less |
---|
| 396 | 'same' => '15 pages in 00:00:30' , // Banned for 15 access on the same page in 30 seconds or less |
---|
| 397 | 'banned during' => '23:59:59' , // Banned time hh:mm:ss or any valid MySQL datetime expression 'YYYY-MM-DD HH:MM:SS' |
---|
| 398 | 'only guest' => true , // True, registred members won't be banned |
---|
| 399 | 'only picture' => false , // True, Check only on picture pages |
---|
| 400 | 'allowed ip' => array() // Allowed IP array (Bots, or your fixed IP) |
---|
| 401 | ); |
---|
| 402 | if (isset($conf['antiaspi'])) $antiaspi = array_merge($antiaspi, $conf['antiaspi']); |
---|
| 403 | |
---|
| 404 | // For AntiAspi follow ANY PREVIOUS access |
---|
| 405 | $access = '0:'; |
---|
| 406 | list($max_any, $maxtext) = explode(' pages in ', $antiaspi['diff']); |
---|
| 407 | $maxtime = whois_online_duration($maxtext); |
---|
| 408 | $prev=''; |
---|
| 409 | $previous = (isset($visit['any_previous'])) ? explode(' ', $visit['any_previous']):Array(); |
---|
| 410 | foreach ($previous as $v) { |
---|
| 411 | $old = explode(':', $v); |
---|
| 412 | $old[0] += $visit['delay']; |
---|
| 413 | if ($old[0]<$maxtime) $prev .= $old[0].': '; |
---|
| 414 | } |
---|
| 415 | $prev = $access . ' ' . $prev; |
---|
| 416 | $prev = substr($prev, 0, -2); |
---|
| 417 | $visit['any_previous'] = $prev; |
---|
| 418 | // For AntiAspi follow ANY SAME PICTURE access |
---|
| 419 | $access = '0:'; |
---|
| 420 | $same_elem = (isset($page['image_id'])) ? $page['image_id']:'0'; |
---|
| 421 | list($max_same, $maxtext) = explode(' pages in ', $antiaspi['same']); |
---|
| 422 | $maxtime = whois_online_duration($maxtext); |
---|
| 423 | $access .= $same_elem . ':'; |
---|
| 424 | $prev=''; |
---|
| 425 | $previous = (isset($visit['same_previous'])) ? explode(' ', $visit['same_previous']):Array(); |
---|
| 426 | foreach ($previous as $v) { |
---|
| 427 | $old = explode(':', $v); |
---|
| 428 | $old[0] += $visit['delay']; |
---|
| 429 | if ($old[0]<$maxtime and $old[1]==$same_elem) $prev .= $old[0].':'.$old[1].': '; |
---|
| 430 | } |
---|
| 431 | $prev = $access . ' ' . $prev; |
---|
| 432 | $prev = substr($prev, 0, -2); |
---|
| 433 | $visit['same_previous'] = $prev; |
---|
| 434 | |
---|
| 435 | // Check limits of $visit['any_previous'] and $visit['same_previous'] |
---|
| 436 | // by 256 characters |
---|
| 437 | // by $max_any and by $max_same |
---|
| 438 | while (strlen($visit['any_previous'])>256) { |
---|
| 439 | $previous = explode(' ',$visit['any_previous']); |
---|
| 440 | $oldest = array_pop($previous); |
---|
| 441 | $visit['any_previous'] = implode(' ', $previous); |
---|
| 442 | } |
---|
| 443 | $ctr_any = count(explode(' ',$visit['any_previous'])); |
---|
| 444 | while (strlen($visit['same_previous'])>256) { |
---|
| 445 | $previous = explode(' ',$visit['same_previous']); |
---|
| 446 | $oldest = array_pop($previous); |
---|
| 447 | $visit['same_previous'] = implode(' ', $previous); |
---|
| 448 | } |
---|
| 449 | $ctr_same = count(explode(' ',$visit['same_previous'])); |
---|
| 450 | |
---|
| 451 | $visit['user_agent'] = $_SERVER['HTTP_USER_AGENT']; |
---|
| 452 | $Vip =& $visit['IP']; |
---|
| 453 | $visit['Allowed_SE'] = false; |
---|
| 454 | if (!empty($antiaspi['allowed ip'])) |
---|
| 455 | { |
---|
| 456 | $allowed_ips = str_replace(array('.', '%'), array('\.', '.*?'), $antiaspi['allowed ip']); |
---|
| 457 | foreach ($allowed_ips as $ip) |
---|
| 458 | { |
---|
| 459 | if (preg_match("#" . $ip . "#", $Vip)) { $visit['Allowed_SE'] = true; break; } |
---|
| 460 | } |
---|
| 461 | } |
---|
| 462 | |
---|
[3319] | 463 | // Step 6 - Update (on Conf_Update and trace) and send trace to determine global tracing or not |
---|
| 464 | $dtrace = 0; |
---|
| 465 | if ($user['status'] == 'admin') $dtrace += $conf_whois['Administrators']; |
---|
| 466 | elseif ($user['status'] == 'webmaster') $dtrace += $conf_whois['Webmasters']; |
---|
| 467 | else $dtrace = 2; |
---|
| 468 | if ($conf['Whois Online Update'] and $dtrace > 0) whois_online_update($global, $visit, $dtrace); |
---|
| 469 | |
---|
| 470 | // Step 7 - Find your recent visits (These image_ids are presumely authorized) |
---|
| 471 | $my_ids = $visit['elm_ids']; |
---|
| 472 | sort($my_ids); |
---|
| 473 | unset($my_ids[0]); |
---|
| 474 | $url_visited = (count($my_ids)>0) ? make_index_url(array('list' => $my_ids)).'&review':''; |
---|
| 475 | |
---|
| 476 | // Step 8 - Guest count and Member list |
---|
| 477 | $h24 = 0; $h1 = 0; $guest = 0; |
---|
| 478 | $list = array(); |
---|
| 479 | $elm = array(); $excl = array(); // Get images_ids from all and from the current visitor |
---|
| 480 | foreach ($online as $k => $monit) |
---|
| 481 | { |
---|
| 482 | if ($user['id']==$monit['user_id']) $excl = $monit['elm_ids']; |
---|
| 483 | else $elm = array_merge($elm, $monit['elm_ids']); |
---|
| 484 | if ($monit['delay'] <= (24*3600)) $h24++; |
---|
| 485 | if ($monit['delay'] <= 3600) $h1++; |
---|
| 486 | // Less than 5 minutes: users are considered as still online... |
---|
| 487 | if ($monit['delay'] <= 300) { |
---|
| 488 | if ($monit['user_id'] == $conf['guest_id']) $guest++; |
---|
| 489 | else $list[] = $monit['username']; |
---|
| 490 | } |
---|
| 491 | } |
---|
| 492 | // The first (and current) access are not recorded in $online |
---|
| 493 | // As visitor you are not expecting your access to be already counted: Ok that the case |
---|
| 494 | // but you are expecting to see you as a member in the member list if you are: and there it is |
---|
| 495 | if ($visit['user_id'] != $conf['guest_id'] ) $list[] = $visit['username']; |
---|
| 496 | $list = array_unique($list); |
---|
| 497 | |
---|
| 498 | if (count($list) > $conf_whois['Users']['max']) { |
---|
| 499 | $conf_whois['Users']['max'] = count($list); |
---|
| 500 | $conf_whois['Users']['When'] = time(); |
---|
| 501 | $conf['Whois Online'] = serialize($conf_whois); |
---|
| 502 | pwg_query('REPLACE INTO ' . CONFIG_TABLE . " (param,value,comment) |
---|
| 503 | VALUES ('Whois Online','". $conf['Whois Online'] ."','Whois Online configuration');"); |
---|
| 504 | } |
---|
| 505 | |
---|
| 506 | // Step 9 - Review pictures viewed by others (all images except yours) |
---|
| 507 | $elm = array_diff( array_unique($elm), $excl ); |
---|
| 508 | shuffle($elm); |
---|
| 509 | $elm = array_slice($elm,0,($conf['top_number']+50)); |
---|
| 510 | sort($elm); |
---|
| 511 | array_shift($elm); |
---|
| 512 | $elm[] = 0; // Check if authorized pictures |
---|
| 513 | $query = 'SELECT DISTINCT(image_id) |
---|
| 514 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
| 515 | INNER JOIN '.IMAGES_TABLE.' ON id = image_id |
---|
| 516 | WHERE image_id IN ('.implode(',', $elm ).') |
---|
| 517 | '.get_sql_condition_FandF( |
---|
| 518 | array( |
---|
| 519 | 'forbidden_categories' => 'category_id', |
---|
| 520 | 'visible_categories' => 'category_id', |
---|
| 521 | 'visible_images' => 'id' |
---|
| 522 | ), "\n AND") . ';'; |
---|
| 523 | $ids = array_from_query($query, 'image_id'); |
---|
| 524 | shuffle($ids); |
---|
| 525 | $ids = array_slice($ids, 0, $conf['top_number']); // Keep some |
---|
| 526 | $url_someothers = (isset($ids[0])) ? make_index_url(array('list' => $ids)).'&others':''; |
---|
| 527 | |
---|
| 528 | // Random page title change |
---|
| 529 | $url_type = pwg_get_session_var('whois_url_type', ''); /* previous review or others */ |
---|
| 530 | $list_ids = pwg_get_session_var('whois_list_ids', ''); /* previous list/xx,yy,zz, ... */ |
---|
| 531 | $list_section = (isset($page['section']) and $page['section'] == 'list') ? true:false; |
---|
| 532 | $same_ids = (isset($page['list']) and $page['list'] == $list_ids) ? true:false; |
---|
| 533 | if ($list_section and isset($_GET['review'])) { |
---|
| 534 | $url_type = 'Whois_review'; |
---|
| 535 | $same_ids = true; |
---|
| 536 | } |
---|
| 537 | if ($list_section and isset($_GET['others'])) { |
---|
| 538 | $url_type = 'Whois_others'; |
---|
| 539 | $same_ids = true; |
---|
| 540 | } |
---|
| 541 | if ($list_section and $same_ids and isset($page['list'])) $list_ids = $page['list']; |
---|
| 542 | else $url_type = ''; // Not the same list |
---|
| 543 | pwg_set_session_var('whois_list_ids', $list_ids); |
---|
| 544 | pwg_set_session_var('whois_url_type', $url_type); |
---|
| 545 | if ($url_type != '' and $list_section) |
---|
| 546 | $page['title'] = '<a href="'.duplicate_index_url(array('start'=>0)).'">' |
---|
| 547 | .l10n($url_type).'</a>'; |
---|
| 548 | |
---|
| 549 | |
---|
| 550 | // Step 10 - Prepare data to display |
---|
| 551 | $yesterday = ($day+13) % 14; |
---|
| 552 | $template->assign('Whois', array( |
---|
| 553 | 'Total' => $global['pag_hits'], |
---|
| 554 | 'Image' => $global['elm_hits'], |
---|
| 555 | 'Other' => ($global['pag_hits']-$global['elm_hits']), |
---|
| 556 | 'Current_minute' => $global['elm_ids'][$minute], |
---|
| 557 | 'Previous_minute' => $global['elm_ids'][($minute+9) % 10], |
---|
| 558 | 'Current_5mins' => $global['elm_ids'][$minute] |
---|
| 559 | + $global['elm_ids'][($minute+9) % 10] |
---|
| 560 | + $global['elm_ids'][($minute+8) % 10] |
---|
| 561 | + $global['elm_ids'][($minute+7) % 10] |
---|
| 562 | + $global['elm_ids'][($minute+6) % 10], |
---|
| 563 | 'Current_10mins' => array_sum(array_slice($global['elm_ids'],0,10)), |
---|
| 564 | 'Current_hour' => array_sum(array_slice($global['cat_ids'],0,12)), |
---|
| 565 | 'Current_24h' => array_sum(array_slice($global['tag_ids'],0,24)), |
---|
| 566 | 'Yesterday' => $global['sch_ids'][$yesterday], |
---|
| 567 | 'Users_Last_day' => $h24, |
---|
| 568 | 'Users_Last_hour' => $h1, |
---|
| 569 | 'Guests' => $guest, |
---|
| 570 | 'Online' => $list, |
---|
| 571 | 'Review_url' => $url_someothers, |
---|
| 572 | 'Their_ids' => $ids, |
---|
| 573 | 'Seen_url' => $url_visited, |
---|
| 574 | 'My_ids' => $my_ids, |
---|
| 575 | 'Slideshow' => isset($_GET['slideshow']) ? true:false, |
---|
| 576 | )); |
---|
| 577 | $template->assign('Online', $global ); |
---|
| 578 | |
---|
| 579 | pwg_debug('end Online_management'); |
---|
| 580 | } |
---|
| 581 | ?> |
---|