1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Server module for HiPanel |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/hipanel-module-server |
6
|
|
|
* @package hipanel-module-server |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hipanel\modules\server\helpers; |
12
|
|
|
|
13
|
|
|
use hipanel\models\Ref; |
14
|
|
|
use hipanel\modules\finance\logic\Calculator; |
15
|
|
|
use hipanel\modules\finance\models\Tariff; |
16
|
|
|
use hipanel\modules\server\models\OpenvzPackage; |
17
|
|
|
use hipanel\modules\server\models\Osimage; |
18
|
|
|
use hipanel\modules\server\models\Package; |
19
|
|
|
use hipanel\modules\server\models\Server; |
20
|
|
|
use hipanel\modules\server\models\ServerUse; |
21
|
|
|
use Yii; |
22
|
|
|
use yii\caching\DbDependency; |
23
|
|
|
use yii\caching\ExpressionDependency; |
24
|
|
|
use yii\helpers\ArrayHelper; |
25
|
|
|
use yii\web\NotFoundHttpException; |
26
|
|
|
use yii\web\UnprocessableEntityHttpException; |
27
|
|
|
use yii\web\User; |
28
|
|
|
|
29
|
|
|
class ServerHelper |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Method groups array of [[ServerUse]] in order to use it in charts. |
33
|
|
|
* @param ServerUse[] $uses |
34
|
|
|
* @return array of two items: |
35
|
|
|
* 0 - labels (like months) |
36
|
|
|
* 1 - values (value for each month) |
37
|
|
|
*/ |
38
|
|
|
public static function groupUsesForChart($uses) |
39
|
|
|
{ |
40
|
|
|
$labels = []; |
41
|
|
|
$data = []; |
42
|
|
|
|
43
|
|
|
ArrayHelper::multisort($uses, 'date'); |
44
|
|
|
|
45
|
|
|
foreach ($uses as $use) { |
46
|
|
|
/** @var ServerUse $use */ |
47
|
|
|
$labels[$use->date] = $use; |
|
|
|
|
48
|
|
|
$data[$use->type][] = $use->getDisplayAmount(); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
foreach ($labels as $date => $use) { |
52
|
|
|
$labels[$date] = $use->getDisplayDate(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return [$labels, $data]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Gets array of [[Osimage]] for $type. |
60
|
|
|
* |
61
|
|
|
* @param string $type |
62
|
|
|
* @return Osimage[]|null |
63
|
|
|
*/ |
64
|
|
|
public static function getOsimages($type = null) |
65
|
|
|
{ |
66
|
|
|
return Yii::$app->cache->getOrSet([__METHOD__, $type], function () use ($type) { |
67
|
|
|
return Osimage::find()->andFilterWhere(['type' => $type])->all(); |
68
|
|
|
}, 3600); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Regroups array of $images into 3 arrays of unique OS vendors (Ubuntu, FreeBSD, ...), |
73
|
|
|
* OSeses (Ubuntu 16.04, FreeBSD 10.3, ...) and softpacks (LAMP, no, ...). |
74
|
|
|
* |
75
|
|
|
* @param Osimage[] $images |
76
|
|
|
* @param bool $ispSupported whether ISP manager panel is supported |
77
|
|
|
* @return array of 3 elements: |
78
|
|
|
* 0 - vendors, |
79
|
|
|
* 1 - oses, |
80
|
|
|
* 2 - softpacks |
81
|
|
|
*/ |
82
|
|
|
public static function groupOsimages($images, $ispSupported = false) |
83
|
|
|
{ |
84
|
|
|
$softpacks = []; |
85
|
|
|
$oses = []; |
86
|
|
|
$vendors = []; |
87
|
|
|
foreach ($images as $image) { |
88
|
|
|
$os = $image->os; |
89
|
|
|
$name = $image->getFullOsName(); |
90
|
|
|
$panel = $image->getPanelName(); |
91
|
|
|
$system = $image->getFullOsName(''); |
92
|
|
|
$softpack_name = $image->getSoftPackName(); |
93
|
|
|
$softpack = $image->getSoftPack(); |
94
|
|
|
|
95
|
|
|
if (!array_key_exists($system, $oses)) { |
96
|
|
|
$vendors[$os]['name'] = $os; |
97
|
|
|
$vendors[$os]['oses'][$system] = $name; |
98
|
|
|
$oses[$system] = ['vendor' => $os, 'name' => $name]; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if ($panel !== 'isp' || ($panel === 'isp' && $ispSupported)) { |
102
|
|
|
$data = [ |
103
|
|
|
'name' => $softpack_name, |
104
|
|
|
'description' => preg_replace('/^ISPmanager - /', '', $softpack['description']), |
105
|
|
|
'osimage' => $image->osimage, |
106
|
|
|
]; |
107
|
|
|
|
108
|
|
|
if ($softpack['soft']) { |
109
|
|
|
$html_desc = []; |
110
|
|
|
foreach ($softpack['soft'] as $soft => $soft_info) { |
111
|
|
|
$soft_info['description'] = preg_replace('/,([^\s])/', ', $1', $soft_info['description']); |
112
|
|
|
|
113
|
|
|
$html_desc[] = "<b>{$soft_info['name']} {$soft_info['version']}</b>: <i>{$soft_info['description']}</i>"; |
114
|
|
|
$data['soft'][$soft] = [ |
115
|
|
|
'name' => $soft_info['name'], |
116
|
|
|
'version' => $soft_info['version'], |
117
|
|
|
'description' => $soft_info['description'], |
118
|
|
|
]; |
119
|
|
|
} |
120
|
|
|
$data['html_desc'] = implode('<br />', $html_desc); |
121
|
|
|
} |
122
|
|
|
$oses[$system]['panel'][$panel]['softpack'][$softpack_name] = $data; |
123
|
|
|
$softpacks[$panel][$softpack_name] = $data; |
124
|
|
|
} else { |
125
|
|
|
$oses[$system]['panel'][$panel] = false; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
foreach ($oses as $system => $os) { |
130
|
|
|
$delete = true; |
131
|
|
|
foreach ($os['panel'] as $panel => $info) { |
132
|
|
|
if ($info !== false) { |
133
|
|
|
$delete = false; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
if ($delete) { |
137
|
|
|
unset($vendors[$os['vendor']]['oses'][$system]); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return compact('vendors', 'oses', 'softpacks'); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return array |
146
|
|
|
*/ |
147
|
|
|
public static function getPanels() |
148
|
|
|
{ |
149
|
|
|
return Ref::getList('type,panel', 'hipanel:server:panel', []); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param string $type (svds|ovds) |
154
|
|
|
* @param integer $tariff_id |
155
|
|
|
* @return Package|array |
156
|
|
|
* @throws UnprocessableEntityHttpException |
157
|
|
|
* @throws NotFoundHttpException |
158
|
|
|
*/ |
159
|
|
|
public static function getAvailablePackages($type = null, $tariff_id = null) |
160
|
|
|
{ |
161
|
|
|
$cacheKeys = [__METHOD__, 'tariffs', Yii::$app->user->id, $type, $tariff_id]; |
162
|
|
|
/** @var Tariff[] $tariffs */ |
163
|
|
|
$tariffs = Yii::$app->getCache()->getOrSet($cacheKeys, function () use ($type, $tariff_id) { |
164
|
|
|
return Tariff::find() |
165
|
|
|
->action('get-available-info') |
166
|
|
|
->details() |
167
|
|
|
->andWhere(['seller' => Yii::$app->params['user.seller']]) |
168
|
|
|
->andFilterWhere(['id' => $tariff_id, 'type' => $type]) |
169
|
|
|
->all(); |
170
|
|
|
}, 3600); |
171
|
|
|
|
172
|
|
|
$cacheKeys = [__METHOD__, 'tariffs', 'packages', Yii::$app->user->id, $type, $tariff_id]; |
173
|
|
|
/** @var Package[] $packages */ |
174
|
|
|
$packages = Yii::$app->getCache()->getOrSet($cacheKeys, function () use ($tariffs) { |
175
|
|
|
$calculator = new Calculator($tariffs); |
176
|
|
|
|
177
|
|
|
$packages = []; |
178
|
|
|
foreach ($tariffs as $tariff) { |
179
|
|
|
$calculation = $calculator->getCalculation($tariff->id); |
180
|
|
|
$packages[] = Yii::createObject([ |
181
|
|
|
'class' => static::buildPackageClass($tariff), |
182
|
|
|
'tariff' => $tariff, |
183
|
|
|
'calculation' => $calculation, |
184
|
|
|
]); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return $packages; |
188
|
|
|
}); |
189
|
|
|
|
190
|
|
|
ArrayHelper::multisort($packages, 'price', SORT_ASC, SORT_NUMERIC); |
191
|
|
|
|
192
|
|
|
if (empty($packages)) { |
193
|
|
|
throw new NotFoundHttpException('Requested tariff is not available'); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
if (isset($tariff_id) && !is_array($tariff_id)) { |
197
|
|
|
return reset($packages); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
return $packages; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param $tariff |
205
|
|
|
* @return mixed |
206
|
|
|
*/ |
207
|
|
|
public static function buildPackageClass($tariff) |
208
|
|
|
{ |
209
|
|
|
if ($tariff->type === Tariff::TYPE_OPENVZ) { |
210
|
|
|
return OpenvzPackage::class; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
return Package::class; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Getting used current user server types |
218
|
|
|
* |
219
|
|
|
* @return array |
220
|
|
|
*/ |
221
|
|
|
public static function getUsedTypes(): array |
222
|
|
|
{ |
223
|
|
|
$types = []; |
224
|
|
|
$usedTypes = Server::perform('get-used-types'); |
225
|
|
|
array_walk($usedTypes, static function ($row) use (&$types): void { |
226
|
|
|
$types[] = $row['type']; |
227
|
|
|
}); |
228
|
|
|
|
229
|
|
|
return $types; |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|