|
16 | 16 | //! Data validator |
17 | 17 | class Audit extends Prefab { |
18 | 18 |
|
| 19 | + //@{ User agents |
| 20 | + const |
| 21 | + UA_Mobile='android|blackberry|iphone|ipod|palm|windows\s+ce', |
| 22 | + UA_Desktop='bsd|linux|os\s+[x9]|solaris|windows', |
| 23 | + UA_Bot='bot|crawl|slurp|spider'; |
| 24 | + //@} |
| 25 | + |
19 | 26 | /** |
20 | 27 | * Return TRUE if string is a valid URL |
21 | 28 | * @return bool |
@@ -87,6 +94,26 @@ function ispublic($addr) { |
87 | 94 | FILTER_FLAG_NO_PRIV_RANGE|FILTER_FLAG_NO_RES_RANGE); |
88 | 95 | } |
89 | 96 |
|
| 97 | + /** |
| 98 | + * Return TRUE if user agent is a desktop browser |
| 99 | + * @return bool |
| 100 | + **/ |
| 101 | + function isdesktop() { |
| 102 | + $agent=Base::instance()->get('AGENT'); |
| 103 | + return empty($agent) || |
| 104 | + (!preg_match('/('.self::UA_Mobile.')/i',$agent) && |
| 105 | + preg_match('/('.self::UA_Desktop.')/i',$agent) || |
| 106 | + preg_match('/('.self::UA_Bot.')/i',$agent)); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Return TRUE if user agent is a mobile device |
| 111 | + * @return bool |
| 112 | + **/ |
| 113 | + function ismobile() { |
| 114 | + return !$this->isdesktop(); |
| 115 | + } |
| 116 | + |
90 | 117 | /** |
91 | 118 | * Return TRUE if specified ID has a valid (Luhn) Mod-10 check digit |
92 | 119 | * @return bool |
@@ -126,4 +153,17 @@ function card($id) { |
126 | 153 | return FALSE; |
127 | 154 | } |
128 | 155 |
|
| 156 | + /** |
| 157 | + * Return entropy estimate of a password (NIST 800-63) |
| 158 | + * @return int |
| 159 | + * @param $str string |
| 160 | + **/ |
| 161 | + function entropy($str) { |
| 162 | + $len=strlen($str); |
| 163 | + return 4*min($len,1)+($len>1?(2*(min($len,8)-1)):0)+ |
| 164 | + ($len>8?(1.5*(min($len,20)-8)):0)+($len>20?($len-20):0)+ |
| 165 | + 6*(bool)(preg_match( |
| 166 | + '/[A-Z].*?[0-9[:punct:]]|[0-9[:punct:]].*?[A-Z]/',$str)); |
| 167 | + } |
| 168 | + |
129 | 169 | } |
0 commit comments