This repository was archived by the owner on Apr 28, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathconfig-plugin.php
More file actions
181 lines (148 loc) · 6.08 KB
/
config-plugin.php
File metadata and controls
181 lines (148 loc) · 6.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
namespace UserFrosting\OAuth;
require_once('controllers/OAuthController.php');
require_once('models/OAuthUser.php');
require_once('models/OAuthUserLoader.php');
// TODO: Let Composer autoload all our classes
use UserFrosting as UF;
function echobr($par_str) {
echo("<br>$par_str<br>");
error_log("$par_str \n");
}
function echoarr($par_arr, $par_comment = 'none') {
if ($par_comment != 'none')
echobr($par_comment);
echo "<pre>";
print_r($par_arr);
echo "</pre>";
error_log("<pre>$par_comment \n" .
print_r($par_arr, true) . " \n\n </pre>");
}
function logarr($par_arr, $par_comment = 'none') {
error_log("<pre>$par_comment \n" .
print_r($par_arr, true) . " \n\n </pre>");
}
// Fetch the relevant controller
function getProviderController($provider, $callback_page, $app) {
switch ($provider) {
case "linkedin" :
require_once('controllers/OAuthControllerLinkedIn.php');
return new OAuthControllerLinkedIn($app, $callback_page);
break;
case "facebook" :
require_once('controllers/OAuthControllerFacebook.php');
return new OAuthControllerFacebook($app, $callback_page);
break;
default:
// return false;
$app->notFound();
break;
}
}
/* Import UserFrosting variables as global Twig variables */
$twig = $app->view()->getEnvironment();
$twig->addFilter(new \Twig_SimpleFilter('cast_to_array', function ($stdClassObject) {
$response = array();
foreach ((array) $stdClassObject as $key => $value) {
$response[str_replace('*', '', $key)] = $value;
}
return $response;
}));
$loader = $twig->getLoader();
// First look in user's theme...
$loader->addPath($app->config('plugins.path') . "/UserFrosting-OAuth/templates");
$table_user_oauth = new UF\DatabaseTable($app->config('db')['db_prefix'] . "user_oauth", [
"provider",
"user_id",
"uid",
"email",
"first_name",
"last_name",
"picture_url",
"oauth_details",
"created_at"]);
UF\Database::setSchemaTable("user_oauth", $table_user_oauth);
// Define routes
// This is the GET route for the "login with ___" button
$app->get('/oauth/:provider/login', function ($provider) use ($app) {
$controller = getProviderController($provider, 'login', $app);
// Store this action so we remember what we're doing after we get the authorization code
$_SESSION['oauth_action'] = "login";
$get = $app->request->get();
// If we received an authorization code, then resume our action
if (isset($get['code'])) {
// If we're logging them in, just call that method and it will automatically redirect us
$controller->login();
} else {
// Otherwise, request an authorization code
return $controller->authorize();
}
});
// This is the GET route for the "register with ___" button
$app->get('/oauth/:provider/register', function ($provider) use ($app) {
$controller = getProviderController($provider, 'register', $app);
// Store this action so we remember what we're doing after we get the authorization code
$_SESSION['oauth_action'] = "register";
$get = $app->request->get();
// If we received an authorization code, then resume our action
if (isset($get['code'])) {
// If OAuth call is successful and we have a code then
// show the updated registration page
$controller->pageRegister();
} else {
// Otherwise, request an authorization code
return $controller->authorize();
}
});
// This is the POST route that actually registers the user
$app->post('/oauth/:provider/register', function ($provider) use ($app) {
$controller = getProviderController($provider, 'register', $app);
// $controller = $_SESSION["userfrosting"]['oauth_controller'];
// complete the registration process
$controller->register();
});
// This is the GET route for the "settings with ___" button
$app->get('/oauth/:provider/settings', function ($provider) use ($app) {
$controller = getProviderController($provider, 'settings', $app);
// Store this action so we remember what we're doing after we get the authorization code
$_SESSION['oauth_action'] = "settings";
$get = $app->request->get();
// If we received an authorization code, then resume our action
if (isset($get['code'])) {
// If OAuth call is successful and we have a code then
// show the OAuth confirmation
$controller->pageConfirmOAuth();
} else {
// Otherwise, request an authorization code
return $controller->authorize();
}
});
// This is the POST route for the "settings/action with ___" button
//http://userfrosting.github/oauth/linkedin/settings/confirm
$app->post('/oauth/:provider/settings/:action', function ($provider, $action) use ($app) {
error_log("Line 150 $provider ");
$controller = getProviderController($provider, 'settings', $app);
// Store this action so we remember what we're doing after we get the authorization code
$_SESSION['oauth_action'] = "settings-$action";
// execute the action using the controller
$controller->doOAuthAction($action);
});
// This is the GET route for the "settings/action with ___" button
$app->get('/oauth/:provider/settings/:action', function ($provider, $action) use ($app) {
$controller = getProviderController($provider, 'settings', $app);
// Store this action so we remember what we're doing after we get the authorization code
$_SESSION['oauth_action'] = "settings-$action";
// execute the action using the controller
$controller->doOAuthAction($action);
});
// TODO: Register hooks for inserting buttons and other content into templates.
// Will this be the same for all providers?
//
// SN: we can call the class function in $controller to push the button, because each provider may have
// a different image or button type
$app->hook('login.page.control', function () use ($app) {
}, 1);
$app->hook('settings.page.control', function () use ($app) {
}, 1);
$app->hook('register.page.control', function () use ($app) {
}, 1);