@@ -17,169 +17,169 @@ discard block |
||
| 17 | 17 | |
| 18 | 18 | class PreferenceManager |
| 19 | 19 | { |
| 20 | - const PREF_WELCOMETEMPLATE = 'welcomeTemplate'; |
|
| 21 | - const PREF_SKIP_JS_ABORT = 'skipJsAbort'; |
|
| 22 | - const PREF_EMAIL_SIGNATURE = 'emailSignature'; |
|
| 23 | - const PREF_CREATION_MODE = 'creationMode'; |
|
| 24 | - const PREF_SKIN = 'skin'; |
|
| 25 | - const PREF_DEFAULT_DOMAIN = 'defaultDomain'; |
|
| 26 | - const PREF_QUEUE_HELP = 'showQueueHelp'; |
|
| 27 | - const CREATION_MANUAL = 0; |
|
| 28 | - const CREATION_OAUTH = 1; |
|
| 29 | - const CREATION_BOT = 2; |
|
| 30 | - /** @var PdoDatabase */ |
|
| 31 | - private $database; |
|
| 32 | - /** @var int */ |
|
| 33 | - private $user; |
|
| 34 | - /** @var ?int */ |
|
| 35 | - private $domain; |
|
| 36 | - /** @var PreferenceManager|null */ |
|
| 37 | - private static $currentUser = null; |
|
| 38 | - private $cachedPreferences = null; |
|
| 39 | - |
|
| 40 | - public function __construct(PdoDatabase $database, int $user, ?int $domain) |
|
| 41 | - { |
|
| 42 | - $this->database = $database; |
|
| 43 | - $this->user = $user; |
|
| 44 | - $this->domain = $domain; |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - public static function getForCurrent(PdoDatabase $database): PreferenceManager |
|
| 48 | - { |
|
| 49 | - if (self::$currentUser === null) { |
|
| 50 | - $user = User::getCurrent($database)->getId(); |
|
| 51 | - $domain = WebRequest::getSessionDomain(); |
|
| 52 | - |
|
| 53 | - self::$currentUser = new self($database, $user, $domain); |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - return self::$currentUser; |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - public function setLocalPreference(string $preference, $value): void |
|
| 60 | - { |
|
| 61 | - if ($this->cachedPreferences === null) { |
|
| 62 | - $this->loadPreferences(); |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - if ($this->cachedPreferences[$preference]['value'] == $value |
|
| 66 | - && $this->cachedPreferences[$preference]['global'] === false) { |
|
| 67 | - return; |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - $localPreference = UserPreference::getLocalPreference($this->database, $this->user, $preference, $this->domain); |
|
| 71 | - if ($localPreference === false) { |
|
| 72 | - $localPreference = new UserPreference(); |
|
| 73 | - $localPreference->setDatabase($this->database); |
|
| 74 | - $localPreference->setDomain($this->domain); |
|
| 75 | - $localPreference->setUser($this->user); |
|
| 76 | - $localPreference->setPreference($preference); |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - $localPreference->setValue($value); |
|
| 80 | - $localPreference->save(); |
|
| 81 | - |
|
| 82 | - $this->cachedPreferences[$preference] = [ |
|
| 83 | - 'value' => $value, |
|
| 84 | - 'global' => false, |
|
| 85 | - ]; |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - public function setGlobalPreference(string $preference, $value): void |
|
| 89 | - { |
|
| 90 | - if ($this->cachedPreferences === null) { |
|
| 91 | - $this->loadPreferences(); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - if ($this->cachedPreferences[$preference]['value'] == $value |
|
| 95 | - && $this->cachedPreferences[$preference]['global'] === true) { |
|
| 96 | - return; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - $this->deleteLocalPreference($preference); |
|
| 100 | - |
|
| 101 | - $globalPreference = UserPreference::getGlobalPreference($this->database, $this->user, $preference); |
|
| 102 | - if ($globalPreference === false) { |
|
| 103 | - $globalPreference = new UserPreference(); |
|
| 104 | - $globalPreference->setDatabase($this->database); |
|
| 105 | - $globalPreference->setDomain(null); |
|
| 106 | - $globalPreference->setUser($this->user); |
|
| 107 | - $globalPreference->setPreference($preference); |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - $globalPreference->setValue($value); |
|
| 111 | - $globalPreference->save(); |
|
| 112 | - |
|
| 113 | - $this->cachedPreferences[$preference] = [ |
|
| 114 | - 'value' => $value, |
|
| 115 | - 'global' => true, |
|
| 116 | - ]; |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - public function getPreference(string $preference) |
|
| 120 | - { |
|
| 121 | - if ($this->cachedPreferences === null) { |
|
| 122 | - $this->loadPreferences(); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - if (!isset($this->cachedPreferences[$preference])) { |
|
| 126 | - return null; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - return $this->cachedPreferences[$preference]['value']; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - public function isGlobalPreference(string $preference) : ?bool |
|
| 133 | - { |
|
| 134 | - if ($this->cachedPreferences === null) { |
|
| 135 | - $this->loadPreferences(); |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - if (!isset($this->cachedPreferences[$preference])) { |
|
| 139 | - return null; |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - return $this->cachedPreferences[$preference]['global']; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - protected function deleteLocalPreference(string $preference): void |
|
| 146 | - { |
|
| 147 | - $getStatement = $this->database->prepare('SELECT * FROM userpreference WHERE preference = :preference AND USER = :user AND domain = :domain'); |
|
| 148 | - $getStatement->execute([ |
|
| 149 | - ':user' => $this->user, |
|
| 150 | - ':preference' => $preference, |
|
| 151 | - ':domain' => $this->domain |
|
| 152 | - ]); |
|
| 153 | - |
|
| 154 | - $localPreference = $getStatement->fetchObject(UserPreference::class); |
|
| 155 | - if ($localPreference !== false) { |
|
| 156 | - $localPreference->setDatabase($this->database); |
|
| 157 | - $localPreference->delete(); |
|
| 158 | - } |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - protected function loadPreferences(): void |
|
| 162 | - { |
|
| 163 | - /** |
|
| 164 | - * OK, this is a bit of a complicated query. |
|
| 165 | - * It's designed to get all the preferences defined for a user in a specified domain, falling back to globally |
|
| 166 | - * defined preferences if a local preference isn't set. As such, this query is the *heart* of how global |
|
| 167 | - * preferences work. |
|
| 168 | - * |
|
| 169 | - * Starting with the WHERE, we filter rows: |
|
| 170 | - * a) where the row's domain is the domain we're looking for |
|
| 171 | - * b) where the row's domain is null, thus it's a global setting |
|
| 172 | - * c) if we don't have a domain we're looking for, fall back to global only |
|
| 173 | - * |
|
| 174 | - * The MAX(...) OVER(...) is a window function, *not* an aggregate. It basically takes the max of all selected |
|
| 175 | - * rows' domain columns, grouped by the preference column. Since any number N < null, this highlights all the |
|
| 176 | - * correct settings (local has precedence over global) such that prefpart == domain. |
|
| 177 | - * |
|
| 178 | - * -1 is used to represent null in the COALESCE() calls, since domain.id is an unsigned int hence -1 is an |
|
| 179 | - * impossible value |
|
| 180 | - */ |
|
| 181 | - $sql = /** @lang SQL */ |
|
| 182 | - <<<'EOF' |
|
| 20 | + const PREF_WELCOMETEMPLATE = 'welcomeTemplate'; |
|
| 21 | + const PREF_SKIP_JS_ABORT = 'skipJsAbort'; |
|
| 22 | + const PREF_EMAIL_SIGNATURE = 'emailSignature'; |
|
| 23 | + const PREF_CREATION_MODE = 'creationMode'; |
|
| 24 | + const PREF_SKIN = 'skin'; |
|
| 25 | + const PREF_DEFAULT_DOMAIN = 'defaultDomain'; |
|
| 26 | + const PREF_QUEUE_HELP = 'showQueueHelp'; |
|
| 27 | + const CREATION_MANUAL = 0; |
|
| 28 | + const CREATION_OAUTH = 1; |
|
| 29 | + const CREATION_BOT = 2; |
|
| 30 | + /** @var PdoDatabase */ |
|
| 31 | + private $database; |
|
| 32 | + /** @var int */ |
|
| 33 | + private $user; |
|
| 34 | + /** @var ?int */ |
|
| 35 | + private $domain; |
|
| 36 | + /** @var PreferenceManager|null */ |
|
| 37 | + private static $currentUser = null; |
|
| 38 | + private $cachedPreferences = null; |
|
| 39 | + |
|
| 40 | + public function __construct(PdoDatabase $database, int $user, ?int $domain) |
|
| 41 | + { |
|
| 42 | + $this->database = $database; |
|
| 43 | + $this->user = $user; |
|
| 44 | + $this->domain = $domain; |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + public static function getForCurrent(PdoDatabase $database): PreferenceManager |
|
| 48 | + { |
|
| 49 | + if (self::$currentUser === null) { |
|
| 50 | + $user = User::getCurrent($database)->getId(); |
|
| 51 | + $domain = WebRequest::getSessionDomain(); |
|
| 52 | + |
|
| 53 | + self::$currentUser = new self($database, $user, $domain); |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + return self::$currentUser; |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + public function setLocalPreference(string $preference, $value): void |
|
| 60 | + { |
|
| 61 | + if ($this->cachedPreferences === null) { |
|
| 62 | + $this->loadPreferences(); |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + if ($this->cachedPreferences[$preference]['value'] == $value |
|
| 66 | + && $this->cachedPreferences[$preference]['global'] === false) { |
|
| 67 | + return; |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + $localPreference = UserPreference::getLocalPreference($this->database, $this->user, $preference, $this->domain); |
|
| 71 | + if ($localPreference === false) { |
|
| 72 | + $localPreference = new UserPreference(); |
|
| 73 | + $localPreference->setDatabase($this->database); |
|
| 74 | + $localPreference->setDomain($this->domain); |
|
| 75 | + $localPreference->setUser($this->user); |
|
| 76 | + $localPreference->setPreference($preference); |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + $localPreference->setValue($value); |
|
| 80 | + $localPreference->save(); |
|
| 81 | + |
|
| 82 | + $this->cachedPreferences[$preference] = [ |
|
| 83 | + 'value' => $value, |
|
| 84 | + 'global' => false, |
|
| 85 | + ]; |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + public function setGlobalPreference(string $preference, $value): void |
|
| 89 | + { |
|
| 90 | + if ($this->cachedPreferences === null) { |
|
| 91 | + $this->loadPreferences(); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + if ($this->cachedPreferences[$preference]['value'] == $value |
|
| 95 | + && $this->cachedPreferences[$preference]['global'] === true) { |
|
| 96 | + return; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + $this->deleteLocalPreference($preference); |
|
| 100 | + |
|
| 101 | + $globalPreference = UserPreference::getGlobalPreference($this->database, $this->user, $preference); |
|
| 102 | + if ($globalPreference === false) { |
|
| 103 | + $globalPreference = new UserPreference(); |
|
| 104 | + $globalPreference->setDatabase($this->database); |
|
| 105 | + $globalPreference->setDomain(null); |
|
| 106 | + $globalPreference->setUser($this->user); |
|
| 107 | + $globalPreference->setPreference($preference); |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + $globalPreference->setValue($value); |
|
| 111 | + $globalPreference->save(); |
|
| 112 | + |
|
| 113 | + $this->cachedPreferences[$preference] = [ |
|
| 114 | + 'value' => $value, |
|
| 115 | + 'global' => true, |
|
| 116 | + ]; |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + public function getPreference(string $preference) |
|
| 120 | + { |
|
| 121 | + if ($this->cachedPreferences === null) { |
|
| 122 | + $this->loadPreferences(); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + if (!isset($this->cachedPreferences[$preference])) { |
|
| 126 | + return null; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + return $this->cachedPreferences[$preference]['value']; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + public function isGlobalPreference(string $preference) : ?bool |
|
| 133 | + { |
|
| 134 | + if ($this->cachedPreferences === null) { |
|
| 135 | + $this->loadPreferences(); |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + if (!isset($this->cachedPreferences[$preference])) { |
|
| 139 | + return null; |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + return $this->cachedPreferences[$preference]['global']; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + protected function deleteLocalPreference(string $preference): void |
|
| 146 | + { |
|
| 147 | + $getStatement = $this->database->prepare('SELECT * FROM userpreference WHERE preference = :preference AND USER = :user AND domain = :domain'); |
|
| 148 | + $getStatement->execute([ |
|
| 149 | + ':user' => $this->user, |
|
| 150 | + ':preference' => $preference, |
|
| 151 | + ':domain' => $this->domain |
|
| 152 | + ]); |
|
| 153 | + |
|
| 154 | + $localPreference = $getStatement->fetchObject(UserPreference::class); |
|
| 155 | + if ($localPreference !== false) { |
|
| 156 | + $localPreference->setDatabase($this->database); |
|
| 157 | + $localPreference->delete(); |
|
| 158 | + } |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + protected function loadPreferences(): void |
|
| 162 | + { |
|
| 163 | + /** |
|
| 164 | + * OK, this is a bit of a complicated query. |
|
| 165 | + * It's designed to get all the preferences defined for a user in a specified domain, falling back to globally |
|
| 166 | + * defined preferences if a local preference isn't set. As such, this query is the *heart* of how global |
|
| 167 | + * preferences work. |
|
| 168 | + * |
|
| 169 | + * Starting with the WHERE, we filter rows: |
|
| 170 | + * a) where the row's domain is the domain we're looking for |
|
| 171 | + * b) where the row's domain is null, thus it's a global setting |
|
| 172 | + * c) if we don't have a domain we're looking for, fall back to global only |
|
| 173 | + * |
|
| 174 | + * The MAX(...) OVER(...) is a window function, *not* an aggregate. It basically takes the max of all selected |
|
| 175 | + * rows' domain columns, grouped by the preference column. Since any number N < null, this highlights all the |
|
| 176 | + * correct settings (local has precedence over global) such that prefpart == domain. |
|
| 177 | + * |
|
| 178 | + * -1 is used to represent null in the COALESCE() calls, since domain.id is an unsigned int hence -1 is an |
|
| 179 | + * impossible value |
|
| 180 | + */ |
|
| 181 | + $sql = /** @lang SQL */ |
|
| 182 | + <<<'EOF' |
|
| 183 | 183 | WITH allprefs AS ( |
| 184 | 184 | SELECT up.domain, up.preference, MAX(up.domain) OVER (PARTITION BY up.preference) AS prefpart, up.value, CASE WHEN up.domain IS NULL THEN 1 END AS isglobal |
| 185 | 185 | FROM userpreference up |
@@ -190,24 +190,24 @@ discard block |
||
| 190 | 190 | FROM allprefs p |
| 191 | 191 | WHERE COALESCE(p.prefpart, -1) = COALESCE(p.domain, -1); |
| 192 | 192 | EOF; |
| 193 | - $statement = $this->database->prepare($sql); |
|
| 194 | - |
|
| 195 | - $statement->execute([ |
|
| 196 | - ':domain' => $this->domain, |
|
| 197 | - ':domainc' => $this->domain, |
|
| 198 | - ':user' => $this->user, |
|
| 199 | - ]); |
|
| 200 | - |
|
| 201 | - $rawPrefs = $statement->fetchAll(PDO::FETCH_ASSOC); |
|
| 202 | - $prefs = []; |
|
| 203 | - |
|
| 204 | - foreach ($rawPrefs as $p) { |
|
| 205 | - $prefs[$p['preference']] = [ |
|
| 206 | - 'value' => $p['value'], |
|
| 207 | - 'global' => $p['isglobal'] == 1, |
|
| 208 | - ]; |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - $this->cachedPreferences = $prefs; |
|
| 212 | - } |
|
| 193 | + $statement = $this->database->prepare($sql); |
|
| 194 | + |
|
| 195 | + $statement->execute([ |
|
| 196 | + ':domain' => $this->domain, |
|
| 197 | + ':domainc' => $this->domain, |
|
| 198 | + ':user' => $this->user, |
|
| 199 | + ]); |
|
| 200 | + |
|
| 201 | + $rawPrefs = $statement->fetchAll(PDO::FETCH_ASSOC); |
|
| 202 | + $prefs = []; |
|
| 203 | + |
|
| 204 | + foreach ($rawPrefs as $p) { |
|
| 205 | + $prefs[$p['preference']] = [ |
|
| 206 | + 'value' => $p['value'], |
|
| 207 | + 'global' => $p['isglobal'] == 1, |
|
| 208 | + ]; |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + $this->cachedPreferences = $prefs; |
|
| 212 | + } |
|
| 213 | 213 | } |
@@ -25,460 +25,460 @@ |
||
| 25 | 25 | |
| 26 | 26 | class OAuthUserHelper implements IMediaWikiClient |
| 27 | 27 | { |
| 28 | - const TOKEN_REQUEST = 'request'; |
|
| 29 | - const TOKEN_ACCESS = 'access'; |
|
| 30 | - /** @var PDOStatement */ |
|
| 31 | - private static $tokenCountStatement = null; |
|
| 32 | - /** @var PDOStatement */ |
|
| 33 | - private $getTokenStatement; |
|
| 34 | - /** |
|
| 35 | - * @var User |
|
| 36 | - */ |
|
| 37 | - private $user; |
|
| 38 | - /** |
|
| 39 | - * @var PdoDatabase |
|
| 40 | - */ |
|
| 41 | - private $database; |
|
| 42 | - /** |
|
| 43 | - * @var IOAuthProtocolHelper |
|
| 44 | - */ |
|
| 45 | - private $oauthProtocolHelper; |
|
| 46 | - /** |
|
| 47 | - * @var bool|null Is the user linked to OAuth |
|
| 48 | - */ |
|
| 49 | - private $linked; |
|
| 50 | - private $partiallyLinked; |
|
| 51 | - /** @var OAuthToken */ |
|
| 52 | - private $accessToken; |
|
| 53 | - /** @var bool */ |
|
| 54 | - private $accessTokenLoaded = false; |
|
| 55 | - /** |
|
| 56 | - * @var OAuthIdentity |
|
| 57 | - */ |
|
| 58 | - private $identity = null; |
|
| 59 | - /** |
|
| 60 | - * @var bool |
|
| 61 | - */ |
|
| 62 | - private $identityLoaded = false; |
|
| 63 | - /** |
|
| 64 | - * @var SiteConfiguration |
|
| 65 | - */ |
|
| 66 | - private $siteConfiguration; |
|
| 67 | - |
|
| 68 | - private $legacyTokens; |
|
| 69 | - |
|
| 70 | - #region Static methods |
|
| 71 | - |
|
| 72 | - public static function findUserByRequestToken($requestToken, PdoDatabase $database) |
|
| 73 | - { |
|
| 74 | - $statement = $database->prepare(<<<'SQL' |
|
| 28 | + const TOKEN_REQUEST = 'request'; |
|
| 29 | + const TOKEN_ACCESS = 'access'; |
|
| 30 | + /** @var PDOStatement */ |
|
| 31 | + private static $tokenCountStatement = null; |
|
| 32 | + /** @var PDOStatement */ |
|
| 33 | + private $getTokenStatement; |
|
| 34 | + /** |
|
| 35 | + * @var User |
|
| 36 | + */ |
|
| 37 | + private $user; |
|
| 38 | + /** |
|
| 39 | + * @var PdoDatabase |
|
| 40 | + */ |
|
| 41 | + private $database; |
|
| 42 | + /** |
|
| 43 | + * @var IOAuthProtocolHelper |
|
| 44 | + */ |
|
| 45 | + private $oauthProtocolHelper; |
|
| 46 | + /** |
|
| 47 | + * @var bool|null Is the user linked to OAuth |
|
| 48 | + */ |
|
| 49 | + private $linked; |
|
| 50 | + private $partiallyLinked; |
|
| 51 | + /** @var OAuthToken */ |
|
| 52 | + private $accessToken; |
|
| 53 | + /** @var bool */ |
|
| 54 | + private $accessTokenLoaded = false; |
|
| 55 | + /** |
|
| 56 | + * @var OAuthIdentity |
|
| 57 | + */ |
|
| 58 | + private $identity = null; |
|
| 59 | + /** |
|
| 60 | + * @var bool |
|
| 61 | + */ |
|
| 62 | + private $identityLoaded = false; |
|
| 63 | + /** |
|
| 64 | + * @var SiteConfiguration |
|
| 65 | + */ |
|
| 66 | + private $siteConfiguration; |
|
| 67 | + |
|
| 68 | + private $legacyTokens; |
|
| 69 | + |
|
| 70 | + #region Static methods |
|
| 71 | + |
|
| 72 | + public static function findUserByRequestToken($requestToken, PdoDatabase $database) |
|
| 73 | + { |
|
| 74 | + $statement = $database->prepare(<<<'SQL' |
|
| 75 | 75 | SELECT u.* FROM user u |
| 76 | 76 | INNER JOIN oauthtoken t ON t.user = u.id |
| 77 | 77 | WHERE t.type = :type AND t.token = :token |
| 78 | 78 | SQL |
| 79 | - ); |
|
| 80 | - $statement->execute(array(':type' => self::TOKEN_REQUEST, ':token' => $requestToken)); |
|
| 81 | - |
|
| 82 | - /** @var User $user */ |
|
| 83 | - $user = $statement->fetchObject(User::class); |
|
| 84 | - $statement->closeCursor(); |
|
| 85 | - |
|
| 86 | - if ($user === false) { |
|
| 87 | - throw new ApplicationLogicException('Token not found in store, please try again'); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - $user->setDatabase($database); |
|
| 91 | - |
|
| 92 | - return $user; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - public static function userIsFullyLinked(User $user, PdoDatabase $database = null) |
|
| 96 | - { |
|
| 97 | - if (self::$tokenCountStatement === null && $database === null) { |
|
| 98 | - throw new ApplicationLogicException('Static link request without initialised statement'); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - return self::runTokenCount($user->getId(), $database, self::TOKEN_ACCESS); |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - public static function userIsPartiallyLinked(User $user, PdoDatabase $database = null) |
|
| 105 | - { |
|
| 106 | - if (self::$tokenCountStatement === null && $database === null) { |
|
| 107 | - throw new ApplicationLogicException('Static link request without initialised statement'); |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - if (self::userIsFullyLinked($user, $database)) { |
|
| 111 | - return false; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - return self::runTokenCount($user->getId(), $database, self::TOKEN_REQUEST) |
|
| 115 | - || $user->getOnWikiName() == null; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * @param PdoDatabase $database |
|
| 120 | - */ |
|
| 121 | - public static function prepareTokenCountStatement(PdoDatabase $database) |
|
| 122 | - { |
|
| 123 | - if (self::$tokenCountStatement === null) { |
|
| 124 | - self::$tokenCountStatement = $database->prepare('SELECT COUNT(*) FROM oauthtoken WHERE user = :user AND type = :type'); |
|
| 125 | - } |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - private static function runTokenCount($userId, $database, $tokenType) |
|
| 129 | - { |
|
| 130 | - if (self::$tokenCountStatement === null) { |
|
| 131 | - self::prepareTokenCountStatement($database); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - self::$tokenCountStatement->execute(array( |
|
| 135 | - ':user' => $userId, |
|
| 136 | - ':type' => $tokenType, |
|
| 137 | - )); |
|
| 138 | - |
|
| 139 | - $tokenCount = self::$tokenCountStatement->fetchColumn(); |
|
| 140 | - $linked = $tokenCount > 0; |
|
| 141 | - self::$tokenCountStatement->closeCursor(); |
|
| 142 | - |
|
| 143 | - return $linked; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - #endregion Static methods |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * OAuthUserHelper constructor. |
|
| 150 | - * |
|
| 151 | - * @param User $user |
|
| 152 | - * @param PdoDatabase $database |
|
| 153 | - * @param IOAuthProtocolHelper $oauthProtocolHelper |
|
| 154 | - * @param SiteConfiguration $siteConfiguration |
|
| 155 | - */ |
|
| 156 | - public function __construct( |
|
| 157 | - User $user, |
|
| 158 | - PdoDatabase $database, |
|
| 159 | - IOAuthProtocolHelper $oauthProtocolHelper, |
|
| 160 | - SiteConfiguration $siteConfiguration |
|
| 161 | - ) { |
|
| 162 | - $this->user = $user; |
|
| 163 | - $this->database = $database; |
|
| 164 | - $this->oauthProtocolHelper = $oauthProtocolHelper; |
|
| 165 | - |
|
| 166 | - $this->linked = null; |
|
| 167 | - $this->partiallyLinked = null; |
|
| 168 | - $this->siteConfiguration = $siteConfiguration; |
|
| 169 | - |
|
| 170 | - self::prepareTokenCountStatement($database); |
|
| 171 | - $this->getTokenStatement = $this->database->prepare('SELECT * FROM oauthtoken WHERE user = :user AND type = :type'); |
|
| 172 | - |
|
| 173 | - $this->legacyTokens = $this->siteConfiguration->getOauthLegacyConsumerTokens(); |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * Determines if the user is fully connected to OAuth. |
|
| 178 | - * |
|
| 179 | - * @return bool |
|
| 180 | - */ |
|
| 181 | - public function isFullyLinked() |
|
| 182 | - { |
|
| 183 | - if ($this->linked === null) { |
|
| 184 | - $this->linked = self::userIsFullyLinked($this->user, $this->database); |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - return $this->linked; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * Attempts to figure out if a user is partially linked to OAuth, and therefore needs to complete the OAuth |
|
| 192 | - * procedure before configuring. |
|
| 193 | - * @return bool |
|
| 194 | - */ |
|
| 195 | - public function isPartiallyLinked() |
|
| 196 | - { |
|
| 197 | - if ($this->partiallyLinked === null) { |
|
| 198 | - $this->partiallyLinked = self::userIsPartiallyLinked($this->user, $this->database); |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - return $this->partiallyLinked; |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - public function canCreateAccount() |
|
| 205 | - { |
|
| 206 | - return $this->isFullyLinked() |
|
| 207 | - && $this->getIdentity(true)->getGrantBasic() |
|
| 208 | - && $this->getIdentity(true)->getGrantHighVolume() |
|
| 209 | - && $this->getIdentity(true)->getGrantCreateAccount(); |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - public function canWelcome() |
|
| 213 | - { |
|
| 214 | - return $this->isFullyLinked() |
|
| 215 | - && $this->getIdentity(true)->getGrantBasic() |
|
| 216 | - && $this->getIdentity(true)->getGrantHighVolume() |
|
| 217 | - && $this->getIdentity(true)->getGrantCreateEditMovePage(); |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - /** |
|
| 221 | - * @throws OAuthException |
|
| 222 | - * @throws CurlException |
|
| 223 | - * @throws OptimisticLockFailedException |
|
| 224 | - * @throws Exception |
|
| 225 | - */ |
|
| 226 | - public function refreshIdentity() |
|
| 227 | - { |
|
| 228 | - $this->loadIdentity(); |
|
| 229 | - |
|
| 230 | - if ($this->identity === null) { |
|
| 231 | - $this->identity = new OAuthIdentity(); |
|
| 232 | - $this->identity->setUserId($this->user->getId()); |
|
| 233 | - $this->identity->setDatabase($this->database); |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - $token = $this->loadAccessToken(); |
|
| 237 | - |
|
| 238 | - try { |
|
| 239 | - $rawTicket = $this->oauthProtocolHelper->getIdentityTicket($token->getToken(), $token->getSecret()); |
|
| 240 | - } |
|
| 241 | - catch (Exception $ex) { |
|
| 242 | - if (strpos($ex->getMessage(), "mwoauthdatastore-access-token-not-found") !== false) { |
|
| 243 | - throw new OAuthException('No approved grants for this access token.', -1, $ex); |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - throw $ex; |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - $this->identity->populate($rawTicket); |
|
| 250 | - |
|
| 251 | - if (!$this->identityIsValid()) { |
|
| 252 | - throw new OAuthException('Identity ticket is not valid!'); |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - $this->identity->save(); |
|
| 256 | - |
|
| 257 | - $this->user->setOnWikiName($this->identity->getUsername()); |
|
| 258 | - $this->user->save(); |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - /** |
|
| 262 | - * @return string |
|
| 263 | - * @throws CurlException |
|
| 264 | - */ |
|
| 265 | - public function getRequestToken() |
|
| 266 | - { |
|
| 267 | - $token = $this->oauthProtocolHelper->getRequestToken(); |
|
| 268 | - |
|
| 269 | - $this->partiallyLinked = true; |
|
| 270 | - $this->linked = false; |
|
| 271 | - |
|
| 272 | - $this->database |
|
| 273 | - ->prepare('DELETE FROM oauthtoken WHERE user = :user AND type = :type') |
|
| 274 | - ->execute(array(':user' => $this->user->getId(), ':type' => self::TOKEN_REQUEST)); |
|
| 275 | - |
|
| 276 | - $this->database |
|
| 277 | - ->prepare('INSERT INTO oauthtoken (user, type, token, secret, expiry) VALUES (:user, :type, :token, :secret, DATE_ADD(NOW(), INTERVAL 1 DAY))') |
|
| 278 | - ->execute(array( |
|
| 279 | - ':user' => $this->user->getId(), |
|
| 280 | - ':type' => self::TOKEN_REQUEST, |
|
| 281 | - ':token' => $token->key, |
|
| 282 | - ':secret' => $token->secret, |
|
| 283 | - )); |
|
| 284 | - |
|
| 285 | - return $this->oauthProtocolHelper->getAuthoriseUrl($token->key); |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - /** |
|
| 289 | - * @param $verificationToken |
|
| 290 | - * |
|
| 291 | - * @throws ApplicationLogicException |
|
| 292 | - * @throws CurlException |
|
| 293 | - * @throws OAuthException |
|
| 294 | - * @throws OptimisticLockFailedException |
|
| 295 | - */ |
|
| 296 | - public function completeHandshake($verificationToken) |
|
| 297 | - { |
|
| 298 | - $this->getTokenStatement->execute(array(':user' => $this->user->getId(), ':type' => self::TOKEN_REQUEST)); |
|
| 299 | - |
|
| 300 | - /** @var OAuthToken $token */ |
|
| 301 | - $token = $this->getTokenStatement->fetchObject(OAuthToken::class); |
|
| 302 | - $this->getTokenStatement->closeCursor(); |
|
| 303 | - |
|
| 304 | - if ($token === false) { |
|
| 305 | - throw new ApplicationLogicException('Cannot find request token'); |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - $token->setDatabase($this->database); |
|
| 309 | - |
|
| 310 | - $accessToken = $this->oauthProtocolHelper->callbackCompleted($token->getToken(), $token->getSecret(), |
|
| 311 | - $verificationToken); |
|
| 312 | - |
|
| 313 | - $clearStatement = $this->database->prepare('DELETE FROM oauthtoken WHERE user = :u AND type = :t'); |
|
| 314 | - $clearStatement->execute(array(':u' => $this->user->getId(), ':t' => self::TOKEN_ACCESS)); |
|
| 315 | - |
|
| 316 | - $token->setToken($accessToken->key); |
|
| 317 | - $token->setSecret($accessToken->secret); |
|
| 318 | - $token->setType(self::TOKEN_ACCESS); |
|
| 319 | - $token->setExpiry(null); |
|
| 320 | - $token->save(); |
|
| 321 | - |
|
| 322 | - $this->partiallyLinked = false; |
|
| 323 | - $this->linked = true; |
|
| 324 | - |
|
| 325 | - $this->refreshIdentity(); |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - public function detach() |
|
| 329 | - { |
|
| 330 | - $this->loadIdentity(); |
|
| 331 | - |
|
| 332 | - $this->identity->delete(); |
|
| 333 | - $statement = $this->database->prepare('DELETE FROM oauthtoken WHERE user = :user'); |
|
| 334 | - $statement->execute(array(':user' => $this->user->getId())); |
|
| 335 | - |
|
| 336 | - $this->identity = null; |
|
| 337 | - $this->linked = false; |
|
| 338 | - $this->partiallyLinked = false; |
|
| 339 | - } |
|
| 340 | - |
|
| 341 | - /** |
|
| 342 | - * @param bool $expiredOk |
|
| 343 | - * |
|
| 344 | - * @return OAuthIdentity |
|
| 345 | - * @throws OAuthException |
|
| 346 | - */ |
|
| 347 | - public function getIdentity($expiredOk = false) |
|
| 348 | - { |
|
| 349 | - $this->loadIdentity(); |
|
| 350 | - |
|
| 351 | - if (!$this->identityIsValid($expiredOk)) { |
|
| 352 | - throw new OAuthException('Stored identity is not valid.'); |
|
| 353 | - } |
|
| 354 | - |
|
| 355 | - return $this->identity; |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - public function doApiCall($params, $method) |
|
| 359 | - { |
|
| 360 | - // Ensure we're logged in |
|
| 361 | - $params['assert'] = 'user'; |
|
| 362 | - |
|
| 363 | - $token = $this->loadAccessToken(); |
|
| 364 | - return $this->oauthProtocolHelper->apiCall($params, $token->getToken(), $token->getSecret(), $method); |
|
| 365 | - } |
|
| 366 | - |
|
| 367 | - /** |
|
| 368 | - * @param bool $expiredOk |
|
| 369 | - * |
|
| 370 | - * @return bool |
|
| 371 | - */ |
|
| 372 | - private function identityIsValid($expiredOk = false) |
|
| 373 | - { |
|
| 374 | - $this->loadIdentity(); |
|
| 375 | - |
|
| 376 | - if ($this->identity === null) { |
|
| 377 | - return false; |
|
| 378 | - } |
|
| 379 | - |
|
| 380 | - if ($this->identity->getIssuedAtTime() === false |
|
| 381 | - || $this->identity->getExpirationTime() === false |
|
| 382 | - || $this->identity->getAudience() === false |
|
| 383 | - || $this->identity->getIssuer() === false |
|
| 384 | - ) { |
|
| 385 | - // this isn't populated properly. |
|
| 386 | - return false; |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - $issue = DateTimeImmutable::createFromFormat("U", $this->identity->getIssuedAtTime()); |
|
| 390 | - $now = new DateTimeImmutable(); |
|
| 391 | - |
|
| 392 | - if ($issue > $now) { |
|
| 393 | - // wat. |
|
| 394 | - return false; |
|
| 395 | - } |
|
| 396 | - |
|
| 397 | - if ($this->identityExpired() && !$expiredOk) { |
|
| 398 | - // soz. |
|
| 399 | - return false; |
|
| 400 | - } |
|
| 401 | - |
|
| 402 | - if ($this->identity->getAudience() !== $this->siteConfiguration->getOAuthConsumerToken()) { |
|
| 403 | - // token not issued for us |
|
| 404 | - |
|
| 405 | - // we allow cases where the cache is expired and the cache is for a legacy token |
|
| 406 | - if (!($expiredOk && in_array($this->identity->getAudience(), $this->legacyTokens))) { |
|
| 407 | - return false; |
|
| 408 | - } |
|
| 409 | - } |
|
| 410 | - |
|
| 411 | - if ($this->identity->getIssuer() !== $this->siteConfiguration->getOauthMediaWikiCanonicalServer()) { |
|
| 412 | - // token not issued by the right person |
|
| 413 | - return false; |
|
| 414 | - } |
|
| 415 | - |
|
| 416 | - // can't find a reason to not trust it |
|
| 417 | - return true; |
|
| 418 | - } |
|
| 419 | - |
|
| 420 | - /** |
|
| 421 | - * @return bool |
|
| 422 | - */ |
|
| 423 | - public function identityExpired() |
|
| 424 | - { |
|
| 425 | - // allowed max age |
|
| 426 | - $gracePeriod = $this->siteConfiguration->getOauthIdentityGraceTime(); |
|
| 427 | - |
|
| 428 | - $expiry = DateTimeImmutable::createFromFormat("U", $this->identity->getExpirationTime()); |
|
| 429 | - $graceExpiry = $expiry->modify($gracePeriod); |
|
| 430 | - $now = new DateTimeImmutable(); |
|
| 431 | - |
|
| 432 | - return $graceExpiry < $now; |
|
| 433 | - } |
|
| 434 | - |
|
| 435 | - /** |
|
| 436 | - * Loads the OAuth identity from the database for the current user. |
|
| 437 | - */ |
|
| 438 | - private function loadIdentity() |
|
| 439 | - { |
|
| 440 | - if ($this->identityLoaded) { |
|
| 441 | - return; |
|
| 442 | - } |
|
| 443 | - |
|
| 444 | - $statement = $this->database->prepare('SELECT * FROM oauthidentity WHERE user = :user'); |
|
| 445 | - $statement->execute(array(':user' => $this->user->getId())); |
|
| 446 | - /** @var OAuthIdentity $obj */ |
|
| 447 | - $obj = $statement->fetchObject(OAuthIdentity::class); |
|
| 448 | - |
|
| 449 | - if ($obj === false) { |
|
| 450 | - // failed to load identity. |
|
| 451 | - $this->identityLoaded = true; |
|
| 452 | - $this->identity = null; |
|
| 453 | - |
|
| 454 | - return; |
|
| 455 | - } |
|
| 456 | - |
|
| 457 | - $obj->setDatabase($this->database); |
|
| 458 | - $this->identityLoaded = true; |
|
| 459 | - $this->identity = $obj; |
|
| 460 | - } |
|
| 461 | - |
|
| 462 | - /** |
|
| 463 | - * @return OAuthToken |
|
| 464 | - * @throws OAuthException |
|
| 465 | - */ |
|
| 466 | - private function loadAccessToken() |
|
| 467 | - { |
|
| 468 | - if (!$this->accessTokenLoaded) { |
|
| 469 | - $this->getTokenStatement->execute(array(':user' => $this->user->getId(), ':type' => self::TOKEN_ACCESS)); |
|
| 470 | - /** @var OAuthToken $token */ |
|
| 471 | - $token = $this->getTokenStatement->fetchObject(OAuthToken::class); |
|
| 472 | - $this->getTokenStatement->closeCursor(); |
|
| 473 | - |
|
| 474 | - if ($token === false) { |
|
| 475 | - throw new OAuthException('Access token not found!'); |
|
| 476 | - } |
|
| 477 | - |
|
| 478 | - $this->accessToken = $token; |
|
| 479 | - $this->accessTokenLoaded = true; |
|
| 480 | - } |
|
| 481 | - |
|
| 482 | - return $this->accessToken; |
|
| 483 | - } |
|
| 79 | + ); |
|
| 80 | + $statement->execute(array(':type' => self::TOKEN_REQUEST, ':token' => $requestToken)); |
|
| 81 | + |
|
| 82 | + /** @var User $user */ |
|
| 83 | + $user = $statement->fetchObject(User::class); |
|
| 84 | + $statement->closeCursor(); |
|
| 85 | + |
|
| 86 | + if ($user === false) { |
|
| 87 | + throw new ApplicationLogicException('Token not found in store, please try again'); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + $user->setDatabase($database); |
|
| 91 | + |
|
| 92 | + return $user; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + public static function userIsFullyLinked(User $user, PdoDatabase $database = null) |
|
| 96 | + { |
|
| 97 | + if (self::$tokenCountStatement === null && $database === null) { |
|
| 98 | + throw new ApplicationLogicException('Static link request without initialised statement'); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + return self::runTokenCount($user->getId(), $database, self::TOKEN_ACCESS); |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + public static function userIsPartiallyLinked(User $user, PdoDatabase $database = null) |
|
| 105 | + { |
|
| 106 | + if (self::$tokenCountStatement === null && $database === null) { |
|
| 107 | + throw new ApplicationLogicException('Static link request without initialised statement'); |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + if (self::userIsFullyLinked($user, $database)) { |
|
| 111 | + return false; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + return self::runTokenCount($user->getId(), $database, self::TOKEN_REQUEST) |
|
| 115 | + || $user->getOnWikiName() == null; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * @param PdoDatabase $database |
|
| 120 | + */ |
|
| 121 | + public static function prepareTokenCountStatement(PdoDatabase $database) |
|
| 122 | + { |
|
| 123 | + if (self::$tokenCountStatement === null) { |
|
| 124 | + self::$tokenCountStatement = $database->prepare('SELECT COUNT(*) FROM oauthtoken WHERE user = :user AND type = :type'); |
|
| 125 | + } |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + private static function runTokenCount($userId, $database, $tokenType) |
|
| 129 | + { |
|
| 130 | + if (self::$tokenCountStatement === null) { |
|
| 131 | + self::prepareTokenCountStatement($database); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + self::$tokenCountStatement->execute(array( |
|
| 135 | + ':user' => $userId, |
|
| 136 | + ':type' => $tokenType, |
|
| 137 | + )); |
|
| 138 | + |
|
| 139 | + $tokenCount = self::$tokenCountStatement->fetchColumn(); |
|
| 140 | + $linked = $tokenCount > 0; |
|
| 141 | + self::$tokenCountStatement->closeCursor(); |
|
| 142 | + |
|
| 143 | + return $linked; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + #endregion Static methods |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * OAuthUserHelper constructor. |
|
| 150 | + * |
|
| 151 | + * @param User $user |
|
| 152 | + * @param PdoDatabase $database |
|
| 153 | + * @param IOAuthProtocolHelper $oauthProtocolHelper |
|
| 154 | + * @param SiteConfiguration $siteConfiguration |
|
| 155 | + */ |
|
| 156 | + public function __construct( |
|
| 157 | + User $user, |
|
| 158 | + PdoDatabase $database, |
|
| 159 | + IOAuthProtocolHelper $oauthProtocolHelper, |
|
| 160 | + SiteConfiguration $siteConfiguration |
|
| 161 | + ) { |
|
| 162 | + $this->user = $user; |
|
| 163 | + $this->database = $database; |
|
| 164 | + $this->oauthProtocolHelper = $oauthProtocolHelper; |
|
| 165 | + |
|
| 166 | + $this->linked = null; |
|
| 167 | + $this->partiallyLinked = null; |
|
| 168 | + $this->siteConfiguration = $siteConfiguration; |
|
| 169 | + |
|
| 170 | + self::prepareTokenCountStatement($database); |
|
| 171 | + $this->getTokenStatement = $this->database->prepare('SELECT * FROM oauthtoken WHERE user = :user AND type = :type'); |
|
| 172 | + |
|
| 173 | + $this->legacyTokens = $this->siteConfiguration->getOauthLegacyConsumerTokens(); |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * Determines if the user is fully connected to OAuth. |
|
| 178 | + * |
|
| 179 | + * @return bool |
|
| 180 | + */ |
|
| 181 | + public function isFullyLinked() |
|
| 182 | + { |
|
| 183 | + if ($this->linked === null) { |
|
| 184 | + $this->linked = self::userIsFullyLinked($this->user, $this->database); |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + return $this->linked; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * Attempts to figure out if a user is partially linked to OAuth, and therefore needs to complete the OAuth |
|
| 192 | + * procedure before configuring. |
|
| 193 | + * @return bool |
|
| 194 | + */ |
|
| 195 | + public function isPartiallyLinked() |
|
| 196 | + { |
|
| 197 | + if ($this->partiallyLinked === null) { |
|
| 198 | + $this->partiallyLinked = self::userIsPartiallyLinked($this->user, $this->database); |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + return $this->partiallyLinked; |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + public function canCreateAccount() |
|
| 205 | + { |
|
| 206 | + return $this->isFullyLinked() |
|
| 207 | + && $this->getIdentity(true)->getGrantBasic() |
|
| 208 | + && $this->getIdentity(true)->getGrantHighVolume() |
|
| 209 | + && $this->getIdentity(true)->getGrantCreateAccount(); |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + public function canWelcome() |
|
| 213 | + { |
|
| 214 | + return $this->isFullyLinked() |
|
| 215 | + && $this->getIdentity(true)->getGrantBasic() |
|
| 216 | + && $this->getIdentity(true)->getGrantHighVolume() |
|
| 217 | + && $this->getIdentity(true)->getGrantCreateEditMovePage(); |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + /** |
|
| 221 | + * @throws OAuthException |
|
| 222 | + * @throws CurlException |
|
| 223 | + * @throws OptimisticLockFailedException |
|
| 224 | + * @throws Exception |
|
| 225 | + */ |
|
| 226 | + public function refreshIdentity() |
|
| 227 | + { |
|
| 228 | + $this->loadIdentity(); |
|
| 229 | + |
|
| 230 | + if ($this->identity === null) { |
|
| 231 | + $this->identity = new OAuthIdentity(); |
|
| 232 | + $this->identity->setUserId($this->user->getId()); |
|
| 233 | + $this->identity->setDatabase($this->database); |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + $token = $this->loadAccessToken(); |
|
| 237 | + |
|
| 238 | + try { |
|
| 239 | + $rawTicket = $this->oauthProtocolHelper->getIdentityTicket($token->getToken(), $token->getSecret()); |
|
| 240 | + } |
|
| 241 | + catch (Exception $ex) { |
|
| 242 | + if (strpos($ex->getMessage(), "mwoauthdatastore-access-token-not-found") !== false) { |
|
| 243 | + throw new OAuthException('No approved grants for this access token.', -1, $ex); |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + throw $ex; |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + $this->identity->populate($rawTicket); |
|
| 250 | + |
|
| 251 | + if (!$this->identityIsValid()) { |
|
| 252 | + throw new OAuthException('Identity ticket is not valid!'); |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + $this->identity->save(); |
|
| 256 | + |
|
| 257 | + $this->user->setOnWikiName($this->identity->getUsername()); |
|
| 258 | + $this->user->save(); |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + /** |
|
| 262 | + * @return string |
|
| 263 | + * @throws CurlException |
|
| 264 | + */ |
|
| 265 | + public function getRequestToken() |
|
| 266 | + { |
|
| 267 | + $token = $this->oauthProtocolHelper->getRequestToken(); |
|
| 268 | + |
|
| 269 | + $this->partiallyLinked = true; |
|
| 270 | + $this->linked = false; |
|
| 271 | + |
|
| 272 | + $this->database |
|
| 273 | + ->prepare('DELETE FROM oauthtoken WHERE user = :user AND type = :type') |
|
| 274 | + ->execute(array(':user' => $this->user->getId(), ':type' => self::TOKEN_REQUEST)); |
|
| 275 | + |
|
| 276 | + $this->database |
|
| 277 | + ->prepare('INSERT INTO oauthtoken (user, type, token, secret, expiry) VALUES (:user, :type, :token, :secret, DATE_ADD(NOW(), INTERVAL 1 DAY))') |
|
| 278 | + ->execute(array( |
|
| 279 | + ':user' => $this->user->getId(), |
|
| 280 | + ':type' => self::TOKEN_REQUEST, |
|
| 281 | + ':token' => $token->key, |
|
| 282 | + ':secret' => $token->secret, |
|
| 283 | + )); |
|
| 284 | + |
|
| 285 | + return $this->oauthProtocolHelper->getAuthoriseUrl($token->key); |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + /** |
|
| 289 | + * @param $verificationToken |
|
| 290 | + * |
|
| 291 | + * @throws ApplicationLogicException |
|
| 292 | + * @throws CurlException |
|
| 293 | + * @throws OAuthException |
|
| 294 | + * @throws OptimisticLockFailedException |
|
| 295 | + */ |
|
| 296 | + public function completeHandshake($verificationToken) |
|
| 297 | + { |
|
| 298 | + $this->getTokenStatement->execute(array(':user' => $this->user->getId(), ':type' => self::TOKEN_REQUEST)); |
|
| 299 | + |
|
| 300 | + /** @var OAuthToken $token */ |
|
| 301 | + $token = $this->getTokenStatement->fetchObject(OAuthToken::class); |
|
| 302 | + $this->getTokenStatement->closeCursor(); |
|
| 303 | + |
|
| 304 | + if ($token === false) { |
|
| 305 | + throw new ApplicationLogicException('Cannot find request token'); |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + $token->setDatabase($this->database); |
|
| 309 | + |
|
| 310 | + $accessToken = $this->oauthProtocolHelper->callbackCompleted($token->getToken(), $token->getSecret(), |
|
| 311 | + $verificationToken); |
|
| 312 | + |
|
| 313 | + $clearStatement = $this->database->prepare('DELETE FROM oauthtoken WHERE user = :u AND type = :t'); |
|
| 314 | + $clearStatement->execute(array(':u' => $this->user->getId(), ':t' => self::TOKEN_ACCESS)); |
|
| 315 | + |
|
| 316 | + $token->setToken($accessToken->key); |
|
| 317 | + $token->setSecret($accessToken->secret); |
|
| 318 | + $token->setType(self::TOKEN_ACCESS); |
|
| 319 | + $token->setExpiry(null); |
|
| 320 | + $token->save(); |
|
| 321 | + |
|
| 322 | + $this->partiallyLinked = false; |
|
| 323 | + $this->linked = true; |
|
| 324 | + |
|
| 325 | + $this->refreshIdentity(); |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + public function detach() |
|
| 329 | + { |
|
| 330 | + $this->loadIdentity(); |
|
| 331 | + |
|
| 332 | + $this->identity->delete(); |
|
| 333 | + $statement = $this->database->prepare('DELETE FROM oauthtoken WHERE user = :user'); |
|
| 334 | + $statement->execute(array(':user' => $this->user->getId())); |
|
| 335 | + |
|
| 336 | + $this->identity = null; |
|
| 337 | + $this->linked = false; |
|
| 338 | + $this->partiallyLinked = false; |
|
| 339 | + } |
|
| 340 | + |
|
| 341 | + /** |
|
| 342 | + * @param bool $expiredOk |
|
| 343 | + * |
|
| 344 | + * @return OAuthIdentity |
|
| 345 | + * @throws OAuthException |
|
| 346 | + */ |
|
| 347 | + public function getIdentity($expiredOk = false) |
|
| 348 | + { |
|
| 349 | + $this->loadIdentity(); |
|
| 350 | + |
|
| 351 | + if (!$this->identityIsValid($expiredOk)) { |
|
| 352 | + throw new OAuthException('Stored identity is not valid.'); |
|
| 353 | + } |
|
| 354 | + |
|
| 355 | + return $this->identity; |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + public function doApiCall($params, $method) |
|
| 359 | + { |
|
| 360 | + // Ensure we're logged in |
|
| 361 | + $params['assert'] = 'user'; |
|
| 362 | + |
|
| 363 | + $token = $this->loadAccessToken(); |
|
| 364 | + return $this->oauthProtocolHelper->apiCall($params, $token->getToken(), $token->getSecret(), $method); |
|
| 365 | + } |
|
| 366 | + |
|
| 367 | + /** |
|
| 368 | + * @param bool $expiredOk |
|
| 369 | + * |
|
| 370 | + * @return bool |
|
| 371 | + */ |
|
| 372 | + private function identityIsValid($expiredOk = false) |
|
| 373 | + { |
|
| 374 | + $this->loadIdentity(); |
|
| 375 | + |
|
| 376 | + if ($this->identity === null) { |
|
| 377 | + return false; |
|
| 378 | + } |
|
| 379 | + |
|
| 380 | + if ($this->identity->getIssuedAtTime() === false |
|
| 381 | + || $this->identity->getExpirationTime() === false |
|
| 382 | + || $this->identity->getAudience() === false |
|
| 383 | + || $this->identity->getIssuer() === false |
|
| 384 | + ) { |
|
| 385 | + // this isn't populated properly. |
|
| 386 | + return false; |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + $issue = DateTimeImmutable::createFromFormat("U", $this->identity->getIssuedAtTime()); |
|
| 390 | + $now = new DateTimeImmutable(); |
|
| 391 | + |
|
| 392 | + if ($issue > $now) { |
|
| 393 | + // wat. |
|
| 394 | + return false; |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + if ($this->identityExpired() && !$expiredOk) { |
|
| 398 | + // soz. |
|
| 399 | + return false; |
|
| 400 | + } |
|
| 401 | + |
|
| 402 | + if ($this->identity->getAudience() !== $this->siteConfiguration->getOAuthConsumerToken()) { |
|
| 403 | + // token not issued for us |
|
| 404 | + |
|
| 405 | + // we allow cases where the cache is expired and the cache is for a legacy token |
|
| 406 | + if (!($expiredOk && in_array($this->identity->getAudience(), $this->legacyTokens))) { |
|
| 407 | + return false; |
|
| 408 | + } |
|
| 409 | + } |
|
| 410 | + |
|
| 411 | + if ($this->identity->getIssuer() !== $this->siteConfiguration->getOauthMediaWikiCanonicalServer()) { |
|
| 412 | + // token not issued by the right person |
|
| 413 | + return false; |
|
| 414 | + } |
|
| 415 | + |
|
| 416 | + // can't find a reason to not trust it |
|
| 417 | + return true; |
|
| 418 | + } |
|
| 419 | + |
|
| 420 | + /** |
|
| 421 | + * @return bool |
|
| 422 | + */ |
|
| 423 | + public function identityExpired() |
|
| 424 | + { |
|
| 425 | + // allowed max age |
|
| 426 | + $gracePeriod = $this->siteConfiguration->getOauthIdentityGraceTime(); |
|
| 427 | + |
|
| 428 | + $expiry = DateTimeImmutable::createFromFormat("U", $this->identity->getExpirationTime()); |
|
| 429 | + $graceExpiry = $expiry->modify($gracePeriod); |
|
| 430 | + $now = new DateTimeImmutable(); |
|
| 431 | + |
|
| 432 | + return $graceExpiry < $now; |
|
| 433 | + } |
|
| 434 | + |
|
| 435 | + /** |
|
| 436 | + * Loads the OAuth identity from the database for the current user. |
|
| 437 | + */ |
|
| 438 | + private function loadIdentity() |
|
| 439 | + { |
|
| 440 | + if ($this->identityLoaded) { |
|
| 441 | + return; |
|
| 442 | + } |
|
| 443 | + |
|
| 444 | + $statement = $this->database->prepare('SELECT * FROM oauthidentity WHERE user = :user'); |
|
| 445 | + $statement->execute(array(':user' => $this->user->getId())); |
|
| 446 | + /** @var OAuthIdentity $obj */ |
|
| 447 | + $obj = $statement->fetchObject(OAuthIdentity::class); |
|
| 448 | + |
|
| 449 | + if ($obj === false) { |
|
| 450 | + // failed to load identity. |
|
| 451 | + $this->identityLoaded = true; |
|
| 452 | + $this->identity = null; |
|
| 453 | + |
|
| 454 | + return; |
|
| 455 | + } |
|
| 456 | + |
|
| 457 | + $obj->setDatabase($this->database); |
|
| 458 | + $this->identityLoaded = true; |
|
| 459 | + $this->identity = $obj; |
|
| 460 | + } |
|
| 461 | + |
|
| 462 | + /** |
|
| 463 | + * @return OAuthToken |
|
| 464 | + * @throws OAuthException |
|
| 465 | + */ |
|
| 466 | + private function loadAccessToken() |
|
| 467 | + { |
|
| 468 | + if (!$this->accessTokenLoaded) { |
|
| 469 | + $this->getTokenStatement->execute(array(':user' => $this->user->getId(), ':type' => self::TOKEN_ACCESS)); |
|
| 470 | + /** @var OAuthToken $token */ |
|
| 471 | + $token = $this->getTokenStatement->fetchObject(OAuthToken::class); |
|
| 472 | + $this->getTokenStatement->closeCursor(); |
|
| 473 | + |
|
| 474 | + if ($token === false) { |
|
| 475 | + throw new OAuthException('Access token not found!'); |
|
| 476 | + } |
|
| 477 | + |
|
| 478 | + $this->accessToken = $token; |
|
| 479 | + $this->accessTokenLoaded = true; |
|
| 480 | + } |
|
| 481 | + |
|
| 482 | + return $this->accessToken; |
|
| 483 | + } |
|
| 484 | 484 | } |
@@ -17,136 +17,136 @@ |
||
| 17 | 17 | |
| 18 | 18 | class BotMediaWikiClient implements IMediaWikiClient |
| 19 | 19 | { |
| 20 | - /** |
|
| 21 | - * @var HttpHelper |
|
| 22 | - */ |
|
| 23 | - private $httpHelper; |
|
| 24 | - /** @var string */ |
|
| 25 | - private $mediawikiWebServiceEndpoint; |
|
| 26 | - /** @var string */ |
|
| 27 | - private $creationBotUsername; |
|
| 28 | - /** @var string */ |
|
| 29 | - private $creationBotPassword; |
|
| 30 | - /** @var bool */ |
|
| 31 | - private $knownLoggedIn = false; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * BotMediaWikiClient constructor. |
|
| 35 | - * |
|
| 36 | - * @param SiteConfiguration $siteConfiguration |
|
| 37 | - * @param Domain $domain |
|
| 38 | - */ |
|
| 39 | - public function __construct(SiteConfiguration $siteConfiguration, Domain $domain) |
|
| 40 | - { |
|
| 41 | - $this->mediawikiWebServiceEndpoint = $domain->getWikiApiPath(); |
|
| 42 | - |
|
| 43 | - $this->creationBotUsername = $siteConfiguration->getCreationBotUsername(); |
|
| 44 | - $this->creationBotPassword = $siteConfiguration->getCreationBotPassword(); |
|
| 45 | - |
|
| 46 | - $this->httpHelper = new HttpHelper( |
|
| 47 | - $siteConfiguration, |
|
| 48 | - $siteConfiguration->getCurlCookieJar() |
|
| 49 | - ); |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - public function doApiCall($apiParams, $method = 'GET') |
|
| 53 | - { |
|
| 54 | - $this->ensureLoggedIn(); |
|
| 55 | - $apiParams['assert'] = 'user'; |
|
| 56 | - |
|
| 57 | - return $this->callApi($apiParams, $method); |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - private function ensureLoggedIn() |
|
| 61 | - { |
|
| 62 | - if ($this->knownLoggedIn) { |
|
| 63 | - return; |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - $userinfoResult = $this->callApi(array('action' => 'query', 'meta' => 'userinfo'), 'GET'); |
|
| 67 | - if (isset($userinfoResult->query->userinfo->anon)) { |
|
| 68 | - // not logged in. |
|
| 69 | - $this->logIn(); |
|
| 70 | - |
|
| 71 | - // retest |
|
| 72 | - $userinfoResult = $this->callApi(array('action' => 'query', 'meta' => 'userinfo'), 'GET'); |
|
| 73 | - if (isset($userinfoResult->query->userinfo->anon)) { |
|
| 74 | - throw new MediaWikiApiException('Unable to log in.'); |
|
| 75 | - } |
|
| 76 | - else { |
|
| 77 | - $this->knownLoggedIn = true; |
|
| 78 | - } |
|
| 79 | - } |
|
| 80 | - else { |
|
| 81 | - $this->knownLoggedIn = true; |
|
| 82 | - } |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * @param $apiParams |
|
| 87 | - * @param $method |
|
| 88 | - * |
|
| 89 | - * @return mixed |
|
| 90 | - * @throws ApplicationLogicException |
|
| 91 | - * @throws CurlException |
|
| 92 | - */ |
|
| 93 | - private function callApi($apiParams, $method) |
|
| 94 | - { |
|
| 95 | - $apiParams['format'] = 'json'; |
|
| 96 | - |
|
| 97 | - if ($method == 'GET') { |
|
| 98 | - $data = $this->httpHelper->get($this->mediawikiWebServiceEndpoint, $apiParams); |
|
| 99 | - } |
|
| 100 | - elseif ($method == 'POST') { |
|
| 101 | - $data = $this->httpHelper->post($this->mediawikiWebServiceEndpoint, $apiParams); |
|
| 102 | - } |
|
| 103 | - else { |
|
| 104 | - throw new ApplicationLogicException('Unsupported HTTP Method'); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - if ($data === false) { |
|
| 108 | - throw new CurlException('Curl error: ' . $this->httpHelper->getError()); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - $result = json_decode($data); |
|
| 112 | - |
|
| 113 | - return $result; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - private function logIn() |
|
| 117 | - { |
|
| 118 | - // get token |
|
| 119 | - $tokenParams = array( |
|
| 120 | - 'action' => 'query', |
|
| 121 | - 'meta' => 'tokens', |
|
| 122 | - 'type' => 'login', |
|
| 123 | - ); |
|
| 124 | - |
|
| 125 | - $response = $this->callApi($tokenParams, 'POST'); |
|
| 126 | - |
|
| 127 | - if (isset($response->error)) { |
|
| 128 | - throw new MediaWikiApiException($response->error->code . ': ' . $response->error->info); |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - $token = $response->query->tokens->logintoken; |
|
| 132 | - |
|
| 133 | - if ($token === null) { |
|
| 134 | - throw new MediaWikiApiException('Edit token could not be acquired'); |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - $params = array( |
|
| 138 | - 'action' => 'login', |
|
| 139 | - 'lgname' => $this->creationBotUsername, |
|
| 140 | - 'lgpassword' => $this->creationBotPassword, |
|
| 141 | - 'lgtoken' => $token, |
|
| 142 | - ); |
|
| 143 | - |
|
| 144 | - $loginResponse = $this->callApi($params, 'POST'); |
|
| 145 | - |
|
| 146 | - if ($loginResponse->login->result == 'Success') { |
|
| 147 | - return; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - throw new ApplicationLogicException(json_encode($loginResponse)); |
|
| 151 | - } |
|
| 20 | + /** |
|
| 21 | + * @var HttpHelper |
|
| 22 | + */ |
|
| 23 | + private $httpHelper; |
|
| 24 | + /** @var string */ |
|
| 25 | + private $mediawikiWebServiceEndpoint; |
|
| 26 | + /** @var string */ |
|
| 27 | + private $creationBotUsername; |
|
| 28 | + /** @var string */ |
|
| 29 | + private $creationBotPassword; |
|
| 30 | + /** @var bool */ |
|
| 31 | + private $knownLoggedIn = false; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * BotMediaWikiClient constructor. |
|
| 35 | + * |
|
| 36 | + * @param SiteConfiguration $siteConfiguration |
|
| 37 | + * @param Domain $domain |
|
| 38 | + */ |
|
| 39 | + public function __construct(SiteConfiguration $siteConfiguration, Domain $domain) |
|
| 40 | + { |
|
| 41 | + $this->mediawikiWebServiceEndpoint = $domain->getWikiApiPath(); |
|
| 42 | + |
|
| 43 | + $this->creationBotUsername = $siteConfiguration->getCreationBotUsername(); |
|
| 44 | + $this->creationBotPassword = $siteConfiguration->getCreationBotPassword(); |
|
| 45 | + |
|
| 46 | + $this->httpHelper = new HttpHelper( |
|
| 47 | + $siteConfiguration, |
|
| 48 | + $siteConfiguration->getCurlCookieJar() |
|
| 49 | + ); |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + public function doApiCall($apiParams, $method = 'GET') |
|
| 53 | + { |
|
| 54 | + $this->ensureLoggedIn(); |
|
| 55 | + $apiParams['assert'] = 'user'; |
|
| 56 | + |
|
| 57 | + return $this->callApi($apiParams, $method); |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + private function ensureLoggedIn() |
|
| 61 | + { |
|
| 62 | + if ($this->knownLoggedIn) { |
|
| 63 | + return; |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + $userinfoResult = $this->callApi(array('action' => 'query', 'meta' => 'userinfo'), 'GET'); |
|
| 67 | + if (isset($userinfoResult->query->userinfo->anon)) { |
|
| 68 | + // not logged in. |
|
| 69 | + $this->logIn(); |
|
| 70 | + |
|
| 71 | + // retest |
|
| 72 | + $userinfoResult = $this->callApi(array('action' => 'query', 'meta' => 'userinfo'), 'GET'); |
|
| 73 | + if (isset($userinfoResult->query->userinfo->anon)) { |
|
| 74 | + throw new MediaWikiApiException('Unable to log in.'); |
|
| 75 | + } |
|
| 76 | + else { |
|
| 77 | + $this->knownLoggedIn = true; |
|
| 78 | + } |
|
| 79 | + } |
|
| 80 | + else { |
|
| 81 | + $this->knownLoggedIn = true; |
|
| 82 | + } |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * @param $apiParams |
|
| 87 | + * @param $method |
|
| 88 | + * |
|
| 89 | + * @return mixed |
|
| 90 | + * @throws ApplicationLogicException |
|
| 91 | + * @throws CurlException |
|
| 92 | + */ |
|
| 93 | + private function callApi($apiParams, $method) |
|
| 94 | + { |
|
| 95 | + $apiParams['format'] = 'json'; |
|
| 96 | + |
|
| 97 | + if ($method == 'GET') { |
|
| 98 | + $data = $this->httpHelper->get($this->mediawikiWebServiceEndpoint, $apiParams); |
|
| 99 | + } |
|
| 100 | + elseif ($method == 'POST') { |
|
| 101 | + $data = $this->httpHelper->post($this->mediawikiWebServiceEndpoint, $apiParams); |
|
| 102 | + } |
|
| 103 | + else { |
|
| 104 | + throw new ApplicationLogicException('Unsupported HTTP Method'); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + if ($data === false) { |
|
| 108 | + throw new CurlException('Curl error: ' . $this->httpHelper->getError()); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + $result = json_decode($data); |
|
| 112 | + |
|
| 113 | + return $result; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + private function logIn() |
|
| 117 | + { |
|
| 118 | + // get token |
|
| 119 | + $tokenParams = array( |
|
| 120 | + 'action' => 'query', |
|
| 121 | + 'meta' => 'tokens', |
|
| 122 | + 'type' => 'login', |
|
| 123 | + ); |
|
| 124 | + |
|
| 125 | + $response = $this->callApi($tokenParams, 'POST'); |
|
| 126 | + |
|
| 127 | + if (isset($response->error)) { |
|
| 128 | + throw new MediaWikiApiException($response->error->code . ': ' . $response->error->info); |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + $token = $response->query->tokens->logintoken; |
|
| 132 | + |
|
| 133 | + if ($token === null) { |
|
| 134 | + throw new MediaWikiApiException('Edit token could not be acquired'); |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + $params = array( |
|
| 138 | + 'action' => 'login', |
|
| 139 | + 'lgname' => $this->creationBotUsername, |
|
| 140 | + 'lgpassword' => $this->creationBotPassword, |
|
| 141 | + 'lgtoken' => $token, |
|
| 142 | + ); |
|
| 143 | + |
|
| 144 | + $loginResponse = $this->callApi($params, 'POST'); |
|
| 145 | + |
|
| 146 | + if ($loginResponse->login->result == 'Success') { |
|
| 147 | + return; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + throw new ApplicationLogicException(json_encode($loginResponse)); |
|
| 151 | + } |
|
| 152 | 152 | } |
@@ -72,12 +72,10 @@ discard block |
||
| 72 | 72 | $userinfoResult = $this->callApi(array('action' => 'query', 'meta' => 'userinfo'), 'GET'); |
| 73 | 73 | if (isset($userinfoResult->query->userinfo->anon)) { |
| 74 | 74 | throw new MediaWikiApiException('Unable to log in.'); |
| 75 | - } |
|
| 76 | - else { |
|
| 75 | + } else { |
|
| 77 | 76 | $this->knownLoggedIn = true; |
| 78 | 77 | } |
| 79 | - } |
|
| 80 | - else { |
|
| 78 | + } else { |
|
| 81 | 79 | $this->knownLoggedIn = true; |
| 82 | 80 | } |
| 83 | 81 | } |
@@ -96,11 +94,9 @@ discard block |
||
| 96 | 94 | |
| 97 | 95 | if ($method == 'GET') { |
| 98 | 96 | $data = $this->httpHelper->get($this->mediawikiWebServiceEndpoint, $apiParams); |
| 99 | - } |
|
| 100 | - elseif ($method == 'POST') { |
|
| 97 | + } elseif ($method == 'POST') { |
|
| 101 | 98 | $data = $this->httpHelper->post($this->mediawikiWebServiceEndpoint, $apiParams); |
| 102 | - } |
|
| 103 | - else { |
|
| 99 | + } else { |
|
| 104 | 100 | throw new ApplicationLogicException('Unsupported HTTP Method'); |
| 105 | 101 | } |
| 106 | 102 | |
@@ -74,8 +74,7 @@ |
||
| 74 | 74 | |
| 75 | 75 | if ($domain !== null) { |
| 76 | 76 | $log->setDomain($domain); |
| 77 | - } |
|
| 78 | - elseif (method_exists($object, 'getDomain')) { |
|
| 77 | + } elseif (method_exists($object, 'getDomain')) { |
|
| 79 | 78 | $log->setDomain($object->getDomain()); |
| 80 | 79 | } |
| 81 | 80 | |
@@ -34,427 +34,427 @@ |
||
| 34 | 34 | */ |
| 35 | 35 | class Logger |
| 36 | 36 | { |
| 37 | - /** |
|
| 38 | - * @param PdoDatabase $database |
|
| 39 | - * @param Request $object |
|
| 40 | - */ |
|
| 41 | - public static function emailConfirmed(PdoDatabase $database, Request $object) |
|
| 42 | - { |
|
| 43 | - self::createLogEntry($database, $object, "Email Confirmed", null, User::getCommunity()); |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * @param PdoDatabase $database |
|
| 48 | - * @param DataObject $object |
|
| 49 | - * @param string $logAction |
|
| 50 | - * @param null|string $comment |
|
| 51 | - * @param User|null $user |
|
| 52 | - * @param int|null $domain |
|
| 53 | - * |
|
| 54 | - * @throws Exception |
|
| 55 | - */ |
|
| 56 | - private static function createLogEntry( |
|
| 57 | - PdoDatabase $database, |
|
| 58 | - DataObject $object, |
|
| 59 | - $logAction, |
|
| 60 | - $comment = null, |
|
| 61 | - $user = null, |
|
| 62 | - ?int $domain = null |
|
| 63 | - ) { |
|
| 64 | - if ($user == null) { |
|
| 65 | - $user = User::getCurrent($database); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - $objectType = get_class($object); |
|
| 69 | - if (strpos($objectType, 'Waca\\DataObjects\\') !== false) { |
|
| 70 | - $objectType = str_replace('Waca\\DataObjects\\', '', $objectType); |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - $log = new Log(); |
|
| 74 | - |
|
| 75 | - if ($domain !== null) { |
|
| 76 | - $log->setDomain($domain); |
|
| 77 | - } |
|
| 78 | - elseif (method_exists($object, 'getDomain')) { |
|
| 79 | - $log->setDomain($object->getDomain()); |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - $log->setDatabase($database); |
|
| 83 | - $log->setAction($logAction); |
|
| 84 | - $log->setObjectId($object->getId()); |
|
| 85 | - $log->setObjectType($objectType); |
|
| 86 | - $log->setUser($user); |
|
| 87 | - $log->setComment($comment); |
|
| 88 | - $log->save(); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - #region Users |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * @throws Exception |
|
| 95 | - */ |
|
| 96 | - public static function newUser(PdoDatabase $database, User $user) |
|
| 97 | - { |
|
| 98 | - self::createLogEntry($database, $user, 'Registered', null, User::getCommunity()); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * @throws Exception |
|
| 103 | - */ |
|
| 104 | - public static function approvedUser(PdoDatabase $database, User $object) |
|
| 105 | - { |
|
| 106 | - self::createLogEntry($database, $object, "Approved"); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * @throws Exception |
|
| 111 | - */ |
|
| 112 | - public static function declinedUser(PdoDatabase $database, User $object, string $comment) |
|
| 113 | - { |
|
| 114 | - self::createLogEntry($database, $object, "Declined", $comment); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * @throws Exception |
|
| 119 | - */ |
|
| 120 | - public static function suspendedUser(PdoDatabase $database, User $object, string $comment) |
|
| 121 | - { |
|
| 122 | - self::createLogEntry($database, $object, "Suspended", $comment); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * @throws Exception |
|
| 127 | - */ |
|
| 128 | - public static function renamedUser(PdoDatabase $database, User $object, string $comment) |
|
| 129 | - { |
|
| 130 | - self::createLogEntry($database, $object, "Renamed", $comment); |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * @throws Exception |
|
| 135 | - */ |
|
| 136 | - public static function userPreferencesChange(PdoDatabase $database, User $object) |
|
| 137 | - { |
|
| 138 | - self::createLogEntry($database, $object, "Prefchange"); |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * @throws Exception |
|
| 143 | - */ |
|
| 144 | - public static function userRolesEdited( |
|
| 145 | - PdoDatabase $database, |
|
| 146 | - User $object, |
|
| 147 | - string $reason, |
|
| 148 | - array $added, |
|
| 149 | - array $removed, |
|
| 150 | - int $domain |
|
| 151 | - ) { |
|
| 152 | - $logData = serialize(array( |
|
| 153 | - 'added' => $added, |
|
| 154 | - 'removed' => $removed, |
|
| 155 | - 'reason' => $reason, |
|
| 156 | - )); |
|
| 157 | - |
|
| 158 | - self::createLogEntry($database, $object, "RoleChange", $logData, null, $domain); |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * @throws Exception |
|
| 163 | - */ |
|
| 164 | - public static function userGlobalRolesEdited( |
|
| 165 | - PdoDatabase $database, |
|
| 166 | - User $object, |
|
| 167 | - string $reason, |
|
| 168 | - array $added, |
|
| 169 | - array $removed |
|
| 170 | - ): void { |
|
| 171 | - $logData = serialize(array( |
|
| 172 | - 'added' => $added, |
|
| 173 | - 'removed' => $removed, |
|
| 174 | - 'reason' => $reason, |
|
| 175 | - )); |
|
| 176 | - |
|
| 177 | - self::createLogEntry($database, $object, "GlobalRoleChange", $logData); |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - #endregion |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * @param PdoDatabase $database |
|
| 184 | - * @param SiteNotice $object |
|
| 185 | - */ |
|
| 186 | - public static function siteNoticeEdited(PdoDatabase $database, SiteNotice $object) |
|
| 187 | - { |
|
| 188 | - self::createLogEntry($database, $object, "Edited"); |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - #region Welcome Templates |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * @param PdoDatabase $database |
|
| 195 | - * @param WelcomeTemplate $object |
|
| 196 | - */ |
|
| 197 | - public static function welcomeTemplateCreated(PdoDatabase $database, WelcomeTemplate $object) |
|
| 198 | - { |
|
| 199 | - self::createLogEntry($database, $object, "CreatedTemplate"); |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - /** |
|
| 203 | - * @param PdoDatabase $database |
|
| 204 | - * @param WelcomeTemplate $object |
|
| 205 | - */ |
|
| 206 | - public static function welcomeTemplateEdited(PdoDatabase $database, WelcomeTemplate $object) |
|
| 207 | - { |
|
| 208 | - self::createLogEntry($database, $object, "EditedTemplate"); |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - /** |
|
| 212 | - * @param PdoDatabase $database |
|
| 213 | - * @param WelcomeTemplate $object |
|
| 214 | - */ |
|
| 215 | - public static function welcomeTemplateDeleted(PdoDatabase $database, WelcomeTemplate $object) |
|
| 216 | - { |
|
| 217 | - self::createLogEntry($database, $object, "DeletedTemplate"); |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - #endregion |
|
| 221 | - |
|
| 222 | - #region Bans |
|
| 223 | - |
|
| 224 | - /** |
|
| 225 | - * @param PdoDatabase $database |
|
| 226 | - * @param Ban $object |
|
| 227 | - * @param string $reason |
|
| 228 | - */ |
|
| 229 | - public static function banned(PdoDatabase $database, Ban $object, $reason) |
|
| 230 | - { |
|
| 231 | - self::createLogEntry($database, $object, "Banned", $reason); |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - /** |
|
| 235 | - * @param PdoDatabase $database |
|
| 236 | - * @param Ban $object |
|
| 237 | - * @param string $reason |
|
| 238 | - */ |
|
| 239 | - public static function unbanned(PdoDatabase $database, Ban $object, $reason) |
|
| 240 | - { |
|
| 241 | - self::createLogEntry($database, $object, "Unbanned", $reason); |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - public static function banReplaced(PdoDatabase $database, Ban $object) |
|
| 245 | - { |
|
| 246 | - self::createLogEntry($database, $object, "BanReplaced"); |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - #endregion |
|
| 250 | - |
|
| 251 | - #region Requests |
|
| 252 | - |
|
| 253 | - /** |
|
| 254 | - * @param PdoDatabase $database |
|
| 255 | - * @param Request $object |
|
| 256 | - * @param string $target |
|
| 257 | - */ |
|
| 258 | - public static function deferRequest(PdoDatabase $database, Request $object, $target) |
|
| 259 | - { |
|
| 260 | - self::createLogEntry($database, $object, "Deferred to $target"); |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - /** |
|
| 264 | - * @param PdoDatabase $database |
|
| 265 | - * @param Request $object |
|
| 266 | - * @param integer $target |
|
| 267 | - * @param string $comment |
|
| 268 | - * @param User|null $logUser |
|
| 269 | - */ |
|
| 270 | - public static function closeRequest(PdoDatabase $database, Request $object, $target, $comment, User $logUser = null) |
|
| 271 | - { |
|
| 272 | - self::createLogEntry($database, $object, "Closed $target", $comment, $logUser); |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - public static function manuallyConfirmRequest(PdoDatabase $database, Request $object) |
|
| 276 | - { |
|
| 277 | - self::createLogEntry($database, $object, "Manually Confirmed"); |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - /** |
|
| 281 | - * @param PdoDatabase $database |
|
| 282 | - * @param Request $object |
|
| 283 | - */ |
|
| 284 | - public static function reserve(PdoDatabase $database, Request $object) |
|
| 285 | - { |
|
| 286 | - self::createLogEntry($database, $object, "Reserved"); |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - /** |
|
| 290 | - * @param PdoDatabase $database |
|
| 291 | - * @param Request $object |
|
| 292 | - */ |
|
| 293 | - public static function breakReserve(PdoDatabase $database, Request $object) |
|
| 294 | - { |
|
| 295 | - self::createLogEntry($database, $object, "BreakReserve"); |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - /** |
|
| 299 | - * @param PdoDatabase $database |
|
| 300 | - * @param Request $object |
|
| 301 | - */ |
|
| 302 | - public static function unreserve(PdoDatabase $database, Request $object) |
|
| 303 | - { |
|
| 304 | - self::createLogEntry($database, $object, "Unreserved"); |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - /** |
|
| 308 | - * @param PdoDatabase $database |
|
| 309 | - * @param Comment $object |
|
| 310 | - * @param Request $request |
|
| 311 | - */ |
|
| 312 | - public static function editComment(PdoDatabase $database, Comment $object, Request $request) |
|
| 313 | - { |
|
| 314 | - self::createLogEntry($database, $request, "EditComment-r"); |
|
| 315 | - self::createLogEntry($database, $object, "EditComment-c", null, null, $request->getDomain()); |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - /** |
|
| 319 | - * @param PdoDatabase $database |
|
| 320 | - * @param Comment $object |
|
| 321 | - */ |
|
| 322 | - public static function flaggedComment(PdoDatabase $database, Comment $object, int $domain) |
|
| 323 | - { |
|
| 324 | - self::createLogEntry($database, $object, "FlaggedComment", null, null, $domain); |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - /** |
|
| 328 | - * @param PdoDatabase $database |
|
| 329 | - * @param Comment $object |
|
| 330 | - */ |
|
| 331 | - public static function unflaggedComment(PdoDatabase $database, Comment $object, int $domain) |
|
| 332 | - { |
|
| 333 | - self::createLogEntry($database, $object, "UnflaggedComment", null, null, $domain); |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - /** |
|
| 337 | - * @param PdoDatabase $database |
|
| 338 | - * @param Request $object |
|
| 339 | - * @param User $target |
|
| 340 | - */ |
|
| 341 | - public static function sendReservation(PdoDatabase $database, Request $object, User $target) |
|
| 342 | - { |
|
| 343 | - self::createLogEntry($database, $object, "SendReserved"); |
|
| 344 | - self::createLogEntry($database, $object, "ReceiveReserved", null, $target); |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - /** |
|
| 348 | - * @param PdoDatabase $database |
|
| 349 | - * @param Request $object |
|
| 350 | - * @param string $comment |
|
| 351 | - */ |
|
| 352 | - public static function sentMail(PdoDatabase $database, Request $object, $comment) |
|
| 353 | - { |
|
| 354 | - self::createLogEntry($database, $object, "SentMail", $comment); |
|
| 355 | - } |
|
| 356 | - |
|
| 357 | - /** |
|
| 358 | - * @param PdoDatabase $database |
|
| 359 | - * @param Request $object |
|
| 360 | - */ |
|
| 361 | - public static function enqueuedJobQueue(PdoDatabase $database, Request $object) |
|
| 362 | - { |
|
| 363 | - self::createLogEntry($database, $object, 'EnqueuedJobQueue'); |
|
| 364 | - } |
|
| 365 | - |
|
| 366 | - public static function hospitalised(PdoDatabase $database, Request $object) |
|
| 367 | - { |
|
| 368 | - self::createLogEntry($database, $object, 'Hospitalised'); |
|
| 369 | - } |
|
| 370 | - #endregion |
|
| 371 | - |
|
| 372 | - #region Email templates |
|
| 373 | - |
|
| 374 | - /** |
|
| 375 | - * @param PdoDatabase $database |
|
| 376 | - * @param EmailTemplate $object |
|
| 377 | - */ |
|
| 378 | - public static function createEmail(PdoDatabase $database, EmailTemplate $object) |
|
| 379 | - { |
|
| 380 | - self::createLogEntry($database, $object, "CreatedEmail"); |
|
| 381 | - } |
|
| 382 | - |
|
| 383 | - /** |
|
| 384 | - * @param PdoDatabase $database |
|
| 385 | - * @param EmailTemplate $object |
|
| 386 | - */ |
|
| 387 | - public static function editedEmail(PdoDatabase $database, EmailTemplate $object) |
|
| 388 | - { |
|
| 389 | - self::createLogEntry($database, $object, "EditedEmail"); |
|
| 390 | - } |
|
| 391 | - |
|
| 392 | - #endregion |
|
| 393 | - |
|
| 394 | - #region Display |
|
| 395 | - |
|
| 396 | - #endregion |
|
| 397 | - |
|
| 398 | - #region Automation |
|
| 399 | - |
|
| 400 | - public static function backgroundJobComplete(PdoDatabase $database, JobQueue $job) |
|
| 401 | - { |
|
| 402 | - self::createLogEntry($database, $job, 'JobCompleted', null, User::getCommunity()); |
|
| 403 | - } |
|
| 404 | - |
|
| 405 | - public static function backgroundJobIssue(PdoDatabase $database, JobQueue $job) |
|
| 406 | - { |
|
| 407 | - $data = array('status' => $job->getStatus(), 'error' => $job->getError()); |
|
| 408 | - self::createLogEntry($database, $job, 'JobIssue', serialize($data), User::getCommunity()); |
|
| 409 | - } |
|
| 410 | - |
|
| 411 | - public static function backgroundJobCancelled(PdoDatabase $database, JobQueue $job) |
|
| 412 | - { |
|
| 413 | - self::createLogEntry($database, $job, 'JobCancelled', $job->getError()); |
|
| 414 | - } |
|
| 415 | - |
|
| 416 | - public static function backgroundJobRequeued(PdoDatabase $database, JobQueue $job) |
|
| 417 | - { |
|
| 418 | - self::createLogEntry($database, $job, 'JobRequeued'); |
|
| 419 | - } |
|
| 420 | - |
|
| 421 | - public static function backgroundJobAcknowledged(PdoDatabase $database, JobQueue $job, $comment = null) |
|
| 422 | - { |
|
| 423 | - self::createLogEntry($database, $job, 'JobAcknowledged', $comment); |
|
| 424 | - } |
|
| 425 | - #endregion |
|
| 426 | - |
|
| 427 | - #region Request Queues |
|
| 428 | - public static function requestQueueCreated(PdoDatabase $database, RequestQueue $queue) |
|
| 429 | - { |
|
| 430 | - self::createLogEntry($database, $queue, 'QueueCreated'); |
|
| 431 | - } |
|
| 432 | - |
|
| 433 | - public static function requestQueueEdited(PdoDatabase $database, RequestQueue $queue) |
|
| 434 | - { |
|
| 435 | - self::createLogEntry($database, $queue, 'QueueEdited'); |
|
| 436 | - } |
|
| 437 | - #endregion |
|
| 438 | - #region Domains |
|
| 439 | - public static function domainCreated(PdoDatabase $database, Domain $domain) |
|
| 440 | - { |
|
| 441 | - self::createLogEntry($database, $domain, 'DomainCreated'); |
|
| 442 | - } |
|
| 443 | - |
|
| 444 | - public static function domainEdited(PdoDatabase $database, Domain $domain) |
|
| 445 | - { |
|
| 446 | - self::createLogEntry($database, $domain, 'DomainEdited'); |
|
| 447 | - } |
|
| 448 | - #endregion |
|
| 449 | - #region Request Forms |
|
| 450 | - public static function requestFormCreated(PdoDatabase $database, RequestForm $requestForm) |
|
| 451 | - { |
|
| 452 | - self::createLogEntry($database, $requestForm, 'RequestFormCreated'); |
|
| 453 | - } |
|
| 454 | - |
|
| 455 | - public static function requestFormEdited(PdoDatabase $database, RequestForm $requestForm) |
|
| 456 | - { |
|
| 457 | - self::createLogEntry($database, $requestForm, 'RequestFormEdited'); |
|
| 458 | - } |
|
| 459 | - #endregion |
|
| 37 | + /** |
|
| 38 | + * @param PdoDatabase $database |
|
| 39 | + * @param Request $object |
|
| 40 | + */ |
|
| 41 | + public static function emailConfirmed(PdoDatabase $database, Request $object) |
|
| 42 | + { |
|
| 43 | + self::createLogEntry($database, $object, "Email Confirmed", null, User::getCommunity()); |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * @param PdoDatabase $database |
|
| 48 | + * @param DataObject $object |
|
| 49 | + * @param string $logAction |
|
| 50 | + * @param null|string $comment |
|
| 51 | + * @param User|null $user |
|
| 52 | + * @param int|null $domain |
|
| 53 | + * |
|
| 54 | + * @throws Exception |
|
| 55 | + */ |
|
| 56 | + private static function createLogEntry( |
|
| 57 | + PdoDatabase $database, |
|
| 58 | + DataObject $object, |
|
| 59 | + $logAction, |
|
| 60 | + $comment = null, |
|
| 61 | + $user = null, |
|
| 62 | + ?int $domain = null |
|
| 63 | + ) { |
|
| 64 | + if ($user == null) { |
|
| 65 | + $user = User::getCurrent($database); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + $objectType = get_class($object); |
|
| 69 | + if (strpos($objectType, 'Waca\\DataObjects\\') !== false) { |
|
| 70 | + $objectType = str_replace('Waca\\DataObjects\\', '', $objectType); |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + $log = new Log(); |
|
| 74 | + |
|
| 75 | + if ($domain !== null) { |
|
| 76 | + $log->setDomain($domain); |
|
| 77 | + } |
|
| 78 | + elseif (method_exists($object, 'getDomain')) { |
|
| 79 | + $log->setDomain($object->getDomain()); |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + $log->setDatabase($database); |
|
| 83 | + $log->setAction($logAction); |
|
| 84 | + $log->setObjectId($object->getId()); |
|
| 85 | + $log->setObjectType($objectType); |
|
| 86 | + $log->setUser($user); |
|
| 87 | + $log->setComment($comment); |
|
| 88 | + $log->save(); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + #region Users |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * @throws Exception |
|
| 95 | + */ |
|
| 96 | + public static function newUser(PdoDatabase $database, User $user) |
|
| 97 | + { |
|
| 98 | + self::createLogEntry($database, $user, 'Registered', null, User::getCommunity()); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * @throws Exception |
|
| 103 | + */ |
|
| 104 | + public static function approvedUser(PdoDatabase $database, User $object) |
|
| 105 | + { |
|
| 106 | + self::createLogEntry($database, $object, "Approved"); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * @throws Exception |
|
| 111 | + */ |
|
| 112 | + public static function declinedUser(PdoDatabase $database, User $object, string $comment) |
|
| 113 | + { |
|
| 114 | + self::createLogEntry($database, $object, "Declined", $comment); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * @throws Exception |
|
| 119 | + */ |
|
| 120 | + public static function suspendedUser(PdoDatabase $database, User $object, string $comment) |
|
| 121 | + { |
|
| 122 | + self::createLogEntry($database, $object, "Suspended", $comment); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * @throws Exception |
|
| 127 | + */ |
|
| 128 | + public static function renamedUser(PdoDatabase $database, User $object, string $comment) |
|
| 129 | + { |
|
| 130 | + self::createLogEntry($database, $object, "Renamed", $comment); |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * @throws Exception |
|
| 135 | + */ |
|
| 136 | + public static function userPreferencesChange(PdoDatabase $database, User $object) |
|
| 137 | + { |
|
| 138 | + self::createLogEntry($database, $object, "Prefchange"); |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * @throws Exception |
|
| 143 | + */ |
|
| 144 | + public static function userRolesEdited( |
|
| 145 | + PdoDatabase $database, |
|
| 146 | + User $object, |
|
| 147 | + string $reason, |
|
| 148 | + array $added, |
|
| 149 | + array $removed, |
|
| 150 | + int $domain |
|
| 151 | + ) { |
|
| 152 | + $logData = serialize(array( |
|
| 153 | + 'added' => $added, |
|
| 154 | + 'removed' => $removed, |
|
| 155 | + 'reason' => $reason, |
|
| 156 | + )); |
|
| 157 | + |
|
| 158 | + self::createLogEntry($database, $object, "RoleChange", $logData, null, $domain); |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * @throws Exception |
|
| 163 | + */ |
|
| 164 | + public static function userGlobalRolesEdited( |
|
| 165 | + PdoDatabase $database, |
|
| 166 | + User $object, |
|
| 167 | + string $reason, |
|
| 168 | + array $added, |
|
| 169 | + array $removed |
|
| 170 | + ): void { |
|
| 171 | + $logData = serialize(array( |
|
| 172 | + 'added' => $added, |
|
| 173 | + 'removed' => $removed, |
|
| 174 | + 'reason' => $reason, |
|
| 175 | + )); |
|
| 176 | + |
|
| 177 | + self::createLogEntry($database, $object, "GlobalRoleChange", $logData); |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + #endregion |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * @param PdoDatabase $database |
|
| 184 | + * @param SiteNotice $object |
|
| 185 | + */ |
|
| 186 | + public static function siteNoticeEdited(PdoDatabase $database, SiteNotice $object) |
|
| 187 | + { |
|
| 188 | + self::createLogEntry($database, $object, "Edited"); |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + #region Welcome Templates |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * @param PdoDatabase $database |
|
| 195 | + * @param WelcomeTemplate $object |
|
| 196 | + */ |
|
| 197 | + public static function welcomeTemplateCreated(PdoDatabase $database, WelcomeTemplate $object) |
|
| 198 | + { |
|
| 199 | + self::createLogEntry($database, $object, "CreatedTemplate"); |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + /** |
|
| 203 | + * @param PdoDatabase $database |
|
| 204 | + * @param WelcomeTemplate $object |
|
| 205 | + */ |
|
| 206 | + public static function welcomeTemplateEdited(PdoDatabase $database, WelcomeTemplate $object) |
|
| 207 | + { |
|
| 208 | + self::createLogEntry($database, $object, "EditedTemplate"); |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + /** |
|
| 212 | + * @param PdoDatabase $database |
|
| 213 | + * @param WelcomeTemplate $object |
|
| 214 | + */ |
|
| 215 | + public static function welcomeTemplateDeleted(PdoDatabase $database, WelcomeTemplate $object) |
|
| 216 | + { |
|
| 217 | + self::createLogEntry($database, $object, "DeletedTemplate"); |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + #endregion |
|
| 221 | + |
|
| 222 | + #region Bans |
|
| 223 | + |
|
| 224 | + /** |
|
| 225 | + * @param PdoDatabase $database |
|
| 226 | + * @param Ban $object |
|
| 227 | + * @param string $reason |
|
| 228 | + */ |
|
| 229 | + public static function banned(PdoDatabase $database, Ban $object, $reason) |
|
| 230 | + { |
|
| 231 | + self::createLogEntry($database, $object, "Banned", $reason); |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + /** |
|
| 235 | + * @param PdoDatabase $database |
|
| 236 | + * @param Ban $object |
|
| 237 | + * @param string $reason |
|
| 238 | + */ |
|
| 239 | + public static function unbanned(PdoDatabase $database, Ban $object, $reason) |
|
| 240 | + { |
|
| 241 | + self::createLogEntry($database, $object, "Unbanned", $reason); |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + public static function banReplaced(PdoDatabase $database, Ban $object) |
|
| 245 | + { |
|
| 246 | + self::createLogEntry($database, $object, "BanReplaced"); |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + #endregion |
|
| 250 | + |
|
| 251 | + #region Requests |
|
| 252 | + |
|
| 253 | + /** |
|
| 254 | + * @param PdoDatabase $database |
|
| 255 | + * @param Request $object |
|
| 256 | + * @param string $target |
|
| 257 | + */ |
|
| 258 | + public static function deferRequest(PdoDatabase $database, Request $object, $target) |
|
| 259 | + { |
|
| 260 | + self::createLogEntry($database, $object, "Deferred to $target"); |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + /** |
|
| 264 | + * @param PdoDatabase $database |
|
| 265 | + * @param Request $object |
|
| 266 | + * @param integer $target |
|
| 267 | + * @param string $comment |
|
| 268 | + * @param User|null $logUser |
|
| 269 | + */ |
|
| 270 | + public static function closeRequest(PdoDatabase $database, Request $object, $target, $comment, User $logUser = null) |
|
| 271 | + { |
|
| 272 | + self::createLogEntry($database, $object, "Closed $target", $comment, $logUser); |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + public static function manuallyConfirmRequest(PdoDatabase $database, Request $object) |
|
| 276 | + { |
|
| 277 | + self::createLogEntry($database, $object, "Manually Confirmed"); |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + /** |
|
| 281 | + * @param PdoDatabase $database |
|
| 282 | + * @param Request $object |
|
| 283 | + */ |
|
| 284 | + public static function reserve(PdoDatabase $database, Request $object) |
|
| 285 | + { |
|
| 286 | + self::createLogEntry($database, $object, "Reserved"); |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + /** |
|
| 290 | + * @param PdoDatabase $database |
|
| 291 | + * @param Request $object |
|
| 292 | + */ |
|
| 293 | + public static function breakReserve(PdoDatabase $database, Request $object) |
|
| 294 | + { |
|
| 295 | + self::createLogEntry($database, $object, "BreakReserve"); |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + /** |
|
| 299 | + * @param PdoDatabase $database |
|
| 300 | + * @param Request $object |
|
| 301 | + */ |
|
| 302 | + public static function unreserve(PdoDatabase $database, Request $object) |
|
| 303 | + { |
|
| 304 | + self::createLogEntry($database, $object, "Unreserved"); |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + /** |
|
| 308 | + * @param PdoDatabase $database |
|
| 309 | + * @param Comment $object |
|
| 310 | + * @param Request $request |
|
| 311 | + */ |
|
| 312 | + public static function editComment(PdoDatabase $database, Comment $object, Request $request) |
|
| 313 | + { |
|
| 314 | + self::createLogEntry($database, $request, "EditComment-r"); |
|
| 315 | + self::createLogEntry($database, $object, "EditComment-c", null, null, $request->getDomain()); |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + /** |
|
| 319 | + * @param PdoDatabase $database |
|
| 320 | + * @param Comment $object |
|
| 321 | + */ |
|
| 322 | + public static function flaggedComment(PdoDatabase $database, Comment $object, int $domain) |
|
| 323 | + { |
|
| 324 | + self::createLogEntry($database, $object, "FlaggedComment", null, null, $domain); |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + /** |
|
| 328 | + * @param PdoDatabase $database |
|
| 329 | + * @param Comment $object |
|
| 330 | + */ |
|
| 331 | + public static function unflaggedComment(PdoDatabase $database, Comment $object, int $domain) |
|
| 332 | + { |
|
| 333 | + self::createLogEntry($database, $object, "UnflaggedComment", null, null, $domain); |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + /** |
|
| 337 | + * @param PdoDatabase $database |
|
| 338 | + * @param Request $object |
|
| 339 | + * @param User $target |
|
| 340 | + */ |
|
| 341 | + public static function sendReservation(PdoDatabase $database, Request $object, User $target) |
|
| 342 | + { |
|
| 343 | + self::createLogEntry($database, $object, "SendReserved"); |
|
| 344 | + self::createLogEntry($database, $object, "ReceiveReserved", null, $target); |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + /** |
|
| 348 | + * @param PdoDatabase $database |
|
| 349 | + * @param Request $object |
|
| 350 | + * @param string $comment |
|
| 351 | + */ |
|
| 352 | + public static function sentMail(PdoDatabase $database, Request $object, $comment) |
|
| 353 | + { |
|
| 354 | + self::createLogEntry($database, $object, "SentMail", $comment); |
|
| 355 | + } |
|
| 356 | + |
|
| 357 | + /** |
|
| 358 | + * @param PdoDatabase $database |
|
| 359 | + * @param Request $object |
|
| 360 | + */ |
|
| 361 | + public static function enqueuedJobQueue(PdoDatabase $database, Request $object) |
|
| 362 | + { |
|
| 363 | + self::createLogEntry($database, $object, 'EnqueuedJobQueue'); |
|
| 364 | + } |
|
| 365 | + |
|
| 366 | + public static function hospitalised(PdoDatabase $database, Request $object) |
|
| 367 | + { |
|
| 368 | + self::createLogEntry($database, $object, 'Hospitalised'); |
|
| 369 | + } |
|
| 370 | + #endregion |
|
| 371 | + |
|
| 372 | + #region Email templates |
|
| 373 | + |
|
| 374 | + /** |
|
| 375 | + * @param PdoDatabase $database |
|
| 376 | + * @param EmailTemplate $object |
|
| 377 | + */ |
|
| 378 | + public static function createEmail(PdoDatabase $database, EmailTemplate $object) |
|
| 379 | + { |
|
| 380 | + self::createLogEntry($database, $object, "CreatedEmail"); |
|
| 381 | + } |
|
| 382 | + |
|
| 383 | + /** |
|
| 384 | + * @param PdoDatabase $database |
|
| 385 | + * @param EmailTemplate $object |
|
| 386 | + */ |
|
| 387 | + public static function editedEmail(PdoDatabase $database, EmailTemplate $object) |
|
| 388 | + { |
|
| 389 | + self::createLogEntry($database, $object, "EditedEmail"); |
|
| 390 | + } |
|
| 391 | + |
|
| 392 | + #endregion |
|
| 393 | + |
|
| 394 | + #region Display |
|
| 395 | + |
|
| 396 | + #endregion |
|
| 397 | + |
|
| 398 | + #region Automation |
|
| 399 | + |
|
| 400 | + public static function backgroundJobComplete(PdoDatabase $database, JobQueue $job) |
|
| 401 | + { |
|
| 402 | + self::createLogEntry($database, $job, 'JobCompleted', null, User::getCommunity()); |
|
| 403 | + } |
|
| 404 | + |
|
| 405 | + public static function backgroundJobIssue(PdoDatabase $database, JobQueue $job) |
|
| 406 | + { |
|
| 407 | + $data = array('status' => $job->getStatus(), 'error' => $job->getError()); |
|
| 408 | + self::createLogEntry($database, $job, 'JobIssue', serialize($data), User::getCommunity()); |
|
| 409 | + } |
|
| 410 | + |
|
| 411 | + public static function backgroundJobCancelled(PdoDatabase $database, JobQueue $job) |
|
| 412 | + { |
|
| 413 | + self::createLogEntry($database, $job, 'JobCancelled', $job->getError()); |
|
| 414 | + } |
|
| 415 | + |
|
| 416 | + public static function backgroundJobRequeued(PdoDatabase $database, JobQueue $job) |
|
| 417 | + { |
|
| 418 | + self::createLogEntry($database, $job, 'JobRequeued'); |
|
| 419 | + } |
|
| 420 | + |
|
| 421 | + public static function backgroundJobAcknowledged(PdoDatabase $database, JobQueue $job, $comment = null) |
|
| 422 | + { |
|
| 423 | + self::createLogEntry($database, $job, 'JobAcknowledged', $comment); |
|
| 424 | + } |
|
| 425 | + #endregion |
|
| 426 | + |
|
| 427 | + #region Request Queues |
|
| 428 | + public static function requestQueueCreated(PdoDatabase $database, RequestQueue $queue) |
|
| 429 | + { |
|
| 430 | + self::createLogEntry($database, $queue, 'QueueCreated'); |
|
| 431 | + } |
|
| 432 | + |
|
| 433 | + public static function requestQueueEdited(PdoDatabase $database, RequestQueue $queue) |
|
| 434 | + { |
|
| 435 | + self::createLogEntry($database, $queue, 'QueueEdited'); |
|
| 436 | + } |
|
| 437 | + #endregion |
|
| 438 | + #region Domains |
|
| 439 | + public static function domainCreated(PdoDatabase $database, Domain $domain) |
|
| 440 | + { |
|
| 441 | + self::createLogEntry($database, $domain, 'DomainCreated'); |
|
| 442 | + } |
|
| 443 | + |
|
| 444 | + public static function domainEdited(PdoDatabase $database, Domain $domain) |
|
| 445 | + { |
|
| 446 | + self::createLogEntry($database, $domain, 'DomainEdited'); |
|
| 447 | + } |
|
| 448 | + #endregion |
|
| 449 | + #region Request Forms |
|
| 450 | + public static function requestFormCreated(PdoDatabase $database, RequestForm $requestForm) |
|
| 451 | + { |
|
| 452 | + self::createLogEntry($database, $requestForm, 'RequestFormCreated'); |
|
| 453 | + } |
|
| 454 | + |
|
| 455 | + public static function requestFormEdited(PdoDatabase $database, RequestForm $requestForm) |
|
| 456 | + { |
|
| 457 | + self::createLogEntry($database, $requestForm, 'RequestFormEdited'); |
|
| 458 | + } |
|
| 459 | + #endregion |
|
| 460 | 460 | } |
@@ -18,135 +18,135 @@ |
||
| 18 | 18 | |
| 19 | 19 | class OAuthProtocolHelper implements Interfaces\IOAuthProtocolHelper |
| 20 | 20 | { |
| 21 | - private $authUrl; |
|
| 22 | - /** |
|
| 23 | - * @var PdoDatabase |
|
| 24 | - */ |
|
| 25 | - private $database; |
|
| 26 | - /** |
|
| 27 | - * @var string |
|
| 28 | - */ |
|
| 29 | - private $consumerKey; |
|
| 30 | - /** |
|
| 31 | - * @var string |
|
| 32 | - */ |
|
| 33 | - private $consumerSecret; |
|
| 34 | - /** |
|
| 35 | - * @var string |
|
| 36 | - */ |
|
| 37 | - private $userAgent; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * OAuthHelper constructor. |
|
| 41 | - * |
|
| 42 | - * @param string $consumerKey |
|
| 43 | - * @param string $consumerSecret |
|
| 44 | - * @param PdoDatabase $database |
|
| 45 | - * @param string $userAgent |
|
| 46 | - */ |
|
| 47 | - public function __construct( |
|
| 48 | - $consumerKey, |
|
| 49 | - $consumerSecret, |
|
| 50 | - PdoDatabase $database, |
|
| 51 | - $userAgent |
|
| 52 | - ) { |
|
| 53 | - $this->consumerKey = $consumerKey; |
|
| 54 | - $this->consumerSecret = $consumerSecret; |
|
| 55 | - $this->userAgent = $userAgent; |
|
| 56 | - $this->database = $database; |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * @inheritDoc |
|
| 61 | - */ |
|
| 62 | - public function getRequestToken() |
|
| 63 | - { |
|
| 64 | - /** @var Token $requestToken */ |
|
| 65 | - |
|
| 66 | - // FIXME: domains! |
|
| 67 | - /** @var Domain $domain */ |
|
| 68 | - $domain = Domain::getById(1, $this->database); |
|
| 69 | - |
|
| 70 | - list($authUrl, $requestToken) = $this->getClient($domain)->initiate(); |
|
| 71 | - $this->authUrl = $authUrl; |
|
| 72 | - return $requestToken; |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * @inheritDoc |
|
| 77 | - */ |
|
| 78 | - public function getAuthoriseUrl($requestToken) |
|
| 79 | - { |
|
| 80 | - return $this->authUrl; |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * @inheritDoc |
|
| 85 | - */ |
|
| 86 | - public function callbackCompleted($oauthRequestToken, $oauthRequestSecret, $oauthVerifier) |
|
| 87 | - { |
|
| 88 | - $requestToken = new Token($oauthRequestToken, $oauthRequestSecret); |
|
| 89 | - |
|
| 90 | - // FIXME: domains! |
|
| 91 | - /** @var Domain $domain */ |
|
| 92 | - $domain = Domain::getById(1, $this->database); |
|
| 93 | - |
|
| 94 | - return $this->getClient($domain)->complete($requestToken, $oauthVerifier); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * @inheritDoc |
|
| 99 | - */ |
|
| 100 | - public function getIdentityTicket($oauthAccessToken, $oauthAccessSecret) |
|
| 101 | - { |
|
| 102 | - // FIXME: domains! |
|
| 103 | - /** @var Domain $domain */ |
|
| 104 | - $domain = Domain::getById(1, $this->database); |
|
| 105 | - |
|
| 106 | - return $this->getClient($domain)->identify(new Token($oauthAccessToken, $oauthAccessSecret)); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * @inheritDoc |
|
| 111 | - */ |
|
| 112 | - public function apiCall($apiParams, $accessToken, $accessSecret, $method = 'GET') |
|
| 113 | - { |
|
| 114 | - $userToken = new Token($accessToken, $accessSecret); |
|
| 115 | - |
|
| 116 | - $apiParams['format'] = 'json'; |
|
| 117 | - |
|
| 118 | - if ($apiParams === null || !is_array($apiParams)) { |
|
| 119 | - throw new CurlException("Invalid API call"); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - // FIXME: domains! |
|
| 123 | - /** @var Domain $domain */ |
|
| 124 | - $domain = Domain::getById(1, $this->database); |
|
| 125 | - |
|
| 126 | - $url = $domain->getWikiApiPath(); |
|
| 127 | - $isPost = ($method === 'POST'); |
|
| 128 | - |
|
| 129 | - if ($method === 'GET') { |
|
| 130 | - $query = http_build_query($apiParams); |
|
| 131 | - $url .= '?' . $query; |
|
| 132 | - $apiParams = null; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - $data = $this->getClient($domain)->makeOAuthCall($userToken, $url, $isPost, $apiParams); |
|
| 136 | - |
|
| 137 | - return json_decode($data); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * @param string $oauthEndpoint |
|
| 142 | - * |
|
| 143 | - * @return Client |
|
| 144 | - */ |
|
| 145 | - protected function getClient(Domain $domain) : Client |
|
| 146 | - { |
|
| 147 | - $oauthClientConfig = new ClientConfig($domain->getWikiArticlePath() . "?title=Special:OAuth"); |
|
| 148 | - $oauthClientConfig->setConsumer(new Consumer($this->consumerKey, $this->consumerSecret)); |
|
| 149 | - $oauthClientConfig->setUserAgent($this->userAgent); |
|
| 150 | - return new Client($oauthClientConfig); |
|
| 151 | - } |
|
| 21 | + private $authUrl; |
|
| 22 | + /** |
|
| 23 | + * @var PdoDatabase |
|
| 24 | + */ |
|
| 25 | + private $database; |
|
| 26 | + /** |
|
| 27 | + * @var string |
|
| 28 | + */ |
|
| 29 | + private $consumerKey; |
|
| 30 | + /** |
|
| 31 | + * @var string |
|
| 32 | + */ |
|
| 33 | + private $consumerSecret; |
|
| 34 | + /** |
|
| 35 | + * @var string |
|
| 36 | + */ |
|
| 37 | + private $userAgent; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * OAuthHelper constructor. |
|
| 41 | + * |
|
| 42 | + * @param string $consumerKey |
|
| 43 | + * @param string $consumerSecret |
|
| 44 | + * @param PdoDatabase $database |
|
| 45 | + * @param string $userAgent |
|
| 46 | + */ |
|
| 47 | + public function __construct( |
|
| 48 | + $consumerKey, |
|
| 49 | + $consumerSecret, |
|
| 50 | + PdoDatabase $database, |
|
| 51 | + $userAgent |
|
| 52 | + ) { |
|
| 53 | + $this->consumerKey = $consumerKey; |
|
| 54 | + $this->consumerSecret = $consumerSecret; |
|
| 55 | + $this->userAgent = $userAgent; |
|
| 56 | + $this->database = $database; |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * @inheritDoc |
|
| 61 | + */ |
|
| 62 | + public function getRequestToken() |
|
| 63 | + { |
|
| 64 | + /** @var Token $requestToken */ |
|
| 65 | + |
|
| 66 | + // FIXME: domains! |
|
| 67 | + /** @var Domain $domain */ |
|
| 68 | + $domain = Domain::getById(1, $this->database); |
|
| 69 | + |
|
| 70 | + list($authUrl, $requestToken) = $this->getClient($domain)->initiate(); |
|
| 71 | + $this->authUrl = $authUrl; |
|
| 72 | + return $requestToken; |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * @inheritDoc |
|
| 77 | + */ |
|
| 78 | + public function getAuthoriseUrl($requestToken) |
|
| 79 | + { |
|
| 80 | + return $this->authUrl; |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * @inheritDoc |
|
| 85 | + */ |
|
| 86 | + public function callbackCompleted($oauthRequestToken, $oauthRequestSecret, $oauthVerifier) |
|
| 87 | + { |
|
| 88 | + $requestToken = new Token($oauthRequestToken, $oauthRequestSecret); |
|
| 89 | + |
|
| 90 | + // FIXME: domains! |
|
| 91 | + /** @var Domain $domain */ |
|
| 92 | + $domain = Domain::getById(1, $this->database); |
|
| 93 | + |
|
| 94 | + return $this->getClient($domain)->complete($requestToken, $oauthVerifier); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * @inheritDoc |
|
| 99 | + */ |
|
| 100 | + public function getIdentityTicket($oauthAccessToken, $oauthAccessSecret) |
|
| 101 | + { |
|
| 102 | + // FIXME: domains! |
|
| 103 | + /** @var Domain $domain */ |
|
| 104 | + $domain = Domain::getById(1, $this->database); |
|
| 105 | + |
|
| 106 | + return $this->getClient($domain)->identify(new Token($oauthAccessToken, $oauthAccessSecret)); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * @inheritDoc |
|
| 111 | + */ |
|
| 112 | + public function apiCall($apiParams, $accessToken, $accessSecret, $method = 'GET') |
|
| 113 | + { |
|
| 114 | + $userToken = new Token($accessToken, $accessSecret); |
|
| 115 | + |
|
| 116 | + $apiParams['format'] = 'json'; |
|
| 117 | + |
|
| 118 | + if ($apiParams === null || !is_array($apiParams)) { |
|
| 119 | + throw new CurlException("Invalid API call"); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + // FIXME: domains! |
|
| 123 | + /** @var Domain $domain */ |
|
| 124 | + $domain = Domain::getById(1, $this->database); |
|
| 125 | + |
|
| 126 | + $url = $domain->getWikiApiPath(); |
|
| 127 | + $isPost = ($method === 'POST'); |
|
| 128 | + |
|
| 129 | + if ($method === 'GET') { |
|
| 130 | + $query = http_build_query($apiParams); |
|
| 131 | + $url .= '?' . $query; |
|
| 132 | + $apiParams = null; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + $data = $this->getClient($domain)->makeOAuthCall($userToken, $url, $isPost, $apiParams); |
|
| 136 | + |
|
| 137 | + return json_decode($data); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * @param string $oauthEndpoint |
|
| 142 | + * |
|
| 143 | + * @return Client |
|
| 144 | + */ |
|
| 145 | + protected function getClient(Domain $domain) : Client |
|
| 146 | + { |
|
| 147 | + $oauthClientConfig = new ClientConfig($domain->getWikiArticlePath() . "?title=Special:OAuth"); |
|
| 148 | + $oauthClientConfig->setConsumer(new Consumer($this->consumerKey, $this->consumerSecret)); |
|
| 149 | + $oauthClientConfig->setUserAgent($this->userAgent); |
|
| 150 | + return new Client($oauthClientConfig); |
|
| 151 | + } |
|
| 152 | 152 | } |
@@ -15,73 +15,73 @@ |
||
| 15 | 15 | |
| 16 | 16 | class RequestQueueHelper |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * @param RequestQueue $queue |
|
| 20 | - * @param bool $enabled |
|
| 21 | - * @param bool $default |
|
| 22 | - * @param bool $antiSpoof |
|
| 23 | - * @param bool $titleBlacklist |
|
| 24 | - */ |
|
| 25 | - public function configureDefaults( |
|
| 26 | - RequestQueue $queue, |
|
| 27 | - bool $enabled, |
|
| 28 | - bool $default, |
|
| 29 | - bool $antiSpoof, |
|
| 30 | - bool $titleBlacklist, |
|
| 31 | - bool $isTarget |
|
| 32 | - ) { |
|
| 33 | - // always allow enabling a queue |
|
| 34 | - if ($enabled) { |
|
| 35 | - $queue->setEnabled($enabled); |
|
| 36 | - } |
|
| 18 | + /** |
|
| 19 | + * @param RequestQueue $queue |
|
| 20 | + * @param bool $enabled |
|
| 21 | + * @param bool $default |
|
| 22 | + * @param bool $antiSpoof |
|
| 23 | + * @param bool $titleBlacklist |
|
| 24 | + */ |
|
| 25 | + public function configureDefaults( |
|
| 26 | + RequestQueue $queue, |
|
| 27 | + bool $enabled, |
|
| 28 | + bool $default, |
|
| 29 | + bool $antiSpoof, |
|
| 30 | + bool $titleBlacklist, |
|
| 31 | + bool $isTarget |
|
| 32 | + ) { |
|
| 33 | + // always allow enabling a queue |
|
| 34 | + if ($enabled) { |
|
| 35 | + $queue->setEnabled($enabled); |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - // only allow other enable-flag changes if we're not a default |
|
| 39 | - if (!($queue->isDefault() || $queue->isDefaultAntispoof() || $queue->isDefaultTitleBlacklist() || $isTarget)) { |
|
| 40 | - $queue->setEnabled($enabled); |
|
| 41 | - } |
|
| 38 | + // only allow other enable-flag changes if we're not a default |
|
| 39 | + if (!($queue->isDefault() || $queue->isDefaultAntispoof() || $queue->isDefaultTitleBlacklist() || $isTarget)) { |
|
| 40 | + $queue->setEnabled($enabled); |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - // only allow enabling the default flags, and only when we're enabled. |
|
| 44 | - $queue->setDefault(($queue->isDefault() || $default) && $queue->isEnabled()); |
|
| 45 | - $queue->setDefaultAntispoof(($queue->isDefaultAntispoof() || $antiSpoof) && $queue->isEnabled()); |
|
| 46 | - $queue->setDefaultTitleBlacklist(($queue->isDefaultTitleBlacklist() || $titleBlacklist) && $queue->isEnabled()); |
|
| 47 | - } |
|
| 43 | + // only allow enabling the default flags, and only when we're enabled. |
|
| 44 | + $queue->setDefault(($queue->isDefault() || $default) && $queue->isEnabled()); |
|
| 45 | + $queue->setDefaultAntispoof(($queue->isDefaultAntispoof() || $antiSpoof) && $queue->isEnabled()); |
|
| 46 | + $queue->setDefaultTitleBlacklist(($queue->isDefaultTitleBlacklist() || $titleBlacklist) && $queue->isEnabled()); |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * Returns true if the specified queue is a target of an enabled email template with a defer action. |
|
| 51 | - * |
|
| 52 | - * @param RequestQueue $queue |
|
| 53 | - * @param PdoDatabase $database |
|
| 54 | - * |
|
| 55 | - * @return bool |
|
| 56 | - */ |
|
| 57 | - public function isEmailTemplateTarget(RequestQueue $queue, PdoDatabase $database): bool |
|
| 58 | - { |
|
| 59 | - $isTarget = false; |
|
| 60 | - /** @var EmailTemplate[] $deferralTemplates */ |
|
| 61 | - $deferralTemplates = EmailTemplate::getAllActiveTemplates(EmailTemplate::ACTION_DEFER, $database, $queue->getDomain()); |
|
| 62 | - foreach ($deferralTemplates as $t) { |
|
| 63 | - if ($t->getQueue() === $queue->getId()) { |
|
| 64 | - $isTarget = true; |
|
| 65 | - break; |
|
| 66 | - } |
|
| 67 | - } |
|
| 49 | + /** |
|
| 50 | + * Returns true if the specified queue is a target of an enabled email template with a defer action. |
|
| 51 | + * |
|
| 52 | + * @param RequestQueue $queue |
|
| 53 | + * @param PdoDatabase $database |
|
| 54 | + * |
|
| 55 | + * @return bool |
|
| 56 | + */ |
|
| 57 | + public function isEmailTemplateTarget(RequestQueue $queue, PdoDatabase $database): bool |
|
| 58 | + { |
|
| 59 | + $isTarget = false; |
|
| 60 | + /** @var EmailTemplate[] $deferralTemplates */ |
|
| 61 | + $deferralTemplates = EmailTemplate::getAllActiveTemplates(EmailTemplate::ACTION_DEFER, $database, $queue->getDomain()); |
|
| 62 | + foreach ($deferralTemplates as $t) { |
|
| 63 | + if ($t->getQueue() === $queue->getId()) { |
|
| 64 | + $isTarget = true; |
|
| 65 | + break; |
|
| 66 | + } |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - return $isTarget; |
|
| 70 | - } |
|
| 69 | + return $isTarget; |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - public function isRequestFormTarget(RequestQueue $queue, PdoDatabase $database): bool |
|
| 73 | - { |
|
| 74 | - $isTarget = false; |
|
| 75 | - $forms = RequestForm::getAllForms($database, 1); // FIXME: domains |
|
| 76 | - foreach ($forms as $t) { |
|
| 77 | - if ($t->isEnabled()) { |
|
| 78 | - if ($t->getOverrideQueue() === $queue->getId()) { |
|
| 79 | - $isTarget = true; |
|
| 80 | - break; |
|
| 81 | - } |
|
| 82 | - } |
|
| 83 | - } |
|
| 72 | + public function isRequestFormTarget(RequestQueue $queue, PdoDatabase $database): bool |
|
| 73 | + { |
|
| 74 | + $isTarget = false; |
|
| 75 | + $forms = RequestForm::getAllForms($database, 1); // FIXME: domains |
|
| 76 | + foreach ($forms as $t) { |
|
| 77 | + if ($t->isEnabled()) { |
|
| 78 | + if ($t->getOverrideQueue() === $queue->getId()) { |
|
| 79 | + $isTarget = true; |
|
| 80 | + break; |
|
| 81 | + } |
|
| 82 | + } |
|
| 83 | + } |
|
| 84 | 84 | |
| 85 | - return $isTarget; |
|
| 86 | - } |
|
| 85 | + return $isTarget; |
|
| 86 | + } |
|
| 87 | 87 | } |
| 88 | 88 | \ No newline at end of file |
@@ -12,37 +12,37 @@ |
||
| 12 | 12 | |
| 13 | 13 | class EmailHelper implements IEmailHelper |
| 14 | 14 | { |
| 15 | - /** @var string */ |
|
| 16 | - private $emailFrom; |
|
| 17 | - private $instance; |
|
| 18 | - |
|
| 19 | - public function __construct(string $emailFrom, $instance) |
|
| 20 | - { |
|
| 21 | - $this->emailFrom = $emailFrom; |
|
| 22 | - $this->instance = $instance; |
|
| 23 | - } |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * @param string|null $replyAddress |
|
| 27 | - * @param string $to |
|
| 28 | - * @param string $subject |
|
| 29 | - * @param string $content |
|
| 30 | - * @param array $headers Extra headers to include |
|
| 31 | - */ |
|
| 32 | - public function sendMail(?string $replyAddress, $to, $subject, $content, $headers = array()) |
|
| 33 | - { |
|
| 34 | - if ($replyAddress !== null) { |
|
| 35 | - $headers['Reply-To'] = $replyAddress; |
|
| 36 | - } |
|
| 37 | - |
|
| 38 | - $headers['From'] = $this->emailFrom; |
|
| 39 | - $headers['X-ACC-Instance'] = $this->instance; |
|
| 40 | - $headerString = ''; |
|
| 41 | - |
|
| 42 | - foreach ($headers as $header => $headerValue) { |
|
| 43 | - $headerString .= $header . ': ' . $headerValue . "\r\n"; |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - mail($to, $subject, $content, $headerString); |
|
| 47 | - } |
|
| 15 | + /** @var string */ |
|
| 16 | + private $emailFrom; |
|
| 17 | + private $instance; |
|
| 18 | + |
|
| 19 | + public function __construct(string $emailFrom, $instance) |
|
| 20 | + { |
|
| 21 | + $this->emailFrom = $emailFrom; |
|
| 22 | + $this->instance = $instance; |
|
| 23 | + } |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * @param string|null $replyAddress |
|
| 27 | + * @param string $to |
|
| 28 | + * @param string $subject |
|
| 29 | + * @param string $content |
|
| 30 | + * @param array $headers Extra headers to include |
|
| 31 | + */ |
|
| 32 | + public function sendMail(?string $replyAddress, $to, $subject, $content, $headers = array()) |
|
| 33 | + { |
|
| 34 | + if ($replyAddress !== null) { |
|
| 35 | + $headers['Reply-To'] = $replyAddress; |
|
| 36 | + } |
|
| 37 | + |
|
| 38 | + $headers['From'] = $this->emailFrom; |
|
| 39 | + $headers['X-ACC-Instance'] = $this->instance; |
|
| 40 | + $headerString = ''; |
|
| 41 | + |
|
| 42 | + foreach ($headers as $header => $headerValue) { |
|
| 43 | + $headerString .= $header . ': ' . $headerValue . "\r\n"; |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + mail($to, $subject, $content, $headerString); |
|
| 47 | + } |
|
| 48 | 48 | } |
| 49 | 49 | \ No newline at end of file |
@@ -17,16 +17,16 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | interface IEmailHelper |
| 19 | 19 | { |
| 20 | - /** |
|
| 21 | - * Sends an email to the specified email address. |
|
| 22 | - * |
|
| 23 | - * @param string $replyAddress |
|
| 24 | - * @param string $to |
|
| 25 | - * @param string $subject |
|
| 26 | - * @param string $content |
|
| 27 | - * @param array $headers Extra headers to include |
|
| 28 | - * |
|
| 29 | - * @return void |
|
| 30 | - */ |
|
| 31 | - public function sendMail(?string $replyAddress, $to, $subject, $content, $headers = array()); |
|
| 20 | + /** |
|
| 21 | + * Sends an email to the specified email address. |
|
| 22 | + * |
|
| 23 | + * @param string $replyAddress |
|
| 24 | + * @param string $to |
|
| 25 | + * @param string $subject |
|
| 26 | + * @param string $content |
|
| 27 | + * @param array $headers Extra headers to include |
|
| 28 | + * |
|
| 29 | + * @return void |
|
| 30 | + */ |
|
| 31 | + public function sendMail(?string $replyAddress, $to, $subject, $content, $headers = array()); |
|
| 32 | 32 | } |
| 33 | 33 | \ No newline at end of file |
@@ -13,21 +13,21 @@ |
||
| 13 | 13 | |
| 14 | 14 | interface IBanHelper |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * @param Request $request |
|
| 18 | - * |
|
| 19 | - * @return bool |
|
| 20 | - */ |
|
| 21 | - public function isBlockBanned(Request $request): bool; |
|
| 16 | + /** |
|
| 17 | + * @param Request $request |
|
| 18 | + * |
|
| 19 | + * @return bool |
|
| 20 | + */ |
|
| 21 | + public function isBlockBanned(Request $request): bool; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * @param Request $request |
|
| 25 | - * |
|
| 26 | - * @return Ban[] |
|
| 27 | - */ |
|
| 28 | - public function getBans(Request $request): array; |
|
| 23 | + /** |
|
| 24 | + * @param Request $request |
|
| 25 | + * |
|
| 26 | + * @return Ban[] |
|
| 27 | + */ |
|
| 28 | + public function getBans(Request $request): array; |
|
| 29 | 29 | |
| 30 | - public function canUnban(Ban $ban): bool; |
|
| 30 | + public function canUnban(Ban $ban): bool; |
|
| 31 | 31 | |
| 32 | - public function isActive(Ban $ban): bool; |
|
| 32 | + public function isActive(Ban $ban): bool; |
|
| 33 | 33 | } |