|
1
|
|
|
<?php |
|
2
|
|
|
namespace Zoho\OAuth\Persistence; |
|
3
|
|
|
|
|
4
|
|
|
use Exception; |
|
5
|
|
|
use Zoho\OAuth\Exception\ZohoOAuthException; |
|
6
|
|
|
use Zoho\OAuth\Utility\Logger; |
|
7
|
|
|
use Zoho\OAuth\Utility\ZohoOAuthTokens; |
|
8
|
|
|
use Zoho\OAuth\ZohoOAuth; |
|
9
|
|
|
|
|
10
|
|
|
class ZohoOAuthPersistenceByFile implements ZohoOAuthPersistenceInterface |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
public function saveOAuthData($zohoOAuthTokens) |
|
14
|
|
|
{ |
|
15
|
|
|
try { |
|
16
|
|
|
self::deleteOAuthTokens($zohoOAuthTokens->getUserEmailId()); |
|
17
|
|
|
$content = file_get_contents(self::getTokenPersistencePath() . "/zcrm_oauthtokens.txt"); |
|
18
|
|
|
if ($content == "") { |
|
19
|
|
|
$arr = []; |
|
20
|
|
|
} else { |
|
21
|
|
|
$arr = unserialize($content); |
|
22
|
|
|
} |
|
23
|
|
|
array_push($arr, $zohoOAuthTokens); |
|
24
|
|
|
$serialized = serialize($arr); |
|
25
|
|
|
file_put_contents(self::getTokenPersistencePath() . "/zcrm_oauthtokens.txt", $serialized); |
|
26
|
|
|
} catch (Exception $ex) { |
|
27
|
|
|
Logger::severe("Exception occured while Saving OAuthTokens to file(file::ZohoOAuthPersistenceByFile)({$ex->getMessage()})\n{$ex}"); |
|
28
|
|
|
throw $ex; |
|
29
|
|
|
} |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function deleteOAuthTokens($userEmailId) |
|
33
|
|
|
{ |
|
34
|
|
|
try { |
|
35
|
|
|
$serialized = file_get_contents(self::getTokenPersistencePath() . "/zcrm_oauthtokens.txt"); |
|
36
|
|
|
if (!isset($serialized) || $serialized == "") { |
|
37
|
|
|
return; |
|
38
|
|
|
} |
|
39
|
|
|
$arr = unserialize($serialized); |
|
40
|
|
|
$found = false; |
|
41
|
|
|
$i = -1; |
|
42
|
|
|
foreach ($arr as $i => $eachObj) { |
|
43
|
|
|
if ($userEmailId === $eachObj->getUserEmailId()) { |
|
44
|
|
|
$found = true; |
|
45
|
|
|
break; |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
if ($found) { |
|
49
|
|
|
unset($arr[$i]); |
|
50
|
|
|
$arr = array_values(array_filter($arr)); |
|
51
|
|
|
} |
|
52
|
|
|
$serialized = serialize($arr); |
|
53
|
|
|
file_put_contents(self::getTokenPersistencePath() . "/zcrm_oauthtokens.txt", $serialized); |
|
54
|
|
|
} catch (Exception $ex) { |
|
55
|
|
|
Logger::severe("Exception occured while Saving OAuthTokens to file(file::ZohoOAuthPersistenceByFile)({$ex->getMessage()})\n{$ex}"); |
|
56
|
|
|
throw $ex; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function getTokenPersistencePath() |
|
61
|
|
|
{ |
|
62
|
|
|
$path = ZohoOAuth::getConfigValue('token_persistence_path'); |
|
63
|
|
|
$path = trim($path); |
|
64
|
|
|
|
|
65
|
|
|
return $path; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function getOAuthTokens($userEmailId) |
|
69
|
|
|
{ |
|
70
|
|
|
try { |
|
71
|
|
|
$serialized = file_get_contents(self::getTokenPersistencePath() . "/zcrm_oauthtokens.txt"); |
|
72
|
|
|
if (!isset($serialized) || $serialized == "") { |
|
73
|
|
|
throw new ZohoOAuthException("No Tokens exist for the given user-identifier,Please generate and try again."); |
|
74
|
|
|
} |
|
75
|
|
|
$arr = unserialize($serialized); |
|
76
|
|
|
$tokens = new ZohoOAuthTokens(); |
|
77
|
|
|
$isValidUser = false; |
|
78
|
|
|
foreach ($arr as $eachObj) { |
|
79
|
|
|
if ($userEmailId === $eachObj->getUserEmailId()) { |
|
80
|
|
|
$tokens = $eachObj; |
|
81
|
|
|
$isValidUser = true; |
|
82
|
|
|
break; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
if (!$isValidUser) { |
|
86
|
|
|
throw new ZohoOAuthException("No Tokens exist for the given user-identifier,Please generate and try again."); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return $tokens; |
|
90
|
|
|
} catch (ZohoOAuthException $e) { |
|
91
|
|
|
throw $e; |
|
92
|
|
|
} catch (Exception $ex) { |
|
93
|
|
|
Logger::severe("Exception occured while fetching OAuthTokens from file(file::ZohoOAuthPersistenceByFile)({$ex->getMessage()})\n{$ex}"); |
|
94
|
|
|
throw $ex; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|