Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [klasa] klasa do obsługi virtualmin'a z webmina + moj singleton
sniver
post 20.05.2012, 11:46:31
Post #1





Grupa: Zarejestrowani
Postów: 159
Pomógł: 5
Dołączył: 31.08.2007

Ostrzeżenie: (0%)
-----


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/cli

Tak 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 smile.gif

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
  1. <?php
  2.  
  3. class singleton {
  4.  
  5. /**
  6. * Zmienna / kontener
  7. *
  8. * @var array
  9. */
  10. static public $var = array();
  11.  
  12.  
  13. /**
  14. * Konstruktor..
  15. */
  16. private function __construct() {}
  17.  
  18.  
  19. /**
  20. * Wysyla zmienna do odczytu
  21. *
  22. * @param string $name
  23. * @return array|string|bool
  24. */
  25. static public function __getVar( $name=false ) {
  26. try {
  27.  
  28. if( !self::$var ) {
  29. self::$var = new self;
  30. }
  31.  
  32. if( !$name ) {
  33. return self::$var;
  34. } else {
  35. if( !isset(self::$var->$name) ) {
  36. return false;
  37. } else {
  38. return self::$var->$name;
  39. }
  40. }
  41. }
  42. catch( Exception $e ) {
  43. var_dump($e);
  44. }
  45. }
  46.  
  47. /**
  48. * Wysyla zmienna do przechowania
  49. *
  50. * @param stringe $name
  51. * @param var $value
  52. * @return bool
  53. */
  54. static public function __setVar( $name=false, $value=false ) {
  55. try {
  56.  
  57. if( !$name || !$value ) {
  58. return false;
  59. }
  60.  
  61. if( !self::$var ) {
  62. self::$var = new singleton;
  63. }
  64.  
  65. if( isset(self::$var->$name) ) {
  66. return false;
  67. } else {
  68. if( is_string($value) ) {
  69. self::$var->$name = $value;
  70. } elseif( is_array($value) ) {
  71. self::$var->$name = array2object($value);
  72. } elseif( is_object($value) ) {
  73. self::$var->$name = $value;
  74. }
  75. }
  76.  
  77. return true;
  78. }
  79. catch( Exception $e ) {
  80. var_dump($e);
  81. }
  82. }
  83.  
  84.  
  85. static public function __unsetVar( $name=false ) {
  86. try {
  87.  
  88. if( !self::$var ) {
  89. self::$var = new singleton;
  90. }
  91.  
  92. if( isset(self::$var->$name) ) {
  93. unset( self::$var->$name );
  94. }
  95.  
  96. return true;
  97. }
  98. catch( Exception $e ) {
  99. var_dump( $e );
  100. }
  101. }
  102.  
  103.  
  104. static public function __isNull( $name ) {
  105. try {
  106.  
  107. $null = 0;
  108. $notNull = 0;
  109. if( self::__getVar( $name ) ) {
  110. foreach( self::__getVar( $name ) as $value ) {
  111. if( !$value ) {
  112. ++$null;
  113. } else {
  114. ++$notNull;
  115. }
  116. }
  117. }
  118.  
  119. return array(
  120. 'null' => $null,
  121. 'notNull' => $notNull
  122. );
  123. }
  124. catch( Exception $e ) {
  125. var_dump($e);
  126. }
  127. }
  128.  
  129. }
  130.  
  131. ?>




No i to co jest najistotniejsze - a więc:
virtualmin.class.php
  1. <?php
  2.  
  3. /**
  4. *
  5. * VirtualMin Class Api <lol2> ;]
  6. *
  7. * @version 0.1
  8. */
  9. class virtualmin {
  10.  
  11.  
  12. private static $vm = false;
  13.  
  14.  
  15. private function __construct() {
  16. try {
  17.  
  18. $this->vm = curl_init();
  19.  
  20.  
  21. }
  22. catch( Exception $e ) {
  23. err( $e );
  24. }
  25. }
  26.  
  27.  
  28. public function __destruct() {
  29. try {
  30.  
  31. if( !self::$vm ) {
  32. self::$vm = new self;
  33. }
  34.  
  35. curl_close(self::$vm->vm);
  36.  
  37. }
  38. catch( Exception $e ) {
  39. err( $e );
  40. }
  41. }
  42.  
  43.  
  44. private function url( $program ) {
  45. try {
  46.  
  47. if( !self::$vm ) {
  48. self::$vm = new self;
  49. }
  50.  
  51.  
  52. $url = singleton::__getVar('virtualMin')->url;
  53. $url.= '?program=' . self::$vm->program;
  54. if( !empty(self::$vm->param) ) {
  55. foreach( self::$vm->param as $key => $value ) {
  56. $url.= '&' . $key . '=' . $value;
  57. }
  58. }
  59.  
  60. if( $program->multiline ) {
  61. $url.= '&multiline=';
  62. }
  63.  
  64. $url.= '&json=1';
  65.  
  66. return $url;
  67.  
  68. }
  69. catch( Exception $e ) {
  70. err( $e );
  71. }
  72. }
  73.  
  74.  
  75. private function connect( $program ) {
  76. try {
  77.  
  78. if( !self::$vm ) {
  79. self::$vm = new self;
  80. }
  81.  
  82. curl_setopt(self::$vm->vm, CURLOPT_URL, self::url( $program ) );
  83. curl_setopt(self::$vm->vm, CURLOPT_RETURNTRANSFER, 1);
  84. curl_setopt(self::$vm->vm, CURLOPT_USERPWD, singleton::__getVar('virtualMin')->user . ':' . singleton::__getVar('virtualMin')->passwd );
  85.  
  86. if( singleton::__getVar('virtualMin')->ssl ) {
  87. curl_setopt(self::$vm->vm, CURLOPT_SSL_VERIFYPEER, false);
  88. }
  89.  
  90. return curl_exec(self::$vm->vm);
  91.  
  92. }
  93. catch( Exception $e ) {
  94. err( $e );
  95. }
  96. }
  97.  
  98.  
  99. private function testParam( $obj, $param ) {
  100. try {
  101.  
  102. foreach( $obj->fields as $key => $value ) {
  103. if( !isset($param[$key]) && $value ) {
  104. return false;
  105. }
  106. }
  107.  
  108. return true;
  109.  
  110. }
  111. catch( Exception $e ) {
  112. err( $e );
  113. }
  114. }
  115.  
  116.  
  117.  
  118. /**
  119. * Wysyla zapytanie
  120. *
  121. * @param array $param['user']
  122. */
  123. public function send( array $param, $program ) {
  124. try {
  125.  
  126. if( !self::$vm ) {
  127. self::$vm = new self;
  128. }
  129.  
  130.  
  131.  
  132. if( !class_exists($program) ) {
  133. if( !file_exists('../mod/hosting/lib/virtualmin.' . strtolower($program) . '.class.php') ) {
  134. throw new Exception('Nieznany interfejs virtualmin');
  135. } else {
  136. include_once '../mod/hosting/lib/virtualmin.' . strtolower($program) . '.class.php';
  137. }
  138. }
  139.  
  140. $program = new $program;
  141.  
  142. if( !self::testParam( $program, $param ) ) {
  143. throw new Exception('Bledne parametry');
  144. }
  145.  
  146. self::$vm->program = $program->name;
  147. self::$vm->param = $param;
  148.  
  149. $json = self::connect( $program );
  150. return json_decode($json);
  151.  
  152. }
  153. catch( Exception $e ) {
  154. err( $e );
  155. }
  156. }
  157.  
  158.  
  159. }
  160.  
  161. ?>


No i do tego kilka plików które są wczytywane względem nazwy metody:


virtualmin.createdomain.class.php
  1. <?php
  2.  
  3. class createDomain {
  4.  
  5. public $name = 'create-domain';
  6.  
  7. public $multiline = false;
  8.  
  9. public $fields = array(
  10. 'domain' => true,
  11. 'user' => true,
  12. 'pass' => true,
  13. 'parent' => false,
  14. 'features-from-plan' => false,
  15. 'plan' => true
  16. );
  17.  
  18. }
  19.  
  20. ?>


virtualmin.createdomain.class.php
  1. <?php
  2.  
  3. class deleteDomain {
  4.  
  5. public $name = 'delete-domain';
  6.  
  7. public $multiline = false;
  8.  
  9. public $fields = array(
  10. 'domain' => true
  11. );
  12.  
  13. }
  14.  
  15. ?>



virtualmin.deletedomain.class.php
  1. <?php
  2.  
  3. class deleteDomain {
  4.  
  5. public $name = 'delete-domain';
  6.  
  7. public $multiline = false;
  8.  
  9. public $fields = array(
  10. 'domain' => true
  11. );
  12.  
  13. }
  14.  
  15. ?>


virtualmin.getdns.class.php
  1. <?php
  2.  
  3. class getDns {
  4.  
  5. public $name = 'get-dns';
  6.  
  7. public $multiline = true;
  8.  
  9. public $fields = array(
  10. 'domain' => true
  11. );
  12.  
  13. }
  14.  
  15. ?>



virtualmin.listdomain.class.php
  1. <?php
  2.  
  3. class listDomain {
  4.  
  5. public $name = 'list-domains';
  6.  
  7. public $multiline = false;
  8.  
  9. public $fields = array(
  10. 'user' => true,
  11. 'domain' => true
  12. );
  13.  
  14. }
  15.  
  16. ?>



virtualmin.listplans.class.php
  1. <?php
  2.  
  3. class listPlans {
  4.  
  5. public $name = 'list-plans';
  6.  
  7. public $multiline = true;
  8.  
  9. public $fields = array(
  10. 'name' => false
  11. );
  12.  
  13. }
  14.  
  15. ?>



virtualmin.modifydns.class.php
  1. <?php
  2.  
  3. class modifyDns {
  4.  
  5. public $name = 'modify-dns';
  6.  
  7. public $multiline = true;
  8.  
  9. public $fields = array(
  10. 'domain' => true,
  11. 'add-record' => false,
  12. 'remove-record' => false
  13. );
  14.  
  15. }
  16.  
  17. ?>



A teraz jak z tego skorzystać:
  1. <?php
  2.  
  3. include_once 'singleton.class.php';
  4. include_once 'virtualmin.class.php';
  5.  
  6. singleton::__setVar('virtualMin', array(
  7. 'url' => 'https://twoj.server.pl:10000/virtual-server/remote.cgi',
  8. 'ssl' => true,
  9. 'user' => 'root',
  10. 'passwd' => '**********'
  11. ));
  12.  
  13.  
  14.  
  15. // Dodawanie serwera lub subserwera wirtualnego + zakladanie uzytkownika
  16. $param = array(
  17. 'domain' => 'test-1234.pl',
  18. 'user' => 'test-1234',
  19. 'pass' => '123456',
  20. // 'parent' => 'subdomain.test-1234.pl', // Odchaszowac jesli to ma byc dodane jako subserwer - w katalogu domains
  21. 'features-from-plan' => null,
  22. 'plan' => 'Default Plan'
  23. );
  24.  
  25. echo '<pre>';
  26. var_dump( virtualmin::send( $param, 'createDomain' ) );
  27. echo '</pre>';
  28.  
  29.  
  30.  
  31. // LISTA DOMEN
  32. $param = array(
  33. 'user' => 'test-1234'
  34. );
  35.  
  36. echo '<pre>';
  37. var_dump( virtualmin::send( $param, 'listDomain' ) );
  38. echo '</pre>';
  39.  
  40.  
  41.  
  42. // Kasowanie serwera wirtualnego lub sub serwera - wystarczy wstawic odpowiednia nazwe domeny
  43. $param = array(
  44. 'domain' => 'test-123.pl'
  45. );
  46.  
  47. echo '<pre>';
  48. var_dump(virtualmin::send( $param, 'deleteDomain' ));
  49. echo '<pre>';
  50.  
  51.  
  52. // Wyswietlanie rekordow dns dla konkretnej domeny
  53. $param = array(
  54. 'domain' => 'test-1234.pl',
  55. );
  56.  
  57. echo '<pre>';
  58. var_dump(virtualmin::send( $param, 'getDns' ));
  59. echo '</pre>';
  60.  
  61.  
  62. // Edycja rekordow dns
  63. $param = array(
  64. 'domain' => 'test-1234.pl',
  65. 'add-record' => '"ftp2.test-1234.pl. A 127.0.0.1"',
  66. //'remove-record' => '"ftp.test-1234.pl. A"',
  67. 'multiline' => false
  68. );
  69.  
  70. echo '<pre>';
  71. var_dump(virtualmin::send( $param, 'modifyDns' ));
  72. echo '</pre>';
  73.  
  74. ?>

Powyższe example pozwala na:
1. dodanie wirtualnego serwera wraz z użytkownikiem (zakładając że jesteś adminem smile.gif)
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


--------------------
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 23.04.2024 - 13:45