/
auth.php
124 lines (94 loc) · 3.14 KB
/
auth.php
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
<?php
### Vladimir S. Udartsev
### udartsev.ru
### Based on [Hybridauth v 3.0.0] https://hybridauth.github.io/
/*
PHP SocialConnectClass - This simple example class illustrate how to authenticate users with Facebook and Vkontakte.
Based on Hybridauth Library and returns normally PHP Array output instead object array.
Most other providers (which includes in Hybridauth/Provider) work pretty much the same just replace Vkontakte/Facebook provider names to another provider name.
*/
/**
* SocialConnectClass
*/
$data = new SocialConect('vkontakte'); //change 'vkontakte' to another provider to catch the data
echo "<pre>";
var_dump($data); // for Tests only
unset($data); //unsetting data
class SocialConect {
private $autoload;
private $provider;
private $config;
private $callback;
private $auth;
private $errorMessage;
public $data = array();
function __construct($provider = null) {
/*Save the Hybridauth Library Path*/
/*change library way address*/
$this->autoload = dirname(__FILE__) . '/hybridauth/autoload.php';
/*Set Social Driver Callback Way*/
/*change callback way address*/
$this->callback = 'http://' . $_SERVER['HTTP_HOST'] . '/SocialConnectClass/auth.php';
/*Set Provide Name*/
$this->provider = $provider;
/*Check Provider*/
$this->providerCheck();
/*Check for errors*/
if ($this->errorMessage) {return $this->errorMessage;}
/*Return Catched Data*/
return $this->data;
}
private function providerCheck() {
/*Checking Matching $provider Name*/
if ($this->provider == 'vkontakte') {
$this->config = [
'callback' => $this->callback,
'keys' => ['id' => '', 'secret' => ''],
];
$this->vkontakteAuth();
}
if ($this->provider == 'facebook') {
$this->config = [
'callback' => $this->callback,
'keys' => ['id' => '', 'secret' => ''],
];
$this->facebookAuth();
}
/*If No Matching - Return False*/
$this->errorMessage = 'ERROR: Provider [' . $this->provider . '] Does Not Found!';
}
private function vkontakteAuth() {
/*Require the Hybridauth Library*/
require $this->autoload;
/*Create new Hybridauth Class*/
$this->auth = new Hybridauth\Provider\Vkontakte($this->config);
/*Connect to Provider*/
$this->connect();
}
private function facebookAuth() {
/*Require the Hybridauth Library*/
require $this->autoload;
/*Create new Hybridauth Class*/
$this->auth = new Hybridauth\Provider\Facebook($this->config);
/*Connect to Provider*/
$this->connect();
}
private function connect() {
/*Do Hybridauth Autentification*/
$this->auth->authenticate();
/*Get User Profile*/
$this->data['userProfile'] = $this->auth->getUserProfile();
/*Get Provider API Response*/
$this->data['apiResponse'] = $this->auth->apiRequest('gists');
/*Get Provider Access Token Key*/
$this->data['accessToken'] = $this->auth->getAccessToken();
/*Get Provider Responce Body*/
//$this->data['ResponseBody'] = $this->auth->getHttpClient()->getResponseBody();
/*Disconnect From Provider*/
$this->auth->disconnect();
/*Unset Variables*/
unset($this->auth, $this->config);
/*Object Data to Array Data*/
$this->data = json_decode(json_encode($this->data), true);
}
}