<?php
// $Id: undisposable.module,v 1.3.4.5 2008/12/14 13:16:36 mustafau Exp $

/**
 * @file
 * Functions for detecting disposable e-mail addresses.
 * 
 * PHP serialization support is built-in.
 * If you want to use XML-RPC, JASON or REST services read lib/README.txt
 * 
 * @see http://undisposable.org/
 */

/**
 * Implementation of hook_help().
 */
function undisposable_help($path, $arg) {
  switch ($path) {
    case 'admin/help#undisposable':
      return '<p>'. t('Undisposable.org is a collaborative protection system against disposable e-mail addressing (DEA) - services like 10minutemail.com, jetable.com, pookmail.org etc.') .'</p>';
  }
}

function undisposable_get_library() {
  return drupal_get_path('module', 'undisposable') .'/lib/undisposable.inc.php';
}

/**
 * Implementation of hook_requirements().
 */
function undisposable_requirements($phase) {
  $requirements = array();
  
  switch ($phase) {
    case 'runtime':
      $requirements['undisposable'] = array('title' => t('Disposable e-mail protection'));
      
      $library = undisposable_get_library();
      
      if (file_exists($library)) {
//        $requirements['undisposable']['severity'] = REQUIREMENT_OK;
        $requirements['undisposable']['value'] = t('Installed');
      }
      else {
//        $requirements['undisposable']['description'] = t('The <a href="@url" target="_blank">Undisposable</a> client library was not found.', array('@url' => 'http://undisposable.net'));
//        $requirements['undisposable']['severity'] = REQUIREMENT_OK;
        $requirements['undisposable']['value'] =  t('Built-in');
      }
  }
  
  return $requirements;
}

/**
 * Implementation of hook_user().
 */
function undisposable_user($type, &$edit, &$user, $category = NULL) {
  if ($type == 'validate' && $category == 'account') {
    // Validate the e-mail address.
    $respond = _undisposable('isDisposableEmail', $edit['mail']);
    if (isset($respond['stat']) && $respond['stat'] == 'ok' && $respond['email']['isdisposable']) {
      form_set_error('mail', t('The e-mail address %email has been reported as disposable.', array('%email' => $edit['mail'])));
      watchdog('user', 'Rejected registration attempt with disposable e-mail address: %name (%email).', array('%name' => $edit['name'], '%email' => $edit['mail']));
    }
  }
}

/**
 * Helper function for querying Undisposable.org database.
 * 
 * Example:
 * @code
 *  $respond = _undisposable('isDisposableEmail', $email);
 *  return ($respond['stat'] == 'ok') ? $respond['email']['isdisposable'] : FALSE;
 * @endcode
 *
 * @param $op
 *   Service to query.
 * @param $arg
 *   Either an e-mail address or hostname.
 */
function _undisposable($op, $arg) {
  $uri = 'http://www.undisposable.net/services/php/';
  switch ($op) {
    case 'isDisposableEmail':
      $uri .= 'isDisposableEmail/?email=';
      break;
    case 'isValidEmail':
      $uri .= 'isValidEmail/?email=';
      break;
    case 'isDisposableHost':
      $uri .= 'isDisposableHost/?host=';
      break;
    default:
      $uri .= $op;
  }
  
  $respond = drupal_http_request($uri . $arg);
  return @unserialize($respond->data);
}
