Marius Cramer
2014-08-13 31230cb7cda673db7a96fb14d93dfaf9262c74cf
commit | author | age
7b9388 1 <?php
MC 2
3 /*
4 Copyright (c) 2007, Till Brehm, projektfarm Gmbh
5 Copyright (c) 2014, Marius Cramer, pixcept KG
6 All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without modification,
9 are permitted provided that the following conditions are met:
10
11     * Redistributions of source code must retain the above copyright notice,
12       this list of conditions and the following disclaimer.
13     * Redistributions in binary form must reproduce the above copyright notice,
14       this list of conditions and the following disclaimer in the documentation
15       and/or other materials provided with the distribution.
16     * Neither the name of ISPConfig nor the names of its contributors
17       may be used to endorse or promote products derived from this software without
18       specific prior written permission.
19
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
27 OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 class validate_password {
33     
34     private function _get_password_strength($password) {
35         $length = strlen($password);
36         $points = 0;
37         if ($length < 5) {
38             return 1;
39         }
40
c9e684 41         $different = 0;
MC 42         if (preg_match('/[abcdefghijklnmopqrstuvwxyz]/', $password)) {
43             $different += 1;
44         }
45
7b9388 46         if (preg_match('/[ABCDEFGHIJKLNMOPQRSTUVWXYZ]/', $password)) {
MC 47             $points += 1;
c9e684 48             $different += 1;
7b9388 49         }
MC 50
51         if (preg_match('/[0123456789]/', $password)) {
52             $points += 1;
c9e684 53             $different += 1;
7b9388 54         }
MC 55
56         if (preg_match('/[`~!@#$%^&*()_+|\\=-[]}{\';:\/?.>,<" ]/', $password)) {
57             $points += 1;
c9e684 58             $different += 1;
7b9388 59         }
MC 60
c9e684 61         if ($points == 0 || $different < 3) {
7b9388 62             if ($length >= 5 && $length <= 6) {
MC 63                 return 1;
64             } else if ($length >= 7 && $length <= 8) {
65                 return 2;
66             } else {
67                 return 3;
68             }
69         } else if ($points == 1) {
70             if ($length >= 5 && $length <= 6) {
71                 return 2;
72             } else if (length >= 7 && length <=10) {
73                 return 3;
74             } else {
75                 return 4;
76             }
77         } else if ($points == 2) {
78             if ($length >= 5 && $length <= 8) {
79                 return 3;
80             } else if ($length >= 9 && $length <= 10) {
81                 return 4;
82             } else {
83                 return 5;
84             }
85         } else if ($points == 3) {
86             if ($length >= 5 && $length <= 6) {
87                 return 3;
88             } else if ($length >= 7 && $length <= 8) {
89                 return 4;
90             } else {
91                 return 5;
92             }
93         } else if ($points >= 4) {
94             if ($length >= 5 && $length <= 6) {
95                 return 4;
96             } else {
97                 return 5;
98             }
99         }
100         
101     }
102     
103     /* Validator function */
104     function password_check($field_name, $field_value, $validator) {
105         global $app;
106         
c9974f 107         if($field_value == '') return false;
MC 108         
7b9388 109         $app->uses('ini_parser,getconf');
MC 110         $server_config_array = $app->getconf->get_global_config();
111         
112         $min_password_strength = 0;
113         $min_password_length = 5;
114         if(isset($server_config_array['misc']['min_password_length'])) $min_password_length = $server_config_array['misc']['min_password_length'];
115         if(isset($server_config_array['misc']['min_password_strength'])) $min_password_strength = $server_config_array['misc']['min_password_strength'];
116         
117         if($min_password_strength > 0) {
118             $lng_text = $app->lng('weak_password_txt');
119             $lng_text = str_replace(array('{chars}', '{strength}'), array($min_password_length, $app->lng('strength_' . $min_password_strength)), $lng_text);
120         } else {
121             $lng_text = $app->lng('weak_password_length_txt');
122             $lng_text = str_replace('{chars}', $min_password_length, $lng_text);
123         }
124         if(!$lng_text) $lng_text = 'weak_password_txt'; // always return a string, even if language is missing - otherwise validator is NOT MATCHING!
125         
126         if(strlen($field_value) < $min_password_length) return $lng_text;
127         if($this->_get_password_strength($field_value) < $min_password_strength) return $lng_text;
128         
129         return false;
130     }
131 }