ClientAreaHeadOutput.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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\Servers\ProxmoxVps\App\Enum\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::DISK_SIZE])
  83. {
  84. $disk = $params['configoptions'][ConfigurableOption::DISK_SIZE];
  85. \ModulesGarden\ProxmoxAddon\App\Services\Utility::unitFormat($disk, $product->getDiskUnit(), 'bytes');
  86. }
  87. else
  88. {
  89. $disk = $product->getDiskSize();
  90. \ModulesGarden\ProxmoxAddon\App\Services\Utility::unitFormat($disk, "gb", 'bytes');
  91. }
  92. //additional disks size
  93. if (isset($params['configoptions'][ConfigurableOption::ADDITIONAL_DISKS_SIZE]))
  94. {
  95. $additionalDisksSpace = $params['configoptions'][ConfigurableOption::ADDITIONAL_DISKS_SIZE];
  96. \ModulesGarden\ProxmoxAddon\App\Services\Utility::unitFormat($additionalDisksSpace, $product->getAdditionalDiskUnit(), 'bytes');
  97. }
  98. else
  99. {
  100. $additionalDisksSpace = $product->getAdditionalDiskSize();
  101. \ModulesGarden\ProxmoxAddon\App\Services\Utility::unitFormat($additionalDisksSpace, "gb", 'bytes');
  102. }
  103. foreach ($configoptions as $configoption)
  104. {
  105. if (isset($params['configoptions']['']) || $configoption['hidden'])
  106. {
  107. continue;
  108. }
  109. $moduleName = DB::table('tblproductconfigoptions')->where('id', $configoption['id'])->value('optionname');
  110. if (preg_match('/\|/', $moduleName))
  111. {
  112. $ex = explode("|", $moduleName);
  113. $moduleName = $ex[0];
  114. }
  115. if (!$moduleName || !isset($params['configoptions'][$moduleName]))
  116. {
  117. continue;
  118. }
  119. if ($configoption['selectedqty'] != 0)
  120. {
  121. $params['configoptions'][$moduleName] = $configoption['selectedqty'];
  122. continue;
  123. }
  124. foreach ($configoption['options'] as $option)
  125. {
  126. if ($configoption['selectedvalue'] == $option['id'])
  127. {
  128. if (preg_match('/\|/', $option['rawName']))
  129. {
  130. $ex = explode("|", $option['rawName']);
  131. $option['rawName'] = $ex[0];
  132. }
  133. $params['configoptions'][$moduleName] = $option['rawName'];
  134. break;
  135. }
  136. }
  137. }
  138. if ($params['configoptions'][ConfigurableOption::DISK_SIZE])
  139. {
  140. $newDisk = $params['configoptions'][ConfigurableOption::DISK_SIZE];
  141. \ModulesGarden\ProxmoxAddon\App\Services\Utility::unitFormat($newDisk, $product->getDiskUnit(), 'bytes');
  142. }
  143. else
  144. {
  145. $newDisk = $product->getDiskSize();
  146. \ModulesGarden\ProxmoxAddon\App\Services\Utility::unitFormat($newDisk, "gb", 'bytes');
  147. }
  148. if ($newDisk < $disk)
  149. {
  150. $newSize = \ModulesGarden\ProxmoxAddon\App\Libs\Format::convertBytes($newDisk);
  151. $size = \ModulesGarden\ProxmoxAddon\App\Libs\Format::convertBytes($disk);
  152. $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);
  153. $displayUpgradePage = false;
  154. }
  155. //Additonal Disk Downsize Protection
  156. if (isset($params['configoptions'][ConfigurableOption::ADDITIONAL_DISKS_SIZE]))
  157. {
  158. $newAdditionalDisksSpace = $params['configoptions'][ConfigurableOption::ADDITIONAL_DISKS_SIZE];
  159. \ModulesGarden\ProxmoxAddon\App\Services\Utility::unitFormat($newAdditionalDisksSpace, $product->getAdditionalDiskUnit(), 'bytes');
  160. }
  161. else
  162. {
  163. $newAdditionalDisksSpace = $product->getAdditionalDiskSize();
  164. \ModulesGarden\ProxmoxAddon\App\Services\Utility::unitFormat($newAdditionalDisksSpace, "gb", 'bytes');
  165. }
  166. if($newAdditionalDisksSpace < $additionalDisksSpace && $params['customfields']['node'] && $params['customfields']['vmid']){
  167. $api = \MGProvision\Proxmox\v2\Factory::api($params);
  168. $vm = \MGProvision\Proxmox\v2\Factory::vmVps($product, $params);
  169. $vm->setApi($api);
  170. $used = 0;
  171. if($vm instanceof \MGProvision\Proxmox\v2\models\Lxc){
  172. $used = $vm->getMounPoints()->additionalSizeToBytes();
  173. }elseif ($vm instanceof \MGProvision\Proxmox\v2\models\Kvm){
  174. foreach ($vm->getHardDisks() as $hd){
  175. if($hd->isMaster()){
  176. continue;
  177. }
  178. $used += $hd->getBytes();
  179. }
  180. }
  181. if($used > $newAdditionalDisksSpace){
  182. $smartyvalues['promoerror'] = $lang->abtr("Disk downgrading is not possible until you remove additional disks.");
  183. $displayUpgradePage = false;
  184. }
  185. }
  186. }
  187. }
  188. $isLoadBalancer = \ModulesGarden\ProxmoxAddon\App\Models\ProductConfiguration::ofProductId($hosting->productId)->ofSetting("loadBalancer")->ofValue('"on"')->count();
  189. //LoadBalancer is enabled?
  190. if ($isLoadBalancer && $displayUpgradePage)
  191. {
  192. $product = new ProductConfigurationRepository($hosting->productId);
  193. //LoadBalancer on upgrade
  194. $onUpgrade = $product->getLoadBalancerOnUpgrade();
  195. if ($onUpgrade == "none" || !$onUpgrade)
  196. {
  197. return;
  198. }
  199. $configoptions = getCartConfigOptions($hosting->productId, $request->get('configoption'), $hosting->billingcycle);
  200. $params = \ModuleBuildParams($hosting->id);
  201. foreach ($configoptions as $configoption)
  202. {
  203. if (isset($params['configoptions']['']) || $configoption['hidden'])
  204. {
  205. continue;
  206. }
  207. $moduleName = DB::table('tblproductconfigoptions')->where('id', $configoption['id'])->value('optionname');
  208. if (preg_match('/\|/', $moduleName))
  209. {
  210. $ex = explode("|", $moduleName);
  211. $moduleName = $ex[0];
  212. }
  213. if (!$moduleName || !isset($params['configoptions'][$moduleName]))
  214. {
  215. continue;
  216. }
  217. if ($configoption['selectedqty'] != 0)
  218. {
  219. $params['configoptions'][$moduleName] = $configoption['selectedqty'];
  220. continue;
  221. }
  222. foreach ($configoption['options'] as $option)
  223. {
  224. if ($configoption['selectedvalue'] == $option['id'])
  225. {
  226. if (preg_match('/\|/', $option['rawName']))
  227. {
  228. $ex = explode("|", $option['rawName']);
  229. $option['rawName'] = $ex[0];
  230. }
  231. $params['configoptions'][$moduleName] = $option['rawName'];
  232. break;
  233. }
  234. }
  235. }
  236. $loadBalancer = new \ModulesGarden\ProxmoxAddon\App\Services\LoadBalancerService($params['serverid']);
  237. $api = \MGProvision\Proxmox\v2\Api::whmcsFactory($params);
  238. $api->debug(\ModulesGarden\ProxmoxAddon\App\Models\ModuleSettings::isDebug());
  239. $loadBalancer->setApi($api);
  240. if ($product->isQemu())
  241. {
  242. $socket = $params['configoptions'][ConfigurableOption::SOCKETS] ? $params['configoptions'][ConfigurableOption::SOCKETS] : $product->getSockets();
  243. $cores = $params['configoptions'][ConfigurableOption::CORES_PER_SOCKET] ? $params['configoptions'][ConfigurableOption::CORES_PER_SOCKET] : $product->getCores();
  244. $cpu = $socket * $cores;
  245. }
  246. else
  247. {
  248. if ($product->isLxc())
  249. {
  250. $cpu = $params['configoptions'][ConfigurableOption::CORES] ? $params['configoptions'][ConfigurableOption::CORES] : $product->getCores();
  251. }
  252. }
  253. //disk
  254. if ($params['configoptions'][ConfigurableOption::DISK_SIZE])
  255. {
  256. $newDisk = $params['configoptions'][ConfigurableOption::DISK_SIZE];
  257. \ModulesGarden\ProxmoxAddon\App\Services\Utility::unitFormat($newDisk, $product->getDiskUnit(), 'bytes');
  258. }
  259. else
  260. {
  261. $newDisk = $product->getDiskSize();
  262. \ModulesGarden\ProxmoxAddon\App\Services\Utility::unitFormat($newDisk, "gb", 'bytes');
  263. }
  264. //ram
  265. if ($params['configoptions'][ConfigurableOption::MEMORY])
  266. {
  267. $ram = $params['configoptions'][ConfigurableOption::MEMORY];
  268. \ModulesGarden\ProxmoxAddon\App\Services\Utility::unitFormat($ram, $product->getMemoryUnit(), 'bytes');
  269. }
  270. else
  271. {
  272. $ram = $product->getMemory();
  273. \ModulesGarden\ProxmoxAddon\App\Services\Utility::unitFormat($ram, "mb", 'bytes');
  274. }
  275. $loadBalancer->setExcludeVmid($params['customfields']['vmid']);
  276. //Upgrade on current node
  277. if ($onUpgrade == "block")
  278. {
  279. $loadBalancerNodes = $loadBalancer->findByNode($params['customfields']['node']);
  280. $loadBalancerNodes = $loadBalancerNodes->findByRam($ram)
  281. ->findByCpu($cpu)
  282. ->findByDisk($disk);
  283. if (!$loadBalancerNodes->isEmpty())
  284. {
  285. return;
  286. }
  287. $smartyvalues['promoerror'] = $lang->abtr("You cannot upgrade there no free resurces on our server");
  288. $displayUpgradePage = false;
  289. }
  290. else
  291. {
  292. if ($onUpgrade == "migrate")
  293. {
  294. try
  295. {
  296. $loadBalancerNodes = $loadBalancer->findByNode($params['customfields']['node']);
  297. $loadBalancerNodes = $loadBalancerNodes->findByRam($ram)
  298. ->findByCpu($cpu)
  299. ->findByDisk($disk);
  300. if (!$loadBalancerNodes->isEmpty())
  301. {
  302. return;
  303. }
  304. $loadBalancerNodes = $loadBalancer->findByVmCreate();
  305. $nodesForUser = $loadBalancer->findNotUser($params['userid']);
  306. if (!$nodesForUser->isEmpty())
  307. {
  308. $loadBalancerNodes = $nodesForUser;
  309. }
  310. $targetNode = $loadBalancerNodes->findByRam($ram)
  311. ->findByCpu($cpu)
  312. ->findByDisk($disk)
  313. ->findByVms(1)
  314. ->nodeLowLoad();
  315. $smartyvalues['promoerror'] = $lang->abtr("Your machine will be migrated to another server");
  316. }
  317. catch (\Exception $ex)
  318. {
  319. $smartyvalues['promoerror'] = $lang->abtr("You cannot migrate there no free resurces on our server");
  320. $displayUpgradePage = false;
  321. }
  322. }
  323. }
  324. }
  325. if (!$displayUpgradePage)
  326. {
  327. return '<script type="text/javascript">
  328. $(document).ready(function(){
  329. //six
  330. if($(".main-content .table").size()){
  331. $(".main-content .table").hide();
  332. $(".main-content .alert-info").hide();
  333. $(".main-content .row").hide();
  334. $(".main-content .form-group .btn").hide();
  335. var form = $(".container .row .panel .panel-footer form").clone();
  336. $(".main-content .form-group.text-center").append(form );
  337. $(".main-content .form-group.text-center .btn-block ").removeClass("btn-block");
  338. }
  339. //Control
  340. if($("#main-content .col-md-12").size()){
  341. $("#main-content .col-md-12").hide();
  342. }
  343. });
  344. </script>';
  345. }
  346. }
  347. catch (\Exception $ex)
  348. {
  349. logModuleCall(
  350. 'ProxmoxVPS',
  351. __FUNCTION__,
  352. $vars,
  353. $ex->getMessage(),
  354. $ex->getTraceAsString()
  355. );
  356. }
  357. }
  358. );