ClientAreaHeadOutput.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxVPS Product developed. (26.03.19)
  4. * *
  5. *
  6. * CREATED BY MODULESGARDEN -> http://modulesgarden.com
  7. * CONTACT -> contact@modulesgarden.com
  8. *
  9. *
  10. * This software is furnished under a license and may be used and copied
  11. * only in accordance with the terms of such license and with the
  12. * inclusion of the above copyright notice. This software or any other
  13. * copies thereof may not be provided or otherwise made available to any
  14. * other person. No title to and ownership of the software is hereby
  15. * transferred.
  16. *
  17. *
  18. * ******************************************************************** */
  19. use Illuminate\Database\Capsule\Manager as DB;
  20. use ModulesGarden\ProxmoxAddon\App\Repositories\Vps\ProductConfigurationRepository;
  21. use ModulesGarden\ProxmoxAddon\App\Enum\Vps\ConfigurableOption;
  22. $hookManager->register(
  23. function ($vars)
  24. {
  25. if ($vars['filename'] != 'upgrade' || $_REQUEST['step'] != 2)
  26. {
  27. return;
  28. }
  29. try
  30. {
  31. if (!function_exists('ModuleBuildParams'))
  32. {
  33. require_once ROOTDIR . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . "modulefunctions.php";
  34. }
  35. global $smartyvalues;
  36. $displayUpgradePage = true;
  37. /* @var $request \ModulesGarden\Servers\ProxmoxVps\Core\Http\Request */
  38. $request = \ModulesGarden\Servers\ProxmoxVps\Core\Helper\sl("request");
  39. /**
  40. * @var \ModulesGarden\Servers\ProxmoxVps\Core\Lang\Lang $lang
  41. */
  42. $lang = \ModulesGarden\Servers\ProxmoxVps\Core\Helper\sl("lang");
  43. $serviceId = $request->get('id');
  44. $h = 'tblhosting';
  45. $p = 'tblproducts';
  46. $hosting = \WHMCS\Service\Service::select("{$h}.*")
  47. ->leftJoin($p, "{$p}.id", '=', "{$h}.packageid")
  48. ->where("{$h}.id", $serviceId)
  49. ->where("{$h}.userid", $request->getSession('uid'))
  50. ->where("{$p}.servertype", 'proxmoxVPS')
  51. ->first();
  52. if (!$hosting)
  53. {
  54. return;
  55. }
  56. // Disk Downsize Protection chosen product as smaller disk size than the current server disk size
  57. if ($request->get('type') && $request->get('pid') && $request->get('type') == "package")
  58. {
  59. $newProductId = $request->get('pid');
  60. $newProduct = new ProductConfigurationRepository($newProductId);
  61. $newDisk = $newProduct->getDiskSize();
  62. \ModulesGarden\ProxmoxAddon\App\Services\Utility::unitFormat($newDisk, "gb", 'bytes');
  63. //Current
  64. $product = new ProductConfigurationRepository($hosting->productId);
  65. $disk = $product->getDiskSize();
  66. \ModulesGarden\ProxmoxAddon\App\Services\Utility::unitFormat($disk, "gb", 'bytes');
  67. if ($newDisk < $disk)
  68. {
  69. $newSize = \ModulesGarden\ProxmoxAddon\App\Libs\Format::convertBytes($newDisk);
  70. $size = \ModulesGarden\ProxmoxAddon\App\Libs\Format::convertBytes($disk);
  71. $smartyvalues['promoerror'] = $lang->abtr("You cannot select a product which disk size is lower than the one you currently have.");
  72. $displayUpgradePage = false;
  73. }
  74. }
  75. else
  76. {
  77. if ($request->get('configoption'))
  78. { //chosen configurable option value for disk size is smaller than the current server disksize
  79. $configoptions = getCartConfigOptions($hosting->productId, $request->get('configoption'), $hosting->billingcycle);
  80. $params = \ModuleBuildParams($hosting->id);
  81. $product = new ProductConfigurationRepository($hosting->productId);
  82. if($params['configoptions'][ConfigurableOption::STORAGE]){
  83. list($storage,$disk) = explode(":", $params['configoptions'][ConfigurableOption::STORAGE],2);
  84. \ModulesGarden\ProxmoxAddon\App\Services\Utility::unitFormat($disk, $product->getDiskUnit(), 'bytes');
  85. }
  86. elseif ($params['configoptions'][ConfigurableOption::DISK_SIZE])
  87. {
  88. $disk = $params['configoptions'][ConfigurableOption::DISK_SIZE];
  89. \ModulesGarden\ProxmoxAddon\App\Services\Utility::unitFormat($disk, $product->getDiskUnit(), 'bytes');
  90. }
  91. else
  92. {
  93. $disk = $product->getDiskSize();
  94. \ModulesGarden\ProxmoxAddon\App\Services\Utility::unitFormat($disk, "gb", 'bytes');
  95. }
  96. //additional disks size
  97. if (isset($params['configoptions'][ConfigurableOption::ADDITIONAL_DISKS_SIZE]))
  98. {
  99. $additionalDisksSpace = $params['configoptions'][ConfigurableOption::ADDITIONAL_DISKS_SIZE];
  100. \ModulesGarden\ProxmoxAddon\App\Services\Utility::unitFormat($additionalDisksSpace, $product->getAdditionalDiskUnit(), 'bytes');
  101. }
  102. else
  103. {
  104. $additionalDisksSpace = $product->getAdditionalDiskSize();
  105. \ModulesGarden\ProxmoxAddon\App\Services\Utility::unitFormat($additionalDisksSpace, "gb", 'bytes');
  106. }
  107. foreach ($configoptions as $configoption)
  108. {
  109. if (isset($params['configoptions']['']) || $configoption['hidden'])
  110. {
  111. continue;
  112. }
  113. $moduleName = DB::table('tblproductconfigoptions')->where('id', $configoption['id'])->value('optionname');
  114. if (preg_match('/\|/', $moduleName))
  115. {
  116. $ex = explode("|", $moduleName);
  117. $moduleName = $ex[0];
  118. }
  119. if (!$moduleName || !isset($params['configoptions'][$moduleName]))
  120. {
  121. continue;
  122. }
  123. if ($configoption['selectedqty'] != 0)
  124. {
  125. $params['configoptions'][$moduleName] = $configoption['selectedqty'];
  126. continue;
  127. }
  128. foreach ($configoption['options'] as $option)
  129. {
  130. if ($configoption['selectedvalue'] == $option['id'])
  131. {
  132. if (preg_match('/\|/', $option['rawName']))
  133. {
  134. $ex = explode("|", $option['rawName']);
  135. $option['rawName'] = $ex[0];
  136. }
  137. $params['configoptions'][$moduleName] = $option['rawName'];
  138. break;
  139. }
  140. }
  141. }
  142. if($params['configoptions'][ConfigurableOption::STORAGE]){
  143. list($storage,$newDisk) = explode(":", $params['configoptions'][ConfigurableOption::STORAGE],2);
  144. \ModulesGarden\ProxmoxAddon\App\Services\Utility::unitFormat($newDisk, $product->getDiskUnit(), 'bytes');
  145. }
  146. elseif ($params['configoptions'][ConfigurableOption::DISK_SIZE])
  147. {
  148. $newDisk = $params['configoptions'][ConfigurableOption::DISK_SIZE];
  149. \ModulesGarden\ProxmoxAddon\App\Services\Utility::unitFormat($newDisk, $product->getDiskUnit(), 'bytes');
  150. }
  151. else
  152. {
  153. $newDisk = $product->getDiskSize();
  154. \ModulesGarden\ProxmoxAddon\App\Services\Utility::unitFormat($newDisk, "gb", 'bytes');
  155. }
  156. if ($newDisk < $disk)
  157. {
  158. $newSize = \ModulesGarden\ProxmoxAddon\App\Libs\Format::convertBytes($newDisk);
  159. $size = \ModulesGarden\ProxmoxAddon\App\Libs\Format::convertBytes($disk);
  160. $smartyvalues['promoerror'] = sprintf($lang->abtr("You are not able to set %s of Disk Size. Current Disk Size: %s. Disk downgrading is not supported."), $newSize, $size);
  161. $displayUpgradePage = false;
  162. }
  163. //Additonal Disk Downsize Protection
  164. if (isset($params['configoptions'][ConfigurableOption::ADDITIONAL_DISKS_SIZE]))
  165. {
  166. $newAdditionalDisksSpace = $params['configoptions'][ConfigurableOption::ADDITIONAL_DISKS_SIZE];
  167. \ModulesGarden\ProxmoxAddon\App\Services\Utility::unitFormat($newAdditionalDisksSpace, $product->getAdditionalDiskUnit(), 'bytes');
  168. }
  169. else
  170. {
  171. $newAdditionalDisksSpace = $product->getAdditionalDiskSize();
  172. \ModulesGarden\ProxmoxAddon\App\Services\Utility::unitFormat($newAdditionalDisksSpace, "gb", 'bytes');
  173. }
  174. if($newAdditionalDisksSpace < $additionalDisksSpace && $params['customfields']['node'] && $params['customfields']['vmid']){
  175. $api = \MGProvision\Proxmox\v2\Factory::api($params);
  176. $vm = \MGProvision\Proxmox\v2\Factory::vmVps($product, $params);
  177. $vm->setApi($api);
  178. $used = 0;
  179. if($vm instanceof \MGProvision\Proxmox\v2\models\Lxc){
  180. $used = $vm->getMounPoints()->additionalSizeToBytes();
  181. }elseif ($vm instanceof \MGProvision\Proxmox\v2\models\Kvm){
  182. foreach ($vm->getHardDisks() as $hd){
  183. if($hd->isMaster()){
  184. continue;
  185. }
  186. $used += $hd->getBytes();
  187. }
  188. }
  189. if($used > $newAdditionalDisksSpace){
  190. $smartyvalues['promoerror'] = $lang->abtr("Disk downgrading is not possible until you remove additional disks.");
  191. $displayUpgradePage = false;
  192. }
  193. }
  194. }
  195. }
  196. $isLoadBalancer = \ModulesGarden\ProxmoxAddon\App\Models\ProductConfiguration::ofProductId($hosting->productId)->ofSetting("loadBalancer")->ofValue('"on"')->count();
  197. //LoadBalancer is enabled?
  198. if ($isLoadBalancer && $displayUpgradePage)
  199. {
  200. $product = new ProductConfigurationRepository($hosting->productId);
  201. //LoadBalancer on upgrade
  202. $onUpgrade = $product->getLoadBalancerOnUpgrade();
  203. if ($onUpgrade == "none" || !$onUpgrade)
  204. {
  205. return;
  206. }
  207. $configoptions = getCartConfigOptions($hosting->productId, $request->get('configoption'), $hosting->billingcycle);
  208. $params = \ModuleBuildParams($hosting->id);
  209. foreach ($configoptions as $configoption)
  210. {
  211. if (isset($params['configoptions']['']) || $configoption['hidden'])
  212. {
  213. continue;
  214. }
  215. $moduleName = DB::table('tblproductconfigoptions')->where('id', $configoption['id'])->value('optionname');
  216. if (preg_match('/\|/', $moduleName))
  217. {
  218. $ex = explode("|", $moduleName);
  219. $moduleName = $ex[0];
  220. }
  221. if (!$moduleName || !isset($params['configoptions'][$moduleName]))
  222. {
  223. continue;
  224. }
  225. if ($configoption['selectedqty'] != 0)
  226. {
  227. $params['configoptions'][$moduleName] = $configoption['selectedqty'];
  228. continue;
  229. }
  230. foreach ($configoption['options'] as $option)
  231. {
  232. if ($configoption['selectedvalue'] == $option['id'])
  233. {
  234. if (preg_match('/\|/', $option['rawName']))
  235. {
  236. $ex = explode("|", $option['rawName']);
  237. $option['rawName'] = $ex[0];
  238. }
  239. $params['configoptions'][$moduleName] = $option['rawName'];
  240. break;
  241. }
  242. }
  243. }
  244. $loadBalancer = new \ModulesGarden\ProxmoxAddon\App\Services\LoadBalancerService($params['serverid']);
  245. $api = \MGProvision\Proxmox\v2\Api::whmcsFactory($params);
  246. $api->debug(\ModulesGarden\ProxmoxAddon\App\Models\ModuleSettings::isDebug());
  247. $loadBalancer->setApi($api);
  248. if ($product->isQemu())
  249. {
  250. $socket = $params['configoptions'][ConfigurableOption::SOCKETS] ? $params['configoptions'][ConfigurableOption::SOCKETS] : $product->getSockets();
  251. $cores = $params['configoptions'][ConfigurableOption::CORES_PER_SOCKET] ? $params['configoptions'][ConfigurableOption::CORES_PER_SOCKET] : $product->getCores();
  252. $cpu = $socket * $cores;
  253. }
  254. else
  255. {
  256. if ($product->isLxc())
  257. {
  258. $cpu = $params['configoptions'][ConfigurableOption::CORES] ? $params['configoptions'][ConfigurableOption::CORES] : $product->getCores();
  259. }
  260. }
  261. //disk
  262. if($params['configoptions'][ConfigurableOption::STORAGE]){
  263. list($storage,$newDisk) = explode(":", $params['configoptions'][ConfigurableOption::STORAGE],2);
  264. \ModulesGarden\ProxmoxAddon\App\Services\Utility::unitFormat($newDisk, $product->getDiskUnit(), 'bytes');
  265. }
  266. elseif ($params['configoptions'][ConfigurableOption::DISK_SIZE])
  267. {
  268. $newDisk = $params['configoptions'][ConfigurableOption::DISK_SIZE];
  269. \ModulesGarden\ProxmoxAddon\App\Services\Utility::unitFormat($newDisk, $product->getDiskUnit(), 'bytes');
  270. }
  271. else
  272. {
  273. $newDisk = $product->getDiskSize();
  274. \ModulesGarden\ProxmoxAddon\App\Services\Utility::unitFormat($newDisk, "gb", 'bytes');
  275. }
  276. //ram
  277. if ($params['configoptions'][ConfigurableOption::MEMORY])
  278. {
  279. $ram = $params['configoptions'][ConfigurableOption::MEMORY];
  280. \ModulesGarden\ProxmoxAddon\App\Services\Utility::unitFormat($ram, $product->getMemoryUnit(), 'bytes');
  281. }
  282. else
  283. {
  284. $ram = $product->getMemory();
  285. \ModulesGarden\ProxmoxAddon\App\Services\Utility::unitFormat($ram, "mb", 'bytes');
  286. }
  287. $loadBalancer->setExcludeVmid($params['customfields']['vmid']);
  288. //Upgrade on current node
  289. if ($onUpgrade == "block")
  290. {
  291. $loadBalancerNodes = $loadBalancer->findByNode($params['customfields']['node']);
  292. $loadBalancerNodes = $loadBalancerNodes->findByRam($ram)
  293. ->findByCpu($cpu)
  294. ->findByDisk($disk);
  295. if (!$loadBalancerNodes->isEmpty())
  296. {
  297. return;
  298. }
  299. $smartyvalues['promoerror'] = $lang->abtr("You cannot upgrade there no free resurces on our server");
  300. $displayUpgradePage = false;
  301. }
  302. else
  303. {
  304. if ($onUpgrade == "migrate")
  305. {
  306. try
  307. {
  308. $loadBalancerNodes = $loadBalancer->findByNode($params['customfields']['node']);
  309. $loadBalancerNodes = $loadBalancerNodes->findByRam($ram)
  310. ->findByCpu($cpu)
  311. ->findByDisk($disk);
  312. if (!$loadBalancerNodes->isEmpty())
  313. {
  314. return;
  315. }
  316. $loadBalancerNodes = $loadBalancer->findByVmCreate();
  317. $nodesForUser = $loadBalancer->findNotUser($params['userid']);
  318. if (!$nodesForUser->isEmpty())
  319. {
  320. $loadBalancerNodes = $nodesForUser;
  321. }
  322. $targetNode = $loadBalancerNodes->findByRam($ram)
  323. ->findByCpu($cpu)
  324. ->findByDisk($disk)
  325. ->findByVms(1)
  326. ->nodeLowLoad();
  327. $smartyvalues['promoerror'] = $lang->abtr("Your machine will be migrated to another server");
  328. }
  329. catch (\Exception $ex)
  330. {
  331. $smartyvalues['promoerror'] = $lang->abtr("You cannot migrate there no free resurces on our server");
  332. $displayUpgradePage = false;
  333. }
  334. }
  335. }
  336. }
  337. if (!$displayUpgradePage)
  338. {
  339. return '<script type="text/javascript">
  340. $(document).ready(function(){
  341. //six
  342. if($(".main-content .table").size()){
  343. $(".main-content .table").hide();
  344. $(".main-content .alert-info").hide();
  345. $(".main-content .row").hide();
  346. $(".main-content .form-group .btn").hide();
  347. var form = $(".container .row .panel .panel-footer form").clone();
  348. $(".main-content .form-group.text-center").append(form );
  349. $(".main-content .form-group.text-center .btn-block ").removeClass("btn-block");
  350. }
  351. //Control
  352. if($("#main-content .col-md-12").size()){
  353. $("#main-content .col-md-12").hide();
  354. }
  355. //twenty one
  356. if($(".card .card-body .alert-info").size()){
  357. $(".card .card-body .alert-info").hide();
  358. }
  359. if($(".card .card-body .table.table-striped").size()){
  360. $(".card .card-body .table.table-striped").hide();
  361. }
  362. if($(".card .card-body .row").size()){
  363. $(".card .card-body .row").hide();
  364. }
  365. if($(".card .card-body .form-group").size()){
  366. $(".card .card-body .form-group").hide();
  367. }
  368. });
  369. </script>';
  370. }
  371. }
  372. catch (\Exception $ex)
  373. {
  374. logModuleCall(
  375. 'ProxmoxVPS',
  376. __FUNCTION__,
  377. $vars,
  378. $ex->getMessage(),
  379. $ex->getTraceAsString()
  380. );
  381. }
  382. }
  383. );