Example #1
0
 /**
  * Get an IpAddress entity for current session or for passed in IP address.
  *
  * @param string $ip
  *
  * @return IpAddress
  */
 public function getIpAddress($ip = null)
 {
     static $ipAddresses = [];
     if ($ip === null) {
         $ip = $this->getIpAddressFromRequest();
     }
     if (empty($ip)) {
         //assume local as the ip is empty
         $ip = '127.0.0.1';
     }
     if (empty($ipAddress[$ip])) {
         $repo = $this->em->getRepository('MauticCoreBundle:IpAddress');
         $ipAddress = $repo->findOneByIpAddress($ip);
         $saveIp = $ipAddress === null;
         if ($ipAddress === null) {
             $ipAddress = new IpAddress();
             $ipAddress->setIpAddress($ip);
         }
         // Ensure the do not track list is inserted
         if (!is_array($this->doNotTrackIps)) {
             $this->doNotTrackIps = [];
         }
         if (!is_array($this->doNotTrackInternalIps)) {
             $this->doNotTrackInternalIps = [];
         }
         $doNotTrack = array_merge(['127.0.0.1', '::1'], $this->doNotTrackIps, $this->doNotTrackInternalIps);
         $ipAddress->setDoNotTrackList($doNotTrack);
         $details = $ipAddress->getIpDetails();
         if ($ipAddress->isTrackable() && empty($details['city'])) {
             // Get the IP lookup service
             // Fetch the data
             if ($this->ipLookup) {
                 $details = $this->ipLookup->setIpAddress($ip)->getDetails();
                 $ipAddress->setIpDetails($details);
                 // Save new details
                 $saveIp = true;
             }
         }
         if ($saveIp) {
             $repo->saveEntity($ipAddress);
         }
         $ipAddresses[$ip] = $ipAddress;
     }
     return $ipAddresses[$ip];
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 public function buildView(FormView $view, FormInterface $form, array $options)
 {
     $view->vars['ipLookupAttribution'] = null !== $this->ipLookup ? $this->ipLookup->getAttribution() : '';
 }