<?php
if
( ! defined(
'BASEPATH'
))
exit
(
'No direct script access allowed'
);
class
Rackspace_API {
protected
$_http_message
;
protected
$_ci
;
protected
$_user_key
;
protected
$_secret_key
;
protected
$_user_agent
;
protected
$_api_version
;
protected
$_rackspace_host
;
function
__construct() {
$this
->_ci =& get_instance();
$this
->_ci->config->load(
'RackspaceAPI'
, TRUE);
$this
->_user_key =
$this
->_ci->config->item(
'user_key'
,
'RackspaceAPI'
);
$this
->_secret_key =
$this
->_ci->config->item(
'secret_key'
,
'RackspaceAPI'
);
$this
->_user_agent =
$this
->_ci->config->item(
'user_agent'
,
'RackspaceAPI'
);
$this
->_api_version =
$this
->_ci->config->item(
'api_version'
,
'RackspaceAPI'
);
$this
->_rackspace_host =
$this
->_ci->config->item(
'rackspace_host'
,
'RackspaceAPI'
);
}
public
function
getDomainInfo(
$domain
) {
return
$this
->genericGet(
'/customers/me/domains/'
.
$domain
);
}
public
function
getDomains() {
$obj
=
$this
->genericGet(
'/customers/me/domains'
);
if
(!
$obj
->error){
foreach
(
$obj
->result->domains
as
$domain
) {
$domains
[]=
$domain
->name;
}
$obj
->result =
$domains
;
}
return
$obj
;
}
public
function
getMailboxInfo(
$domain
,
$id
) {
return
$this
->genericGet(
'/customers/me/domains/'
.
$domain
.
'/rs/mailboxes/'
.
$id
);
}
private
function
genericGet(
$url
) {
$this
->get(
$url
,
'application/json'
);
if
(
$this
->_http_message->getResponseCode() == 200) {
$json
=
'['
.
$this
->_http_message->getBody().
']'
;
if
(
is_string
(
$json
)) {
$obj
= json_decode(
$json
);
$result
->error = false;
$result
->result =
$obj
[0];
}
else
{
$result
->error = true;
$result
->result =
'Failed to parse JSON'
;
}
}
else
{
$result
->error = true;
$result
->result =
$this
->_http_message->getHeader(
"x-error-message"
);
}
return
$result
;
}
public
function
addMailbox(
$domain
,
$id
,
$first
,
$last
,
$name
,
$office
,
$locno
,
$password
,
$fwd
,
$save
=
'true'
) {
$fields
=
array
(
'password'
=>
$password
,
'emailForwardingAddresses'
=>
$fwd
,
'firstName'
=>
$first
,
'lastName'
=>
$last
,
'displayName'
=>
$name
,
'saveForwardedEmail'
=>
$save
,
'organization'
=>
$office
,
'organizationUnit'
=>
$locno
);
return
$this
->genericPost(
'/customers/me/domains/'
.
$domain
.
'/rs/mailboxes/'
.
$id
,
$fields
);
}
private
function
genericPost(
$url
,
$fields
) {
$this
->post(
$url
,
$fields
,
'application/json'
);
if
(
$this
->_http_message->getResponseCode() == 200) {
$result
->error = false;
$result
->result =
$this
->_http_message->getBody();
}
else
{
$result
->error = true;
$result
->result =
$this
->_http_message->getHeader(
"x-error-message"
);
}
return
$result
;
}
public
function
changeForwarding(
$domain
,
$id
,
$fwd
) {
$fields
=
array
(
'emailForwardingAddresses'
=>
$fwd
);
return
$this
->genericPut(
'/customers/me/domains/'
.
$domain
.
'/rs/mailboxes/'
.
$id
,
$fields
);
}
public
function
changeLocation(
$domain
,
$id
,
$office
,
$locno
) {
$fields
=
array
(
'organization'
=>
$office
,
'organizationUnit'
=>
$locno
);
return
$this
->genericPut(
'/customers/me/domains/'
.
$domain
.
'/rs/mailboxes/'
.
$id
,
$fields
);
}
public
function
changeName(
$domain
,
$id
,
$first
,
$last
,
$name
) {
$fields
=
array
(
'firstName'
=>
$first
,
'lastName'
=>
$last
,
'displayName'
=>
$name
);
return
$this
->genericPut(
'/customers/me/domains/'
.
$domain
.
'/rs/mailboxes/'
.
$id
,
$fields
);
}
public
function
changePassword(
$domain
,
$id
,
$password
) {
$fields
=
array
(
'password'
=>
$password
);
return
$this
->genericPut(
'/customers/me/domains/'
.
$domain
.
'/rs/mailboxes/'
.
$id
,
$fields
);
}
private
function
genericPut(
$url
,
$fields
) {
$this
->put(
$url
,
$fields
);
if
(
$this
->_http_message->getResponseCode() == 200) {
$result
->error = false;
$result
->result =
$this
->_http_message->getBody();
}
else
{
$result
->error = true;
$result
->result =
$this
->_http_message->getHeader(
"x-error-message"
);
}
return
$result
;
}
public
function
deleteMailbox(
$domain
,
$id
) {
return
$this
->genericDelete(
"/customers/me/domains/$domain/rs/mailboxes/$id"
);
}
private
function
genericDelete(
$url
) {
$this
->
delete
(
$url
);
if
(
$this
->_http_message->getResponseCode() == 200) {
$result
->error = false;
}
else
{
if
(
$this
->_http_message->getResponseCode() == 500) {
$result
->error = true;
$result
->result =
'An internal server error occurred deleting object. Url: '
.
$url
;
}
else
{
$result
->error = true;
$result
->result =
$this
->_http_message->getHeader(
"x-error-message"
);
}
}
return
$result
;
}
private
function
get(
$url_string
,
$format
) {
$headers
=
array
(
"Accept: $format"
);
$curl_session
= self::construct_session(
$url_string
,
$headers
);
$this
->_http_message = self::send_request(
$curl_session
);
}
private
function
post(
$url_string
,
$fields
,
$format
) {
$headers
=
array
(
"Accept: $format"
);
$curl_session
= self::construct_session(
$url_string
,
$headers
);
curl_setopt(
$curl_session
, CURLOPT_POST, true);
curl_setopt(
$curl_session
, CURLOPT_POSTFIELDS,
$fields
);
$this
->_http_message = self::send_request(
$curl_session
);
}
private
function
put(
$url_string
,
$fields
) {
$curl_session
= self::construct_session(
$url_string
,
array
());
curl_setopt(
$curl_session
, CURLOPT_CUSTOMREQUEST,
'PUT'
);
curl_setopt(
$curl_session
, CURLOPT_POSTFIELDS,
$fields
);
$this
->_http_message = self::send_request(
$curl_session
);
}
private
function
delete
(
$url_string
) {
$curl_session
= self::construct_session(
$url_string
,
array
());
curl_setopt(
$curl_session
, CURLOPT_CUSTOMREQUEST,
'DELETE'
);
$this
->_http_message = self::send_request(
$curl_session
);
}
private
function
send_request(
$curl_session
) {
$response
= curl_exec(
$curl_session
);
curl_close(
$curl_session
);
$response
= preg_replace(
'|HTTP/1.1 100.*HTTP/1.1|isU'
,
'HTTP/1.1'
,
$response
);
return
new
HttpMessage(
$response
);
}
private
function
construct_session(
$url_string
,
$existing_headers
) {
$headers
=
array_merge
(
self::authorization_headers(),
$existing_headers
);
$url
= self::construct_uri(
$url_string
);
$curl_session
= curl_init(
$url
);
curl_setopt(
$curl_session
, CURLOPT_HEADER, true);
curl_setopt(
$curl_session
, CURLOPT_HTTPHEADER,
$headers
);
curl_setopt(
$curl_session
, CURLOPT_RETURNTRANSFER, true);
return
$curl_session
;
}
private
function
authorization_headers() {
$time_stamp
=
date
(
'YmdHis'
);
$data_to_sign
=
$this
->_user_key .
$this
->_user_agent .
$time_stamp
.
$this
->_secret_key;
$signature
=
base64_encode
(sha1(
$data_to_sign
, true));
$headers
=
array
();
$headers
[] =
"User-Agent: "
.
$this
->_user_agent;
$headers
[] =
'X-Api-Signature: '
.
$this
->_user_key .
":$time_stamp:$signature"
;
return
$headers
;
}
private
function
construct_uri(
$url_string
) {
$url
=
'http://'
.
$this
->_rackspace_host .
'/'
.
$this
->_api_version .
$url_string
;
return
$url
;
}
}
?>