c9b1a4335e8aea850e6e8e59173e0b16d7bd3bcd
[WP-Plugins/glm-member-db.git] /
1 <?php
2 namespace PayPal\Core;
3 use PayPal\Core\PPCredentialManager;
4 use PayPal\Auth\PPSignatureCredential;
5 use PayPal\Auth\PPCertificateCredential;
6 use PayPal\Auth\PPSubjectAuthorization;
7 use PayPal\Exception\PPInvalidCredentialException;
8 use PayPal\Exception\PPMissingCredentialException;
9
10 class PPCredentialManager
11 {
12         private static $instance;
13         //hashmap to contain credentials for accounts.
14         private $credentialHashmap = array();
15         /**
16          * Contains the API username of the default account to use
17          * when authenticating API calls.
18          * @var string
19          */
20         private $defaultAccountName;
21         
22         /*
23          * Constructor initialize credential for multiple accounts specified in property file.
24          */
25         private function __construct($config){
26                 try {
27                         $this->initCredential($config);
28                 } catch (Exception $e) {
29                         $this->credentialHashmap = array();
30                         throw $e;
31                 }               
32         }
33         
34         /*
35          * Create singleton instance for this class.
36          */
37         public static function getInstance($config)
38         {
39                 
40                         return self::$instance = new PPCredentialManager($config);
41                 
42         }
43         
44         /*
45          * Load credentials for multiple accounts, with priority given to Signature credential. 
46          */
47         private function initCredential($config){
48         //      $configMgr = PPConfigManager::getInstance();
49                 $suffix = 1;
50                 $prefix = "acct";
51
52         //      $credArr = $configMgr->get($prefix);
53         //      $arrayPartKeys = $configMgr->getIniPrefix();
54                 
55                 if(array_key_exists($prefix, $config))
56                 {
57                         $credArr =  $this->config[$searchKey];
58                 }
59                 else {
60                         $arr = array();
61                         foreach ($config as $k => $v){
62                                 if(strstr($k, $prefix)){
63                                         $arr[$k] = $v;
64                                 }
65                         }
66                                 
67                         $credArr =  $arr;
68                 }
69                 
70                 $arr = array();
71                 foreach ($config as $key => $value) {
72                         $pos = strpos($key, '.');
73                         if(strstr($key, "acct")){
74                                 $arr[] = substr($key, 0, $pos);
75                         }
76                 }
77                 $arrayPartKeys =  array_unique($arr);
78                 
79                 if(count($arrayPartKeys) == 0)
80                         throw new PPMissingCredentialException("No valid API accounts have been configured");
81
82                 $key = $prefix.$suffix;
83                 while (in_array($key, $arrayPartKeys)){
84                                                         
85                         if(isset($credArr[$key.".Signature"]) 
86                                         && $credArr[$key.".Signature"] != null && $credArr[$key.".Signature"] != ""){
87                                         
88                                 $userName = isset($credArr[$key.'.UserName']) ? $credArr[$key.'.UserName'] : "";
89                                 $password = isset($credArr[$key.'.Password']) ? $credArr[$key.'.Password'] : "";
90                                 $signature = isset($credArr[$key.'.Signature']) ? $credArr[$key.'.Signature'] : "";
91                                 
92                                 $this->credentialHashmap[$userName] = new PPSignatureCredential($userName, $password, $signature);
93                                 if (isset($credArr[$key.'.AppId'])) {                           
94                                         $this->credentialHashmap[$userName]->setApplicationId($credArr[$key.'.AppId']);
95                                 }
96                                 
97                         } elseif (isset($credArr[$key.".CertPath"]) 
98                                         && $credArr[$key.".CertPath"] != null && $credArr[$key.".CertPath"] != ""){
99                                                 
100                                 $userName = isset($credArr[$key.'.UserName']) ? $credArr[$key.'.UserName'] : "";
101                                 $password = isset($credArr[$key.'.Password']) ? $credArr[$key.'.Password'] : "";
102                                 $certPassPhrase = isset($credArr[$key.'.CertKey']) ? $credArr[$key.'.CertKey'] : "";    
103                                 $certPath = isset($credArr[$key.'.CertPath']) ? $credArr[$key.'.CertPath'] : "";                                
104                                 
105                                 $this->credentialHashmap[$userName] = new PPCertificateCredential($userName, $password, $certPath, $certPassPhrase);
106                                 if (isset($credArr[$key.'.AppId'])) {
107                                         $this->credentialHashmap[$userName]->setApplicationId($credArr[$key.'.AppId']);
108                                 }                               
109                         } elseif (isset($credArr[$key.".ClientId"]) && isset($credArr[$key.".ClientId"]) ){
110                                 $userName = $key;
111                                 $this->credentialHashmap[$userName] = array('clientId' => $credArr[$key.".ClientId"], 
112                                                 'clientSecret' => $credArr[$key.".ClientSecret"]);
113                         }
114                         if($userName && isset($credArr[$key . ".Subject"]) && trim($credArr[$key . ".Subject"]) != "" ) {
115                                 $this->credentialHashmap[$userName]->setThirdPartyAuthorization(
116                                                 new PPSubjectAuthorization($credArr[$key . ".Subject"]));
117                         }
118                         
119                         if ($userName && $this->defaultAccountName == null) {
120                                 if(array_key_exists($key. '.UserName', $credArr)) {
121                                         $this->defaultAccountName = $credArr[$key . '.UserName'];
122                                 } else {
123                                         $this->defaultAccountName = $key;
124                                 }
125                         }
126                         $suffix++;
127                         $key = $prefix.$suffix;
128                 }
129
130         }
131
132         /*
133          * Obtain Credential Object based on UserId provided.
134          */
135         public function getCredentialObject($userId = null){
136                 
137                 if($userId == null) {                   
138                         $credObj = $this->credentialHashmap[$this->defaultAccountName];
139                 } else if (array_key_exists($userId, $this->credentialHashmap)) {
140                         $credObj = $this->credentialHashmap[$userId];
141                 }
142                         
143                 if (empty($credObj)) {
144                         throw new PPInvalidCredentialException("Invalid userId $userId");
145                 }
146                 return $credObj;
147         }
148         
149         
150         public function __clone()
151         {
152                 trigger_error('Clone is not allowed.', E_USER_ERROR);
153         }
154
155 }