¿Para que te puede servir este codigo? Pues para lo que tu desees, yo por ejemplo lo uso para dar opcion a cada usuario de mi web a poder crearse una cuenta de email con mi dominio tipo: [email protected] y asi es un servicio mas que podemos ofrecer a nuestros usuarios ;).
Bueno aqui pongo el codigo:
Son dos archivos:
1º - client.php:
Código PHP:
<table>
<?php
if ($_SERVER['REQUEST_METHOD']=="POST")
{
include_once 'class.plesk.php';
$plesk=new Plesk("https://tudominio.es:8443/enterprise/control/agent.php","usuario de plesk","contraseña"); /* Aqui tienes que indicar el dominio en el que se crearan las cuentas de email, tambien teneis que indicar el usuario de plesk y la contraseña*/
$result=$plesk->add_email(2,$_POST['username'],$_POST['password']);
echo "<tr><td colspan=\"2\" align=\"center\">".$result['message']."</td></tr><tr><td> </td></tr>"; /*Esta linea es la que muestra el resultado de la operacion*/
}
?>
<form method="POST">
<tr>
<td width="195" align="right">Email:</td>
<td width="321"><input type="text" name="username">
<strong>@tudominio.es</strong></td>
</tr>
<tr>
<td align="right">Contraseña:</td><td><input type="password" name="password"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Crear Email"></td>
</tr>
</form>
</table>
Código PHP:
<?php
error_reporting ( E_ALL );
ini_set ( 'display_errors', 1 );
ini_set ( 'max_execution_time', 300 );
class Plesk {
public $hostname;
public $username;
public $password;
public $port;
public function __construct($hostname = '', $username = '', $password = '') {
$this->hostname = $hostname;
$this->username = $username;
$this->password = $password;
}
public function add_email($domain_id, $username, $password,$quota=500) {
$quota=$quota*1024*1024;
$packet = "<packet version='1.4.2.0'>
<mail>
<create>
<filter>
<domain_id>{$domain_id}</domain_id>
<mailname>
<name>{$username}</name>
<mailbox>
<enabled>true</enabled>
<quota>{$quota}</quota>
</mailbox>
<password>{$password}</password>
<password_type>plain</password_type>
<permissions>
<cp_access>true</cp_access>
</permissions>
</mailname>
</filter>
</create>
</mail>
</packet>";
$result=$this->send ( $packet );
//print_r($result);
$result1=@$result[0]['elements'][0]['elements'];
if(@$result1[0]['text']=="error")
{
return array("status"=>"error","error_code"=>$result1[1]['text'],"message"=>$result1[2]['text']);
}
elseif (@$result[0]['elements'][0]['elements'][0]['elements'][0]['elements'][0]['text']=="error")
{
return array("status"=>"error",'error_code'=>$result[0]['elements'][0]['elements'][0]['elements'][0]['elements'][1]['text'],'message'=>$result[0]['elements'][0]['elements'][0]['elements'][0]['elements'][2]['text']);
}
else
{
return array("status"=>"ok","message"=>"created");
}
}
public function send($packet = '') {
$headers = array ('HTTP_AUTH_LOGIN: ' . $this->username, 'HTTP_AUTH_PASSWD: ' . $this->password, 'Content-Type: text/xml' );
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt ( $ch, CURLOPT_URL, $this->hostname );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $packet );
$retval = curl_exec ( $ch );
curl_close ( $ch );
return $this->xml2array($retval);
}
public function xml2array($xml) {
$xmlary = array ();
$reels = '/<(\w+)\s*([^\/>]*)\s*(?:\/>|>(.*)<\/\s*\\1\s*>)/s';
$reattrs = '/(\w+)=(?:"|\')([^"\']*)(:?"|\')/';
preg_match_all ( $reels, $xml, $elements );
foreach ( $elements [1] as $ie => $xx ) {
$xmlary [$ie] ["name"] = $elements [1] [$ie];
if ($attributes = trim ( $elements [2] [$ie] )) {
preg_match_all ( $reattrs, $attributes, $att );
foreach ( $att [1] as $ia => $xx )
$xmlary [$ie] ["attributes"] [$att [1] [$ia]] = $att [2] [$ia];
}
$cdend = strpos ( $elements [3] [$ie], "<" );
if ($cdend > 0) {
$xmlary [$ie] ["text"] = substr ( $elements [3] [$ie], 0, $cdend - 1 );
}
if (preg_match ( $reels, $elements [3] [$ie] ))
$xmlary [$ie] ["elements"] = $this->xml2array ( $elements [3] [$ie] );
else if ($elements [3] [$ie]) {
$xmlary [$ie] ["text"] = $elements [3] [$ie];
}
}
return $xmlary;
}
}
?>