No to tak - postanowiłem napisac sobie takie coś co pomoże mi wykorzystać webmin'a, a w zasadzie virtualmin'a (darmowe rozwiązanie do zarządzania serwerem) w oparciu o api - remote.cgi. ->
http://www.virtualmin.com/documentation/developer/cliTak więc nie chciałem robić niczego nad wyrost, a klasa ma za zadanie tylko i wyłącznie komunikować aplikację z api i odbierać ewentualne komunikaty...
Ze względu na bardziej złorzoną strukturę, postanowiłem całość okroić do minimum, aby pokazać swoje wypociny, a nuż widelec komuś się przyda
Klasa o nazwie singleton - sluży jedynie jako "pośrednik" w dostarczaniu danych (bo jak powyżej napisałem, nie chciałem tego okroić nadmiernie).
singleton.class.php<?php
class singleton {
/**
* Zmienna / kontener
*
* @var array
*/
/**
* Konstruktor..
*/
private function __construct() {}
/**
* Wysyla zmienna do odczytu
*
* @param string $name
* @return array|string|bool
*/
static public function __getVar
( $name=false ) { try {
if( !self::$var ) {
self::$var = new self;
}
if( !$name ) {
return self::$var;
} else {
if( !isset(self::$var->$name) ) { return false;
} else {
return self::$var->$name;
}
}
}
catch( Exception $e ) {
}
}
/**
* Wysyla zmienna do przechowania
*
* @param stringe $name
* @param var $value
* @return bool
*/
static public function __setVar
( $name=false, $value=false ) { try {
if( !$name || !$value ) {
return false;
}
if( !self::$var ) {
self::$var = new singleton;
}
if( isset(self::$var->$name) ) { return false;
} else {
self::$var->$name = $value;
self::$var->$name = array2object($value);
self::$var->$name = $value;
}
}
return true;
}
catch( Exception $e ) {
}
}
static public function __unsetVar
( $name=false ) { try {
if( !self::$var ) {
self::$var = new singleton;
}
if( isset(self::$var->$name) ) { unset( self::$var->$name ); }
return true;
}
catch( Exception $e ) {
}
}
static public function __isNull
( $name ) { try {
$null = 0;
$notNull = 0;
if( self::__getVar( $name ) ) {
foreach( self::__getVar( $name ) as $value ) {
if( !$value ) {
++$null;
} else {
++$notNull;
}
}
}
'null' => $null,
'notNull' => $notNull
);
}
catch( Exception $e ) {
}
}
}
?>
No i to co jest najistotniejsze - a więc:
virtualmin.class.php<?php
/**
*
* VirtualMin Class Api <lol2> ;]
*
* @version 0.1
*/
class virtualmin {
private function __construct() {
try {
$this->vm = curl_init();
}
catch( Exception $e ) {
err( $e );
}
}
public function __destruct() {
try {
if( !self::$vm ) {
self::$vm = new self;
}
curl_close(self::$vm->vm);
}
catch( Exception $e ) {
err( $e );
}
}
private function url( $program ) {
try {
if( !self::$vm ) {
self::$vm = new self;
}
$url = singleton::__getVar('virtualMin')->url;
$url.= '?program=' . self::$vm->program;
if( !empty(self::$vm->param) ) { foreach( self::$vm->param as $key => $value ) {
$url.= '&' . $key . '=' . $value;
}
}
if( $program->multiline ) {
$url.= '&multiline=';
}
$url.= '&json=1';
return $url;
}
catch( Exception $e ) {
err( $e );
}
}
private function connect( $program ) {
try {
if( !self::$vm ) {
self::$vm = new self;
}
curl_setopt(self::$vm->vm, CURLOPT_URL, self::url( $program ) );
curl_setopt(self::$vm->vm, CURLOPT_RETURNTRANSFER, 1);
curl_setopt(self::$vm->vm, CURLOPT_USERPWD, singleton::__getVar('virtualMin')->user . ':' . singleton::__getVar('virtualMin')->passwd );
if( singleton::__getVar('virtualMin')->ssl ) {
curl_setopt(self::$vm->vm, CURLOPT_SSL_VERIFYPEER, false);
}
return curl_exec(self::$vm->vm);
}
catch( Exception $e ) {
err( $e );
}
}
private function testParam( $obj, $param ) {
try {
foreach( $obj->fields as $key => $value ) {
if( !isset($param[$key]) && $value ) { return false;
}
}
return true;
}
catch( Exception $e ) {
err( $e );
}
}
/**
* Wysyla zapytanie
*
* @param array $param['user']
*/
public function send
( array $param, $program ) { try {
if( !self::$vm ) {
self::$vm = new self;
}
if( !class_exists($program) ) {
throw new Exception('Nieznany interfejs virtualmin');
} else {
include_once '../mod/hosting/lib/virtualmin.' . strtolower($program) . '.class.php'; }
}
$program = new $program;
if( !self::testParam( $program, $param ) ) {
throw new Exception('Bledne parametry');
}
self::$vm->program = $program->name;
self::$vm->param = $param;
$json = self::connect( $program );
return json_decode($json);
}
catch( Exception $e ) {
err( $e );
}
}
}
?>
No i do tego kilka plików które są wczytywane względem nazwy metody:
virtualmin.createdomain.class.php<?php
class createDomain {
public $name = 'create-domain';
public $multiline = false;
'domain' => true,
'user' => true,
'pass' => true,
'parent' => false,
'features-from-plan' => false,
'plan' => true
);
}
?>
virtualmin.createdomain.class.php<?php
class deleteDomain {
public $name = 'delete-domain';
public $multiline = false;
'domain' => true
);
}
?>
virtualmin.deletedomain.class.php<?php
class deleteDomain {
public $name = 'delete-domain';
public $multiline = false;
'domain' => true
);
}
?>
virtualmin.getdns.class.php<?php
class getDns {
public $name = 'get-dns';
public $multiline = true;
'domain' => true
);
}
?>
virtualmin.listdomain.class.php<?php
class listDomain {
public $name = 'list-domains';
public $multiline = false;
'user' => true,
'domain' => true
);
}
?>
virtualmin.listplans.class.php<?php
class listPlans {
public $name = 'list-plans';
public $multiline = true;
'name' => false
);
}
?>
virtualmin.modifydns.class.php<?php
class modifyDns {
public $name = 'modify-dns';
public $multiline = true;
'domain' => true,
'add-record' => false,
'remove-record' => false
);
}
?>
A teraz jak z tego skorzystać:
<?php
include_once 'singleton.class.php';
include_once 'virtualmin.class.php';
singleton
::__setVar
('virtualMin', array( 'url' => 'https://twoj.server.pl:10000/virtual-server/remote.cgi',
'ssl' => true,
'user' => 'root',
'passwd' => '**********'
));
// Dodawanie serwera lub subserwera wirtualnego + zakladanie uzytkownika
'domain' => 'test-1234.pl',
'user' => 'test-1234',
'pass' => '123456',
// 'parent' => 'subdomain.test-1234.pl', // Odchaszowac jesli to ma byc dodane jako subserwer - w katalogu domains
'features-from-plan' => null,
'plan' => 'Default Plan'
);
var_dump( virtualmin
::send( $param, 'createDomain' ) );
// LISTA DOMEN
'user' => 'test-1234'
);
var_dump( virtualmin
::send( $param, 'listDomain' ) );
// Kasowanie serwera wirtualnego lub sub serwera - wystarczy wstawic odpowiednia nazwe domeny
'domain' => 'test-123.pl'
);
var_dump(virtualmin
::send( $param, 'deleteDomain' ));
// Wyswietlanie rekordow dns dla konkretnej domeny
'domain' => 'test-1234.pl',
);
var_dump(virtualmin
::send( $param, 'getDns' ));
// Edycja rekordow dns
'domain' => 'test-1234.pl',
'add-record' => '"ftp2.test-1234.pl. A 127.0.0.1"',
//'remove-record' => '"ftp.test-1234.pl. A"',
'multiline' => false
);
var_dump(virtualmin
::send( $param, 'modifyDns' ));
?>
Powyższe example pozwala na:
1. dodanie wirtualnego serwera wraz z użytkownikiem (zakładając że jesteś adminem
)
2. listowanie wirtualnych serwerów i subserwerów
3. usuwanie serwerów wirtualnych i subserwerów
4. listowanie stref dns
5. edycje i usuwanie rekordów w dns'ach
Mam nadzieję ze się komuś przyda - bo niestety nie znalazłem nic gotowego, a jest mi potrzebne do własnego projektu...
Ten post edytował sniver 20.05.2012, 11:48:55