public function top_count($top_id, $action) { /* GIF изображение */ header('Cache-Control: public'); header("Content-type: image/gif"); $error = false; $user_ip = $this->ip(); /* Выводим данные сайта */ $sql_array = array('SELECT' => 'r.top_id, r.top_online, r.top_hits, r.top_hosts, r.top_hits_all, r.top_hosts_all, r.top_icon_big, r.top_icon_small', 'FROM' => array(RATING_TABLE => 'r')); $sql_array['SELECT'] .= ', hi.top_ip'; $sql_array['LEFT_JOIN'][] = array('FROM' => array(RATING_HITS_TABLE => 'hi'), 'ON' => 'r.top_id = hi.top_id AND `top_ip` = "' . $user_ip . '"'); $sql_array['SELECT'] .= ', o.top_time'; $sql_array['LEFT_JOIN'][] = array('FROM' => array(RATING_ONLINE_TABLE => 'o'), 'ON' => 'r.top_id = o.top_id AND o.top_ip = "' . $user_ip . '"'); $sql_array['WHERE'] = "r.top_id = " . (int) $top_id; $sql = $this->db->sql_build_query('SELECT', $sql_array); $result = $this->db->sql_query($sql); $row = $this->db->sql_fetchrow($result); $this->db->sql_freeresult($result); /* Ошибка или бот поисковой */ if (!$row['top_id'] && !$this->user->data['is_bot']) { $error = true; } /* Не будем счиать ошибки */ if (!$error) { $sql_upd = array(); $ip_prov_id = 0; /* Пока cron не сделает сброс, ни каких подсчётов. */ if ($this->config['top_rating_last_gc'] > time() - $this->config['top_rating_gc']) { $sql = 'SELECT ip_prov_id FROM ' . RATING_IP_TABLE . " WHERE INET_ATON('" . $user_ip . "') BETWEEN `ip_start` AND `ip_finish`"; $result = $this->db->sql_query($sql); if ($iprow = $this->db->sql_fetchrow($result)) { $ip_prov_id = $iprow['ip_prov_id']; } $this->db->sql_freeresult($result); /* Если нет ip пользователя в базе, значит нужно его создать и подсчитать хосты */ if ($row['top_ip'] != $user_ip) { if ($row['top_hits'] > $row['top_hosts'] + $row['top_hosts']) { $sql = 'INSERT INTO ' . RATING_HITS_TABLE . " SET\n\t\t\t\t\t\t\t`top_id`\t\t= " . $row['top_id'] . ",\n\t\t\t\t\t\t\t`top_time`\t\t= '" . time() . "',\n\t\t\t\t\t\t\t`top_ip`\t\t= '" . $user_ip . "',\n\t\t\t\t\t\t\t`top_device`\t= '" . (string) $this->browser() . "',\n\t\t\t\t\t\t\t`top_prov_id`\t= " . $ip_prov_id . ",\n\t\t\t\t\t\t\t`top_count`\t\t= 1"; $this->db->sql_query($sql); $sql_upd += array('top_hosts' => $row['top_hosts'] + 1, 'top_hosts_all' => $row['top_hosts_all'] + 1); } } else { /* Иначе просто обновим количество визитов пользователя */ $sql = 'UPDATE ' . RATING_HITS_TABLE . ' SET `top_time` = "' . time() . '", `top_count` = (`top_count` + 1) WHERE top_id = ' . $row['top_id'] . ' AND top_ip = "' . $row['top_ip'] . '"'; $this->db->sql_query($sql); } /* Общее число и количество визитов */ if ($user_ip) { $sql_upd += array('top_hits' => $row['top_hits'] + 1, 'top_hits_all' => $row['top_hits_all'] + 1); } /* Время онлайн, индивидуально для каждого пользователя */ if (!$row['top_time']) { $sql = 'INSERT INTO ' . RATING_ONLINE_TABLE . " SET\n\t\t\t\t\t\t`top_id`\t\t= " . $row['top_id'] . ",\n\t\t\t\t\t\t`top_time`\t\t= '" . time() . "',\n\t\t\t\t\t\t`top_ip`\t\t= '" . $user_ip . "',\n\t\t\t\t\t\t`top_device`\t= '" . (string) $this->browser() . "',\n\t\t\t\t\t\t`top_prov_id`\t= " . $ip_prov_id; $this->db->sql_query($sql); $sql_upd += array('top_online' => $row['top_online'] + 1); } else { $this->db->sql_query('DELETE FROM ' . RATING_ONLINE_TABLE . ' WHERE top_id = ' . $row['top_id'] . ' AND top_time < ' . (time() - 360)); $sql = 'SELECT COUNT(*) AS top_time FROM ' . RATING_ONLINE_TABLE . ' WHERE top_id = ' . (int) $row['top_id']; $result = $this->db->sql_query($sql); $top_online = (int) $this->db->sql_fetchfield('top_time'); $this->db->sql_freeresult($result); $sql_upd += array('top_online' => $top_online); } /* Все изменеия фиксируем у данного сайта */ $sql = 'UPDATE ' . RATING_TABLE . ' SET ' . $this->db->sql_build_array('UPDATE', $sql_upd) . ' WHERE top_id = ' . $row['top_id']; $this->db->sql_query($sql); } /* Если создать и(или) вывести счётчик не получилось, то ошибка */ if (!($counter = $this->count_img($action, $row))) { $action = $row['top_hosts']; $top_id = $row['top_hits']; $error = true; } } /* Если ошибка дошла сюда, значит пора выводить изображение по умолчанию */ if ($error) { $this->default_img($action, $top_id); } /* Очищаем буфер, закрываем соединение и завершаем работу скрипта */ flush(); $this->db->sql_close(); exit; }