1: <?php
2: namespace Genetsis\core;
3:
4: use DOMDocument;
5: use Exception;
6: use Genetsis\Config;
7:
8: /**
9: * Manages OAuth configuration file.
10: *
11: * @package Genetsis
12: * @category Bean
13: * @version 1.0
14: * @access private
15: * @todo Review source code.
16: */
17: class OAuthConfig
18: {
19: /** @var array Where the settings are saved. */
20: private static $config = array();
21:
22: public static function init()
23: {
24: self::$config = FileCache::get('config');
25: if (!self::$config) {
26: self::loadXml(dirname(__FILE__) . '/' . Config::configPath());
27: }
28: }
29:
30: /**
31: * Loads a XML from file.
32: *
33: * @param string Full path to file.
34: * @return void
35: * @throws \Exception If there is an error: file doesn't exists, not well-formed, ...
36: */
37: private static function loadXml($file)
38: {
39: if ((($file = trim((string)$file)) == '') || !file_exists($file) || !is_file($file) || !is_readable($file)) {
40: throw new Exception('The config file is not found or can“t be readable' . $file);
41: }
42:
43: $xmlObj = new DOMDocument();
44: $xmlObj->load($file);
45:
46: try {
47:
48: foreach ($xmlObj->getElementsByTagName("oauth-config")->item(0)->attributes as $attrName => $attrNode) {
49: if (!$version = $attrNode->value) {
50: throw new Exception('Not version');
51: }
52: }
53:
54: if ($version !== Config::CONF_VERSION) {
55: throw new Exception('Not correct version');
56: }
57:
58: if (!$credentials = $xmlObj->getElementsByTagName("credentials")->item(0)) {
59: throw new Exception('Not credentials');
60: }
61:
62: foreach ($credentials->childNodes as $node) {
63: if ($node->nodeName === 'clientid') {
64: self::$config['clientid'] = $node->nodeValue;
65: }
66: if ($node->nodeName === 'clientsecret') {
67: self::$config['clientsecret'] = $node->nodeValue;
68: }
69: }
70: if ((!isset(self::$config['clientid'])) || (!isset(self::$config['clientsecret']))) {
71: throw new Exception('Not defined credentials');
72: }
73:
74: self::getParserXML($xmlObj, 'redirections', 'type');
75:
76: self::getParserXML($xmlObj, 'endpoints', 'id');
77: // Parse urls from apis
78: $apis = $xmlObj->getElementsByTagName("apis")->item(0);
79: foreach ($apis->childNodes as $singleApi) {
80: if ($singleApi->nodeType == XML_ELEMENT_NODE) {
81: $apiName = trim($singleApi->getAttribute('name'));
82:
83: if ($apiName == 'api.activityid' || $apiName == 'api.user') {
84: self::$config['apis'][$apiName]['base_url'] = trim($singleApi->getAttribute('base-url'));
85: }
86:
87: foreach ($singleApi->childNodes as $urlNode) {
88: if ($urlNode->nodeType == XML_ELEMENT_NODE) {
89:
90: self::$config['apis'][$apiName][$urlNode->getAttribute('id')] = $urlNode->nodeValue;
91: }
92: }
93: }
94: }
95: self::getParserXML($xmlObj, 'sections', 'id');
96: FileCache::set('config', self::$config, 600);
97: } catch (Exception $e) {
98: throw new Exception('The config file is not valid - ' . $e->getMessage());
99: }
100: }
101:
102: /**
103: * Parse a XML section and loads it on "self::$config" variable.
104: *
105: * @param $xmlObj DOMDocument Object with XML loaded where search will performed.
106: * @param $section string The section to be parsed. Example of sections: apis, enpoints, redirections, ... It depends on XML.
107: * @param $typeId string The type of identifier. Example of identifiers: id, type, ... It depends on XML.
108: * @throws \Exception If the sections is not defined
109: * @return void
110: */
111: private static function getParserXML($xmlObj, $section, $typeId)
112: {
113: if (!$sections = $xmlObj->getElementsByTagName($section)->item(0)) {
114: throw new Exception('Not ' . $section . ' Defined');
115: }
116:
117: foreach ($sections->childNodes as $node) {
118:
119: if ($node->hasAttributes()) {
120: $default = $node->getAttribute('default');
121: $id = $node->getAttribute($typeId);
122: $value = $node->nodeValue;
123:
124: /* Now redirections can to have more than one url for each type.
125: * Check if a value is previously assigned and if it is so then
126: * a structure data (array) is created for this resource.
127: * $redirectionsData = array(
128: * // A list of all url of callbacks. Key is the value of url for callback and
129: * // value is an array with all attribues for this redirection
130: * 'callbacks_list' => array(),
131: * // Is default callback
132: * 'default_url' => '',
133: * // A url for callback can to be forced by user.
134: * 'forced_url' => '',
135: * );
136: */
137: if ($section == 'redirections') {
138:
139: // Set to callback list
140: self::$config[$section][$id]['callbacks_list'][$value] = array(
141: 'default' => $default
142: );
143:
144: // Check if this url is a default url
145: if ($default) {
146: self::$config[$section][$id]['default_url'] = $value;
147: }
148: // Initialize forced url
149: self::$config[$section][$id]['forced_url'] = '';
150: } // Sections
151: else {
152: if ($section == 'sections') {
153:
154: $promotionId = $node->getAttribute('promotionId');
155:
156: $prizes = null;
157: foreach ($node->childNodes as $prizesDOM) {
158: if ($prizesDOM->nodeType == XML_ELEMENT_NODE) {
159: foreach ($prizesDOM->childNodes as $prizeNode) {
160: if ($prizeNode->nodeType == XML_ELEMENT_NODE) {
161: $prizes[$prizeNode->getAttribute('id')] = $prizeNode->getAttribute('id');
162: }
163: }
164: }
165: }
166: // Sections has an structure of attributes
167: self::$config[$section][$id] = array(
168: 'value' => $id,
169: 'default' => $default,
170: 'promotionId' => $promotionId,
171: 'prizes' => $prizes
172: );
173: } else {
174: // Standar insert
175: self::$config[$section][$id] = $value;
176: }
177: }
178: }
179: }
180: }
181:
182: /**
183: * Returns web client identifier.
184: *
185: * @return string Web client identifier. It could be empty.
186: */
187: public static function getClientId()
188: {
189: return (isset(self::$config['clientid']) ? self::$config['clientid'] : false);
190: }
191:
192: /**
193: * Returns web client secret.
194: *
195: * @return string Web client secret. It could be empty.
196: */
197: public static function getClientSecret()
198: {
199: return (isset(self::$config['clientsecret']) ? self::$config['clientsecret'] : false);
200: }
201:
202: /**
203: * Returns an URL to redirect user.
204: * Redirects can to have more than a url associate to a type.
205: * Value in first position is the default value.
206: *
207: * @param string $type Identifier to select a URL type.
208: * @param string $urlCallback Url for callback. This url must to be defined in 'oauthconf.xml'
209: * @return string The URL selected. It could be empty if not exists
210: * that type or if $urlCallback is not defined in 'oauthconf.xml'.
211: */
212: public static function getRedirectUrl($type, $urlCallback = null)
213: {
214: $type = trim((string)$type);
215:
216: // Check if is defined
217: if (isset(self::$config['redirections'][$type])) {
218: // Check if value is a simple string (url).
219: // Now redirections are the only defined by an array but other are strings
220: if (is_string(self::$config['redirections'][$type])) {
221: // Return url
222: return self::$config['redirections'][$type];
223: } // Value is a associative array
224: else {
225: // If $urlCallback is defined then check if exist and return
226: if ($urlCallback !== null) {
227:
228: // Check if url is defined in array.
229: if (isset(self::$config['redirections'][$type]['callbacks_list'][$urlCallback])) {
230: return $urlCallback;
231: } // Callback url is not define. Url is not valid.
232: else {
233: return false;
234: }
235: } // Check if default url exist
236: else {
237: if (self::$config['redirections'][$type]['default_url']) {
238: // Return default url
239: return self::$config['redirections'][$type]['default_url'];
240: } // Default url is not exist.
241: else {
242: // Return false
243: return false;
244: }
245: }
246: }
247: }
248:
249: // Url for this type is not defined
250: return false;
251: }
252:
253: /**
254: * Returns an endpoint to interact with Genetsis ID servers.
255: *
256: * @param $type string Identifier to select an URL.
257: * @return string The URL selected. It could be empty if not exists that type.
258: */
259: public static function getEndpointUrl($type)
260: {
261: $type = trim((string)$type);
262: return (isset(self::$config['endpoints'][$type]) ? self::$config['endpoints'][$type] : false);
263: }
264:
265: /**
266: * Returns an endpoint to interact with API-Query.
267: *
268: * @param $type Identifier to select an URL.
269: * @param null $verb
270: * @return bool
271: */
272: public static function getApiUrl($type, $verb = null)
273: {
274: $type = trim((string)$type);
275: $verb = trim((string)$verb);
276: return (isset(self::$config['apis'][$type][$verb]) ? self::$config['apis'][$type][$verb] : false);
277: }
278:
279: /**
280: * Returns a section.
281: *
282: * @param string $type Identifier to select a section.
283: * @return string The section selected. It could be empty if not exists that type.
284: */
285: public static function getSection($type)
286: {
287: $type = trim((string)$type);
288: return (isset(self::$config['sections'][$type]) ? self::$config['sections'][$type] : false);
289: }
290:
291: /**
292: * Return default section or false if no exist a default.
293: *
294: * @return mixed default section or false
295: */
296: public static function getDefaultSection()
297: {
298: // Looking for default section
299: foreach (self::$config['sections'] as $item) {
300: // If default is true return value
301: if ($item['default']) {
302: return $item['value'];
303: }
304: }
305: return false;
306: }
307:
308: /**
309: * Return a prize for the section in activityID if exist
310: *
311: * @param $section Section identifier
312: * @param $prize Prize identifier in ActivityID
313: * @return mixed int prize ID in activityId, null if prize not exist
314: */
315: public static function getPrizeSection($section, $prize) {
316:
317: if (is_array(self::getSection($section)['prizes'])) {
318: if (isset(self::getSection($section)['prizes'][$prize])) {
319: return self::getSection($section)['prizes'][$prize];
320: }
321: }
322: return null;
323: }
324:
325: /**
326: * Return Api Opinator URL
327: *
328: * @return string
329: */
330: public static function getApiOpinatorUrl()
331: {
332: //return 'https:' . self::getHost() . '/api-rest/opinator';
333: return 'http://www.sandbox.cocacola.es/api-rest/opinator';
334: }
335:
336: /**
337: * Returns hosts for GID environement
338: *
339: * @param
340: * string Identifier to select an URL.
341: * @return string The URL selected. It could be empty if not exists
342: * that type.
343: */
344: public static function getHost()
345: {
346: return (isset(self::$config['host']) ? self::$config['host'] : false);
347: }
348:
349: }