| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace CybozuHttp; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use CybozuHttp\Exception\NotExistRequiredException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @author ochi51 <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | class Config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * @var array $config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     private $config = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @var array $default | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     private $default = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         'domain' => "cybozu.com", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         'use_api_token' => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         'use_basic' => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         'use_client_cert' => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         'base_url' => null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         'defaults' => [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         'use_cache' => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         'cache_dir' => null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         'cache_ttl' => 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         'debug' => false | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @var array $required | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     private $required = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         'domain', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         'subdomain', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         'use_api_token', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         'use_basic', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         'use_client_cert', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         'base_url', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         'defaults', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         'use_cache', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         'debug' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     public function __construct(array $config) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $this->config = $config + $this->default; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $this->config['base_url'] = $this->getBaseUrl(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $this->configureDefaults(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * configure default options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     private function configureDefaults() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $this->configureAuth(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $this->configureBasicAuth(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $this->configureCert(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     private function configureAuth() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         if ($this->get('use_api_token')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             $this->config['defaults']['headers']['X-Cybozu-API-Token'] = $this->get('token'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             $this->config['defaults']['headers']['X-Cybozu-Authorization'] = | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 base64_encode($this->get('login') . ':' . $this->get('password')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     private function configureBasicAuth() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         if ($this->get('use_basic')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             $this->config['defaults']['auth'] = $this->getBasicAuthOptions(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     private function configureCert() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         if ($this->get('use_client_cert')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             $this->config['defaults']['verify'] = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             $this->config['defaults']['cert'] = $this->getCertOptions(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             $this->config['defaults']['verify'] = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     private function getBasicAuthOptions() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         if ($this->hasRequiredOnBasicAuth()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                 $this->get('basic_login'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |                 $this->get('basic_password') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         throw new NotExistRequiredException("kintone.empty_basic_password"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     private function getCertOptions() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         if ($this->hasRequiredOnCert()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                 $this->get('cert_file'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                 $this->get('cert_password') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         throw new NotExistRequiredException("kintone.empty_cert"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     public function toArray() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         return $this->config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * @param $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      * @return string|bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     public function get($key) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         if (isset($this->config[$key])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |             return $this->config[$key]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |      * @return bool | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 147 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 148 |  |  |     public function hasRequired() | 
            
                                                                        
                            
            
                                    
            
            
                | 149 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 150 |  |  |         foreach ($this->required as $r) { | 
            
                                                                        
                            
            
                                    
            
            
                | 151 |  |  |             if (!array_key_exists($r, $this->config)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 152 |  |  |                 return false; | 
            
                                                                        
                            
            
                                    
            
            
                | 153 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 154 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 156 |  |  |         return $this->hasRequiredOnAuth() | 
            
                                                                        
                            
            
                                    
            
            
                | 157 |  |  |                 && $this->hasRequiredOnBasicAuth() | 
            
                                                                        
                            
            
                                    
            
            
                | 158 |  |  |                 && $this->hasRequiredOnCert() | 
            
                                                                        
                            
            
                                    
            
            
                | 159 |  |  |                 && $this->hasRequiredOnCache(); | 
            
                                                                        
                            
            
                                    
            
            
                | 160 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |     private function hasRequiredOnAuth() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |         if ($this->get('use_api_token')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |             return !empty($this->get('token')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |         return $this->get('login') && $this->get('password'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |     private function hasRequiredOnBasicAuth() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |         return $this->hasKeysByUse('use_basic', ['basic_login', 'basic_password']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |     private function hasRequiredOnCert() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |         return $this->hasKeysByUse('use_client_cert', ['cert_file', 'cert_password']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |     private function hasRequiredOnCache() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |         return $this->hasKeysByUse('use_cache', ['cache_dir', 'cache_ttl']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |      * @param string $use | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |      * @param string[] $keys | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |     private function hasKeysByUse($use, array $keys) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |         if (!$this->get($use)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |         foreach ($keys as $key) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |             if (is_null($this->get($key)) || $this->get($key) === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |                 return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  |     public function getBaseUrl() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  |         $subdomain = $this->get('subdomain'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  |         $uri = "https://" . $subdomain; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  |         if (strpos($subdomain, '.') === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  |             if ($this->get('use_client_cert')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  |                 $uri .= ".s"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  |             $uri .= "." . $this->get('domain'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  |         return $uri; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 235 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 236 |  |  | } |