Author Topic: Création sous-domaine automatique, enfin !!  (Read 1012 times)

Offline Jejedev

  • Revendeurs
  • Full Member
  • ******
  • Posts: 215
  • Karma: 18
    • View Profile
Création sous-domaine automatique, enfin !!
« on: Février 06, 2006, 09:49:04 pm »
Bonjour,

Depuis plusieurs jours je test ce script et je l'ai fait fonctionner !!

Vous allez pouvoir enfin créer vos sous-domaine automatiquement via un script php à l'aide de DirectAdmin.

Voici le premier code : (Créez une page : creation_sousdomaine.php)
Modifiez le nom de domaine sans "http://www."
Indiquez le pseudo de votre compte revendeur ainsi que le mot de passe.

Code: [Select]
<?php
if($creation=="ok")
{
$domaine="votre_domaine.com";
$pseudo="votre_pseudo";
$motdepasse="votre_motdepasse";

include 
'httpsocket.php';

$sock = new HTTPSocket;
$sock->connect($domaine,2222);
$sock->set_login($pseudo,$motdepasse);
$sock->set_method('POST');
$sock->query('/CMD_SUBDOMAIN',
    array(
        
'action' => 'create',
        
'domain' => $domaine,
        
'subdomain' => $sousdomaine
    
));
$result $sock->fetch_body();

echo 
"Sous-domaine créé : $sousdomaine.$domaine";
}
else
{
?>

<form method="post" action="creation_sousdomaine.php?creation=ok">
<input type="text" name="sousdomaine"> .votre_site.com
<input type="submit" value=" Créer le sous-domaine ">
</form>
<?
}
?>

Ensuite créez une nouvelle page, nommez la httpsocket.php
Inserez ce script :

Code: [Select]
<?php
class HTTPSocket {

var $version '2.6';

var $method 'GET';

var $remote_host;
var $remote_port;
var $remote_uname;
var $remote_passwd;

var $result;
var $result_header;
var $result_body;
var $result_status_code;

var $lastTransferSpeed;

var $bind_host;

var $error = array();
var $warn = array();
var $query_cache = array();

var $doFollowLocationHeader TRUE;
var $redirectURL;

var $extra_headers = array();

function connect($host$port '' )
{
if (!is_numeric($port))
{
$port 80;
}

$this->remote_host $host;
$this->remote_port $port;
}

function bind$ip '' )
{
if ( $ip == '' )
{
$ip $_SERVER['SERVER_ADDR'];
}

$this->bind_host $ip;
}

function set_method$method 'GET' )
{
$this->method strtoupper($method);
}

function set_login$uname ''$passwd '' )
{
if ( strlen($uname) > )
{
$this->remote_uname $uname;
}

if ( strlen($passwd) > )
{
$this->remote_passwd $passwd;
}

}

function query$request$content ''$doSpeedCheck )
{
$this->error $this->warn = array();
$this->result_status_code NULL;

if (preg_match('!^http://!i',$request))
{
$location parse_url($request);
$this->connect($location['host'],$location['port']);
$this->set_login($location['user'],$location['pass']);

$request $location['path'];
$content $location['query'];

if ( strlen($request) < )
{
$request '/';
}

}

$array_headers = array(
'User-Agent' => "HTTPSocket/$this->version",
'Host' => ( $this->remote_port == 80 $this->remote_host "$this->remote_host:$this->remote_port" ),
'Accept' => '*/*',
'Connection' => 'Close' );

foreach ( $this->extra_headers as $key => $value )
{
$array_headers[$key] = $value;
}

$this->result $this->result_header $this->result_body '';

if (is_array($content))
{
$pairs = array();

foreach ( $content as $key => $value )
{
$pairs[] = "$key=".urlencode($value);
}

$content join('&',$pairs);
unset($pairs);
}

$OK TRUE;

if ($this->bind_host)
{
$socket socket_create(AF_INETSOCK_STREAMSOL_TCP);
socket_bind($socket,$this->bind_host);

if (!@socket_connect($socket,$this->remote_host,$this->remote_port))
{
$OK FALSE;
}

}
else
{
$socket = @fsockopen$this->remote_host$this->remote_port$sock_errno$sock_errstr10 );
}

if ( !$socket || !$OK )
{
$this->error[] = "Can't create socket connection to $this->remote_host:$this->remote_port.";
return 0;
}

if ( isset($this->remote_uname) && isset($this->remote_passwd) )
{
$array_headers['Authorization'] = 'Basic '.base64_encode("$this->remote_uname:$this->remote_passwd");
}

if ( isset($this->remote_uname) && $this->remote_passwd == NULL )
{
$array_headers['Cookie'] = "session={$_SERVER['SESSION_ID']}; key={$_SERVER['SESSION_KEY']}";
}

if ( $this->method == 'POST' )
{
$array_headers['Content-type'] = 'application/x-www-form-urlencoded';
$array_headers['Content-length'] = strlen($content);
}
else
{
if ($content)
{
$request .= "?$content";
}
}

$query "$this->method $request HTTP/1.0\r\n";
foreach ( $array_headers as $key => $value )
{
$query .= "$key: $value\r\n";
}
$query .= "\r\n";

if ( $this->method == 'POST' && $content )
{
$query .= "$content\r\n\r\n";
}

if ($this->bind_host)
{
socket_write($socket,$query);

while ( $out socket_read($socket,2048) )
{
$this->result .= $out;
}
}
else
{
fwrite$socket$querystrlen($query) );

$this->lastTransferSpeed 0;
$status socket_get_status($socket);
$startTime time();
$length 0;
$prevSecond 0;
while ( !feof($socket) && !$status['timed_out'] )
{
$chunk fgets($socket,1024);
$length += strlen($chunk);
$this->result .= $chunk;

$elapsedTime time() - $startTime;

if ( $elapsedTime )
{
$this->lastTransferSpeed = ($length/1024)/$elapsedTime;
}

if ( $doSpeedCheck && $elapsedTime && $this->lastTransferSpeed $doSpeedCheck )
{
$this->warn[] = "kB/s for last 5 seconds is below 50 kB/s (~".( ($length/1024)/$elapsedTime )."), dropping connection...";
$this->result_status_code 503;
break;
}

}

if ( $this->lastTransferSpeed == )
{
$this->lastTransferSpeed $length/1024;
}

}

list($this->result_header,$this->result_body) = split("\r\n\r\n",$this->result,2);

if ($this->bind_host)
{
socket_close($socket);
}
else
{
fclose($socket);
}

$this->query_cache[] = $query;


$headers $this->fetch_header();

if (!$this->result_status_code)
{
preg_match("#HTTP/1\.. (\d+)#",$headers[0],$matches);
$this->result_status_code $matches[1];
}

if ( !empty($headers['content-length']) && $headers['content-length'] != strlen($this->result_body) )
{
$this->result_status_code 206;
}

if ($this->doFollowLocationHeader)
{
if ($headers['location'])
{
$this->redirectURL $headers['location'];
$this->query($headers['location']);
}
}

}

function getTransferSpeed()
{
return $this->lastTransferSpeed;
}

function get($location$asArray FALSE )
{
$this->query($location);

if ( $this->get_status_code() == 200 )
{
if ($asArray)
{
return split("\n",$this->fetch_body());
}

return $this->fetch_body();
}

return FALSE;
}

function get_status_code()
{
return $this->result_status_code;
}

function add_header($key,$value)
{
$this->extra_headers[$key] = $value;
}

function clear_headers()
{
$this->extra_headers = array();
}

function fetch_result()
{
return $this->result;
}

function fetch_header$header '' )
{
$array_headers split("\r\n",$this->result_header);

$array_return = array( => $array_headers[0] );
unset($array_headers[0]);

foreach ( $array_headers as $pair )
{
list($key,$value) = split(": ",$pair,2);
$array_return[strtolower($key)] = $value;
}

if ( $header != '' )
{
return $array_return[strtolower($header)];
}

return $array_return;
}


function fetch_body()
{
return $this->result_body;
}


function fetch_parsed_body()
{
parse_str($this->result_body,$x);
return $x;
}

}

?>

J'ai fait plusieurs tests, il crée bien le sous-domaine sous directadmin, ainsi que le dossier présent la racine de votre compte.
Le sous-domaine fonctionne immédiatement.

A vous de transformer ce script afin qu'il s'adapte à votre situation.

Cordialement

Offline staff

  • Tech. EdelweissHosting
  • Administrator
  • Hero Member
  • *****
  • Posts: 6048
  • Karma: 50
    • View Profile
    • EdelweissHosting
Re : Création sous-domaine automatique, enfin !!
« Reply #1 on: Février 06, 2006, 10:02:05 pm »
Merci :)

A utiliser avec beaucoup beaucoup de précautions quand même. Il est important de faire un screening manuel des comptes avant de les créer.
Membre Staff EdelweissHosting
Site Principal: http://edelweisshosting.com
Votre IP: http://edelweisshosting.com/ip.php

Offline ilestla

  • Newbie
  • *
  • Posts: 16
  • Karma: 0
    • View Profile
Re : Création sous-domaine automatique, enfin !!
« Reply #2 on: Mars 01, 2006, 01:07:27 am »
Super bien ce script ce qui serait un plus serait  l'envoie de mail des parametres.
Merci et bon courage.

Offline cedric

  • Revendeurs
  • Full Member
  • ******
  • Posts: 249
  • Karma: 4
    • View Profile
Re : Création sous-domaine automatique, enfin !!
« Reply #3 on: Mars 03, 2006, 09:22:45 am »
Hello,

Quel méthode me conseillez-vous d'utiliser :

httpsocket ou CURL

Meric d'avance

Offline staff

  • Tech. EdelweissHosting
  • Administrator
  • Hero Member
  • *****
  • Posts: 6048
  • Karma: 50
    • View Profile
    • EdelweissHosting
Re : Création sous-domaine automatique, enfin !!
« Reply #4 on: Mars 03, 2006, 04:43:39 pm »
Bonjour

CURL vous l'appellez depuis l'URL ?
Membre Staff EdelweissHosting
Site Principal: http://edelweisshosting.com
Votre IP: http://edelweisshosting.com/ip.php

Offline bebs

  • Jr. Member
  • **
  • Posts: 50
  • Karma: 2
    • View Profile
Re : Création sous-domaine automatique, enfin !!
« Reply #5 on: Mars 21, 2006, 01:43:05 pm »
Désolé pour cette réponse tardive.

Quel méthode me conseillez-vous d'utiliser :  httpsocket ou CURL

Aucune idée... je ne pense pas que httpsocket permette de se connecter vers httpS (secure socket), et je ne connais pas de librairie httpSecureSocket... donc plutôt cURL en ce qui me concerne.


CURL vous l'appellez depuis l'URL ?

non, directement depuis php dans la ligne:
$ch = curl_init();

qui crée une "session" cURL, à laquelle je fourni tous les paramètres nécessaires pour une connexion sécurisée (url, login, mdp...) et qui se connecte ensuite pour effectuer la commande, comme le ferait DirectAdmin.

en espérant que ce soit clair...
bonne journée.
« Last Edit: Mars 21, 2006, 01:52:12 pm by bebs »