Skip to content

Instantly share code, notes, and snippets.

@levelKro
Created February 4, 2019 11:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save levelKro/b579bded268161717471e1a9f80bf0e2 to your computer and use it in GitHub Desktop.
Save levelKro/b579bded268161717471e1a9f80bf0e2 to your computer and use it in GitHub Desktop.
PHP Virtualmin Class for add/update/delete DNS
<?php
error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT);
ini_set("display_errors", 1);
/*
Virtualmin API Class v1.0
Mathieu Légaré
levelkro@yahoo.ca
https://levelkro.net
Used for change DNS on the fly, modify API result for own purpose.
*/
// Cnfiguration
$cfg=array(
"username"=>"user_change_me", // Root or Virtualmin account
"password"=>"password_change_me", // Pass of account
"host"=>"localhost", // Host to use with Virtualmin, use localhost if same machine
"port"=>"10000", // Port of Virtualmin, default is 10000
"protocol"=>"https", // Use "https" if SSL is installed, or "http"
"banned"=>array("www","mail","ftp","m","admin","webmail","localhost"), // Banned DNS entries
);
// How to load this class, remove before use as an external file
$VMAPI=new virtualmin_API($cfg);
echo "<pre>";
print_r($VMAPI->domain_list());
echo "</pre>";
// Class, do not touch
class virtualmin_API{
private $username=null;
private $password=null;
private $host=null;
private $port=null;
private $banned=null;
private $protocol=null;
public function __call($method, $args){
if (isset($this->$method)) {
$func = $this->$method;
return call_user_func_array($func, $args);
}
}
function __construct($cfg) {
$this->username=$cfg['username'];
$this->password=$cfg['password'];
$this->host=$cfg['host'];
$this->port=$cfg['port'];
$this->protocol=$cfg['protocol'];
$this->banned=$cfg['banned'];
}
// Remove spaces
function apicleanstr($str){
return trim(preg_replace('/\s\s+/', ' ', str_replace("\n", " ", $str)));
}
// Call Virtualmin API
function apicall($values){
$values['json']="1";
$vars="@@";
foreach($values as $n=>$v) $vars.="&".$n."=".$v;
$vars=str_replace("@@&","?",$vars);
$out=json_decode(shell_exec("wget -O - --quiet --http-user=".$this->username." --http-passwd=".$this->password." --no-check-certificate '".$this->protocol."://".$this->host.":".$this->port."/virtual-server/remote.cgi".$vars."'"),true);
if($out['status']=="success") return (($out['data'])?$out['data']:$out);
else return array("error");
}
// Add DNS entry
function dns_add($domain,$dns,$type,$value){
return $this->apicall(array("program"=>"modify-dns","domain"=>$domain,"add-record"=>$dns." ".$type." ".$value));
}
// Remove DNS entry
function dns_rem($domain,$dns,$type){
return $this->apicall(array("program"=>"modify-dns","domain"=>$domain,"remove-record"=>$dns." ".$type));
}
// Update DNS entry (or create)
function dns_update($domain,$dns,$type,$value){
$rem=array("status"=>"Nothing","output"=>"No entrye, nothing to remove.");
if($this->dns_check($domain,$dns,$type)) $rem=$this->dns_rem($domain,$dns,$type);
$add=$this->dns_add($domain,$dns,$type,$value);
return array("rem"=>$rem,"add"=>$add);
}
// List DNS of specified domain
function dns_list($domain,$type="A"){
$raw = $this->apicall(array("program"=>"get-dns","domain"=>$domain));
if($raw[0]=="error") return false;
else{
$names=array();
foreach($raw as $entry){
$tmp=explode(" ",$this->apicleanstr($entry['name']));
if($tmp[1]==$type && !in_array($tmp[0],$this->banned) && strpos($tmp[0],$domain)===false) $names[]=$tmp;
}
return $names;
}
}
// Verify if DNS exists
function dns_check($domain,$dns,$type){
$raw = $this->dns_list($domain,$type);
if($raw==false) return false;
else{
$out=false;
foreach($raw as $entry) if($entry[0]==$dns) $out=true;
return $out;
}
}
// List Domains
function domain_list(){
$raw = $this->apicall(array("program"=>"list-domains"));
if($raw[0]=="error") return false;
else{
$names=array();
foreach($raw as $entry){
$tmp=explode(" ",$this->apicleanstr($entry['name']));
if(strpos($tmp[0],".")) $names[]=array("domain"=>$tmp[0],"user"=>$tmp[1]);
}
return $names;
}
}
// Verify if Domain exists
function domain_check($domain){
$raw = $this->domain_list();
if($raw==false) return false;
else{
$out=false;
foreach($raw as $entry) if($entry['domain']==$domain) $out=true;
return $out;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment