1: <?php
2: 3: 4: 5: 6: 7:
8: namespace ApiAxle\Shared;
9:
10: 11: 12: 13: 14: 15:
16: class Config
17: {
18: 19: 20: 21: 22:
23: protected $endpoint;
24:
25: 26: 27: 28: 29:
30: protected $key;
31:
32: 33: 34: 35: 36:
37: protected $secret;
38:
39: 40: 41: 42: 43:
44: protected $ssl_verifypeer = true;
45:
46: 47: 48: 49: 50: 51: 52: 53: 54:
55: protected $ssl_cainfo = null;
56:
57: 58: 59: 60: 61: 62: 63: 64:
65: protected $ssl_capath = null;
66:
67: 68: 69: 70: 71:
72: protected $proxy_enable = false;
73:
74: 75: 76: 77: 78:
79: protected $proxy_host;
80:
81: 82: 83: 84: 85:
86: protected $proxy_port;
87:
88: 89: 90:
91: private $isInitialized = false;
92:
93: 94: 95: 96: 97: 98: 99:
100: public function __construct($config = array())
101: {
102: if($config && is_array($config) && count($config) > 0){
103: $this->setConfg($config);
104: } elseif($config instanceof \ApiAxle\Shared\Config){
105: $this->setEndpoint($config->getEndpoint());
106: $this->setKey($config->getKey());
107: $this->setSecret($config->getSecret());
108: $this->setSslVerifypeer($config->getSslVerifypeer());
109: $this->setSslCainfo($config->getSslCainfo());
110: $this->setSslCapath($config->getSslCapath());
111: $this->setProxyEnable($config->getProxyEnable());
112: $this->setProxyHost($config->getProxyHost());
113: $this->setProxyPort($config->getProxyPort());
114: } else {
115: $this->loadConfigFile();
116: }
117:
118: if(!is_null($this->endpoint) && !is_null($this->key) && !is_null($this->secret)){
119: $this->isInitialized = true;
120: }
121: }
122:
123: 124: 125: 126: 127:
128: public function setConfg($config)
129: {
130: $this->setEndpoint(isset($config['endpoint']) ? $config['endpoint'] : false);
131: $this->setKey(isset($config['key']) ? $config['key'] : false);
132: $this->setSecret(isset($config['secret']) ? $config['secret'] : false);
133: $this->setSslVerifypeer(isset($config['ssl_verifypeer']) ? $config['ssl_verifypeer'] : true);
134: $this->setSslCainfo(isset($config['ssl_cainfo']) ? $config['ssl_cainfo'] : true);
135: $this->setSslCapath(isset($config['ssl_capath']) ? $config['ssl_capath'] : true);
136: $this->setProxyEnable(isset($config['proxy_enable']) ? $config['proxy_enable'] : false);
137: $this->setProxyHost(isset($config['proxy_host']) ? $config['proxy_host'] : null);
138: $this->setProxyPort(isset($config['proxy_port']) ? $config['proxy_port'] : null);
139: }
140:
141: 142: 143: 144: 145:
146: public function getConfig()
147: {
148: return array(
149: 'endpoint' => $this->endpoint,
150: 'key' => $this->key,
151: 'secret' => $this->secret
152: );
153: }
154:
155: 156: 157: 158: 159: 160: 161:
162: public function loadConfigFile($file=false)
163: {
164: $file = $file ?: __DIR__.'/../../../config/config.local.php';
165: if(file_exists($file)){
166: $config = include $file;
167: if(is_array($config) && count($config) > 0){
168: $this->setConfg($config);
169: return true;
170: }
171: }
172: throw new \Exception('Unable to load configuration from file '.$file, '100');
173: }
174:
175: public function getEndpoint()
176: {
177: return $this->endpoint;
178: }
179:
180: public function setEndpoint($endpoint)
181: {
182: $url = filter_var($endpoint, FILTER_VALIDATE_URL);
183: if($url){
184: if(preg_match('/^http[s]{0,1}:\/\//', $endpoint)){
185: $this->endpoint = $endpoint;
186: } else {
187: throw new \Exception('Endpoint must start with http:// or https://',101);
188: }
189: } else {
190: throw new \Exception('Invalid URL specified for Endpoint.',102);
191: }
192:
193: return $this;
194: }
195:
196: public function getKey()
197: {
198: return $this->key;
199: }
200:
201: public function setKey($key)
202: {
203: $this->key = $key;
204: }
205:
206: public function getSecret()
207: {
208: return $this->secret;
209: }
210:
211: public function setSecret($secret)
212: {
213: $this->secret = $secret;
214: }
215:
216: public function getSslVerifypeer()
217: {
218: return $this->ssl_verifypeer;
219: }
220:
221: public function setSslVerifypeer($ssl_verifypeer)
222: {
223: $this->ssl_verifypeer = $ssl_verifypeer;
224: }
225:
226: public function getSslCainfo()
227: {
228: return $this->ssl_cainfo;
229: }
230:
231: public function setSslCainfo($ssl_cainfo)
232: {
233: $this->ssl_cainfo = $ssl_cainfo;
234: }
235:
236: public function getSslCapath()
237: {
238: return $this->ssl_capath;
239: }
240:
241: public function setSslCapath($ssl_capath)
242: {
243: $this->ssl_capath = $ssl_capath;
244: }
245:
246: public function getProxyEnable()
247: {
248: return $this->proxy_enable;
249: }
250:
251: public function setProxyEnable($proxy_enable)
252: {
253: $this->proxy_enable = $proxy_enable;
254: }
255:
256: public function getProxyHost()
257: {
258: return $this->proxy_host;
259: }
260:
261: public function setProxyHost($proxy_host)
262: {
263: $this->proxy_host = $proxy_host;
264: }
265:
266: public function getProxyPort()
267: {
268: return $this->proxy_port;
269: }
270:
271: public function setProxyPort($proxy_port)
272: {
273: $this->proxy_port = $proxy_port;
274: }
275:
276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288:
289: public function getSignature()
290: {
291: if(!is_null($this->secret)){
292: $api_sig = hash_hmac('sha1', time().$this->getKey(), $this->getSecret());
293: return $api_sig;
294: } else {
295: return false;
296: }
297: }
298:
299: }