Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Gets counter ID for the current site from Yandex.Metrics.
- */
- function yandex_metrics_get_counter_for_current_site() {
- $counter_id = variable_get('yandex_metrics_counter_id', '');
- if (!empty($counter_id)) {
- return $counter_id;
- }
- $result = yandex_metrics_retreive_data('/counters', array('field' => 'mirrors'));
- if (isset($result->error)) {
- watchdog('yandex_metrics', 'Counters request seems to be fail, due to "%error".', array('%error' => $result->code . ' ' . $result->error), WATCHDOG_WARNING);
- return;
- }
- $counters = json_decode($result->data);
- $current_host = $_SERVER['HTTP_HOST'];
- // Try to decode national domain.
- $decoded_domain = _yandex_metrics_idna_decode($current_host);
- if ($decoded_domain != FALSE && $decoded_domain != $current_host) {
- $current_host = $decoded_domain;
- }
- // list of domains, mirrors for current counter;
- $available_domains = array();
- foreach ($counters->counters as $key => $counter) {
- if ($counter->site == $current_host) {
- variable_set('yandex_metrics_counter_id', $counter->id);
- return $counter->id;
- }
- else {
- $available_domains[] = $counter->site;
- }
- // If current host is equal of any site mirror.
- if (isset($counter->mirrors)) {
- if(in_array($current_host, $counter->mirrors)) {
- variable_set('yandex_metrics_counter_id', $counter->id);
- return $counter->id;
- }
- else {
- $available_domains = array_merge($available_domains, $counter->mirrors);
- }
- }
- }
- if (!empty($available_domains)) {
- watchdog('yandex_metrics', 'No legal domains or mirrors for this host. Current host: "%host"; available domains: "%available_domains".', array('%error' => $current_host, "%available_domains" => implode(", ", $available_domains) ), WATCHDOG_WARNING);
- }
- return FALSE;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement