Contao\System::anonymizeIp PHP Méthode

anonymizeIp() public static méthode

Anonymize an IP address by overriding the last chunk
public static anonymizeIp ( string $strIp ) : string
$strIp string The IP address
Résultat string The encoded IP address
    public static function anonymizeIp($strIp)
    {
        // The feature has been disabled
        if (!\Config::get('privacyAnonymizeIp')) {
            return $strIp;
        }
        // Localhost
        if ($strIp == '127.0.0.1' || $strIp == '::1') {
            return $strIp;
        }
        // IPv6
        if (strpos($strIp, ':') !== false) {
            return substr_replace($strIp, ':0000', strrpos($strIp, ':'));
        } else {
            return substr_replace($strIp, '.0', strrpos($strIp, '.'));
        }
    }

Usage Example

Exemple #1
0
 /**
  * Add the subscription and send the activation mail (double opt-in)
  *
  * @param CommentsModel $objComment
  */
 public static function addCommentsSubscription(CommentsModel $objComment)
 {
     $objNotify = \CommentsNotifyModel::findBySourceParentAndEmail($objComment->source, $objComment->parent, $objComment->email);
     // The subscription exists already
     if ($objNotify !== null) {
         return;
     }
     $time = time();
     // Prepare the record
     $arrSet = array('tstamp' => $time, 'source' => $objComment->source, 'parent' => $objComment->parent, 'name' => $objComment->name, 'email' => $objComment->email, 'url' => \Environment::get('request'), 'addedOn' => $time, 'ip' => \System::anonymizeIp(\Environment::get('ip')), 'tokenConfirm' => md5(uniqid(mt_rand(), true)), 'tokenRemove' => md5(uniqid(mt_rand(), true)));
     // Store the subscription
     $objNotify = new \CommentsNotifyModel();
     $objNotify->setRow($arrSet)->save();
     $strUrl = \Idna::decode(\Environment::get('base')) . \Environment::get('request');
     $strConnector = strpos($strUrl, '?') !== false ? '&' : '?';
     // Send the activation mail
     $objEmail = new \Email();
     $objEmail->from = $GLOBALS['TL_ADMIN_EMAIL'];
     $objEmail->fromName = $GLOBALS['TL_ADMIN_NAME'];
     $objEmail->subject = sprintf($GLOBALS['TL_LANG']['MSC']['com_optInSubject'], \Idna::decode(\Environment::get('host')));
     $objEmail->text = sprintf($GLOBALS['TL_LANG']['MSC']['com_optInMessage'], $objComment->name, $strUrl, $strUrl . $strConnector . 'token=' . $objNotify->tokenConfirm, $strUrl . $strConnector . 'token=' . $objNotify->tokenRemove);
     $objEmail->sendTo($objComment->email);
 }
All Usage Examples Of Contao\System::anonymizeIp