1 | <?php |
||
44 | class Session |
||
45 | { |
||
46 | // HTTP Client instance |
||
47 | protected $httpClient; |
||
48 | |||
49 | // Service URL to which client connects to |
||
50 | protected $vtigerUrl = null; |
||
51 | protected $wsBaseURL = null; |
||
52 | |||
53 | // Vtiger CRM and WebServices API version |
||
54 | private $vtigerApiVersion = '0.0'; |
||
55 | private $vtigerVersion = '0.0'; |
||
56 | |||
57 | // Webservice login validity |
||
58 | private $serviceExpireTime = null; |
||
59 | private $serviceToken = null; |
||
60 | |||
61 | // Webservice user credentials |
||
62 | private $userName = null; |
||
63 | private $accessKey = null; |
||
64 | |||
65 | // Webservice login credentials |
||
66 | private $userID = null; |
||
67 | private $sessionName = null; |
||
68 | |||
69 | /** |
||
70 | * Class constructor |
||
71 | * @param string $vtigerUrl The URL of the remote WebServices server |
||
72 | * @param string [$wsBaseURL = 'webservice.php'] WebServices base URL appended to vTiger root URL |
||
73 | */ |
||
74 | public function __construct($vtigerUrl, $wsBaseURL = 'webservice.php') |
||
84 | |||
85 | /** |
||
86 | * Login to the server using username and VTiger access key token |
||
87 | * @access public |
||
88 | * @param string $username VTiger user name |
||
89 | * @param string $accessKey VTiger access key token (visible on user profile/settings page) |
||
90 | * @return boolean Returns true if login operation has been successful |
||
91 | */ |
||
92 | public function login($username, $accessKey) |
||
124 | |||
125 | /** |
||
126 | * Allows you to login using username and password instead of access key (works on some VTige forks) |
||
127 | * @access public |
||
128 | * @param string $username VTiger user name |
||
129 | * @param string $password VTiger password (used to access CRM using the standard login page) |
||
130 | * @param string $accessKey This parameter will be filled with user's VTiger access key |
||
131 | * @return boolean Returns true if login operation has been successful |
||
132 | */ |
||
133 | public function loginPassword($username, $password, &$accessKey = null) |
||
134 | { |
||
135 | // Do the challenge before loggin in |
||
136 | if ($this->passChallenge($username) === false) { |
||
137 | return false; |
||
138 | } |
||
139 | |||
140 | $postdata = [ |
||
141 | 'operation' => 'login_pwd', |
||
142 | 'username' => $username, |
||
143 | 'password' => $password |
||
144 | ]; |
||
145 | |||
146 | $result = $this->sendHttpRequest($postdata); |
||
147 | if (!is_array($result) || empty($result)) { |
||
148 | return false; |
||
149 | } |
||
150 | |||
151 | $this->accessKey = array_key_exists('accesskey', $result) |
||
152 | ? $result[ 'accesskey' ] |
||
153 | : $result[ 0 ]; |
||
154 | |||
155 | return $this->login($username, $accessKey); |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Gets a challenge token from the server and stores for future requests |
||
160 | * @access private |
||
161 | * @param string $username VTiger user name |
||
162 | * @return boolean Returns false in case of failure |
||
163 | */ |
||
164 | private function passChallenge($username) |
||
181 | |||
182 | /** |
||
183 | * Gets an array containing the basic information about current API user |
||
184 | * @access public |
||
185 | * @return array Basic information about current API user |
||
186 | */ |
||
187 | public function getUserInfo() |
||
195 | |||
196 | /** |
||
197 | * Gets vTiger version, retrieved on successful login |
||
198 | * @access public |
||
199 | * @return string vTiger version, retrieved on successful login |
||
200 | */ |
||
201 | public function getVtigerVersion() |
||
205 | |||
206 | /** |
||
207 | * Gets vTiger WebServices API version, retrieved on successful login |
||
208 | * @access public |
||
209 | * @return string vTiger WebServices API version, retrieved on successful login |
||
210 | */ |
||
211 | public function getVtigerApiVersion() |
||
215 | |||
216 | /** |
||
217 | * Sends HTTP request to VTiger web service API endpoint |
||
218 | * @access private |
||
219 | * @param array $requestData HTTP request data |
||
220 | * @param string $method HTTP request method (GET, POST etc) |
||
221 | * @return array Returns request result object (null in case of failure) |
||
222 | */ |
||
223 | public function sendHttpRequest(array $requestData, $method = 'POST') |
||
260 | |||
261 | /** |
||
262 | * Cleans and fixes vTiger URL |
||
263 | * @access private |
||
264 | * @static |
||
265 | * @param string Base URL of vTiger CRM |
||
266 | * @param string $baseUrl |
||
267 | * @return string Returns cleaned and fixed vTiger URL |
||
268 | */ |
||
269 | private static function fixVtigerBaseUrl($baseUrl) |
||
279 | |||
280 | /** |
||
281 | * Check if server response contains an error, therefore the requested operation has failed |
||
282 | * @access private |
||
283 | * @static |
||
284 | * @param array $jsonResult Server response object to check for errors |
||
285 | * @return boolean True if response object contains an error |
||
286 | */ |
||
287 | private static function checkForError(array $jsonResult) |
||
304 | } |
||
305 |