Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolaevas committed May 3, 2016
1 parent 4b7830d commit bdb0879
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 40 deletions.
47 changes: 35 additions & 12 deletions src/CloudFlare/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public function __construct()
$this->email = $parameters[0];
$this->auth_key = $parameters[1];
}

return $this;
}

/**
Expand Down Expand Up @@ -98,8 +100,11 @@ public function setCurlOption($key, $value)

/**
* API call method for sending requests using GET
*
* @param string $path Path of the endpoint
* @param array|null $data Data to be sent along with the request
*
* @return array
*/
public function get($path, array $data = null)
{
Expand All @@ -108,8 +113,11 @@ public function get($path, array $data = null)

/**
* API call method for sending requests using POST
*
* @param string $path Path of the endpoint
* @param array|null $data Data to be sent along with the request
*
* @return array
*/
public function post($path, array $data = null)
{
Expand All @@ -118,8 +126,11 @@ public function post($path, array $data = null)

/**
* API call method for sending requests using PUT
*
* @param string $path Path of the endpoint
* @param array|null $data Data to be sent along with the request
*
* @return array
*/
public function put($path, array $data = null)
{
Expand All @@ -128,8 +139,11 @@ public function put($path, array $data = null)

/**
* API call method for sending requests using DELETE
*
* @param string $path Path of the endpoint
* @param array|null $data Data to be sent along with the request
*
* @return array
*/
public function delete($path, array $data = null)
{
Expand All @@ -138,56 +152,63 @@ public function delete($path, array $data = null)

/**
* API call method for sending requests using PATCH
*
* @param string $path Path of the endpoint
* @param array|null $data Data to be sent along with the request
*
* @return array
*/
public function patch($path, array $data = null)
{
return $this->request($path, $data, 'patch', 'edit');
}

/**
* Retrieves the users' permisison levels
* Retrieves the users' permission levels
*/
public function permissions()
{
if(!$this->permissions) {
$api = new User($this->email, $this->auth_key);
$user = $api->user();
if(!$user->result->organizations[0]) {
if(isset($user['result']) && !isset($user['result']['organizations'][0])) {
$this->permissions = array('read' => true, 'write' => true);
} else {
$this->permissions = $user->result->organizations[0]->permissions;
$this->permissions = $user['result']['organizations'][0];
}
}
return $this->permissions;
}

/**
*
* @codeCoverageIgnore
*
* API call method for sending requests using GET, POST, PUT, DELETE OR PATCH
*
* @param string $path Path of the endpoint
* @param array|null $data Data to be sent along with the request
* @param string|null $method Type of method that should be used ('GET', 'POST', 'PUT', 'DELETE', 'PATCH')
* @param string|null $permission_level Permission level required to preform the action
*
* @return array
* @throws Exception
*/
protected function request($path, array $data = null, $method = null, $permission_level = null)
{
if(!isset($this->email) || !isset($this->auth_key)) {
if (!isset($this->email) || !isset($this->auth_key)) {
throw new Exception('Authentication information must be provided');
return false;
}
$data = (is_null($data) ? array() : $data);
$method = (is_null($method) ? 'get' : $method);
$permission_level = (is_null($permission_level) ? 'read' : $permission_level);

if(!is_null($this->permission_level[$permission_level])) {
if(!$this->permissions) {
if (!is_null($this->permission_level[$permission_level])) {
if (!$this->permissions) {
$this->permissions();
}
if(!isset($this->permissions) || !in_array($this->permission_level[$permission_level], $this->permissions)) {
if (!isset($this->permissions) ||
(!in_array($this->permission_level[$permission_level], $this->permissions) &&
!$this->permissions[$permission_level])) {
throw new Exception('You do not have permission to perform this request');
return false;
}
Expand Down Expand Up @@ -223,7 +244,8 @@ protected function request($path, array $data = null, $method = null, $permissio
if($method === 'post') {
curl_setopt($ch, CURLOPT_POST, true);
//curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$headers[] = "Content-type: application/json";
} else if ($method === 'put') {
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
Expand Down Expand Up @@ -255,11 +277,12 @@ protected function request($path, array $data = null, $method = null, $permissio
'error' => $error,
'http_code' => $http_code,
'method' => $method,
'result' => $http_result,
'result' => json_decode($http_result, true),
'information' => $information
);
} else {
return json_decode($http_result);
$result = json_decode($http_result, true);
return $result;
}
}
}
8 changes: 7 additions & 1 deletion src/CloudFlare/Organizations.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ class Organizations extends Api
/**
* Organization details (permission needed: #organization:read)
* Get information about a specific organization that you are a member of
*
* @param string $identifier
*
* @return array
*/
public function organization($identifier)
{
Expand All @@ -34,8 +37,11 @@ public function organization($identifier)
/**
* Update organization (permission needed: #organization:edit)
* Update an existing Organization
*
* @param string|null $identifier
* @param string|null $name Organization Name
* @param string|null $name Organization Name
*
* @return array
*/
public function update($identifier = null, $name = null)
{
Expand Down
24 changes: 21 additions & 3 deletions src/CloudFlare/Railguns.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ class Railguns extends Api

/**
* Create Railgun (permission needed: #railgun:edit)
*
* @param string $name Readable identifier of the railgun
*
* @return array
*/
public function create($name)
{
Expand All @@ -35,9 +38,12 @@ public function create($name)
/**
* List Railguns (permission needed: #railgun:read)
* List, search, sort and filter your Railguns
* @param int|null $page Page number of paginated results
* @param int|null $per_page Number of items per page
* @param string|null $direction Direction to order Railguns (asc, desc)
*
* @param int|null $page Page number of paginated results
* @param int|null $per_page Number of items per page
* @param string|null $direction Direction to order Railguns (asc, desc)
*
* @return array
*/
public function railguns($page = null, $per_page = null, $direction = null)
{
Expand All @@ -51,7 +57,10 @@ public function railguns($page = null, $per_page = null, $direction = null)

/**
* Railgun details (permission needed: #railgun:read)
*
* @param string $identifier API item identifier tag
*
* @return array
*/
public function details($identifier)
{
Expand All @@ -61,7 +70,10 @@ public function details($identifier)
/**
* Get zones connected to a Railgun (permission needed: #railgun:read)
* The zones that are currently using this Railgun
*
* @param string $identifier API item identifier tag
*
* @return array
*/
public function zones($identifier)
{
Expand All @@ -71,8 +83,11 @@ public function zones($identifier)
/**
* Enable or disable a Railgun (permission needed: #railgun:edit)
* Enable or disable a Railgun for all zones connected to it
*
* @param string $zone_identifier API item identifier tag
* @param bool|null $enabled Flag to determine if the Railgun is accepting connections
*
* @return array
*/
public function enabled($zone_identifier, $enabled = null)
{
Expand All @@ -85,7 +100,10 @@ public function enabled($zone_identifier, $enabled = null)
/**
* Delete Railgun (permission needed: #railgun:edit)
* Disable and delete a Railgun. This will immediately disable the Railgun for any connected zones
*
* @param string $identifier API item identifier tag
*
* @return array
*/
public function delete_railgun($identifier)
{
Expand Down
26 changes: 24 additions & 2 deletions src/CloudFlare/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@ public function user()
/**
* Update user
* Update part of your user details
*
* @param string|null $first_name User's first name
* @param string|null $last_name User's last name
* @param string|null $telephone User's telephone number
* @param string|null $country The country in which the user lives. (Full list is here: http://en.wikipedia.org/wiki/List_of_country_calling_codes)
* @param string|null $country The country in which the user lives. (Full list is here:
* http://en.wikipedia.org/wiki/List_of_country_calling_codes)
* @param string|null $zipcode The zipcode or postal code where the user lives.
*
* @return array
*/
public function update($first_name = null, $last_name = null, $telephone = null, $country = null, $zipcode = null)
{
Expand All @@ -51,9 +55,12 @@ public function update($first_name = null, $last_name = null, $telephone = null,

/**
* Change your email address. Note: You must provide your current password.
*
* @param string $email Your contact email address
* @param string $email_confirm Your contact email address, repeated
* @param string $password Your current password
*
* @return array
*/
public function change_email($email, $email_confirm, $password)
{
Expand All @@ -67,9 +74,12 @@ public function change_email($email, $email_confirm, $password)

/**
* Change your password
*
* @param string $old_password Your current password
* @param string $new_password Your new password
* @param string $new_password_confirm Your new password, repeated
*
* @return array
*/
public function change_password($old_password, $new_password, $new_password_confirm)
{
Expand All @@ -83,8 +93,11 @@ public function change_password($old_password, $new_password, $new_password_conf

/**
* Change your username. Note: You must provide your current password.
*
* @param string $username A username used to access other cloudflare services, like support
* @param string $password Your current password
*
* @return array
*/
public function change_username($username, $password)
{
Expand All @@ -97,9 +110,12 @@ public function change_username($username, $password)

/**
* Begin setting up CloudFlare two-factor authentication with a given telephone number
* @param int $country_code The country code of your mobile phone number
*
* @param int $country_code The country code of your mobile phone number
* @param string $mobile_phone_number Your mobile phone number
* @param string $current_password Your current CloudFlare password
*
* @return array
*/
public function initialize_two_factor_authentication($country_code, $mobile_phone_number, $current_password)
{
Expand All @@ -113,7 +129,10 @@ public function initialize_two_factor_authentication($country_code, $mobile_phon

/**
* Finish setting up CloudFlare two-factor authentication with a given telephone number
*
* @param int $auth_token The token provided by the two-factor authenticator
*
* @return array
*/
public function finalize_two_factor_authentication($auth_token)
{
Expand All @@ -125,7 +144,10 @@ public function finalize_two_factor_authentication($auth_token)

/**
* Disable two-factor authentication for your CloudFlare user account
*
* @param int The token provided by the two-factor authenticator
*
* @return array
*/
public function disable_two_factor_authentication($auth_token)
{
Expand Down
Loading

0 comments on commit bdb0879

Please sign in to comment.