This repository was archived by the owner on Jan 10, 2019. It is now read-only.
forked from vadimcomanescu/vmwarephp
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSessionManager.php
More file actions
75 lines (67 loc) · 2.61 KB
/
SessionManager.php
File metadata and controls
75 lines (67 loc) · 2.61 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
<?php
namespace Vmwarephp\Extensions;
class SessionManager extends \Vmwarephp\ManagedObject {
private $cloneTicketFile;
private $session;
private $userName;
function acquireSession($userName, $password) {
if (empty($this->userName)) {
$this->userName=$userName;
}
if ($this->userName === $userName) {
if ($this->session) {
return $this->session;
}
try {
$this->session = $this->acquireSessionUsingCloneTicket();
} catch (\Exception $e) {
$this->session = $this->acquireANewSession($userName, $password);
}
} else {
$this->session = $this->acquireANewSession($userName, $password);
}
return $this->session;
}
private function acquireSessionUsingCloneTicket() {
$cloneTicket = $this->readCloneTicket();
if (!$cloneTicket) {
throw new \Exception('Cannot find any clone ticket.');
}
return $this->CloneSession(array('cloneTicket' => $cloneTicket));
}
private function acquireANewSession($userName, $password) {
$session = $this->Login(array('userName' => $userName, 'password' => $password, 'locale' => null));
$cloneTicket = $this->AcquireCloneTicket();
$this->saveCloneTicket($cloneTicket);
return $session;
}
private function saveCloneTicket($cloneTicket) {
if (!file_put_contents($this->getCloneTicketFile(), $cloneTicket))
throw new \Exception(sprintf('There was an error writing to the clone ticket path. Check the permissions of the cache directory(%s)', __DIR__ . '/../'));
}
private function readCloneTicket() {
$ticketFile = $this->getCloneTicketFile();
if (file_exists($ticketFile)) {
return file_get_contents($ticketFile);
}
}
private function getCloneTicketFile() {
if (!$this->cloneTicketFile) {
$this->cloneTicketFile = __DIR__ . '/../.' . $this->getUserNameForFileName() .'_clone_ticket.cache';
}
return $this->cloneTicketFile;
}
private function getUserNameForFileName() {
if (empty($this->userName)) {
throw new Exception('UserName is Empty');
}
if (strpos($this->userName, "\\") !== FALSE) {
$tmpUser= explode("\\", $this->userName)[1];
} elseif (strpos($this->userName, "@") !== FALSE) {
$tmpUser= explode("@", $this->userName)[0];
} else {
$tmpUser= $this->userName;
}
return $tmpUser;
}
}