|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* OpauthResponseHelper |
|
5
|
|
|
* Utility class for handling responses from Opauth. |
|
6
|
|
|
* Used in parsing. Can and should be referenced in _config.yml for helping |
|
7
|
|
|
* parse things for member_mapping. |
|
8
|
|
|
* @author Will Morgan <@willmorgan> |
|
9
|
|
|
* @copyright Copyright (c) 2013, Better Brief LLP |
|
10
|
|
|
*/ |
|
11
|
|
|
class OpauthResponseHelper { |
|
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Take the first part of the name |
|
15
|
|
|
* @return string |
|
16
|
|
|
*/ |
|
17
|
|
|
public static function get_first_name($source) { |
|
18
|
|
|
$name = explode(' ', self::parse_source_path('info.name', $source)); |
|
19
|
|
|
return array_shift($name); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Take all but the first part of the name |
|
24
|
|
|
* @return string |
|
25
|
|
|
*/ |
|
26
|
|
|
public static function get_last_name($source) { |
|
27
|
|
|
$name = explode(' ', self::parse_source_path('info.name', $source)); |
|
28
|
|
|
array_shift($name); |
|
29
|
|
|
return join(' ', $name); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Twitter responds with just a language (also a TZ, but unused for now) |
|
34
|
|
|
* If the PECL Locale extension is used it may be possible to combine both |
|
35
|
|
|
* the TZ and the language to fine tune a user's location, but a bit OTT. |
|
36
|
|
|
* @return string |
|
37
|
|
|
*/ |
|
38
|
|
|
public static function get_twitter_locale($source) { |
|
39
|
|
|
$language = self::parse_source_path('raw.lang', $source); |
|
40
|
|
|
return self::get_smart_locale($language); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Google responds near perfectly for locales, if populated. |
|
45
|
|
|
* Fallback otherwise. |
|
46
|
|
|
* @return string |
|
47
|
|
|
*/ |
|
48
|
|
|
public static function get_google_locale($source) { |
|
49
|
|
|
$locale = self::parse_source_path('raw.locale', $source); |
|
50
|
|
|
if(!$locale) { |
|
|
|
|
|
|
51
|
|
|
return self::get_smart_locale(); |
|
52
|
|
|
} |
|
53
|
|
|
return str_replace('-', '_', $locale); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Try very hard to get a locale for this user. Helps for i18n etc. |
|
58
|
|
|
* @return string |
|
59
|
|
|
*/ |
|
60
|
|
|
public static function get_smart_locale($language = null) { |
|
61
|
|
|
|
|
62
|
|
|
require_once FRAMEWORK_PATH . '/thirdparty/Zend/Locale.php'; |
|
63
|
|
|
$locale = Zend_Locale::getBrowser(); |
|
64
|
|
|
|
|
65
|
|
|
if(!$locale) { |
|
66
|
|
|
if($language) { |
|
67
|
|
|
return i18n::get_locale_from_lang($language); |
|
68
|
|
|
} |
|
69
|
|
|
else { |
|
70
|
|
|
return i18n::get_locale(); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$locale = array_keys($locale); |
|
75
|
|
|
$firstPref = array_shift($locale); |
|
76
|
|
|
|
|
77
|
|
|
if(strpos($firstPref, '_') === false) { |
|
78
|
|
|
return i18n::get_locale_from_lang($language); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $firstPref; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Dot notation parser. Looks for an index or fails gracefully if not found. |
|
86
|
|
|
* @param string $path The path, dot notated. |
|
87
|
|
|
* @param array $source The source in which to search. |
|
88
|
|
|
* @return string|null |
|
89
|
|
|
*/ |
|
90
|
|
|
public static function parse_source_path($path, $source) { |
|
91
|
|
|
$fragments = explode('.', $path); |
|
92
|
|
|
$currentFrame = $source; |
|
93
|
|
|
foreach($fragments as $fragment) { |
|
94
|
|
|
if(!isset($currentFrame[$fragment])) { |
|
95
|
|
|
return null; |
|
96
|
|
|
} |
|
97
|
|
|
$currentFrame = $currentFrame[$fragment]; |
|
98
|
|
|
} |
|
99
|
|
|
return $currentFrame; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.