diff options
author | Roeland Jago Douma <roeland@famdouma.nl> | 2017-04-10 13:34:50 +0300 |
---|---|---|
committer | Roeland Jago Douma <roeland@famdouma.nl> | 2017-04-10 13:34:50 +0300 |
commit | fe81493f6ba17a2c876b8f1e0463e8156948f4c4 (patch) | |
tree | 935a5b1e9e3cc27d86c4cf546ec76b76be8917eb | |
parent | 5153616b59f85b467a46e1356ff2dda208a5c57a (diff) |
Fix replication export
Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
-rwxr-xr-x | doc/architecture.md | 2 | ||||
-rw-r--r-- | server/index.php | 2 | ||||
-rw-r--r-- | server/lib/Replication.php | 82 | ||||
-rw-r--r-- | server/lib/UserManager.php | 3 | ||||
-rw-r--r-- | server/src/config.php | 3 | ||||
-rw-r--r-- | server/src/dependencies.php | 3 | ||||
-rw-r--r-- | server/vendor/composer/ClassLoader.php | 10 | ||||
-rw-r--r-- | server/vendor/composer/LICENSE | 2 | ||||
-rw-r--r-- | server/vendor/composer/autoload_classmap.php | 1 | ||||
-rw-r--r-- | server/vendor/composer/autoload_static.php | 1 |
10 files changed, 102 insertions, 7 deletions
diff --git a/doc/architecture.md b/doc/architecture.md index 32f0b82..44660c9 100755 --- a/doc/architecture.md +++ b/doc/architecture.md @@ -117,7 +117,7 @@ curl -X GET http://dev/nextcloud/lookup-server/server/users?search=searchstring ### Get replication log This call is used for master-master replication between different nodes. Example: -curl -X GET http://lookup:foobar@dev/nextcloud/lookup-server/server/replication.php/?timestamp=123456\&page=0 +curl -X GET http://lookup:foobar@dev/nextcloud/lookup-server/replication?timestamp=123456\&page=0 ## High availability Several Lookup-Server can do master-master replication and sync their data. diff --git a/server/index.php b/server/index.php index de9c028..457d2ba 100644 --- a/server/index.php +++ b/server/index.php @@ -22,4 +22,6 @@ $app->delete('/users', 'UserManager:delete'); $app->get('/validate/email/{token}', 'EmailValidator:validate')->setName('validateEmail'); $app->get('/status', 'Status:status'); +$app->get('/replication', 'Replication:export'); + $app->run(); diff --git a/server/lib/Replication.php b/server/lib/Replication.php new file mode 100644 index 0000000..a7c5cef --- /dev/null +++ b/server/lib/Replication.php @@ -0,0 +1,82 @@ +<?php + +namespace LookupServer; + +use Slim\Http\Request; +use Slim\Http\Response; + +class Replication { + + /** @var \PDO */ + private $db; + + /** @var string */ + private $auth; + + public function __construct(\PDO $db, $auth) { + $this->db = $db; + $this->auth = $auth; + } + + public function export(Request $request, Response $response) { + $userInfo = $request->getUri()->getUserInfo(); + + $userInfo = explode(':', $userInfo, 2); + + if (count($userInfo) !== 2 || $userInfo[0] !== 'lookup' || $userInfo[1] !== $this->auth) { + $response = $response->withStatus(401); + return $response; + } + + $params = $request->getQueryParams(); + if (!isset($params['timestamp'], $params['page']) || !ctype_digit($params['timestamp']) || + !ctype_digit($params['page'])) { + $response = $response->withStatus(400); + return $response; + } + + $timestamp = (int)$params['timestamp']; + $page = (int)$params['page']; + + $stmt = $this->db->prepare('SELECT * + FROM users + WHERE timestamp >= :timestamp + ORDER BY timestamp, id + LIMIT :limit + OFFSET :offset'); + $stmt->bindParam('timestamp', $timestamp); + $stmt->bindValue('limit', 100, \PDO::PARAM_INT); + $stmt->bindValue('offset', 100 * $page, \PDO::PARAM_INT); + + $stmt->execute(); + + $result = []; + while($data = $stmt->fetch()) { + $user = [ + 'federationId' => $data['federationId'], + 'timestamp' => $data['timestamp'], + 'data' => [], + ]; + + $stmt2 = $this->db->prepare('SELECT * + FROM store + WHERE userId = :uid'); + $stmt2->bindValue('uid', $data['id']); + $stmt2->execute(); + + while($userData = $stmt2->fetch()) { + $user['data'][] = [ + 'key' => $userData['k'], + 'value' => $userData['v'], + 'validated' => $userData['valid'], + ]; + } + $stmt2->closeCursor(); + + $result[] = $user; + } + + $response->getBody()->write(json_encode($result)); + return $response; + } +} diff --git a/server/lib/UserManager.php b/server/lib/UserManager.php index 8c995d5..fd8f181 100644 --- a/server/lib/UserManager.php +++ b/server/lib/UserManager.php @@ -25,7 +25,8 @@ class UserManager { if (!isset($params['search']) || $params['search'] === '') { $response->withStatus(404); - return $response; } + return $response; + } $search = $params['search']; $stmt = $this->db->prepare('SELECT * diff --git a/server/src/config.php b/server/src/config.php index 960bae7..534f700 100644 --- a/server/src/config.php +++ b/server/src/config.php @@ -4,7 +4,7 @@ require __DIR__ . '/../config/config.php'; return [ 'settings' => [ - 'displayErrorDetails' => false, + 'displayErrorDetails' => true, 'addContentLengthHeader' => true, 'db' => [ 'host' => $CONFIG['DB']['host'], @@ -14,5 +14,6 @@ return [ ], 'host' => $CONFIG['PUBLIC_URL'], 'emailfrom' => $CONFIG['EMAIL_SENDER'], + 'replication_auth' => $CONFIG['REPLICATION_AUTH'], ] ]; diff --git a/server/src/dependencies.php b/server/src/dependencies.php index 5d186ca..d08f920 100644 --- a/server/src/dependencies.php +++ b/server/src/dependencies.php @@ -22,3 +22,6 @@ $container['EmailValidator'] = function($c) { $container['Status'] = function($c) { return new \LookupServer\Status(); }; +$container['Replication'] = function ($c) { + return new \LookupServer\Replication($c->db, $c->settings['replication_auth']); +}; diff --git a/server/vendor/composer/ClassLoader.php b/server/vendor/composer/ClassLoader.php index 4626994..2c72175 100644 --- a/server/vendor/composer/ClassLoader.php +++ b/server/vendor/composer/ClassLoader.php @@ -374,9 +374,13 @@ class ClassLoader $first = $class[0]; if (isset($this->prefixLengthsPsr4[$first])) { - foreach ($this->prefixLengthsPsr4[$first] as $prefix => $length) { - if (0 === strpos($class, $prefix)) { - foreach ($this->prefixDirsPsr4[$prefix] as $dir) { + $subPath = $class; + while (false !== $lastPos = strrpos($subPath, '\\')) { + $subPath = substr($subPath, 0, $lastPos); + $search = $subPath.'\\'; + if (isset($this->prefixDirsPsr4[$search])) { + foreach ($this->prefixDirsPsr4[$search] as $dir) { + $length = $this->prefixLengthsPsr4[$first][$search]; if (file_exists($file = $dir . DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $length))) { return $file; } diff --git a/server/vendor/composer/LICENSE b/server/vendor/composer/LICENSE index 1a28124..f27399a 100644 --- a/server/vendor/composer/LICENSE +++ b/server/vendor/composer/LICENSE @@ -1,5 +1,5 @@ -Copyright (c) 2016 Nils Adermann, Jordi Boggiano +Copyright (c) Nils Adermann, Jordi Boggiano Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/server/vendor/composer/autoload_classmap.php b/server/vendor/composer/autoload_classmap.php index e6c8d48..8505a8b 100644 --- a/server/vendor/composer/autoload_classmap.php +++ b/server/vendor/composer/autoload_classmap.php @@ -91,6 +91,7 @@ return array( 'Interop\\Container\\Exception\\ContainerException' => $vendorDir . '/container-interop/container-interop/src/Interop/Container/Exception/ContainerException.php', 'Interop\\Container\\Exception\\NotFoundException' => $vendorDir . '/container-interop/container-interop/src/Interop/Container/Exception/NotFoundException.php', 'LookupServer\\BruteForceMiddleware' => $baseDir . '/lib/BruteForceMiddleware.php', + 'LookupServer\\Replication' => $baseDir . '/lib/Replication.php', 'LookupServer\\Status' => $baseDir . '/lib/Status.php', 'LookupServer\\UserManager' => $baseDir . '/lib/UserManager.php', 'LookupServer\\Validator\\Email' => $baseDir . '/lib/Validator/Email.php', diff --git a/server/vendor/composer/autoload_static.php b/server/vendor/composer/autoload_static.php index 11fc612..22ec6d1 100644 --- a/server/vendor/composer/autoload_static.php +++ b/server/vendor/composer/autoload_static.php @@ -173,6 +173,7 @@ class ComposerStaticInit509ee4e79733fbe3199b97373b795eca 'Interop\\Container\\Exception\\ContainerException' => __DIR__ . '/..' . '/container-interop/container-interop/src/Interop/Container/Exception/ContainerException.php', 'Interop\\Container\\Exception\\NotFoundException' => __DIR__ . '/..' . '/container-interop/container-interop/src/Interop/Container/Exception/NotFoundException.php', 'LookupServer\\BruteForceMiddleware' => __DIR__ . '/../..' . '/lib/BruteForceMiddleware.php', + 'LookupServer\\Replication' => __DIR__ . '/../..' . '/lib/Replication.php', 'LookupServer\\Status' => __DIR__ . '/../..' . '/lib/Status.php', 'LookupServer\\UserManager' => __DIR__ . '/../..' . '/lib/UserManager.php', 'LookupServer\\Validator\\Email' => __DIR__ . '/../..' . '/lib/Validator/Email.php', |