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