forked from mlcheng/php-CpanelDdns
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCpanelDdns.php
118 lines (96 loc) · 2.56 KB
/
CpanelDdns.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
/***********************************************
"CpanelDdns.php"
Created by Michael Cheng on 04/16/2014 14:40
http://michaelcheng.us/
--All Rights Reserved--
***********************************************/
require('lib/HttpRequest.php');
/**
* Cpanel object. For now, the only public facing method is to update the DDNS
*/
class CpanelDdns {
private $_url;
private $_user;
private $_pass;
private $_token;
private $_cookieFile;
/**
* Update DDNS on your domain. This method requires a login, therefore be sure to provide the URL, username, and password before calling
* @param [String] $subdomain Your website's subdomain, used to connect to your VNC or whatever, e.g. "vnc"
* @param [String] $domain Your website, e.g. "example.com"
* @return Returns the result of the POST.
*/
public function updateDdns($subdomain, $domain) {
if(!$this->login()) return false;
$http = new HttpRequest($this->getUrl() . $this->getToken() . "/json-api/cpanel", $this->_cookieFile);
return $http
->post(array(
"address" => $_SERVER['REMOTE_ADDR'],
"class" => "IN",
"cpanel_jsonapi_func" => "edit_zone_record",
"cpanel_jsonapi_module" => "ZoneEdit",
"cpanel_jsonapi_version" => 2,
"domain" => $domain,
"line" => 36,
"name" => $subdomain . "." . $domain . ".",
"ttl" => 1200,
"type" => "A"
));
}
/**
* Login to your cpanel.
* @return [Boolean] True if the login succeeded
*/
private function login() {
$url = $this->getUrl();
$user = $this->getUser();
$pass = $this->getPass();
$params = "user=" . $user . "&pass=" . $pass;
$http = new HttpRequest($url . "/login");
$this->_cookieFile = $http->getCookieFile();
$result = $http->post(array(
"user" => $user,
"pass" => $pass
));
$inf = $http->getCurlInfo();
// Get the session
if(strpos($inf['url'], "cpsess")) {
$pattern = "/.*?(\/cpsess.*?)\/.*?/is";
$preg_res = preg_match($pattern, $inf['url'], $cpsess);
$this->setToken($cpsess[1]);
return true;
}
return false;
}
function getUrl() {
return $this->_url;
}
function setUrl($url) {
$this->_url = $url;
return $this;
}
function getUser() {
return $this->_user;
}
function setUser($user) {
$this->_user = $user;
return $this;
}
function getPass() {
return $this->_pass;
}
function setPass($pass) {
$this->_pass = $pass;
return $this;
}
function getToken() {
return $this->_token;
}
function setToken($token) {
$this->_token = $token;
return $this;
}
}
?>