|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sanchescom\WiFi\System\Windows\Profile; |
|
4
|
|
|
|
|
5
|
|
|
use Sanchescom\WiFi\System\Windows\Network; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class Service. |
|
9
|
|
|
*/ |
|
10
|
|
|
class Service |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var string |
|
14
|
|
|
*/ |
|
15
|
|
|
protected static $tmpFolderName = 'tmp'; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected static $templateFolderName = 'templates'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected static $fileNamePostfix = 'PersonalProfileTemplate.xml'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var Network |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $network; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Service constructor. |
|
34
|
|
|
* |
|
35
|
|
|
* @param Network $network |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct(Network $network) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->network = $network; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param string $password |
|
44
|
|
|
* |
|
45
|
|
|
* @return bool |
|
46
|
|
|
*/ |
|
47
|
|
|
public function create(string $password): bool |
|
48
|
|
|
{ |
|
49
|
|
|
return file_put_contents( |
|
|
|
|
|
|
50
|
|
|
$this->getTmpProfileFileName(), |
|
51
|
|
|
$this->renderTemplate($password) |
|
52
|
|
|
); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Delete tmp profile file created for chosen network. |
|
57
|
|
|
*/ |
|
58
|
|
|
public function delete(): bool |
|
59
|
|
|
{ |
|
60
|
|
|
$tmpProfile = $this->getTmpProfileFileName(); |
|
61
|
|
|
|
|
62
|
|
|
if (file_exists($tmpProfile)) { |
|
63
|
|
|
return unlink($tmpProfile); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return false; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @return string |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getTmpProfileFileName(): string |
|
73
|
|
|
{ |
|
74
|
|
|
return __DIR__ |
|
75
|
|
|
.DIRECTORY_SEPARATOR |
|
76
|
|
|
.static::$tmpFolderName |
|
77
|
|
|
.DIRECTORY_SEPARATOR |
|
78
|
|
|
.$this->network->ssid |
|
79
|
|
|
.'.xml'; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param string $password |
|
84
|
|
|
* |
|
85
|
|
|
* @return mixed |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function renderTemplate(string $password): string |
|
88
|
|
|
{ |
|
89
|
|
|
$content = file_get_contents($this->getTemplateProfileFileName()) ?: ''; |
|
90
|
|
|
|
|
91
|
|
|
return str_replace( |
|
92
|
|
|
[ |
|
93
|
|
|
'{ssid}', |
|
94
|
|
|
'{hex}', |
|
95
|
|
|
'{key}', |
|
96
|
|
|
], |
|
97
|
|
|
[ |
|
98
|
|
|
$this->network->ssid, |
|
99
|
|
|
$this->ssidToHex(), |
|
100
|
|
|
$password, |
|
101
|
|
|
], |
|
102
|
|
|
$content |
|
103
|
|
|
); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @return string |
|
108
|
|
|
*/ |
|
109
|
|
|
protected function getTemplateProfileFileName(): string |
|
110
|
|
|
{ |
|
111
|
|
|
if ($this->network->isWpa2()) { |
|
112
|
|
|
$security = 'WPA2'; |
|
113
|
|
|
} elseif ($this->network->isWpa()) { |
|
114
|
|
|
$security = 'WPA'; |
|
115
|
|
|
} elseif ($this->network->isWep()) { |
|
116
|
|
|
$security = 'WEP'; |
|
117
|
|
|
} else { |
|
118
|
|
|
$security = 'Unknown'; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return __DIR__ |
|
122
|
|
|
.DIRECTORY_SEPARATOR |
|
123
|
|
|
.static::$templateFolderName |
|
124
|
|
|
.DIRECTORY_SEPARATOR |
|
125
|
|
|
.$security |
|
126
|
|
|
.'-' |
|
127
|
|
|
.static::$fileNamePostfix; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @return string |
|
132
|
|
|
*/ |
|
133
|
|
|
protected function ssidToHex(): string |
|
134
|
|
|
{ |
|
135
|
|
|
$ssid = $this->network->ssid; |
|
136
|
|
|
$len = strlen($ssid); |
|
137
|
|
|
$hex = ''; |
|
138
|
|
|
|
|
139
|
|
|
for ($i = 0; $i < $len; $i++) { |
|
140
|
|
|
$hex .= substr('0'.dechex(ord($ssid[$i])), -2); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
return strtoupper($hex); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|