Scripts

Direct Admin API

Dit is een wrapper voor de Direct Admin Api, momenteel voor - Email - Forwarders - Autoresponders Hiermee kun je bijvoorbeeld in je eigen CMS email adressen aanmaken. Natuurlijk heeft Direct Admin nog veel meer opties, en het zou dan ook mooi zijn als dit ook meer wordt ondersteund door deze classes. Daarom nodig ik je uit om (via github) verder te ontwikkelen, bijvoorbeeld dmv een fork. Github repository: http://github.com/arian/DirectAdminApi Download: http://github.com/arian/DirectAdminApi/downloads Verder is nog HTTPSocket.php nodig. Deze is niet zelf gemaakt maar staat wel er bij github bij: http://github.com/arian/DirectAdminApi/blob/master/Source/HTTPSocket.php

direct-admin-api
[b]Voorbeeld[/b]
<?php

// http://github.com/arian/DirectAdminApi/blob/master/demo/example.php

include_once '../Source/HTTPSocket.php';
 
$sock = new HTTPSocket();
$sock->connect('domain.nl',2222);
$sock->set_login('DirectAdminUsername','DirectAdminPassword');
 
include_once '../Source/DA/Emails.php';
$emails = new DA_Emails($sock,'domain.com');
 
try {
	// Fetch a list of users (before the @ sign) or pop emails
	print_r($emails->fetch());
 
	// Fetch a list of users and their usage and quota
	print_r($emails->fetchQuotas());
 
	// Fetch the quota and usage for a single user
	print_r($emails->fetchUserQuota('username'));
 
	// Create a user email => username - password - quota (MiB)
	var_dump($emails->create('username','pass',50));
 
	// Modify a user email => username - password - quota (MiB)
	var_dump($emails->modify('username'));
 
	// Delete a user email
	var_dump($emails->delete('username'));
 
}catch(DA_Exception $e){
	echo $e->getMessage();
}

?>

[b]Code:[/b] (Elke class zijn eigen bestand)
<?php
 
include_once 'HTTPSocket.php';
 
abstract class DA_API {
 
	/**
	 * @var HTTPSocket
	 */
	protected $sock;
 
	/**
	 * The default domain
	 * @var string
	 */
	protected $domain;
 
	/**
	 *
	 * @param HTTPSocket $sock
	 * @return
	 */
	public function __construct(HTTPSocket $sock,$domain=null){
		$this->sock = $sock;
		$this->domain = $domain;
	}
 
	public function setDomain($domain){
		$this->domain = $domain;
	}
 
	public function getDomain($domain=null){
		$domain = $domain ? $domain : $this->domain;
		if(empty($domain)){
			include_once 'Exception.php';
			throw new DA_Exception('No Domain is set!');
		}
		return $domain;
	}
 
}

 
/**
 * http://www.directadmin.com/api.html#email
 * http://www.directadmin.com/features.php?id=348
 */
 
include_once 'Api.php';
 
class DA_Autoresponders extends DA_API {
 
	/**
	 * Fetch all the Autoresponders
	 * @param string $domain
	 * @return array array(array('user' => 'destination email'))
	 */
	public function fetch($domain=null){
		$domain = $this->getDomain($domain);
 
		$this->sock->query('/CMD_API_EMAIL_AUTORESPONDER',array(
			'domain' => $domain
		));
		$rows = $this->sock->fetch_parsed_body();
		$keys = array_keys($rows);
		if(isset($keys[1]) && $keys[1] == '#95API'){
			$rows = array();
		}
		return $rows;
	}
 
	/**
	 * Fetch the destination url of a forwarder
	 * @param string $user
	 * @param string $domain
	 * @return string
	 */
	public function fetchUser($user,$domain=null){
		$domain = $this->getDomain($domain);
 
		$this->sock->query('/CMD_API_EMAIL_AUTORESPONDER_MODIFY',array(
			'domain'	=> $domain,
			'user'		=> $user
		));
		return $this->sock->fetch_parsed_body();
	}
 
	/**
	 * Create a forwarder
	 * @param string $user
	 * @param string $email
	 * @param string $domain
	 * @return bool
	 */
	public function create($user,$msg,$email=null,$domain=null){
		$domain = $this->getDomain($domain);
 
		$data = array(
			'action' 	=> 'create',
			'domain' 	=> $domain,
			'user'		=> $user,
			'text'		=> $msg,
			'cc'		=> empty($email) ? 'OFF' : 'ON',
			'email'		=> $email,
			'create'	=> 'Create'
		);
		$this->sock->query('/CMD_API_EMAIL_AUTORESPONDER',$data);
 
		$ret = $this->sock->fetch_parsed_body();
		return isset($ret['error']) && $ret['error'] == 0;
	}
 
	/**
	 * Set the password of an emailaddress
	 * @param string $user
	 * @param string $pass
	 * @param string $domain
	 * @return bool
	 */
	public function modify($user,$msg,$email,$domain=null){
		$domain = $this->getDomain($domain);
 
		$this->sock->query('/CMD_API_EMAIL_AUTORESPONDER',array(
			'action'	=> 'modify',
			'domain' 	=> $domain,
			'user'		=> $user,
			'text'		=> $msg,
			'cc'		=> empty($email) ? 'OFF' : 'ON',
			'email'		=> $email,
			'create'	=> 'Create'
		));
 
		$ret = $this->sock->fetch_parsed_body();
		return isset($ret['error']) && $ret['error'] == 0;
	}
 
	/**
	 * Delete an user
	 * @param string $user
	 * @param string $domain
	 * @return bool
	 */
	public function delete($user,$domain=null){
		$domain = $this->getDomain($domain);
 
		$this->sock->query('/CMD_API_EMAIL_AUTORESPONDER',array(
			'action'	=> 'delete',
			'domain' 	=> $domain,
			'user'		=> $user,
			'select0'	=> $user
		));
 
		$ret = $this->sock->fetch_parsed_body();
		return isset($ret['error']) && $ret['error'] == 0;
	}
 
}

 
/**
 * http://www.directadmin.com/api.html#email
 */
 
include_once 'Api.php';
 
class DA_Emails extends DA_API {
 
	/**
	 *
	 * @param string $domain
	 * @return array
	 */
	public function fetch($domain=null){
		$domain = $this->getDomain($domain);
 
		$this->sock->query('/CMD_API_POP',array(
			'action' => 'list',
			'domain' => $domain
		));
		$row = $this->sock->fetch_parsed_body();
 
		if(is_array($row)){
			foreach($row as &$item){
				parse_str($item,$item);
			}
			if(empty($item) || !is_array($item) || !isset($item['quota'])){
				$row = array();
			}
		}else{
			$row = array();
		}
		return array();
	}
 
	/**
	 * Get a list of the users and the quota and usage
	 * @param string $domain
	 * @return array for example array('user' => array(usage=>3412,quota=>123543))
	 */
	public function fetchQuotas($domain=null){
		$domain = $this->getDomain($domain);
 
		$this->sock->query('/CMD_API_POP',array(
			'action'	=> 'list',
			'type'		=> 'quota',
			'domain'	=> $domain
		));
		$row = $this->sock->fetch_parsed_body();
		if(is_array($row)){
			foreach($row as &$item){
				parse_str($item,$item);
			}
			if(empty($item) || !is_array($item) || !isset($item['quota'])){
				$row = array();
			}
		}else{
			$row = array();
		}
 
		return $row;
	}
 
	/**
	 * Get the quota and usage for a user
	 * @param string $user
	 * @param string $domain
	 * @return array for example array(usage=>3412,quota=>123543)
	 */
	public function fetchUserQuota($user,$domain=null){
		$quotas = $this->fetchQuotas($domain);
		return isset($quotas[$user]) ? $quotas[$user] : array();
	}
 
	/**
	 * Create an Email Address
	 * @param string $user
	 * @param string $pass
	 * @param int $quota [optional] Integer in Megabytes. Zero for unlimited, 1+ for number of Megabytes.
	 * @param string $domain
	 * @return bool
	 */
	public function create($user,$pass,$quota=0,$domain=null){
		$domain = $this->getDomain($domain);
 
		$this->sock->query('/CMD_API_POP',array(
			'action' 	=> 'create',
			'domain' 	=> $domain,
			'quota'		=> $quota,
			'user'		=> $user,
			'passwd'	=> $pass
		));
 
		$ret = $this->sock->fetch_parsed_body();
		return isset($ret['error']) && $ret['error'] == 0;
	}
 
	/**
	 * Set the password of an emailaddress
	 * @param string $user
	 * @param string $pass
	 * @param string $domain
	 * @return bool
	 */
	public function modify($user,$pass=null,$quota=0,$domain=null){
		$domain = $this->getDomain($domain);
 
		$this->sock->query('/CMD_API_POP',array(
			'action'	=> 'modify',
			'domain' 	=> $domain,
			'user'		=> $user,
			'passwd'	=> $pass,
			'passwd2'	=> $pass,
			'quota'		=> $quota
		));
 
		$ret = $this->sock->fetch_parsed_body();
		return isset($ret['error']) && $ret['error'] == 0;
	}
 
	/**
	 * Delete an user
	 * @param string $user
	 * @param string $domain
	 * @return bool
	 */
	public function delete($user,$domain=null){
		$domain = $this->getDomain($domain);
 
		$this->sock->query('/CMD_API_POP',array(
			'action'	=> 'delete',
			'domain' 	=> $domain,
			'user'		=> $user
		));
 
		$ret = $this->sock->fetch_parsed_body();
		return isset($ret['error']) && $ret['error'] == 0;
	}
 
}

class DA_Exception extends Exception {}

/**
 * http://www.directadmin.com/api.html#email
 */
 
include_once 'Api.php';
 
class DA_Forwarders extends DA_API {
 
	/**
	 * Fetch all the forwarders
	 * @param string $domain
	 * @return array array(array('user' => 'destination email'))
	 */
	public function fetch($domain=null){
		$domain = $this->getDomain($domain);
 
		$this->sock->query('/CMD_API_EMAIL_FORWARDERS',array(
			'action' => 'list',
			'domain' => $domain
		));
		$rows = $this->sock->fetch_parsed_body();
		$keys = array_keys($rows);
		if(isset($keys[1]) && $keys[1] == '#95API'){
			$rows = array();
		}
		return $rows;
	}
 
	/**
	 * Fetch the destination url of a forwarder
	 * @param string $user
	 * @param string $domain
	 * @return string
	 */
	public function fetchUser($user,$domain=null){
		$users = $this->fetch($domain);
		return isset($users[$user]) ? $users[$user] : null;
	}
 
	/**
	 * Create a forwarder
	 * @param string $user
	 * @param string $email
	 * @param string $domain
	 * @return bool
	 */
	public function create($user,$email,$domain=null){
		$domain = $this->getDomain($domain);
 
		$this->sock->query('/CMD_API_EMAIL_FORWARDERS',array(
			'action' 	=> 'create',
			'domain' 	=> $domain,
			'user'		=> $user,
			'email'		=> $email,
		));
 
		$ret = $this->sock->fetch_parsed_body();
		return isset($ret['error']) && $ret['error'] == 0;
	}
 
	/**
	 * Set the password of an emailaddress
	 * @param string $user
	 * @param string $pass
	 * @param string $domain
	 * @return bool
	 */
	public function modify($user,$email,$domain=null){
		$domain = $this->getDomain($domain);
 
		$this->sock->query('/CMD_API_EMAIL_FORWARDERS',array(
			'action'	=> 'modify',
			'domain' 	=> $domain,
			'user'		=> $user,
			'email'		=> $email,
		));
 
		$ret = $this->sock->fetch_parsed_body();
		return isset($ret['error']) && $ret['error'] == 0;
	}
 
	/**
	 * Delete an user
	 * @param string $user
	 * @param string $domain
	 * @return bool
	 */
	public function delete($user,$domain=null){
		$domain = $this->getDomain($domain);
 
		$this->sock->query('/CMD_API_EMAIL_FORWARDERS',array(
			'action'	=> 'delete',
			'domain' 	=> $domain,
			'user'		=> $user,
			'select0'	=> $user
		));
 
		$ret = $this->sock->fetch_parsed_body();
		return isset($ret['error']) && $ret['error'] == 0;
	}
 
}

?>

Reacties

0
Nog geen reacties.