|
1
|
|
|
<?php namespace PCextreme\Cloudstack\Util; |
|
2
|
|
|
|
|
3
|
|
|
use InvalidArgumentException; |
|
4
|
|
|
use PCextreme\Cloudstack\Exception\ClientException; |
|
5
|
|
|
|
|
6
|
|
|
trait UrlHelpersTrait |
|
7
|
|
|
{ |
|
8
|
|
|
/** |
|
9
|
|
|
* Generate Client URL for specified username. |
|
10
|
|
|
* |
|
11
|
|
|
* @param string $username |
|
12
|
|
|
* @param string $domainId |
|
13
|
|
|
* @return string |
|
14
|
|
|
* @throws \InvalidArgumentEception |
|
15
|
|
|
*/ |
|
16
|
|
|
public function clientUrl($username, $domainId) |
|
17
|
|
|
{ |
|
18
|
|
|
if (is_null($this->urlClient)) { |
|
|
|
|
|
|
19
|
|
|
throw new InvalidArgumentException( |
|
20
|
|
|
'Required options not defined: urlClient' |
|
21
|
|
|
); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
// Prepare session. |
|
25
|
|
|
// Using the SSO (Single Sign On) key we can generate a sessionkey used for the console url. |
|
26
|
|
|
$command = 'login'; |
|
27
|
|
|
$params = [ |
|
28
|
|
|
'command' => $command, |
|
29
|
|
|
'username' => $username, |
|
30
|
|
|
'domainid' => $domainId, |
|
31
|
|
|
'timestamp' => round(microtime(true) * 1000), |
|
32
|
|
|
'response' => 'json', |
|
33
|
|
|
]; |
|
34
|
|
|
|
|
35
|
|
|
$method = $this->getCommandMethod($command); |
|
|
|
|
|
|
36
|
|
|
$query = $this->enableSso()->getCommandQuery($params); |
|
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
return $this->urlClient.'?loginUrl='.urlencode($query); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Generate Console URL for specified username owning the virtual machine. |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $username |
|
45
|
|
|
* @param string $domainId |
|
46
|
|
|
* @param string $virtualMachineId |
|
47
|
|
|
* @return string |
|
48
|
|
|
* @throws \InvalidArgumentEception |
|
49
|
|
|
*/ |
|
50
|
|
|
public function consoleUrl($username, $domainId, $virtualMachineId) |
|
51
|
|
|
{ |
|
52
|
|
|
if (is_null($this->urlConsole)) { |
|
|
|
|
|
|
53
|
|
|
throw new InvalidArgumentException( |
|
54
|
|
|
'Required options not defined: urlConsole' |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
// Prepare session. |
|
59
|
|
|
// Using the SSO (Single Sign On) key we can generate a sessionkey used for the console url. |
|
60
|
|
|
$command = 'login'; |
|
61
|
|
|
$params = [ |
|
62
|
|
|
'command' => $command, |
|
63
|
|
|
'username' => $username, |
|
64
|
|
|
'domainid' => $domainId, |
|
65
|
|
|
'timestamp' => round(microtime(true) * 1000), |
|
66
|
|
|
'response' => 'json', |
|
67
|
|
|
]; |
|
68
|
|
|
|
|
69
|
|
|
$base = $this->urlApi; |
|
|
|
|
|
|
70
|
|
|
$method = $this->getCommandMethod($command); |
|
|
|
|
|
|
71
|
|
|
$query = $this->enableSso()->getCommandQuery($params); |
|
|
|
|
|
|
72
|
|
|
$url = $this->appendQuery($base, $query); |
|
|
|
|
|
|
73
|
|
|
$request = $this->getRequest($method, $url); |
|
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
$login = $this->getResponse($request); |
|
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
// Prepare a signed request for the Console url. |
|
78
|
|
|
// Effectively this will be the console url, it won't be requested at the Cloudstack API. |
|
79
|
|
|
$params = [ |
|
80
|
|
|
'cmd' => 'access', |
|
81
|
|
|
'vm' => $virtualMachineId, |
|
82
|
|
|
'userid' => $login['loginresponse']['userid'], |
|
83
|
|
|
'sessionkey' => $login['loginresponse']['sessionkey'], |
|
84
|
|
|
'timestamp' => round(microtime(true) * 1000), |
|
85
|
|
|
'apikey' => $this->apiKey, |
|
|
|
|
|
|
86
|
|
|
]; |
|
87
|
|
|
|
|
88
|
|
|
$base = $this->urlConsole; |
|
89
|
|
|
$query = $this->getCommandQuery($params); |
|
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
return $this->appendQuery($base, $query); |
|
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: