PageRenderTime 56ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/user/helper.php

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