Location: PHPKode > scripts > Virtual Mail ManaGer Interface > virtual-mail-manager-interface/vm/vm.showcase.php
<?php

/*
 *  FAKE Interface - no VMailMgr daemon required, absolutely no changes to your
 *  system's configuration made, no risks involved
 *
 *  This is useful for:
 *  - Showcases (demonstration of vmmi)
 *  - Creating templates / writing language files on an environment where no
 *    VMailMgr daemon is available
 *
 *  This interface is distributed under the LGPL (see LICENSE.LGPL)
 *
 *  Authors:
 *    Daniel Lorch <hide@address.com>
 *
 */

if(!isset($_SESSION['userlist'])) {
  $_SESSION['userlist'] = array(

    /* example account */
    array('username'             => 'peter',
          'mailboxdir'           => './users/peter',
          'personal'             => 'Peter Kurten',
	  'forwards'             => array(),
	  'hardquota'            => '-',
	  'softquota'            => '-',
	  'messagesize'          => '-',
	  'messagecount'         => '-',
	  'creation'             => '0',
	  'expiry'               => '-',
	  'flags'                => array('mailbox_enabled' => true),
	  'autoresponse'         => "Subject: Autoresponse %S\nFrom: Peter Kurten <hide@address.com>\n\nThis is my autoresponse.",
	  'autoresponse_enabled' => true
    ),

    /* example forward */
    array('username'             => 'linda',
          'mailboxdir'           => '',
          'personal'             => 'Linda Jones',
	  'forwards'             => array('hide@address.com'),
	  'creation'             => '0',
	  'expiry'               => '-',
	  'flags'                => array('mailbox_enabled' => true)
    )
    
  );
}

class vmailmgr {

  /* configuration part */
  var $my_domain = 'demo.com';
  var $my_pass   = 'demo';

  /* private: domain and password */
  var $domain;
  var $pass;

  /* public: constructor */
  function vmailmgr($domain, $pass) {
    $this->domain = $domain;
    $this->pass   = $pass;
  }
  
  /* private: send raw command */
  function raw($cmd, $args=array()) {
    if(!($this->domain == $this->my_domain && $this->pass == $this->my_pass))  
      return false;
  
    switch($cmd) {
      case 'listdomain':
        $this->response = $_SESSION['userlist'];

	return true;
        break;

      case 'lookup':
        foreach($_SESSION['userlist'] as $key => $val) {
	  if($val['username'] == $args[1]) {
	    $this->response = $_SESSION['userlist'][$key];
            return true;
	  }
	}
	
	return false;
	break;
	
      case 'adduser2':
        /* does username already exist? */
        foreach($_SESSION['userlist'] as $key => $val) {
	  if($val['username'] == $args[1]) {
            $this->response = 'User already exists';
            return false;
	  }
	}

        $count = count($_SESSION['userlist']);
        
	while(isset($_SESSION['userlist'][$count])) {
	  ++$count;
	}
	
	$_SESSION['userlist'][$count]['username'] = $args[1];

        /* account */
        if($args[1] == $args[4]) {
	  $_SESSION['userlist'][$count]['mailboxdir'] = './users/' . $args[1];
	}
	/* forward alias */
	else {
	  $_SESSION['userlist'][$count]['mailboxdir'] = '';
	}

        $_SESSION['userlist'][$count]['flags']['mailbox_enabled'] = true;

        if($args[5] == '') {
          $_SESSION['userlist'][$count]['forwards'] = array();
        }
	else {
	  for($i=5; $i<count($args); ++$i) {
	    $_SESSION['userlist'][$count]['forwards'][] = $args[$i];
	  }
	}

        return true;
        break;

      case 'deluser':
        foreach($_SESSION['userlist'] as $key => $val) {
	  if($val['username'] == $args[1]) {
	    unset($_SESSION['userlist'][$key]);
	    return true;
	  }
	}
	
	return false;
	break;
	
      case 'chattr':

        foreach($_SESSION['userlist'] as $key => $val) {
	  if($val['username'] == $args[1]) {
	    switch($args[3]) {
              /* set password */
	      case 1:
	        return true;
	        break;

	      /* set forwards */
	      case 2:
                unset($_SESSION['userlist'][$key]['forwards']);
	        if($args[4] == '') {
	          $_SESSION['userlist'][$key]['forwards'] = array();
	        }
		else {
		  for($i=4; $i<count($args); ++$i) {
		    $_SESSION['userlist'][$key]['forwards'][] = $args[$i];
		  }
		}
		return true;
		break;
	     
	      /* hardquota */
	      case '3':
	        $_SESSION['userlist'][$key]['hardquota'] = $args[4];
	        return true;
	        break;
		 
	      /* softquota */
	      case '4':
	        $_SESSION['userlist'][$key]['softquota'] = $args[4];
	        return true;
		break;

	      /* message size */
	      case '5':
	        $_SESSION['userlist'][$key]['messagesize'] = $args[4];
	        return true;
		break;

	      /* message count */
	      case '6':
	        $_SESSION['userlist'][$key]['messagecount'] = $args[4];
	        return true;
		break;

	      /* expiry */
	      case '7':
	        $_SESSION['userlist'][$key]['expiry'] = $args[4];
	        return true;
		break;

	      /* enabled */
	      case '8':
	        $_SESSION['userlist'][$key]['flags']['mailbox_enabled'] = ($args[4] == '1');
	        return true;
		break;

	      /* personal */
	      case '9':
	        $_SESSION['userlist'][$key]['personal'] = $args[4];
	        return true;
		break;
 
	    } // end: switch
	  } // end: if
	} // end: foreach
         
	return false;
        break;
    }
    
    return false;
  }
  

  /* public: list all users */  
  function list_users() {
    if(!$this->raw("listdomain", array($this->domain, $this->pass)))
      return false;

    /* cleanup array */
    if(count($_SESSION['userlist']) > 0) {
      foreach($_SESSION['userlist'] as $key => $val) {
        $new[] = $val;
      }
    }
            
    return $_SESSION['userlist'] = $new;
  }

  /* public: retrieve info on user */
  function user_info($user) {
    if(!$this->raw("lookup", array($this->domain, $user, $this->pass)))
      return false;

    return $this->response;
  }

  function add_account($user, $pass, $forwards=array()) {
    return $this->raw("adduser2", array_merge($this->domain, $user, $this->pass, $pass, $user, $forwards));
  }

  function add_alias($user, $pass="", $forwards=array()) {
    return $this->raw("adduser2", array_merge($this->domain, $user, $this->pass, $pass, "", $forwards));
  }
  
  function delete_user($user) {
    return $this->raw("deluser", array($this->domain, $user, $this->pass));
  }

  function set_password($user, $pass) {
    return $this->raw("chattr", array($this->domain, $user, $this->pass, 1, $pass));
  }

  function set_forwards($user, $forwards=NULL) {
    if(!isset($forwards) || count($forwards) == 0)
      $forwards = '';
  
    return $this->raw("chattr", array_merge($this->domain, $user, $this->pass, 2, $forwards));
  }

  function set_hardquota($user, $quota='-') {
    return $this->raw("chattr", array($this->domain, $user, $this->pass, 3, trim($quota) == '' ? '-' : $quota));
  }

  function set_softquota($user, $quota='-') {
    return $this->raw("chattr", array($this->domain, $user, $this->pass, 4, trim($quota) == '' ? '-' : $quota));
  }

  function set_messagesize($user, $size='-') {
    return $this->raw("chattr", array($this->domain, $user, $this->pass, 5, trim($size) == '' ? '-' : $size));
  }

  function set_messagecount($user, $count='-') {
    return $this->raw("chattr", array($this->domain, $user, $this->pass, 6, trim($count) == '' ? '-' : $count));  
  }

  function set_expiry($user, $timestamp='-') {
    return $this->raw("chattr", array($this->domain, $user, $this->pass, 7, trim($timestamp) == '' ? '-' : $timestamp));
  }

  function set_enabled($user, $enable) {
    return $this->raw("chattr", array($this->domain, $user, $this->pass, 8, $enable ? '1' : '0'));
  }

  function set_personal($user, $text='') {
    return $this->raw("chattr", array($this->domain, $user, $this->pass, 9, $text));  
  }

  function set_catchall($user=NULL) {
    $this->delete_user("+");

    if(!isset($user)) {
      return true;
    }
    else {
      return $this->add_alias("+", "", $user)
	  && $this->set_personal("+", "Catchall Alias");
    }
  }

  function autoresponse_set($user, $message) {
    foreach($_SESSION['userlist'] as $key => $val) {
      if($val['username'] == $user) {
        $_SESSION['userlist'][$key]['autoresponse'] = $message;
        return true;
      }
    }
    
    return false;
  }

  function autoresponse_get($user) {
    foreach($_SESSION['userlist'] as $key => $val) {
      if($val['username'] == $user) {
        return $_SESSION['userlist'][$key]['autoresponse'];
      }
    }
    
    return false;
  }

  function autoresponse_set_enabled($user, $enable) {
    foreach($_SESSION['userlist'] as $key => $val) {
      if($val['username'] == $user) {
        $_SESSION['userlist'][$key]['autoresponse_enabled'] = $enable;
        return true;
      }
    }
    
    return false;
  }

  function autoresponse_isset($user) {
    foreach($_SESSION['userlist'] as $key => $val) {
      if($val['username'] == $user) {
        return $_SESSION['userlist'][$key]['autoresponse_enabled'];
      }
    }
    
    return false;
  }
  
  function autoresponse_parse($message) {
    if(preg_match("/(.*?)\r?\n\r?\n(.*)/s", $message, $matches)) {
      $result['body'] = $matches[2];    

      $headers = preg_split("/\r?\n/", $matches[1]);

      for($i=0; $i<count($headers); ++$i) {
        if(preg_match("/([^:]+): (.*)/", $headers[$i], $matches)) {
	  $result['headers'][$matches[1]] = $matches[2];
	}
      }
    }
    else {
      $result['body'] = $message;
    }
  
    return $result;
  }
  
  /* return vmailmgr's last text message */
  function last_response() {
    return $this->response;
  }
}

?>
Return current item: Virtual Mail ManaGer Interface