Overview

Namespaces

  • ApiAxle
    • Api
    • Shared
  • ApiAxleTest
    • Api
    • Shared
  • PHP

Classes

  • Config
  • HttpRequest
  • ItemList
  • Utilities

Exceptions

  • ApiException
  • Overview
  • Namespace
  • Class
  • Tree
  • Todo
  1: <?php
  2: /**
  3:  * ApiAxle (https://github.com/fillup/apiaxle-module/)
  4:  *
  5:  * @link      https://github.com/fillup/apiaxle-module for the canonical source repository
  6:  * @license   MIT
  7:  */
  8: namespace ApiAxle\Shared;
  9: 
 10: /**
 11:  * ApiAxle\Config class encapsulates common configuration settings for calling
 12:  * ApiAxle apis
 13:  * 
 14:  * @author Phillip Shipley <phillip@phillipshipley.com>
 15:  */
 16: class Config
 17: {
 18:     /**
 19:      * ApiAxle endpoing url
 20:      * 
 21:      * @var string endpoint
 22:      */
 23:     protected $endpoint;
 24:     
 25:     /**
 26:      * ApiAxle API key
 27:      * 
 28:      * @var string key
 29:      */
 30:     protected $key;
 31:     
 32:     /**
 33:      * ApiAxle API shared secret for signing requests
 34:      * 
 35:      * @var string secret
 36:      */
 37:     protected $secret;
 38:     
 39:     /**
 40:      * Enable/Disable verification of peer certificate when calling API
 41:      * 
 42:      * @var bool $ssl_verifypeer
 43:      */
 44:     protected $ssl_verifypeer = true;
 45:     
 46:     /**
 47:      * Additional config for cURL providing alternative CA certificate info.
 48:      * The name of a file holding one or more certificates to verify the peer with.
 49:      * Requires absolute path.
 50:      * Use this option alongside CURLOPT_SSL_VERIFYPEER.
 51:      * 
 52:      * @link http://us2.php.net/manual/en/function.curl-setopt.php cURL configuration options
 53:      * @var string $ssl_cainfo
 54:      */
 55:     protected $ssl_cainfo = null;
 56:     
 57:     /**
 58:      * Additional config for cURL providing alternative CA certificate path.
 59:      * A directory that holds multiple CA certificates.
 60:      * Use this option alongside CURLOPT_SSL_VERIFYPEER.
 61:      * 
 62:      * @link http://us2.php.net/manual/en/function.curl-setopt.php cURL configuration options
 63:      * @var string $ssl_capath
 64:      */
 65:     protected $ssl_capath = null;
 66:             
 67:     /**
 68:      * Enable/disable usage of a proxy server
 69:      * 
 70:      * @var bool $proxy_enable
 71:      */
 72:     protected $proxy_enable = false;
 73:     
 74:     /**
 75:      * Set proxy server hostname
 76:      * 
 77:      * @var string $proxy_host
 78:      */
 79:     protected $proxy_host;
 80:     
 81:     /**
 82:      * Set proxy server port
 83:      * 
 84:      * @var string $proxy_port
 85:      */
 86:     protected $proxy_port;
 87:     
 88:     /**
 89:      * Initialization status
 90:      */
 91:     private $isInitialized = false;
 92:     
 93:     /**
 94:      * Construct config object
 95:      * 
 96:      * If config is provided, parse it and setup object
 97:      * 
 98:      * @param array $config
 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:      * Setup configuration
125:      * 
126:      * @param array $config
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:      * Return configuration settings as array
143:      * 
144:      * @return array
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:      * Load configuration from default config file unless alternate file is specified
157:      * 
158:      * @param string $file
159:      * @return boolean
160:      * @throws Exception
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:      * Generate an API signature based on key and shared secret. 
278:      * 
279:      * Uses hmac_sha1 to generate hash. There is a six second window where the
280:      * hash is valid and ApiAxle will check the hash for three seconds before
281:      * and after the call to validate it. Be sure you are using a network time
282:      * server to keep your servers in sync with correct time
283:      * 
284:      * Returns false if a sharedSecret is not set for the key
285:      * 
286:      * @return boolean
287:      * @return string
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: }
fillup/apiaxle API documentation generated by ApiGen 2.8.0