-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgauth-lib.php
113 lines (91 loc) · 3.85 KB
/
gauth-lib.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
<?php
/* Copyright 2009 Carson McDonald ([email protected])
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class GAuthLib {
var $base_url = 'https://www.google.com/accounts/ClientLogin';
var $client_name;
var $http_code;
var $response_hash;
function GAuthLib( $client_name ) {
$this->client_name = $client_name;
}
function authenticate( $email, $password, $service, $login_token = '', $login_captcha = '' ) {
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $this->base_url );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_POST, true );
$post_data = array( 'accountType' => 'GOOGLE', 'Email' => $email, 'Passwd' => $password, 'service' => $service, 'source' => $this->client_name );
if ( $login_token != '' ) {
$post_data['logintoken'] = $login_token;
$post_data['logincaptcha'] = $login_captcha;
}
curl_setopt( $ch, CURLOPT_POSTFIELDS, $post_data );
$return = curl_exec( $ch );
$this->http_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
$this->response_hash = array();
foreach ( explode( "\n", $return ) as $line ) {
if ( trim( $line ) != "" ) {
$pos = strpos( $line, '=' );
if ( $pos !== false ) {
$this->response_hash[strtolower( substr( $line, 0, $pos ) )] = substr( $line, $pos + 1 );
}
}
}
curl_close( $ch );
}
function isError() {
return $this->http_code != 200;
}
function requiresCaptcha() {
return $this->isError() && $this->response_hash['error'] == 'CaptchaRequired';
}
function getCaptchaImageURL() {
return 'http://www.google.com/accounts/' . $this->response_hash['captchaurl'];
}
function getAuthToken() {
return $this->response_hash['auth'];
}
function getErrorMessage() {
switch ( $this->response_hash['error'] ) {
case 'BadAuthentication':
return __( 'The login request is for a username or password that is not recognized.', 'google-analytics-dashboard' );
case 'NotVerified':
return __( 'The account email address has not been verified. You will need to access your Google account directly to resolve this issue.', 'google-analytics-dashboard' );
case 'TermsNotAgreed':
return __( 'You have not agreed to Google terms. You will need access your Google account directly to resolve the issue.', 'google-analytics-dashboard' );
case 'CaptchaRequired':
return __( 'A CAPTCHA is required.', 'google-analytics-dashboard' );
case 'Unknown':
return __( 'Unknown error.', 'google-analytics-dashboard' );
case 'AccountDeleted':
return __( 'Your Google account has been deleted.', 'google-analytics-dashboard' );
case 'AccountDisabled':
return __( 'Your Google account has been disabled.', 'google-analytics-dashboard' );
case 'ServiceDisabled':
return __( 'Your Google access to the specified service has been disabled.', 'google-analytics-dashboard' );
case 'ServiceUnavailable':
return __( 'The service is not available; try again later.', 'google-analytics-dashboard' );
default:
return $this->response_hash['error'];
}
}
function getRawErrorMessage() {
return $this->response_hash['error'];
}
function getCaptchaToken() {
return $this->response_hash['captchatoken'];
}
}
?>