├── LICENSE ├── composer.json ├── config └── cloudflareapi.php └── src ├── CloudFlareAPI.php ├── CloudFlareAPIManager.php ├── CloudFlareAPIServiceProvider.php ├── Facades └── CloudFlareAPI.php ├── Factories ├── ClientFactory.php ├── CloudFlareAPIFactory.php └── description.php ├── Models ├── AbstractModel.php ├── Ip.php ├── Record.php ├── Zone.php └── ZoneIp.php ├── Repositories ├── AbstractRepository.php ├── IpRepository.php └── ZoneRepository.php └── Subscribers └── ErrorSubscriber.php /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013-2015 Graham Campbell 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "graham-campbell/cloudflare-api", 3 | "description": "CloudFlare API Is A CloudFlare API Client For Laravel 5", 4 | "keywords": ["laravel", "framework", "cloudflare api", "cloudflare", "api", "CloudFlare API", "CloudFlare-API", "Laravel CloudFlare API", "Laravel-CloudFlare-API", "Graham Campbell", "GrahamCampbell"], 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Graham Campbell", 9 | "email": "graham@mineuk.com" 10 | } 11 | ], 12 | "require": { 13 | "php": ">=5.4.7", 14 | "illuminate/contracts": "5.0.*", 15 | "illuminate/support": "5.0.*", 16 | "graham-campbell/manager": "~2.0", 17 | "guzzlehttp/guzzle": "~5.0", 18 | "guzzlehttp/guzzle-services": "0.5.*", 19 | "guzzlehttp/retry-subscriber": "~2.0" 20 | }, 21 | "require-dev": { 22 | "graham-campbell/testbench": "~2.0" 23 | }, 24 | "autoload": { 25 | "psr-4": { 26 | "GrahamCampbell\\CloudFlareAPI\\": "src/" 27 | } 28 | }, 29 | "autoload-dev": { 30 | "psr-4": { 31 | "GrahamCampbell\\Tests\\CloudFlareAPI\\": "tests/" 32 | } 33 | }, 34 | "extra": { 35 | "branch-alias": { 36 | "dev-master": "0.7-dev" 37 | } 38 | }, 39 | "minimum-stability": "dev", 40 | "prefer-stable": true 41 | } 42 | -------------------------------------------------------------------------------- /config/cloudflareapi.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | return [ 13 | 14 | /* 15 | |-------------------------------------------------------------------------- 16 | | Default Connection Name 17 | |-------------------------------------------------------------------------- 18 | | 19 | | Here you may specify which of the connections below you wish to use as 20 | | your default connection for all work. Of course, you may use many 21 | | connections at once using the manager class. 22 | | 23 | */ 24 | 25 | 'default' => 'main', 26 | 27 | /* 28 | |-------------------------------------------------------------------------- 29 | | CloudFlare Connections 30 | |-------------------------------------------------------------------------- 31 | | 32 | | Here are each of the connections setup for your application. Example 33 | | configuration has been included, but you may add as many connections as 34 | | you would like. 35 | | 36 | */ 37 | 38 | 'connections' => [ 39 | 40 | 'main' => [ 41 | 'token' => 'your-token', 42 | 'email' => 'your-email', 43 | ], 44 | 45 | 'alternative' => [ 46 | 'token' => 'your-token', 47 | 'email' => 'your-email', 48 | ], 49 | 50 | ], 51 | 52 | ]; 53 | -------------------------------------------------------------------------------- /src/CloudFlareAPI.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GrahamCampbell\CloudFlareAPI; 13 | 14 | use GrahamCampbell\CloudFlareAPI\Repositories\IpRepository; 15 | use GrahamCampbell\CloudFlareAPI\Repositories\ZoneRepository; 16 | 17 | /** 18 | * This is the cloudflare api class. 19 | * 20 | * @author Graham Campbell 21 | */ 22 | class CloudFlareAPI 23 | { 24 | /** 25 | * The zone repository instance. 26 | * 27 | * @var \GrahamCampbell\CloudFlareAPI\Repositories\ZoneRepository 28 | */ 29 | protected $zone; 30 | 31 | /** 32 | * The ip repository instance. 33 | * 34 | * @var \GrahamCampbell\CloudFlareAPI\Repositories\IpRepository 35 | */ 36 | protected $ip; 37 | 38 | /** 39 | * Create a new cloudflare api instance. 40 | * 41 | * @param \GrahamCampbell\CloudFlareAPI\Repositories\ZoneRepository $zone 42 | * @param \GrahamCampbell\CloudFlareAPI\Repositories\IpRepository $ip 43 | * 44 | * @return void 45 | */ 46 | public function __construct(ZoneRepository $zone, IpRepository $ip) 47 | { 48 | $this->zone = $zone; 49 | $this->ip = $ip; 50 | } 51 | 52 | /** 53 | * Get a collection of all the zones. 54 | * 55 | * @return \Illuminate\Support\Collection 56 | */ 57 | public function zones() 58 | { 59 | return $this->zone->all(); 60 | } 61 | 62 | /** 63 | * Get a single zone object. 64 | * 65 | * @param string $zone 66 | * 67 | * @return \GrahamCampbell\CloudFlareAPI\Models\Zone 68 | */ 69 | public function zone($zone) 70 | { 71 | return $this->zone->get($zone); 72 | } 73 | 74 | /** 75 | * Get a single ip object. 76 | * 77 | * @param string $ip 78 | * 79 | * @return \GrahamCampbell\CloudFlareAPI\Models\Ip 80 | */ 81 | public function ip($ip) 82 | { 83 | return $this->ip->get($ip); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /src/CloudFlareAPIManager.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GrahamCampbell\CloudFlareAPI; 13 | 14 | use GrahamCampbell\CloudFlareAPI\Factories\CloudFlareAPIFactory; 15 | use GrahamCampbell\Manager\AbstractManager; 16 | use Illuminate\Contracts\Config\Repository; 17 | 18 | /** 19 | * This is the cloudflare api manager class. 20 | * 21 | * @author Graham Campbell 22 | */ 23 | class CloudFlareAPIManager extends AbstractManager 24 | { 25 | /** 26 | * The factory instance. 27 | * 28 | * @var \GrahamCampbell\CloudFlareAPI\Factories\CloudFlareAPIFactory 29 | */ 30 | protected $factory; 31 | 32 | /** 33 | * Create a new cloudflare api manager instance. 34 | * 35 | * @param \Illuminate\Contracts\Config\Repository $config 36 | * @param \GrahamCampbell\CloudFlareAPI\Factories\CloudFlareAPIFactory $factory 37 | * 38 | * @return void 39 | */ 40 | public function __construct(Repository $config, CloudFlareAPIFactory $factory) 41 | { 42 | parent::__construct($config); 43 | $this->factory = $factory; 44 | } 45 | 46 | /** 47 | * Create the connection instance. 48 | * 49 | * @param array $config 50 | * 51 | * @return \GrahamCampbell\CloudFlareAPI\CloudFlareAPI 52 | */ 53 | protected function createConnection(array $config) 54 | { 55 | return $this->factory->make($config); 56 | } 57 | 58 | /** 59 | * Get the configuration name. 60 | * 61 | * @return string 62 | */ 63 | protected function getConfigName() 64 | { 65 | return 'cloudflareapi'; 66 | } 67 | 68 | /** 69 | * Get the factory instance. 70 | * 71 | * @return \GrahamCampbell\CloudFlareAPI\Factories\CloudFlareAPIFactory 72 | */ 73 | public function getFactory() 74 | { 75 | return $this->factory; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/CloudFlareAPIServiceProvider.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GrahamCampbell\CloudFlareAPI; 13 | 14 | use Illuminate\Contracts\Foundation\Application; 15 | use Illuminate\Support\ServiceProvider; 16 | 17 | /** 18 | * This is the cloudflare api service provider class. 19 | * 20 | * @author Graham Campbell 21 | */ 22 | class CloudFlareAPIServiceProvider extends ServiceProvider 23 | { 24 | /** 25 | * Boot the service provider. 26 | * 27 | * @return void 28 | */ 29 | public function boot() 30 | { 31 | $this->setupConfig(); 32 | } 33 | 34 | /** 35 | * Setup the config. 36 | * 37 | * @return void 38 | */ 39 | protected function setupConfig() 40 | { 41 | $source = realpath(__DIR__.'/../config/cloudflareapi.php'); 42 | 43 | $this->publishes([$source => config_path('cloudflareapi.php')]); 44 | 45 | $this->mergeConfigFrom($source, 'cloudflareapi'); 46 | } 47 | 48 | /** 49 | * Register the service provider. 50 | * 51 | * @return void 52 | */ 53 | public function register() 54 | { 55 | $this->registerFactory($this->app); 56 | $this->registerManager($this->app); 57 | } 58 | 59 | /** 60 | * Register the factory class. 61 | * 62 | * @param \Illuminate\Contracts\Foundation\Application $app 63 | * 64 | * @return void 65 | */ 66 | protected function registerFactory(Application $app) 67 | { 68 | $app->singleton('cloudflareapi.factory', function () { 69 | $client = new Factories\ClientFactory(); 70 | 71 | return new Factories\CloudFlareAPIFactory($client); 72 | }); 73 | 74 | $app->alias('cloudflareapi.factory', 'GrahamCampbell\CloudFlareAPI\Factories\CloudFlareAPIFactory'); 75 | } 76 | 77 | /** 78 | * Register the manager class. 79 | * 80 | * @param \Illuminate\Contracts\Foundation\Application $app 81 | * 82 | * @return void 83 | */ 84 | protected function registerManager(Application $app) 85 | { 86 | $app->singleton('cloudflareapi', function ($app) { 87 | $config = $app['config']; 88 | $factory = $app['cloudflareapi.factory']; 89 | 90 | return new CloudFlareAPIManager($config, $factory); 91 | }); 92 | 93 | $app->alias('cloudflareapi', 'GrahamCampbell\CloudFlareAPI\CloudFlareAPIManager'); 94 | } 95 | 96 | /** 97 | * Get the services provided by the provider. 98 | * 99 | * @return string[] 100 | */ 101 | public function provides() 102 | { 103 | return [ 104 | 'cloudflareapi', 105 | 'cloudflareapi.factory', 106 | ]; 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /src/Facades/CloudFlareAPI.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GrahamCampbell\CloudFlareAPI\Facades; 13 | 14 | use Illuminate\Support\Facades\Facade; 15 | 16 | /** 17 | * This is the cloudflare api facade class. 18 | * 19 | * @author Graham Campbell 20 | */ 21 | class CloudFlareAPI extends Facade 22 | { 23 | /** 24 | * Get the registered name of the component. 25 | * 26 | * @return string 27 | */ 28 | protected static function getFacadeAccessor() 29 | { 30 | return 'cloudflareapi'; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Factories/ClientFactory.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GrahamCampbell\CloudFlareAPI\Factories; 13 | 14 | use GrahamCampbell\CloudFlareAPI\Subscribers\ErrorSubscriber; 15 | use GuzzleHttp\Client; 16 | use GuzzleHttp\Command\Guzzle\Description; 17 | use GuzzleHttp\Command\Guzzle\GuzzleClient; 18 | use GuzzleHttp\Subscriber\Retry\RetrySubscriber; 19 | 20 | /** 21 | * This is the client factory class. 22 | * 23 | * @author Graham Campbell 24 | */ 25 | class ClientFactory 26 | { 27 | /** 28 | * Make a new guzzle services client. 29 | * 30 | * @param string[] $config 31 | * 32 | * @return \GuzzleHttp\Command\Guzzle\GuzzleClient 33 | */ 34 | public function make(array $config) 35 | { 36 | $client = $this->makeBaseClient($config); 37 | 38 | return $this->makeServicesClient($client); 39 | } 40 | 41 | /** 42 | * Make a guzzle client. 43 | * 44 | * @param string[] $config 45 | * 46 | * @return \GuzzleHttp\Client 47 | */ 48 | protected function makeBaseClient($config) 49 | { 50 | $parameters = $this->getParameters($config); 51 | 52 | $client = new Client($parameters); 53 | 54 | $this->attachSubscribers($client); 55 | 56 | return $client; 57 | } 58 | 59 | /** 60 | * Get the client constructor parameters. 61 | * 62 | * @param string[] $config 63 | * 64 | * @return array 65 | */ 66 | protected function getParameters(array $config) 67 | { 68 | $config = $this->getConfig($config); 69 | 70 | return [ 71 | 'base_url' => 'https://www.cloudflare.com/api_json.html', 72 | 'defaults' => ['query' => ['tkn' => $config['token'], 'email' => $config['email']]], 73 | ]; 74 | } 75 | 76 | /** 77 | * Get the configuration. 78 | * 79 | * @param string[] $config 80 | * 81 | * @throws \InvalidArgumentException 82 | * 83 | * @return string[] 84 | */ 85 | protected function getConfig(array $config) 86 | { 87 | if (!array_key_exists('token', $config) || !array_key_exists('email', $config)) { 88 | throw new \InvalidArgumentException('The cloudflare client requires configuration.'); 89 | } 90 | 91 | return array_only($config, ['token', 'email']); 92 | } 93 | 94 | /** 95 | * Attach all subscribers to the guzzle client. 96 | * 97 | * @param \GuzzleHttp\Client $client 98 | * 99 | * @return \GuzzleHttp\Client 100 | */ 101 | protected function attachSubscribers(Client $client) 102 | { 103 | $client->getEmitter()->attach($this->getRetrySubscriber()); 104 | $client->getEmitter()->attach($this->getErrorSubscriber()); 105 | 106 | return $client; 107 | } 108 | 109 | /** 110 | * Get the retry subscriber. 111 | * 112 | * @return \GuzzleHttp\Subscriber\Retry\RetrySubscriber 113 | */ 114 | protected function getRetrySubscriber() 115 | { 116 | $filter = RetrySubscriber::createChainFilter([ 117 | RetrySubscriber::createIdempotentFilter(), 118 | RetrySubscriber::createStatusFilter(), 119 | ]); 120 | 121 | return new RetrySubscriber(['filter' => $filter]); 122 | } 123 | 124 | /** 125 | * Get the cloudflare api error subscriber. 126 | * 127 | * @return \GrahamCampbell\CloudFlareAPI\Subscribers\ErrorSubscriber 128 | */ 129 | protected function getErrorSubscriber() 130 | { 131 | return new ErrorSubscriber(); 132 | } 133 | 134 | /** 135 | * Make a new guzzle services client. 136 | * 137 | * @param \GuzzleHttp\Client $client 138 | * 139 | * @return \GuzzleHttp\Command\Guzzle\GuzzleClient 140 | */ 141 | protected function makeServicesClient(Client $client) 142 | { 143 | $parameters = $this->getDescription(); 144 | 145 | $description = new Description($parameters); 146 | 147 | return new GuzzleClient($client, $description); 148 | } 149 | 150 | /** 151 | * Get the description constructor parameters. 152 | * 153 | * @return array 154 | */ 155 | protected function getDescription() 156 | { 157 | return require 'description.php'; 158 | } 159 | } 160 | -------------------------------------------------------------------------------- /src/Factories/CloudFlareAPIFactory.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GrahamCampbell\CloudFlareAPI\Factories; 13 | 14 | use GrahamCampbell\CloudFlareAPI\CloudFlareAPI; 15 | use GrahamCampbell\CloudFlareAPI\Repositories\IpRepository; 16 | use GrahamCampbell\CloudFlareAPI\Repositories\ZoneRepository; 17 | 18 | /** 19 | * This is the cloudflare api factory class. 20 | * 21 | * @author Graham Campbell 22 | */ 23 | class CloudFlareAPIFactory 24 | { 25 | /** 26 | * The client factory instance. 27 | * 28 | * @var \GrahamCampbell\CloudFlareAPI\Factories\ClientFactory 29 | */ 30 | protected $client; 31 | 32 | /** 33 | * Create a new cloudflare api factory instance. 34 | * 35 | * @param \GrahamCampbell\CloudFlareAPI\Factories\ClientFactory $client 36 | * 37 | * @return void 38 | */ 39 | public function __construct(ClientFactory $client) 40 | { 41 | $this->client = $client; 42 | } 43 | 44 | /** 45 | * Make a new api instance. 46 | * 47 | * @param string[] $config 48 | * 49 | * @return \GrahamCampbell\CloudFlareAPI\CloudFlareAPI 50 | */ 51 | public function make(array $config) 52 | { 53 | $client = $this->createClient($config); 54 | $zone = new ZoneRepository($client); 55 | $ip = new IpRepository($client); 56 | 57 | return new CloudFlareAPI($zone, $ip); 58 | } 59 | 60 | /** 61 | * Get a new guzzle client. 62 | * 63 | * @param string[] $config 64 | * 65 | * @return \GuzzleHttp\Command\Guzzle\GuzzleClient 66 | */ 67 | public function createClient(array $config) 68 | { 69 | return $this->client->make($config); 70 | } 71 | 72 | /** 73 | * Get the client factory instance. 74 | * 75 | * @return \GrahamCampbell\CloudFlareAPI\Factories\ClientFactory 76 | */ 77 | public function getClient() 78 | { 79 | return $this->client; 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/Factories/description.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | return [ 13 | 'operations' => [ 14 | 'stats' => [ 15 | 'httpMethod' => 'GET', 16 | 'uri' => '?a=stats', 17 | 'responseModel' => 'jsonResponse', 18 | 'parameters' => [ 19 | 'z' => [ 20 | 'type' => 'string', 21 | 'location' => 'query', 22 | ], 23 | 'interval' => [ 24 | 'type' => 'integer', 25 | 'location' => 'query', 26 | ], 27 | ], 28 | ], 29 | 'zoneLoadMulti' => [ 30 | 'httpMethod' => 'GET', 31 | 'uri' => '?a=zone_load_multi', 32 | 'responseModel' => 'jsonResponse', 33 | 'parameters' => [ 34 | // none 35 | ], 36 | ], 37 | 'recLoadAll' => [ 38 | 'httpMethod' => 'GET', 39 | 'uri' => '?a=rec_load_all', 40 | 'responseModel' => 'jsonResponse', 41 | 'parameters' => [ 42 | 'z' => [ 43 | 'type' => 'string', 44 | 'location' => 'query', 45 | ], 46 | 'o' => [ 47 | 'type' => 'integer', 48 | 'location' => 'query', 49 | ], 50 | ], 51 | ], 52 | 'zoneCheck' => [ 53 | 'httpMethod' => 'GET', 54 | 'uri' => '?a=zone_check', 55 | 'responseModel' => 'jsonResponse', 56 | 'parameters' => [ 57 | 'zones' => [ 58 | 'type' => 'string', 59 | 'location' => 'query', 60 | ], 61 | ], 62 | ], 63 | 'zoneIps' => [ 64 | 'httpMethod' => 'GET', 65 | 'uri' => '?a=zone_ips&geo=1', 66 | 'responseModel' => 'jsonResponse', 67 | 'parameters' => [ 68 | 'z' => [ 69 | 'type' => 'string', 70 | 'location' => 'query', 71 | ], 72 | 'hours' => [ 73 | 'type' => 'integer', 74 | 'location' => 'query', 75 | ], 76 | 'class' => [ 77 | 'type' => 'string', 78 | 'location' => 'query', 79 | ], 80 | ], 81 | ], 82 | 'ipLkup' => [ 83 | 'httpMethod' => 'GET', 84 | 'uri' => '?a=ip_lkup', 85 | 'responseModel' => 'jsonResponse', 86 | 'parameters' => [ 87 | 'ip' => [ 88 | 'type' => 'string', 89 | 'location' => 'query', 90 | ], 91 | ], 92 | ], 93 | 'zoneSettings' => [ 94 | 'httpMethod' => 'GET', 95 | 'uri' => '?a=zone_settings', 96 | 'responseModel' => 'jsonResponse', 97 | 'parameters' => [ 98 | 'z' => [ 99 | 'type' => 'string', 100 | 'location' => 'query', 101 | ], 102 | ], 103 | ], 104 | 'secLvl' => [ 105 | 'httpMethod' => 'POST', 106 | 'uri' => '?a=sec_lvl', 107 | 'responseModel' => 'jsonResponse', 108 | 'parameters' => [ 109 | 'z' => [ 110 | 'type' => 'string', 111 | 'location' => 'query', 112 | ], 113 | 'v' => [ 114 | 'type' => 'string', 115 | 'location' => 'query', 116 | ], 117 | ], 118 | ], 119 | 'cacheLvl' => [ 120 | 'httpMethod' => 'POST', 121 | 'uri' => '?a=cache_lvl', 122 | 'responseModel' => 'jsonResponse', 123 | 'parameters' => [ 124 | 'z' => [ 125 | 'type' => 'string', 126 | 'location' => 'query', 127 | ], 128 | 'v' => [ 129 | 'type' => 'string', 130 | 'location' => 'query', 131 | ], 132 | ], 133 | ], 134 | 'devMode' => [ 135 | 'httpMethod' => 'POST', 136 | 'uri' => '?a=devmode', 137 | 'responseModel' => 'jsonResponse', 138 | 'parameters' => [ 139 | 'z' => [ 140 | 'type' => 'string', 141 | 'location' => 'query', 142 | ], 143 | 'v' => [ 144 | 'type' => 'integer', 145 | 'location' => 'query', 146 | ], 147 | ], 148 | ], 149 | 'fpurgeTs' => [ 150 | 'httpMethod' => 'POST', 151 | 'uri' => '?a=fpurge_ts&v=1', 152 | 'responseModel' => 'jsonResponse', 153 | 'parameters' => [ 154 | 'z' => [ 155 | 'type' => 'string', 156 | 'location' => 'query', 157 | ], 158 | ], 159 | ], 160 | 'zoneFilePurge' => [ 161 | 'httpMethod' => 'POST', 162 | 'uri' => '?a=zone_file_purge', 163 | 'responseModel' => 'jsonResponse', 164 | 'parameters' => [ 165 | 'z' => [ 166 | 'type' => 'string', 167 | 'location' => 'query', 168 | ], 169 | 'url' => [ 170 | 'type' => 'string', 171 | 'location' => 'query', 172 | ], 173 | ], 174 | ], 175 | 'zoneGrab' => [ 176 | 'httpMethod' => 'POST', 177 | 'uri' => '?a=zone_grab', 178 | 'responseModel' => 'jsonResponse', 179 | 'parameters' => [ 180 | 'zid' => [ 181 | 'type' => 'integer', 182 | 'location' => 'query', 183 | ], 184 | ], 185 | ], 186 | 'wl' => [ 187 | 'httpMethod' => 'POST', 188 | 'uri' => '?a=wl', 189 | 'responseModel' => 'jsonResponse', 190 | 'parameters' => [ 191 | 'key' => [ 192 | 'type' => 'string', 193 | 'location' => 'query', 194 | ], 195 | ], 196 | ], 197 | 'ban' => [ 198 | 'httpMethod' => 'POST', 199 | 'uri' => '?a=ban', 200 | 'responseModel' => 'jsonResponse', 201 | 'parameters' => [ 202 | 'key' => [ 203 | 'type' => 'string', 204 | 'location' => 'query', 205 | ], 206 | ], 207 | ], 208 | 'nul' => [ 209 | 'httpMethod' => 'POST', 210 | 'uri' => '?a=nul', 211 | 'responseModel' => 'jsonResponse', 212 | 'parameters' => [ 213 | 'key' => [ 214 | 'type' => 'string', 215 | 'location' => 'query', 216 | ], 217 | ], 218 | ], 219 | 'ipv46' => [ 220 | 'httpMethod' => 'POST', 221 | 'uri' => '?a=ipv46', 222 | 'responseModel' => 'jsonResponse', 223 | 'parameters' => [ 224 | 'z' => [ 225 | 'type' => 'string', 226 | 'location' => 'query', 227 | ], 228 | 'v' => [ 229 | 'type' => 'integer', 230 | 'location' => 'query', 231 | ], 232 | ], 233 | ], 234 | 'async' => [ 235 | 'httpMethod' => 'POST', 236 | 'uri' => '?a=async', 237 | 'responseModel' => 'jsonResponse', 238 | 'parameters' => [ 239 | 'z' => [ 240 | 'type' => 'string', 241 | 'location' => 'query', 242 | ], 243 | 'v' => [ 244 | 'type' => 'string', 245 | 'location' => 'query', 246 | ], 247 | ], 248 | ], 249 | 'minify' => [ 250 | 'httpMethod' => 'POST', 251 | 'uri' => '?a=minify', 252 | 'responseModel' => 'jsonResponse', 253 | 'parameters' => [ 254 | 'z' => [ 255 | 'type' => 'string', 256 | 'location' => 'query', 257 | ], 258 | 'v' => [ 259 | 'type' => 'integer', 260 | 'location' => 'query', 261 | ], 262 | ], 263 | ], 264 | 'mirage' => [ 265 | 'httpMethod' => 'POST', 266 | 'uri' => '?a=mirage2', 267 | 'responseModel' => 'jsonResponse', 268 | 'parameters' => [ 269 | 'z' => [ 270 | 'type' => 'string', 271 | 'location' => 'query', 272 | ], 273 | 'v' => [ 274 | 'type' => 'integer', 275 | 'location' => 'query', 276 | ], 277 | ], 278 | ], 279 | 'recNew' => [ 280 | 'httpMethod' => 'POST', 281 | 'uri' => '?a=rec_new', 282 | 'responseModel' => 'jsonResponse', 283 | 'parameters' => [ 284 | 'z' => [ 285 | 'type' => 'string', 286 | 'location' => 'query', 287 | ], 288 | 'type' => [ 289 | 'type' => 'string', 290 | 'location' => 'query', 291 | ], 292 | 'name' => [ 293 | 'type' => 'string', 294 | 'location' => 'query', 295 | ], 296 | 'content' => [ 297 | 'type' => 'string', 298 | 'location' => 'query', 299 | ], 300 | 'ttl' => [ 301 | 'type' => 'integer', 302 | 'location' => 'query', 303 | ], 304 | 'prio' => [ 305 | 'type' => 'integer', 306 | 'location' => 'query', 307 | ], 308 | 'service' => [ 309 | 'type' => 'string', 310 | 'location' => 'query', 311 | ], 312 | 'srvname' => [ 313 | 'type' => 'string', 314 | 'location' => 'query', 315 | ], 316 | 'protocol' => [ 317 | 'type' => 'string', 318 | 'location' => 'query', 319 | ], 320 | 'weight' => [ 321 | 'type' => 'integer', 322 | 'location' => 'query', 323 | ], 324 | 'port' => [ 325 | 'type' => 'integer', 326 | 'location' => 'query', 327 | ], 328 | 'target' => [ 329 | 'type' => 'string', 330 | 'location' => 'query', 331 | ], 332 | ], 333 | ], 334 | 'recEdit' => [ 335 | 'httpMethod' => 'POST', 336 | 'uri' => '?a=rec_edit', 337 | 'responseModel' => 'jsonResponse', 338 | 'parameters' => [ 339 | 'z' => [ 340 | 'type' => 'string', 341 | 'location' => 'query', 342 | ], 343 | 'type' => [ 344 | 'type' => 'string', 345 | 'location' => 'query', 346 | ], 347 | 'id' => [ 348 | 'type' => 'integer', 349 | 'location' => 'query', 350 | ], 351 | 'name' => [ 352 | 'type' => 'string', 353 | 'location' => 'query', 354 | ], 355 | 'content' => [ 356 | 'type' => 'string', 357 | 'location' => 'query', 358 | ], 359 | 'ttl' => [ 360 | 'type' => 'integer', 361 | 'location' => 'query', 362 | ], 363 | 'prio' => [ 364 | 'type' => 'integer', 365 | 'location' => 'query', 366 | ], 367 | 'service' => [ 368 | 'type' => 'string', 369 | 'location' => 'query', 370 | ], 371 | 'srvname' => [ 372 | 'type' => 'string', 373 | 'location' => 'query', 374 | ], 375 | 'protocol' => [ 376 | 'type' => 'string', 377 | 'location' => 'query', 378 | ], 379 | 'weight' => [ 380 | 'type' => 'integer', 381 | 'location' => 'query', 382 | ], 383 | 'port' => [ 384 | 'type' => 'integer', 385 | 'location' => 'query', 386 | ], 387 | 'target' => [ 388 | 'type' => 'string', 389 | 'location' => 'query', 390 | ], 391 | ], 392 | ], 393 | 'recDelete' => [ 394 | 'httpMethod' => 'POST', 395 | 'uri' => '?a=rec_delete', 396 | 'responseModel' => 'jsonResponse', 397 | 'parameters' => [ 398 | 'z' => [ 399 | 'type' => 'string', 400 | 'location' => 'query', 401 | ], 402 | 'id' => [ 403 | 'type' => 'integer', 404 | 'location' => 'query', 405 | ], 406 | ], 407 | ], 408 | ], 409 | 'models' => [ 410 | 'jsonResponse' => [ 411 | 'type' => 'object', 412 | 'additionalProperties' => [ 413 | 'location' => 'json', 414 | ], 415 | ], 416 | ], 417 | ]; 418 | -------------------------------------------------------------------------------- /src/Models/AbstractModel.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GrahamCampbell\CloudFlareAPI\Models; 13 | 14 | use GuzzleHttp\Command\Guzzle\GuzzleClient; 15 | 16 | /** 17 | * This is the abstract model class. 18 | * 19 | * @author Graham Campbell 20 | */ 21 | abstract class AbstractModel 22 | { 23 | /** 24 | * The guzzle client class. 25 | * 26 | * @var \GuzzleHttp\Command\Guzzle\GuzzleClient 27 | */ 28 | protected $client; 29 | 30 | /** 31 | * The request cache. 32 | * 33 | * @var array 34 | */ 35 | protected $cache; 36 | 37 | /** 38 | * Create a new model instance. 39 | * 40 | * @param \GuzzleHttp\Command\Guzzle\GuzzleClient $client 41 | * @param array $cache 42 | * 43 | * @return void 44 | */ 45 | public function __construct(GuzzleClient $client, array $cache = []) 46 | { 47 | $this->client = $client; 48 | $this->cache = $cache; 49 | } 50 | 51 | /** 52 | * Clear the request cache. 53 | * 54 | * @param string|string[] $methods 55 | * 56 | * @return $this 57 | */ 58 | public function clearCache($methods = null) 59 | { 60 | if ($methods === null || $methods === 'all') { 61 | $this->cache = []; 62 | } else { 63 | foreach ((array) $methods as $method) { 64 | $this->cache[$method] = []; 65 | } 66 | } 67 | 68 | return $this; 69 | } 70 | 71 | /** 72 | * Make a get request. 73 | * 74 | * @param string $method 75 | * @param array $data 76 | * @param string $key 77 | * 78 | * @return array 79 | */ 80 | protected function get($method, array $data = [], $key = 'key') 81 | { 82 | $data = $this->data($data); 83 | 84 | if (!isset($this->cache[$method][$key])) { 85 | $this->cache[$method][$key] = $this->client->$method($data); 86 | } 87 | 88 | return $this->cache[$method][$key]; 89 | } 90 | 91 | /** 92 | * Make a request. 93 | * 94 | * @param string $method 95 | * @param array $data 96 | * @param string|string[]|bool|null $flush 97 | * 98 | * @return array 99 | */ 100 | protected function action($method, array $data = [], $flush = null) 101 | { 102 | $data = $this->data($data); 103 | 104 | $this->clearCache($flush); 105 | 106 | return $this->client->$method($data); 107 | } 108 | 109 | /** 110 | * Get the data to make a request. 111 | * 112 | * @param array $data 113 | * 114 | * @return array 115 | */ 116 | protected function data(array $data = []) 117 | { 118 | return $data; 119 | } 120 | 121 | /** 122 | * Get the guzzle client instance. 123 | * 124 | * @return \GuzzleHttp\Command\Guzzle\GuzzleClient 125 | */ 126 | public function getClient() 127 | { 128 | return $this->client; 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /src/Models/Ip.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GrahamCampbell\CloudFlareAPI\Models; 13 | 14 | use GuzzleHttp\Command\Guzzle\GuzzleClient; 15 | 16 | /** 17 | * This is the ip model class. 18 | * 19 | * @author Graham Campbell 20 | */ 21 | class Ip extends AbstractModel 22 | { 23 | /** 24 | * The ip address. 25 | * 26 | * @var string 27 | */ 28 | protected $ip; 29 | 30 | /** 31 | * Create a new model instance. 32 | * 33 | * @param \GuzzleHttp\Command\Guzzle\GuzzleClient $client 34 | * @param string $ip 35 | * @param array $cache 36 | * 37 | * @return void 38 | */ 39 | public function __construct(GuzzleClient $client, $ip, array $cache = []) 40 | { 41 | parent::__construct($client, $cache); 42 | 43 | $this->ip = (string) $ip; 44 | } 45 | 46 | /** 47 | * Get the ip address. 48 | * 49 | * @return string 50 | */ 51 | public function getIp() 52 | { 53 | return (string) $this->ip; 54 | } 55 | 56 | /** 57 | * Get the threat score. 58 | * 59 | * @return string 60 | */ 61 | public function getScore() 62 | { 63 | $ipLkup = $this->get('ipLkup', ['ip' => $this->ip]); 64 | 65 | $score = $ipLkup['response'][$this->ip]; 66 | 67 | if ($score) { 68 | return (string) $score; 69 | } else { 70 | return 'unknown'; 71 | } 72 | } 73 | 74 | /** 75 | * Whitelist this ip. 76 | * 77 | * @return $this 78 | */ 79 | public function whitelist() 80 | { 81 | $this->action('wl', ['key' => $this->ip], false); 82 | 83 | return $this; 84 | } 85 | 86 | /** 87 | * Ban this ip. 88 | * 89 | * @return $this 90 | */ 91 | public function ban() 92 | { 93 | $this->action('ban', ['key' => $this->ip], false); 94 | 95 | return $this; 96 | } 97 | 98 | /** 99 | * Unlist this ip. 100 | * 101 | * @return $this 102 | */ 103 | public function unlist() 104 | { 105 | $this->action('nul', ['key' => $this->ip], false); 106 | 107 | return $this; 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /src/Models/Record.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GrahamCampbell\CloudFlareAPI\Models; 13 | 14 | use GuzzleHttp\Command\Guzzle\GuzzleClient; 15 | 16 | /** 17 | * This is the zone model class. 18 | * 19 | * @author Graham Campbell 20 | */ 21 | class Record extends AbstractModel 22 | { 23 | /** 24 | * The id. 25 | * 26 | * @var int 27 | */ 28 | protected $id; 29 | 30 | /** 31 | * The zone object. 32 | * 33 | * @var \GrahamCampbell\CloudFlareAPI\Models\Zone 34 | */ 35 | protected $zone; 36 | 37 | /** 38 | * Create a new model instance. 39 | * 40 | * @param \GuzzleHttp\Command\Guzzle\GuzzleClient $client 41 | * @param int $id 42 | * @param \GrahamCampbell\CloudFlareAPI\Models\Zone $zone 43 | * @param array $cache 44 | * 45 | * @return void 46 | */ 47 | public function __construct(GuzzleClient $client, $id, Zone $zone, array $cache = []) 48 | { 49 | parent::__construct($client, $cache); 50 | 51 | $this->id = (int) $id; 52 | $this->zone = $zone; 53 | } 54 | 55 | /** 56 | * Get the id. 57 | * 58 | * @return int 59 | */ 60 | public function getId() 61 | { 62 | return (int) $this->id; 63 | } 64 | 65 | /** 66 | * Get the name. 67 | * 68 | * @return string 69 | */ 70 | public function getName() 71 | { 72 | return (string) $this->lookup('name'); 73 | } 74 | 75 | /** 76 | * Get the type. 77 | * 78 | * @return string 79 | */ 80 | public function getType() 81 | { 82 | return (string) $this->lookup('type'); 83 | } 84 | 85 | /** 86 | * Get the priority. 87 | * 88 | * @return int 89 | */ 90 | public function getPriority() 91 | { 92 | return (int) $this->lookup('prio'); 93 | } 94 | 95 | /** 96 | * Get the content. 97 | * 98 | * @return string 99 | */ 100 | public function getContent() 101 | { 102 | return (string) $this->lookup('content'); 103 | } 104 | 105 | /** 106 | * Get the time to live. 107 | * 108 | * @return int 109 | */ 110 | public function getTtl() 111 | { 112 | return (int) $this->lookup('ttl_ceil'); 113 | } 114 | 115 | /** 116 | * Is the time to live automatic? 117 | * 118 | * @return bool 119 | */ 120 | public function isTtlAutomatic() 121 | { 122 | return (bool) $this->lookup('auto_ttl'); 123 | } 124 | 125 | /** 126 | * Get the ssl id. 127 | * 128 | * @return int 129 | */ 130 | public function getSslId() 131 | { 132 | return (int) $this->lookup('ssl_id'); 133 | } 134 | 135 | // TODO: ssl methods are incomplete 136 | 137 | /** 138 | * Are we in service? 139 | * 140 | * @return bool 141 | */ 142 | public function isInService() 143 | { 144 | return (bool) $this->lookup('service_mode'); 145 | } 146 | 147 | /** 148 | * Are we proxiable? 149 | * 150 | * @return bool 151 | */ 152 | public function isProxiable() 153 | { 154 | return (bool) $this->lookup('props')['proxiable']; 155 | } 156 | 157 | /** 158 | * Is the cloud on? 159 | * 160 | * @return bool 161 | */ 162 | public function isTheCloudOn() 163 | { 164 | return (bool) $this->lookup('props')['cloud_on']; 165 | } 166 | 167 | /** 168 | * Are we open? 169 | * 170 | * @return bool 171 | */ 172 | public function isOpen() 173 | { 174 | return (bool) $this->lookup('props')['cf_open']; 175 | } 176 | 177 | /** 178 | * Are we vanity locked? 179 | * 180 | * @return bool 181 | */ 182 | public function isVanityLocked() 183 | { 184 | return (bool) $this->lookup('props')['vanity_lock']; 185 | } 186 | 187 | /** 188 | * Dump all data. 189 | * 190 | * @param array $data 191 | * 192 | * @return $this 193 | */ 194 | public function modify(array $data) 195 | { 196 | $data['id'] = $this->getId(); 197 | 198 | $this->action('recEdit', $data, 'recLoad'); 199 | 200 | return $this; 201 | } 202 | 203 | /** 204 | * Lookup information. 205 | * 206 | * @param string $key 207 | * 208 | * @return mixed 209 | */ 210 | protected function lookup($key) 211 | { 212 | if (!$this->cache['recLoad']) { 213 | $records = $this->client->recLoadAll($this->data())['response']['recs']['objs']; 214 | foreach ($records as $record) { 215 | if ((int) $record['rec_id'] === $this->id) { 216 | $this->cache['recLoad'] = $record; 217 | break; 218 | } 219 | } 220 | } 221 | 222 | return $this->cache['recLoad'][$key]; 223 | } 224 | 225 | /** 226 | * Get the data to make a request. 227 | * 228 | * @param array $data 229 | * 230 | * @return array 231 | */ 232 | protected function data(array $data = []) 233 | { 234 | return array_merge(['z' => $this->zone->getZone()], $data); 235 | } 236 | 237 | /** 238 | * Clear the request cache. 239 | * 240 | * This method overrides the method in the parent class. 241 | * 242 | * @param string|string[] $methods 243 | * 244 | * @return $this 245 | */ 246 | public function clearCache($methods = null) 247 | { 248 | if ($methods === null || $methods === 'all') { 249 | $this->cache = []; 250 | } else { 251 | foreach ((array) $methods as $method) { 252 | $this->cache[$method] = []; 253 | // we may need to clear out the record cache in the zone model 254 | // to avoid unintuitive behaviour 255 | if (($method == 'recLoad' || !$method) && $method !== false) { 256 | $this->zone->clearRecordCache(); 257 | } 258 | } 259 | } 260 | 261 | return $this; 262 | } 263 | 264 | /** 265 | * Get the zone instance. 266 | * 267 | * @return \GrahamCampbell\CloudFlareAPI\Models\Zone 268 | */ 269 | public function getZone() 270 | { 271 | return $this->zone; 272 | } 273 | } 274 | -------------------------------------------------------------------------------- /src/Models/Zone.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GrahamCampbell\CloudFlareAPI\Models; 13 | 14 | use GuzzleHttp\Command\Guzzle\GuzzleClient; 15 | use Illuminate\Support\Collection; 16 | 17 | /** 18 | * This is the zone model class. 19 | * 20 | * @author Graham Campbell 21 | */ 22 | class Zone extends AbstractModel 23 | { 24 | /** 25 | * The zone name. 26 | * 27 | * @var string 28 | */ 29 | protected $zone; 30 | 31 | /** 32 | * Create a new model instance. 33 | * 34 | * @param \GuzzleHttp\Command\Guzzle\GuzzleClient $client 35 | * @param string $zone 36 | * @param array $cache 37 | * 38 | * @return void 39 | */ 40 | public function __construct(GuzzleClient $client, $zone, array $cache = []) 41 | { 42 | parent::__construct($client, $cache); 43 | 44 | $this->zone = (string) $zone; 45 | } 46 | 47 | /** 48 | * Get the zone name. 49 | * 50 | * @return string 51 | */ 52 | public function getZone() 53 | { 54 | return (string) $this->zone; 55 | } 56 | 57 | /** 58 | * Get the traffic information. 59 | * 60 | * @param int $time 61 | * 62 | * @return array 63 | */ 64 | public function getTraffic($time = 20) 65 | { 66 | return (array) $this->stat($time, 'trafficBreakdown'); 67 | } 68 | 69 | /** 70 | * Get the bandwidth information. 71 | * 72 | * @param int $time 73 | * 74 | * @return array 75 | */ 76 | public function getBandwidth($time = 20) 77 | { 78 | return (array) $this->stat($time, 'bandwidthServed'); 79 | } 80 | 81 | /** 82 | * Get the requests information. 83 | * 84 | * @param int $time 85 | * 86 | * @return array 87 | */ 88 | public function getRequests($time = 20) 89 | { 90 | return (array) $this->stat($time, 'requestsServed'); 91 | } 92 | 93 | /** 94 | * Get the user security level. 95 | * 96 | * @return string 97 | */ 98 | public function getUserSecurity() 99 | { 100 | return (string) $this->setting('userSecuritySetting'); 101 | } 102 | 103 | /** 104 | * Is in dev mode? 105 | * 106 | * @return bool 107 | */ 108 | public function isInDevMode() 109 | { 110 | return (bool) $this->setting('dev_mode'); 111 | } 112 | 113 | /** 114 | * Get the supported ip versions. 115 | * 116 | * @return int 117 | */ 118 | public function getIpVersions() 119 | { 120 | return (int) $this->setting('ipv46'); 121 | } 122 | 123 | /** 124 | * Is always online enabled? 125 | * 126 | * @return bool 127 | */ 128 | public function isAlwaysOnline() 129 | { 130 | return (bool) $this->setting('ob'); 131 | } 132 | 133 | /** 134 | * Get the cache level. 135 | * 136 | * @return string 137 | */ 138 | public function getCacheLevel() 139 | { 140 | return (string) $this->setting('cache_lvl'); 141 | } 142 | 143 | /** 144 | * Is outbound linking analysis enabled? 145 | * 146 | * @return bool 147 | */ 148 | public function isOutboundinking() 149 | { 150 | return (bool) ($this->setting('outboundLinks') != 'disabled'); 151 | } 152 | 153 | /** 154 | * Get the rocket loader level. 155 | * 156 | * @return string 157 | */ 158 | public function getRocketLoader() 159 | { 160 | return (string) $this->setting('async'); 161 | } 162 | 163 | /** 164 | * Is browser checking enabled? 165 | * 166 | * @return bool 167 | */ 168 | public function isBrowserChecking() 169 | { 170 | return (bool) $this->setting('bic'); 171 | } 172 | 173 | /** 174 | * Get the challenge page's time to live. 175 | * 176 | * @return int 177 | */ 178 | public function getChallengeTtl() 179 | { 180 | return (int) $this->setting('chl_ttl'); 181 | } 182 | 183 | /** 184 | * Get the cached items' time to live. 185 | * 186 | * @return int 187 | */ 188 | public function getExpireTtl() 189 | { 190 | return (int) $this->setting('exp_ttl'); 191 | } 192 | 193 | /** 194 | * Is hot linking protection enabled? 195 | * 196 | * @return bool 197 | */ 198 | public function isHotlinkingProtected() 199 | { 200 | return (bool) $this->setting('hotlink'); 201 | } 202 | 203 | /** 204 | * Get auto-resizing configuration. 205 | * 206 | * @return int 207 | */ 208 | public function getAutoResizing() 209 | { 210 | return (int) $this->setting('img'); 211 | } 212 | 213 | /** 214 | * Is lazy loading enabled? 215 | * 216 | * @return bool 217 | */ 218 | public function isLazyLoading() 219 | { 220 | return (bool) $this->setting('lazy'); 221 | } 222 | 223 | /** 224 | * Get the minification level. 225 | * 226 | * @return int 227 | */ 228 | public function getMinification() 229 | { 230 | return (int) $this->setting('minify'); 231 | } 232 | 233 | /** 234 | * Is pre-loading enabled? 235 | * 236 | * @return bool 237 | */ 238 | public function isPreloading() 239 | { 240 | return (bool) $this->setting('preload'); 241 | } 242 | 243 | /** 244 | * Are smart errors enabled? 245 | * 246 | * @return bool 247 | */ 248 | public function areErrorsSmart() 249 | { 250 | return (bool) $this->setting('s404'); 251 | } 252 | 253 | /** 254 | * Get security level. 255 | * 256 | * @return string 257 | */ 258 | public function getSecurityLevel() 259 | { 260 | return (string) $this->setting('sec_lvl'); 261 | } 262 | 263 | /** 264 | * Is spdy enabled? 265 | * 266 | * @return bool 267 | */ 268 | public function isSpdy() 269 | { 270 | return (bool) $this->setting('spdy'); 271 | } 272 | 273 | /** 274 | * Get ssl level. 275 | * 276 | * @return int 277 | */ 278 | public function getSsl() 279 | { 280 | return (int) $this->setting('ssl'); 281 | } 282 | 283 | /** 284 | * Get web application firewall profile. 285 | * 286 | * @return string 287 | */ 288 | public function getWafProfile() 289 | { 290 | return (string) $this->setting('waf_profile'); 291 | } 292 | 293 | /** 294 | * Set the security level. 295 | * 296 | * @param string $level 297 | * 298 | * @return $this 299 | */ 300 | public function setSecurityLevel($level) 301 | { 302 | $this->action('secLvl', ['v' => $level], 'zoneSettings'); 303 | 304 | return $this; 305 | } 306 | 307 | /** 308 | * Set the cache level. 309 | * 310 | * @param string $level 311 | * 312 | * @return $this 313 | */ 314 | public function setCacheLevel($level) 315 | { 316 | $this->action('cacheLvl', ['v' => $level], 'zoneSettings'); 317 | 318 | return $this; 319 | } 320 | 321 | /** 322 | * Enable dev mode. 323 | * 324 | * @return $this 325 | */ 326 | public function enableDevMode() 327 | { 328 | $this->action('devMode', ['v' => 1], 'zoneSettings'); 329 | 330 | return $this; 331 | } 332 | 333 | /** 334 | * Disable dev mode. 335 | * 336 | * @return $this 337 | */ 338 | public function disableDevMode() 339 | { 340 | $this->action('devMode', ['v' => 0], 'zoneSettings'); 341 | 342 | return $this; 343 | } 344 | 345 | /** 346 | * Purge all entries from the cloudflare cache. 347 | * 348 | * @return $this 349 | */ 350 | public function purgeAll() 351 | { 352 | $this->action('fpurgeTs', ['v' => 1], false); 353 | 354 | return $this; 355 | } 356 | 357 | /** 358 | * Purge a single url from the cloudflare cache. 359 | * 360 | * @param string $url 361 | * 362 | * @return $this 363 | */ 364 | public function purgeUrl($url) 365 | { 366 | $this->action('zoneFilePurge', ['url' => $url], false); 367 | 368 | return $this; 369 | } 370 | 371 | /** 372 | * Purge multiple urls from the cloudflare cache. 373 | * 374 | * @param string[] $urls 375 | * 376 | * @return $this 377 | */ 378 | public function purgeUrls(array $urls) 379 | { 380 | foreach ($urls as $url) { 381 | $this->purgeUrl($url); 382 | } 383 | 384 | return $this; 385 | } 386 | 387 | /** 388 | * Updated the site snapshot on the challenge page. 389 | * 390 | * @return $this 391 | */ 392 | public function updateSnapshot() 393 | { 394 | $zoneSettings = $this->get('zoneCheck', ['zones' => $this->zone]); 395 | 396 | $zid = (int) array_get($zoneSettings, 'response.zones')[$this->zone]; 397 | 398 | $this->action('zoneGrab', ['zid' => $zid], false); 399 | 400 | return $this; 401 | } 402 | 403 | /** 404 | * Set the supported the ip versions. 405 | * 406 | * @param int $versions 407 | * 408 | * @return $this 409 | */ 410 | public function setIpVersions($versions) 411 | { 412 | $this->action('ipv46', ['v' => $versions], 'zoneSettings'); 413 | 414 | return $this; 415 | } 416 | 417 | /** 418 | * Set the rocket loader level. 419 | * 420 | * @param string $level 421 | * 422 | * @return $this 423 | */ 424 | public function setRocketLoader($level) 425 | { 426 | $this->action('async', ['v' => $level], 'zoneSettings'); 427 | 428 | return $this; 429 | } 430 | 431 | /** 432 | * Set the minification level. 433 | * 434 | * @param int $level 435 | * 436 | * @return $this 437 | */ 438 | public function setMinification($level) 439 | { 440 | $this->action('minify', ['v' => $level], 'zoneSettings'); 441 | 442 | return $this; 443 | } 444 | 445 | /** 446 | * Enable mirage2. 447 | * 448 | * @return $this 449 | */ 450 | public function enableMirage() 451 | { 452 | $this->action('mirage', ['v' => 1], 'zoneSettings'); 453 | 454 | return $this; 455 | } 456 | 457 | /** 458 | * Disable mirage2. 459 | * 460 | * @return $this 461 | */ 462 | public function disableMirage() 463 | { 464 | $this->action('mirage', ['v' => 0], 'zoneSettings'); 465 | 466 | return $this; 467 | } 468 | 469 | /** 470 | * Get all ips as a collection of models. 471 | * 472 | * @param int $hours 473 | * @param string|null $class 474 | * 475 | * @return \Illuminate\Support\Collection 476 | */ 477 | public function ips($hours = 24, $class = null) 478 | { 479 | $zoneIps = $this->get('zoneIps', $this->data(compact('hours', 'class')), $hours.$class); 480 | 481 | $ips = array_get($zoneIps, 'response.ips'); 482 | 483 | $all = new Collection(); 484 | 485 | foreach ($ips as $ip) { 486 | $name = $ip['ip']; 487 | $zoneIp = new ZoneIP($this->client, $name, $this, ['zoneIp' => $ip]); 488 | $all->put($name, $zoneIp); 489 | } 490 | 491 | return $all; 492 | } 493 | 494 | /** 495 | * Get an ip as a model. 496 | * 497 | * @param string $address 498 | * @param int $hours 499 | * @param string|null $class 500 | * 501 | * @return \GrahamCampbell\CloudFlareAPI\Models\ZoneIp 502 | */ 503 | public function ip($address, $hours = 24, $class = null) 504 | { 505 | $zoneIps = $this->get('zoneIps', $this->data(compact('hours', 'class')), $hours.$class); 506 | 507 | $ips = array_get($zoneIps, 'response.ips'); 508 | 509 | foreach ($ips as $ip) { 510 | if ($ip['ip'] == $address) { 511 | return new ZoneIP($this->client, $address, $this, ['zoneIp' => $ip]); 512 | } 513 | } 514 | } 515 | 516 | /** 517 | * Get all dns records as a collection of models. 518 | * 519 | * @return \Illuminate\Support\Collection 520 | */ 521 | public function records() 522 | { 523 | $recs = $this->get('recLoadAll'); 524 | 525 | $records = array_get($recs, 'response.recs.objs'); 526 | 527 | $all = new Collection(); 528 | 529 | foreach ($records as $record) { 530 | $id = (int) $record['rec_id']; 531 | $model = new Record($this->client, $id, $this, ['recLoad' => $record]); 532 | $all->put($id, $model); 533 | } 534 | 535 | return $all; 536 | } 537 | 538 | /** 539 | * Get a dns record as a model. 540 | * 541 | * @param int $id 542 | * 543 | * @return \GrahamCampbell\CloudFlareAPI\Models\Record 544 | */ 545 | public function record($id) 546 | { 547 | $recs = $this->get('recLoadAll'); 548 | 549 | $records = array_get($recs, 'response.recs.objs'); 550 | 551 | foreach ($records as $record) { 552 | if ((int) $record['rec_id'] === (int) $id) { 553 | return new Record($this->client, (int) $id, $this, ['recLoad' => $record]); 554 | } 555 | } 556 | } 557 | 558 | /** 559 | * Create a new record. 560 | * 561 | * @param array $data 562 | * 563 | * @return $this 564 | */ 565 | public function createRecord(array $data) 566 | { 567 | $this->action('recNew', $data, 'recLoadAll'); 568 | 569 | return $this; 570 | } 571 | 572 | /** 573 | * Clear all dns record caches. 574 | * 575 | * This function will be automatically called by the record model whenever 576 | * it is updated. This method is not intended for public use. 577 | * 578 | * @return $this 579 | */ 580 | public function clearRecordCache() 581 | { 582 | return $this->clearCache('recLoadAll'); 583 | } 584 | 585 | /** 586 | * Lookup a statistic. 587 | * 588 | * @param int $time 589 | * @param string $name 590 | * 591 | * @return array 592 | */ 593 | protected function stat($time, $name) 594 | { 595 | $stats = $this->get('stats', ['interval' => $time], (string) $time); 596 | 597 | return array_get($stats, 'response.result.objs.0.'.$name); 598 | } 599 | 600 | /** 601 | * Lookup a setting. 602 | * 603 | * @param string $name 604 | * 605 | * @return mixed 606 | */ 607 | protected function setting($name) 608 | { 609 | $zoneSettings = $this->get('zoneSettings'); 610 | 611 | return array_get($zoneSettings, 'response.result.objs.0.'.$name); 612 | } 613 | 614 | /** 615 | * Get the data to make a request. 616 | * 617 | * @param array $data 618 | * 619 | * @return array 620 | */ 621 | protected function data(array $data = []) 622 | { 623 | return array_merge(['z' => $this->zone], $data); 624 | } 625 | } 626 | -------------------------------------------------------------------------------- /src/Models/ZoneIp.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GrahamCampbell\CloudFlareAPI\Models; 13 | 14 | use GuzzleHttp\Command\Guzzle\GuzzleClient; 15 | 16 | /** 17 | * This is the zone ip model class. 18 | * 19 | * @author Graham Campbell 20 | */ 21 | class ZoneIp extends Ip 22 | { 23 | /** 24 | * The zone object. 25 | * 26 | * @var \GrahamCampbell\CloudFlareAPI\Models\Zone 27 | */ 28 | protected $zone; 29 | 30 | /** 31 | * Create a new model instance. 32 | * 33 | * @param \GuzzleHttp\Command\Guzzle\GuzzleClient $client 34 | * @param string $ip 35 | * @param \GrahamCampbell\CloudFlareAPI\Models\Zone $zone 36 | * @param array $cache 37 | * 38 | * @return void 39 | */ 40 | public function __construct(GuzzleClient $client, $ip, Zone $zone, array $cache = []) 41 | { 42 | parent::__construct($client, $ip, $cache); 43 | 44 | $this->zone = $zone; 45 | } 46 | 47 | /** 48 | * Get the threat classification. 49 | * 50 | * @return string 51 | */ 52 | public function getClassification() 53 | { 54 | return (string) $this->lookup('classification'); 55 | } 56 | 57 | /** 58 | * Get the number of hits. 59 | * 60 | * @return int 61 | */ 62 | public function getHits() 63 | { 64 | return (int) $this->lookup('hits'); 65 | } 66 | 67 | /** 68 | * Get the latitude. 69 | * 70 | * @return int 71 | */ 72 | public function getLatitude() 73 | { 74 | return (int) $this->lookup('latitude'); 75 | } 76 | 77 | /** 78 | * Get the longitude. 79 | * 80 | * @return int 81 | */ 82 | public function getLongitude() 83 | { 84 | return (int) $this->lookup('longitude'); 85 | } 86 | 87 | /** 88 | * Lookup information. 89 | * 90 | * @param string $key 91 | * 92 | * @return mixed 93 | */ 94 | protected function lookup($key) 95 | { 96 | if (!$this->cache['zoneIp']) { 97 | $data = ['z' => $this->zone->getZone()]; 98 | $ips = $this->client->zoneIps($this->data($data))['response']['ips']; 99 | foreach ($ips as $ip) { 100 | if ($ip['ip'] == $this->ip) { 101 | $this->cache['zoneIp'] = $ip; 102 | break; 103 | } 104 | } 105 | } 106 | 107 | return $this->cache['zoneIp'][$key]; 108 | } 109 | 110 | /** 111 | * Get the zone instance. 112 | * 113 | * @return \GrahamCampbell\CloudFlareAPI\Models\Zone 114 | */ 115 | public function getZone() 116 | { 117 | return $this->zone; 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /src/Repositories/AbstractRepository.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GrahamCampbell\CloudFlareAPI\Repositories; 13 | 14 | use GuzzleHttp\Command\Guzzle\GuzzleClient; 15 | 16 | /** 17 | * This is the abstract repository class. 18 | * 19 | * @author Graham Campbell 20 | */ 21 | abstract class AbstractRepository 22 | { 23 | /** 24 | * The guzzle client class. 25 | * 26 | * @var \GuzzleHttp\Command\Guzzle\GuzzleClient 27 | */ 28 | protected $client; 29 | 30 | /** 31 | * Create a new repository instance. 32 | * 33 | * @param \GuzzleHttp\Command\Guzzle\GuzzleClient $client 34 | * 35 | * @return void 36 | */ 37 | public function __construct(GuzzleClient $client) 38 | { 39 | $this->client = $client; 40 | } 41 | 42 | /** 43 | * Get the guzzle client instance. 44 | * 45 | * @return \GuzzleHttp\Command\Guzzle\GuzzleClient 46 | */ 47 | public function getClient() 48 | { 49 | return $this->client; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/Repositories/IpRepository.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GrahamCampbell\CloudFlareAPI\Repositories; 13 | 14 | use GrahamCampbell\CloudFlareAPI\Models\Ip; 15 | 16 | /** 17 | * This is the ip repository class. 18 | * 19 | * @author Graham Campbell 20 | */ 21 | class IpRepository extends AbstractRepository 22 | { 23 | /** 24 | * Get a single ip object. 25 | * 26 | * @param string $ip 27 | * 28 | * @return \GrahamCampbell\CloudFlareAPI\Models\Ip 29 | */ 30 | public function get($ip) 31 | { 32 | return new Ip($this->client, $ip); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/Repositories/ZoneRepository.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GrahamCampbell\CloudFlareAPI\Repositories; 13 | 14 | use GrahamCampbell\CloudFlareAPI\Models\Zone; 15 | use Illuminate\Support\Collection; 16 | 17 | /** 18 | * This is the zone repository class. 19 | * 20 | * @author Graham Campbell 21 | */ 22 | class ZoneRepository extends AbstractRepository 23 | { 24 | /** 25 | * Get a collection of all the zones. 26 | * 27 | * @return \Illuminate\Support\Collection 28 | */ 29 | public function all() 30 | { 31 | $multi = $this->client->zoneLoadMulti(); 32 | 33 | $zones = array_get($multi, 'response.zones.objs'); 34 | 35 | $all = new Collection(); 36 | 37 | foreach ($zones as $zone) { 38 | $name = $zone['zone_name']; 39 | $all->put($name, $this->get($name)); 40 | } 41 | 42 | return $all; 43 | } 44 | 45 | /** 46 | * Get a single zone object. 47 | * 48 | * @param string $zone 49 | * 50 | * @return \GrahamCampbell\CloudFlareAPI\Models\Zone 51 | */ 52 | public function get($zone) 53 | { 54 | return new Zone($this->client, $zone); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/Subscribers/ErrorSubscriber.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GrahamCampbell\CloudFlareAPI\Subscribers; 13 | 14 | use GuzzleHttp\Event\CompleteEvent; 15 | use GuzzleHttp\Event\RequestEvents; 16 | use GuzzleHttp\Event\SubscriberInterface; 17 | use GuzzleHttp\Exception\RequestException; 18 | 19 | /** 20 | * This is the error subscriber class. 21 | * 22 | * @author Graham Campbell 23 | */ 24 | class ErrorSubscriber implements SubscriberInterface 25 | { 26 | /** 27 | * Get the subscriber events. 28 | * 29 | * @return array 30 | */ 31 | public function getEvents() 32 | { 33 | return ['complete' => ['onComplete', RequestEvents::VERIFY_RESPONSE - 50]]; 34 | } 35 | 36 | /** 37 | * Throw a RequestException if the response is not marked as successful. 38 | * 39 | * @param \GuzzleHttp\Event\CompleteEvent $event 40 | * 41 | * @throws \GuzzleHttp\Exception\RequestException 42 | * 43 | * @return void 44 | */ 45 | public function onComplete(CompleteEvent $event) 46 | { 47 | $json = $event->getResponse()->json(); 48 | 49 | if (array_get($json, 'result') !== 'success' || array_key_exists('response', $json) === false) { 50 | throw RequestException::create($event->getRequest(), $event->getResponse()); 51 | } 52 | } 53 | } 54 | --------------------------------------------------------------------------------