PageRenderTime 32ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/users/AuthenticationModule.php

http://showslow.googlecode.com/
PHP | 169 lines | 107 code | 21 blank | 41 comment | 5 complexity | ed8fd971f22e5b0e93b8d312bd0028eb MD5 | raw file
  1. <?php
  2. /**
  3. * @package StartupAPI
  4. * @subpackage Authentication
  5. */
  6. interface IAuthenticationModule extends IUserBaseModule
  7. {
  8. public function renderLoginForm($action);
  9. public function renderRegistrationForm($full = false, $action = null, $errors = null, $data = null);
  10. public function processLogin($data, &$remember);
  11. public function processAutoLogin();
  12. public function getAutoLogoutURL($return);
  13. public function renderAutoLogoutForm();
  14. public function processRegistration($data, &$remember);
  15. /**
  16. * This method should return user credentials object
  17. *
  18. * @param User $user User to get credentials for
  19. * @return UserCredentials User credentials object specific to the module
  20. */
  21. public function getUserCredentials($user);
  22. /**
  23. * This module returns total number of connections with provider
  24. * @return int Number of users who have connections through this provider
  25. * Some modules might allow for multiple connections, but the user is only counted once
  26. */
  27. public function getTotalConnectedUsers();
  28. }
  29. abstract class AuthenticationModule extends UserBaseModule implements IAuthenticationModule {
  30. public function __construct() {
  31. parent::__construct();
  32. UserConfig::$authentication_modules[] = $this;
  33. }
  34. /**
  35. * Returns authentication module by ID
  36. * @param string $id ID of the module
  37. */
  38. public static function get($id) {
  39. foreach (UserConfig::$authentication_modules as $module)
  40. {
  41. if ($module->getID() == $id) {
  42. return $module;
  43. }
  44. }
  45. }
  46. /**
  47. * By default, do not auto-login, should be overriden by modules that support auto-login
  48. */
  49. public function processAutoLogin() {
  50. return null;
  51. }
  52. /**
  53. * By default, modules do not support auto-logout, don't try doing that
  54. */
  55. public function getAutoLogoutURL($return) {
  56. return null;
  57. }
  58. /**
  59. * By default, modules do not support auto-logout
  60. */
  61. public function renderAutoLogoutForm() {
  62. return null;
  63. }
  64. /*
  65. * retrieves aggregated registrations numbers
  66. */
  67. public function getDailyRegistrations()
  68. {
  69. $db = UserConfig::getDB();
  70. $dailyregs = array();
  71. if ($stmt = $db->prepare('SELECT CAST(regtime AS DATE) AS regdate, count(*) AS regs FROM '.UserConfig::$mysql_prefix.'users WHERE regmodule = ? GROUP BY regdate'))
  72. {
  73. if (!$stmt->bind_param('s', $this->getID()))
  74. {
  75. throw new Exception("Can't bind parameter".$stmt->error);
  76. }
  77. if (!$stmt->execute())
  78. {
  79. throw new Exception("Can't execute statement: ".$stmt->error);
  80. }
  81. if (!$stmt->bind_result($regdate, $regs))
  82. {
  83. throw new Exception("Can't bind result: ".$stmt->error);
  84. }
  85. while($stmt->fetch() === TRUE)
  86. {
  87. $dailyregs[] = array('regdate' => $regdate, 'regs' => $regs);
  88. }
  89. $stmt->close();
  90. }
  91. else
  92. {
  93. throw new Exception("Can't prepare statement: ".$db->error);
  94. }
  95. return $dailyregs;
  96. }
  97. }
  98. class InputValidationException extends Exception {
  99. private $errors;
  100. public function __construct($string, $code, $errors)
  101. {
  102. parent::__construct($string, $code);
  103. $this->errors = $errors;
  104. }
  105. public function getErrors()
  106. {
  107. return $this->errors;
  108. }
  109. }
  110. class ExistingUserException extends Exception {
  111. private $errors;
  112. public function __construct($string, $code, $errors)
  113. {
  114. parent::__construct($string, $code);
  115. $this->errors = $errors;
  116. }
  117. public function getErrors()
  118. {
  119. return $this->errors;
  120. }
  121. }
  122. class UserCreationException extends Exception {
  123. private $field;
  124. public function __construct($string, $code, $field)
  125. {
  126. parent::__construct($string, $code);
  127. $this->field = $field;
  128. }
  129. public function getField()
  130. {
  131. return $this->field;
  132. }
  133. }
  134. /*
  135. * Class representing user credentials for particular module
  136. * Must be subclassed and implemented by module
  137. */
  138. abstract class UserCredentials {
  139. /**
  140. * This method should return HTML representation of user credentials to be included in admin interface
  141. * Usually linking to user's public profile at the source service
  142. *
  143. * @return string HTML representation of user credentials
  144. */
  145. public abstract function getHTML();
  146. }