| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- <?php
- namespace MGModule\DNSManager2\mgLibs\custom\reverse;
- use \MGModule\DNSManager2\mgLibs\custom\dns\utils\IP;
- use \MGModule\DNSManager2\mgLibs\custom\exceptions\ValidationException;
- use \MGModule\DNSManager2\mgLibs\custom\helpers\IPManagerIntegration;
- use \MGModule\DNSManager2\mgLibs\custom\manager\ClientHelper;
- use \MGModule\DNSManager2\mgLibs\custom\manager\GlobalSettingHelper;
- use \MGModule\DNSManager2\models\custom\globalsetting\GlobalSettingEnum;
- use \MGModule\DNSManager2\models\custom\zone\ZoneTypeEnum;
- use \MGModule\DNSManager2\models\whmcs\hostingaddon\hostingaddon;
- class IPHelper {
- public static function isCustomIPEnabledForZoneCreate() {
- return GlobalSettingHelper::getSetting(GlobalSettingEnum::ZONE_CREATE_CUSTOM_IP) == 'on';
- }
-
- public static function isCustomIPEnabled() {
- return GlobalSettingHelper::getSetting(GlobalSettingEnum::RDNS_CUSTOM_IP) == 'on';
- }
- public static function isSubnetCustomIPEnabled() {
- return GlobalSettingHelper::getSetting(GlobalSettingEnum::RDNS_SUBNET_CUSTOM_IP) == 'on';
- }
-
- public static function getIPsArray($clientid, $type, $relid) {
- $out = array();
- $client = new ClientHelper($clientid);
-
- if(GlobalSettingHelper::getSetting(GlobalSettingEnum::SERVICE_ONLY_IPS)) {
- if($type == ZoneTypeEnum::DOMAIN || $type == ZoneTypeEnum::OTHER) {
- return array('pools' => array(), 'ips' => array());
- }
-
- if($type == ZoneTypeEnum::ADDON) {
- $serviceid = hostingaddon::factory($relid)->hostingid;
- } else {
- $serviceid = $relid;
- }
-
- $out['pools'] = IPManagerIntegration::getIPPoolsForService($serviceid);
- $out['all_ips'] = $out['ips'] = $client->getIPsForService($serviceid);
- } else {
- $out['pools'] = IPManagerIntegration::getIPPoolsForClient($clientid);
- $out['all_ips'] = $out['ips'] = $client->getClientIPs();
- }
-
- foreach($out['pools'] as $pk => $pool) {
- $empty_pool = true;
- foreach($out['ips'] as $k => $ip) {
- $ip = new IP($ip);
- if($ip->isInNetwork($pool['pool'], $pool['mask'])) {
- unset($out['ips'][$k]);
- $empty_pool = false;
- }
- }
-
- if($empty_pool && !self::isSubnetCustomIPEnabled()) {
- unset($out['pools'][$pk]);
- }
- }
- return $out;
- }
-
- public static function validateIfClientCanUseIP($clientid, $type, $relid, $ip, $zone_create = false) {
- $ip = new IP($ip);
- if(!$ip->isValid()) {
- throw new ValidationException('Invalid IP', 1);
- }
-
- if(( $zone_create === FALSE && self::isCustomIPEnabled() )
- || ($zone_create === TRUE && self::isCustomIPEnabledForZoneCreate()) ) {
- return true;
- }
-
- $ip_array = self::getIPsArray($clientid, $type, $relid);
- if( !self::isSubnetCustomIPEnabled() ) {
- if(!in_array((string)$ip, $ip_array['all_ips'])) {
- throw new ValidationException("You cannot use IP that do not belongs to you", 301);
- }
- return true;
- }
-
- if(in_array((string)$ip, $ip_array['ips'])) {
- return true;
- }
-
- foreach($ip_array['pools'] as $pool) {
- if($ip->isInNetwork($pool['pool'], $pool['mask'])) {
- return true;
- }
- }
-
- throw new ValidationException("You cannot use IP that does not belong to subnet", 302);
- }
- }
|