| 103 |
- |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/***************************************************************************
|
|
|
4 |
* Copyright (C) 2009-2011 by Geo Varghese(www.seopanel.in) *
|
|
|
5 |
* sendtogeo@gmail.com *
|
|
|
6 |
* *
|
|
|
7 |
* This program is free software; you can redistribute it and/or modify *
|
|
|
8 |
* it under the terms of the GNU General Public License as published by *
|
|
|
9 |
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
10 |
* (at your option) any later version. *
|
|
|
11 |
* *
|
|
|
12 |
* This program is distributed in the hope that it will be useful, *
|
|
|
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
15 |
* GNU General Public License for more details. *
|
|
|
16 |
* *
|
|
|
17 |
* You should have received a copy of the GNU General Public License *
|
|
|
18 |
* along with this program; if not, write to the *
|
|
|
19 |
* Free Software Foundation, Inc., *
|
|
|
20 |
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
|
21 |
***************************************************************************/
|
|
|
22 |
|
|
|
23 |
// include google api module
|
|
|
24 |
include_once(SP_LIBPATH . "/google-api-php-client/vendor/autoload.php");
|
|
|
25 |
include_once(SP_CTRLPATH . "/user-token.ctrl.php");
|
|
|
26 |
|
|
|
27 |
// class defines all google api controller functions
|
|
|
28 |
class GoogleAPIController extends Controller{
|
|
|
29 |
|
|
|
30 |
var $tokenCtrler;
|
|
|
31 |
var $sourceName = 'google';
|
|
|
32 |
|
|
|
33 |
/*
|
|
|
34 |
* contructor
|
|
|
35 |
*/
|
|
|
36 |
function __construct() {
|
|
|
37 |
parent::__construct();
|
|
|
38 |
$this->tokenCtrler = new UserTokenController();
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
/*
|
|
|
42 |
* function to create auth api client with credentials
|
|
|
43 |
*/
|
|
|
44 |
function createAuthAPIClient() {
|
|
|
45 |
|
|
|
46 |
// if credentials defined
|
|
|
47 |
if (defined('SP_GOOGLE_API_CLIENT_ID') && defined('SP_GOOGLE_API_CLIENT_SECRET') && SP_GOOGLE_API_CLIENT_ID != '' && SP_GOOGLE_API_CLIENT_SECRET != '') {
|
|
|
48 |
$client = new Google_Client();
|
|
|
49 |
$client->setApplicationName("SP_CHECKER");
|
|
|
50 |
$client->setClientId(SP_GOOGLE_API_CLIENT_ID);
|
|
|
51 |
$client->setClientSecret(SP_GOOGLE_API_CLIENT_SECRET);
|
|
|
52 |
$client->setAccessType('offline');
|
|
|
53 |
$redirectUrl = SP_WEBPATH . "/admin-panel.php?sec=connections&action=connect_return&category=" . $this->sourceName;
|
|
|
54 |
$client->setRedirectUri($redirectUrl);
|
|
|
55 |
|
|
|
56 |
// set app scopes
|
|
|
57 |
$client = $this->setAppScopes($client);
|
|
|
58 |
|
|
|
59 |
} else {
|
|
|
60 |
$alertCtler = new AlertController();
|
|
|
61 |
$alertInfo = array(
|
|
|
62 |
'alert_subject' => "Click here to enter Google Auth Credentials",
|
|
|
63 |
'alert_message' => "Error: Google Auth Credentials not set",
|
|
|
64 |
'alert_url' => SP_WEBPATH ."/admin-panel.php?sec=google-settings",
|
|
|
65 |
'alert_type' => "danger",
|
|
|
66 |
'alert_category' => "reports",
|
|
|
67 |
);
|
|
|
68 |
$alertCtler->createAlert($alertInfo, false, true);
|
|
|
69 |
return "Error: Google Auth Credentials not set.";
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
return $client;
|
|
|
73 |
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
|
|
|
77 |
/**
|
|
|
78 |
* function to get auth client
|
|
|
79 |
*/
|
|
|
80 |
function getAuthClient($userId) {
|
|
|
81 |
|
|
|
82 |
$client = $this->createAuthAPIClient();
|
|
|
83 |
|
|
|
84 |
// if client created successfully
|
|
|
85 |
if (is_object($client)) {
|
|
|
86 |
|
|
|
87 |
// get user token
|
|
|
88 |
$tokenInfo = $this->tokenCtrler->getUserToken($userId, $this->sourceName);
|
|
|
89 |
|
|
|
90 |
// if token not set for the user
|
|
|
91 |
if (empty($tokenInfo['access_token'])) {
|
|
|
92 |
$spTextWebmaster = $this->getLanguageTexts('webmaster', $_SESSION['lang_code']);
|
|
|
93 |
$errorText = $spTextWebmaster["Error: Google api connection failed"] . ". ";
|
|
|
94 |
$errorText .= "<a href='".SP_WEBPATH ."/admin-panel.php?sec=connections' target='_blank'>{$spTextWebmaster['Click here to connect to your google account']}.</a>";
|
|
|
95 |
$alertCtler = new AlertController();
|
|
|
96 |
$alertInfo = array(
|
|
|
97 |
'alert_subject' => $spTextWebmaster['Click here to connect to your google account'],
|
|
|
98 |
'alert_message' => $spTextWebmaster["Error: Google api connection failed"],
|
|
|
99 |
'alert_url' => SP_WEBPATH ."/admin-panel.php?sec=connections",
|
|
|
100 |
'alert_type' => "danger",
|
|
|
101 |
'alert_category' => "reports",
|
|
|
102 |
);
|
|
|
103 |
$alertCtler->createAlert($alertInfo, $userId);
|
|
|
104 |
return $errorText;
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
// set token info
|
|
|
108 |
$tokenInfo['created'] = strtotime($tokenInfo['created']);
|
|
|
109 |
$client->setAccessToken($tokenInfo);
|
|
|
110 |
|
|
|
111 |
// check whether token expired, then refresh existing token
|
|
|
112 |
if ($client->isAccessTokenExpired()) {
|
|
|
113 |
|
|
|
114 |
try {
|
|
|
115 |
$client->refreshToken($tokenInfo['refresh_token']);
|
|
|
116 |
$newToken = $client->getAccessToken();
|
|
|
117 |
$newTokenInfo = array();
|
|
|
118 |
$newTokenInfo['created'] = date('Y-m-d H:i:s', $newToken['created']);
|
|
|
119 |
$newTokenInfo['access_token'] = $newToken['access_token'];
|
|
|
120 |
$newTokenInfo['token_type'] = $newToken['token_type'];
|
|
|
121 |
$newTokenInfo['expires_in'] = $newToken['expires_in'];
|
|
|
122 |
|
|
|
123 |
// comment refresh token update to test the perfomnace
|
|
|
124 |
/*$newTokenInfo['refresh_token'] = $newToken['refresh_token'];*/
|
|
|
125 |
|
|
|
126 |
$this->tokenCtrler->updateUserToken($tokenInfo['id'], $newTokenInfo);
|
|
|
127 |
} catch (Exception $e) {
|
|
|
128 |
$err = $e->getMessage();
|
|
|
129 |
return "Error: Refresh token - $err";
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
return $client;
|
|
|
137 |
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
/*
|
|
|
141 |
* function to setup app scopes(read write permissions)
|
|
|
142 |
*/
|
|
|
143 |
function setAppScopes($client) {
|
|
|
144 |
$client->addScope([Google_Service_Webmasters::WEBMASTERS, Google_Service_AnalyticsReporting::ANALYTICS_READONLY]);
|
|
|
145 |
return $client;
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
/*
|
|
|
149 |
* function to get auth url
|
|
|
150 |
*/
|
|
|
151 |
function getAPIAuthUrl($userId) {
|
|
|
152 |
$ret = array('auth_url' => false);
|
|
|
153 |
$client = $this->createAuthAPIClient();
|
|
|
154 |
|
|
|
155 |
// if client created successfully
|
|
|
156 |
if (is_object($client)) {
|
|
|
157 |
|
|
|
158 |
try {
|
|
|
159 |
$authUrl = $client->createAuthUrl();
|
|
|
160 |
$ret['auth_url'] = $authUrl;
|
|
|
161 |
} catch (Exception $e) {
|
|
|
162 |
$err = $e->getMessage();
|
|
|
163 |
$ret['msg'] = "Error: Create token - $err";
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
} else {
|
|
|
167 |
$ret['msg'] = $client;
|
|
|
168 |
}
|
|
|
169 |
|
|
|
170 |
return $ret;
|
|
|
171 |
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
/*
|
|
|
175 |
* function to create auth token
|
|
|
176 |
*/
|
|
|
177 |
function createUserAuthToken($userId, $authCode) {
|
|
|
178 |
|
|
|
179 |
$ret = array('status' => false);
|
|
|
180 |
$client = $this->createAuthAPIClient();
|
|
|
181 |
|
|
|
182 |
// if client created successfully
|
|
|
183 |
if (is_object($client)) {
|
|
|
184 |
|
|
|
185 |
try {
|
|
|
186 |
$tkInfo = $client->fetchAccessTokenWithAuthCode($authCode);
|
|
|
187 |
$tokenInfo['created'] = date('Y-m-d H:i:s', $tkInfo['created']);
|
|
|
188 |
$tokenInfo['user_id'] = intval($userId);
|
|
|
189 |
$tokenInfo['access_token'] = $tkInfo['access_token'];
|
|
|
190 |
$tokenInfo['token_type'] = $tkInfo['token_type'];
|
|
|
191 |
$tokenInfo['expires_in'] = $tkInfo['expires_in'];
|
|
|
192 |
$tokenInfo['refresh_token'] = $tkInfo['refresh_token'];
|
|
|
193 |
$this->tokenCtrler->insertUserToken($tokenInfo);
|
|
|
194 |
$ret['status'] = true;
|
|
|
195 |
} catch (Exception $e) {
|
|
|
196 |
$err = $e->getMessage();
|
|
|
197 |
$ret['msg'] = "Error: Create token - $err";
|
|
|
198 |
}
|
|
|
199 |
|
|
|
200 |
} else {
|
|
|
201 |
$ret['msg'] = $client;
|
|
|
202 |
}
|
|
|
203 |
|
|
|
204 |
return $ret;
|
|
|
205 |
|
|
|
206 |
}
|
|
|
207 |
|
|
|
208 |
/*
|
|
|
209 |
* function to remove all user tokens
|
|
|
210 |
*/
|
|
|
211 |
function removeUserAuthToken($userId) {
|
|
|
212 |
$ret = array('status' => false);
|
|
|
213 |
|
|
|
214 |
try {
|
|
|
215 |
|
|
|
216 |
$tokenInfo = $this->tokenCtrler->getUserToken($userId, $this->sourceName);
|
|
|
217 |
|
|
|
218 |
if (!empty($tokenInfo['id'])) {
|
|
|
219 |
$client = $this->createAuthAPIClient();
|
|
|
220 |
$client->revokeToken($tokenInfo['access_token']);
|
|
|
221 |
}
|
|
|
222 |
|
|
|
223 |
} catch (Exception $e) {
|
|
|
224 |
$err = $e->getMessage();
|
|
|
225 |
$ret['msg'] = "Error: revoke token - $err";
|
|
|
226 |
}
|
|
|
227 |
|
|
|
228 |
$tokenInfo = $this->tokenCtrler->deleteAllUserTokens($userId, $this->sourceName);
|
|
|
229 |
return $ret;
|
|
|
230 |
|
|
|
231 |
}
|
|
|
232 |
|
|
|
233 |
}
|
|
|
234 |
?>
|