Login sample. The logic is from WebLoginPE snippet.
<?php
$username = $cake->escape( $cake->stripModxTags($_POST['username'], true) );
$password = md5( $cake->escape( $cake->stripModxTags($_POST['password'], true) ) );
$time = time();
if(!isset($username) || !isset($password))
return "Fill necessary fields!";
if(! $userId = $cake->getUserField(0, 'id', 'web_user', "username='$username'") )
return "Wrong Username entered!";
if(! $attrs = $cake->getUserField($userId, '*', 'web_user_attributes') )
return "An error occured!";
if(! $cake->getUserField(0, 'id', 'web_user', "username='$username' AND password='$password'") )
{
$cake->updateUserProfile($userId, 'web_user_attributes', array('failedlogincount' => $attrs['failedlogincount']+1));
return "Wrong password entered!";
}
if( isset($modx->config['allowed_ip']) && strpos($modx->config['allowed_ip'], $cake->getIP()) === false)
return "You can not access to site!";
if (isset($modx->config['allowed_days']))
{
$date = getdate();
$day = $date['wday']+1;
if(strpos($modx->config['allowed_days'], $day) === false)
return "You can not login today!";
}
if($attrs['blocked'] == 1)
return "You have been blocked by site admins!";
if($attrs['blockedafter'] != 0 && $attrs['blockedafter'] < $time)
return "You have been blocked from " .$time - $attrs['blockedafter']. " seconds ago!";
if($attrs['blockeduntil'] > $time)
return "You have been blocked for " .$attrs['blockeduntil'] - $time. " seconds";
if($attrs['failedlogincount'] >= $modx->config['failed_login_attempts'])
{
$cake->updateUserProfile($userId, 'web_user_attributes', array('failedlogincount' => $attrs['failedlogincount']+1));
return "You had many failed login count!";
}
$cake->updateUserProfile($userId, 'web_user_attributes', array('lastlogin'=>$time, 'logincount'=>$attrs['logincount']+1));
$active_users_tbl = $cake->getFullTableName('active_users');
$insert = array('internalKey'=>$userId, 'username'=>$username, 'lasthit'=>$time, 'id'=>'0', 'action'=>'998', 'ip'=>$cake->getIP());
if( $cake->db->getRecordCount( $cake->db->select('internalKey', $active_users_tbl, "internalKey=$userId") ) == 0 )
$cake->db->insert($insert, $active_users_tbl);
else
$cake->db->update($insert, $active_users_tbl, "internalKey=$userId");
$_SESSION['webShortname'] = $username;
$_SESSION['webFullname'] = $attrs['fullname'];
$_SESSION['webEmail'] = $attrs['email'];
$_SESSION['webValidated'] = 1;
$_SESSION['webInternalKey'] = $userId;
$_SESSION['webValid'] = base64_encode($password);
$_SESSION['webUser'] = base64_encode($username);
$_SESSION['webFailedlogins'] = $attrs['failedlogincount'];
$_SESSION['webLastlogin'] = $time;
$_SESSION['webnrlogins'] = $attrs['logincount'] + 1;
$_SESSION['webUserGroupNames'] = array();
$selectWebGroups = $cake->db->query("
SELECT gn.name FROM ".$cake->getFullTableName('web_groups')." g, ".$cake->getFullTableName('webgroup_names')." gn
WHERE gn.id = g.webgroup AND g.webuser = $userId"
);
while($group = $cake->db->getRow($selectWebGroups, 'num'))
$_SESSION['webUserGroupNames'][] = $group[0];