|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Socializer plugin for Craft CMS 3.x |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://enupal.com/ |
|
6
|
|
|
* @copyright Copyright (c) 2019 Enupal LLC |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace enupal\socializer\services; |
|
10
|
|
|
|
|
11
|
|
|
use Craft; |
|
12
|
|
|
use craft\db\Query; |
|
13
|
|
|
use craft\helpers\UrlHelper; |
|
14
|
|
|
use yii\base\Component; |
|
15
|
|
|
use enupal\socializer\models\Settings as SettingsModel; |
|
16
|
|
|
use enupal\socializer\Socializer; |
|
17
|
|
|
use craft\helpers\App; |
|
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
class Settings extends Component |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Saves Settings |
|
23
|
|
|
* |
|
24
|
|
|
* @param $scenario |
|
25
|
|
|
* @param $settings SettingsModel |
|
26
|
|
|
* |
|
27
|
|
|
* @return bool |
|
28
|
|
|
*/ |
|
29
|
|
|
public function saveSettings(SettingsModel $settings, $scenario = null): bool |
|
30
|
|
|
{ |
|
31
|
|
|
$plugin = $this->getPlugin(); |
|
32
|
|
|
|
|
33
|
|
|
if (!is_null($scenario)) { |
|
34
|
|
|
$settings->setScenario($scenario); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
// Validate them, now that it's a model |
|
38
|
|
|
if ($settings->validate() === false) { |
|
39
|
|
|
return false; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$success = Craft::$app->getPlugins()->savePluginSettings($plugin, $settings->getAttributes()); |
|
43
|
|
|
|
|
44
|
|
|
return $success; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/*** |
|
48
|
|
|
* @return string |
|
49
|
|
|
* @throws \craft\errors\SiteNotFoundException |
|
50
|
|
|
*/ |
|
51
|
|
|
public function getPrimarySiteUrl() |
|
52
|
|
|
{ |
|
53
|
|
|
return UrlHelper::baseSiteUrl(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getCallbackUrl() |
|
60
|
|
|
{ |
|
61
|
|
|
$baseUrl = $this->getPrimarySiteUrl(); |
|
62
|
|
|
$settings = $this->getSettings(); |
|
63
|
|
|
if (!empty($settings->siteUrl)) { |
|
64
|
|
|
$settingsBaseUrl = rtrim(trim(App::parseEnv($settings->siteUrl), "/")); |
|
|
|
|
|
|
65
|
|
|
if (UrlHelper::isProtocolRelativeUrl($settingsBaseUrl)) { |
|
66
|
|
|
$baseUrl = $settingsBaseUrl; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return $baseUrl."/socializer/login/callback"; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @return SettingsModel |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getSettings() |
|
77
|
|
|
{ |
|
78
|
|
|
/** @var SettingsModel $settings */ |
|
79
|
|
|
$settings = $this->getPlugin()->getSettings(); |
|
80
|
|
|
|
|
81
|
|
|
return $settings; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return \craft\base\PluginInterface|null |
|
86
|
|
|
*/ |
|
87
|
|
|
public function getPlugin() |
|
88
|
|
|
{ |
|
89
|
|
|
return Craft::$app->getPlugins()->getPlugin('enupal-socializer'); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return string|null |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getPluginUid() |
|
96
|
|
|
{ |
|
97
|
|
|
$plugin = (new Query()) |
|
98
|
|
|
->select(['uid']) |
|
99
|
|
|
->from('{{%plugins}}') |
|
100
|
|
|
->where(["handle" => 'enupal-socializer']) |
|
101
|
|
|
->one(); |
|
102
|
|
|
|
|
103
|
|
|
return $plugin['uid'] ?? null; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @return array |
|
108
|
|
|
*/ |
|
109
|
|
|
public function getGlobalFieldMapping() |
|
110
|
|
|
{ |
|
111
|
|
|
$settings = $this->getSettings(); |
|
112
|
|
|
|
|
113
|
|
|
return $settings->fieldMapping ?? Socializer::$app->providers->getDefaultFieldMapping(); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @return array|\craft\models\UserGroup[] |
|
118
|
|
|
*/ |
|
119
|
|
|
public function getUserGroups() |
|
120
|
|
|
{ |
|
121
|
|
|
$userGroups = [ |
|
122
|
|
|
["name" => "None", "id" => ""] |
|
123
|
|
|
]; |
|
124
|
|
|
$craftUserGroups = Craft::$app->getUserGroups()->getAllGroups(); |
|
125
|
|
|
|
|
126
|
|
|
$userGroups = array_merge($userGroups, $craftUserGroups); |
|
127
|
|
|
|
|
128
|
|
|
return $userGroups; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @return array|null |
|
133
|
|
|
*/ |
|
134
|
|
|
public function getConfigSettings() |
|
135
|
|
|
{ |
|
136
|
|
|
return Craft::$app->config->getGeneral()->socializer ?? null; |
|
|
|
|
|
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
public function validateAppleSettings() |
|
140
|
|
|
{ |
|
141
|
|
|
$config = $this->getConfigSettings(); |
|
142
|
|
|
|
|
143
|
|
|
if (!isset($config['apple'])) { |
|
144
|
|
|
Craft::error('Apple config is not set', __METHOD__); |
|
145
|
|
|
return false; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
$apple = $config['apple']; |
|
149
|
|
|
|
|
150
|
|
|
if (!isset($apple['keys']['id']) || !isset($apple['keys']['team_id']) || |
|
151
|
|
|
!isset($apple['keys']['key_id']) || |
|
152
|
|
|
!isset($apple['keys']['key_file']) || !isset($apple['scope']) || |
|
153
|
|
|
!isset($apple['verifyTokenSignature'])) { |
|
154
|
|
|
Craft::error('Missing a required Apple config, please check our docs.', __METHOD__); |
|
155
|
|
|
return false; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
if (!file_exists($apple['keys']['key_file'])) { |
|
159
|
|
|
Craft::error('Unable to find Apple key file: '.$apple['keys']['key_file'], __METHOD__); |
|
160
|
|
|
return false; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
return true; |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: