Advertisement
Guest User

Untitled

a guest
Aug 25th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. <?php
  2.  
  3. $ip = '192.168.1.5';
  4. $ips = [
  5. '192.168.1.3',
  6. '192.168.1.4',
  7. '192.168.1.5',
  8. ];
  9.  
  10. foreach ($ips as $i) {
  11. if ($ip === $i) {
  12. goto allowed;
  13. }
  14. }
  15.  
  16. throw new Exception('Not allowed');
  17.  
  18. allowed:
  19.  
  20. ...
  21.  
  22. $allowed = false;
  23.  
  24. foreach ($ips as $i) {
  25. if ($ip === $i) {
  26. $allowed = true;
  27. break;
  28. }
  29. }
  30.  
  31. if (!$allowed) {
  32. throw new Exception('Not allowed');
  33. }
  34.  
  35. if (!in_array($ip, $ips)) throw new Exception('Not allowed');
  36.  
  37. if (!contains($ips, $ip)) throw new Exception('Not allowed');
  38.  
  39. $list_contains_ip = undef; # STATE: we don't know yet
  40.  
  41. foreach ($ips as $i) {
  42. if ($ip === $i) {
  43. $list_contains_ip = true; # STATE: positive
  44. break;
  45. }
  46. # STATE: we still don't know yet, huh?
  47. }
  48. # Well, then...
  49. $list_contains_ip = false; # STATE: negative
  50.  
  51. if (!$list_contains_ip) {
  52. throw new Exception('Not allowed');
  53. }
  54.  
  55. # STATE: unknown
  56. foreach ($ips as $i) { # What are we checking here anyway?
  57. if ($ip === $i) {
  58. goto allowed; # STATE: positive
  59. }
  60. # STATE: unknown
  61. }
  62. # guess this means STATE: negative
  63. throw new Exception('Not allowed');
  64.  
  65. allowed: # Guess we jumped over the trap door
  66.  
  67. if ($ip =~ /:/) goto IP_V6;
  68. if ($ip =~ ///) goto IP_RANGE;
  69. if ($ip =~ /^10./) goto IP_IS_PRIVATE;
  70.  
  71. foreach ($ips as $i) { ... }
  72.  
  73. IP_IS_PRIVATE:
  74. foreach ($ip_priv as $i) { ... }
  75.  
  76. IP_V6:
  77. foreach ($ipv6 as $i) { ... }
  78.  
  79. IP_RANGE:
  80. # i don't even want to know how you'd implement that
  81.  
  82. ALLOWED:
  83. # Wait, is this code even correct?
  84. # There seems to be a bug in here.
  85.  
  86. func foo(){
  87. foreach ($ips as $i) {
  88. if ($ip === $i) {
  89. return;
  90. }
  91. }
  92.  
  93. throw new Exception('Not allowed');
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement