@@ -22,67 +22,67 @@ |
||
| 22 | 22 | |
| 23 | 23 | class AuthenticationManager |
| 24 | 24 | { |
| 25 | - const AUTH_OK = 1; |
|
| 26 | - const AUTH_FAIL = 2; |
|
| 27 | - const AUTH_REQUIRE_NEXT_STAGE = 3; |
|
| 28 | - private $typeMap = array(); |
|
| 29 | - /** |
|
| 30 | - * @var PdoDatabase |
|
| 31 | - */ |
|
| 32 | - private $database; |
|
| 25 | + const AUTH_OK = 1; |
|
| 26 | + const AUTH_FAIL = 2; |
|
| 27 | + const AUTH_REQUIRE_NEXT_STAGE = 3; |
|
| 28 | + private $typeMap = array(); |
|
| 29 | + /** |
|
| 30 | + * @var PdoDatabase |
|
| 31 | + */ |
|
| 32 | + private $database; |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * AuthenticationManager constructor. |
|
| 36 | - * |
|
| 37 | - * @param PdoDatabase $database |
|
| 38 | - * @param SiteConfiguration $siteConfiguration |
|
| 39 | - * @param HttpHelper $httpHelper |
|
| 40 | - */ |
|
| 41 | - public function __construct(PdoDatabase $database, SiteConfiguration $siteConfiguration, HttpHelper $httpHelper) |
|
| 42 | - { |
|
| 43 | - // setup providers |
|
| 44 | - // note on type map: this *must* be the value in the database, as this is what it maps. |
|
| 45 | - $this->typeMap['password'] = new PasswordCredentialProvider($database, $siteConfiguration); |
|
| 46 | - $this->typeMap['yubikeyotp'] = new YubikeyOtpCredentialProvider($database, $siteConfiguration, $httpHelper); |
|
| 47 | - $this->typeMap['totp'] = new TotpCredentialProvider($database, $siteConfiguration); |
|
| 48 | - $this->typeMap['scratch'] = new ScratchTokenCredentialProvider($database, $siteConfiguration); |
|
| 49 | - $this->typeMap['u2f'] = new U2FCredentialProvider($database, $siteConfiguration); |
|
| 50 | - $this->database = $database; |
|
| 51 | - } |
|
| 34 | + /** |
|
| 35 | + * AuthenticationManager constructor. |
|
| 36 | + * |
|
| 37 | + * @param PdoDatabase $database |
|
| 38 | + * @param SiteConfiguration $siteConfiguration |
|
| 39 | + * @param HttpHelper $httpHelper |
|
| 40 | + */ |
|
| 41 | + public function __construct(PdoDatabase $database, SiteConfiguration $siteConfiguration, HttpHelper $httpHelper) |
|
| 42 | + { |
|
| 43 | + // setup providers |
|
| 44 | + // note on type map: this *must* be the value in the database, as this is what it maps. |
|
| 45 | + $this->typeMap['password'] = new PasswordCredentialProvider($database, $siteConfiguration); |
|
| 46 | + $this->typeMap['yubikeyotp'] = new YubikeyOtpCredentialProvider($database, $siteConfiguration, $httpHelper); |
|
| 47 | + $this->typeMap['totp'] = new TotpCredentialProvider($database, $siteConfiguration); |
|
| 48 | + $this->typeMap['scratch'] = new ScratchTokenCredentialProvider($database, $siteConfiguration); |
|
| 49 | + $this->typeMap['u2f'] = new U2FCredentialProvider($database, $siteConfiguration); |
|
| 50 | + $this->database = $database; |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - public function authenticate(User $user, $data, $stage) |
|
| 54 | - { |
|
| 55 | - $sql = 'SELECT type FROM credential WHERE user = :user AND factor = :stage AND disabled = 0 ORDER BY priority ASC'; |
|
| 56 | - $statement = $this->database->prepare($sql); |
|
| 57 | - $statement->execute(array(':user' => $user->getId(), ':stage' => $stage)); |
|
| 58 | - $options = $statement->fetchAll(PDO::FETCH_COLUMN); |
|
| 53 | + public function authenticate(User $user, $data, $stage) |
|
| 54 | + { |
|
| 55 | + $sql = 'SELECT type FROM credential WHERE user = :user AND factor = :stage AND disabled = 0 ORDER BY priority ASC'; |
|
| 56 | + $statement = $this->database->prepare($sql); |
|
| 57 | + $statement->execute(array(':user' => $user->getId(), ':stage' => $stage)); |
|
| 58 | + $options = $statement->fetchAll(PDO::FETCH_COLUMN); |
|
| 59 | 59 | |
| 60 | - $sql = 'SELECT count(DISTINCT factor) FROM credential WHERE user = :user AND factor > :stage AND disabled = 0 AND type <> :scratch'; |
|
| 61 | - $statement = $this->database->prepare($sql); |
|
| 62 | - $statement->execute(array(':user' => $user->getId(), ':stage' => $stage, ':scratch' => 'scratch')); |
|
| 63 | - $requiredFactors = $statement->fetchColumn(); |
|
| 60 | + $sql = 'SELECT count(DISTINCT factor) FROM credential WHERE user = :user AND factor > :stage AND disabled = 0 AND type <> :scratch'; |
|
| 61 | + $statement = $this->database->prepare($sql); |
|
| 62 | + $statement->execute(array(':user' => $user->getId(), ':stage' => $stage, ':scratch' => 'scratch')); |
|
| 63 | + $requiredFactors = $statement->fetchColumn(); |
|
| 64 | 64 | |
| 65 | - // prep the correct OK response based on how many factors are ahead of this one |
|
| 66 | - $success = self::AUTH_OK; |
|
| 67 | - if ($requiredFactors > 0) { |
|
| 68 | - $success = self::AUTH_REQUIRE_NEXT_STAGE; |
|
| 69 | - } |
|
| 65 | + // prep the correct OK response based on how many factors are ahead of this one |
|
| 66 | + $success = self::AUTH_OK; |
|
| 67 | + if ($requiredFactors > 0) { |
|
| 68 | + $success = self::AUTH_REQUIRE_NEXT_STAGE; |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - foreach ($options as $type) { |
|
| 72 | - if (!isset($this->typeMap[$type])) { |
|
| 73 | - // does this type have a credentialProvider registered? |
|
| 74 | - continue; |
|
| 75 | - } |
|
| 71 | + foreach ($options as $type) { |
|
| 72 | + if (!isset($this->typeMap[$type])) { |
|
| 73 | + // does this type have a credentialProvider registered? |
|
| 74 | + continue; |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - /** @var ICredentialProvider $credentialProvider */ |
|
| 78 | - $credentialProvider = $this->typeMap[$type]; |
|
| 79 | - if ($credentialProvider->authenticate($user, $data)) { |
|
| 80 | - return $success; |
|
| 81 | - } |
|
| 82 | - } |
|
| 77 | + /** @var ICredentialProvider $credentialProvider */ |
|
| 78 | + $credentialProvider = $this->typeMap[$type]; |
|
| 79 | + if ($credentialProvider->authenticate($user, $data)) { |
|
| 80 | + return $success; |
|
| 81 | + } |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - // We've iterated over all the available providers for this stage. |
|
| 85 | - // They all hate you. |
|
| 86 | - return self::AUTH_FAIL; |
|
| 87 | - } |
|
| 84 | + // We've iterated over all the available providers for this stage. |
|
| 85 | + // They all hate you. |
|
| 86 | + return self::AUTH_FAIL; |
|
| 87 | + } |
|
| 88 | 88 | } |
| 89 | 89 | \ No newline at end of file |
@@ -15,74 +15,74 @@ |
||
| 15 | 15 | |
| 16 | 16 | class ConsoleStart extends ApplicationBase |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * @var ConsoleTaskBase |
|
| 20 | - */ |
|
| 21 | - private $consoleTask; |
|
| 18 | + /** |
|
| 19 | + * @var ConsoleTaskBase |
|
| 20 | + */ |
|
| 21 | + private $consoleTask; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * ConsoleStart constructor. |
|
| 25 | - * |
|
| 26 | - * @param SiteConfiguration $configuration |
|
| 27 | - * @param ConsoleTaskBase $consoleTask |
|
| 28 | - */ |
|
| 29 | - public function __construct(SiteConfiguration $configuration, ConsoleTaskBase $consoleTask) |
|
| 30 | - { |
|
| 31 | - parent::__construct($configuration); |
|
| 32 | - $this->consoleTask = $consoleTask; |
|
| 33 | - } |
|
| 23 | + /** |
|
| 24 | + * ConsoleStart constructor. |
|
| 25 | + * |
|
| 26 | + * @param SiteConfiguration $configuration |
|
| 27 | + * @param ConsoleTaskBase $consoleTask |
|
| 28 | + */ |
|
| 29 | + public function __construct(SiteConfiguration $configuration, ConsoleTaskBase $consoleTask) |
|
| 30 | + { |
|
| 31 | + parent::__construct($configuration); |
|
| 32 | + $this->consoleTask = $consoleTask; |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - protected function setupEnvironment() |
|
| 36 | - { |
|
| 37 | - // initialise super-global providers |
|
| 38 | - WebRequest::setGlobalStateProvider(new FakeGlobalStateProvider()); |
|
| 35 | + protected function setupEnvironment() |
|
| 36 | + { |
|
| 37 | + // initialise super-global providers |
|
| 38 | + WebRequest::setGlobalStateProvider(new FakeGlobalStateProvider()); |
|
| 39 | 39 | |
| 40 | - if (WebRequest::method() !== null) { |
|
| 41 | - throw new EnvironmentException('This is a console task, which cannot be executed via the web.'); |
|
| 42 | - } |
|
| 40 | + if (WebRequest::method() !== null) { |
|
| 41 | + throw new EnvironmentException('This is a console task, which cannot be executed via the web.'); |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - return parent::setupEnvironment(); |
|
| 45 | - } |
|
| 44 | + return parent::setupEnvironment(); |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - protected function cleanupEnvironment() |
|
| 48 | - { |
|
| 49 | - } |
|
| 47 | + protected function cleanupEnvironment() |
|
| 48 | + { |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * Main application logic |
|
| 53 | - */ |
|
| 54 | - protected function main() |
|
| 55 | - { |
|
| 56 | - $database = PdoDatabase::getDatabaseConnection('acc'); |
|
| 51 | + /** |
|
| 52 | + * Main application logic |
|
| 53 | + */ |
|
| 54 | + protected function main() |
|
| 55 | + { |
|
| 56 | + $database = PdoDatabase::getDatabaseConnection('acc'); |
|
| 57 | 57 | |
| 58 | - if ($this->getConfiguration()->getIrcNotificationsEnabled()) { |
|
| 59 | - $notificationsDatabase = PdoDatabase::getDatabaseConnection('notifications'); |
|
| 60 | - } |
|
| 61 | - else { |
|
| 62 | - // pass through null |
|
| 63 | - $notificationsDatabase = null; |
|
| 64 | - } |
|
| 58 | + if ($this->getConfiguration()->getIrcNotificationsEnabled()) { |
|
| 59 | + $notificationsDatabase = PdoDatabase::getDatabaseConnection('notifications'); |
|
| 60 | + } |
|
| 61 | + else { |
|
| 62 | + // pass through null |
|
| 63 | + $notificationsDatabase = null; |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - $this->setupHelpers($this->consoleTask, $this->getConfiguration(), $database, $notificationsDatabase); |
|
| 66 | + $this->setupHelpers($this->consoleTask, $this->getConfiguration(), $database, $notificationsDatabase); |
|
| 67 | 67 | |
| 68 | - // initialise a database transaction |
|
| 69 | - if (!$database->beginTransaction()) { |
|
| 70 | - throw new Exception('Failed to start transaction on primary database.'); |
|
| 71 | - } |
|
| 68 | + // initialise a database transaction |
|
| 69 | + if (!$database->beginTransaction()) { |
|
| 70 | + throw new Exception('Failed to start transaction on primary database.'); |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - try { |
|
| 74 | - // run the task |
|
| 75 | - $this->consoleTask->execute(); |
|
| 73 | + try { |
|
| 74 | + // run the task |
|
| 75 | + $this->consoleTask->execute(); |
|
| 76 | 76 | |
| 77 | - if ($database->hasActiveTransaction()) { |
|
| 78 | - $database->commit(); |
|
| 79 | - } |
|
| 80 | - } |
|
| 81 | - finally { |
|
| 82 | - // Catch any hanging on transactions |
|
| 83 | - if ($database->hasActiveTransaction()) { |
|
| 84 | - $database->rollBack(); |
|
| 85 | - } |
|
| 86 | - } |
|
| 87 | - } |
|
| 77 | + if ($database->hasActiveTransaction()) { |
|
| 78 | + $database->commit(); |
|
| 79 | + } |
|
| 80 | + } |
|
| 81 | + finally { |
|
| 82 | + // Catch any hanging on transactions |
|
| 83 | + if ($database->hasActiveTransaction()) { |
|
| 84 | + $database->rollBack(); |
|
| 85 | + } |
|
| 86 | + } |
|
| 87 | + } |
|
| 88 | 88 | } |
| 89 | 89 | \ No newline at end of file |
@@ -18,43 +18,43 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class GlobalStateProvider implements IGlobalStateProvider |
| 20 | 20 | { |
| 21 | - /** |
|
| 22 | - * @return array |
|
| 23 | - */ |
|
| 24 | - public function &getServerSuperGlobal() |
|
| 25 | - { |
|
| 26 | - return $_SERVER; |
|
| 27 | - } |
|
| 21 | + /** |
|
| 22 | + * @return array |
|
| 23 | + */ |
|
| 24 | + public function &getServerSuperGlobal() |
|
| 25 | + { |
|
| 26 | + return $_SERVER; |
|
| 27 | + } |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * @return array |
|
| 31 | - */ |
|
| 32 | - public function &getGetSuperGlobal() |
|
| 33 | - { |
|
| 34 | - return $_GET; |
|
| 35 | - } |
|
| 29 | + /** |
|
| 30 | + * @return array |
|
| 31 | + */ |
|
| 32 | + public function &getGetSuperGlobal() |
|
| 33 | + { |
|
| 34 | + return $_GET; |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * @return array |
|
| 39 | - */ |
|
| 40 | - public function &getPostSuperGlobal() |
|
| 41 | - { |
|
| 42 | - return $_POST; |
|
| 43 | - } |
|
| 37 | + /** |
|
| 38 | + * @return array |
|
| 39 | + */ |
|
| 40 | + public function &getPostSuperGlobal() |
|
| 41 | + { |
|
| 42 | + return $_POST; |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * @return array |
|
| 47 | - */ |
|
| 48 | - public function &getSessionSuperGlobal() |
|
| 49 | - { |
|
| 50 | - return $_SESSION; |
|
| 51 | - } |
|
| 45 | + /** |
|
| 46 | + * @return array |
|
| 47 | + */ |
|
| 48 | + public function &getSessionSuperGlobal() |
|
| 49 | + { |
|
| 50 | + return $_SESSION; |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * @return array |
|
| 55 | - */ |
|
| 56 | - public function &getCookieSuperGlobal() |
|
| 57 | - { |
|
| 58 | - return $_COOKIE; |
|
| 59 | - } |
|
| 53 | + /** |
|
| 54 | + * @return array |
|
| 55 | + */ |
|
| 56 | + public function &getCookieSuperGlobal() |
|
| 57 | + { |
|
| 58 | + return $_COOKIE; |
|
| 59 | + } |
|
| 60 | 60 | } |
| 61 | 61 | \ No newline at end of file |
@@ -14,28 +14,28 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | interface IGlobalStateProvider |
| 16 | 16 | { |
| 17 | - /** |
|
| 18 | - * @return array |
|
| 19 | - */ |
|
| 20 | - public function getServerSuperGlobal(); |
|
| 17 | + /** |
|
| 18 | + * @return array |
|
| 19 | + */ |
|
| 20 | + public function getServerSuperGlobal(); |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * @return array |
|
| 24 | - */ |
|
| 25 | - public function getGetSuperGlobal(); |
|
| 22 | + /** |
|
| 23 | + * @return array |
|
| 24 | + */ |
|
| 25 | + public function getGetSuperGlobal(); |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * @return array |
|
| 29 | - */ |
|
| 30 | - public function getPostSuperGlobal(); |
|
| 27 | + /** |
|
| 28 | + * @return array |
|
| 29 | + */ |
|
| 30 | + public function getPostSuperGlobal(); |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * @return array |
|
| 34 | - */ |
|
| 35 | - public function getSessionSuperGlobal(); |
|
| 32 | + /** |
|
| 33 | + * @return array |
|
| 34 | + */ |
|
| 35 | + public function getSessionSuperGlobal(); |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * @return array |
|
| 39 | - */ |
|
| 40 | - public function getCookieSuperGlobal(); |
|
| 37 | + /** |
|
| 38 | + * @return array |
|
| 39 | + */ |
|
| 40 | + public function getCookieSuperGlobal(); |
|
| 41 | 41 | } |
| 42 | 42 | \ No newline at end of file |
@@ -18,34 +18,34 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class FakeGlobalStateProvider extends GlobalStateProvider implements IGlobalStateProvider |
| 20 | 20 | { |
| 21 | - var $server = array(); |
|
| 22 | - var $get = array(); |
|
| 23 | - var $post = array(); |
|
| 24 | - var $session = array(); |
|
| 25 | - var $cookie = array(); |
|
| 21 | + var $server = array(); |
|
| 22 | + var $get = array(); |
|
| 23 | + var $post = array(); |
|
| 24 | + var $session = array(); |
|
| 25 | + var $cookie = array(); |
|
| 26 | 26 | |
| 27 | - public function &getServerSuperGlobal() |
|
| 28 | - { |
|
| 29 | - return $this->server; |
|
| 30 | - } |
|
| 27 | + public function &getServerSuperGlobal() |
|
| 28 | + { |
|
| 29 | + return $this->server; |
|
| 30 | + } |
|
| 31 | 31 | |
| 32 | - public function &getGetSuperGlobal() |
|
| 33 | - { |
|
| 34 | - return $this->get; |
|
| 35 | - } |
|
| 32 | + public function &getGetSuperGlobal() |
|
| 33 | + { |
|
| 34 | + return $this->get; |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - public function &getPostSuperGlobal() |
|
| 38 | - { |
|
| 39 | - return $this->post; |
|
| 40 | - } |
|
| 37 | + public function &getPostSuperGlobal() |
|
| 38 | + { |
|
| 39 | + return $this->post; |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - public function &getSessionSuperGlobal() |
|
| 43 | - { |
|
| 44 | - return $this->session; |
|
| 45 | - } |
|
| 42 | + public function &getSessionSuperGlobal() |
|
| 43 | + { |
|
| 44 | + return $this->session; |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - public function &getCookieSuperGlobal() |
|
| 48 | - { |
|
| 49 | - return $this->cookie; |
|
| 50 | - } |
|
| 47 | + public function &getCookieSuperGlobal() |
|
| 48 | + { |
|
| 49 | + return $this->cookie; |
|
| 50 | + } |
|
| 51 | 51 | } |
| 52 | 52 | \ No newline at end of file |
@@ -21,107 +21,107 @@ |
||
| 21 | 21 | */ |
| 22 | 22 | class IpLocationProvider implements ILocationProvider |
| 23 | 23 | { |
| 24 | - /** @var string */ |
|
| 25 | - private $apiKey; |
|
| 26 | - /** @var PdoDatabase */ |
|
| 27 | - private $database; |
|
| 28 | - /** @var HttpHelper */ |
|
| 29 | - private $httpHelper; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * IpLocationProvider constructor. |
|
| 33 | - * |
|
| 34 | - * @param PdoDatabase $database |
|
| 35 | - * @param string $apiKey |
|
| 36 | - * @param HttpHelper $httpHelper |
|
| 37 | - */ |
|
| 38 | - public function __construct(PdoDatabase $database, $apiKey, HttpHelper $httpHelper) |
|
| 39 | - { |
|
| 40 | - $this->database = $database; |
|
| 41 | - $this->apiKey = $apiKey; |
|
| 42 | - $this->httpHelper = $httpHelper; |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * @param string $address |
|
| 47 | - * |
|
| 48 | - * @return array|null |
|
| 49 | - * @throws Exception |
|
| 50 | - * @throws OptimisticLockFailedException |
|
| 51 | - */ |
|
| 52 | - public function getIpLocation($address) |
|
| 53 | - { |
|
| 54 | - $address = trim($address); |
|
| 55 | - |
|
| 56 | - // lets look in our database first. |
|
| 57 | - $location = GeoLocation::getByAddress($address, $this->database, true); |
|
| 58 | - |
|
| 59 | - if ($location != null) { |
|
| 60 | - // touch cache timer |
|
| 61 | - $location->save(); |
|
| 62 | - |
|
| 63 | - return $location->getData(); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - // OK, it's not there, let's do an IP2Location lookup. |
|
| 67 | - $result = $this->getResult($address); |
|
| 68 | - |
|
| 69 | - if ($result != null) { |
|
| 70 | - $location = new GeoLocation(); |
|
| 71 | - $location->setDatabase($this->database); |
|
| 72 | - $location->setAddress($address); |
|
| 73 | - $location->setData($result); |
|
| 74 | - $location->save(); |
|
| 75 | - |
|
| 76 | - return $result; |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - return null; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - // adapted from http://www.ipinfodb.com/ip_location_api.php |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * @param string $ip |
|
| 86 | - * |
|
| 87 | - * @return array|null |
|
| 88 | - */ |
|
| 89 | - private function getResult($ip) |
|
| 90 | - { |
|
| 91 | - try { |
|
| 92 | - if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { |
|
| 93 | - $xml = $this->httpHelper->get($this->getApiBase(), array( |
|
| 94 | - 'key' => $this->apiKey, |
|
| 95 | - 'ip' => $ip, |
|
| 96 | - 'format' => 'xml', |
|
| 97 | - )); |
|
| 98 | - |
|
| 99 | - $response = @new SimpleXMLElement($xml); |
|
| 100 | - |
|
| 101 | - $result = array(); |
|
| 102 | - |
|
| 103 | - foreach ($response as $field => $value) { |
|
| 104 | - $result[(string)$field] = (string)$value; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - return $result; |
|
| 108 | - } |
|
| 109 | - } |
|
| 110 | - catch (Exception $ex) { |
|
| 111 | - return null; |
|
| 112 | - |
|
| 113 | - // LOGME: do something smart here, or wherever we use this value. |
|
| 114 | - // This is just a temp hack to squash errors on the UI for now. |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - return null; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * @return string |
|
| 122 | - */ |
|
| 123 | - protected function getApiBase() |
|
| 124 | - { |
|
| 125 | - return "http://api.ipinfodb.com/v3/ip-city/"; |
|
| 126 | - } |
|
| 24 | + /** @var string */ |
|
| 25 | + private $apiKey; |
|
| 26 | + /** @var PdoDatabase */ |
|
| 27 | + private $database; |
|
| 28 | + /** @var HttpHelper */ |
|
| 29 | + private $httpHelper; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * IpLocationProvider constructor. |
|
| 33 | + * |
|
| 34 | + * @param PdoDatabase $database |
|
| 35 | + * @param string $apiKey |
|
| 36 | + * @param HttpHelper $httpHelper |
|
| 37 | + */ |
|
| 38 | + public function __construct(PdoDatabase $database, $apiKey, HttpHelper $httpHelper) |
|
| 39 | + { |
|
| 40 | + $this->database = $database; |
|
| 41 | + $this->apiKey = $apiKey; |
|
| 42 | + $this->httpHelper = $httpHelper; |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * @param string $address |
|
| 47 | + * |
|
| 48 | + * @return array|null |
|
| 49 | + * @throws Exception |
|
| 50 | + * @throws OptimisticLockFailedException |
|
| 51 | + */ |
|
| 52 | + public function getIpLocation($address) |
|
| 53 | + { |
|
| 54 | + $address = trim($address); |
|
| 55 | + |
|
| 56 | + // lets look in our database first. |
|
| 57 | + $location = GeoLocation::getByAddress($address, $this->database, true); |
|
| 58 | + |
|
| 59 | + if ($location != null) { |
|
| 60 | + // touch cache timer |
|
| 61 | + $location->save(); |
|
| 62 | + |
|
| 63 | + return $location->getData(); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + // OK, it's not there, let's do an IP2Location lookup. |
|
| 67 | + $result = $this->getResult($address); |
|
| 68 | + |
|
| 69 | + if ($result != null) { |
|
| 70 | + $location = new GeoLocation(); |
|
| 71 | + $location->setDatabase($this->database); |
|
| 72 | + $location->setAddress($address); |
|
| 73 | + $location->setData($result); |
|
| 74 | + $location->save(); |
|
| 75 | + |
|
| 76 | + return $result; |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + return null; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + // adapted from http://www.ipinfodb.com/ip_location_api.php |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * @param string $ip |
|
| 86 | + * |
|
| 87 | + * @return array|null |
|
| 88 | + */ |
|
| 89 | + private function getResult($ip) |
|
| 90 | + { |
|
| 91 | + try { |
|
| 92 | + if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { |
|
| 93 | + $xml = $this->httpHelper->get($this->getApiBase(), array( |
|
| 94 | + 'key' => $this->apiKey, |
|
| 95 | + 'ip' => $ip, |
|
| 96 | + 'format' => 'xml', |
|
| 97 | + )); |
|
| 98 | + |
|
| 99 | + $response = @new SimpleXMLElement($xml); |
|
| 100 | + |
|
| 101 | + $result = array(); |
|
| 102 | + |
|
| 103 | + foreach ($response as $field => $value) { |
|
| 104 | + $result[(string)$field] = (string)$value; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + return $result; |
|
| 108 | + } |
|
| 109 | + } |
|
| 110 | + catch (Exception $ex) { |
|
| 111 | + return null; |
|
| 112 | + |
|
| 113 | + // LOGME: do something smart here, or wherever we use this value. |
|
| 114 | + // This is just a temp hack to squash errors on the UI for now. |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + return null; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * @return string |
|
| 122 | + */ |
|
| 123 | + protected function getApiBase() |
|
| 124 | + { |
|
| 125 | + return "http://api.ipinfodb.com/v3/ip-city/"; |
|
| 126 | + } |
|
| 127 | 127 | } |
@@ -15,113 +15,113 @@ |
||
| 15 | 15 | |
| 16 | 16 | class PdoDatabase extends PDO |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * @var PdoDatabase[] |
|
| 20 | - */ |
|
| 21 | - private static $connections = array(); |
|
| 22 | - /** |
|
| 23 | - * @var bool True if a transaction is active |
|
| 24 | - */ |
|
| 25 | - protected $hasActiveTransaction = false; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * Unless you're doing low-level work, this is not the function you want. |
|
| 29 | - * |
|
| 30 | - * @param string $connectionName |
|
| 31 | - * |
|
| 32 | - * @return PdoDatabase |
|
| 33 | - * @throws Exception |
|
| 34 | - */ |
|
| 35 | - public static function getDatabaseConnection($connectionName) |
|
| 36 | - { |
|
| 37 | - if (!isset(self::$connections[$connectionName])) { |
|
| 38 | - global $cDatabaseConfig; |
|
| 39 | - |
|
| 40 | - if (!array_key_exists($connectionName, $cDatabaseConfig)) { |
|
| 41 | - throw new Exception("Database configuration not found for alias $connectionName"); |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - try { |
|
| 45 | - $databaseObject = new PdoDatabase( |
|
| 46 | - $cDatabaseConfig[$connectionName]["dsrcname"], |
|
| 47 | - $cDatabaseConfig[$connectionName]["username"], |
|
| 48 | - $cDatabaseConfig[$connectionName]["password"], |
|
| 49 | - $cDatabaseConfig[$connectionName]["options"] |
|
| 50 | - ); |
|
| 51 | - } |
|
| 52 | - catch (PDOException $ex) { |
|
| 53 | - // wrap around any potential stack traces which may include passwords |
|
| 54 | - throw new EnvironmentException("Error connecting to database '$connectionName': " . $ex->getMessage()); |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - $databaseObject->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
| 58 | - |
|
| 59 | - // emulating prepared statements gives a performance boost on MySQL. |
|
| 60 | - // |
|
| 61 | - // however, our version of PDO doesn't seem to understand parameter types when emulating |
|
| 62 | - // the prepared statements, so we're forced to turn this off for now. |
|
| 63 | - // -- stw 2014-02-11 |
|
| 64 | - $databaseObject->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); |
|
| 65 | - |
|
| 66 | - self::$connections[$connectionName] = $databaseObject; |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - return self::$connections[$connectionName]; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * Determines if this connection has a transaction in progress or not |
|
| 74 | - * @return boolean true if there is a transaction in progress. |
|
| 75 | - */ |
|
| 76 | - public function hasActiveTransaction() |
|
| 77 | - { |
|
| 78 | - return $this->hasActiveTransaction; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Summary of beginTransaction |
|
| 83 | - * @return bool |
|
| 84 | - */ |
|
| 85 | - public function beginTransaction() |
|
| 86 | - { |
|
| 87 | - // Override the pre-existing method, which doesn't stop you from |
|
| 88 | - // starting transactions within transactions - which doesn't work and |
|
| 89 | - // will throw an exception. This eliminates the need to catch exceptions |
|
| 90 | - // all over the rest of the code |
|
| 91 | - if ($this->hasActiveTransaction) { |
|
| 92 | - return false; |
|
| 93 | - } |
|
| 94 | - else { |
|
| 95 | - // set the transaction isolation level for every transaction. |
|
| 96 | - $this->exec("SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;"); |
|
| 97 | - |
|
| 98 | - // start a new transaction, and return whether or not the start was |
|
| 99 | - // successful |
|
| 100 | - $this->hasActiveTransaction = parent::beginTransaction(); |
|
| 101 | - |
|
| 102 | - return $this->hasActiveTransaction; |
|
| 103 | - } |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * Commits the active transaction |
|
| 108 | - */ |
|
| 109 | - public function commit() |
|
| 110 | - { |
|
| 111 | - if ($this->hasActiveTransaction) { |
|
| 112 | - parent::commit(); |
|
| 113 | - $this->hasActiveTransaction = false; |
|
| 114 | - } |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * Rolls back a transaction |
|
| 119 | - */ |
|
| 120 | - public function rollBack() |
|
| 121 | - { |
|
| 122 | - if ($this->hasActiveTransaction) { |
|
| 123 | - parent::rollback(); |
|
| 124 | - $this->hasActiveTransaction = false; |
|
| 125 | - } |
|
| 126 | - } |
|
| 18 | + /** |
|
| 19 | + * @var PdoDatabase[] |
|
| 20 | + */ |
|
| 21 | + private static $connections = array(); |
|
| 22 | + /** |
|
| 23 | + * @var bool True if a transaction is active |
|
| 24 | + */ |
|
| 25 | + protected $hasActiveTransaction = false; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * Unless you're doing low-level work, this is not the function you want. |
|
| 29 | + * |
|
| 30 | + * @param string $connectionName |
|
| 31 | + * |
|
| 32 | + * @return PdoDatabase |
|
| 33 | + * @throws Exception |
|
| 34 | + */ |
|
| 35 | + public static function getDatabaseConnection($connectionName) |
|
| 36 | + { |
|
| 37 | + if (!isset(self::$connections[$connectionName])) { |
|
| 38 | + global $cDatabaseConfig; |
|
| 39 | + |
|
| 40 | + if (!array_key_exists($connectionName, $cDatabaseConfig)) { |
|
| 41 | + throw new Exception("Database configuration not found for alias $connectionName"); |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + try { |
|
| 45 | + $databaseObject = new PdoDatabase( |
|
| 46 | + $cDatabaseConfig[$connectionName]["dsrcname"], |
|
| 47 | + $cDatabaseConfig[$connectionName]["username"], |
|
| 48 | + $cDatabaseConfig[$connectionName]["password"], |
|
| 49 | + $cDatabaseConfig[$connectionName]["options"] |
|
| 50 | + ); |
|
| 51 | + } |
|
| 52 | + catch (PDOException $ex) { |
|
| 53 | + // wrap around any potential stack traces which may include passwords |
|
| 54 | + throw new EnvironmentException("Error connecting to database '$connectionName': " . $ex->getMessage()); |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + $databaseObject->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
| 58 | + |
|
| 59 | + // emulating prepared statements gives a performance boost on MySQL. |
|
| 60 | + // |
|
| 61 | + // however, our version of PDO doesn't seem to understand parameter types when emulating |
|
| 62 | + // the prepared statements, so we're forced to turn this off for now. |
|
| 63 | + // -- stw 2014-02-11 |
|
| 64 | + $databaseObject->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); |
|
| 65 | + |
|
| 66 | + self::$connections[$connectionName] = $databaseObject; |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + return self::$connections[$connectionName]; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * Determines if this connection has a transaction in progress or not |
|
| 74 | + * @return boolean true if there is a transaction in progress. |
|
| 75 | + */ |
|
| 76 | + public function hasActiveTransaction() |
|
| 77 | + { |
|
| 78 | + return $this->hasActiveTransaction; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Summary of beginTransaction |
|
| 83 | + * @return bool |
|
| 84 | + */ |
|
| 85 | + public function beginTransaction() |
|
| 86 | + { |
|
| 87 | + // Override the pre-existing method, which doesn't stop you from |
|
| 88 | + // starting transactions within transactions - which doesn't work and |
|
| 89 | + // will throw an exception. This eliminates the need to catch exceptions |
|
| 90 | + // all over the rest of the code |
|
| 91 | + if ($this->hasActiveTransaction) { |
|
| 92 | + return false; |
|
| 93 | + } |
|
| 94 | + else { |
|
| 95 | + // set the transaction isolation level for every transaction. |
|
| 96 | + $this->exec("SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;"); |
|
| 97 | + |
|
| 98 | + // start a new transaction, and return whether or not the start was |
|
| 99 | + // successful |
|
| 100 | + $this->hasActiveTransaction = parent::beginTransaction(); |
|
| 101 | + |
|
| 102 | + return $this->hasActiveTransaction; |
|
| 103 | + } |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * Commits the active transaction |
|
| 108 | + */ |
|
| 109 | + public function commit() |
|
| 110 | + { |
|
| 111 | + if ($this->hasActiveTransaction) { |
|
| 112 | + parent::commit(); |
|
| 113 | + $this->hasActiveTransaction = false; |
|
| 114 | + } |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * Rolls back a transaction |
|
| 119 | + */ |
|
| 120 | + public function rollBack() |
|
| 121 | + { |
|
| 122 | + if ($this->hasActiveTransaction) { |
|
| 123 | + parent::rollback(); |
|
| 124 | + $this->hasActiveTransaction = false; |
|
| 125 | + } |
|
| 126 | + } |
|
| 127 | 127 | } |
@@ -26,133 +26,133 @@ discard block |
||
| 26 | 26 | */ |
| 27 | 27 | class IdentificationVerifier |
| 28 | 28 | { |
| 29 | - /** |
|
| 30 | - * This field is an array of parameters, in key => value format, that should be appended to the Meta Wikimedia |
|
| 31 | - * Web Service Endpoint URL to query if a user is listed on the Identification Noticeboard. Note that URL encoding |
|
| 32 | - * of these values is *not* necessary; this is done automatically. |
|
| 33 | - * |
|
| 34 | - * @var string[] |
|
| 35 | - * @category Security-Critical |
|
| 36 | - */ |
|
| 37 | - private static $apiQueryParameters = array( |
|
| 38 | - 'action' => 'query', |
|
| 39 | - 'format' => 'json', |
|
| 40 | - 'prop' => 'links', |
|
| 41 | - // Populated from SiteConfiguration->getIdentificationNoticeboardPage |
|
| 42 | - 'titles' => '', |
|
| 43 | - // Username of the user to be checked, with User: prefix, goes here! Set in isIdentifiedOnWiki() |
|
| 44 | - 'pltitles' => '', |
|
| 45 | - ); |
|
| 46 | - /** @var HttpHelper */ |
|
| 47 | - private $httpHelper; |
|
| 48 | - /** @var SiteConfiguration */ |
|
| 49 | - private $siteConfiguration; |
|
| 50 | - /** @var PdoDatabase */ |
|
| 51 | - private $dbObject; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * IdentificationVerifier constructor. |
|
| 55 | - * |
|
| 56 | - * @param HttpHelper $httpHelper |
|
| 57 | - * @param SiteConfiguration $siteConfiguration |
|
| 58 | - * @param PdoDatabase $dbObject |
|
| 59 | - */ |
|
| 60 | - public function __construct(HttpHelper $httpHelper, SiteConfiguration $siteConfiguration, PdoDatabase $dbObject) |
|
| 61 | - { |
|
| 62 | - $this->httpHelper = $httpHelper; |
|
| 63 | - $this->siteConfiguration = $siteConfiguration; |
|
| 64 | - $this->dbObject = $dbObject; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * Checks if the given user is identified to the Wikimedia Foundation. |
|
| 69 | - * |
|
| 70 | - * @param string $onWikiName The Wikipedia username of the user |
|
| 71 | - * |
|
| 72 | - * @return bool |
|
| 73 | - * @category Security-Critical |
|
| 74 | - * @throws EnvironmentException |
|
| 75 | - */ |
|
| 76 | - public function isUserIdentified($onWikiName) |
|
| 77 | - { |
|
| 78 | - if ($this->checkIdentificationCache($onWikiName)) { |
|
| 79 | - return true; |
|
| 80 | - } |
|
| 81 | - else { |
|
| 82 | - if ($this->isIdentifiedOnWiki($onWikiName)) { |
|
| 83 | - $this->cacheIdentificationStatus($onWikiName); |
|
| 84 | - |
|
| 85 | - return true; |
|
| 86 | - } |
|
| 87 | - else { |
|
| 88 | - return false; |
|
| 89 | - } |
|
| 90 | - } |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Checks if the given user has a valid entry in the idcache table. |
|
| 95 | - * |
|
| 96 | - * @param string $onWikiName The Wikipedia username of the user |
|
| 97 | - * |
|
| 98 | - * @return bool |
|
| 99 | - * @category Security-Critical |
|
| 100 | - */ |
|
| 101 | - private function checkIdentificationCache($onWikiName) |
|
| 102 | - { |
|
| 103 | - $interval = $this->siteConfiguration->getIdentificationCacheExpiry(); |
|
| 104 | - |
|
| 105 | - $query = <<<SQL |
|
| 29 | + /** |
|
| 30 | + * This field is an array of parameters, in key => value format, that should be appended to the Meta Wikimedia |
|
| 31 | + * Web Service Endpoint URL to query if a user is listed on the Identification Noticeboard. Note that URL encoding |
|
| 32 | + * of these values is *not* necessary; this is done automatically. |
|
| 33 | + * |
|
| 34 | + * @var string[] |
|
| 35 | + * @category Security-Critical |
|
| 36 | + */ |
|
| 37 | + private static $apiQueryParameters = array( |
|
| 38 | + 'action' => 'query', |
|
| 39 | + 'format' => 'json', |
|
| 40 | + 'prop' => 'links', |
|
| 41 | + // Populated from SiteConfiguration->getIdentificationNoticeboardPage |
|
| 42 | + 'titles' => '', |
|
| 43 | + // Username of the user to be checked, with User: prefix, goes here! Set in isIdentifiedOnWiki() |
|
| 44 | + 'pltitles' => '', |
|
| 45 | + ); |
|
| 46 | + /** @var HttpHelper */ |
|
| 47 | + private $httpHelper; |
|
| 48 | + /** @var SiteConfiguration */ |
|
| 49 | + private $siteConfiguration; |
|
| 50 | + /** @var PdoDatabase */ |
|
| 51 | + private $dbObject; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * IdentificationVerifier constructor. |
|
| 55 | + * |
|
| 56 | + * @param HttpHelper $httpHelper |
|
| 57 | + * @param SiteConfiguration $siteConfiguration |
|
| 58 | + * @param PdoDatabase $dbObject |
|
| 59 | + */ |
|
| 60 | + public function __construct(HttpHelper $httpHelper, SiteConfiguration $siteConfiguration, PdoDatabase $dbObject) |
|
| 61 | + { |
|
| 62 | + $this->httpHelper = $httpHelper; |
|
| 63 | + $this->siteConfiguration = $siteConfiguration; |
|
| 64 | + $this->dbObject = $dbObject; |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * Checks if the given user is identified to the Wikimedia Foundation. |
|
| 69 | + * |
|
| 70 | + * @param string $onWikiName The Wikipedia username of the user |
|
| 71 | + * |
|
| 72 | + * @return bool |
|
| 73 | + * @category Security-Critical |
|
| 74 | + * @throws EnvironmentException |
|
| 75 | + */ |
|
| 76 | + public function isUserIdentified($onWikiName) |
|
| 77 | + { |
|
| 78 | + if ($this->checkIdentificationCache($onWikiName)) { |
|
| 79 | + return true; |
|
| 80 | + } |
|
| 81 | + else { |
|
| 82 | + if ($this->isIdentifiedOnWiki($onWikiName)) { |
|
| 83 | + $this->cacheIdentificationStatus($onWikiName); |
|
| 84 | + |
|
| 85 | + return true; |
|
| 86 | + } |
|
| 87 | + else { |
|
| 88 | + return false; |
|
| 89 | + } |
|
| 90 | + } |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Checks if the given user has a valid entry in the idcache table. |
|
| 95 | + * |
|
| 96 | + * @param string $onWikiName The Wikipedia username of the user |
|
| 97 | + * |
|
| 98 | + * @return bool |
|
| 99 | + * @category Security-Critical |
|
| 100 | + */ |
|
| 101 | + private function checkIdentificationCache($onWikiName) |
|
| 102 | + { |
|
| 103 | + $interval = $this->siteConfiguration->getIdentificationCacheExpiry(); |
|
| 104 | + |
|
| 105 | + $query = <<<SQL |
|
| 106 | 106 | SELECT COUNT(`id`) |
| 107 | 107 | FROM `idcache` |
| 108 | 108 | WHERE `onwikiusername` = :onwikiname |
| 109 | 109 | AND DATE_ADD(`checktime`, INTERVAL {$interval}) >= NOW(); |
| 110 | 110 | SQL; |
| 111 | - $stmt = $this->dbObject->prepare($query); |
|
| 112 | - $stmt->bindValue(':onwikiname', $onWikiName, PDO::PARAM_STR); |
|
| 113 | - $stmt->execute(); |
|
| 114 | - |
|
| 115 | - // Guaranteed by the query to only return a single row with a single column |
|
| 116 | - $results = $stmt->fetch(PDO::FETCH_NUM); |
|
| 117 | - |
|
| 118 | - // I don't expect this to ever be a value other than 0 or 1 since the `onwikiusername` column is declared as a |
|
| 119 | - // unique key - but meh. |
|
| 120 | - return $results[0] != 0; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Does pretty much exactly what it says on the label - this method will clear all expired idcache entries from the |
|
| 125 | - * idcache table. Meant to be called periodically by a maintenance script. |
|
| 126 | - * |
|
| 127 | - * @param SiteConfiguration $siteConfiguration |
|
| 128 | - * @param PdoDatabase $dbObject |
|
| 129 | - * |
|
| 130 | - * @return void |
|
| 131 | - */ |
|
| 132 | - public static function clearExpiredCacheEntries(SiteConfiguration $siteConfiguration, PdoDatabase $dbObject) |
|
| 133 | - { |
|
| 134 | - $interval = $siteConfiguration->getIdentificationCacheExpiry(); |
|
| 135 | - |
|
| 136 | - $query = <<<SQL |
|
| 111 | + $stmt = $this->dbObject->prepare($query); |
|
| 112 | + $stmt->bindValue(':onwikiname', $onWikiName, PDO::PARAM_STR); |
|
| 113 | + $stmt->execute(); |
|
| 114 | + |
|
| 115 | + // Guaranteed by the query to only return a single row with a single column |
|
| 116 | + $results = $stmt->fetch(PDO::FETCH_NUM); |
|
| 117 | + |
|
| 118 | + // I don't expect this to ever be a value other than 0 or 1 since the `onwikiusername` column is declared as a |
|
| 119 | + // unique key - but meh. |
|
| 120 | + return $results[0] != 0; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Does pretty much exactly what it says on the label - this method will clear all expired idcache entries from the |
|
| 125 | + * idcache table. Meant to be called periodically by a maintenance script. |
|
| 126 | + * |
|
| 127 | + * @param SiteConfiguration $siteConfiguration |
|
| 128 | + * @param PdoDatabase $dbObject |
|
| 129 | + * |
|
| 130 | + * @return void |
|
| 131 | + */ |
|
| 132 | + public static function clearExpiredCacheEntries(SiteConfiguration $siteConfiguration, PdoDatabase $dbObject) |
|
| 133 | + { |
|
| 134 | + $interval = $siteConfiguration->getIdentificationCacheExpiry(); |
|
| 135 | + |
|
| 136 | + $query = <<<SQL |
|
| 137 | 137 | DELETE FROM `idcache` |
| 138 | 138 | WHERE DATE_ADD(`checktime`, INTERVAL {$interval}) < NOW(); |
| 139 | 139 | SQL; |
| 140 | - $dbObject->prepare($query)->execute(); |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * This method will add an entry to the idcache that the given Wikipedia user has been verified as identified. This |
|
| 145 | - * is so we don't have to hit the API every single time we check. The cache entry is valid for as long as specified |
|
| 146 | - * in the ACC configuration (validity enforced by checkIdentificationCache() and clearExpiredCacheEntries()). |
|
| 147 | - * |
|
| 148 | - * @param string $onWikiName The Wikipedia username of the user |
|
| 149 | - * |
|
| 150 | - * @return void |
|
| 151 | - * @category Security-Critical |
|
| 152 | - */ |
|
| 153 | - private function cacheIdentificationStatus($onWikiName) |
|
| 154 | - { |
|
| 155 | - $query = <<<SQL |
|
| 140 | + $dbObject->prepare($query)->execute(); |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * This method will add an entry to the idcache that the given Wikipedia user has been verified as identified. This |
|
| 145 | + * is so we don't have to hit the API every single time we check. The cache entry is valid for as long as specified |
|
| 146 | + * in the ACC configuration (validity enforced by checkIdentificationCache() and clearExpiredCacheEntries()). |
|
| 147 | + * |
|
| 148 | + * @param string $onWikiName The Wikipedia username of the user |
|
| 149 | + * |
|
| 150 | + * @return void |
|
| 151 | + * @category Security-Critical |
|
| 152 | + */ |
|
| 153 | + private function cacheIdentificationStatus($onWikiName) |
|
| 154 | + { |
|
| 155 | + $query = <<<SQL |
|
| 156 | 156 | INSERT INTO `idcache` |
| 157 | 157 | (`onwikiusername`) |
| 158 | 158 | VALUES |
@@ -161,45 +161,45 @@ discard block |
||
| 161 | 161 | `onwikiusername` = VALUES(`onwikiusername`), |
| 162 | 162 | `checktime` = CURRENT_TIMESTAMP; |
| 163 | 163 | SQL; |
| 164 | - $stmt = $this->dbObject->prepare($query); |
|
| 165 | - $stmt->bindValue(':onwikiname', $onWikiName, PDO::PARAM_STR); |
|
| 166 | - $stmt->execute(); |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * Queries the Wikimedia API to determine if the specified user is listed on the identification noticeboard. |
|
| 171 | - * |
|
| 172 | - * @param string $onWikiName The Wikipedia username of the user |
|
| 173 | - * |
|
| 174 | - * @return bool |
|
| 175 | - * @throws EnvironmentException |
|
| 176 | - * @category Security-Critical |
|
| 177 | - */ |
|
| 178 | - private function isIdentifiedOnWiki($onWikiName) |
|
| 179 | - { |
|
| 180 | - $strings = new StringFunctions(); |
|
| 181 | - |
|
| 182 | - // First character of Wikipedia usernames is always capitalized. |
|
| 183 | - $onWikiName = $strings->upperCaseFirst($onWikiName); |
|
| 184 | - |
|
| 185 | - $parameters = self::$apiQueryParameters; |
|
| 186 | - $parameters['pltitles'] = "User:" . $onWikiName; |
|
| 187 | - $parameters['titles'] = $this->siteConfiguration->getIdentificationNoticeboardPage(); |
|
| 188 | - |
|
| 189 | - try { |
|
| 190 | - $endpoint = $this->siteConfiguration->getMetaWikimediaWebServiceEndpoint(); |
|
| 191 | - $response = $this->httpHelper->get($endpoint, $parameters); |
|
| 192 | - $response = json_decode($response, true); |
|
| 193 | - } catch (CurlException $ex) { |
|
| 194 | - // failed getting identification status, so throw a nicer error. |
|
| 195 | - $message = 'Could not contact metawiki API to determine user\' identification status. ' |
|
| 196 | - . 'This is probably a transient error, so please try again.'; |
|
| 197 | - |
|
| 198 | - throw new EnvironmentException($message); |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - $page = @array_pop($response['query']['pages']); |
|
| 202 | - |
|
| 203 | - return @$page['links'][0]['title'] === "User:" . $onWikiName; |
|
| 204 | - } |
|
| 164 | + $stmt = $this->dbObject->prepare($query); |
|
| 165 | + $stmt->bindValue(':onwikiname', $onWikiName, PDO::PARAM_STR); |
|
| 166 | + $stmt->execute(); |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * Queries the Wikimedia API to determine if the specified user is listed on the identification noticeboard. |
|
| 171 | + * |
|
| 172 | + * @param string $onWikiName The Wikipedia username of the user |
|
| 173 | + * |
|
| 174 | + * @return bool |
|
| 175 | + * @throws EnvironmentException |
|
| 176 | + * @category Security-Critical |
|
| 177 | + */ |
|
| 178 | + private function isIdentifiedOnWiki($onWikiName) |
|
| 179 | + { |
|
| 180 | + $strings = new StringFunctions(); |
|
| 181 | + |
|
| 182 | + // First character of Wikipedia usernames is always capitalized. |
|
| 183 | + $onWikiName = $strings->upperCaseFirst($onWikiName); |
|
| 184 | + |
|
| 185 | + $parameters = self::$apiQueryParameters; |
|
| 186 | + $parameters['pltitles'] = "User:" . $onWikiName; |
|
| 187 | + $parameters['titles'] = $this->siteConfiguration->getIdentificationNoticeboardPage(); |
|
| 188 | + |
|
| 189 | + try { |
|
| 190 | + $endpoint = $this->siteConfiguration->getMetaWikimediaWebServiceEndpoint(); |
|
| 191 | + $response = $this->httpHelper->get($endpoint, $parameters); |
|
| 192 | + $response = json_decode($response, true); |
|
| 193 | + } catch (CurlException $ex) { |
|
| 194 | + // failed getting identification status, so throw a nicer error. |
|
| 195 | + $message = 'Could not contact metawiki API to determine user\' identification status. ' |
|
| 196 | + . 'This is probably a transient error, so please try again.'; |
|
| 197 | + |
|
| 198 | + throw new EnvironmentException($message); |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + $page = @array_pop($response['query']['pages']); |
|
| 202 | + |
|
| 203 | + return @$page['links'][0]['title'] === "User:" . $onWikiName; |
|
| 204 | + } |
|
| 205 | 205 | } |
@@ -21,141 +21,141 @@ |
||
| 21 | 21 | */ |
| 22 | 22 | class SessionAlert |
| 23 | 23 | { |
| 24 | - private $message; |
|
| 25 | - private $title; |
|
| 26 | - private $type; |
|
| 27 | - private $closable; |
|
| 28 | - private $block; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * @param string $message |
|
| 32 | - * @param string $title |
|
| 33 | - * @param string $type |
|
| 34 | - * @param bool $closable |
|
| 35 | - * @param bool $block |
|
| 36 | - */ |
|
| 37 | - public function __construct($message, $title, $type = "alert-info", $closable = true, $block = true) |
|
| 38 | - { |
|
| 39 | - $this->message = $message; |
|
| 40 | - $this->title = $title; |
|
| 41 | - $this->type = $type; |
|
| 42 | - $this->closable = $closable; |
|
| 43 | - $this->block = $block; |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * Shows a quick one-liner message |
|
| 48 | - * |
|
| 49 | - * @param string $message |
|
| 50 | - * @param string $type |
|
| 51 | - */ |
|
| 52 | - public static function quick($message, $type = "alert-info") |
|
| 53 | - { |
|
| 54 | - self::append(new SessionAlert($message, "", $type, true, false)); |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * @param SessionAlert $alert |
|
| 59 | - */ |
|
| 60 | - public static function append(SessionAlert $alert) |
|
| 61 | - { |
|
| 62 | - $data = WebRequest::getSessionAlertData(); |
|
| 63 | - $data[] = serialize($alert); |
|
| 64 | - WebRequest::setSessionAlertData($data); |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * Shows a quick one-liner success message |
|
| 69 | - * |
|
| 70 | - * @param string $message |
|
| 71 | - */ |
|
| 72 | - public static function success($message) |
|
| 73 | - { |
|
| 74 | - self::append(new SessionAlert($message, "", "alert-success", true, true)); |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Shows a quick one-liner warning message |
|
| 79 | - * |
|
| 80 | - * @param string $message |
|
| 81 | - * @param string $title |
|
| 82 | - */ |
|
| 83 | - public static function warning($message, $title = "Warning!") |
|
| 84 | - { |
|
| 85 | - self::append(new SessionAlert($message, $title, "alert-warning", true, true)); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * Shows a quick one-liner error message |
|
| 90 | - * |
|
| 91 | - * @param string $message |
|
| 92 | - * @param string $title |
|
| 93 | - */ |
|
| 94 | - public static function error($message, $title = "Error!") |
|
| 95 | - { |
|
| 96 | - self::append(new SessionAlert($message, $title, "alert-danger", true, true)); |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * Retrieves the alerts which have been saved to the session |
|
| 101 | - * @return array |
|
| 102 | - */ |
|
| 103 | - public static function getAlerts() |
|
| 104 | - { |
|
| 105 | - $alertData = array(); |
|
| 106 | - |
|
| 107 | - foreach (WebRequest::getSessionAlertData() as $a) { |
|
| 108 | - $alertData[] = unserialize($a); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - return $alertData; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Clears the alerts from the session |
|
| 116 | - */ |
|
| 117 | - public static function clearAlerts() |
|
| 118 | - { |
|
| 119 | - WebRequest::clearSessionAlertData(); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * @return boolean |
|
| 124 | - */ |
|
| 125 | - public function isBlock() |
|
| 126 | - { |
|
| 127 | - return $this->block; |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * @return boolean |
|
| 132 | - */ |
|
| 133 | - public function isClosable() |
|
| 134 | - { |
|
| 135 | - return $this->closable; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * @return string |
|
| 140 | - */ |
|
| 141 | - public function getType() |
|
| 142 | - { |
|
| 143 | - return $this->type; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * @return string |
|
| 148 | - */ |
|
| 149 | - public function getTitle() |
|
| 150 | - { |
|
| 151 | - return $this->title; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * @return string |
|
| 156 | - */ |
|
| 157 | - public function getMessage() |
|
| 158 | - { |
|
| 159 | - return $this->message; |
|
| 160 | - } |
|
| 24 | + private $message; |
|
| 25 | + private $title; |
|
| 26 | + private $type; |
|
| 27 | + private $closable; |
|
| 28 | + private $block; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * @param string $message |
|
| 32 | + * @param string $title |
|
| 33 | + * @param string $type |
|
| 34 | + * @param bool $closable |
|
| 35 | + * @param bool $block |
|
| 36 | + */ |
|
| 37 | + public function __construct($message, $title, $type = "alert-info", $closable = true, $block = true) |
|
| 38 | + { |
|
| 39 | + $this->message = $message; |
|
| 40 | + $this->title = $title; |
|
| 41 | + $this->type = $type; |
|
| 42 | + $this->closable = $closable; |
|
| 43 | + $this->block = $block; |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * Shows a quick one-liner message |
|
| 48 | + * |
|
| 49 | + * @param string $message |
|
| 50 | + * @param string $type |
|
| 51 | + */ |
|
| 52 | + public static function quick($message, $type = "alert-info") |
|
| 53 | + { |
|
| 54 | + self::append(new SessionAlert($message, "", $type, true, false)); |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * @param SessionAlert $alert |
|
| 59 | + */ |
|
| 60 | + public static function append(SessionAlert $alert) |
|
| 61 | + { |
|
| 62 | + $data = WebRequest::getSessionAlertData(); |
|
| 63 | + $data[] = serialize($alert); |
|
| 64 | + WebRequest::setSessionAlertData($data); |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * Shows a quick one-liner success message |
|
| 69 | + * |
|
| 70 | + * @param string $message |
|
| 71 | + */ |
|
| 72 | + public static function success($message) |
|
| 73 | + { |
|
| 74 | + self::append(new SessionAlert($message, "", "alert-success", true, true)); |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Shows a quick one-liner warning message |
|
| 79 | + * |
|
| 80 | + * @param string $message |
|
| 81 | + * @param string $title |
|
| 82 | + */ |
|
| 83 | + public static function warning($message, $title = "Warning!") |
|
| 84 | + { |
|
| 85 | + self::append(new SessionAlert($message, $title, "alert-warning", true, true)); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * Shows a quick one-liner error message |
|
| 90 | + * |
|
| 91 | + * @param string $message |
|
| 92 | + * @param string $title |
|
| 93 | + */ |
|
| 94 | + public static function error($message, $title = "Error!") |
|
| 95 | + { |
|
| 96 | + self::append(new SessionAlert($message, $title, "alert-danger", true, true)); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * Retrieves the alerts which have been saved to the session |
|
| 101 | + * @return array |
|
| 102 | + */ |
|
| 103 | + public static function getAlerts() |
|
| 104 | + { |
|
| 105 | + $alertData = array(); |
|
| 106 | + |
|
| 107 | + foreach (WebRequest::getSessionAlertData() as $a) { |
|
| 108 | + $alertData[] = unserialize($a); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + return $alertData; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Clears the alerts from the session |
|
| 116 | + */ |
|
| 117 | + public static function clearAlerts() |
|
| 118 | + { |
|
| 119 | + WebRequest::clearSessionAlertData(); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * @return boolean |
|
| 124 | + */ |
|
| 125 | + public function isBlock() |
|
| 126 | + { |
|
| 127 | + return $this->block; |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * @return boolean |
|
| 132 | + */ |
|
| 133 | + public function isClosable() |
|
| 134 | + { |
|
| 135 | + return $this->closable; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * @return string |
|
| 140 | + */ |
|
| 141 | + public function getType() |
|
| 142 | + { |
|
| 143 | + return $this->type; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * @return string |
|
| 148 | + */ |
|
| 149 | + public function getTitle() |
|
| 150 | + { |
|
| 151 | + return $this->title; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * @return string |
|
| 156 | + */ |
|
| 157 | + public function getMessage() |
|
| 158 | + { |
|
| 159 | + return $this->message; |
|
| 160 | + } |
|
| 161 | 161 | } |