-
Notifications
You must be signed in to change notification settings - Fork 0
/
entity_metrics.module
142 lines (130 loc) · 4.36 KB
/
entity_metrics.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
/**
* @file
* Contains entity_metrics.module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\StreamWrapper\StreamWrapperManager;
use GuzzleHttp\Client;
/**
* Implements hook_help().
*/
function entity_metrics_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the entity_metrics module.
case 'help.page.entity_metrics':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('My Awesome Module') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_preprocess_HOOK() for node templates.
*/
function entity_metrics_preprocess_node(&$variables) {
// Check if the current page is a node view.
if (\Drupal::routeMatch()->getRouteName() === 'entity.node.canonical') {
$variables['#attached']['library'][] = 'entity_metrics/record';
}
}
/**
* Implements hook_file_download().
*/
function entity_metrics_file_download($uri) {
$scheme = StreamWrapperManager::getScheme($uri);
if ($scheme == 'fedora') {
$ip = \Drupal::requestStack()->getCurrentRequest()->getClientIp();
$first_octet = substr($ip, 0, 3);
// Do not record metrics on internal networking requests.
if (in_array($first_octet, ['127', '172', '192'])) {
return NULL;
}
$mid = \Drupal::database()->query('SELECT COALESCE(ma.entity_id, md.entity_id, mf.entity_id, mi.entity_id, mv.entity_id) AS mid from file_managed f
LEFT JOIN media__field_media_audio_file ma ON field_media_audio_file_target_id = f.fid
LEFT JOIN media__field_media_document md ON field_media_document_target_id = f.fid
LEFT JOIN media__field_media_file mf ON field_media_file_target_id = f.fid
LEFT JOIN media__field_media_image mi ON field_media_image_target_id = f.fid
LEFT JOIN media__field_media_video_file mv ON field_media_video_file_target_id = f.fid
WHERE uri = :uri', [
':uri' => $uri,
])->fetchField();
if ($mid) {
$config = \Drupal::config('entity_metrics.settings');
$cookieName = $config && $config->get('cookie');
\Drupal::database()->insert('entity_metrics_data')
->fields([
'entity_type' => 'media',
'entity_id' => $mid,
'session_id' => \Drupal::service('session_manager')->getId(),
'timestamp' => \Drupal::time()->getCurrentTime(),
'ip_address' => $ip,
'cookie_set' => !empty($cookieName) && isset($_COOKIE[$cookieName]) && $_COOKIE[$cookieName] === '1' ? '1' : '0',
])
->execute();
}
}
return NULL;
}
/**
* Implements hook_cron().
*/
function entity_metrics_cron($limit = 500) {
if (!is_int($limit) || $limit < 1) {
$limit = 500;
}
$visits = \Drupal::database()->query("SELECT ip_address
FROM {entity_metrics_data}
WHERE ip_address IS NOT NULL
GROUP BY ip_address
LIMIT $limit");
foreach ($visits as $visit) {
try {
$client = new Client();
$json = $client->request('GET', 'https://api.ip2location.io/', [
'query' => [
'key' => Drupal::service('key.repository')->getKey('ip2location')->getKeyValue(),
'ip' => $visit->ip_address,
],
])->getBody()->getContents();
$location = json_decode($json, TRUE);
$data = [
'country' => $location['country_code'],
'region' => $location['region_name'],
'city' => $location['city_name'],
'latitude' => $location['latitude'],
'longitude' => $location['longitude'],
];
}
catch (Exception $e) {
$data = [
'country' => '',
'region' => '',
'city' => '',
'latitude' => 0,
'longitude' => 0,
];
}
// Perform the merge query.
\Drupal::database()->merge('entity_metrics_regions')
->key($data)
->fields($data)
->execute();
$d_args = [];
foreach ($data as $key => $value) {
$key = ':' . $key;
$d_args[$key] = $value;
}
$id = \Drupal::database()->query('SELECT id FROM {entity_metrics_regions}
WHERE country = :country
AND region = :region
AND city = :city
AND latitude = :latitude
AND longitude = :longitude', $d_args)->fetchField();
\Drupal::database()->query('UPDATE {entity_metrics_data} SET region_id = :id, ip_address = NULL WHERE ip_address = :ip', [
':id' => $id,
':ip' => $visit->ip_address,
]);
}
}