Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Highlight duplicated IP addresses #1

Merged
merged 4 commits into from
Sep 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Latest users plugin to [Question2Answer](http://question2answer.org/)

Plugin add pages with latest registered and latest logged users with action IP.
Plugin add pages with latest registered and latest logged users with action IP. Plugin highlights duplicated IP addresses, so you can simply detect multi-accounts.

## Installation

Expand All @@ -19,4 +19,4 @@ After you enable pages, links show in `Users` page in submenu. Addresses to this
---

**Important notice about code!**
This code don't respect PHP PSR-2 standard, because Question2Answer unfortunately too don't respect this. Also, I couldn't design classes and functions in my own way, because most of them impose Question2Answer script.
This code don't respect PHP PSR-2 standard, because Question2Answer unfortunately too don't respect this. Also, I couldn't design classes and functions in my own way, because most of them impose Question2Answer script.
51 changes: 46 additions & 5 deletions latest-users-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ public function process_request($request)
.lastest-center {
text-align: center;
}

.duplicated-ip,
.duplicated-ip:hover,
.duplicated-ip:visited {
color: #FF0000;
}

</style>';

$sql = '';
Expand All @@ -51,23 +58,40 @@ public function process_request($request)
if (!empty($sql)) {
$users = qa_db_read_all_assoc(qa_db_query_sub($sql, $limit));
$usershtml = qa_userids_handles_html($users);

$user_array_ip_key = '';

if ($request === 'users/latest-registered') {
$user_array_ip_key = 'createip';
} elseif ($request === 'users/latest-logged') {
$user_array_ip_key = 'loginip';
}

$duplicated_ips = $this->get_duplicates(array_column($users, $user_array_ip_key));

$usersrows = '';
foreach ($users as $user) {
if (QA_FINAL_EXTERNAL_USERS) {
$avatarhtml = qa_get_external_avatar_html($user['userid'], qa_opt('avatar_users_size'), true);
} else {
$avatarhtml = qa_get_user_avatar_html($user['flags'], $user['email'], $user['handle'], $user['avatarblobid'], $user['avatarwidth'], $user['avatarheight'], qa_opt('avatar_users_size'), true);
}
if ($request === 'users/latest-registered') {
$ip = qa_ip_anchor_html(long2ip($user['createip']));
} elseif ($request === 'users/latest-logged') {
$ip = qa_ip_anchor_html(long2ip($user['loginip']));

$ip_link_style = '';

foreach ($duplicated_ips as $duplicate) {
if ($user[$user_array_ip_key] === $duplicate) {
$ip_link_style = 'duplicated-ip';
}
}

$ip = long2ip($user[$user_array_ip_key]);
$ip_html = $this->get_ip_html($ip, $ip_link_style);

$usersrows .= '<tr>
<td class="qa-top-users-label">' . $avatarhtml . $usershtml[$user['userid']] . '</td>
<td class="lastest-center">' . ($request === 'users/latest-registered' ? $user['created'] : $user['loggedin']) . '</td>
<td class="lastest-center">' . $ip . '</td>
<td class="lastest-center">' . $ip_html . '</td>
</tr>';
}

Expand Down Expand Up @@ -125,4 +149,21 @@ function admin_form()

return $form;
}

private function get_duplicates($array)
{
$unique_values = array_unique($array);
$duplicate_values = array_diff_assoc($array, $unique_values);

return array_unique($duplicate_values);
}

private function get_ip_html($ip, $link_style = '')
{
$href = qa_path_html('ip/' . $ip);
$title = qa_lang_html_sub('main/ip_address_x', qa_html($ip));

$html = '<a href="' . $href . '" title="' . $title . '" class="qa-ip-link ' . $link_style . '">' . $ip . '</a>';
return $html;
}
}