|
|
@@ -0,0 +1,208 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+/*
|
|
|
+Klasa do zarzadania konfiguracja serveradmin
|
|
|
+
|
|
|
+Przyklad uzycia:
|
|
|
+$test = new ServeradminParser();
|
|
|
+$test->dnsDelIpAddress("procesy5.pl", "demo3"); - usuwa wszystkie wpisy A dla hosta demo3.procesy5.pl w domenie procesy5.pl
|
|
|
+$test->dnsDelIpAddress("biall.pl", "", "94.158.130.34"); - usuwa adres IP z rekordu biall..pl
|
|
|
+$test->dnsAddIpAddress("biall.pl", "", "94.158.130.34"); - dodaje adres IP do rekordu biall.pl
|
|
|
+
|
|
|
+echo $test->getConf();
|
|
|
+
|
|
|
+echo $test->applyConf();
|
|
|
+
|
|
|
+*/
|
|
|
+class ServeradminParser {
|
|
|
+
|
|
|
+ private $data = Array();
|
|
|
+ private $result;
|
|
|
+
|
|
|
+ // Funkcja inicjujaca objekt
|
|
|
+ public function serveradminParser($key = "") {
|
|
|
+ if (strlen($key)) $this->getData($key); // Opcjonalne pobranie danych z serveradmin
|
|
|
+ }
|
|
|
+
|
|
|
+ // Funkcja pobierajaca dane z serveradmin i wywolujaca funkcje parsujaca dane tekstowe do arraya
|
|
|
+ private function getData($key) {
|
|
|
+ $conf = shell_exec("sudo serveradmin set ".$key);
|
|
|
+ $this->data = $this->txtToArr($conf);
|
|
|
+ }
|
|
|
+
|
|
|
+ // Funkcja parsujaca dane tekstowe do arraya
|
|
|
+ private function txtToArr($conf) {
|
|
|
+ $data = Array();
|
|
|
+ $lines = explode("\n", $conf);
|
|
|
+ foreach ($lines as $line) {
|
|
|
+ $lineArr = explode(' = ', $line);
|
|
|
+ if (count($lineArr)==2) {
|
|
|
+ $keys=$lineArr[0];
|
|
|
+ $value=$lineArr[1];
|
|
|
+ $keysArr = explode(':', $keys);
|
|
|
+
|
|
|
+ $lastRoot = & $data;
|
|
|
+ $lastKey = array_pop($keysArr);
|
|
|
+
|
|
|
+ foreach ($keysArr as $key) {
|
|
|
+ if (!array_key_exists($key, $lastRoot)) $lastRoot[$key] = Array();
|
|
|
+ $lastRoot = & $lastRoot[$key];
|
|
|
+ }
|
|
|
+ $lastRoot[$lastKey] = $value;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Funkcja konwertujaca dane z arraya do postaci tekstowej rozumianej przez serveradmin
|
|
|
+ private function arrToTxt($arr, $keys = Array(), $key = "") {
|
|
|
+ if (strlen($key)) $keys[] = $key;
|
|
|
+ foreach ($arr as $key => $value) {
|
|
|
+ if (is_array($value)) $this->arrToTxt($value, $keys, $key);
|
|
|
+ else $this->result .= implode(":", array_merge($keys,array($key)))." = ".$value."\n";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Funkcja zwracajaca bierzaca konfiguracje do serveradmin (testowa public)
|
|
|
+ public function getConf() {
|
|
|
+ $this->result="";
|
|
|
+ $this->arrToTxt($this->data);
|
|
|
+ return $this->result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function applyConf() {
|
|
|
+ $conf = $this->getConf();
|
|
|
+ $result = shell_exec("echo '".$conf."' | serveradmin set");
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Funkcja dodajaca (ew. modyfikujaca) wpis A (adres IP) w konfiguracji DNS
|
|
|
+ public function dnsAddIpAddress($domain, $host, $ip, $replace = 0) {
|
|
|
+ // Weryfikacja argumentow funkcji (TODO nalezy upsrawnic)
|
|
|
+ if (!strlen($domain)) throw new Exception("Bad domain.");
|
|
|
+ if (!strlen($ip)) throw new Exception("Bad IP address.");
|
|
|
+
|
|
|
+ // Pobranie danych z serverconfig
|
|
|
+ $this->getData("dns:views:_array_id:com.apple.ServerAdmin.DNS.public:primaryZones:_array_id:".$domain.":machines");
|
|
|
+
|
|
|
+ // Sprawdzenie czy domena istnieje, jezeli nie - wyrucenie wyjatku
|
|
|
+ if (!isset($this->data["dns"]["views"]["_array_id"]["com.apple.ServerAdmin.DNS.public"]["primaryZones"]["_array_id"][$domain])) {
|
|
|
+ $this->data = Array();
|
|
|
+ throw new Exception("Domain ".$domain." not found");
|
|
|
+ }
|
|
|
+
|
|
|
+ // Sprawdzenie czy istnieja jakiekolwiek wpisy A (adresy IP) dla domeny. Jezeli nie - zainicjowanie pustej tablicy
|
|
|
+ if (!is_array($this->data["dns"]["views"]["_array_id"]["com.apple.ServerAdmin.DNS.public"]["primaryZones"]["_array_id"][$domain]["machines"])) {
|
|
|
+ unset($this->data["dns"]["views"]["_array_id"]["com.apple.ServerAdmin.DNS.public"]["primaryZones"]["_array_id"][$domain]["machines"]);
|
|
|
+ $this->data["dns"]["views"]["_array_id"]["com.apple.ServerAdmin.DNS.public"]["primaryZones"]["_array_id"][$domain]["machines"]["_array_index"] = Array();
|
|
|
+ }
|
|
|
+
|
|
|
+ // Ustawienie wskaznika na tablicy wpisow A
|
|
|
+ $domainArr = & $this->data["dns"]["views"]["_array_id"]["com.apple.ServerAdmin.DNS.public"]["primaryZones"]["_array_id"][$domain]["machines"]["_array_index"];
|
|
|
+
|
|
|
+ // Wartosci przekazywane do serveradmin musza byc w nawiasach
|
|
|
+ if (strlen($host)) $name = '"'.$host.'.'.$domain.'."';
|
|
|
+ else $name = '"'.$domain.'."';
|
|
|
+ $ipAddress = '"'.$ip.'"';
|
|
|
+
|
|
|
+ // Sprawdzenie czy istnieje juz jakis wpis A dla danego hosta
|
|
|
+ unset ($ipArr);
|
|
|
+ foreach ($domainArr as & $domainEl) {
|
|
|
+ if ($domainEl["name"] == $name) {
|
|
|
+ $ipArr = & $domainEl["ipAddresses"]["_array_index"]; // Ustawiamy wskaznik do tablicy adresow IP dla danego hosta (oznacza to istnienie danego hosta)
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Jezeli dany host juz istnieje, dodajemy kolejny adres IP
|
|
|
+ if (isset($ipArr)) {
|
|
|
+ foreach ($ipArr as $ipEl) {
|
|
|
+ if ($ipEl["ipAddress"] == $ipAddress) return false; // Jezeli konkretny wpis dla danego adresu IP juz istnieje, konczymy dzialanie i zwracamy false
|
|
|
+ }
|
|
|
+ if ($replace) $ipArr = Array(); // Jezeli funkcja zostala wywolana z replace==true, usuwamy wszystkie dotychczasowe wpisy A dla danego hosta
|
|
|
+ $ipArr[]["ipAddress"] = $ipAddress;
|
|
|
+ } else { // Jezeli dany host nie istnieje, definiujemy nowey i dodajemy adres IP
|
|
|
+ $ipAddresses["_array_index"][]["ipAddress"] = $ipAddress;
|
|
|
+ $domainArr[] = Array('name' => $name, 'ipAddresses' => $ipAddresses);
|
|
|
+ }
|
|
|
+ return true; // Rzadany rekord zostal pomyslnie dodany i zapisany w zmiennej $this->data
|
|
|
+ }
|
|
|
+
|
|
|
+ // Funkcja usuwajaca wpis A (adres IP) z konfiguracji DNS
|
|
|
+ public function dnsDelIpAddress($domain, $host, $ip, $replace = false) {
|
|
|
+ // Weryfikacja argumentow funkcji (TODO nalezy upsrawnic)
|
|
|
+ if (!strlen($domain)) throw new Exception("Bad domain.");
|
|
|
+ if (!strlen($ip)) throw new Exception("Bad IP address.");
|
|
|
+
|
|
|
+ // Pobranie danych z serverconfig
|
|
|
+ $this->getData("dns:views:_array_id:com.apple.ServerAdmin.DNS.public:primaryZones:_array_id:".$domain.":machines");
|
|
|
+
|
|
|
+ // Sprawdzenie czy domena istnieje, jezeli nie - wyrucenie wyjatku
|
|
|
+ if (!isset($this->data["dns"]["views"]["_array_id"]["com.apple.ServerAdmin.DNS.public"]["primaryZones"]["_array_id"][$domain])) {
|
|
|
+ $this->data = Array();
|
|
|
+ throw new Exception("Domain ".$domain." not found");
|
|
|
+ }
|
|
|
+
|
|
|
+ // Sprawdzenie czy istnieja jakiekolwiek wpisy A (adresy IP) dla domeny. Jezeli nie - wyrzucenie wyjatku
|
|
|
+ if (!isset($this->data["dns"]["views"]["_array_id"]["com.apple.ServerAdmin.DNS.public"]["primaryZones"]["_array_id"][$domain]["machines"])) {
|
|
|
+ throw new Exception("Domain ".$domain." has no A entries");
|
|
|
+ }
|
|
|
+
|
|
|
+ // Ustawienie wskaznika na tablicy wpisow A
|
|
|
+ $domainArr = & $this->data["dns"]["views"]["_array_id"]["com.apple.ServerAdmin.DNS.public"]["primaryZones"]["_array_id"][$domain]["machines"]["_array_index"];
|
|
|
+
|
|
|
+ // Wartosci przekazywane do serveradmin musza byc w nawiasach
|
|
|
+ if (strlen($host)) $name = '"'.$host.'.'.$domain.'."';
|
|
|
+ else $name = '"'.$domain.'."';
|
|
|
+ $ipAddress = '"'.$ip.'"';
|
|
|
+
|
|
|
+ // Wyszukanie czy istnieje jakikolwiek wpis A dla danego hosta i znalezienie klucza w tablicy
|
|
|
+ unset ($hostKey);
|
|
|
+ foreach ($domainArr as $key => $domainEl) {
|
|
|
+ if ($domainEl["name"] == $name) {
|
|
|
+ $hostKey = $key;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Jezeli nie znaleziono danego hosta, wyrzucamy wyjatek.
|
|
|
+ if (!isset($hostKey)) throw new Exception("No A entries found for host ".$host.".".$domain.".");
|
|
|
+
|
|
|
+ // Jezeli nie zdefiniowano konkretnego adres IP do usuniecia, od razu usuwamy wszystkie wpisy A dla dajego hosta (przesuwamy ostatni element tablicy w miejsce danego wpisu)
|
|
|
+ if (!$ip) {
|
|
|
+ $lastKey = max(array_keys($domainArr));
|
|
|
+ $domainArr[$hostKey] = $domainArr[$lastKey];
|
|
|
+ unset($domainArr[$lastKey]);
|
|
|
+ } else {
|
|
|
+ // W przeciwnym wypadku poszukujemy konkretnego rekordu A
|
|
|
+ unset($ipKey);
|
|
|
+ foreach ($domainArr[$hostKey]["ipAddresses"]["_array_index"] as $key => $ipArr) {
|
|
|
+ if ($ipArr["ipAddress"] == $ipAddress) {
|
|
|
+ $ipKey = $key;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Jezelni nie znaleziono danego wpisu A, wyrzucamy wyjatek.
|
|
|
+ if (!isset($ipKey)) throw new Exception("A entry for host ".$host.".".$domain." and IP address ".$ip." not found.");
|
|
|
+
|
|
|
+ // Usuwamy wpis A
|
|
|
+ $lastKey = max(array_keys($domainArr[$hostKey]["ipAddresses"]["_array_index"]));
|
|
|
+ $domainArr[$hostKey]["ipAddresses"]["_array_index"][$ipKey]=$domainArr[$hostKey]["ipAddresses"]["_array_index"][$lastKey];
|
|
|
+ unset($domainArr[$hostKey]["ipAddresses"]["_array_index"][$lastKey]);
|
|
|
+
|
|
|
+ // Jezeli nie pozostal zaden wpis A dla danego hosta, usuwamy z domeny definicje wpisow A dla danego hosta
|
|
|
+ if (!count($domainArr[$hostKey]["ipAddresses"]["_array_index"])) {
|
|
|
+ $lastKey = max(array_keys($domainArr));
|
|
|
+ $domainArr[$hostKey] = $domainArr[$lastKey];
|
|
|
+ unset($domainArr[$lastKey]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Jezeli nie pozostal zaden rekord A dla domeny, nalezy wykasowac tablice i zastapic ja wartoscia _empty_array
|
|
|
+ if (!count($domainArr)) $this->data["dns"]["views"]["_array_id"]["com.apple.ServerAdmin.DNS.public"]["primaryZones"]["_array_id"][$domain]["machines"] = "_empty_array";
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+?>
|