Marius Burkard
2016-04-20 4569cae57f127afd093794310ccd290d2d9fdf36
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);
d22277 36         
7b9388 37         $points = 0;
MC 38         if ($length < 5) {
39             return 1;
40         }
41
c9e684 42         $different = 0;
MC 43         if (preg_match('/[abcdefghijklnmopqrstuvwxyz]/', $password)) {
44             $different += 1;
45         }
46
7b9388 47         if (preg_match('/[ABCDEFGHIJKLNMOPQRSTUVWXYZ]/', $password)) {
MC 48             $points += 1;
c9e684 49             $different += 1;
7b9388 50         }
MC 51
52         if (preg_match('/[0123456789]/', $password)) {
53             $points += 1;
c9e684 54             $different += 1;
7b9388 55         }
MC 56
d22277 57         if (preg_match('/[`~!@#$%^&*()_+|\\=\-\[\]}{\';:\/?.>,<" ]/', $password)) {
7b9388 58             $points += 1;
c9e684 59             $different += 1;
7b9388 60         }
2c49c5 61         
7b9388 62
c9e684 63         if ($points == 0 || $different < 3) {
7b9388 64             if ($length >= 5 && $length <= 6) {
MC 65                 return 1;
66             } else if ($length >= 7 && $length <= 8) {
67                 return 2;
68             } else {
69                 return 3;
70             }
71         } else if ($points == 1) {
72             if ($length >= 5 && $length <= 6) {
73                 return 2;
74             } else if (length >= 7 && length <=10) {
75                 return 3;
76             } else {
77                 return 4;
78             }
79         } else if ($points == 2) {
80             if ($length >= 5 && $length <= 8) {
81                 return 3;
82             } else if ($length >= 9 && $length <= 10) {
83                 return 4;
84             } else {
85                 return 5;
86             }
87         } else if ($points == 3) {
88             if ($length >= 5 && $length <= 6) {
89                 return 3;
90             } else if ($length >= 7 && $length <= 8) {
91                 return 4;
92             } else {
93                 return 5;
94             }
95         } else if ($points >= 4) {
96             if ($length >= 5 && $length <= 6) {
97                 return 4;
98             } else {
99                 return 5;
100             }
101         }
102         
103     }
104     
105     /* Validator function */
106     function password_check($field_name, $field_value, $validator) {
107         global $app;
108         
c9974f 109         if($field_value == '') return false;
MC 110         
7b9388 111         $app->uses('ini_parser,getconf');
MC 112         $server_config_array = $app->getconf->get_global_config();
113         
114         $min_password_strength = 0;
115         $min_password_length = 5;
116         if(isset($server_config_array['misc']['min_password_length'])) $min_password_length = $server_config_array['misc']['min_password_length'];
117         if(isset($server_config_array['misc']['min_password_strength'])) $min_password_strength = $server_config_array['misc']['min_password_strength'];
118         
119         if($min_password_strength > 0) {
120             $lng_text = $app->lng('weak_password_txt');
121             $lng_text = str_replace(array('{chars}', '{strength}'), array($min_password_length, $app->lng('strength_' . $min_password_strength)), $lng_text);
122         } else {
123             $lng_text = $app->lng('weak_password_length_txt');
124             $lng_text = str_replace('{chars}', $min_password_length, $lng_text);
125         }
126         if(!$lng_text) $lng_text = 'weak_password_txt'; // always return a string, even if language is missing - otherwise validator is NOT MATCHING!
2c49c5 127
7b9388 128         if(strlen($field_value) < $min_password_length) return $lng_text;
MC 129         if($this->_get_password_strength($field_value) < $min_password_strength) return $lng_text;
130         
131         return false;
132     }
133 }