This repository has been archived by the owner on Nov 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 82
Add hosting provider support (Provider) #10
Open
msaladna
wants to merge
3
commits into
jamesryanbell:master
Choose a base branch
from
apisnetworks:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
<?php | ||
|
||
namespace Cloudflare; | ||
|
||
use Cloudflare\Api; | ||
|
||
/** | ||
* CloudFlare API wrapper | ||
* | ||
* Provider | ||
* | ||
* @author Matt Saladna <[email protected]> | ||
* @version 1 | ||
*/ | ||
|
||
class Provider extends Api | ||
{ | ||
public function __construct($key) { | ||
parent::__construct($key); | ||
// 5 seconds is too low for admin operations like full_zone_set | ||
$this->setCurlOption(CURLOPT_TIMEOUT, 20); | ||
} | ||
|
||
/** | ||
* Create a new CloudFlare user | ||
* | ||
* @return string user key | ||
*/ | ||
public function create_user($email, $pass, $username = null, $uniqid = null, $clobber = false) | ||
{ | ||
$data = array( | ||
'cloudflare_email' => $email, | ||
'cloudflare_pass' => $pass, | ||
'clobber_unique_id' => $clobber | ||
); | ||
|
||
if (!is_null($username)) { | ||
$data['cloudflare_username'] = $username; | ||
} | ||
|
||
if (!is_null($uniqid)) { | ||
$data['unique_id'] = $uniqid; | ||
} | ||
|
||
return $this->post('user_create', $data); | ||
} | ||
|
||
/** | ||
* Setup a user's zone for CNAME hosting | ||
* | ||
* Set both $resolve_to and $subdomains to null for a full | ||
* zone hosting by CF | ||
* | ||
* @param string $key 32-char key | ||
* @param string $zone_name zone for which CloudFlare filters | ||
* @param $resolve_to string|null target CNAME to resolve after filtering | ||
* @param $subdomains array|null subdomains CF hosts | ||
* @return object CF response | ||
*/ | ||
public function zone_set($key, $zone_name, $resolve_to = null, array $subdomains = null) | ||
{ | ||
$data = array( | ||
'user_key' => $key, | ||
'zone_name' => $zone_name | ||
); | ||
$method = 'full_zone_set'; | ||
// partial zone set | ||
if ($resolve_to) { | ||
$method = 'zone_set'; | ||
$data['resolve_to'] = $resolve_to; | ||
$data['subdomains'] = join(",", $subdomains); | ||
} | ||
|
||
return $this->post($method, $data); | ||
} | ||
|
||
/** | ||
* Add a zone using the full setup method | ||
* | ||
* @param string $key 32-char auth key | ||
* @param string $zone_name zone for which CF is authoritative | ||
* @return object CF response | ||
*/ | ||
public function full_zone_set($key, $zone_name) { | ||
return $this->zone_set($key, $zone_name); | ||
} | ||
|
||
/** | ||
* Lookup a user's CF account information | ||
* | ||
* @param string $uniqid user unique id set during creation | ||
* @return object CF response | ||
*/ | ||
public function user_lookup($uniqid) { | ||
return $this->post('user_lookup', $uniqid); | ||
} | ||
|
||
public function user_auth($email, $passwd, $uniqid = null, $clobber = false) { | ||
$data = array( | ||
'cloudflare_email' => $email, | ||
'cloudflare_pass' => $passwd, | ||
'unique_id' => $uniqid, | ||
'clobber_unique_id' => $clobber | ||
); | ||
|
||
$this->post('user_auth', $data); | ||
} | ||
|
||
public function zone_lookup($key, $zone_name) { | ||
$data = array( | ||
'user_key' => $key, | ||
'zone_name' => $zone_name | ||
); | ||
return $this->post('zone_lookup', $data); | ||
} | ||
|
||
public function zone_delete($key, $zone_name) { | ||
$data = array( | ||
'user_key' => $key, | ||
'zone_name' => $zone_name | ||
); | ||
return $this->post('zone_delete', $data); | ||
} | ||
|
||
/** | ||
* Renegerate your host key | ||
* | ||
* @XXX DANGEROUS!!! | ||
* | ||
* @return array|mixed | ||
*/ | ||
public function host_key_regen() { | ||
|
||
return $this->post('host_key_regen'); | ||
} | ||
|
||
/** | ||
* List the domains currently active on CloudFlare for the given host | ||
* | ||
* Valid options are: | ||
* limit | ||
* offset | ||
* zone_name | ||
* sub_id | ||
* zone_status | ||
* sub_status | ||
* | ||
* Valid response zone_status codes: | ||
* V: active | ||
* D: deleted | ||
* | ||
* Valid subscription response status codes: | ||
* V: active | ||
* CNL: canceled | ||
* | ||
* @param array $options | ||
* @return object | ||
* | ||
*/ | ||
public function zone_list(array $options = array()) { | ||
$myoptions = array( | ||
'limit' => 100, | ||
'offset' => 0, | ||
'zone_name' => NULL, | ||
'sub_id' => NULL, | ||
'zone_status' => 'ALL', | ||
'sub_status' => 'ALL' | ||
); | ||
|
||
foreach (array_keys($myoptions) as $k) { | ||
if (isset($options[$k])) { | ||
$myoptions[$k] = $options[$k]; | ||
} | ||
} | ||
|
||
if (isset($options['sub_status'])) { | ||
$valid = array('V','CNL','ALL'); | ||
$status = strtoupper($options['sub_status']); | ||
if (!in_array($status, $valid)) { | ||
throw new \InvalidArgumentException("unknown sub_status `" . | ||
$status ."'"); | ||
} | ||
$myoptions['sub_status'] = $status; | ||
} | ||
|
||
if (isset($options['zone_status'])) { | ||
$valid = array('V', 'D', 'ALL'); | ||
$status = strtoupper($options['zone_status']); | ||
if (!in_array($status, $valid)) { | ||
throw new \InvalidArgumentException("unknown zone_status `" . | ||
$status ."'"); | ||
} | ||
$myoptions['zone_status'] = $status; | ||
} | ||
|
||
return $this->post('zone_list', $myoptions); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given that the provider authentication method is only used with the Provider class, I feel it would be simpler to revert this method back and just add another condition to the if statement for 3 arguments being passed in.
Then in the provider constructor you can do this:
I know it's a bit of a workaround but it saves having variable such as $arg1, $arg2 etc.. and reduces the complexity of the conditions. It also means we aren't introducing the breaking change of the constructor requiring at least one argument.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My argument to that is that the method signature now becomes ambiguous. Formal parameters are supposed to lend some insight as to what the method accepts and also work in conjunction with docblock commenting.
Not withholding, it also breaks reflection on the method. You'll get inconsistent results with what actually needs to be passed, e.g.
Alternatively, we can drop support for pre-PHP 5.6 and use the variadic notation, i.e. function foo(...$args) { ... }. Ultimately it's your project, so let me know the direction you want to proceed. That's just my two cents on the matter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi
Just holding off on this now as CloudFlare have added a third parameter to their authentication methods: User Service Key (https://api.cloudflare.com/#endpoints). So I need to think about how this factors into this work.
Thanks,
James