Exemple #1
0
 /**
  * @param $input
  **/
 function prepareInput($input)
 {
     // In case of entity transfer, $input['network'] is not defined
     if (!isset($input['network']) && isset($this->fields['network'])) {
         $input['network'] = $this->fields['network'];
     }
     // In case of entity transfer, $input['gateway'] is not defined
     if (!isset($input['gateway']) && isset($this->fields['gateway'])) {
         $input['gateway'] = $this->fields['gateway'];
     }
     // If $this->fields["id"] is not set then, we are adding a new network
     // Or if $this->fields["network"] != $input["network"] we a updating the network
     $address = new IPAddress();
     $netmask = new IPNetmask();
     // Don't validate an empty network
     if (empty($input["network"])) {
         return array('error' => __('Invalid network address'), 'input' => false);
     }
     if (!isset($this->fields["id"]) || !isset($this->fields["network"]) || $input["network"] != $this->fields["network"]) {
         $network = explode("/", $input["network"]);
         if (count($network) != 2) {
             return array('error' => __('Invalid input format for the network'), 'input' => false);
         }
         if (!$address->setAddressFromString(trim($network[0]))) {
             return array('error' => __('Invalid network address'), 'input' => false);
         }
         if (!$netmask->setNetmaskFromString(trim($network[1]), $address->getVersion())) {
             return array('error' => __('Invalid subnet mask'), 'input' => false);
         }
         // After checking that address and netmask are valid, modify the address to be the "real"
         // network address : the first address of the network. This is not required for SQL, but
         // that looks better for the human
         self::computeNetworkRangeFromAdressAndNetmask($address, $netmask, $address);
         // Now, we look for already existing same network inside the database
         $params = array("address" => $address, "netmask" => $netmask);
         if (isset($this->fields["id"])) {
             $params["exclude IDs"] = $this->fields["id"];
         }
         if (isset($this->fields["entities_id"])) {
             $entities_id = $this->fields["entities_id"];
         } else {
             if (isset($input["entities_id"])) {
                 $entities_id = $input["entities_id"];
             } else {
                 $entities_id = -1;
             }
         }
         // TODO : what is the best way ? recursive or not ?
         $sameNetworks = self::searchNetworks("equals", $params, $entities_id, false);
         // Check unicity !
         if ($sameNetworks && count($sameNetworks) > 0) {
             return array('error' => __('Network already defined in visible entities'), 'input' => false);
         }
         // Then, update $input to reflect the network and the netmask
         $input = $address->setArrayFromAddress($input, "version", "address", "address");
         $input = $netmask->setArrayFromAddress($input, "", "netmask", "netmask");
         // We check to see if the network is modified
         $previousAddress = new IPAddress();
         $previousAddress->setAddressFromArray($this->fields, "version", "address", "address");
         $previousNetmask = new IPNetmask();
         $previousNetmask->setAddressFromArray($this->fields, "version", "netmask", "netmask");
         if ($previousAddress->equals($address) && $previousNetmask->equals($netmask)) {
             $this->networkUpdate = false;
         } else {
             $this->networkUpdate = true;
         }
     } else {
         // If netmask and address are not modified, then, load them from DB to check the validity
         // of the gateway
         $this->networkUpdate = false;
         $address->setAddressFromArray($this->fields, "version", "address", "address");
         $netmask->setAddressFromArray($this->fields, "version", "netmask", "netmask");
         $entities_id = $this->fields['entities_id'];
     }
     // Update class for the CommonImplicitTree update ...
     $this->data_for_implicit_update = array('address' => $address, 'netmask' => $netmask, 'entities_id' => $entities_id);
     $returnValue = array();
     // If the gateway has been altered, or the network information (address or netmask) changed,
     // then, we must revalidate the gateway !
     if (!isset($this->fields["gateway"]) || $input["gateway"] != $this->fields["gateway"] || $this->networkUpdate) {
         $gateway = new IPAddress();
         if (!empty($input["gateway"])) {
             if (!$gateway->setAddressFromString($input["gateway"]) || !self::checkIPFromNetwork($gateway, $address, $netmask)) {
                 $returnValue['error'] = __('Invalid gateway address');
                 if (!empty($this->fields["gateway"])) {
                     if (!$gateway->setAddressFromString($this->fields["gateway"]) || !self::checkIPFromNetwork($gateway, $address, $netmask)) {
                         $gateway->disableAddress();
                     }
                 } else {
                     $gateway->disableAddress();
                 }
             }
         }
         $input = $gateway->setArrayFromAddress($input, "", "gateway", "gateway");
     }
     $returnValue['input'] = $input;
     return $returnValue;
 }