1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package Social Login |
5
|
|
|
* @author Iurii Makukh |
6
|
|
|
* @copyright Copyright (c) 2018, Iurii Makukh |
7
|
|
|
* @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+ |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace gplcart\modules\social_login; |
11
|
|
|
|
12
|
|
|
use Exception; |
13
|
|
|
use gplcart\core\Controller; |
14
|
|
|
use gplcart\core\Module; |
15
|
|
|
use InvalidArgumentException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Main class for Social Login module |
19
|
|
|
*/ |
20
|
|
|
class Main |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Module class instance |
25
|
|
|
* @var \gplcart\core\Module $module |
26
|
|
|
*/ |
27
|
|
|
protected $module; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param Module $module |
31
|
|
|
*/ |
32
|
|
|
public function __construct(Module $module) |
33
|
|
|
{ |
34
|
|
|
$this->module = $module; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Implements hook "construct.controller.frontend" |
39
|
|
|
* @param \gplcart\core\controllers\frontend\Controller $controller |
40
|
|
|
*/ |
41
|
|
|
public function hookConstructControllerFrontend($controller) |
42
|
|
|
{ |
43
|
|
|
if (!$controller->isInternalRoute() && !$controller->isLoggedIn()) { |
44
|
|
|
|
45
|
|
|
$controller->setData('print_social_login_buttons', function ($controller) { |
46
|
|
|
return $this->getButtonsSafely($controller); |
47
|
|
|
}); |
48
|
|
|
|
49
|
|
|
if (in_array($controller->path(), array('login', 'register'))) { |
50
|
|
|
$controller->setData('social_login_buttons', $this->getButtonsSafely($controller)); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Implements hook "route.list" |
57
|
|
|
* @param array $routes |
58
|
|
|
*/ |
59
|
|
|
public function hookRouteList(array &$routes) |
60
|
|
|
{ |
61
|
|
|
$routes['admin/module/settings/social_login'] = array( |
62
|
|
|
'access' => 'module_edit', |
63
|
|
|
'handlers' => array( |
64
|
|
|
'controller' => array('gplcart\\modules\\social_login\\controllers\\Settings', 'editSettings') |
65
|
|
|
) |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Implements hook "module.oauth.providers" |
71
|
|
|
* @param array $providers |
72
|
|
|
*/ |
73
|
|
|
public function hookModuleOauthProviders(array &$providers) |
74
|
|
|
{ |
75
|
|
|
$providers = array_merge($providers, $this->getProviders()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Returns an array of rendered buttons keyed by a provider ID |
80
|
|
|
* @param \gplcart\core\controllers\frontend\Controller $controller |
81
|
|
|
* @param array $params |
82
|
|
|
* @return array |
83
|
|
|
* @throws InvalidArgumentException |
84
|
|
|
*/ |
85
|
|
|
public function getButtons($controller, array $params = array()) |
86
|
|
|
{ |
87
|
|
|
if (!$controller instanceof Controller) { |
|
|
|
|
88
|
|
|
throw new InvalidArgumentException('First argument must be instance of \gplcart\core\Controller'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** @var \gplcart\modules\oauth\Main $module */ |
92
|
|
|
$module = $this->module->getInstance('oauth'); |
93
|
|
|
$model = $module->getModel(); |
94
|
|
|
|
95
|
|
|
$buttons = array(); |
96
|
|
|
|
97
|
|
|
foreach ($module->getProviders(array('type' => 'login')) as $id => $provider) { |
98
|
|
|
|
99
|
|
|
if (empty($provider['status']) |
100
|
|
|
|| empty($provider['settings']['client_id']) |
101
|
|
|
|| empty($provider['settings']['client_secret'])) { |
102
|
|
|
continue; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$data = array('provider' => $provider, 'url' => $model->getAuthUrl($provider, $params)); |
106
|
|
|
$buttons[$id] = $controller->render($provider['template']['button'], $data); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $buttons; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Returns an array of rendered buttons or caught exception messages |
114
|
|
|
* @param \gplcart\core\controllers\frontend\Controller $controller |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
|
|
protected function getButtonsSafely($controller) |
118
|
|
|
{ |
119
|
|
|
try { |
120
|
|
|
return $this->getButtons($controller); |
121
|
|
|
} catch (Exception $ex) { |
122
|
|
|
return array($ex->getMessage()); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Returns an array of providers |
128
|
|
|
* @return array |
129
|
|
|
*/ |
130
|
|
|
protected function getProviders() |
131
|
|
|
{ |
132
|
|
|
$providers = array( |
133
|
|
|
'facebook' => array( |
134
|
|
|
'name' => 'Facebook', // @text |
135
|
|
|
'scope' => 'email', |
136
|
|
|
'url' => array( |
137
|
|
|
'auth' => 'https://www.facebook.com/v2.8/dialog/oauth', |
138
|
|
|
'token' => 'https://graph.facebook.com/v2.8/oauth/access_token' |
139
|
|
|
) |
140
|
|
|
), |
141
|
|
|
'google' => array( |
142
|
|
|
'name' => 'Google+', // @text |
143
|
|
|
'scope' => 'email', |
144
|
|
|
'url' => array( |
145
|
|
|
'auth' => 'https://accounts.google.com/o/oauth2/auth', |
146
|
|
|
'token' => 'https://accounts.google.com/o/oauth2/token' |
147
|
|
|
) |
148
|
|
|
) |
149
|
|
|
); |
150
|
|
|
|
151
|
|
|
return $this->prepareProviders($providers); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Prepare an array of providers |
156
|
|
|
* @param array $providers |
157
|
|
|
* @return array |
158
|
|
|
*/ |
159
|
|
|
protected function prepareProviders(array $providers) |
160
|
|
|
{ |
161
|
|
|
$settings = $this->module->getSettings('social_login'); |
162
|
|
|
|
163
|
|
|
foreach ($providers as $provider_id => &$provider) { |
164
|
|
|
$provider += array( |
165
|
|
|
'id' => $provider_id, |
166
|
|
|
'type' => 'login', |
167
|
|
|
'status' => !empty($settings['status'][$provider_id]), |
168
|
|
|
'settings' => array( |
169
|
|
|
'register' => $settings['register'], |
170
|
|
|
'register_login' => $settings['register_login'], |
171
|
|
|
'register_status' => $settings['register_status'], |
172
|
|
|
'client_id' => isset($settings['client_id'][$provider_id]) ? $settings['client_id'][$provider_id] : '', |
173
|
|
|
'client_secret' => isset($settings['client_secret'][$provider_id]) ? $settings['client_secret'][$provider_id] : '', |
174
|
|
|
), |
175
|
|
|
'template' => array('button' => "social_login|buttons/$provider_id"), |
176
|
|
|
'handlers' => array( |
177
|
|
|
'authorize' => array("gplcart\\modules\\social_login\\handlers\\$provider_id", 'authorize'), |
178
|
|
|
) |
179
|
|
|
); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return $providers; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
|
186
|
|
|
} |
187
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.