View source
<?php
module_load_include('php', 'shareaholic', 'lib/social-share-counts/drupal_http');
module_load_include('php', 'shareaholic', 'lib/social-share-counts/seq_share_count');
class ShareaholicUtilities {
const MODULE_VERSION = '7.x-3.31';
const URL = 'https://www.shareaholic.com';
const API_URL = 'https://web.shareaholic.com';
const CM_API_URL = 'https://cm-web.shareaholic.com';
public static function has_accepted_terms_of_service() {
return variable_get('shareaholic_has_accepted_tos');
}
public static function accept_terms_of_service() {
variable_set('shareaholic_has_accepted_tos', true);
ShareaholicUtilities::log_event('AcceptedToS');
}
private static function defaults() {
return array(
'disable_internal_share_counts_api' => 'on',
'api_key' => '',
'verification_key' => '',
);
}
public static function get_settings() {
return variable_get('shareaholic_settings', self::defaults());
}
public static function get_option($option) {
$settings = self::get_settings();
return isset($settings[$option]) ? $settings[$option] : array();
}
public static function update_options($array) {
$old_settings = self::get_settings();
$new_settings = self::array_merge_recursive_distinct($old_settings, $array);
variable_set('shareaholic_settings', $new_settings);
}
public static function destroy_settings() {
ShareaholicUtilities::delete_api_key();
variable_del('shareaholic_settings');
}
public static function set_settings($settings) {
variable_set('shareaholic_settings', $settings);
}
public static function site_url() {
return preg_replace('/https?:\\/\\//', '', $GLOBALS['base_url']);
}
public static function site_name() {
return variable_get('site_name', $GLOBALS['base_url']);
}
public static function site_language() {
$language_id_map = array(
"ar" => 1,
"bg" => 2,
"zh-hans" => 3,
"zh-hant" => 4,
"hr" => 5,
"cs" => 6,
"da" => 7,
"nl" => 8,
"en" => 9,
"et" => 10,
"fi" => 11,
"fr" => 12,
"de" => 13,
"el" => 14,
"he" => 15,
"hu" => 16,
"id" => 17,
"it" => 18,
"ja" => 19,
"ko" => 20,
"lv" => 21,
"lt" => 22,
"nn" => 23,
"pl" => 24,
"pt-pt" => 25,
"ro" => 26,
"ru" => 27,
"sr" => 28,
"sk" => 29,
"sl" => 30,
"es" => 31,
"sv" => 32,
"th" => 33,
"tr" => 34,
"uk" => 35,
"vi" => 36,
);
$language = $GLOBALS['language']->language;
return isset($language_id_map[$language]) ? $language_id_map[$language] : NULL;
}
public static function get_or_create_api_key() {
$api_key = self::get_option('api_key');
if ($api_key && strlen($api_key) > 30) {
return $api_key;
}
$old_settings = self::get_settings();
self::destroy_settings();
if (isset($old_settings['share_counts_connect_check'])) {
self::update_options(array(
'share_counts_connect_check' => $old_settings['share_counts_connect_check'],
));
}
$verification_key = md5(mt_rand());
$page_types = self::page_types();
$turned_on_recommendations_locations = self::get_default_rec_on_locations();
$turned_off_recommendations_locations = self::get_default_rec_off_locations();
$turned_on_share_buttons_locations = self::get_default_sb_on_locations();
$turned_off_share_buttons_locations = self::get_default_sb_off_locations();
$share_buttons_attributes = array_merge($turned_on_share_buttons_locations, $turned_off_share_buttons_locations);
$recommendations_attributes = array_merge($turned_on_recommendations_locations, $turned_off_recommendations_locations);
$post_data = array(
'configuration_publisher' => array(
'verification_key' => $verification_key,
'site_name' => self::site_name(),
'domain' => self::site_url(),
'platform_id' => '2',
'language_id' => self::site_language(),
'shortener' => 'shrlc',
'recommendations_attributes' => array(
'locations_attributes' => $recommendations_attributes,
),
'share_buttons_attributes' => array(
'locations_attributes' => $share_buttons_attributes,
),
),
);
$response = drupal_http_request(self::API_URL . '/publisher_tools/anonymous', array(
'method' => 'POST',
'headers' => array(
'Content-Type' => 'application/json',
),
'data' => json_encode($post_data),
));
if (self::has_bad_response($response, 'FailedToCreateApiKey', true)) {
return NULL;
}
$response = (array) $response;
$json_response = json_decode($response['data'], true);
self::update_options(array(
'version' => self::get_version(),
'api_key' => $json_response['api_key'],
'verification_key' => $verification_key,
'location_name_ids' => $json_response['location_name_ids'],
));
if (isset($json_response['location_name_ids']) && is_array($json_response['location_name_ids']) && isset($json_response['location_name_ids']['recommendations']) && isset($json_response['location_name_ids']['share_buttons'])) {
self::set_default_location_settings($json_response['location_name_ids']);
ShareaholicContentManager::single_domain_worker();
}
else {
ShareaholicUtilities::log_event('FailedToCreateApiKey', array(
'reason' => 'no location name ids the response was: ' . $response['data'],
));
}
}
public static function get_default_sb_on_locations() {
$page_types = self::page_types();
$turned_on_share_buttons_locations = array();
foreach ($page_types as $key => $page_type) {
$page_type_name = $page_type->type;
$turned_on_share_buttons_locations[] = array(
'name' => $page_type_name . '_below_content',
);
}
return $turned_on_share_buttons_locations;
}
public static function get_default_sb_off_locations() {
$page_types = self::page_types();
$turned_off_share_buttons_locations = array();
foreach ($page_types as $key => $page_type) {
$page_type_name = $page_type->type;
$turned_off_share_buttons_locations[] = array(
'name' => $page_type_name . '_above_content',
);
}
return $turned_off_share_buttons_locations;
}
public static function get_default_rec_on_locations() {
$page_types = self::page_types();
$turned_on_recommendations_locations = array();
foreach ($page_types as $key => $page_type) {
$page_type_name = $page_type->type;
if ($page_type_name === 'article' || $page_type_name === 'page') {
$turned_on_recommendations_locations[] = array(
'name' => $page_type_name . '_below_content',
);
}
}
return $turned_on_recommendations_locations;
}
public static function get_default_rec_off_locations() {
$page_types = self::page_types();
$turned_off_recommendations_locations = array();
foreach ($page_types as $key => $page_type) {
$page_type_name = $page_type->type;
if ($page_type_name !== 'article' && $page_type_name !== 'page') {
$turned_off_recommendations_locations[] = array(
'name' => $page_type_name . '_below_content',
);
}
}
return $turned_off_recommendations_locations;
}
public static function set_default_location_settings($location_name_ids) {
$turned_on_share_buttons_locations = self::get_default_sb_on_locations();
$turned_off_share_buttons_locations = self::get_default_sb_off_locations();
$turned_on_recommendations_locations = self::get_default_rec_on_locations();
$turned_off_recommendations_locations = self::get_default_rec_off_locations();
$turned_on_share_buttons_keys = array();
foreach ($turned_on_share_buttons_locations as $loc) {
$turned_on_share_buttons_keys[] = $loc['name'];
}
$turned_on_recommendations_keys = array();
foreach ($turned_on_recommendations_locations as $loc) {
$turned_on_recommendations_keys[] = $loc['name'];
}
$turned_off_share_buttons_keys = array();
foreach ($turned_off_share_buttons_locations as $loc) {
$turned_off_share_buttons_keys[] = $loc['name'];
}
$turned_off_recommendations_keys = array();
foreach ($turned_off_recommendations_locations as $loc) {
$turned_off_recommendations_keys[] = $loc['name'];
}
$turn_on = array(
'share_buttons' => self::associative_array_slice($location_name_ids['share_buttons'], $turned_on_share_buttons_keys),
'recommendations' => self::associative_array_slice($location_name_ids['recommendations'], $turned_on_recommendations_keys),
);
$turn_off = array(
'share_buttons' => self::associative_array_slice($location_name_ids['share_buttons'], $turned_off_share_buttons_keys),
'recommendations' => self::associative_array_slice($location_name_ids['recommendations'], $turned_off_recommendations_keys),
);
ShareaholicUtilities::turn_on_locations($turn_on, $turn_off);
}
public static function reset_settings() {
$settings = self::get_settings();
$api_key = self::get_option('api_key');
$response = drupal_http_request(self::API_URL . '/publisher_tools/' . $api_key . '/reset/', array(
'method' => 'POST',
'headers' => array(
'Content-Type' => 'application/json',
),
'data' => json_encode($settings),
));
if (isset($settings['location_name_ids']) && is_array($settings['location_name_ids'])) {
self::set_default_location_settings($settings['location_name_ids']);
}
}
public static function delete_api_key() {
$payload = array(
'site_id' => self::get_option('api_key'),
'verification_key' => self::get_option('verification_key'),
);
$response = drupal_http_request(self::API_URL . '/integrations/plugin/delete', array(
'method' => 'POST',
'headers' => array(
'Content-Type' => 'application/json',
),
'data' => json_encode($payload),
));
}
public static function has_bad_response($response, $type, $json_parse = FALSE) {
if (!$response) {
ShareaholicUtilities::log_event($type, array(
'reason' => 'there was no response',
));
return true;
}
$response = (array) $response;
if (isset($response['error'])) {
$error_message = print_r($response['error'], true);
ShareaholicUtilities::log_event($type, array(
'reason' => 'there was an error: ' . $error_message,
));
return true;
}
if (!preg_match('/20*/', $response['code'])) {
ShareaholicUtilities::log_event($type, array(
'reason' => 'the server responded with code ' . $response['code'],
));
return true;
}
if ($json_parse && json_decode($response['data']) === NULL) {
ShareaholicUtilities::log_event($type, array(
'reason' => 'could not parse JSON. The response was: ' . $response['data'],
));
return true;
}
return false;
}
public static function log($message) {
if (defined('SHAREAHOLIC_DEBUG') && SHAREAHOLIC_DEBUG) {
watchdog('Shareaholic', print_r($message, true));
}
}
public static function array_merge_recursive_distinct(array &$array1, array &$array2) {
$merged = $array1;
foreach ($array2 as $key => &$value) {
if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
if (empty($value)) {
$merged[$key] = array();
}
else {
$merged[$key] = self::array_merge_recursive_distinct($merged[$key], $value);
}
}
else {
$merged[$key] = $value;
}
}
return $merged;
}
public static function asset_url($asset) {
$env = self::get_env();
if ($env === 'development') {
return 'http://spreadaholic.com:8080/' . $asset;
}
elseif ($env === 'staging') {
return '//d2062rwknz205x.cloudfront.net/' . $asset;
}
else {
return '//cdn.shareaholic.net/' . $asset;
}
}
public static function get_env() {
if (preg_match('/spreadaholic/', self::URL)) {
return 'development';
}
elseif (preg_match('/stageaholic/', self::URL)) {
return 'staging';
}
else {
return 'production';
}
}
public static function has_tos_and_apikey() {
return ShareaholicUtilities::has_accepted_terms_of_service() && ShareaholicUtilities::get_option('api_key');
}
public static function get_version() {
return self::MODULE_VERSION;
}
public static function set_version($version) {
self::update_options(array(
'version' => $version,
));
}
public static function is_admin_page() {
return path_is_admin(current_path());
}
public static function is_shareaholic_settings_page() {
return preg_match('/admin\\/config\\/shareaholic\\//', request_uri());
}
public static function clean_string($word) {
return trim(trim(strtolower(trim(htmlspecialchars(htmlspecialchars_decode($word), ENT_QUOTES))), ","));
}
public static function associative_array_slice($array) {
$keys = array_slice(func_get_args(), 1);
if (func_num_args() == 2 && is_array($keys[0])) {
$keys = $keys[0];
}
$result = array();
foreach ($keys as $key) {
$result[$key] = $array[$key];
}
return $result;
}
public static function turn_on_locations($array, $turn_off_array = array()) {
if (is_array($array)) {
foreach ($array as $app => $ids) {
if (is_array($ids)) {
foreach ($ids as $name => $id) {
self::update_options(array(
$app => array(
$name => 'on',
),
));
}
}
}
}
if (is_array($turn_off_array)) {
foreach ($turn_off_array as $app => $ids) {
if (is_array($ids)) {
foreach ($ids as $name => $id) {
self::update_options(array(
$app => array(
$name => 'off',
),
));
}
}
}
}
}
public static function page_types() {
$page_types = node_type_get_types();
$teaser = new stdClass();
$teaser->name = 'Teaser';
$teaser->type = 'teaser';
$page_types['shareaholic_custom_type'] = $teaser;
return $page_types;
}
public static function api_key_verified() {
$settings = self::get_settings();
if (isset($settings['api_key_verified']) && $settings['api_key_verified']) {
return true;
}
$api_key = $settings['api_key'];
if (!$api_key) {
return false;
}
$response = drupal_http_request(self::API_URL . '/publisher_tools/' . $api_key . '/verified', array(
'method' => 'GET',
));
if (self::has_bad_response($response, 'FailedApiKeyVerified')) {
return false;
}
$response = (array) $response;
$result = $response['data'];
if ($result == 'true') {
ShareaholicUtilities::update_options(array(
'api_key_verified' => true,
));
return true;
}
return false;
}
public static function clear_fb_opengraph($node) {
if ($node->status !== NODE_PUBLISHED) {
return;
}
$page_link = url('node/' . $node->nid, array(
'absolute' => TRUE,
));
if (isset($page_link)) {
$fb_graph_url = "https://graph.facebook.com/?id=" . urlencode($page_link) . "&scrape=true";
$options = array(
'method' => 'POST',
'timeout' => 5,
);
$result = drupal_http_request($fb_graph_url, $options);
}
}
public static function log_event($event_name = 'Default', $extra_params = false) {
$event_metadata = array(
'plugin_version' => self::get_version(),
'api_key' => self::get_option('api_key'),
'domain' => $GLOBALS['base_url'],
'language' => $GLOBALS['language']->language,
'stats' => self::get_stats(),
'diagnostics' => array(
'php_version' => phpversion(),
'drupal_version' => self::get_drupal_version(),
'theme' => variable_get('theme_default', $GLOBALS['theme']),
'active_plugins' => module_list(),
),
'features' => array(
'share_buttons' => self::get_option('share_buttons'),
'recommendations' => self::get_option('recommendations'),
),
);
if ($extra_params) {
$event_metadata = array_merge($event_metadata, $extra_params);
}
$event_api_url = self::API_URL . '/api/events';
$event_params = array(
'name' => "Drupal:" . $event_name,
'data' => json_encode($event_metadata),
);
$options = array(
'method' => 'POST',
'headers' => array(
'Content-Type' => 'application/json',
),
'body' => $event_params,
);
ShareaholicHttp::send($event_api_url, $options, true);
}
public static function total_comments() {
if (!db_table_exists('comment')) {
return array();
}
return db_query("SELECT count(cid) FROM {comment}")
->fetchField();
}
public static function get_stats() {
$stats = array();
$result = db_query("SELECT type, count(*) as count FROM {node} GROUP BY type");
foreach ($result as $record) {
$stats[$record->type . '_total'] = $record->count;
}
$stats['comments_total'] = self::total_comments();
return $stats;
}
public static function get_drupal_version() {
if (defined('VERSION')) {
return VERSION;
}
return '7';
}
public static function connectivity_check() {
$health_check_url = self::API_URL . "/haproxy_health_check";
$response = ShareaholicHttp::send($health_check_url, array(
'method' => 'GET',
), true);
if (is_array($response) && array_key_exists('body', $response)) {
$response_code = $response['response']['code'];
if ($response_code == "200") {
return "SUCCESS";
}
else {
return "FAIL";
}
}
else {
return "FAIL";
}
}
public static function load_template($template, $vars = array()) {
$template_path = SHAREAHOLIC_DIR . '/templates/' . $template . '.php';
extract($vars);
require $template_path;
}
public static function share_counts_api_connectivity_check() {
$share_counts_connect_check = self::get_option('share_counts_connect_check');
if (isset($share_counts_connect_check) && $share_counts_connect_check == 'SUCCESS') {
return $share_counts_connect_check;
}
$services_config = ShareaholicSeqShareCount::get_services_config();
$services = array_keys($services_config);
$param_string = implode('&services[]=', $services);
$share_counts_api_url = url('shareaholic/api/share_counts/v1', array(
'absolute' => TRUE,
)) . '?action=shareaholic_share_counts_api&url=https%3A%2F%2Fwww.google.com%2F&services[]=' . $param_string;
$cache_key = 'share_counts_api_connectivity_check';
$response = ShareaholicCache::get($cache_key);
if (!$response) {
$response = ShareaholicHttp::send($share_counts_api_url, array(
'method' => 'GET',
), true);
}
$response_status = self::get_share_counts_api_status($response);
if (empty($share_counts_connect_check) && $response_status == 'FAIL') {
self::update_options(array(
'disable_internal_share_counts_api' => 'on',
));
}
if ($response_status == 'SUCCESS') {
ShareaholicCache::set($cache_key, $response, SHARE_COUNTS_CHECK_CACHE_LENGTH);
}
self::update_options(array(
'share_counts_connect_check' => $response_status,
));
return $response_status;
}
public static function get_share_counts_api_status($response) {
if (!$response || !isset($response['body'])) {
return 'FAIL';
}
$response['body'] = json_decode($response['body'], TRUE);
if (!is_array($response['body'])) {
return 'FAIL';
}
$has_majority_services = count(array_keys($response['body']['data'])) >= 5 ? true : false;
$has_important_services = true;
foreach (array(
'facebook',
'pinterest',
) as $service) {
if (!isset($response['body']['data'][$service]) || !is_numeric($response['body']['data'][$service])) {
$has_important_services = false;
}
}
if (!$has_majority_services || !$has_important_services) {
return 'FAIL';
}
return 'SUCCESS';
}
public static function get_host() {
$parse = parse_url($GLOBALS['base_url']);
return $parse['host'];
}
}