doAuthTools.class.php 1.75 KB
Newer Older
Игорь's avatar
init    
Игорь committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php

/**
 * This library is very important to secure your users and their passwords.
 * Please, be sure, that you don't show Email, Password, Salt fields to public
 * If you need you can update this algorithms by copying this class to your lib folder
 */

class doAuthTools {

  /**
   * Returns a hash to save in cookie
   *
   * @param User $user
   * @return string
   *
   */
  

  public static function rememberHash(User $user) {
    return sha1($user->getId().sfConfig::get('sf_csrf_secret','').$user->getEmail().substr($user->getSalt().$user->getPassword(),20,30));
  }

  /**
   * Returns an activation code
   *
   * @param User $user
   * @return string
   *
   */

  public static function activationCode(User $user) {
    return sha1(mt_rand(10000,99999).sfConfig::get('sf_csrf_secret','').$user->getEmail());
  }

  /**
   * Returns a code for password reset
   *
   * @param User $user
   * @return string
   *
   */

  public static function passwordResetCode(User $user) {
    return sha1(substr($user->getSalt(),10,10).sfConfig::get('sf_csrf_secret','').$user->getEmail().$user->getPassword());
  }

  /**
   * Returns a new password on user request
   *
   * @return string
   *
   */


  public static function generatePassword() {
    $pool   = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    for ($i = 1; $i <= 10; $i++)
    {
      mt_srand();
      $string .= substr($pool, mt_rand(0, 61), 1);
    }

    return $string;
  }
  
  public static function generatePassword2($length = 8, $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
  {
    $string = '';

    for ($i = 1; $i <= $length; $i++)
    {
      mt_srand();
      $string .= substr($pool, mt_rand(0, strlen($pool) - 1), 1);
    }
    return $string;
  }
}


?>