b3e2038 by madeinnordeste at 2010-03-19 1
<?php defined('SYSPATH') or die('No direct script access.');
2
 
3
class Cpanel_Core {
4
	
5
	/*
6
	* Cpanel core  - access cpanel xml-API
7
	* From Luiz Alberto  < madeinnordeste at gmail.com>	
262649b by madeinnordeste at 2010-03-19 8
	* http://beto.euqueroserummacaco.com
b3e2038 by madeinnordeste at 2010-03-19 9
	*
10
	* For more information from Cpanel XML API
11
	* http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/XmlApi
12
	*
13
	* examples:
14
	*  
15
   	* $cp = new Cpanel('http://www.youdomain.net', 'you-user', 'you-user-password');
16
	* $r = $cp->execute('servicestatus');
17
	*
18
	*	
19
	* $cp = new Cpanel('http://www.youdomain.net', 'you-user', 'you-user-password');
20
	* $r = $cp->execute('servicestatus', NULL, 'json');
21
	*
22
	*	
23
   	* $cp = new Cpanel('http://www.youdomain.net', 'you-user', 'you-user-password');
24
	* $r = $cp->execute('domainuserdata', array('domain' => 'youhosteddomain.com'));    
25
	*
26
	*	
27
	* $cp = new Cpanel('http://www.youdomain.net', 'you-user', 'you-user-password');
28
	* $r = $cp->execute('domainuserdata', array('domain' => 'youhosteddomain.com'));    
29
	*
30
	*	
31
	* $cp = new Cpanel('http://www.youdomain.net', 'you-user', 'you-user-password');
32
	* $r = $cp->execute('domainuserdata', array('domain' => 'youhosteddomain.com'));    
33
	*
34
	*/
35
	
36
	public function __construct($domain, $user, $password, $port = '2086'){
37
	
38
		$this->setup($domain, $user, $password, $port);
39
	
40
	}
41
	
42
	/**
43
	* Setup object params
44
	* @param string $domain domain in WHM hosted
45
	* @param string $user WHM user
46
	* @param string $password WHM user password	
47
	* @param string $port WHM http port
48
	* @return void
49
	*/
50
	public function setup($domain, $user, $password, $port = '2086'){
51
	
52
		$this->domain = $domain;
53
		$this->user = $user;
54
		$this->password = $password;
55
		$this->port = $port;
56
	
57
	}
58
	
59
	
60
		
61
	
62
	/**
63
	* Execute url request from API
64
	* @param string $action xml-API action/method
65
	* @param array $params action/method params
66
	* @param string $format response format
67
	* @return string
68
	*/
69
	public function execute($action, $params = array(), $format = 'xml'){
70
	
71
		$url = $this->make_url($action, $params, $format);
72
		
73
		$ch = curl_init();
74
	
75
	    curl_setopt($ch, CURLOPT_URL, $url);
76
	    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
77
	    curl_setopt($ch, CURLOPT_USERPWD, $this->user.':'.$this->password);
78
	    
79
	    $output = curl_exec($ch);
80
	
81
	    $response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
82
	
83
	    //trata o erro
84
	    if ($response_code == '404') {
85
	        return FALSE;
86
	    } else {
87
	        return $output;
88
	    }
89
	    	    
90
	    
91
92
	}
93
	
94
	
95
		/**
96
		* create string params for url
97
		* @param array $params params for url request
98
		* @return string
99
		*/
100
		public function make_params($params){
101
			$c = 0;
102
			$p = '?';
103
			foreach($params as $k => $v){
104
				if($c > 0){
105
					$p.='&';				
106
				}
107
				$p.=$k.'='.$v;				
108
				$c++;
109
			}
110
			return $p;	
111
		}
112
		
113
		
114
		/**
115
		* create string url for request
116
		* @param string $action xml-API action/method
117
		* @param array $params action/method params
118
		* @param string $format response format
119
		* @return string
120
		*/
121
		public function make_url($action, $params = array(), $format = 'xml'){
122
		
123
			$url = $this->domain.':'.$this->port.'/'.$format.'-api/'.$action;
124
			if(sizeof($params)){
125
				$url.=$this->make_params($params);
126
			}
127
			return $url;
128
			
129
		
130
		}
131
132
	
133
134
}