10 defined(
'JPATH_PLATFORM') or die;
38 public static function addUserToGroup($userId, $groupId)
41 $user =
new JUser((
int) $userId);
44 if (!in_array($groupId, $user->groups))
48 $query = $db->getQuery(
true)
49 ->select($db->quoteName(
'title'))
50 ->from($db->quoteName(
'#__usergroups'))
51 ->where($db->quoteName(
'id') .
' = ' . (int) $groupId);
52 $db->setQuery($query);
53 $title = $db->loadResult();
58 throw new RuntimeException(
'Access Usergroup Invalid');
62 $user->groups[$title] = $groupId;
72 $temp->groups = $user->groups;
77 if ($temp->id == $userId)
79 $temp->groups = $user->groups;
95 public static function getUserGroups($userId)
100 return isset($user->groups) ? $user->groups : array();
113 public static function removeUserFromGroup($userId, $groupId)
119 $key = array_search($groupId, $user->groups);
124 unset($user->groups[$key]);
132 $temp->groups = $user->groups;
137 if ($temp->id == $userId)
139 $temp->groups = $user->groups;
155 public static function setUserGroups($userId, $groups)
162 $user->groups = $groups;
166 $query = $db->getQuery(
true)
167 ->select($db->quoteName(
'id') .
', ' . $db->quoteName(
'title'))
168 ->from($db->quoteName(
'#__usergroups'))
169 ->where($db->quoteName(
'id') .
' = ' . implode(
' OR ' . $db->quoteName(
'id') .
' = ', $user->groups));
170 $db->setQuery($query);
171 $results = $db->loadObjectList();
174 for ($i = 0, $n = count($results); $i < $n; $i++)
176 $user->groups[$results[$i]->id] = $results[$i]->id;
186 $temp->groups = $user->groups;
191 if ($temp->id == $userId)
193 $temp->groups = $user->groups;
209 public static function getProfile($userId = 0)
219 JPluginHelper::importPlugin(
'user');
225 $dispatcher->trigger(
'onContentPrepareData', array(
'com_users.profile', &$data));
239 public static function activateUser($activation)
244 $query = $db->getQuery(
true)
245 ->select($db->quoteName(
'id'))
246 ->from($db->quoteName(
'#__users'))
247 ->where($db->quoteName(
'activation') .
' = ' . $db->quote($activation))
248 ->where($db->quoteName(
'block') .
' = 1')
249 ->where($db->quoteName(
'lastvisitDate') .
' = ' . $db->quote(
'0000-00-00 00:00:00'));
250 $db->setQuery($query);
251 $id = (int) $db->loadResult();
258 $user->set(
'block',
'0');
259 $user->set(
'activation',
'');
288 public static function getUserId($username)
292 $query = $db->getQuery(
true)
293 ->select($db->quoteName(
'id'))
294 ->from($db->quoteName(
'#__users'))
295 ->where($db->quoteName(
'username') .
' = ' . $db->quote($username));
296 $db->setQuery($query, 0, 1);
298 return $db->loadResult();
320 public static function getCryptedPassword($plaintext, $salt =
'', $encryption =
'bcrypt', $show_encrypt =
false)
324 if ($app->getClientId() != 2)
326 $joomlaPluginEnabled = JPluginHelper::isEnabled(
'user',
'joomla');
330 if (!empty($joomlaPluginEnabled))
332 JPluginHelper::importPlugin(
'user',
'joomla');
333 $userPlugin = JPluginHelper::getPlugin(
'user',
'joomla');
334 $userPluginParams =
new JRegistry($userPlugin->params);
335 PlgUserJoomla::setDefaultEncryption($userPluginParams);
340 if (strlen($plaintext) > 55)
342 $app->enqueueMessage(
JText::_(
'JLIB_USER_ERROR_PASSWORD_TOO_LONG'),
'error');
350 $salt = self::getSalt($encryption, $salt, $plaintext);
360 $encrypted = base64_encode(mhash(MHASH_SHA1, $plaintext));
362 return ($show_encrypt) ?
'{SHA}' . $encrypted : $encrypted;
367 case 'crypt-blowfish':
368 return ($show_encrypt ?
'{crypt}' :
'') . crypt($plaintext, $salt);
371 $encrypted = base64_encode(mhash(MHASH_MD5, $plaintext));
373 return ($show_encrypt) ?
'{MD5}' . $encrypted : $encrypted;
376 $encrypted = base64_encode(mhash(MHASH_SHA1, $plaintext . $salt) . $salt);
378 return ($show_encrypt) ?
'{SSHA}' . $encrypted : $encrypted;
381 $encrypted = base64_encode(mhash(MHASH_MD5, $plaintext . $salt) . $salt);
383 return ($show_encrypt) ?
'{SMD5}' . $encrypted : $encrypted;
386 $length = strlen($plaintext);
387 $context = $plaintext .
'$apr1$' . $salt;
388 $binary = self::_bin(md5($plaintext . $salt . $plaintext));
390 for ($i = $length; $i > 0; $i -= 16)
392 $context .= substr($binary, 0, ($i > 16 ? 16 : $i));
394 for ($i = $length; $i > 0; $i >>= 1)
396 $context .= ($i & 1) ? chr(0) : $plaintext[0];
399 $binary = self::_bin(md5($context));
401 for ($i = 0; $i < 1000; $i++)
403 $new = ($i & 1) ? $plaintext : substr($binary, 0, 16);
413 $new .= ($i & 1) ? substr($binary, 0, 16) : $plaintext;
414 $binary = self::_bin(md5($new));
419 for ($i = 0; $i < 5; $i++)
428 $p[] = self::_toAPRMD5((ord($binary[$i]) << 16) | (ord($binary[$k]) << 8) | (ord($binary[$j])), 5);
431 return '$apr1$' . $salt .
'$' . implode(
'', $p) . self::_toAPRMD5(ord($binary[11]), 3);
434 $encrypted = ($salt) ? md5($plaintext . $salt) : md5($plaintext);
436 return ($show_encrypt) ?
'{MD5}' . $encrypted : $encrypted;
439 $encrypted = ($salt) ? hash(
'sha256', $plaintext . $salt) .
':' . $salt : hash(
'sha256', $plaintext);
441 return ($show_encrypt) ?
'{SHA256}' . $encrypted :
'{SHA256}' . $encrypted;
448 if ($useStrongEncryption ===
true)
450 $encrypted = password_hash($plaintext, PASSWORD_BCRYPT);
455 return static::getCryptedPassword($plaintext,
'',
'sha256',
false);
458 return ($show_encrypt) ?
'{BCRYPT}' . $encrypted : $encrypted;
463 return static::getCryptedPassword($plaintext,
'',
'sha256',
false);
489 public static function getSalt($encryption =
'md5-hex', $seed =
'', $plaintext =
'')
498 return substr(preg_replace(
'|^{crypt}|i',
'', $seed), 0, 2);
502 return substr(md5(mt_rand()), 0, 2);
509 return preg_replace(
'|^{sha256}|i',
'', $seed);
513 return static::genRandomPassword(16);
520 return substr(preg_replace(
'|^{crypt}|i',
'', $seed), 0, 12);
524 return '$1$' . substr(md5(mt_rand()), 0, 8) .
'$';
528 case 'crypt-blowfish':
531 return substr(preg_replace(
'|^{crypt}|i',
'', $seed), 0, 16);
535 return '$2$' . substr(md5(mt_rand()), 0, 12) .
'$';
542 return substr(preg_replace(
'|^{SSHA}|',
'', $seed), -20);
546 return mhash_keygen_s2k(MHASH_SHA1, $plaintext, substr(pack(
'h*', md5(mt_rand())), 0, 8), 4);
553 return substr(preg_replace(
'|^{SMD5}|',
'', $seed), -16);
557 return mhash_keygen_s2k(MHASH_MD5, $plaintext, substr(pack(
'h*', md5(mt_rand())), 0, 8), 4);
562 $APRMD5 =
'./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
566 return substr(preg_replace(
'/^\$apr1\$(.{8}).*/',
'\\1', $seed), 0, 8);
572 for ($i = 0; $i < 8; $i++)
574 $salt .= $APRMD5{rand(0, 63)};
606 public static function genRandomPassword($length = 16)
608 $salt =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
609 $base = strlen($salt);
620 $shift = ord($random[0]);
622 for ($i = 1; $i <= $length; ++$i)
624 $makepass .= $salt[($shift + ord($random[$i])) % $base];
625 $shift += ord($random[$i]);
641 protected static function _toAPRMD5($value, $count)
644 $APRMD5 =
'./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
647 $count = abs($count);
651 $aprmd5 .= $APRMD5[$value & 0x3f];
666 private static function _bin($hex)
669 $length = strlen($hex);
671 for ($i = 0; $i < $length; $i += 2)
673 $tmp = sscanf(substr($hex, $i, 2),
'%x');
674 $bin .= chr(array_shift($tmp));
690 public static function invalidateCookie($userId, $cookieName)
693 $query = $db->getQuery(
true);
697 ->update($db->quoteName(
'#__user_keys'))
698 ->
set($db->quoteName(
'invalid') .
' = 1')
699 ->where($db->quotename(
'user_id') .
' = ' . $db->quote($userId));
701 $db->setQuery($query)->execute();
705 $app->input->cookie->set($cookieName,
false, time() - 42000, $app->get(
'cookie_path'), $app->get(
'cookie_domain'),
false,
true);
717 public static function clearExpiredTokens()
722 $query = $db->getQuery(
true)
723 ->delete(
'#__user_keys')
724 ->where($db->quoteName(
'time') .
' < ' . $db->quote($now));
726 return $db->setQuery($query)->execute();
736 public static function getRememberCookieData()
739 $cookieName = static::getShortHashedUserAgent();
743 $cookieValue = $app->input->cookie->get($cookieName);
745 if (!empty($cookieValue))
747 return explode(
'.', $cookieValue);
763 public static function getShortHashedUserAgent()
766 $uaString = $ua->userAgent;
767 $browserVersion = $ua->browserVersion;
768 $uaShort = str_replace($browserVersion,
'abcd', $uaString);