-
Notifications
You must be signed in to change notification settings - Fork 2
/
contact_auth.php
120 lines (97 loc) · 3.91 KB
/
contact_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
<?php
/*
FusionPBX
Version: MPL 1.1
The contents of this file are subject to the Mozilla Public License Version
1.1 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
for the specific language governing rights and limitations under the
License.
The Original Code is FusionPBX
The Initial Developer of the Original Code is
Mark J Crane <[email protected]>
Portions created by the Initial Developer are Copyright (C) 2008-2013
the Initial Developer. All Rights Reserved.
Contributor(s):
Mark J Crane <[email protected]>
*/
require_once "root.php";
require_once "resources/require.php";
require_once "resources/check_auth.php";
if (permission_exists('contact_add')) {
//access granted
}
else {
echo "access denied";
exit;
}
/*
echo "bang!";
exit;
*/
//add multi-lingual support
$language = new text;
$text = $language->get();
$_SESSION['contact_auth']['source'] = ($_SESSION['contact_auth']['source'] == '') ? $_REQUEST['source'] : $_SESSION['contact_auth']['source'];
$_SESSION['contact_auth']['target'] = ($_SESSION['contact_auth']['target'] == '') ? $_REQUEST['target'] : $_SESSION['contact_auth']['target'];
//google api authentication
if ($_SESSION['contact_auth']['source'] == 'google') {
if ($_REQUEST['error']) {
message::add(($text['message-'.$_REQUEST['error']] != '') ? $text['message-'.$_REQUEST['error']] : $_REQUEST['error'], 'negative');
header("Location: ".$_SESSION['contact_auth']['referer']);
unset($_SESSION['contact_auth']);
exit;
}
if (isset($_REQUEST['signout'])) {
unset($_SESSION['contact_auth']['token']);
message::add($text['message-google_signed_out']);
header("Location: https://www.google.com/accounts/Logout?continue=https://appengine.google.com/_ah/logout?continue=".(($_SERVER["HTTPS"] == "on") ? "https" : "http")."://".$_SERVER['HTTP_HOST'].PROJECT_PATH."/app/contacts/".$_SESSION['contact_auth']['referer']);
exit;
}
if ($_GET['code'] == '') {
header("Location: https://accounts.google.com/o/oauth2/auth?client_id=".$_SESSION['contact']['google_oauth_client_id']['text']."&redirect_uri=".(($_SERVER["HTTPS"] == "on") ? "https" : "http")."://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']."&scope=https://www.google.com/m8/feeds/&response_type=code");
exit;
}
else {
$auth_code = $_GET["code"];
}
/*******************************************************************************************/
// request access token
$fields = array(
'code' => urlencode($auth_code),
'client_id' => urlencode($_SESSION['contact']['google_oauth_client_id']['text']),
'client_secret' => urlencode($_SESSION['contact']['google_oauth_client_secret']['text']),
'redirect_uri' => urlencode((($_SERVER["HTTPS"] == "on") ? "https" : "http")."://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']),
'grant_type' => urlencode('authorization_code')
);
foreach($fields as $key => $value) {
$post_fields[] = $key.'='.$value;
}
$post_fields = implode("&", $post_fields);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://accounts.google.com/o/oauth2/token');
curl_setopt($curl, CURLOPT_POST, 5);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
$result = curl_exec($curl);
curl_close($curl);
$response = json_decode($result);
$access_token = $response->access_token;
if ($access_token != '') {
// redirect to target script
$_SESSION['contact_auth']['token'] = $access_token;
header("Location: ".$_SESSION['contact_auth']['target']);
exit;
}
}
else {
message::add($text['message-access_denied'], 'negative');
header("Location: ".$_SESSION['contact_auth']['referer']);
unset($_SESSION['contact_auth']);
exit;
}
?>