-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAccesoController.php
More file actions
executable file
·253 lines (192 loc) · 8.7 KB
/
AccesoController.php
File metadata and controls
executable file
·253 lines (192 loc) · 8.7 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php
class AccesoController extends Controller
{
public function actionGoogle()
{
require_once ('protected/vendors/google-api-php-client/src/Google_Client.php');
require_once ('protected/vendors/google-api-php-client/src/contrib/Google_Oauth2Service.php');
session_start();
$client = new Google_Client();
$client->setClientId('489451579759.apps.googleusercontent.com');
$client->setClientSecret('vB3LeyMP06S0aHoSw5vYPxJ5');
$client->setRedirectUri('http://www.codespain.es/index.php/Acceso/Google');
//$client->setRedirectUri('http://localhost/CodeSpain/index.php/Acceso/Google');
$client->setDeveloperKey('AIzaSyAm4Db2U-kRW0PjdAlvedYt2eEF8sEzfuU');
$plus = new Google_Oauth2Service($client);
if (isset($_REQUEST['logout'])) {
unset($_SESSION['access_token']);
}
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}
if (isset($_SESSION['access_token'])) {
$client->setAccessToken($_SESSION['access_token']);
}
if ($client->getAccessToken()) {
$me = $plus->userinfo_v2_me->get();
// These fields are currently filtered through the PHP sanitize filters.
// See http://www.php.net/manual/en/filter.filters.sanitize.php
//$url = filter_var($me['url'], FILTER_VALIDATE_URL);
//$img = filter_var($me['image']['url'], FILTER_VALIDATE_URL);
$mail = filter_var($me['email'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
//$personMarkup = "<a rel='me' href='$url'>$name</a><div><img src='$img'></div>";
/*Insertar en BD*/
$decode = json_decode($_SESSION['access_token'], true);
$this->GuardarEnBD($mail,$decode['access_token']);
/*$optParams = array('maxResults' => 100);
$activities = $plus->activities->listActivities('me', 'public', $optParams);
$activityMarkup = '';
foreach($activities['items'] as $activity) {
// These fields are currently filtered through the PHP sanitize filters.
// See http://www.php.net/manual/en/filter.filters.sanitize.php
$url = filter_var($activity['url'], FILTER_VALIDATE_URL);
$title = filter_var($activity['title'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
$content = filter_var($activity['object']['content'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
$activityMarkup .= "<div class='activity'><a href='$url'>$title</a><div>$content</div></div>";*/
// The access token may have been updated lazily.
$_SESSION['access_token'] = $client->getAccessToken();
} else {
if(isset($_GET['error']))
{
$urlIni = Yii::app()->createUrl("");
header("Location: $urlIni");
}
else
{
$authUrl = $client->createAuthUrl();
header('Location: ' . $authUrl);
}
}
}
public function actionGoogleCallback()
{
}
public function actionTwitter()
{
require_once('protected/vendors/twitteroauth-master/twitteroauth/twitteroauth.php');
require_once('protected/vendors/twitteroauth-master/config.php');
/* Start session and load library. */
session_start();
/* Build TwitterOAuth object with client credentials. */
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
/* Get temporary credentials. */
$request_token = $connection->getRequestToken(OAUTH_CALLBACK);
/* Save temporary credentials to session. */
$_SESSION['oauth_token'] = $token = $request_token['oauth_token'];
$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];
/* If last connection failed don't display authorization link. */
switch ($connection->http_code)
{
case 200:
/* Build authorize URL and redirect user to Twitter. */
$url = $connection->getAuthorizeURL($token);
header('Location: ' . $url);
break;
default:
/* Show notification if something went wrong. */
echo 'No ha sido posible conectar con twitter. Refresca o intentalo más tarde';
}
}
public function actionTwitterCallback()
{
/* Start session and load lib */
session_start();
require_once('protected/vendors/twitteroauth-master/twitteroauth/twitteroauth.php');
require_once('protected/vendors/twitteroauth-master/config.php');
/* If the oauth_token is old redirect to the connect page. */
if (isset($_REQUEST['oauth_token']) && $_SESSION['oauth_token'] !== $_REQUEST['oauth_token']) {
$_SESSION['oauth_status'] = 'oldtoken';
unset($_SESSION['oauth_token']);
unset($_SESSION['oauth_token_secret']);
$urlIni = Yii::app()->createUrl("");
header("Location: $urlIni");
return true;
}
if (!isset($_REQUEST['oauth_verifier']))
{
unset($_SESSION['oauth_token']);
unset($_SESSION['oauth_token_secret']);
$urlIni = Yii::app()->createUrl("");
header("Location: $urlIni");
return true;
}
/* Create TwitteroAuth object with app key/secret and token key/secret from default phase */
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
/* Request access tokens from twitter */
$access_token = $connection->getAccessToken($_REQUEST['oauth_verifier']);
/* Save the access tokens. Normally these would be saved in a database for future use. */
$_SESSION['access_token'] = $access_token;
/* Remove no longer needed request tokens */
unset($_SESSION['oauth_token']);
unset($_SESSION['oauth_token_secret']);
/* If HTTP response is 200 continue otherwise send to connect page to retry */
if (200 == $connection->http_code) {
/* The user has been verified and the access tokens can be saved for future use */
$_SESSION['status'] = 'verified';
$user = $connection->get('account/verify_credentials');
$this->GuardarEnBD($user->screen_name,$access_token["oauth_token_secret"]);
} else
{
/* Save HTTP status for error dialog on connnect page.*/
unset($_SESSION['access_token']);
$urlIni = Yii::app()->createUrl("");
header("Location: $urlIni");
}
}
public function actionFacebook()
{
require 'protected/vendors/facebook-php-sdk-master/src/facebook.php';
$facebook = new Facebook(array(
'appId' => '532159653489490',
'secret' => '6aef4999e4dee55f07022de18aa14c31',
));
// Get User ID
$user = $facebook->getUser();
// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
$this->GuardarEnBD($user_profile["email"],$user_profile["id"]);
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
//$logoutUrl = $facebook->getLogoutUrl();
} else {
$params = array(
'scope' => 'email'
);
$loginUrl = $facebook->getLoginUrl($params);
header('Location: ' . $loginUrl);
}
}
public function GuardarEnBD($nombre,$token)
{
$ctrlUsuarios = new UsuariosController('usuarios');
$model = $ctrlUsuarios->buscarPorNombre($nombre);
if($model == null)
{
$ctrlUsuarios->Create($nombre,$token);
}
else
{
$ctrlUsuarios->Update($model->idUsuarios,$token);
}
$identity=new UserIdentity($nombre,$token);
$identity->authenticate();
$login=Yii::app()->user;
$login->login($identity);
$urlIni = Yii::app()->createUrl("");
header("Location: $urlIni");
}
}