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