IPHelper.php 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace MGModule\DNSManager2\mgLibs\custom\reverse;
  3. use \MGModule\DNSManager2\mgLibs\custom\dns\utils\IP;
  4. use \MGModule\DNSManager2\mgLibs\custom\exceptions\ValidationException;
  5. use \MGModule\DNSManager2\mgLibs\custom\helpers\IPManagerIntegration;
  6. use \MGModule\DNSManager2\mgLibs\custom\manager\ClientHelper;
  7. use \MGModule\DNSManager2\mgLibs\custom\manager\GlobalSettingHelper;
  8. use \MGModule\DNSManager2\models\custom\globalsetting\GlobalSettingEnum;
  9. use \MGModule\DNSManager2\models\custom\zone\ZoneTypeEnum;
  10. use \MGModule\DNSManager2\models\whmcs\hostingaddon\hostingaddon;
  11. class IPHelper {
  12. public static function isCustomIPEnabledForZoneCreate() {
  13. return GlobalSettingHelper::getSetting(GlobalSettingEnum::ZONE_CREATE_CUSTOM_IP) == 'on';
  14. }
  15. public static function isCustomIPEnabled() {
  16. return GlobalSettingHelper::getSetting(GlobalSettingEnum::RDNS_CUSTOM_IP) == 'on';
  17. }
  18. public static function isSubnetCustomIPEnabled() {
  19. return GlobalSettingHelper::getSetting(GlobalSettingEnum::RDNS_SUBNET_CUSTOM_IP) == 'on';
  20. }
  21. public static function getIPsArray($clientid, $type, $relid) {
  22. $out = array();
  23. $client = new ClientHelper($clientid);
  24. if(GlobalSettingHelper::getSetting(GlobalSettingEnum::SERVICE_ONLY_IPS)) {
  25. if($type == ZoneTypeEnum::DOMAIN || $type == ZoneTypeEnum::OTHER) {
  26. return array('pools' => array(), 'ips' => array());
  27. }
  28. if($type == ZoneTypeEnum::ADDON) {
  29. $serviceid = hostingaddon::factory($relid)->hostingid;
  30. } else {
  31. $serviceid = $relid;
  32. }
  33. $out['pools'] = IPManagerIntegration::getIPPoolsForService($serviceid);
  34. $out['all_ips'] = $out['ips'] = $client->getIPsForService($serviceid);
  35. } else {
  36. $out['pools'] = IPManagerIntegration::getIPPoolsForClient($clientid);
  37. $out['all_ips'] = $out['ips'] = $client->getClientIPs();
  38. }
  39. foreach($out['pools'] as $pk => $pool) {
  40. $empty_pool = true;
  41. foreach($out['ips'] as $k => $ip) {
  42. $ip = new IP($ip);
  43. if($ip->isInNetwork($pool['pool'], $pool['mask'])) {
  44. unset($out['ips'][$k]);
  45. $empty_pool = false;
  46. }
  47. }
  48. if($empty_pool && !self::isSubnetCustomIPEnabled()) {
  49. unset($out['pools'][$pk]);
  50. }
  51. }
  52. return $out;
  53. }
  54. public static function validateIfClientCanUseIP($clientid, $type, $relid, $ip, $zone_create = false) {
  55. $ip = new IP($ip);
  56. if(!$ip->isValid()) {
  57. throw new ValidationException('Invalid IP', 1);
  58. }
  59. if(( $zone_create === FALSE && self::isCustomIPEnabled() )
  60. || ($zone_create === TRUE && self::isCustomIPEnabledForZoneCreate()) ) {
  61. return true;
  62. }
  63. $ip_array = self::getIPsArray($clientid, $type, $relid);
  64. if( !self::isSubnetCustomIPEnabled() ) {
  65. if(!in_array((string)$ip, $ip_array['all_ips'])) {
  66. throw new ValidationException("You cannot use IP that do not belongs to you", 301);
  67. }
  68. return true;
  69. }
  70. if(in_array((string)$ip, $ip_array['ips'])) {
  71. return true;
  72. }
  73. foreach($ip_array['pools'] as $pool) {
  74. if($ip->isInNetwork($pool['pool'], $pool['mask'])) {
  75. return true;
  76. }
  77. }
  78. throw new ValidationException("You cannot use IP that does not belong to subnet", 302);
  79. }
  80. }