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