-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathZohoOAuth.php
More file actions
169 lines (147 loc) · 6.62 KB
/
ZohoOAuth.php
File metadata and controls
169 lines (147 loc) · 6.62 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
<?php
namespace zcrmsdk\oauth;
use Exception;
use zcrmsdk\oauth\exception\ZohoOAuthException;
use zcrmsdk\oauth\persistence\ZohoOAuthPersistenceByFile;
use zcrmsdk\oauth\persistence\ZohoOAuthPersistenceHandler;
use zcrmsdk\oauth\persistence\ZohoOAuthPersistenceInterface;
use zcrmsdk\oauth\utility\ZohoOAuthConstants;
use zcrmsdk\oauth\utility\ZohoOAuthParams;
class ZohoOAuth
{
private static $configProperties = array();
public static function initialize($configuration)
{
self::setConfigValues($configuration);
if (! array_key_exists(ZohoOAuthConstants::TOKEN_PERSISTENCE_PATH, self::$configProperties) || self::$configProperties[ZohoOAuthConstants::TOKEN_PERSISTENCE_PATH] == "") {
if (! array_key_exists(ZohoOAuthConstants::DATABASE_PORT, self::$configProperties)) {
self::$configProperties[ZohoOAuthConstants::DATABASE_PORT] = "3306";
}
if (! array_key_exists(ZohoOAuthConstants::DATABASE_USERNAME, self::$configProperties)) {
self::$configProperties[ZohoOAuthConstants::DATABASE_USERNAME] = "root";
}
if (! array_key_exists(ZohoOAuthConstants::DATABASE_PASSWORD, self::$configProperties)) {
self::$configProperties[ZohoOAuthConstants::DATABASE_PASSWORD] = "";
}
if (! array_key_exists(ZohoOAuthConstants::DATABASE_NAME, self::$configProperties)) {
self::$configProperties[ZohoOAuthConstants::DATABASE_NAME] = "zohooauth";
}
if (! array_key_exists(ZohoOAuthConstants::HOST_ADDRESS, self::$configProperties)) {
self::$configProperties[ZohoOAuthConstants::HOST_ADDRESS] = "localhost";
}
}
$oAuthParams = new ZohoOAuthParams();
$oAuthParams->setAccessType(self::getConfigValue(ZohoOAuthConstants::ACCESS_TYPE));
$oAuthParams->setClientId(self::getConfigValue(ZohoOAuthConstants::CLIENT_ID));
$oAuthParams->setClientSecret(self::getConfigValue(ZohoOAuthConstants::CLIENT_SECRET));
$oAuthParams->setRedirectURL(self::getConfigValue(ZohoOAuthConstants::REDIRECT_URL));
ZohoOAuthClient::getInstance($oAuthParams);
}
private static function setConfigValues($configuration)
{
$config_keys = array(
ZohoOAuthConstants::CLIENT_ID,
ZohoOAuthConstants::CLIENT_SECRET,
ZohoOAuthConstants::REDIRECT_URL,
ZohoOAuthConstants::ACCESS_TYPE,
ZohoOAuthConstants::PERSISTENCE_HANDLER,
ZohoOAuthConstants::PERSISTENCE_HANDLER_CLASS,
ZohoOAuthConstants::IAM_URL,
ZohoOAuthConstants::TOKEN_PERSISTENCE_PATH,
ZohoOAuthConstants::DATABASE_PORT,
ZohoOAuthConstants::DATABASE_PASSWORD,
ZohoOAuthConstants::DATABASE_USERNAME,
ZohoOAuthConstants::PERSISTENCE_HANDLER_CLASS_NAME,
ZohoOAuthConstants::HOST_ADDRESS
);
if (! array_key_exists(ZohoOAuthConstants::ACCESS_TYPE, $configuration) || $configuration[ZohoOAuthConstants::ACCESS_TYPE] == "") {
self::$configProperties[ZohoOAuthConstants::ACCESS_TYPE] = "offline";
}
if (! array_key_exists(ZohoOAuthConstants::PERSISTENCE_HANDLER_CLASS, $configuration) || $configuration[ZohoOAuthConstants::PERSISTENCE_HANDLER_CLASS] == "") {
self::$configProperties[ZohoOAuthConstants::PERSISTENCE_HANDLER_CLASS] = "ZohoOAuthPersistenceHandler";
}
if (! array_key_exists(ZohoOAuthConstants::IAM_URL, $configuration) || $configuration[ZohoOAuthConstants::IAM_URL] == "") {
self::$configProperties[ZohoOAuthConstants::IAM_URL] = "https://accounts.zoho.com";
}
foreach ($config_keys as $key) {
if (array_key_exists($key, $configuration))
self::$configProperties[$key] = $configuration[$key];
}
}
public static function getConfigValue($key)
{
return isset(self::$configProperties[$key])?self::$configProperties[$key]:"";
}
public static function getAllConfigs()
{
return self::$configProperties;
}
public static function getIAMUrl()
{
return self::getConfigValue(ZohoOAuthConstants::IAM_URL);
}
public static function getGrantURL()
{
return self::getIAMUrl() . "/oauth/v2/auth";
}
public static function getTokenURL()
{
return self::getIAMUrl() . "/oauth/v2/token";
}
public static function getRefreshTokenURL()
{
return self::getIAMUrl() . "/oauth/v2/token";
}
public static function getRevokeTokenURL()
{
return self::getIAMUrl() . "/oauth/v2/token/revoke";
}
public static function getUserInfoURL()
{
return self::getIAMUrl() . "/oauth/user/info";
}
public static function getClientID()
{
return self::getConfigValue(ZohoOAuthConstants::CLIENT_ID);
}
public static function getClientSecret()
{
return self::getConfigValue(ZohoOAuthConstants::CLIENT_SECRET);
}
public static function getRedirectURL()
{
return self::getConfigValue(ZohoOAuthConstants::REDIRECT_URL);
}
public static function getAccessType()
{
return self::getConfigValue(ZohoOAuthConstants::ACCESS_TYPE);
}
public static function getPersistenceHandlerInstance()
{
try {
if(ZohoOAuth::getConfigValue("token_persistence_path")!=""){
return new ZohoOAuthPersistenceByFile() ;
}
else if(self::$configProperties[ZohoOAuthConstants::PERSISTENCE_HANDLER] instanceof ZohoOAuthPersistenceInterface){
return self::$configProperties[ZohoOAuthConstants::PERSISTENCE_HANDLER];
}
else if(self::$configProperties[ZohoOAuthConstants::PERSISTENCE_HANDLER_CLASS] == "ZohoOAuthPersistenceHandler"){
return new ZohoOAuthPersistenceHandler();
}
else{
require_once realpath(self::$configProperties[ZohoOAuthConstants::PERSISTENCE_HANDLER_CLASS]);
$str=self::$configProperties[ZohoOAuthConstants::PERSISTENCE_HANDLER_CLASS_NAME];
return new $str();
}
} catch (Exception $ex) {
throw new ZohoOAuthException($ex);
}
}
public static function getClientInstance()
{
if (ZohoOAuthClient::getInstanceWithOutParam() == null) {
throw new ZohoOAuthException("ZCRMRestClient::initialize(\$configMap) must be called before this.");
}
return ZohoOAuthClient::getInstanceWithOutParam();
}
}