View source
<?php
include_once DRUPAL_ROOT . '/includes/locale.inc';
function ip2country_drush_command() {
$items = array();
$items['ip2country-update'] = array(
'aliases' => array(
'ip-update',
),
'description' => dt('Updates the Ip2Country database from the selected Regional Internet Registry.'),
'drupal dependencies' => array(
'ip2country',
),
'core' => array(
'7',
),
'options' => array(
'registry' => dt('Registry used to obtain data.'),
),
'examples' => array(
'drush ip-update --registry=ripe' => dt('Updates Ip2Country database of ip/country associations.'),
),
);
$items['ip2country-lookup'] = array(
'aliases' => array(
'ip-lookup',
),
'description' => dt('Returns the country name and two-character ISO 3166 country code associated with the given IP address.'),
'drupal dependencies' => array(
'ip2country',
),
'core' => array(
'7',
),
'arguments' => array(
'IPV4' => dt('The IPV4 address to look up, in dotted-quad notation (e.g. 127.0.0.1).'),
),
'required-arguments' => TRUE,
'examples' => array(
'drush ip-lookup IPV4' => dt('Returns a country code associated with the given IP address.'),
'drush ip-lookup IPV4 --fields=ip_address,name --no-field-labels' => dt('Returns the IP address and Country name.'),
),
'outputformat' => array(
'default' => 'table',
'pipe-format' => 'list',
'field-labels' => array(
'ip_address' => (string) dt('IP address'),
'name' => (string) dt('Country'),
'country_code_iso2' => (string) dt('Country code'),
),
'output-data-type' => 'format-table',
),
);
$items['ip2country-status'] = array(
'aliases' => array(
'ip-status',
),
'description' => dt('Displays the time and RIR of the last database update.'),
'drupal dependencies' => array(
'ip2country',
),
'core' => array(
'7',
),
'examples' => array(
'drush ip-status' => dt('Displays the time and RIR of the last database update.'),
),
);
return $items;
}
function ip2country_drush_help($section) {
switch ($section) {
case 'meta:ip2country:title':
return dt('Ip2Country commands');
case 'meta:ip2country:summary':
return dt("Identifies a user's location based on IP address.");
case 'drush:ip2country-lookup':
return dt('Returns the 2-character ISO 3166 country code associated with a given IP address.');
case 'drush:ip2country-update':
return dt('Updates the Ip2Country database from the selected Regional Internet Registry.');
case 'drush:ip2country-status':
return dt('Displays the time and RIR of the last database update.');
}
}
function drush_ip2country_lookup_validate() {
$ipv4 = drush_get_arguments();
if (!isset($ipv4[1])) {
drush_set_error('DRUSH_IP2COUNTRY_MISSING_ARG', dt('Usage: drush ip2country-lookup IPV4' . PHP_EOL . PHP_EOL . "Try 'drush help ip2country-lookup' for more information."));
}
}
function drush_ip2country_update() {
module_load_include('inc', 'ip2country');
$watchdog = variable_get('ip2country_watchdog', TRUE);
$registry = (string) drush_get_option('registry', variable_get('ip2country_rir', 'arin'));
drush_print(dt('Updating ... '), 0, NULL, FALSE);
$status = ip2country_update_database($registry);
if ($status != FALSE) {
drush_print(dt('Completed.'));
drush_print(dt('Database updated from @registry server. Table contains @rows rows.', array(
'@registry' => mb_strtoupper($registry),
'@rows' => $status,
)));
if ($watchdog) {
watchdog('ip2country', 'Drush-initiated database update from @registry server.', array(
'@registry' => mb_strtoupper($registry),
), WATCHDOG_NOTICE);
}
}
else {
drush_print(dt('Failed.'));
drush_print(dt('Database update from @registry server FAILED.', array(
'@registry' => mb_strtoupper($registry),
)));
if ($watchdog) {
watchdog('ip2country', 'Drush-initiated database update from @registry server FAILED.', array(
'@registry' => mb_strtoupper($registry),
), WATCHDOG_WARNING);
}
}
}
function drush_ip2country_lookup($ip_address) {
$country_code = ip2country_get_country($ip_address);
if ($country_code == FALSE) {
drush_print(dt('IP address not found in the database.'));
}
else {
$country_list = country_get_list();
$country_name = $country_list[$country_code];
return array(
$ip_address => array(
'ip_address' => $ip_address,
'name' => (string) dt($country_name),
'country_code_iso2' => $country_code,
),
);
}
}
function drush_ip2country_status() {
$update_time = variable_get('ip2country_last_update', 0);
if (!empty($update_time)) {
$message = dt('Database last updated on @date at @time from @registry server.', array(
'@date' => format_date($update_time, 'custom', variable_get('date_format_ip2country_date', 'n/j/Y')),
'@time' => format_date($update_time, 'custom', variable_get('date_format_ip2country_time', 'H:i:s T')),
'@registry' => drupal_strtoupper(variable_get('ip2country_last_update_rir', 'unknown')),
));
}
else {
$message = dt('Database is empty.');
}
drush_print($message);
}