Advertisement
sanjiisan

Untitled

Sep 4th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. //User.php
  2. <?php
  3.  
  4. abstract class User
  5. {
  6. protected $userName;
  7. protected $password;
  8. protected $wrongLoginCounter = 0;
  9. protected $age;
  10.  
  11. abstract public function checkLogin($username, $password);
  12.  
  13. public function setLogin($userName)
  14. {
  15. $this->userName = $userName;
  16. }
  17.  
  18. abstract public function setPassword($password);
  19.  
  20. abstract public function setAge($age);
  21.  
  22. final public function login($username, $password)
  23. {
  24. if ($this->wrongLoginCounter >= 3) {
  25. return false;
  26. }
  27.  
  28. return $this->checkLogin($username, $password);
  29. }
  30. }
  31. ////Admin.php
  32. <?php
  33. require __DIR__ . '/User.php';
  34.  
  35. class Admin extends User
  36. {
  37. public function setAge($age)
  38. {
  39. $this->age = $age;
  40. }
  41.  
  42. public function checkLogin($username, $password)
  43. {
  44. if (!$username || $username != $this->userName || $this->password != $password) {
  45. $this->wrongLoginCounter++;
  46. return false;
  47. }
  48. $addressParts = explode('.', $_SERVER['REMOTE_ADDR']);
  49.  
  50. $validAddress = [127, 192];
  51.  
  52. if (!in_array($addressParts[0], $validAddress)) {
  53. $this->wrongLoginCounter++;
  54. return false;
  55. }
  56.  
  57. return true;
  58. }
  59.  
  60. public function setPassword($password)
  61. {
  62. if (strlen($password) < 10 || !is_int($this->age) || $this->age <= 0) {
  63. return false;
  64. }
  65.  
  66. $this->password = $password;
  67. }
  68. }
  69.  
  70. $admin = new Admin();
  71. $admin->setLogin('Paweł');
  72. $admin->setAge(23);
  73. $admin->setPassword('tajneHasłoPaweła');
  74.  
  75. echo '<pre>';
  76. var_dump($admin->login('Paweł', 'asd'));
  77. echo '</pre>';
  78. die;;
  79.  
  80.  
  81. //Client.php
  82. <?php
  83. require __DIR__ . '/User.php';
  84.  
  85. class Client extends User
  86. {
  87. public function setAge($age)
  88. {
  89. $this->age = $age;
  90. }
  91.  
  92. public function checkLogin($username, $password)
  93. {
  94. if (!$username || $username != $this->userName || $this->password != $password) {
  95. $this->wrongLoginCounter++;
  96. return false;
  97. }
  98.  
  99. $this->wrongLoginCounter = 0;
  100.  
  101. return true;
  102. }
  103.  
  104. public function setPassword($password)
  105. {
  106. if (strlen($password) < 8 || !is_int($this->age) || $this->age <= 17) {
  107. return false;
  108. }
  109.  
  110. $this->password = $password;
  111. }
  112. }
  113.  
  114. $client = new Client();
  115. $client->setLogin('Paweł');
  116. $client->setAge(23);
  117. $client->setPassword('tajneHasłoPaweła');
  118.  
  119. echo '<pre>';
  120. var_dump($client->login('Paweł', 'asd'));
  121. echo '</pre>';
  122. die;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement