audit.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /*
  3. Copyright (c) 2009-2019 F3::Factory/Bong Cosca, All rights reserved.
  4. This file is part of the Fat-Free Framework (http://fatfreeframework.com).
  5. This is free software: you can redistribute it and/or modify it under the
  6. terms of the GNU General Public License as published by the Free Software
  7. Foundation, either version 3 of the License, or later.
  8. Fat-Free Framework is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with Fat-Free Framework. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. //! Data validator
  16. class Audit extends Prefab {
  17. //@{ User agents
  18. const
  19. UA_Mobile='android|blackberry|phone|ipod|palm|windows\s+ce',
  20. UA_Desktop='bsd|linux|os\s+[x9]|solaris|windows',
  21. UA_Bot='bot|crawl|slurp|spider';
  22. //@}
  23. /**
  24. * Return TRUE if string is a valid URL
  25. * @return bool
  26. * @param $str string
  27. **/
  28. function url($str) {
  29. return is_string(filter_var($str,FILTER_VALIDATE_URL));
  30. }
  31. /**
  32. * Return TRUE if string is a valid e-mail address;
  33. * Check DNS MX records if specified
  34. * @return bool
  35. * @param $str string
  36. * @param $mx boolean
  37. **/
  38. function email($str,$mx=TRUE) {
  39. $hosts=[];
  40. return is_string(filter_var($str,FILTER_VALIDATE_EMAIL)) &&
  41. (!$mx || getmxrr(substr($str,strrpos($str,'@')+1),$hosts));
  42. }
  43. /**
  44. * Return TRUE if string is a valid IPV4 address
  45. * @return bool
  46. * @param $addr string
  47. **/
  48. function ipv4($addr) {
  49. return (bool)filter_var($addr,FILTER_VALIDATE_IP,FILTER_FLAG_IPV4);
  50. }
  51. /**
  52. * Return TRUE if string is a valid IPV6 address
  53. * @return bool
  54. * @param $addr string
  55. **/
  56. function ipv6($addr) {
  57. return (bool)filter_var($addr,FILTER_VALIDATE_IP,FILTER_FLAG_IPV6);
  58. }
  59. /**
  60. * Return TRUE if IP address is within private range
  61. * @return bool
  62. * @param $addr string
  63. **/
  64. function isprivate($addr) {
  65. return !(bool)filter_var($addr,FILTER_VALIDATE_IP,
  66. FILTER_FLAG_IPV4|FILTER_FLAG_IPV6|FILTER_FLAG_NO_PRIV_RANGE);
  67. }
  68. /**
  69. * Return TRUE if IP address is within reserved range
  70. * @return bool
  71. * @param $addr string
  72. **/
  73. function isreserved($addr) {
  74. return !(bool)filter_var($addr,FILTER_VALIDATE_IP,
  75. FILTER_FLAG_IPV4|FILTER_FLAG_IPV6|FILTER_FLAG_NO_RES_RANGE);
  76. }
  77. /**
  78. * Return TRUE if IP address is neither private nor reserved
  79. * @return bool
  80. * @param $addr string
  81. **/
  82. function ispublic($addr) {
  83. return (bool)filter_var($addr,FILTER_VALIDATE_IP,
  84. FILTER_FLAG_IPV4|FILTER_FLAG_IPV6|
  85. FILTER_FLAG_NO_PRIV_RANGE|FILTER_FLAG_NO_RES_RANGE);
  86. }
  87. /**
  88. * Return TRUE if user agent is a desktop browser
  89. * @return bool
  90. * @param $agent string
  91. **/
  92. function isdesktop($agent=NULL) {
  93. if (!isset($agent))
  94. $agent=Base::instance()->AGENT;
  95. return (bool)preg_match('/('.self::UA_Desktop.')/i',$agent) &&
  96. !$this->ismobile($agent);
  97. }
  98. /**
  99. * Return TRUE if user agent is a mobile device
  100. * @return bool
  101. * @param $agent string
  102. **/
  103. function ismobile($agent=NULL) {
  104. if (!isset($agent))
  105. $agent=Base::instance()->AGENT;
  106. return (bool)preg_match('/('.self::UA_Mobile.')/i',$agent);
  107. }
  108. /**
  109. * Return TRUE if user agent is a Web bot
  110. * @return bool
  111. * @param $agent string
  112. **/
  113. function isbot($agent=NULL) {
  114. if (!isset($agent))
  115. $agent=Base::instance()->AGENT;
  116. return (bool)preg_match('/('.self::UA_Bot.')/i',$agent);
  117. }
  118. /**
  119. * Return TRUE if specified ID has a valid (Luhn) Mod-10 check digit
  120. * @return bool
  121. * @param $id string
  122. **/
  123. function mod10($id) {
  124. if (!ctype_digit($id))
  125. return FALSE;
  126. $id=strrev($id);
  127. $sum=0;
  128. for ($i=0,$l=strlen($id);$i<$l;$i++)
  129. $sum+=$id[$i]+$i%2*(($id[$i]>4)*-4+$id[$i]%5);
  130. return !($sum%10);
  131. }
  132. /**
  133. * Return credit card type if number is valid
  134. * @return string|FALSE
  135. * @param $id string
  136. **/
  137. function card($id) {
  138. $id=preg_replace('/[^\d]/','',$id);
  139. if ($this->mod10($id)) {
  140. if (preg_match('/^3[47][0-9]{13}$/',$id))
  141. return 'American Express';
  142. if (preg_match('/^3(?:0[0-5]|[68][0-9])[0-9]{11}$/',$id))
  143. return 'Diners Club';
  144. if (preg_match('/^6(?:011|5[0-9][0-9])[0-9]{12}$/',$id))
  145. return 'Discover';
  146. if (preg_match('/^(?:2131|1800|35\d{3})\d{11}$/',$id))
  147. return 'JCB';
  148. if (preg_match('/^5[1-5][0-9]{14}$|'.
  149. '^(222[1-9]|2[3-6]\d{2}|27[0-1]\d|2720)\d{12}$/',$id))
  150. return 'MasterCard';
  151. if (preg_match('/^4[0-9]{12}(?:[0-9]{3})?$/',$id))
  152. return 'Visa';
  153. }
  154. return FALSE;
  155. }
  156. /**
  157. * Return entropy estimate of a password (NIST 800-63)
  158. * @return int|float
  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. }