| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | class DirectAdminSignAPI |
| 6: | { |
| 7: | private $da ; |
| 8: | private $arg_names = array( |
| 9: | 'CMD_API_POP' => array( 'action', 'domain', 'user', 'passwd', 'passwd2', 'quota', 'limit' ), |
| 10: | ) ; |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | public function __construct($url) |
| 18: | { |
| 19: | $this->da = parse_url($url); |
| 20: | |
| 21: | if (! $this->da['port']) { |
| 22: | $this->da['port'] = ($this->da['scheme'] == 'https') ? 443 : 80 ; |
| 23: | } |
| 24: | } |
| 25: | |
| 26: | public function __call($name, $arguments) |
| 27: | { |
| 28: | $data = $this->make_command_data($name, $arguments) ; |
| 29: | return $this->DoCommand(array( 'method' => 'POST', 'command' => $name, 'data' => $data )) ; |
| 30: | } |
| 31: | |
| 32: | private function make_command_data($name, $arguments) |
| 33: | { |
| 34: | if (! isset($this->arg_names[ $name ])) { |
| 35: | throw new Exception('Invalid DirectAdmin command') ; |
| 36: | } |
| 37: | |
| 38: | $names = &$this->arg_names[ $name ] ; |
| 39: | |
| 40: | if (count($names) != count($arguments)) { |
| 41: | throw new Exception('Invalid arguments of DirectAdmin command ' . $name) ; |
| 42: | } |
| 43: | |
| 44: | $transformed = array() ; |
| 45: | foreach ($names as $index => $value) { |
| 46: | $transformed[$value] = $arguments[$index] ; |
| 47: | } |
| 48: | |
| 49: | return $transformed ; |
| 50: | } |
| 51: | |
| 52: | private function DoCommand($argument) |
| 53: | { |
| 54: | if (! is_array($argument) || ! count($argument)) { |
| 55: | return null ; |
| 56: | } |
| 57: | |
| 58: | $command = $argument['command'] ; |
| 59: | if (empty($command) || ! is_string($command)) { |
| 60: | return null ; |
| 61: | } |
| 62: | |
| 63: | switch (strcasecmp($argument['method'], 'POST')) { |
| 64: | case 0: |
| 65: | $post = 1; |
| 66: | $method = 'POST'; |
| 67: | break; |
| 68: | default: |
| 69: | $post = 0; |
| 70: | $method = 'GET'; |
| 71: | } |
| 72: | |
| 73: | $content_length = 0 ; |
| 74: | $data = ''; |
| 75: | if (is_array($argument['data']) && count($argument['data'])) { |
| 76: | $pair = '' ; |
| 77: | foreach ($argument['data'] as $index => $value) { |
| 78: | $pair .= $index . '=' . urlencode($value) . '&' ; |
| 79: | } |
| 80: | |
| 81: | $data = rtrim($pair, '&') ; |
| 82: | $content_length = ($post) ? strlen($data) : 0 ; |
| 83: | } |
| 84: | |
| 85: | $prefix = ($this->da['scheme'] == 'https') ? 'ssl://' : null; |
| 86: | $error = array(); |
| 87: | $fp = @fsockopen($prefix . $this->da['host'], $this->da['port'], $error['number'], $error['string'], 10) ; |
| 88: | if (! $fp) { |
| 89: | return null ; |
| 90: | } |
| 91: | |
| 92: | $http_header = array( |
| 93: | $method . ' /' . $command . ((!$post) ? '?' . $data : '') . ' HTTP/1.0', |
| 94: | 'Authorization: Basic ' . base64_encode($this->da['user'] . ':' . $this->da['pass']), |
| 95: | 'Host: ' . $this->da['host'], |
| 96: | 'Content-Type: application/x-www-form-urlencoded', |
| 97: | 'Content-Length: ' . $content_length, |
| 98: | 'Connection: close' |
| 99: | ) ; |
| 100: | |
| 101: | $request = implode("\r\n", $http_header) . "\r\n\r\n" ; |
| 102: | fwrite($fp, $request . (($post) ? $data : '')) ; |
| 103: | |
| 104: | $returned = '' ; |
| 105: | while ($line = @fread($fp, 1024)) { |
| 106: | $returned .= $line; |
| 107: | } |
| 108: | |
| 109: | fclose($fp); |
| 110: | |
| 111: | $h = strpos($returned, "\r\n\r\n"); |
| 112: | $head['all'] = substr($returned, 0, $h); |
| 113: | $head['part'] = explode("\r\n", $head['all']); |
| 114: | |
| 115: | $body = substr($returned, $h + 4); |
| 116: | |
| 117: | return rtrim((string) $body); |
| 118: | } |
| 119: | } |
| 120: | |