| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * @author Chris Hilsdon <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Cloudflare; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Symfony\Component\Yaml\Yaml; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use GuzzleHttp\Client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class Cloudflare | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     protected $Endpoint = "https://api.cloudflare.com/client/v4/"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     protected $APIKey; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     protected $Email; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     protected $MakeRequests = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     protected $Guzzle; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     public function setGuzzle($Guzzle) | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |         $this->Guzzle = $Guzzle; | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     public function getEndpoint() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         return $this->Endpoint; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     public function getAPIKey() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         return $this->APIKey; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     public function getEmail() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         return $this->Email; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     public function getMakeRequests() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         return $this->MakeRequests; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     public function getGuzzle() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         //If Guzzle has not been setup yet | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         if($this->Guzzle === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             var_dump('Guzzle was created'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             $Guzzle = new Client(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             $this->setGuzzle($Guzzle); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         return $this->Guzzle; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     public function diableRequests() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $this->MakeRequests = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     public function enableRequests() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $this->MakeRequests = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     public function __construct($options = null, $Guzzle = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         if (is_null($options) || !is_array($options)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             $this->loadConfig(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             $this->APIKey = $options['APIKey']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             $this->Email = $options['Email']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         if (!is_null($Guzzle)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             $this->setGuzzle($Guzzle); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     private function loadConfig() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $config = 'Cloudflare.yml'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $configDist = $config.'.dist'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $dir = $_SERVER['DOCUMENT_ROOT']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         if (@file_exists($dir . $config)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             $load = $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         } elseif (@file_exists($dir . $configDist)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             $load = $configDist; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             die('Unable to load either "'.$config.'" or "'.$configDist.'"'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $config = Yaml::parse(file_get_contents($load)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $this->APIKey = $config['APIKey']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         $this->Email = $config['Email']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 99 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 100 |  |  |  |