cloudflare/Cloudflare-WordPress
Publicmirrored fromhttps://github.com/cloudflare/Cloudflare-WordPress
src/WordPress/WordPressAPI.php
158lines · modecode
9 years ago
| 1 | <?php |
| 2 | |
| 3 | namespace CF\WordPress; |
| 4 | |
| 5 | use CF\Integration\IntegrationAPIInterface; |
| 6 | use CF\DNSRecord; |
| 7 | |
| 8 | class WordPressAPI implements IntegrationAPIInterface |
| 9 | { |
| 10 | const API_NONCE = 'cloudflare-db-api-nonce'; |
| 11 | |
| 12 | private $dataStore; |
| 13 | private $wordPressWrapper; |
| 14 | |
| 15 | /** |
| 16 | * @param $dataStore |
| 17 | */ |
| 18 | public function __construct(DataStore $dataStore) |
| 19 | { |
| 20 | $this->dataStore = $dataStore; |
| 21 | |
| 22 | $this->wordPressWrapper = new WordPressWrapper(); |
| 23 | } |
| 24 | |
| 25 | public function setWordPressWrapper(WordPressWrapper $wordPressWrapper) |
| 26 | { |
| 27 | $this->wordPressWrapper = $wordPressWrapper; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * @param $domain_name |
| 32 | * |
| 33 | * @return mixed |
| 34 | */ |
| 35 | public function getDNSRecords($domain_name) |
| 36 | { |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * @param $domain_name |
| 42 | * @param DNSRecord $DNSRecord |
| 43 | * |
| 44 | * @return mixed |
| 45 | */ |
| 46 | public function addDNSRecord($domain_name, DNSRecord $DNSRecord) |
| 47 | { |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * @param $domain_name |
| 53 | * @param DNSRecord $DNSRecord |
| 54 | * |
| 55 | * @return mixed |
| 56 | */ |
| 57 | public function editDNSRecord($domain_name, DNSRecord $DNSRecord) |
| 58 | { |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @param $domain_name |
| 64 | * @param DNSRecord $DNSRecord |
| 65 | * |
| 66 | * @return mixed |
| 67 | */ |
| 68 | public function removeDNSRecord($domain_name, DNSRecord $DNSRecord) |
| 69 | { |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * @return mixed |
| 75 | */ |
| 76 | public function getHostAPIKey() |
| 77 | { |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * We wrap the return value with an array to be consistent between |
| 83 | * other plugins. |
| 84 | * |
| 85 | * @param null $userId |
| 86 | * |
| 87 | * @return mixed |
| 88 | */ |
| 89 | public function getDomainList($userId = null) |
| 90 | { |
| 91 | $cachedDomainName = $this->dataStore->getDomainNameCache(); |
| 92 | if (empty($cachedDomainName)) { |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | return array($cachedDomainName); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @return string |
| 101 | */ |
| 102 | public function getOriginalDomain() |
| 103 | { |
| 104 | $siteURL = $this->wordPressWrapper->getSiteURL(); |
| 105 | |
| 106 | return $this->formatDomain($siteURL); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * @return bool |
| 111 | */ |
| 112 | public function setDomainNameCache($newDomainName) |
| 113 | { |
| 114 | return $this->dataStore->setDomainNameCache($newDomainName); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * @return mixed |
| 119 | */ |
| 120 | public function getUserId() |
| 121 | { |
| 122 | return $this->dataStore->getCloudFlareEmail(); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * @param domain name |
| 127 | * |
| 128 | * @return string |
| 129 | */ |
| 130 | private function formatDomain($domainName) |
| 131 | { |
| 132 | // Remove instances which are before the domain name: |
| 133 | // * http |
| 134 | // * https |
| 135 | // * www |
| 136 | // * user:pass@ |
| 137 | preg_match_all('/^(?:https?:\/\/)?(?:[^@\/\n]+@)?(?:www\.)?([^:\/\n]+)/im', $domainName, $matches); |
| 138 | $formattedDomain = $matches[1][0]; |
| 139 | |
| 140 | return $formattedDomain; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * @return mixed |
| 145 | */ |
| 146 | public function checkIfValidCloudflareSubdomain($response, $domainName) |
| 147 | { |
| 148 | if (isset($response['result'])) { |
| 149 | foreach ($response['result'] as $zone) { |
| 150 | if (Utils::isSubdomainOf($domainName, $zone['name'])) { |
| 151 | return $zone['name']; |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | return false; |
| 157 | } |
| 158 | } |