Skip to content

Commit f4e98e7

Browse files
Adicionando arquivos
0 parents  commit f4e98e7

21 files changed

+2702
-0
lines changed

README.md

Whitespace-only changes.

com/imasters/php/cpanel/cPanel.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
/**
3+
* @brief Biblioteca cPanel
4+
* @details Classes e interfaces para integração com a API do cPanel
5+
* @package com.imasters.php.cpanel
6+
*/
7+
8+
require_once 'com/imasters/php/http/HTTPCookieManager.php';
9+
require_once 'com/imasters/php/cpanel/operation/account/AccountModule.php';
10+
11+
/**
12+
* @brief Implementação da API do cPanel
13+
* @author João Batista Neto <neto.joaobatista@imasters.com.br>
14+
*/
15+
class cPanel {
16+
const UNSECURED_PORT = 2086;
17+
const SECURED_PORT = 2087;
18+
19+
/**
20+
* @var string
21+
*/
22+
private $host;
23+
24+
/**
25+
* @var HTTPAuthenticator
26+
*/
27+
private $httpAuthenticator;
28+
29+
/**
30+
* @var integer
31+
*/
32+
private $port = cPanel::SECURED_PORT;
33+
34+
/**
35+
* @var boolean
36+
*/
37+
private $secure = true;
38+
39+
/**
40+
* @brief Constroi o objeto para integração com a API do cPanel
41+
* @param string $host
42+
* @param HTTPAuthenticator $httpAuthenticator
43+
*/
44+
public function __construct( $host , HTTPAuthenticator $httpAuthenticator ) {
45+
$this->host = $host;
46+
$this->httpAuthenticator = $httpAuthenticator;
47+
$this->useSecureConnection();
48+
}
49+
50+
/**
51+
* @brief Módulo para gerenciamento de contas.
52+
* @return AccountModule
53+
*/
54+
public function account() {
55+
return new AccountModule( $this );
56+
}
57+
58+
/**
59+
* @brief Conexão HTTP
60+
* @details Recupera um objeto de conexão HTTP para ser utilizado
61+
* nas chamadas às operações da API.
62+
* @return HTTPConnection
63+
*/
64+
public function getHTTPConnection() {
65+
$httpConnection = new HTTPConnection();
66+
$httpConnection->setAuthenticator( $this->httpAuthenticator );
67+
$httpConnection->setCookieManager( new HTTPCookieManager() );
68+
$httpConnection->initialize( $this->host , $this->secure , $this->port );
69+
70+
return $httpConnection;
71+
}
72+
73+
/**
74+
* @brief Define a porta HTTP utilizada na conexão.
75+
* @param integer $port
76+
*/
77+
public function setHTTPPort( $port ) {
78+
$this->port = (int) $port;
79+
}
80+
81+
/**
82+
* @brief Define se será utilizado conexão segura (https).
83+
* @param boolean $secure
84+
*/
85+
public function useSecureConnection( $secure = true ) {
86+
$this->secure = !!$secure;
87+
$this->port = $this->secure ? cPanel::SECURED_PORT : cPanel::UNSECURED_PORT;
88+
}
89+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
/**
3+
* @brief Biblioteca cPanel
4+
* @details Classes e interfaces para integração com a API do cPanel
5+
* @package com.imasters.php.cpanel
6+
*/
7+
8+
require_once 'com/imasters/php/http/HTTPAuthenticator.php';
9+
10+
/**
11+
* @brief Autenticação HTTP Básica
12+
* @author João Batista Neto <neto.joaobatista@imasters.com.br>
13+
*/
14+
class cPanelBasicAuthentication implements HTTPAuthenticator {
15+
/**
16+
* @var string
17+
*/
18+
private $password;
19+
20+
/**
21+
* @var string
22+
*/
23+
private $username;
24+
25+
/**
26+
* @brief Constroi o objeto de autenticação
27+
* @param string $username Nome do usuário do cPanel
28+
* @param string $password Senha do usuário do cPanel
29+
*/
30+
public function __construct( $username , $password ) {
31+
$this->setUsername( $username );
32+
$this->setPassword( $password );
33+
}
34+
35+
/**
36+
* @param HTTPRequest $httpRequest
37+
* @see HTTPAuthenticator::authenticate()
38+
*/
39+
public function authenticate( HTTPRequest $httpRequest ) {
40+
$httpRequest->addRequestHeader( 'Authorization' , sprintf(
41+
'Basic %s' , base64_encode(
42+
sprintf( '%s:%s' , $this->username , $this->password )
43+
) )
44+
);
45+
}
46+
47+
/**
48+
* @brief Recupera a senha do usuário
49+
* @return string
50+
*/
51+
public function getPassword() {
52+
return $this->password;
53+
}
54+
55+
/**
56+
* @brief Recupera o nome do usuário
57+
* @return string
58+
*/
59+
public function getUsername() {
60+
return $this->username;
61+
}
62+
63+
/**
64+
* @brief Define a senha do usuário
65+
* @param string $password A senha que será definida
66+
*/
67+
public function setPassword( $password ) {
68+
$this->password = $password;
69+
}
70+
71+
/**
72+
* @brief Define o nome do usuário
73+
* @param string $username O nome que será definido
74+
*/
75+
public function setUsername( $username ) {
76+
$this->username = $username;
77+
}
78+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
/**
3+
* @brief Biblioteca cPanel
4+
* @details Classes e interfaces para integração com a API do cPanel
5+
* @package com.imasters.php.cpanel
6+
*/
7+
8+
require_once 'com/imasters/php/http/HTTPAuthenticator.php';
9+
10+
/**
11+
* @brief Autenticação HTTP utilizando hash
12+
* @author João Batista Neto <neto.joaobatista@imasters.com.br>
13+
*/
14+
class cPanelHashAuthentication implements HTTPAuthenticator {
15+
/**
16+
* @var string
17+
*/
18+
private $hash;
19+
20+
/**
21+
* @var string
22+
*/
23+
private $username;
24+
25+
/**
26+
* @brief Constroi o objeto de autenticação
27+
* @param string $username Nome do usuário do cPanel
28+
* @param string $hash O hash
29+
*/
30+
public function __construct( $username , $hash ) {
31+
$this->setUsername( $username );
32+
$this->setHash( $hash );
33+
}
34+
35+
/**
36+
* @param HTTPRequest $httpRequest
37+
* @see HTTPAuthenticator::authenticate()
38+
*/
39+
public function authenticate( HTTPRequest $httpRequest ) {
40+
$httpRequest->addRequestHeader( 'Authorization' , sprintf(
41+
'WHM %s:%s' , $this->username , preg_replace( "/(\r|\n)/" , null,
42+
$this->hash
43+
) )
44+
);
45+
}
46+
47+
/**
48+
* @brief Recupera o hash
49+
* @return string
50+
*/
51+
public function getHash() {
52+
return $this->password;
53+
}
54+
55+
/**
56+
* @brief Recupera o nome do usuário
57+
* @return string
58+
*/
59+
public function getUsername() {
60+
return $this->username;
61+
}
62+
63+
/**
64+
* @brief Define o hash
65+
* @param string $hash O hash que será definido
66+
*/
67+
public function setHash( $hash ) {
68+
$this->hash = $hash;
69+
}
70+
71+
/**
72+
* @brief Define o nome do usuário
73+
* @param string $username O nome que será definido
74+
*/
75+
public function setUsername( $username ) {
76+
$this->username = $username;
77+
}
78+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* @brief Biblioteca cPanel
4+
* @details Classes e interfaces para integração com a API do cPanel
5+
* @package com.imasters.php.cpanel
6+
*/
7+
8+
/**
9+
* @brief Interface para implementação de um módulo
10+
* @author João Batista Neto <neto.joaobatista@imasters.com.br>
11+
*/
12+
abstract class cPanelModule {
13+
/**
14+
* @var cPanel
15+
*/
16+
protected $cpanel;
17+
18+
final public function __construct( cPanel $cpanel ) {
19+
$this->cpanel = $cpanel;
20+
}
21+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* @brief Biblioteca cPanel
4+
* @details Classes e interfaces para integração com a API do cPanel
5+
* @package com.imasters.php.cpanel
6+
*/
7+
8+
/**
9+
* @brief Interface para implementação de uma operação
10+
* @author João Batista Neto <neto.joaobatista@imasters.com.br>
11+
*/
12+
abstract class cPanelOperation {
13+
/**
14+
* @var cPanel
15+
*/
16+
protected $cpanel;
17+
18+
/**
19+
* @var HTTPConnection
20+
*/
21+
protected $httpConnection;
22+
23+
/**
24+
* @var HTTPResponse
25+
*/
26+
private $httpResponse;
27+
28+
/**
29+
* @brief Constroi o objeto que representa uma operação
30+
* @param cPanel $cpanel
31+
*/
32+
public final function __construct( cPanel $cpanel ) {
33+
$this->cpanel = $cpanel;
34+
$this->httpConnection = $cpanel->getHTTPConnection();
35+
}
36+
37+
/**
38+
* @return HTTPResponse
39+
*/
40+
public function __getLastResponse() {
41+
return $this->httpResponse;
42+
}
43+
44+
/**
45+
* @brief Efetua a chamada à operação da API
46+
* @return stdClass
47+
*/
48+
public function call() {
49+
$this->httpResponse = $this->httpConnection->execute( '/json-api/' . $this->getOperationName() );
50+
51+
return json_decode( $this->httpResponse->getContent() );
52+
}
53+
54+
/**
55+
* @brief Recupera o nome da operação
56+
* @return string
57+
*/
58+
public abstract function getOperationName();
59+
}

0 commit comments

Comments
 (0)