|
|
|
@ -1,92 +1,110 @@
@@ -1,92 +1,110 @@
|
|
|
|
|
<?php |
|
|
|
|
|
|
|
|
|
// Default response |
|
|
|
|
$response = [ |
|
|
|
|
'success' => false, |
|
|
|
|
'message' => _('Internal server error'), |
|
|
|
|
'profile' => [] |
|
|
|
|
]; |
|
|
|
|
|
|
|
|
|
// Access allowed for authorized users only |
|
|
|
|
if (isset($_SESSION['userName'])) { |
|
|
|
|
|
|
|
|
|
// Prepare user request, authorized user by default |
|
|
|
|
$userName = isset($_GET['userName']) ? Filter::userName($_GET['userName']) : $_SESSION['userName']; |
|
|
|
|
|
|
|
|
|
if ($profile = $_memcache->get('api.user.profile.' . $userName)) { |
|
|
|
|
// Check user exists in the database |
|
|
|
|
if ($userId = $_modelUser->getUserId($userName)) { |
|
|
|
|
|
|
|
|
|
$response = [ |
|
|
|
|
'success' => true, |
|
|
|
|
'message' => _('Profile successfully received from Cache'), |
|
|
|
|
'profile' => $profile |
|
|
|
|
]; |
|
|
|
|
/* |
|
|
|
|
* Step 1: try to obtain profile from cache |
|
|
|
|
* |
|
|
|
|
* */ |
|
|
|
|
if ($mcProfile = $_memcache->get('api.user.profile.' . $userName)) { |
|
|
|
|
|
|
|
|
|
} else if ($userProfileVersions = $_twister->getDHT($userName, 'profile', 's')) { |
|
|
|
|
$response = [ |
|
|
|
|
'success' => true, |
|
|
|
|
'message' => _('Profile successfully received from Cache'), |
|
|
|
|
'profile' => $mcProfile |
|
|
|
|
]; |
|
|
|
|
|
|
|
|
|
// Check user exists |
|
|
|
|
if ($userId = $_modelUser->getUserId($userName)) { |
|
|
|
|
/* |
|
|
|
|
* Step 2: try to obtain profile from DHT |
|
|
|
|
* |
|
|
|
|
* */ |
|
|
|
|
} else if ($dhtProfileRevisions = $_twister->getDHTProfileRevisions($userName)) { |
|
|
|
|
|
|
|
|
|
// Add DHT version if not exists |
|
|
|
|
foreach ($userProfileVersions as $userProfileVersion) { |
|
|
|
|
foreach ((array) $dhtProfileRevisions as $dhtProfileRevision) { |
|
|
|
|
|
|
|
|
|
// Save revision into the database if not exists |
|
|
|
|
if (!$_modelProfile->versionExists($userId, |
|
|
|
|
Filter::int($userProfileVersion['p']['height']), |
|
|
|
|
Filter::int($userProfileVersion['p']['seq']))) { |
|
|
|
|
|
|
|
|
|
$profile = $userProfileVersion['p']['v']; |
|
|
|
|
$dhtProfileRevision['height'], |
|
|
|
|
$dhtProfileRevision['seq'])) { |
|
|
|
|
|
|
|
|
|
$_modelProfile->add($userId, |
|
|
|
|
Filter::int($userProfileVersion['p']['height']), |
|
|
|
|
Filter::int($userProfileVersion['p']['seq']), |
|
|
|
|
Filter::int($userProfileVersion['p']['time']), |
|
|
|
|
|
|
|
|
|
isset($profile['fullname']) ? Filter::string($profile['fullname']) : '', |
|
|
|
|
isset($profile['bio']) ? Filter::string($profile['bio']) : '', |
|
|
|
|
isset($profile['location']) ? Filter::string($profile['location']) : '', |
|
|
|
|
isset($profile['url']) ? Filter::string($profile['url']) : '', |
|
|
|
|
isset($profile['bitmessage']) ? Filter::string($profile['bitmessage']) : '', |
|
|
|
|
isset($profile['tox']) ? Filter::string($profile['tox']) : ''); |
|
|
|
|
$dhtProfileRevision['height'], |
|
|
|
|
$dhtProfileRevision['seq'], |
|
|
|
|
$dhtProfileRevision['time'], |
|
|
|
|
|
|
|
|
|
$dhtProfileRevision['fullName'], |
|
|
|
|
$dhtProfileRevision['bio'], |
|
|
|
|
$dhtProfileRevision['location'], |
|
|
|
|
$dhtProfileRevision['url'], |
|
|
|
|
$dhtProfileRevision['bitMessage'], |
|
|
|
|
$dhtProfileRevision['tox']); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Get latest version available |
|
|
|
|
if ($profileInfo = $_modelProfile->get($userId)) { |
|
|
|
|
/* |
|
|
|
|
* Step 3: Select latest version available from DB revisions |
|
|
|
|
* |
|
|
|
|
* */ |
|
|
|
|
if ($dbProfileRevision = $_modelProfile->get($userId)) { |
|
|
|
|
|
|
|
|
|
// Format output |
|
|
|
|
$profile = [ |
|
|
|
|
'userName' => $userName, |
|
|
|
|
'fullName' => Filter::string($profileInfo['fullName']), |
|
|
|
|
'location' => Filter::string($profileInfo['location']), |
|
|
|
|
'url' => Filter::string($profileInfo['url']), |
|
|
|
|
'bitMessage' => Filter::string($profileInfo['bitMessage']), |
|
|
|
|
'tox' => Filter::string($profileInfo['tox']), |
|
|
|
|
'bio' => nl2br(Filter::string($profileInfo['bio'])), |
|
|
|
|
'fullName' => Format::text($dbProfileRevision['fullName']), |
|
|
|
|
'location' => Format::text($dbProfileRevision['location']), |
|
|
|
|
'url' => Format::text($dbProfileRevision['url']), |
|
|
|
|
'bitMessage' => Format::text($dbProfileRevision['bitMessage']), |
|
|
|
|
'tox' => Format::text($dbProfileRevision['tox']), |
|
|
|
|
'bio' => Format::text($dbProfileRevision['bio']), |
|
|
|
|
]; |
|
|
|
|
|
|
|
|
|
// Save request into the cache pool |
|
|
|
|
$_memcache->set('api.user.profile.' . $userName, $profile, MEMCACHE_COMPRESS, MEMCACHE_DHT_PROFILE_TIMEOUT); |
|
|
|
|
|
|
|
|
|
// Response |
|
|
|
|
$response = [ |
|
|
|
|
'success' => true, |
|
|
|
|
'message' => _('Profile successfully received from DHT'), |
|
|
|
|
'message' => _('Profile successfully received from DHT/DB'), |
|
|
|
|
'profile' => $profile |
|
|
|
|
]; |
|
|
|
|
|
|
|
|
|
$_memcache->set('api.user.profile.' . $userName, $profile, MEMCACHE_COMPRESS, MEMCACHE_DHT_PROFILE_TIMEOUT); |
|
|
|
|
|
|
|
|
|
// Cache, DHT, DB not contain any the details about user requested |
|
|
|
|
} else { |
|
|
|
|
|
|
|
|
|
$response = [ |
|
|
|
|
'success' => false, |
|
|
|
|
'message' => _('Profile data not available'), |
|
|
|
|
'message' => _('Could not receive any profile details'), |
|
|
|
|
'profile' => [] |
|
|
|
|
]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// User not found in the local database registry |
|
|
|
|
} else { |
|
|
|
|
|
|
|
|
|
$response = [ |
|
|
|
|
'success' => false, |
|
|
|
|
'message' => _('Could not receive profile details'), |
|
|
|
|
'message' => _('Requested user not found'), |
|
|
|
|
'profile' => [] |
|
|
|
|
]; |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Session expired response |
|
|
|
|
} else { |
|
|
|
|
|
|
|
|
|
$response = [ |
|
|
|
|