View source
<?php
define('LIVECHAT_VISIBILITY_NOTLISTED', 0);
define('LIVECHAT_VISIBILITY_LISTED', 1);
define('LIVECHAT_VISIBILITY_DEFAULT_PAGES', "admin\nadmin/*\nnode/add*\nnode/*/*\nuser/*/*\n");
define('LIVECHAT_VISIBILITY_SYSTEM_PATHS', "batch\nfile/ajax/*\nsystem/ajax\nadmin_menu/*\njs/admin_menu/*");
function livechat_menu() {
$items['admin/config/services/livechat'] = array(
'title' => 'LiveChat',
'description' => 'Integrate LiveChat with your website.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'livechat_admin_settings_form',
),
'access arguments' => array(
'administer livechat',
),
'file' => 'livechat.admin.inc',
'type' => MENU_NORMAL_ITEM,
);
$items['admin/config/services/livechat/settings'] = array(
'title' => 'Settings',
'access arguments' => array(
'administer livechat',
),
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -1,
);
$items['admin/config/services/livechat/install'] = array(
'title' => 'Install',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'livechat_admin_license_form',
),
'access arguments' => array(
'administer livechat',
),
'file' => 'livechat.admin.inc',
'type' => MENU_LOCAL_TASK,
);
return $items;
}
function livechat_permission() {
return array(
'administer livechat' => array(
'title' => t('Administer LiveChat'),
'description' => t('Administer the LiveChat settings.'),
),
'use livechat' => array(
'title' => t('Use LiveChat'),
'description' => t('Access the LiveChat widget.'),
),
);
}
function livechat_preprocess_html(&$vars) {
if (!variable_get('livechat_enabled', TRUE)) {
return;
}
if (user_access('use livechat', $GLOBALS['user']) && livechat_check_visibility()) {
livechat_add_js();
}
}
function livechat_add_js() {
$license = variable_get('livechat_license');
if (empty($license)) {
return FALSE;
}
$params = array();
$visitor = array();
drupal_alter('livechat', $params, $visitor);
$settings = array(
'LiveChat' => array(
'license' => $license,
'params' => $params,
'visitor' => $visitor,
),
);
$group_id = variable_get('livechat_group', '');
if ($group_id) {
$settings['LiveChat']['group'] = $group_id;
}
drupal_add_js($settings, 'setting');
drupal_add_js(drupal_get_path('module', 'livechat') . '/livechat.js', array(
'scope' => 'footer',
));
}
function livechat_is_installed() {
$license = variable_get('livechat_license');
if (empty($license)) {
return FALSE;
}
return livechat_validate_license($license);
}
function livechat_validate_license($license) {
if (empty($license)) {
return FALSE;
}
return preg_match('/^[0-9]{1,20}$/', $license);
}
function livechat_check_visibility($path = NULL) {
if (!isset($path)) {
$path = current_path();
}
$visibility = variable_get('livechat_visibility', LIVECHAT_VISIBILITY_NOTLISTED);
$pages = drupal_strtolower(variable_get('livechat_pages', LIVECHAT_VISIBILITY_DEFAULT_PAGES));
$exclude_system_paths = variable_get('livechat_exclude_system_paths', 1);
if ($visibility == LIVECHAT_VISIBILITY_NOTLISTED && $exclude_system_paths) {
$pages .= "\n" . LIVECHAT_VISIBILITY_SYSTEM_PATHS;
}
$expanded_path = drupal_strtolower(drupal_get_path_alias($path));
$page_match = drupal_match_path($expanded_path, $pages);
if ($expanded_path != $path) {
$page_match = $page_match || drupal_match_path($path, $pages);
}
return !($visibility xor $page_match);
}