<?php
// $Id$

/**
 * @file
 * Custom functions for this site.
 */

/**
 * Implementation of hook_install().
 */
function countryban_install() {
  drupal_install_schema('countryban');
  $path = drupal_get_path('module', 'countryban');

  // Our CSV file is from the very kind people at: http://ip-to-country.webhosting.info/node/view/6
  // It must be downloaded and pushed up with this module in order for it to work!

  $csvfile = $path . '/ip-to-country.csv';
  if (($handle = fopen($csvfile, "r")) !== FALSE)  {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)  {
      $query = "INSERT INTO {countryban_iptocountry} VALUES ('" . $data[0] . "', '" .
        $data[1] . "', '" . $data[2] . "', '" . $data[3] . "', '" . str_replace(',', '', str_replace('\'', '', $data[4])) . "')";  
      db_query($query);
    }
    fclose($handle);
  }  
}

/**
 * Implementation of hook_uninstall().
 */
function countryban_uninstall() {
  // Remove the module from the system table
  drupal_uninstall_schema('countryban');
}

/**
 * Implementation of hook_schema().
 */
function countryban_schema() {
  $schema['countryban_iptocountry'] = array('fields' => array('ip_from' => array('type' => 'int', 'size' => 'big'),
    'ip_to'        => array('type' => 'int', 'size' => 'big'),
    'countrycode2' => array('type' => 'varchar', 'length' => '2'),
    'countrycode3' => array('type' => 'varchar', 'length' => '3'),
    'countryname'  => array('type' => 'varchar', 'length' => '75'), ));
  return $schema;
}
