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