Syncing Drupal to DA email

alsyka

Verified User
Joined
Oct 24, 2008
Messages
8
I am attempting to use Drupals user profiles to manage email accounts on a DA server. I contracted with a coder to provide the script, but now he has not completed it, nor responds to my emails :(

This script will create email accounts using the "directmail_adduser" and "directmail_bulk_form". However, the "directmail_bulk_form" function lists all users, despite an email account being created.

The delete, change password and update functions do not work. Obviously the connection to the DA interface works, as email accounts can be created, but thats where it ends. Any help would be appreciated.

Code:
<?php

// directmail.module, v 0.01
  

function directmail_init() {
  include_once('httpsocket.php');
}  

/**
* Implementation of hook_help().
*/
function directmail_help($section = '') {
  $output = '';

  switch ($section) {
    case "admin/help#directmail":
      $output = '<p>'.  t("DirectMail API module."). '</p>';
      break;
  }

  return $output;
} // function directmail_help


/**
 * Implementation of hook_menu().
 */
function directmail_menu($may_cache) {
  $items = array();
  //if($may_cache) {
    $items[] = array(
      'title' => t('Directmail settings'),
      'path' => 'admin/settings/directmail',
      'access' => user_access('site administration'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array('directmail_admin_settings'),
    );
    
    $items[] = array(
      'title' => t('Settings'),
      'path' => 'admin/settings/directmail/settings',
      'access' => user_access('site administration'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array('directmail_admin_settings'),
      'weight' => -10,
      'type' => MENU_DEFAULT_LOCAL_TASK
    );
  
    $items[] = array(
      'title' => t('All email users'),
      'path' => 'admin/settings/directmail/users',
      'access' => user_access('site administration'),
      'callback' => 'directmail_showusers_page',
      'weight' => -9,
      'type' => MENU_LOCAL_TASK
    );
    
    $items[] = array(
      'title' => t('Add a new email user'),
      'path' => 'admin/settings/directmail/adduser',
      'access' => user_access('site administration'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array('directmail_adduser_form'),
      'weight' => -8,
      'type' => MENU_LOCAL_TASK
    );
    
    $items[] = array(
      'title' => t('Bulk create'),
      'path' => 'admin/settings/directmail/bulk',
      'access' => user_access('site administration'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array('directmail_bulk_form'),
      'weight' => -7,
      'type' => MENU_LOCAL_TASK
    );
  //}
  return $items;
} // function directmail_menu


/**
 * Implementation of hook_user().
 */
function directmail_user($op, &$edit, &$account, $category = NULL) {
  switch($op) {
    case 'insert':
        $pass = $_POST['pass']['pass1'];
        $username = $account->name;
        $result = directmail_adduser($username, $pass, variable_get('directmail_quota', ''), variable_get('directmail_domain', ''));
      break;
    case 'update':
        $name = $_POST['name'];
        $domain = variable_get('directmail_domain', '');
        $oldpassword = $_POST['old_password'];
        $newpassword = $_POST['pass']['pass1'];
        $result = directmail_updateuser($name, $domain, $oldpassword, $newpassword);
        drupal_set_message($result);
      break;
    case 'delete':
        $domain = variable_get('directmail_domain', '');
        directmail_deleteuser($account->name, $domain);
      break;
  }
}

function directmail_form_alter($form_id, &$form) {
  if($form_id == 'user_edit') {
    $form['account']['name']['#weight'] = -10;
    $form['account']['mail']['#weight'] = -9;
    $form['account']['old_password'] = array(
      '#type' => 'password',
      '#title' => t('Old password'),
      '#description' => 'To change the password for directmail account, enter the old password.',
      '#size' => 25,
      '#weight' => -8,
    );
  }
}

function directmail_admin_settings() {
  $form = array();
  
  $form['connection'] = array(
    '#title' => t('Connection settings'),
    '#type' => 'fieldset',
  );
  
  $form['connection']['directmail_hostname'] = array(
    '#title' => t('Hostname'),
    '#type' => 'textfield',
    '#default_value' => variable_get('directmail_hostname', ''),
  );
  
  $form['connection']['directmail_port'] = array(
    '#title' => t('Port'),
    '#type' => 'textfield',
    '#default_value' => variable_get('directmail_port', ''),
  );
  
  $form['connection']['directmail_username'] = array(
    '#title' => t('Username'),
    '#type' => 'textfield',
    '#default_value' => variable_get('directmail_username', ''),
  );
  
  $form['connection']['directmail_password'] = array(
    '#title' => t('Password'),
    '#type' => 'password',
    '#default_value' => variable_get('directmail_password', ''),
  );
  
  $form['email_accounts'] = array(
    '#title' => t('Email accounts settings'),
    '#type' => 'fieldset',
  );
  
  $form['email_accounts']['directmail_domain'] = array(
    '#title' => t('Default domain'),
    '#type' => 'textfield',
    '#default_value' => variable_get('directmail_domain', ''),
  );
  
  $form['email_accounts']['directmail_quota'] = array(
    '#title' => t('Default space quota'),
    '#type' => 'textfield',
    '#default_value' => variable_get('directmail_quota', ''),
    '#suffix' => t('[MB]'),
  );
  
  $form['bulk_create'] = array(
    '#type' => 'fieldset',
    '#title' => t('Email accounts - bulk create'),
  );
  
  $form['bulk_create']['directmail_email_subject'] = array(
    '#type' => 'textfield',
    '#title' => t('Configurable email subject'),
    '#default_value' => variable_get('directmail_email_subject', ''),
    '#description' => t('Enter a default subject of the notification email.'),
  );
  
  $form['bulk_create']['directmail_email_body'] = array(
    '#type' => 'textarea',
    '#title' => t('Configurable email body'),
    '#default_value' => variable_get('directmail_email_body', ''),
    '#description' => t('Enter the default email template to notify users about created email accounts. Use the following tokens: %user, %email_account, %email_password, %server_url'),
  );
  
  $form['bulk_create']['directmail_password_length'] = array(
    '#type' => 'textfield',
    '#title' => t('Random password length'),
    '#default_value' => variable_get('directmail_password_length', '8'),
    '#description' => t('Length of default passwords, which are about to be assigned to new email accounts.'),
  );
  
  $form['bulk_create']['directmail_login_url'] = array(
    '#type' => 'textfield',
    '#title' => t('Email server login URL'),
    '#default_value' => variable_get('directmail_login_url', ''),
  );
  
  return system_settings_form($form);
}

function directmail_adduser_form() {
  $form = array();
  
  $form['username'] = array(
    '#title' => t('Username'),
    '#type' => 'textfield',
    '#required' => true,
  );
  
  $form['password'] = array(
    '#title' => t('Password'),
    '#type' => 'password',
    '#required' => true,
  );
  
  $form['domain'] = array(
    '#title' => t('Default domain'),
    '#type' => 'textfield',
    '#default_value' => variable_get('directmail_domain', ''),
    '#required' => true,
  );
  
  $form['quota'] = array(
    '#title' => t('Default space quota'),
    '#type' => 'textfield',
    '#default_value' => variable_get('directmail_quota', ''),
    '#suffix' => t('[MB]'),
    '#required' => true,
  );
  
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Add'),
  );
  
  return $form;
}

function directmail_adduser_form_submit($form_id, $form_values) {
  $message = directmail_adduser($form_values['username'], $form_values['password'], $form_values['quota'], $form_values['domain']);
  drupal_set_message($message);
}

function directmail_showusers_page() {
  $data = directmail_showusers(variable_get('directmail_domain', ''));
  return theme('item_list', $data);
}

function directmail_bulk_form() {
  $form = array();
  $missing = array();
  
  $accounts = directmail_showusers(variable_get('directmail_domain', ''));
  $result = db_query("SELECT name FROM users WHERE status=1 AND uid<>1");
  while($u = db_fetch_object($result)) {
    if(!in_array($u->name, $accounts) && $u->name) {
      $missing[] = $u->name;
    }
  }
  
  $userlist = implode('<br />', $missing);
  if($missing) {
    $form['users'] = array(
      '#type' => 'value',
      '#value' => $missing,
    );
  
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Bulk create'),
      '#prefix' => t('Users without any email account:') . "<br /> $userlist <br />",
      '#description' => t('Press the button to create email accounts for user without any email account.'),
    );
  } else {
    drupal_set_message(t('There are no active users without an email account.'));
  }
  return $form;
}

function directmail_bulk_form_submit($form_id, $form_values) {
  if($form_values['users']) {
    foreach($form_values['users'] as $u) {
      $user = db_fetch_object(db_query("SELECT name, mail FROM {users} WHERE name='%s'", $u));
      $password = directmail_createrandompassword(variable_get('directmail_password_length', '8'));
      
      directmail_adduser($user->name, $password, variable_get('directmail_quota', ''), variable_get('directmail_domain', ''));
      
      $search = array('%user', '%email_account', '%email_password', '%server_url');
      $replace = array($user->name, $user->mail, $password, variable_get('directmail_login_url', ''));
      $to = $user->mail;
      $subject = variable_get('directmail_email_subject', '');
      $body = variable_get('directmail_email_body', '');
      $body = check_markup(str_replace($search, $replace, $body));
      $from = variable_get('site_mail', ini_get('sendmail_from'));
      $headers['Content-Type'] = " text/html; charset=utf-8";
      drupal_mail('contact-user-mail', $to, $subject, $body, $from, $headers);
    }
    drupal_set_message(t('Email accounts were created.'));
  }
}


function directmail_createrandompassword($length) {
    $chars = "abcdefghijkmnopqrstuvwxyz023456789";
    srand((double)microtime()*1000000);
    $i = 0;
    $pass = '' ;
    while ($i <= $length) {
        $num = rand() % 33;
        $tmp = substr($chars, $num, 1);
        $pass = $pass . $tmp;
        $i++;
    }
    return $pass;
}



/* API */

function directmail_showusers($domain) {
  $sock = new HTTPSocket;

  $sock->connect(variable_get('directmail_hostname', ''), variable_get('directmail_port', '2222'));
  $sock->set_login(variable_get('directmail_username', ''), variable_get('directmail_password', ''));
  
  $sock->query('/CMD_API_POP', array('action' => 'list', 'domain' => $domain));
  $result = $sock->fetch_parsed_body();

  return $result['list'];
}

function directmail_adduser($name, $password, $quota = 0, $domain) {
  $sock = new HTTPSocket;

  $sock->connect(variable_get('directmail_hostname', ''), variable_get('directmail_port', '2222'));
  $sock->set_login(variable_get('directmail_username', ''), variable_get('directmail_password', ''));

  $sock->query('/CMD_API_POP',
    array(
		  'action' => 'create',
		  'user' => $name,
		  'passwd' => $password,
		  'quota' => $quota,
		  'domain' => $domain,
    )
  );
  $result = $sock->fetch_parsed_body();
  return $result['text'];
}

function directmail_updateuser($name, $domain, $oldpassword, $newpassword) {
  $sock = new HTTPSocket;

  $sock->connect(variable_get('directmail_hostname', ''), variable_get('directmail_port', '2222'));
  $sock->set_login(variable_get('directmail_username', ''), variable_get('directmail_password', ''));

  $sock->query('/CMD_CHANGE_EMAIL_PASSWORD',
    array(
		  'email' => $name . '@' . $domain,
		  'oldpassword' => $oldpassword,
		  'password1' => $newpassword,
		  'password2' => $newpassword,
    )
  );
  $result = $sock->fetch_parsed_body();
  return $result['text'];
}

function directmail_deleteuser($name, $domain) {
  $sock = new HTTPSocket;

  $sock->connect(variable_get('directmail_hostname', ''), variable_get('directmail_port', '2222'));
  $sock->set_login(variable_get('directmail_username', ''), variable_get('directmail_password', ''));

  $sock->query('/CMD_API_POP',
    array(
		  'action' => 'delete',
		  'user' => $name . '@' . $domain,
		  'domain' => $domain,
    )
  );
  $result = $sock->fetch_parsed_body();
  return $result['text'];
}
 
Top