PageRenderTime 40ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/site/libraries/joomla/user/helper.php

https://bitbucket.org/manchas/pperezm
PHP | 341 lines | 199 code | 38 blank | 104 comment | 31 complexity | 40299b4974572a1cbeac084a79c427c3 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * @version $Id:helper.php 6961 2007-03-15 16:06:53Z tcp $
  4. * @package Joomla.Framework
  5. * @subpackage User
  6. * @copyright Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
  7. * @license GNU/GPL, see LICENSE.php
  8. * Joomla! is free software. This version may have been modified pursuant
  9. * to the GNU General Public License, and as distributed it includes or
  10. * is derivative of works licensed under the GNU General Public License or
  11. * other free or open source software licenses.
  12. * See COPYRIGHT.php for copyright notices and details.
  13. */
  14. /**
  15. * Authorization helper class, provides static methods to perform various tasks relevant
  16. * to the Joomla user and authorization classes
  17. *
  18. * This class has influences and some method logic from the Horde Auth package
  19. *
  20. * @static
  21. * @package Joomla.Framework
  22. * @subpackage User
  23. * @since 1.5
  24. */
  25. class JUserHelper
  26. {
  27. /**
  28. * Method to activate a user
  29. *
  30. * @param string $activation Activation string
  31. * @return boolean True on success
  32. * @since 1.5
  33. */
  34. function activateUser($activation)
  35. {
  36. //Initialize some variables
  37. $db = & JFactory::getDBO();
  38. // Lets get the id of the user we want to activate
  39. $query = 'SELECT id'
  40. . ' FROM #__users'
  41. . ' WHERE activation = '.$db->Quote($activation)
  42. . ' AND block = 1'
  43. . ' AND lastvisitDate = '.$db->Quote('0000-00-00 00:00:00');
  44. ;
  45. $db->setQuery( $query );
  46. $id = intval( $db->loadResult() );
  47. // Is it a valid user to activate?
  48. if ($id)
  49. {
  50. $user =& JUser::getInstance( (int) $id );
  51. $user->set('block', '0');
  52. $user->set('activation', '');
  53. // Time to take care of business.... store the user.
  54. if (!$user->save())
  55. {
  56. JError::raiseWarning( "SOME_ERROR_CODE", $user->getError() );
  57. return false;
  58. }
  59. }
  60. else
  61. {
  62. JError::raiseWarning( "SOME_ERROR_CODE", JText::_('UNABLE TO FIND A USER WITH GIVEN ACTIVATION STRING') );
  63. return false;
  64. }
  65. return true;
  66. }
  67. /**
  68. * Returns userid if a user exists
  69. *
  70. * @param string The username to search on
  71. * @return int The user id or 0 if not found
  72. */
  73. function getUserId($username)
  74. {
  75. // Initialize some variables
  76. $db = & JFactory::getDBO();
  77. $query = 'SELECT id FROM #__users WHERE username = ' . $db->Quote( $username );
  78. $db->setQuery($query, 0, 1);
  79. return $db->loadResult();
  80. }
  81. /**
  82. * Formats a password using the current encryption.
  83. *
  84. * @access public
  85. * @param string $plaintext The plaintext password to encrypt.
  86. * @param string $salt The salt to use to encrypt the password. []
  87. * If not present, a new salt will be
  88. * generated.
  89. * @param string $encryption The kind of pasword encryption to use.
  90. * Defaults to md5-hex.
  91. * @param boolean $show_encrypt Some password systems prepend the kind of
  92. * encryption to the crypted password ({SHA},
  93. * etc). Defaults to false.
  94. *
  95. * @return string The encrypted password.
  96. */
  97. function getCryptedPassword($plaintext, $salt = '', $encryption = 'md5-hex', $show_encrypt = false)
  98. {
  99. // Get the salt to use.
  100. $salt = JUserHelper::getSalt($encryption, $salt, $plaintext);
  101. // Encrypt the password.
  102. switch ($encryption)
  103. {
  104. case 'plain' :
  105. return $plaintext;
  106. case 'sha' :
  107. $encrypted = base64_encode(mhash(MHASH_SHA1, $plaintext));
  108. return ($show_encrypt) ? '{SHA}'.$encrypted : $encrypted;
  109. case 'crypt' :
  110. case 'crypt-des' :
  111. case 'crypt-md5' :
  112. case 'crypt-blowfish' :
  113. return ($show_encrypt ? '{crypt}' : '').crypt($plaintext, $salt);
  114. case 'md5-base64' :
  115. $encrypted = base64_encode(mhash(MHASH_MD5, $plaintext));
  116. return ($show_encrypt) ? '{MD5}'.$encrypted : $encrypted;
  117. case 'ssha' :
  118. $encrypted = base64_encode(mhash(MHASH_SHA1, $plaintext.$salt).$salt);
  119. return ($show_encrypt) ? '{SSHA}'.$encrypted : $encrypted;
  120. case 'smd5' :
  121. $encrypted = base64_encode(mhash(MHASH_MD5, $plaintext.$salt).$salt);
  122. return ($show_encrypt) ? '{SMD5}'.$encrypted : $encrypted;
  123. case 'aprmd5' :
  124. $length = strlen($plaintext);
  125. $context = $plaintext.'$apr1$'.$salt;
  126. $binary = JUserHelper::_bin(md5($plaintext.$salt.$plaintext));
  127. for ($i = $length; $i > 0; $i -= 16) {
  128. $context .= substr($binary, 0, ($i > 16 ? 16 : $i));
  129. }
  130. for ($i = $length; $i > 0; $i >>= 1) {
  131. $context .= ($i & 1) ? chr(0) : $plaintext[0];
  132. }
  133. $binary = JUserHelper::_bin(md5($context));
  134. for ($i = 0; $i < 1000; $i ++) {
  135. $new = ($i & 1) ? $plaintext : substr($binary, 0, 16);
  136. if ($i % 3) {
  137. $new .= $salt;
  138. }
  139. if ($i % 7) {
  140. $new .= $plaintext;
  141. }
  142. $new .= ($i & 1) ? substr($binary, 0, 16) : $plaintext;
  143. $binary = JUserHelper::_bin(md5($new));
  144. }
  145. $p = array ();
  146. for ($i = 0; $i < 5; $i ++) {
  147. $k = $i +6;
  148. $j = $i +12;
  149. if ($j == 16) {
  150. $j = 5;
  151. }
  152. $p[] = JUserHelper::_toAPRMD5((ord($binary[$i]) << 16) | (ord($binary[$k]) << 8) | (ord($binary[$j])), 5);
  153. }
  154. return '$apr1$'.$salt.'$'.implode('', $p).JUserHelper::_toAPRMD5(ord($binary[11]), 3);
  155. case 'md5-hex' :
  156. default :
  157. $encrypted = ($salt) ? md5($plaintext.$salt) : md5($plaintext);
  158. return ($show_encrypt) ? '{MD5}'.$encrypted : $encrypted;
  159. }
  160. }
  161. /**
  162. * Returns a salt for the appropriate kind of password encryption.
  163. * Optionally takes a seed and a plaintext password, to extract the seed
  164. * of an existing password, or for encryption types that use the plaintext
  165. * in the generation of the salt.
  166. *
  167. * @access public
  168. * @param string $encryption The kind of pasword encryption to use.
  169. * Defaults to md5-hex.
  170. * @param string $seed The seed to get the salt from (probably a
  171. * previously generated password). Defaults to
  172. * generating a new seed.
  173. * @param string $plaintext The plaintext password that we're generating
  174. * a salt for. Defaults to none.
  175. *
  176. * @return string The generated or extracted salt.
  177. */
  178. function getSalt($encryption = 'md5-hex', $seed = '', $plaintext = '')
  179. {
  180. // Encrypt the password.
  181. switch ($encryption)
  182. {
  183. case 'crypt' :
  184. case 'crypt-des' :
  185. if ($seed) {
  186. return substr(preg_replace('|^{crypt}|i', '', $seed), 0, 2);
  187. } else {
  188. return substr(md5(mt_rand()), 0, 2);
  189. }
  190. break;
  191. case 'crypt-md5' :
  192. if ($seed) {
  193. return substr(preg_replace('|^{crypt}|i', '', $seed), 0, 12);
  194. } else {
  195. return '$1$'.substr(md5(mt_rand()), 0, 8).'$';
  196. }
  197. break;
  198. case 'crypt-blowfish' :
  199. if ($seed) {
  200. return substr(preg_replace('|^{crypt}|i', '', $seed), 0, 16);
  201. } else {
  202. return '$2$'.substr(md5(mt_rand()), 0, 12).'$';
  203. }
  204. break;
  205. case 'ssha' :
  206. if ($seed) {
  207. return substr(preg_replace('|^{SSHA}|', '', $seed), -20);
  208. } else {
  209. return mhash_keygen_s2k(MHASH_SHA1, $plaintext, substr(pack('h*', md5(mt_rand())), 0, 8), 4);
  210. }
  211. break;
  212. case 'smd5' :
  213. if ($seed) {
  214. return substr(preg_replace('|^{SMD5}|', '', $seed), -16);
  215. } else {
  216. return mhash_keygen_s2k(MHASH_MD5, $plaintext, substr(pack('h*', md5(mt_rand())), 0, 8), 4);
  217. }
  218. break;
  219. case 'aprmd5' :
  220. /* 64 characters that are valid for APRMD5 passwords. */
  221. $APRMD5 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  222. if ($seed) {
  223. return substr(preg_replace('/^\$apr1\$(.{8}).*/', '\\1', $seed), 0, 8);
  224. } else {
  225. $salt = '';
  226. for ($i = 0; $i < 8; $i ++) {
  227. $salt .= $APRMD5 {
  228. rand(0, 63)
  229. };
  230. }
  231. return $salt;
  232. }
  233. break;
  234. default :
  235. $salt = '';
  236. if ($seed) {
  237. $salt = $seed;
  238. }
  239. return $salt;
  240. break;
  241. }
  242. }
  243. /**
  244. * Generate a random password
  245. *
  246. * @static
  247. * @param int $length Length of the password to generate
  248. * @return string Random Password
  249. * @since 1.5
  250. */
  251. function genRandomPassword($length = 8)
  252. {
  253. $salt = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  254. $len = strlen($salt);
  255. $makepass = '';
  256. $stat = @stat(__FILE__);
  257. if(empty($stat) || !is_array($stat)) $stat = array(php_uname());
  258. mt_srand(crc32(microtime() . implode('|', $stat)));
  259. for ($i = 0; $i < $length; $i ++) {
  260. $makepass .= $salt[mt_rand(0, $len -1)];
  261. }
  262. return $makepass;
  263. }
  264. /**
  265. * Converts to allowed 64 characters for APRMD5 passwords.
  266. *
  267. * @access private
  268. * @param string $value
  269. * @param integer $count
  270. * @return string $value converted to the 64 MD5 characters.
  271. * @since 1.5
  272. */
  273. function _toAPRMD5($value, $count)
  274. {
  275. /* 64 characters that are valid for APRMD5 passwords. */
  276. $APRMD5 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  277. $aprmd5 = '';
  278. $count = abs($count);
  279. while (-- $count) {
  280. $aprmd5 .= $APRMD5[$value & 0x3f];
  281. $value >>= 6;
  282. }
  283. return $aprmd5;
  284. }
  285. /**
  286. * Converts hexadecimal string to binary data.
  287. *
  288. * @access private
  289. * @param string $hex Hex data.
  290. * @return string Binary data.
  291. * @since 1.5
  292. */
  293. function _bin($hex)
  294. {
  295. $bin = '';
  296. $length = strlen($hex);
  297. for ($i = 0; $i < $length; $i += 2) {
  298. $tmp = sscanf(substr($hex, $i, 2), '%x');
  299. $bin .= chr(array_shift($tmp));
  300. }
  301. return $bin;
  302. }
  303. }