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