Skip to content

Instantly share code, notes, and snippets.

@matdave
Last active August 26, 2016 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matdave/2250a8007993663f1e5340565697c61f to your computer and use it in GitHub Desktop.
Save matdave/2250a8007993663f1e5340565697c61f to your computer and use it in GitHub Desktop.
MODX Manager IP Log - Log a managers IP when they login
<?php
switch ($modx->event->name) {
case 'OnManagerLogin':
$userinfo =& $modx->event->params['user'];
if(!empty($userinfo)){
$ip = getenv('HTTP_CLIENT_IP')?:
getenv('HTTP_X_FORWARDED_FOR')?:
getenv('HTTP_X_FORWARDED')?:
getenv('HTTP_FORWARDED_FOR')?:
getenv('HTTP_FORWARDED')?:
getenv('REMOTE_ADDR');
$profile = $userinfo->getOne('Profile');
$comment = $profile->get('comment');
if (strpos($comment, $ip) === false) {
$comment .= $ip."\r\n";
}
$profile->set('comment',$comment);
if(!$profile->save()){
$modx->log(xPDO::LOG_LEVEL_ERROR, 'Could not save user '.$userinfo->get('username').' with IP '.$ip.'!' );
}else{
$log = $modx->newObject('modManagerLog');
$log->fromArray(array('user'=>$userinfo->get('id'),
'occurred' => date('Y-m-d H:i:s'),
'action' => 'ip_logged',
'classKey' => 'modContext',
'item' => $ip
));
$log->save();
}
}else{
$modx->log(xPDO::LOG_LEVEL_ERROR, 'User not found!' );
}
break;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment